Modularity is a basic notion in a modern computer language. Modules allow dividing programs into several parts, which have their own independent name spaces. Each module is written in its own file (see module/2 and module/3) and consists of a sequence of directives and predicate definitions.
Modules provide functionality to other modules by exporting some of the predicates defined inside the module (and also through multifile predicates). However, a module does not modify the syntax that can be used in another module that loads it. This is done instead through the mechanism of packages (see Packages and language extension).
See [CH00a] for a detailed description of the Ciao module system.
The module system in Ciao is, as in most Prolog implementations, procedure based. This means that predicate names are local to a module, but functor/atom names in data are shared (at least by default).
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, where explicit imports have priority over implicit ones (that is, a predicate imported through a use_module/2 declaration is always preferred over a predicate imported through a use_module/1 declaration). To refer to a predicate from a module which is not the default module 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 in Ciao this module qualification cannot be used for gaining access to predicates that have not been imported, nor for defining clauses of other modules.
All predicates defined in files with no module declaration belong to a special module called user, from which they are all implicitly exported. This provides backward compatibility for programs written for Prolog implementations with no module system and allows dividing programs into several files without being aware of the module system at all. Note that this feature is only supported for the above-mentioned backward-compatibility reasons, and the use of user files is discouraged. Many attractive compilation features of Ciao cannot be supported for 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 the special module multifile.
Unlike in other Prolog systems, directives in Ciao are not goals to be executed by the compiler or top level. Instead, they are read and acted upon by these programs. The advantage of this is that the effect of the directives is consistent for executables, code loaded in the top level, code analyzed by the preprocessor, etc.
As a result, by default only the builtin directives or declarations defined in this section are available in user programs. However, it is possible to define new declarations using the new_declaration/1 and new_declaration/2 directives (or using packages including them). Also, packages may define new directives via code translations.
While in Ciao there are no 'built-in' predicates (i.e., predicates whose load cannot be avoided or that that cannot be redefined --see below) for convenience every module or user file imports implicitly a number of modules called builtin modules (also referred to as default modules). Which exact modules are imported by default is controlled by the third argument of module/3 declarations, the lack thereof in module/2 declarations, some rules for user files, etc., as described below. For example, for backward compatibility with traditional Prolog systems, if a module/2 declaration is used, then the traditional predicates that are built in in most Prolog systems are imported in that module (see Classic Prolog).
Predicates coming from builtin/default modules are imported before all other importations of the module. This allows the redefinition of builtins, i.e., the redefinition of any of the predicates imported by default from builtin/default modules (with the exception of true/0) by either defining local versions of these predicates or by importing them from other modules.
Moreover, the implicit importation of the basic modules can be fully disabled by some special packages (for example, omitting all default imports with noprelude, or defining pure Prolog modules with the pure package).
M is a module name (an atom).
Declares a module of name 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 used. If the source is specified with a path alias, this is the file included, if it is an atom, the library paths are searched. See package/1 for a brief description of package files.
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.
Same as directive module/3, with an implicit package default. This default package provides all the standard features provided by most Prolog systems so that Prolog programs with traditional module/2 declarations can run without any change. See Classic Prolog.
This directive must appear the first in the file, or just after a module/3 declaration. A file with no module declaration, in the absence of this directive, uses an implicit package default (see Other predicates and features defined by default).
Usage 1::- use_package(Package).
Usage 2::- use_package(Package).
Specifies that this code imports from the module defined in Module the predicates in Imports. The imported predicates must be exported by the other module.
Specifies that this code imports from the module defined in 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.
Declares that this code imports from the module with name 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.
This is an example of such a case for a dynamically loaded module:
:- module(_,_). :- import(bar,[b/1]). main(X) :- use_module(bar), b(X).
Specifies that this code reexports from the module defined in 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 .
Specifies that this code reexports from the module defined in 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 .
Specifies that the code present in File will be included in the executable being prepared, in the user module. The file File cannot have a module declaration. This directive is intended to be used by programs not divided in modules. Dividing programs into modules is however strongly encouraged, since most of the attractive features of Ciao (such as static debugging and global optimization) are only partially available for user modules.
The contents of the file File are included in the current program text exactly as if they had been written in place of this directive.
Usage 1::- export(Pred).
Adds Pred to the set of exported predicates.
Usage 2::- export(Exports).
Adds Exports to the set of exported predicates.
Usage:ISO:- multifile Predicates.
Specifies that each predicate in Predicates may have clauses in more than one file. Each file that contains clauses for a multifile predicate must contain a directive multifile for the predicate. The directive should precede all clauses of the affected predicates, and also dynamic/data declarations for the predicate. This directive is defined as a prefix operator in the compiler.
Usage::- meta_predicate MetaSpecs.
Specifies that the predicates in MetaSpecs have arguments which have to be module expanded (predicates, goals, etc). meta_predicate/1 directives are only mandatory for exported predicates (in modules). This directive is defined as a prefix operator in the compiler.
Specifies that this module redefines predicate Predicate, also imported from other module, or imports it from more than one module. This prevents the compiler giving warnings about redefinitions of that predicate. Predicate can be partially (or totally) uninstantiated, to allow disabling those warnings for several (or all) predicates at once.
Usage:ISO:- discontiguous Predicates.
Specifies that each predicate in Predicates may be defined in this file by clauses which are not in consecutive order. Otherwise, a warning is signaled by the compiler when clauses of a predicate are not consecutive (this behavior is controllable by the prolog flag discontiguous_warnings). The directive should precede all clauses of the affected predicates. This directive is defined as a prefix operator in the compiler.
Specifies that each predicate in Predicates is implicitly defined in the current prolog source, either because it is a builtin predicate or because it is defined in a C file. Otherwise, a warning is signaled by the compiler when an exported predicate is not defined in the module or imported from other module.
M is a meta-predicate specification.
Goal will be executed at the start of the execution of any program containing the current code. The initialization of a module/file never runs before the initializations of the modules from which the module/file imports (excluding circular dependencies).
Goal will be executed after an abort of the execution of any program containing the current code.