Manual: Loading Programs


Quintus Prolog Manual


(PREV) (NEXT)

G-4: Loading Programs

G-4-0: Overview

There are two ways of loading programs into Prolog -- compiling source files and loading pre-compiled QOF files. Earlier releases of Prolog distinguished between compiling and consulting source files. Consulting a file caused the code to be loaded in an interpreted mode so that it could be debugged. Now compiled code is fully debuggable, so there is no longer any need to distinguish between compiling and consulting, and the built-in predicate consult/1 is now just a synonym for compile/1. Interpretation is now only used for the execution of dynamic code.

This chapter contains references to the use of the module system. These can be ignored if the module system is not being used (see Chapter G-13 for information on the module system).

G-4-1: The Load Predicates

Loading a program is accomplished by one of these predicates

load_files(File)
compiles source file or loads QOF file, whichever is the more recent. load_files(File) can also be written as [File].
compile(File)
compiles source file.
consult(File)
Same as compile
ensure_loaded(File)
loads more recent of source and QOF files, unless the file has already been loaded and it has not been modified since it was loaded.
load_files(File, Options)
loads file according to the specified options. All the above predicates can be regarded as special cases of this one.
reconsult(Files)/1
same as consult

The following notes apply to all the Load Predicates:

  1. The File argument must be one of:
    NOTE: If the file name is not a valid atom, it must be enclosed in single quotes. For example,
    
       load_files(expert)
       load_files('Expert')
       compile('/usr/joe/expert')
       ensure_loaded('expert.pl')
    
       
  2. These predicates resolve relative file names in the same way as absolute_file_name/2. For information on file names refer to Chapter G-6.
  3. The above predicates raise an exception if any of the files named in File does not exist, unless the 'nofileerrors' flag is set using nofileerrors/0. 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, and an appropriate error message is printed.
  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, but some or all of them can be turned off if desired using no_style_check/1. The possible style warnings are:
    1. A named variable occurs only once in a clause. Variables beginning with a '_' are considered not named.
    2. All the clauses for a predicate are not adjacent to one another in the file.
    3. A predicate is being redefined in a file different from the one in which it was previously defined.
  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 section G-13-2, for information about module-files. If a file being loaded is a module-file, it is first loaded in the normal way, then the source module imports all the public predicates of the module-file except for use_module and load_file 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, then these are executed as they are encountered. 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 file name it is resolved with respect to the directory containing the file which is being loaded, not the current working directory of the Prolog system. Any legal Prolog goal may be included as a directive. Note, however, that if the file is compiled by qpc, the goal will be executed by qpc, not when the .qof file is loaded or when application begins execution. The initialization/1 declaration provides this functionality. There is no difference between a ':-'/1 and a '?-'/1 goal in a file being compiled.
  8. 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 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.
  9. Terms which are notational variants of Prolog terms, notably grammar terms, are expanded into Prolog code during compilation. By defining the hook predicate term_expansion/2 (in module user), you can specify any desired transformation to be done as clauses are loaded.
  10. Any predicates that need to be called during the compilation of a file, including term_expansion/2 and all the predicates it calls, must be treated specially if you wish to be able to compile that file with qpc. See section H-1-6-1 for information on this.
  11. The current load context (module, file, stream, directory) can be queried using prolog_load_context/2.

G-4-2: Redefining Procedures during Program Execution

You can redefine procedures during the execution of the program, which can be very useful while debugging. The normal way to do this is to use the 'break' option of the debugger to enter a break state (see break/0, section G-11-1), and then load an altered version of some procedures. If you do this, it is advisable, after redefining the procedures and exiting from the break state, to wind the computation back to the first call to any of the procedures you are changing: you can do this by using the 'retry' option with an argument which is the invocation number of that call. If you do not wind the computation back like this, then:

G-4-3: Predicate List

Detailed information is found in the reference pages for the following:



          compile/1          consult/1
          ensure_loaded/1    load_files/[1,2]
          multifile/1        no_style_check/1
          style_check/1      prolog_load_context/2
          term_expansion/2   use_module/[1,2,3]


Copyright (C) 1998 SICS
contact: product support sales information