5.4 Importing Predicates into a Module

As explained before, in the predicate based approach adapted by SWI-Prolog, each module has its own predicate space. In SWI-Prolog, a module initially is completely empty. Predicates can be added to a module by loading a module file as demonstrated in the previous section, using assert or by importing them from another module.

Two mechanisms for importing predicates explicitly from another module exist. The use_module/[1,2] predicates load a module file and import (part of the) public predicates of the file. The import/1 predicate imports any predicate from any module.

use_module(+File)
Load the file(s) specified with File just like ensure_loaded/1. The files must all be module files. All exported predicates from the loaded files are imported into the context module. This predicate is equivalent to ensure_loaded/1, except that it raises an error if File is not a module file.
use_module(+File, +ImportList)
Load the file specified with File (only one file is accepted). File must be a module file. ImportList is a list of predicate indicators specifying the predicates that will be imported from the loaded module. If a predicate is specified that is not exported from the loaded module a warning will be printed. The predicate will nevertheless be imported to simplify debugging. ImportList also allows for renaming or import-everything-except. See also import option of load_files/2. The first example below loads member/2 from the lists library and append/2 under the name list_concat, which how this predicate is named in YAP. The second example loads a all exports from library option, except for meta_options/3. These renaming facilities are generally used to deal with portability issues with as few as possible changes to the actual code. See also section C and section 5.5.
:- use_module(library(lists), [ member/2,
                                append/2 as list_concat
                              ]).
:- use_module(library(option), except([meta_options/3])).
import(+PredicateIndicator)
Import predicate PredicateIndicator into the current context module. PredicateIndicator must specify the source module using the <module>:<pi> construct. Note that predicates are normally imported using one of the directives use_module/[1,2]. The import/1 alternative is meant for handling imports into dynamically created modules. See also export/1 and export_list/2.

It would be rather inconvenient to have to import each predicate referred to by the module, including the system predicates. For this reason each module is assigned a default module. All predicates in the default module are available without extra declarations. Their definition however can be overruled in the local module. This schedule is implemented by the exception handling mechanism of SWI-Prolog: if an undefined predicate exception is raised for a predicate in some module, the exception handler first tries to import the predicate from one of the module's import modules. On success, normal execution is resumed.

5.4.1 Reserved Modules

SWI-Prolog contains two special modules. The first one is the module system. This module contains all built-in predicates described in this manual. Module system has no default module assigned to it. The second special module is the module user. This module forms the initial working space of the user. Initially it is empty. The import module of module user is system, making all built-in predicate definitions available as defaults. Built-in predicates thus can be overruled by defining them in module user before they are used.

All other modules import from the module user. This implies they can use all predicates imported into user without explicitly importing them.