Example for when you want to call main
of your source file immediately after the source file has been loaded (from swipl-devel/man/select.pl
):
:- initialization ( catch(main, E, ( print_message(error, E), fail )) -> halt ; halt(1) ).
Did you know ... | Search Documentation: |
Predicate initialization/1 |
The ISO standard only allows for using :- Term
if Term
is a
directive. This means that arbitrary goals can only be called
from a directive by means of the initialization/1
directive. SWI-Prolog does not enforce this rule.
The initialization/1 directive must be used to do program initialization in saved states (see qsave_program/1). A saved state contains the predicates, Prolog flags and operators present at the moment the state was created. Other resources (records, foreign resources, etc.) must be recreated using initialization/1 directives or from the entry goal of the saved state.
Up to SWI-Prolog 5.7.11, Goal was executed immediately
rather than after loading the program text in which the directive
appears as dictated by the ISO standard. In many cases the exact moment
of execution is irrelevant, but there are exceptions. For example,
load_foreign_library/1
must be executed immediately to make the loaded foreign predicates
available for exporting. SWI-Prolog now provides the directive use_foreign_library/1
to ensure immediate loading as well as loading after restoring a saved
state. If the system encounters a directive :-
initialization(load_foreign_library(...))
, it will load the
foreign library immediately and issue a warning to update your code.
This behaviour can be extended by providing clauses for the multifile
hook predicate prolog:initialize_now(Term, Advice)
, where Advice
is an atom that gives advice on how to resolve the compatibility issue.
Example for when you want to call main
of your source file immediately after the source file has been loaded (from swipl-devel/man/select.pl
):
:- initialization ( catch(main, E, ( print_message(error, E), fail )) -> halt ; halt(1) ).