The save_program/[1,2]
and restore/1
predicates discussed in
the previous section are used for saving and restoring the entire Prolog
database. To save selected parts of a Prolog database, the predicates
save_modules/2
and save_predicates/2
are used.
For example, to save the modules user
and special
you would use:
| ?- save_modules([user,special],'file1.qof').
All predicates in those modules will be saved, and in addition any foreign code files previously loaded into these modules will generate an object file dependency in the QOF file. All information in these modules about predicates attached to foreign functions, and also predicates that have been made externally callable from foreign code, is saved as a normal part of the module.
For each module imported by one of the specified modules, a QOF file
dependency is included in the QOF file. This means that when you load
file1.qof
into another Prolog session, it will automatically load
any additional QOF files that it needs.
To just save certain predicates you would use:
| ?- save_predicates([person/2,dept/4],'file2.qof').
This will only save the predicates specified. In this case no additional dependency information is saved into the QOF file. Note that the module information for these predicates is included. When the QOF file is loaded the predicates will be loaded into the same module they were in originally.
Any QOF file, however generated, can be loaded into Prolog with
load_files/[1,2]
:
| ?- load_files('file1.qof')
or, equivalently:
| ?- ['file1.qof'].
The information from each QOF file loaded is incrementally added to the
database. This means that definitions from later loads may replace
definitions from previous loads. A saved-state QOF file saved with
save_program/[1,2]
can also be loaded with load_files/[1,2]
in
which case the contents of the saved-state are just incrementally added to
the database as for any other QOF file. The use of load_files/[1,2]
for this is different from the use of restore/1
in that restore/1
will re-execute the executable thus reinitializing the database. Using
load_files/[1,2]
allows the database to be incrementally changed
within the same process.
If the loaded QOF file has object file dependencies then those object files will be linked and loaded as part of loading the QOF file unless they have already been loaded. If the object file cannot be found or linked, then an exception will be raised.
The predicates load_files/[1,2]
are used for compiling and
loading source files as well as QOF files. If file1.qof
and
file1.pl
both exist (and file1
does not), then load_files
(file 1) will load the source (.pl
) or the QOF, whichever is the most
recent. Refer to ref-lod for more information on
loading programs, and also to the reference page for
load_files/[1,2]
.
Advanced note: Both
save_modules/2
andsave_predicates/2
will save Prolog code that is statically linked if such modules or predicates are specified. This is different fromsave_program/[1,2]
, which will not save statically linked Prolog code. Note that if such a QOF file is loaded back into the same executable that saved it, then the new definitions from the QOF file will replace the statically linked code. There is no problem with this, except that some space will be wasted. The original statically linked code will not be used, but since it is linked into the executable its space cannot be reclaimed. Since static linking is normally used to optimize start-up time and the space usage for code, it is somewhat of a waste to circumvent this by saving and loading a lot of Prolog code that is already in the executable. If the QOF file is to be used for other purposes, such as re-linking the executable, or as a part to be loaded into another program, then, of course, the saving of statically linked code is probably exactly what is required.