Once a program has been read, the system will have available all the information necessary for its execution. This information is called a program state.
The state of a program may be saved on disk for future execution.
The state consists of all predicates and modules except
built-in predicates and clauses of volatile
predicates, the current operator declarations, the
current character-conversion mapping, the values of all writable
Prolog flags except debugging
, source_info
, and the
user_*
stream aliases (see State Info), any
blackboard data (see Blackboard Primitives), database data
(see Database), and profiling data (see Profiling), but no
information for source-linked debugging.
To save a program into a file File, type the following query. On UNIX platforms, the file becomes executable:
| ?- save_program(File).
You can also specify a goal to be run when a saved program is restored. This is done by:
| ?- save_program(File, start).
where start/0
is the predicate to be called.
Once a program has been saved into a file File, the following query will restore the system to the saved-state:
| ?- restore(File).
If a saved-state has been moved or copied to another machine, the path names of foreign resources and other files needed upon restore are typically different at restore time from their save time values. To solve this problem, certain atoms will be relocated during restore as follows:
$SP_PATH/library
(the name of the directory
containing the Prolog Library) as prefix at save time will have that
prefix replaced by the corresponding restore time value.
The purpose of this procedure is to be able to build and deploy an application consisting of a saved-state and other files as a directory tree with the saved-state at the root: as long as the other files maintain their relative position in the deployed copy, they can still be found upon restore.
Please note: Foreign resources, see Calling C from Prolog, are unloaded bysave_program/[1,2]
. The names and paths of the resources, typically$SP_PATH/library
relative, are however included in the saved-state. After the save, and after restoring a saved-state, this information is used to reload the foreign resources again. The state of the foreign resource in terms of global C variables and allocated memory is thus not preserved. Foreign resources may define init and deinit functions to take special action upon loading and unloading; see Init and Deinit Functions.
As of SICStus Prolog 3.8, partial saved-states corresponding to a
set of source files, modules, and predicates can be created
by the built-in predicates save_files/2
,
save_modules/2
, and save_predicates/2
respectively. These
predicates create files in a binary format, by default with the
prefix .po
(for Prolog object file), which can be loaded by
load_files/[1,2]
. For example, to compile a program
split into several source files into a single object file, type:
| ?- compile(Files), save_files(Files, Object).
For each filename given, the first goal will try to locate a
source file with the default suffix .pl
and compile it into
memory. The second goal will save the program just
compiled into an object file whose default suffix is .po
.
Thus the object file will contain a partial memory image.
Please note: Prolog object files can be created with any suffix, but cannot be
loaded unless the suffix is .po
!