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, f(t1 , … , tn ), the address points to a block of n + 1 consecutive words, where the first word points to the record for the functor, f/n, in the symbol table, and the remaining n 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, [H|T], 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.

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.

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.

G.5 Manipulating and Writing Picat Terms

G.6 Building Picat Terms

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)