QP_get_*()

Synopsis

     #include <quintus/quintus.h>
     
     int QP_get_atom(term, atom)
     QP_term_ref      term;
     QP_atom         *atom;
     
     int QP_get_integer(term, integer)
     QP_term_ref      term;
     long int        *integer;
     
     int QP_get_float(term, float)
     QP_term_ref      term;
     double          *float;
     
     int QP_get_functor(term, name, arity)
     QP_term_ref      term;
     QP_atom         *name;
     int             *arity;
     
     int QP_get_arg(argnum, term, arg)
     int              argnum;
     QP_term_ref      term;
     QP_term_ref      arg;
     
     int QP_get_list(term, head, tail)
     QP_term_ref      term;
     QP_term_ref      head;
     QP_term_ref      tail;
     
     int QP_get_head(term, head)
     QP_term_ref      term;
     QP_term_ref      head;
     
     int QP_get_tail(term, tail)
     QP_term_ref      term;
     QP_term_ref      tail;
     
     int QP_get_nil(term)
     QP_term_ref      term;
     
     int QP_get_db_reference(term, ref)
     QP_term_ref      term;
     QP_db_reference *ref;
     

These C functions can be used to test and access Prolog terms passed to C through the foreign interface.

Description

If term refers to an atom then QP_get_atom() assigns to *atom the unsigned integer representing that atom and returns 1. Else QP_get_atom() returns 0. To get at the string corresponding to the atom, use QP_string_from_atom().

If term refers to a Prolog integer then QP_get_integer() assigns that integer to *integer and returns 1. Else QP_get_integer() returns 0.

If term refers to a floating point number then QP_get_float() assigns that number to *float and returns 1. Else QP_get_float() returns 0.

If term refers to a compound term then QP_get_functor() assigns to *name the unsigned integer representing the name of the functor, assigns to *arity the arity of the functor and returns 1. If term refers to an atom, then QP_get_functor() assigns to *name that atom, assigns 0 to *arity and returns 1. If term does not refer to a compound term or an atom then QP_get_functor() returns 0. Note that a list is a compound term with functor . and arity 2.

If term refers to a compound term and argnum is between 1 and the arity of the compound term then QP_get_arg() assigns to arg a reference to the argnum argument of the compound term and returns 1. If term does not refer to a compound term QP_get_arg() returns 0. Note that QP_get_arg() is similar to the Prolog builtin arg/3 with its first and second arguments bound and its third argument unbound. QP_get_arg() differs from the other QP_get functions in that it does not have term as its first argument. This is to make it consistent with arg/3.

If term refers to a list then QP_get_list() assigns to head a reference to the head of that list, assigns to tail a reference to the tail of the list and returns 1. If term does not refer to a list then QP_get_list() returns 0.

If term refers to a list then QP_get_head() assigns to head a reference to the head of that list and returns 1. If term does not refer to a list then QP_get_head() returns 0.

If term refers to a list then QP_get_tail() assigns to tail a reference to the tail of that list and returns 1. If term does not refer to a list then QP_get_tail() returns 0.

If term refers to the atom [] then QP_get_nil() returns 1. Else it returns 0.

If term refers to a db_reference (e.g. returned by asserta/3 or recorda/3) then QP_get_db_reference() assigns to *ref that reference and returns 1. If term does not refer to a db_reference then QP_get_db_reference() returns 0.

Examples

write_term() is a C function that writes out a Prolog term passed to it.

                                     
foo.pl
foreign(write_term, c, write_term(+term)).
                                      
foo.c
#include <quintus/quintus.h> void write_term(term) QP_term_ref term; { QP_atom a; long int i; double d; switch (QP_term_type(term)) { case QP_VARIABLE: QP_printf("_"); break; case QP_INTEGER: QP_get_integer(term, &i); QP_printf("%d", i); break; case QP_FLOAT: QP_get_float(term, &d); QP_printf("%f", d); break; case QP_ATOM: QP_get_atom(term, &a); QP_printf("%s", QP_string_from_atom(a)); break; case QP_DB_REFERENCE: QP_printf("'$ref'()"); break; case QP_COMPOUND: if (QP_is_list(term)) { write_list(term); } else { write_compound(term); } break; } }
                                      
foo.c
void write_list(term) QP_term_ref term; { QP_term_ref head = QP_new_term_ref(); QP_term_ref tail = QP_new_term_ref(); QP_atom a; QP_printf("["); QP_get_list(term, head, tail); write_term(head); while (QP_is_list(tail)) { QP_printf(","); QP_get_list(tail, head, tail); write_term(head); } if (QP_get_nil(tail)) { QP_printf("]"); } else { QP_printf("|"); write_term(tail); QP_printf("]"); } } void write_compound(term) QP_term_ref term; { int i, arity; QP_atom name; QP_term_ref arg = QP_new_term_ref(); QP_get_functor(term, &name, &arity); QP_printf("%s(", QP_string_from_atom(name)); for (i = 1; i < arity; i++) { QP_get_arg(i, term, arg); write_term(arg); QP_printf(","); } QP_get_arg(i, term, arg); write_term(arg); QP_printf(")"); }

See Also

QP_term_type(), QP_put_*(), QP_new_term_ref()