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:

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


use_module(:F,+I)
import the procedure(s) I from the module-file F, loading module-file F if necessary
use_module(?M,:F,+I)
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 or its meta-predicate declaration (see ref-mod-met) 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 or whose module name expansion requirements have changed. 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 do not. Whenever the possibility of such inconsistency arises, you will be warned at the end of the load that certain modules need to be reloaded. This warning will be repeated at the end of each subsequent load until those modules have been reloaded.

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


Send feedback on this subject.