A Prolog program has up to three types of code in it:
term_expansion/2
,
which allows you to specify arbitrary transformations to be done by
the compiler as it reads in clauses. See the reference page for more
information on term_expansion/2
.
When using the built-in compiler in the Development System, no
distinction has to be made between the three types of code. They can
coexist in one file. Before using qpc
on a program,
however, compile-time code must be separated out into its own file (or
files). Then, to each file that needs this new file or files at
compile time, add the goal (A) near the top of the file. This tells
qpc
to load NewFile directly into qpc
before further compiling the current file. It does not include
NewFile as a runtime dependency of the file. If you need
NewFile to be loaded at compile time and also at runtime, use
the goal (B) instead. This approach will work in the Development
System as well as qpc
.
:- load_files(NewFile, [when(compile_time)]). (A) :- load_files(NewFile, [when(both)]). (B)
Alternatively, you may omit the use of load_files/2
, instead
specifying files to be loaded into qpc
with the
-i
option. In this case, when you want to compile this
file into the Development System, remember to first load the file(s) needed at
compile time.
It is good programming style to use initialization/1
for goals to
be activated at runtime. Note that predicates called as :- Goal.
need to be available at compile time, whereas predicates called as
:- initialization(Goal).
only need to be available at runtime.