4.11.5 Loading a Module

To gain access to the public predicates of a module-file, load it as you would any other file—using compile/1, or ensure_loaded/1 as appropriate. For example, if your code contains a directive such as

     :- ensure_loaded(File).

this directive will load the appropriate file File whether or not File is a module-file. The only difference is that if File is a module-file any private predicates that it defines will not be visible to your program.

The load predicates are adequate for use at Prolog's top-level, or when the file being loaded is a utility such as a library file. When you are writing modules of your own, use_module/[1,2,3] is the most useful.

The following predicates are used to load modules:

import the module-file(s) F, loading them if necessary; same as ensure_loaded(F) if all files in F are module-files

import the procedure(s) I from the module-file F, loading module-file F if necessary
import I from module M, loading module-file F if necessary

Before a module-file is loaded, the associated module is reinitialized: any predicates previously imported into or defined in that module are forgotten by the module.

If a module of the same name with a different PublicPredList or different meta-predicate list has previously been loaded from a different module-file, a warning is printed and you are given the option of abandoning the load. Only one of these two modules can exist in the system at one time.

Normally, a module-file can be reloaded after editing with no need to reload any other modules. However, when a module-file is reloaded after its PublicPredList has been changed, any modules that import predicates from it may have become inconsistent. This is because a module is associated with a predicate at compile time, rather than run time. Thus, other modules may refer to predicates in a module-file that are no longer public. In the case of module-importation (where all, rather than specific, public predicates of a module are imported), it is possible that some predicates in the importing module should now refer to a newly-public predicate but don't. SICStus Prolog tries to detect such inconsistencies, and issues a warning when it does detect one. Similarly, if a meta-predicate declaration of an exported predicate changes, modules that have already imported that predicate become inconsistent, because module name expansion requirements have changed. The current release of SICStus Prolog is unable to detect such inconsistencies.

Modules may be saved to a PO file by calling save_modules(Modules,File) (see ref-sls).

Send feedback on this subject.