4.3 Loading Prolog source files
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Loading Prolog source files
          • load_files/1
          • load_files/2
          • consult/1
          • ensure_loaded/1
          • include/1
          • require/1
          • encoding/1
          • make/0
          • library_directory/1
          • file_search_path/2
          • expand_file_search_path/2
          • prolog_file_type/2
          • source_file/1
          • source_file/2
          • source_file_property/2
          • exists_source/1
          • exists_source/2
          • unload_file/1
          • prolog_load_context/2
          • source_location/2
          • at_halt/1
          • cancel_halt/1
          • initialization/1
          • initialization/2
          • initialize/0
          • compiling/0
          • Conditional compilation and program transformation
          • Reloading files, active code and threads
          • Quick load files
    • Packages
Availability:built-in
Source[ISO]:- initialization(:Goal)
Call Goal after loading the source file in which this directive appears has been completed. In addition, Goal is executed if a saved state created using qsave_program/1 is restored.

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.