Appendix G
External Language Interface with C
On this page
Picat has an interface with C, through which Picat programs can call deterministic predicates that are written as functions in C. C programs that use this interface must include the file "picat.h" in the directory Picat/Emulator. In order to make C-defined predicates available to Picat, users have to recompile Picat’s C source code together with the newly added C functions.
G.1 Term Representation
Picat’s C interface provides functions for accessing, manipulating, and building Picat terms. In order to understand these functions, users need to know how terms are represented in Picat’s virtual machine.
A term is represented by a word that contains a value and a tag. A word has 32 bits or 64 bits, depending on the underlying CPU and OS. The tag in a word distinguishes the type of the term.
The value of a term is an address, except when the term is a small integer (in which case, the value represents the integer itself). The location to which the address points is dependent on the type of the term. In a reference, the address points to the referenced term. An unbound variable is represented by a self-referencing pointer. In an atom, the address points to the record for the atom symbol in the symbol table. In a structure, , the address points to a block of consecutive words, where the first word points to the record for the functor, f/n, in the symbol table, and the remaining words store the components of the structure. Arrays, floating-point numbers, and big integers are represented as special structures. Picat lists are singly linked lists. In a list, [HT], the address points to a block of two consecutive words, where the first word stores the car, H, and the second word stores the cdr, T.
G.2 Fetching Arguments of Picat Calls
A C function that defines a Picat predicate takes no arguments. The following function is used in order to fetch arguments in the current Picat call.
- TERM picat_get_call_arg(int i, int arity): Fetch the ith argument,
where arity is the arity of the predicate, and i must be an integer between 1 and arity. The validity of the arguments is not checked, and an invalid argument may cause fatal errors.
G.3 Testing Picat Terms
The following functions are provided for testing Picat terms. They return PICAT_TRUE when they succeed and PICAT_FALSE when they fail.
- int picat_is_var(TERM t): Term t is a variable.
- int picat_is_attr_var(TERM t): Term t is an attributed variable.
- int picat_is_dvar(TERM t): Term t is an attributed domain variable.
- int picat_is_bool_dvar(TERM t): Term t is an attributed Boolean variable.
- int picat_is_integer(TERM t): Term t is an integer.
- int picat_is_float(TERM t): Term t is a floating-point number.
- int picat_is_atom(TERM t): Term t is an atom.
- int picat_is_nil(TERM t): Term t is nil, i.e., the empty list [].
- int picat_is_list(TERM t): Term t is a list.
- int picat_is_string(TERM t): Term t is a string.
- int picat_is_structure(TERM t): Term t is a structure (but not a list).
- int picat_is_array(TERM t): Term t is an array.
- int picat_is_compound(TERM t): True if either picat_is_list(t)
or picat_is_structure(t) is true. - int picat_is_identical(TERM t1, TERM t2): t1 and t2 are identical. This function is equivalent to the Picat call t1 == t2.
- int picat_is_unifiable(TERM t1, TERM t2): t1 and t2 are unifiable. This is equivalent to the Picat call not(not(t1 = t2)).
G.4 Converting Picat Terms into C
The following functions convert Picat terms to C. If a Picat term does not have the expected type, then the global C variable exception, which is of type TERM, is assigned a term. A C program that uses these functions must check exception in order to see whether data are converted correctly. The converted data are only correct when exception is (TERM)NULL.
- long picat_get_integer(TERM t): Convert the Picat integer t into C. The term t must be an integer; otherwise exception is set to integer_expected and 0 is returned. Note that precision may be lost if t is a big integer.
- double picat_get_float(TERM t): Convert the Picat float t into C. The term t must be a floating-point number; otherwise exception is set to number_expected, and 0.0 is returned.
- char *picat_get_atom_name(TERM t): Return a pointer to the string that is the
name of atom t. The term t must be an atom; otherwise, NULL is returned after
exception is set to atom_expected. - char *picat_get_struct_name(TERM t): Return a pointer to the string that is the name of structure t. The term t must be a structure; otherwise, exception is set to structure_expected, and NULL is returned.
- int picat_get_struct_arity(TERM t): Return the arity of term t. The term t must be a structure; otherwise, exception is set to structure_expected and 0 is returned.
G.5 Manipulating and Writing Picat Terms
- int picat_unify(TERM t1, TERM t2): Unify two Picat terms t1 and t2. The result is PICAT_TRUE if the unification succeeds, and PICAT_FALSE if the unification fails.
- TERM picat_get_arg(int i, TERM t): Return the ith argument of term t. The term t must be compound, and i must be an integer that is between 1 and the arity of t; otherwise, exception is set to compound_expected, and the Picat integer 0 is returned.
- TERM picat_get_car(TERM t): Return the car of the list t. The term t must be a non-empty list; otherwise exception is set to list_expected, and the Picat integer 0 is returned.
- TERM picat_get_cdr(TERM t): Return the cdr of the list t. The term t must be a non-empty list; otherwise exception is set to list_expected, and the Picat integer 0 is returned.
- void picat_write_term(TERM t): Send term t to the standard output stream.
G.6 Building Picat Terms
- TERM picat_build_var(): Return a free Picat variable.
- TERM picat_build_integer(long i): Return a Picat integer whose value is i.
- TERM picat_build_float(double f): Return a Picat float whose value is f.
- TERM picat_build_atom(char*name): Return a Picat atom whose name is name.
- TERM picat_build_nil(): Return an empty Picat list.
- TERM picat_build_list(): Return a Picat list whose car and cdr are free variables.
- TERM picat_build_structure(char *name, int arity): Return a Picat
structure whose name is name and arity is arity. The structure’s arguments are all free variables. - TERM picat_build_array(int n): Return a Picat array whose size is n. The array’s arguments are all free variables.
G.7 Registering C-defined Predicates
The following function registers a predicate that is defined by a C function.
insert_cpred(char *name, int arity, int (*func)())
The first argument is the predicate name, the second argument is the arity, and the third argument is the name of the function that defines the predicate. The function that defines the predicate takes no arguments. As described above, picat_get_call_arg(i, arity) is used to fetch arguments from the Picat call.
For example, the following registers a predicate whose name is "p", and whose arity is 2.
extern int p();
insert_cpred("p", 2, p);
The C function’s name does not need to be the same as the predicate name.
Predicates that are defined in C should be registered after the Picat engine is initialized, and before any call is executed. One good place for registering predicates is the Cboot() function in the file cpreds.c, which registers all of the C-defined built-ins of Picat. After registration, the predicate can be called. All C-defined predicates must be explicitly called with the module qualifier bp, as in bp.p(a, X).
Example
Consider the Picat predicate:
p(a, X) => X = $f(a).
p(b, X) => X = [1].
p(c, X) => X = 1.2.
where the first argument is given and the second is unknown. The following steps show how to define this predicate in C, and how to make it callable from Picat.
-
Step 1
-
. Write a C function to implement the predicate. The following shows a sample:
#include "picat.h" int p(){ TERM a1, a2, a, b, c, f1, l1, f12; char *name_ptr; /* prepare Picat terms */ a1 = picat_get_call_arg(1, 2); /* first argument */ a2 = picat_get_call_arg(2, 2); /* second argument */ a = picat_build_atom("a"); b = picat_build_atom("b"); c = picat_build_atom("c"); f1 = picat_build_structure("f", 1); /* f(a) */ picat_unify(picat_get_arg(1, f1), a); l1 = picat_build_list(); /* [1] */ picat_unify(picat_get_car(l1), picat_build_integer(1)); picat_unify(picat_get_cdr(l1), picat_build_nil()); f12 = picat_build_float(1.2); /* 1.2 */ /* code for the rules */ if (!picat_is_atom(a1)) return PICAT_FALSE; name_ptr = picat_get_atom_name(a1); switch (*name_ptr){ case 'a': return (picat_unify(a1, a) ? picat_unify(a2, f1) : PICAT_FALSE); case 'b': return (picat_unify(a1, b) ? picat_unify(a2, l1) : PICAT_FALSE); case 'c': return (picat_unify(a1, c) ? picat_unify(a2, f12) : PICAT_FALSE); default: return PICAT_FALSE; } } -
Step 2
-
. Insert the following two lines into Cboot() in cpreds.c:
extern int p(); insert_cpred("p", 2, p); -
Step 3
-
. Modify the makefile, if necessary, and recompile the system. Now, p/2 is in the group of built-ins in Picat.
-
Step 4
-
. Use bp.p(...) to call the predicate.
Picat> bp.p(a, X) X = f(a)