Go to the first, previous, next, last section, table of contents.


The module system

Author(s): Daniel Cabeza and the CLIP Group.

Version: 1.10#7 (2006/4/26, 19:22:13 CEST)

Version of last change: 1.9#28 (2002/11/20, 14:3:5 CET)

Modularity is a basic notion in a modern computer language. Modules allow dividing programs in several parts, which have its own independent name spaces. The module system in Ciao [CH00a], as in many other Prolog implementations, is procedure based. This means that predicate names are local to a module, but functor/atom names in data are shared.

The predicates visible in a module are the predicates defined in that module, plus the predicates imported from other modules. Only predicates exported by a module can be imported from other modules. The default module of a given predicate name is the local one if the predicate is defined locally, else the last module from which the predicate is imported, having explicit imports priority (that is, a predicate imported by an use_module/2 declaration is always preferred above a predicate imported by an use_module/1 declaration). To refer to a predicate from a module which is not the default for that predicate the name has to be module qualified. A module qualified predicate name has the form Module:Predicate as in the call debugger:debug_module(M). Note that this does not allow having access to predicates not imported, nor defining clauses of other modules.

All predicates defined in files with no module declaration belong to a special module called user, and all are implicitly exported. This allows dividing programs in several files without being aware of the module system at all. Note that this feature is only supported for compatibility reasons, being its use discouraged. Many attractive compilation features of Ciao cannot be performed in user modules.

The case of multifile predicates (defined with the declaration multifile/1) is also special. Multifile predicates can be defined by clauses distributed in several modules, and all modules which define a predicate as multifile can use that predicate. The name space of multifile predicates is independent, as if they belonged to special module multifile.

Every user or module file imports implicitly a number of modules called builtin modules. They are imported before all other importations of the module, allowing thus redefining any of their predicates (with the exception of true/0) by defining local versions or importing them from other modules. Importing explicitly from a builtin module, however, disables the implicit importation of the rest (this feature is used by package library(pure) to define pure prolog code).

Usage and interface (modules)

Documentation on internals (modules)

DECLARATION: module/3:

Usage: :- module(Name, Exports, Packages).

DECLARATION: module/2:

Usage: :- module(Name, Exports).

DECLARATION: export/1:

Usage 1: :- export(Pred).

Usage 2: :- export(Exports).

DECLARATION: use_module/2:

Usage: :- use_module(Module, Imports).

DECLARATION: use_module/1:

Usage: :- use_module(Module).

DECLARATION: import/2:

Usage: :- import(Module, Imports).

DECLARATION: reexport/2:

Usage: :- reexport(Module, Preds).

DECLARATION: reexport/1:

Usage: :- reexport(Module).

DECLARATION: meta_predicate/1:

Usage: :- meta_predicate MetaSpecs.

REGTYPE: modulename/1:

A module name is an atom, not containing characters `:' or `$'. Also, user and multifile are reserved, as well as the module names of all builtin modules (because in an executable all modules must have distinct names).

Usage: modulename(M)

REGTYPE: metaspec/1:

A meta-predicate specification for a predicate is the functor of that predicate applied to atoms which represent the kind of module expansion that should be done with the arguments. Possible contents are represented as:

goal
This argument will be a term denoting a goal (either a simple or complex one) which will be called. For commpatibility reasons it can be named as : as well.
clause
This argument will be a term denoting a clause.
fact
This argument should be instantiated to a term denoting a fact (head-only clause).
spec
This argument should be instantiated to a predicate name, as Functor/Arity.
pred(N)
This argument should be instantiated to a predicate construct to be called by means of a call/N predicate call (see call/2).
addmodule
This is in fact is not a real meta-data specification. It specifies that in an argument added after this one will be passed the calling module, to allow handling more involved meta-data (e.g., lists of goals) by using conversion builtins.
?,+,-,_
These other values denote that this argument is not module expanded.

Usage: metaspec(M)


Go to the first, previous, next, last section, table of contents.