The module system

Author(s): Daniel Cabeza, The CLIP Group.

Modularity is a basic notion in a modern computer language. Modules allow dividing programs into several parts, which have their own independent name spaces. The module system in Ciao [CH00a] 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).

Visibility rules

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.

Files with no mudule declaration ('user' files)

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.

Multifile predicates

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.

Libraries imported by default ('builtins')

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.

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.

Importing explicitly from a builtin module, however, disables the implicit importation of the rest of the builtin modules that would be otherwise loaded (this feature is used for example by package library(pure) to define pure modules that do not import any traditional Prolog builtins; i.e., pure Prolog code).


Usage and interface

Documentation on internals

DECLARATION

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

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.

  • The following properties hold at call time:
    (modules:modulename/1)Name is a module name (an atom).
    (basic_props:list/2)Exports is a list of prednames.
    (basic_props:list/2)Packages is a list of sourcenames.

DECLARATION

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

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.

  • The following properties hold at call time:
    (modules:modulename/1)Name is a module name (an atom).
    (basic_props:list/2)Exports is a list of prednames.

DECLARATION

(True) Usage::- package(Name).

Declares a package of name Name. Like in modules, Name must match with the name of the file where the package resides, without extension. This directive must appear the first in the file.

Package files provide syntactic extensions and their related functionalities by defining operators, new declarations, code translations, etc., as well as declaring imports from other modules and defining additional code. Most Ciao syntactic and semantic extensions, such as functional syntax, constraint solving, or breadth-first search are implemented as packages.

  • The following properties hold at call time:
    (modules:modulename/1)Name is a module name (an atom).

DECLARATION

(True) Usage 1::- export(Pred).

Adds Pred to the set of exported predicates.

  • The following properties hold at call time:
    (basic_props:predname/1)Pred is a Name/Arity structure denoting a predicate name:
    predname(P/A) :-
            atm(P),
            nnegint(A).
    

(True) Usage 2::- export(Exports).

Adds Exports to the set of exported predicates.

  • The following properties hold at call time:
    (basic_props:list/2)Exports is a list of prednames.

DECLARATION

(True) Usage::- use_module(Module,Imports).

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.

  • The following properties hold at call time:
    (streams_basic:sourcename/1)Module is a source name.
    (basic_props:list/2)Imports is a list of prednames.

DECLARATION

(True) Usage::- use_module(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.

  • The following properties hold at call time:
    (streams_basic:sourcename/1)Module is a source name.

DECLARATION

(True) Usage::- import(Module,Imports).

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).

  • The following properties hold at call time:
    (modules:modulename/1)Module is a module name (an atom).
    (basic_props:list/2)Imports is a list of prednames.

DECLARATION

(True) Usage::- reexport(Module,Preds).

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 .

  • The following properties hold at call time:
    (streams_basic:sourcename/1)Module is a source name.
    (basic_props:list/2)Preds is a list of prednames.

DECLARATION

(True) Usage::- reexport(Module).

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 .

  • The following properties hold at call time:
    (streams_basic:sourcename/1)Module is a source name.

DECLARATION

(True) 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.

  • The following properties hold at call time:
    (basic_props:sequence/2)MetaSpecs is a sequence of metaspecs.

REGTYPE
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).

    REGTYPE
    A meta-predicate specification for a predicate is the functor of that predicate applied to terms which represent the kind of module expansion that should be applied to each argument. Possible contents are represented as:

    ?,+,-,_
    These values denote that this argument is not module expanded.

    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).

    list(Meta)
    This argument should be instantiated to a list of terms as described by Meta (e.g. list(goal)).

    addterm(Meta)
    This argument should be instantiated to the meta-data specified by Meta, and an argument added after this one will carry the original data without module expansion. Not intended to be used by normal users.

    addmodule(Meta)
    This argument should be instantiated to the meta-data specified by Meta, and in an argument added after this one will be passed the calling module, for example to allow handling more involved meta-data by using conversion builtins. addmodule is an alias of addmodule(?). Not intended to be used by normal users.

    Usage:metaspec(M)

    M is a meta-predicate specification.