Did you know ... | Search Documentation: |
The class PlTerm |
As we have seen from the examples, the PlTerm class plays a central role in conversion and operating on Prolog data. This section provides complete documentation of this class.
void *
.
PREDICATE(make_my_object, 1) { myclass *myobj = new myclass(); return A1 = (void *)myobj; } PREDICATE(free_my_object, 1) { myclass *myobj = (void *)A1; delete(myobj); return TRUE; }
PlTerm
can be cast to the following types:
long
if the PlTerm
is a Prolog integer or float that can be converted without loss to a
long. throws a
type_error
exception otherwise.long
, but might represent fewer bits.CVT_ALL|CVT_WRITE|BUF_RING
, which implies Prolog atoms and
strings are converted to the represented text. All other data is handed
to write/1. If
the text is static in Prolog, a direct pointer to the string is
returned. Otherwise the text is saved in a ring of 16 buffers and must
be copied to avoid overwriting.
=
is defined for the Types PlTerm,
long
, double
, char *
, wchar_t*
and
PlAtom. It performs Prolog
unification and returns TRUE
if successful and FALSE
otherwise.
The boolean return-value leads to somewhat unconventional-looking code as normally, assignment returns the value assigned in C. Unification however is fundamentally different to assignment as it can succeed or fail. Here is a common example.
PREDICATE(hostname, 1) { char buf[32]; if ( gethostname(buf, sizeof(buf)) == 0 ) return A1 = buf; return FALSE; }
long
and perform standard C-comparison between the two long integers. If PlTerm
cannot be converted a type_error
is raised.TRUE
if the PlTerm
is an atom or string representing the same text as the argument, FALSE
if the conversion was successful, but the strings are not equal and an
type_error
exception if the conversion failed.Below are some typical examples. See section 1.6 for direct manipulation of atoms in their internal representation.
A1 < 0 | Test A1 to hold a Prolog integer or float that can be transformed lossless to an integer less than zero. |
A1 < PlTerm(0) | A1
is before the term‘0’in the‘standard order of terms’.
This means that if A1 represents an atom, this test yields TRUE . |
A1 == PlCompound("a(1)") | Test A1
to represent the term
a(1) . |
A1 == "now" | Test A1 to be an atom or string holding the text “now” . |
Compound terms can be viewed as an array of terms with a name and
arity (length). This view is expressed by overloading the
operator.
[]
A type_error
is raised if the argument is not compound
and a
domain_error
if the index is out of range.
In addition, the following functions are defined:
type_error
is raised. Id arg is out of range, a
domain_error
is raised. Please note the counting from 1
which is consistent to Prolog's arg/3
predicate, but inconsistent to C's normal view on an array. See also
class PlCompound. The following
example tests x to represent a term with first-argument an
atom or string equal to gnat
.
..., if ( x[1] == "gnat" ) ...
const char *
holding the name of the functor of
the compound term. Raises a type_error
if the argument is
not compound.type_error
if the argument is not compound.
PL_VARIABLE
, PL_FLOAT
, PL_INTEGER
,
PL_ATOM
, PL_STRING
or PL_TERM
To avoid very confusing combinations of constructors and therefore possible undesirable effects a number of subclasses of PlTerm have been defined that provide constructors for creating special Prolog terms. These subclasses are defined below.
A SWI-Prolog string represents a byte-string on the global stack. It's lifetime is the same as for compound terms and other data living on the global stack. Strings are not only a compound representation of text that is garbage-collected, but as they can contain 0-bytes, they can be used to contain arbitrary C-data structures.
Character lists are compliant to Prolog's atom_chars/2 predicate.
syntax_error
exception is raised. Otherwise a new
term-reference holding the parsed text is created.hello(world)
.
PlCompound("hello", PlTermv("world"))
The class PlTail is both for analysing and constructing lists. It is called PlTail as enumeration-steps make the term-reference follow the‘tail’of the list.
"gnat"
,
a list of the form [gnat|B]
is created and the PlTail
object now points to the new variable B.
This function returns TRUE
if the unification succeeded
and
FALSE
otherwise. No exceptions are generated.
The example below translates the main() argument vector to Prolog and calls the prolog predicate entry/1 with it.
int main(int argc, char **argv) { PlEngine e(argv[0]); PlTermv av(1); PlTail l(av[0]); for(int i=0; i<argc; i++) l.append(argv[i]); l.close(); PlQuery q("entry", av); return q.next_solution() ? 0 : 1; }
[]
and returns the
result of the unification.TRUE
on success and FALSE
if
PlTail represents the empty list.
If PlTail is neither a list nor the
empty list, a type_error
is thrown. The example below
prints the elements of a list.
PREDICATE(write_list, 1) { PlTail tail(A1); PlTerm e; while(tail.next(e)) cout << (char *)e << endl; return TRUE; }