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).
modules
)modules
)
Usage: :- module(Name, Exports, Packages)
.
Name
which exports the predicates in Exports
, and uses the packages in Packages
. Name
must match with the name of the file where the module resides, without extension. For each source in Packages
, a
package file is included, as if by an
include/1
declaration. If the source is specified with a
path alias, this is the file included, if it is an atom, the library paths are searched. Package files provide functionalities by declaring imports from other modules, defining operators, new declarations, translations of code, etc.
This directive must appear the first in the file.
Also, if the compiler finds an unknown declaration as the first term in a file, the name of the declaration is regarded as a package library to be included, and the arguments of the declaration (if present) are interpreted like the arguments of
module/3
.
Name
is a module name (an atom).
(modules:modulename/1
)
Exports
is a list of predname
s.
(basic_props:list/2
)
Packages
is a list of sourcename
s.
(basic_props:list/2
)
Usage: :- module(Name, Exports)
.
Usage 1: :- export(Pred)
.
Pred
to the set of exported predicates.
Pred
is a Name/Arity structure denoting a predicate name:
predname(P/A) :- atm(P), nnegint(A).(
basic_props:predname/1
)
Usage 2: :- export(Exports)
.
Exports
to the set of exported predicates.
Exports
is a list of predname
s.
(basic_props:list/2
)
Usage: :- use_module(Module, Imports)
.
Module
the predicates in Imports
. The imported predicates must be exported by the other module.
Module
is a source name.
(streams_basic:sourcename/1
)
Imports
is a list of predname
s.
(basic_props:list/2
)
Usage: :- use_module(Module)
.
Module
all the predicates exported by it. The previous version with the explicit import list is preferred to this as it minimizes the chances to have to recompile this code if the other module changes.
Module
is a source name.
(streams_basic:sourcename/1
)
Usage: :- import(Module, Imports)
.
Module
the predicates in Imports
.
Important note: this declaration is intended to be used when the current module or the imported module is going to be dynamically loaded, and so the compiler does not include the code of the imported module in the current executable (if only because the compiler cannot know the location of the module file at the time of compilation). For the same reason the predicates imported are not checked to be exported by Module
. Its use in other cases is strongly discouraged, as it disallows many compiler optimizations.
Module
is a module name (an atom).
(modules:modulename/1
)
Imports
is a list of predname
s.
(basic_props:list/2
)
Usage: :- reexport(Module, Preds)
.
Module
the predicates in Preds
. This implies that this module imports from the module defined in Module
the predicates in Preds
, an also that this module exports the predicates in Preds
.
Module
is a source name.
(streams_basic:sourcename/1
)
Preds
is a list of predname
s.
(basic_props:list/2
)
Usage: :- reexport(Module)
.
Module
all the predicates exported by it. This implies that this module imports from the module defined in Module
all the predicates exported by it, an also that this module exports all such predicates .
Module
is a source name.
(streams_basic:sourcename/1
)
Usage: :- meta_predicate MetaSpecs
.
MetaSpecs
have arguments which represent predicates and thus have to be module expanded. The directive is only mandatory for exported predicates (in modules). This directive is defined as a prefix operator in the compiler.
MetaSpecs
is a sequence of metaspec
s.
(basic_props:sequence/2
)
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)
M
is a module name (an atom).
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
:
as well.
clause
fact
spec
pred(N)
call/N
predicate call (see
call/2
).
addmodule
?,+,-,_
Usage: metaspec(M)
M
is a meta-predicate specification.
Go to the first, previous, next, last section, table of contents.