A module is a set of predicates with a name, so that two files can each define a predicate with the same name and arity without clashing. The standard ISO/IEC 13211-2 defines modules for Prolog, but most systems follow an older design that SICStus and Quintus introduced, and SWI-Prolog, YAP, GNU Prolog (not fully), XSB, Ciao and others each differ in details. A file that starts with :- module(Name, Exports). is a module file; a file without it adds its clauses to the module that loads it (often user).
Declaring and loading modules
| Directive | Meaning |
|---|---|
module(Name, Exports) | the file defines module Name; Exports is a list of name/arity (or name//arity) items, and op(P, T, N) terms that export operators; it must be the first term of the file |
use_module(File) | load the module file if it is not loaded, and import all its exports into the current module |
use_module(File, Imports) | import only the listed predicates; SWI-Prolog also accepts except(List) and Name/Arity as NewName |
reexport(File), reexport(File, Imports) | import and also export (SWI-Prolog, SICStus and others) |
ensure_loaded(File), consult(File), [File] | load a file that is not necessarily a module |
meta_predicate Specs | says which arguments of a predicate are goals or closures (0 to 9, :, ^, //), so that they are qualified with the module of the caller |
A file specification is an atom (a relative file name, with or without the extension), or an alias such as library(lists), library(clp/clpfd) or foreign(name) in SWI-Prolog; the aliases are defined by the system and the user (file_search_path/2). SICStus uses library(lists) too; GNU Prolog loads libraries as built-ins with no module system for them.
:- module(shapes, [area/2, perimeter/2, op(700, xfx, ===>)]).
:- use_module(library(lists), [sum_list/2]).
:- use_module(library(apply)).
:- meta_predicate total(2, +, -).
area(circle(R), A) :- A is pi * R * R.
area(square(S), A) :- A is S * S.
perimeter(square(S), P) :- P is 4 * S.
total(G, Shapes, T) :- maplist(G, Shapes, Parts), sum_list(Parts, T).
Qualification and visibility
A predicate of another module is called as Module:Goal, and the same prefix puts a clause into another module: lists:foo(X) :- .... The operator : has priority 200 in neither ISO text but 600 in the common table (see Terms and operators). Predicates that are not exported are visible only inside their module. When a module is not found, a call is looked up in the module user and then in system (SWI-Prolog); the flag unknown decides what happens when no definition is found.
Operators are local to a module as well in SWI-Prolog, which is why a module exports them in its list. In other systems an operator is global.
Libraries of the main systems
| System | What the library has |
|---|---|
| SWI-Prolog | lists, apply, aggregate, assoc, pairs, ordsets, option, error, readutil, dcg/basics, dicts, yall (lambda expressions), ugraphs, rbtrees, heaps, clpfd, chr, tabling, solution_sequences, strings, plunit, pldoc and many more, including packages for HTTP, SGML and XML, RDF and databases |
| SICStus Prolog | lists, assoc, ordsets, ugraphs, clpfd, chr, sockets, codesio, terms and others |
| GNU Prolog | finite domain constraints and a set of list and assoc predicates built in, with no module system |
| Scryer Prolog | lists, between, dcgs, format, clpz, clpb, assoc, charsio, reif and others in library(...) |
| YAP, XSB, ECLiPSe, Trealla, Ciao | each has its own library set; many of them provide lists, apply and assoc with the same names, and compatibility layers for SWI-Prolog and SICStus |
The viewer knows two libraries by name, library(clpfd) and library(chr), because they declare operators; loading either adds those operators to the rest of the file.
Not described
Known, not yet covered
- Reference: ISO/IEC 13211-2 is described in outline only; its
import/1,use_module/1,2,export/1,reexportconventions and the module-related error terms are not written out, and the standard text is not freely available. - Reference: the module systems of SICStus, XSB, Ciao and ECLiPSe (hierarchical modules,
exportandimportdirectives, assertions in Ciao) differ and are not described one by one. - Reference: the contents of each library are not listed predicate by predicate.
- Viewer: the viewer lists a file's module, exports, imports and loaded files, but does not load them, so it does not know what an imported module defines or which operators it exports.