Home Contents Index Summary Previous Next

2.12 Automatic loading of libraries

If ---at runtime--- an undefined predicate is trapped the system will first try to import the predicate from the module's default module. If this fails the auto loader is activated. On first activation an index to all library files in all library directories is loaded in core (see library_directory/1). If the undefined predicate can be located in the one of the libraries that library file is automatically loaded and the call to the (previously undefined) predicate is resumed. By default this mechanism loads the file silently. The feature/2 verbose_autoload is provided to get verbose loading. The feature autoload can be used to enable/disable the entire auto load system.

The auto-loader only works if the unknown flag (see unknown/2) is set to trace (default). A more appropriate interaction with this flag will be considered.

Autoloading only handles (library) source files that use the module mechanism described in chapter 4. The files are loaded with use_module/2 and only the trapped undefined predicate will be imported to 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 format:


index(Name, Arity, Module, File).

The predicate make/0 scans the autoload libraries and updates the index if it exists, is writable and out-of-date. It is advised to create an empty file called INDEX.pl in a library directory meant for auto loading before doing anything else. This index file can then be updated by running the prolog make_library_index/1 (`%' is the Unix prompt):


% mkdir ~/lib/prolog
% cd !$
% pl -g true -t 'make_library_index(.)'

If there are more than one library files containing the desired predicate the following search schema is followed:

  1. If there is a library file that defines the module in which the undefined predicate is trapped, this file is used.
  2. Otherwise library files are considered in the order they appear in the library_directory/1 predicate and within the directory alphabetically.

make_library_index(+Directory)
Create an index for this directory. The index is written to the file 'INDEX.pl' in the specified directory. Fails with a warning if the directory does not exist or is write protected.