6 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 differ 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 6.2 and section 6.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 6.5. Section 6.6 to section 6.9 describe more advanced issues. Starting with section 6.10, we discuss more low-level aspects of the SWI-Prolog module system that are used to implement the visible module system, and can be used to build other code reuse mechanisms.


Section Index


6.1 Why Use Modules?
6.2 Defining a Module
6.3 Importing Predicates into a Module
6.4 Controlled autoloading for modules
6.5 Defining a meta-predicate
6.6 Overruling Module Boundaries
6.6.1 Explicit manipulation of the calling context
6.7 Interacting with modules from the top level
6.8 Composing modules from other modules
6.9 Operators and modules
6.10 Dynamic importing using import modules
6.11 Reserved Modules and using the‘user' module
6.12 An alternative import/export interface
6.13 Dynamic Modules
6.14 Transparent predicates: definition and context module
6.15 Module properties
6.16 Compatibility of the Module System