If ---at runtime--- an undefined predicate is trapped, the system will first try to import the predicate from the module's default module (see section 6.9. If this fails the auto loader is activated.20Actually, the hook user:exception/3 is called; only if this hook fails does it call the autoloader. On first activation an index to all library files in all library directories is loaded in core (see library_directory/1, file_search_path/2 and reload_library_index/0). If the undefined predicate can be located in one of the libraries, that library file is automatically loaded and the call to the (previously undefined) predicate is restarted. By default this mechanism loads the file silently. The current_prolog_flag/2 key verbose_autoload is provided to get verbose loading. The Prolog flag autoload can be used to enable/disable the autoload system.
Autoloading only handles (library) source files that use the module
mechanism described in chapter
6. The files are loaded with use_module/2
and only the trapped undefined predicate is imported into the module
where the undefined predicate was called. Each library directory must
hold a file
INDEX.pl that contains an index to all library
files in the directory. This file consists of lines of the following
index(Name, Arity, Module, File).
The predicate make/0
updates the autoload index. It searches for all library directories (see library_directory/1
holding the file
the current user can write or create the file
it does not exist or is older than the directory or one of its files,
the index for this directory is updated. If the file
exists, updating is achieved by loading this file, normally containing a
is called, creating an index for all
*.pl files containing
Below is an example creating an indexed library directory.
% mkdir ~/lib/prolog % cd ~/lib/prolog % swipl -g true -t 'make_library_index(.)'
If there is more than one library file containing the desired predicate, the following search schema is followed:
autoloadand reloads the library index. For example:
If this call appears as a directive, it is term-expanded into a clause for user:file_search_path/2 and a directive calling reload_library_index/0. This keeps source information and allows for removing this directive.
MKINDEX.pl, this predicate creates
INDEX.plfor Directory, indexing all files that match one of the file patterns in ListOfPatterns.
Sometimes library packages consist of one public load file and a
number of files used by this load file, exporting predicates that should
not be used directly by the end user. Such a library can be placed in a
sub-directory of the library and the files containing public
functionality can be added to the index of the library. As an example we
give the XPCE library's
MKINDEX.pl, including the public
trace/browse.pl to the autoloadable
predicates for the XPCE package.
:- make_library_index('.', [ '*.pl', 'trace/browse.pl' ]).
INDEX.plfiles. This predicate does not update the
INDEX.plfiles. Check make_library_index/[1,2] and make/0 for updating the index files.
Normally, the index is reloaded automatically if a predicate cannot be found in the index and the set of library directories has changed. Using reload_library_index/0 is necessary if directories are removed or the order of the library directories is changed.
When creating an executable using either qsave_program/2 or the -c command line options, it is necessarry to load all predicates that would normally be autoloaded explicitly. This is discussed in section 11. See autoload/0.