term_t
:
a reference to a Prolog term
The principal data type is term_t
. Type term_t
is what Quintus calls QP_term_ref
. This name indicates
better what the type represents: it is a handle for a term
rather than the term itself. Terms can only be represented and
manipulated using this type, as this is the only safe way to ensure the
Prolog kernel is aware of all terms referenced by foreign code and thus
allows the kernel to perform garbage collection and/or stack-shifts
while foreign code is active, for example during a callback from C.
A term reference is a C uintptr_t
, representing the
offset of a variable on the Prolog environment stack. A foreign function
is passed term references for the predicate arguments, one for each
argument. If references for intermediate results are needed, such
references may be created using PL_new_term_ref()
or PL_new_term_refs().
These references normally live till the foreign function returns control
back to Prolog. Their scope can be explicitly limited using PL_open_foreign_frame()
and
PL_close_foreign_frame()/PL_discard_foreign_frame().
A term_t
always refers to a valid Prolog term (variable,
atom, integer, float or compound term). A term lives either until
backtracking takes us back to a point before the term was created, the
garbage collector has collected the term, or the term was created after
a
PL_open_foreign_frame()
and PL_discard_foreign_frame()
has been called.
The foreign interface functions can either read, unify
or write to term references. In this document we use the
following notation for arguments of type term_t
:
term_t +t
Accessed in read-mode. The‘+’indicates the argument is‘input’. term_t -t
Accessed in write-mode. term_t ?t
Accessed in unify-mode.
WARNING Term references that are accessed in‘write’(-) mode will refer to an invalid term if the term is allocated on the global stack and backtracking takes us back to a point before the term was written.214This could have been avoided by trailing term references when data is written to them. This seriously hurts performance in some scenarios though. If this is desired, use PL_put_variable() followed by one of the PL_unify_*() functions. Compound terms, dicts, large integers, rational numbers, floats and strings are all allocated on the global stack. Below is a typical scenario where this may happen. The first solution writes a term extracted from the solution into a. After the system backtracks due to PL_next_solution(), a becomes a reference to a term that no longer exists.
term_t a = PL_new_term_ref(); ... query = PL_open_query(...); while(PL_next_solution(query)) { PL_get_arg(i, ..., a); } PL_close_query(query);
There are two solutions to this problem. One is to scope the term reference using PL_open_foreign_frame() and PL_close_foreign_frame() and makes sure it goes out of scope before backtracking happens. The other is to clear the term reference using PL_put_variable() before backtracking.
Term references are obtained in any of the following ways:
Term references can safely be copied to other C variables of type
term_t
, but all copies will always refer to the same term.
(term_t)0
on failure.(term_t)0
on
failure. There are two reasons for using this function. PL_open_query()
and PL_cons_functor()
expect the arguments as a set of consecutive term references, and very
time-critical code requiring a number of term references can be written
as:
pl_mypredicate(term_t a0, term_t a1) { term_t t0 = PL_new_term_refs(2); term_t t1 = t0+1; ... }
(term_t)0
on failure. An example of
its use is given below, in the sample code pl_write_atoms()
.Note that returning from the foreign context to Prolog will reclaim all references used in the foreign context. This call is only necessary if references are created inside a loop that never exits back to Prolog. See also PL_open_foreign_frame(), PL_close_foreign_frame() and PL_discard_foreign_frame().
Prolog implements two mechanisms for avoiding stack overflow: garbage
collection and stack expansion. On machines that allow for it, Prolog
will use virtual memory management to detect stack overflow and expand
the runtime stacks. On other machines Prolog will reallocate the stacks
and update all pointers to them. To do so, Prolog needs to know which
data is referenced by C code. As all Prolog data known by C is
referenced through term references (term_t
), Prolog has all
the information necessary to perform its memory management without
special precautions from the C programmer.