4.3.2 The Load Predicates

Loading a program is accomplished by one of these predicates

[]
[:File|+Files]
load_files(:Files)
load_files(:Files, +Options)
loads source or PO file(s), whichever is the more recent, according to Options.
compile(:File)
loads source file into virtual machine code.
consult(:File)
reconsult(:File)
loads source file into interpreted representation.
ensure_loaded(:File)
loads a source and PO file, whichever is more recent, unless the file has already been loaded and it has not been modified since it was loaded.

The following notes apply to all the Load Predicates:

  1. The File argument must be one of the following:
  2. These predicates resolve relative file names in the same way as absolute_file_name/2. For information on file names refer to ref-fdi.
  3. The above predicates raise an exception if any of the files named in File does not exist, unless the fileerrors flag is set to off.

    Errors detected during compilation, such as an attempt to redefine a built-in predicate, also cause exceptions to be raised. However, these exceptions are caught by the compiler, an appropriate error message is printed, and compilation continues.

  4. There are a number of style warnings that may appear when a file is compiled. These are designed to aid in catching simple errors in your programs and are initially on, but can be turned off if desired by setting the appropriate flags, which are:
    single_var_warnings
    If on, warnings are printed when a sentence (see ref-syn-syn-sen) containing variables not beginning with `_' occurring once only is compiled or consulted.

    The Prolog flag legacy_char_classification (see Prolog Flags) expands the set of variable names for which warnings are printed. When legacy_char_classification is in effect warnings are printed also for variables that occur only once and whose name begin with `_' followed by a character that is not an uppercase Latin 1 character.

    redefine_warnings
    This flag can take more values; see ref-lps-flg. If on, the user is asked what to do when:
    • a module or predicate is being redefined from a different file than its previous definition.
    • a predicate is being imported whilst it was locally defined already.
    • a predicate is being redefined locally whilst it was imported already.
    • a predicate is being imported whilst it was imported from another module already.

    discontiguous_warnings
    If on, warnings are printed when clauses are not together in source files, and the relevant predicate has not been declared discontiguous.
  5. By default, all clauses for a predicate are required to come from just one file. A predicate must be declared multifile if its clauses are to be spread across several different files. See the reference page for multifile/1.
  6. If a file being loaded is not a module-file, all the predicates defined in the file are loaded into the source module. The form load_files(Module:File) can be used to load the file into the specified module. See ref-mod-def for information about module-files. If a file being loaded is a module-file, it is first loaded in the normal way, the source module imports all the public predicates of the module-file except for use_module/[1,2,3] and load_files/[1,2] if you specify an import list.
  7. If there are any directives in the file being loaded, that is, any terms with principal functor :-/1 or ?-/1, these are executed as they are encountered. Only the first solution of directives is produced, and variable bindings are not displayed. Directives that fail or raise exceptions give rise to warning or error messages, but do not terminate the load. However, these warning or error messages can be intercepted by the hook user:portray_message/2, which can call abort/0 to terminate the load, if that is the desired behavior.
  8. A common type of directive to have in a file is one that loads another file, such as
               :- [otherfile].
         

    In this case, if otherfile is a relative filename it is resolved with respect to the directory containing the file that is being loaded, not the current working directory of the Prolog system.

    Any legal Prolog goal may be included as a directive. There is no difference between a `:-/1' and a `?-/1' goal in a file being compiled.

  9. If File is the atom user, or File is a list, and during loading of the list user is encountered, procedures are to be typed directly into Prolog from user_input, e.g. the terminal. A special prompt, `| ', is displayed at the beginning of every new clause entered from the terminal. Continuation lines of clauses typed at the terminal are preceded by a prompt of five spaces. When all clauses have been typed in, the last should be followed by an end-of-file character, or the atom end_of_file followed by a full-stop.
  10. During loading of source code, all terms being read in are subject to term expansion. Grammar rules is a special, built-in case of this mechanism. By defining the hook predicates user:term_expansion/6 and goal_expansion/5, you can specify any desired transformation to be done as clauses are loaded.
  11. The current load context (module, file, stream, directory) can be queried using prolog_load_context/2.
  12. Predicates loading source code are affected by the character-conversion mapping, cf. char_conversion/2.

Send feedback on this subject.