Node:Read In, Next:, Up:Input Output

Reading-in Programs

When the predicates discussed in this section are invoked, file specifications are treated as relative to the current working directory. While loading code, however, file specifications are treated as relative to the directory containing the file being read in. This has the effect that if one of these predicates is invoked recursively, the file specification of the recursive load is relative to the directory of the enclosing load. See Load Intro, for an introduction to these predicates.

Directives will be executed in order of occurrence. Be aware of the rules governing relative file specifications, as they could have an effect on the semantics of directives. Only the first solution of directives is produced, and variable bindings are not displayed. Directives that fail or raise exceptions give rise to warning or error messages, but do not terminate the load. However, these warning or error messages can be intercepted by the hook user:portray_message/2, which can call abort/0 to terminate the load, if that is the desired behavior.

Predicates loading source code are affected by the character-conversion mapping, cf. char_conversion/2; see Term I/O.

Most of the predicates listed below take an argument Files, which is a single file specification or a list of file specifications. Source, object and QL files usually end with a .pl, .po and .ql suffix respectively. These suffixes are optional. Each file specification may optionally be prefixed by a module name. The module name specifies where to import the exported predicates of a module-file, or where to store the predicates of a non-module-file. The module is created if it doesn't exist already.

absolute_file_name/3 (see Stream Pred) is used for resolving the file specifications. The file specification user is reserved and denotes the standard input stream.

These predicates are available in runtime systems with the following limitations:

load_files(:Files, +Options)

A generic predicate for loading the files specified by Files with a list of options to provide extra control. This predicate in fact subsumes the other predicates except use_module/3, which also returns the name of the loaded module, or imports a set of predicates from an existing module. Options is a list of zero or more of the following:

true (the default) to always load, or changed to load only if the file has not yet been loaded or if it has been modified since it was last loaded. A non-module-file is not considered to have been previously loaded if it was loaded into a different module. The file user is never considered to have been previously loaded.
always (the default) to always load, or compile_time to load only if the goal is not in the scope of another load_files/[1,2] directive occurring in a .po or .ql file.

The latter is intended for use when the file only defines predicates that are needed for proper term or goal expansion during compilation of other files.

source to load source files only, object to load object (.po) files only, ql (obsolescent) to load .ql files only, or latest (the default) to load any type of file, whichever is newest. If the file is user, source is forced.
all (the default) to import all exported predicates if the file is a module-file, or a list of predicates to import.
compile to translate into compiled code, consult to translate into static, interpreted code, or assert_all to translate into dynamic, interpreted code.

The default is the compilation mode of any ancestor load_files/[1,2] goal, or compile otherwise. Note that Mode has no effect when a .po or .ql file is loaded, and that it is recommended to use assert_all in conjunction with load_type(source), to ensure that the source file will be loaded even in the presence of a .po or .ql file.

To pass the term Wcx to the wide character extension component; see Prolog Level WCX Features.

reconsult(:Files) obsolescent

Consults the source file or list of files specified by File and Files. Same as load_files(Files, [load_type(source),compilation_mode(consult)]).


Compiles the source file or list of files specified by Files. The compiled code is placed in-core, i.e. is added incrementally to the Prolog database. Same as load_files(Files, [load_type(source),compilation_mode(compile)]).

load(:Files) obsolescent

Loads the .ql file or list of files specified by Files. Same as load_files(Files, [load_type(ql)]).

ensure_loaded(:Files) ISO

Compiles or loads the file or files specified by Files that have been modified after the file was last loaded, or that have not yet been loaded. The recommended style is to use this predicate for non-module-files only, but if any module-files are encountered, their public predicates are imported. Same as load_files(Files, [if(changed)]).


Compiles or loads the module-file specified by File if it has been modified after it was last loaded, or not yet been loaded. Its public predicates are imported. The recommended style is to use this predicate for module-files only, but any non-module-files encountered are simply compiled or loaded. Same as load_files(File, [if(changed)]).

use_module(:File, +Imports)
Loads the module-file File like ensure_loaded/1 and imports the predicates in Imports. If any of these are not public, a warning is issued. Imports may also be set to the atom all in which case all public predicates are imported. Same as load_files(File, [if(changed),imports(Imports)]).
use_module(-Module, :File, +Imports)
use_module(+Module, :File, +Imports)
If used with +Module, and that module has already been loaded, this merely imports Imports from that module. Otherwise, this is equivalent to use_module(File, Imports) with the addition that Module is unified with the loaded module.
fcompile(:Files) development,obsolescent

Compiles the source file or list of files specified by Files. If Files are prefixed by a module name, that module name will be used for module name expansion during the compilation (see Considerations). The suffix .pl is added to the given file names to yield the real source file names. The compiled code is placed on the .ql file or list of files formed by adding the suffix .ql to the given file names.


File is the absolute name of a source file currently in the system.

Head is the most general goal for a predicate loaded from File.
require(:PredSpecOrSpecs) development

PredSpecOrSpecs is a predicate spec or a list or a conjunction of such. The predicate will check if the specified predicates are loaded and if not, will try to load or import them using use_module/2. The file containing the predicate definitions will be located in the following way: