SWI-Prolog by default support autoloading from its standard library. Autoloading implies that when a predicate is found missing during execution the library is searched and the predicate is imported lazily using use_module/2. See section 2.14 for details.
The advantage of autoloading is that it requires less typing while it
reduces the startup time and reduces the memory footprint of an
application. It also allows moving old predicates or emulation thereof
the module library(backcomp)
without affecting existing
code. This procedure keeps the libraries and system clean. We make sure
that there are not two modules that provide the same predicate as
autoload predicate.
Nevertheless, a disadvantage of this autoloader is that the dependencies of a module on the libraries are not explicit and tooling such as PceEmacs or gxref/0 are required to find these dependencies. Some users want explicit control over which library predicates are accessed from where, preferably by using use_module/2 which explicitly states which predicates are imported from which library.183Note that built-in predicates still add predicates for general use to all name spaces.
Large applications typically contain source files that are not immediately needed and often are not needed at all in many runs of the program. This can be solved by creating an application-specific autoload library, but with multiple parties providing autoloadable predicates the maintenance becomes fragile. For these two reasons we added autoload/1 and autoload/2 that behave similar to use_module/[1,2], but do not perform the actual loading. The generic autoloader now proceeds as follows if a missing predicate is encountered:
true
.
Predicates made available using autoload/2 behave as defined predicates, which implies that any operation on them will perform autoloading if necessary. Notably predicate_property/2, current_predicate/1 and clause/2 are supported.
Currently, neither the existence of File, nor whether it actually exports the given predicates (autoload/2) is verified when the file is loaded. Instead, the declarations are verified when searching for a missing predicate.
If the Prolog flag autoload
is set to false
, these declarations are interpreted as use_module/[1,2].