5 Modules

A Prolog module is a collection of predicates which defines a public interface by means of a set of provided predicates and operators. Prolog modules are defined by an ISO standard. Unfortunately, the standard is considered a failure and, as far as we are aware, not implemented by any concrete Prolog implementation. The SWI-Prolog module system syntax is derived from the Quintus Prolog module system. The Quintus module system has been the starting point for the module systems of a number of mainstream Prolog systems, such as SICStus, Ciao and YAP. The underlying primitives of the SWI-Prolog module system differs from the mentioned systems. These primitives allow for multiple modules in a file, hierarchical modules, emulation of other modules interfaces, etc.

This chapter motivates and describes the SWI-Prolog module system. Novices can start using the module system after reading section 5.2 and section 5.3. The primitives defined in these sections suffice for basic usage until one needs to export predicates that call or manage other predicates dynamically (e.g., use call/1, assert/1, etc.). Such predicates are called meta predicates and are discussed in section 5.4. Section 5.5 to section 5.8 describe more advanced issues. Starting with section 5.9, we discuss more low-level aspects of the SWI-Prolog module systems that are used to implement the visible module system, and can be used to build other code reuse mechanisms.


Section Index


5.1 Why Use Modules?
5.2 Defining a Module
5.3 Importing Predicates into a Module
5.4 Defining a meta-predicate
5.5 Overruling Module Boundaries
5.5.1 Explicit manipulation of the calling context
5.6 Interacting with modules from the toplevel
5.7 Composing modules from other modules
5.8 Operators and modules
5.9 Dynamic importing using import modules
5.10 Reserved Modules and using the `user' module
5.11 An alternative import/export interface
5.12 Dynamic Modules
5.13 Transparent predicates: definition and context module
5.14 Module properties
5.15 Compatibility of the Module System