QP_initialize()
#include <quintus/quintus.h> int QP_initialize(argc, argv) int argc; char **argv;
Initializes Prolog's memory management, I/O, symbol table, etc.
You can ignore
QP_initialize()
if you aren't redefining main()
.
Must be called before any other QP_ functions if you are redefining
main()
(in which case your top-level view of Prolog is via
QP_predicate()
and QP_query()
).
Summary of functionality: Initializes memory, I/O; sets up command line arguments; initializes file search paths, file tables and symbol tables; do initializations and start up hooks associated with a statically linked component in qof files; do any necessary restores, and any initlization and start up hooks associated with the restored files.
QP_initialize()
also sets up signal handlers so that users
can interrupt the execution of a start-up goal or initialization
with a ^c. If users chose the a option after a ^c (or
if the builtin abort/0
is called) when initializations are run,
then QP_initialize()
returns. In a default system, (where main()
hasnt been redefined) this means that QP_toplevel()
gets called.
QP_toplevel()
executes the toplevel read-prove loop in a development
system. In a runtime system, it results in runtime_entry(start)
being called.
argc and argv are necessary for Prolog to execute the builtin
unix/1
(e.g. unix(argv(_))
etc.) properly,
as well as for restoring saved states.
Can be safely called any number of times.
Please note: The first call to
QP_initialize()
with non-null arguments will determine the command line arguments as seen by Prolog.
QP_SUCCESS
QP_FAILURE
In Quintus Prolog the default implementation of main()
looks like this:
#include <quintus/quintus.h> main(argc, argv) int argc; char **argv; { int status; status = QP_initialize(argc, argv); if (status == QP_SUCCESS) QP_toplevel(); }
The user can choose not to have the default main()
and the default
toplevel loop. Here is an example of how the user can call a Prolog
predicate with their own main()
.
hello.pl:- extern(hi(+atom)). hi(X) :- format('Hello world from ~a to Prolog~n',[X]).
main.c#include <quintus/quintus.h> main(argc, argv) int argc; char **argv; { int status; QP_pred_ref pred; status = QP_initialize(argc, argv); if (status == QP_SUCCESS) { pred = QP_predicate("hi", 1, "user"); if (pred != QP_BAD_PREDREF) { status = QP_query(pred, QP_atom_from_string("C")); if (status == QP_FAILURE) { printf("hi/1 failed\n"); exit(1); } else if (status == QP_ERROR) { printf("hi/1 raised exception\n"); /* Use QP_exception_term to get the error term signaled */ exit(1); } } else { printf("hi/1 doesn't exist or "); printf("doesn't have an extern "); printf("declaration\n"); exit(1); } } else { printf("QP_initialize didn't succeed\n"); exit(1); } }
Steps to produce the executable:
hello.pl
using qpc -c hello.pl
main.c
using cc -c main.c
qtest
. The output should be: Hello from C to Prolog
runtime_entry/1
, unix/1
, QP_predicate()
,
QP_query()
, QP_toplevel()
fli-emb