Ciao Prolog includes a high-level, flexible way to interface C and Prolog, based on the use of assertions to declare what are the expected types and modes of the arguments of a Prolog predicate, and which C files contain the corresponding code. To this end, the user provides:

A set of C files, or a precompiled shared library,

A Ciao Prolog module defining whith predicates are implemented in the C files and the types and modes of their arguments, and

an (optional) set of flags required for the compilation of the files.

The Ciao Prolog compiler analyzes the Prolog code written by the user and gathers this information in order to generate automatically C "glue" code implementing the data translation between Prolog and C, and to compile the C code into dynamically loadable C object files, which are linked automatically when needed.

Each predicate implemented as a foreign C function must have accompanying declarations in the Ciao Prolog associated file stating the types and modes of the C function. A sample declaration for prolog_predicate which is implemented as foreign_function_name is:

where m1, ..., mN and type1, ..., typeN are respectively the modes and types of the arguments. foreign_function_name is the name of the C function implementing
prolog_predicate/N, and the result of this function is unified with ArgR, which must be one of Arg1 ... ArgN.

This notation can be simplified in several ways. If the name of the foreign function is the same as the name of the Ciao Prolog predicate, foreign(foreign_function_name) can be replaced by foreign/0. returns(ArgR) specifies that the result of the function corresponds to the ArgR argument of the Ciao Prolog predicate. If the foreign function does not return anything (or if its value is ignored), then returns(ArgR) must be removed. Note that returns cannot be used without foreign. A simplified, minimal form is thus:

The automatic translation between Ciao Prolog and C types is defined (at the moment) only for some simple but useful types. The translation to be performed is solely defined by the types of the arguments in the Ciao Prolog file (i.e., no inspection of the corresponding C file is done). The names (and meaning) of the types known for performing that translation are to be found in section Foreign Language Interface Properties; they are also summarized below (Prolog types are on the left, and the corresponding C types on the right):

num <-> double

int <-> int

atm <-> char *

string <-> char * (with trailing zero)

byte_list <-> char * (a buffer of bytes, with associated length)

int_list <-> int * (a buffer of integers, with associated length)

address <-> void *

Strings, atoms, and lists of bytes are passed to (and from) C as dynamically (ciao_malloc) created arrays of characters (bytes). Those arrays are freed by Ciao Prolog upon return of the foreign function unless the property
do_not_free/2 is specified (see examples below). This caters for the case in which the C files save in a private state (either by themselves, or by a library function being called by them) the values passed on from Prolog. The type
byte_list/1 requires an additional property,
size_of/2, to indicate which argument represents its size.
Empty lists of bytes and integers are converted into C NULL pointers, and vice versa. Empty strings ([]) and null atoms (") are converted into zero-length, zero-ended C strings (""). C NULL strings and empty buffers (i.e., buffers with zero length) are transformed into the empty list or the null atom (").

Most of the work is performed by the predicates in the section Foreign Language Interface Builder, which can be called explicitly by the user. Doing that is not usually needed, since the Ciao Prolog Compiler takes care of building glue code files an of compiling and linking whatever is necessary.

The (prefix) +/1 ISO mode (or, equivalently, the in/1 mode) states that the corresponding Prolog argument is ground at the time of the call, and therefore it is an input argument in the C part; this groundness is automatically checked upon entry. The (prefix) -/1 ISO mode (or, equivalently, the go/1 mode) states that Prolog expects the C side to generate a (ground) value for that argument. Arguments with output mode should appear in C functions as pointers to the corresponding base type (as it is usual with C), i.e., an argument which is an integer generated by the C file, declared as

:- true pred get_int(go(ThisInt)) :: int + foreign

or as

:- true pred get_int(-ThisInt) :: int + foreign

should appear in the C code as

void get_int(int *thisint)
{
....
}

Note the type of the (single) argument of the function. Besides, the return value of a function can always be used as an output argument, just by specifying to which Prolog arguments it corresponds, using the foreing/1 property. The examples below illustrate this point, and the use of several assertions to guide the compilation.

Automatic type conversions does not cover all the possible cases. When the automatic type conversion is not enough (or if the user, for any reason, does not want to go through the automatic conversion), it is possible to instruct Ciao Prolog not to make implicit type conversion. The strategy in that case is to pass the relevant argument(s) with a special type (a ciao_term) which can represent any term which can be built in Prolog. Operations to construct, traverse, and test this data abstraction from C are provided. The prototypes of these operations are placed on the "ciao_prolog.h" file, under the include subdirectory of the installation directory (the Ciao Prolog compiler knowns where it has been installed, and gives the C compiler the appropriate flags). This non direct correspondence mode is activated whenever a Ciao Prolog type unknown to the foreign interface (i.e., none of these in section Foreign Language Interface Properties) or the type any_term (which is explicitly recognised by the foreign language interface) is found. The latter is preferred, as it is much more informative, and external tools, as the the
CiaoPP preprocessor, can take advantage of them.

All term construction primitives return an argument of type ciao_term, which is the result of constructing a term. All Ciao Prolog terms can be built using the interface operations ciao_var(), ciao_structure(), ciao_integer(), and ciao_float(). There are, however, variants and specialized versions of these operations which can be freely intermixed. Using one version or another is a matter of taste and convenience. We list below the prototypes of the primitives in order of complexity.

Throughout this section, true, when referred to a boolean value, correspond to the integer value 1, and false correspond to the integer value 0, as is customary in C boolean expressions. These values also available as the (predefined) constants ciao_true and ciao_false, both of type ciao_bool.

ciao_term ciao_var();
Returns a fresh, unbound variable.

ciao_term ciao_integer(int i);
Creates a term, representing an integer from the Prolog point of view, from a C integer.

ciao_term ciao_float(double i);
Creates a term, representing a floating point number, from a floating point number.

ciao_term ciao_put_number_chars(char *number_string);
It converts number_string (which must a string representing a syntactically valid number) into a ciao_term.

ciao_term ciao_atom(char *name);
Creates an atom whose printable name is given as a C string.

ciao_term ciao_structure_a(char *name, int arity, ciao_term *args);
Creates a structure with name 'name' (i.e., the functor name ), arity 'arity' and the components of the array 'args' as arguments: args[0] will be the first argument, args[1] the second, and so on. The 'args' array itself is not needed after the term is created, and can thus be a variable local to a procedure. An atom can be represented as a 0-arity structure (with ciao_structure(name, 0)), and a list cell can be constructed using the '.'/2 structure name. The _a suffix stands for array.

ciao_term ciao_structure(char *name, int arity, ...);
Similar to ciao_structure_a, but the C arguments after the arity are used to fill in the arguments of the structure.

ciao_term ciao_list(ciao_term head, ciao_term tail);
Creates a list from a head and a tail. It is equivalent to ciao_structure(".", 2, head, tail).

ciao_term ciao_empty_list();
Creates an empty list. It is equivalent to ciao_atom("[]").

ciao_term ciao_listn_a(int len, ciao_term *args);
Creates a list with 'len' elements from the array args. The nth element of the list (starting at 1) is args[n-1] (starting at zero).

ciao_term ciao_listn(int length, ...);
Like ciao_listn_a(), but the list elements appear explicitly as arguments in the call.

ciao_term ciao_dlist_a(int len, ciao_term *args, ciao_term base);
Like ciao_listn_a, but a difference list is created. base whill be used as the tail of the list, instead of the empty list.

ciao_term ciao_dlist(int length, ...);
Similar to ciao_dlist_a() with a variable number of arguments. The last one is the tail of the list.

ciao_term ciao_copy_term(ciao_term src_term);
Returns a new copy of the term, with fresh variables (as copy_term/2 does).

A ciao_term can contain any Prolog term, and its implementation is opaque to the C code. Therefore the only way to know reliably what data is passed on is using explicit functions to test term types. Below, ciao_bool is a type defined in "ciao_prolog.h" which can take the values 1 (for true) and 0 (for false).

ciao_bool ciao_is_variable(ciao_term term);
Returns true if term is currently an uninstantiated variable.

ciao_bool ciao_is_number(ciao_term term);
Returns true if term is an integer (of any length) or a floating point number.

ciao_bool ciao_is_integer(ciao_term term);
Returns true if term is instantiated to an integer.

ciao_bool ciao_fits_in_int(ciao_term term);
Returns true if term is instantiated to an integer which can be stored in an int, and false otherwise.

ciao_bool ciao_is_atom(ciao_term atom);
Returns true if term is an atom.

ciao_bool ciao_is_list(ciao_term term);
Returns true if term is a list (actually, a cons cell).

ciao_bool ciao_is_empty_list(ciao_term term);
Returns true if term is the atom which represents the empty list (i.e., []).

ciao_bool ciao_is_structure(ciao_term term);
Returns true if term is a structure of any arity. This includes atoms (i.e., structures of arity zero) and lists, but excludes variables and numbers.

The functions below can be used to recover the value of a ciao_term into C variables, or to inspect Prolog structures.

int ciao_to_integer(ciao_term term);
Converts term to an integer. ciao_is_integer(term) must hold.

ciao_bool ciao_to_integer_check(ciao_term term, int *result);
Checks whether term fits into the size of an integer. If so, true is returned and *result is unified with the integer term represents. Otherwise, false is returned and *result is not touched.

double ciao_to_float(ciao_term term);
Converts term to a float value. ciao_is_number(term) must hold.

char *ciao_get_number_chars(ciao_term term);
It converts ciao_term (which must be instantiated to a number) into a C string representing the number in the current radix. The string returned is a copy, which must (eventually) be explicitly deallocated by the user C code using the operation ciao_free()

char *ciao_atom_name(ciao_term atom);
Returns the name of the atom. The returned string is the one internally used by Ciao Prolog, and should not be deallocated, changed or altered in any form. The advantage of using it is that it is fast, as no data copying is needed.

char *ciao_atom_name_dup(ciao_term atom);
Obtains a copy of the name of the atom. The string can be modified, and the programmer has the responsibility of deallocating it after being used. Due to the copy, it is slower than calling char *ciao_atom_name().

ciao_term ciao_list_head(ciao_term term);
Extracts the head of the list term. Requires term to be a list.

ciao_term ciao_list_tail(ciao_term term);
Extracts the tail of the list term. Requires term to be a list.

char *ciao_structure_name(ciao_term term);
Extracts the name of the structure term. Requires term to be a structure.

int ciao_structure_arity(ciao_term term);
Extracts the arity of the structure term.
Requires term to be a structure.

ciao_term ciao_structure_arg(ciao_term term, int n);
Extracts the nth argument of the structure term. It behaves like arg/3, so the first argument has index 1. Requires term to be a structure.

Variables of type ciao_term cannot be tested directly for equality: they are (currently) implemented as a sort of pointers which may be aliased (two different pointers may refer to the same object). The interface provides helper functions for testing term equality and to perform unification of terms.

ciao_bool ciao_unify(ciao_term x, ciao_term y);
Performs the unification of the terms x and y, and returns true if the unification was successful. This is equivalent to calling the (infix) Prolog predicate =/2. The bindings are trailed and undone on backtracking.

ciao_bool ciao_equal(ciao_term x, ciao_term y);
Performs equality testing of terms, and returns true if the test was successful. This is equivalent to calling the (infix) Prolog predicate ==/2. Equality testing does not modify the terms compared.

The following functions offers a way of throwing
exceptions from C that can be caught in Prolog with catch/3. The term that reaches Prolog is exactly the same which was thrown by C. The execution flow is broken at the point where ciao_raise_exception() is executed, and it returns to Prolog.

void ciao_raise_exception(ciao_term ball);
Raises an exception an throws the term ball.

Memory to be used solely by the user C code can be reserved/disposed of using, e.g., the well-known malloc()/free() functions (or whatever other functions the user may have available). However, memory explicitly allocated by Ciao Prolog and passed to C code, or allocated by C code and passed on to Ciao Prolog (and subject to garbage collection by it) should be allotted and freed (when necessary) by using the functions:

void *ciao_malloc(int size);

void ciao_free(void *pointer);

whose behavior is similar to malloc()/free(), but which will cooordinate properly with Ciao Prolog's internal memory management.

It is also possible to make arbitraty calls to Prolog predicates from C. There are two basic ways of make a query, depending on whether only one solution is needed (or if the predicate to be called is known to generate only one solution), or if several solutions are required.

When only one solution is needed ciao_commit_call obtains it (the solution obtained will obviously be the first one) and discards the resources used for finding it:

ciao_bool ciao_commit_call(char *name, int arity, ...);
Makes a call to a predicate and returns true or false depending on whether the query has succedeed or not. In case of success, the (possibly) instantiated variables are reachable from C.

ciao_bool ciao_commit_call_term(ciao_term goal);
Like ciao_commit_call() but uses the previously built term goal as goal.

If more than one solution is needed, it is necessary to use the ciao_query operations. A consult begins with a ciao_query_begin which returns a ciao_query object. Whenever an additional solution is required, the ciao_query_next function can be called. The query ends by calling ciao_query_end and all pending search branches are pruned.

ciao_query *ciao_query_begin(char *name, int arity, ...);
The predicate with the given name, arity and arguments (similar to the ciao_structure() operation) is transformed into a ciao_query object which can be used to make the actual query.

ciao_query *ciao_query_begin_term(ciao_term goal);
Like ciao_query_begin but using the term goal instead.

ciao_bool ciao_query_ok(ciao_query *query);
Determines whether the query may have pending solutions. A false return value means that there are no more solutions; a true return value means that there are more possible solutions.

void ciao_query_next(ciao_query *query);
Ask for a new solution.

void ciao_query_end(ciao_query *query);
Ends the query and frees the used resources.

In this example, the standard mathematical library is accessed to provide the sin, cos, and fabs functions. Note that the library is specified simply as

:- use_foreign_library([m]).

The foreign interface adds the -lm at compile time. Note also how some additional options are added to optimize the compiled code (only glue code, in this case) and mathematics (only in the case of Linux in an Intel processor).

The address type designates any pointer, and provides a means to deal with C pointers in Prolog without interpreting them whatsoever. The C source file which implements the operations accessed from Prolog is declared with the

A list of bytes (c.f., a list of ints) corresponds to a byte buffer in C. The length of the buffer is associated to that of the list using the property size_of/2. The returned buffer is freed by Ciao Prolog upon its recepction, unless the do_not_free/1 property is specified (see later). Conversely, a list of natural numbers in the range 0 to 255 can be passed to C as a buffer.

A C string can be seen as a buffer whose end is denoted by the trailing zero, and therefore stating its length is not needed. Two translations are possible into Ciao Prolog: as a Prolog string (list of bytes, with no trailing zero) and as an atom. These are selected automatically just by choosing the corresponding type (look at the examples below).

Note how the do_not_free/1 property is specified in the
a_string/1 predicate: the string returned by C is static, and therefore it should not be freed by Prolog.
Filestrings_and_atoms.pl:

Unbound length integers (and, in general, any number) can be converted to/from ciao_terms by using strings. The following examples show two possibilities: one which tries to be as smart as possible (checking whether numbers fit into a machine int or not), and being lazy and simpler -and probably slower.

:- true pred make_smart_conversion_c(in(X), go(Y), go(How))::
any_term * any_term * any_term + foreign #
"Given a number @var{X}, it is unified with @var{Y} by using the most
specific internal representation (short integer, float, or long
integer). @var{How} returns how the conversion was done.
It behaves unpredictably if @var{X} is not a number.".

:- true pred force_string_conversion_c(in(X), go(Y))::
any_term * any_term + foreign #
"Given a number @var{X}, it is unified with @var{Y} by using the most
general internal representation (a string of characters). It behaves
unpredictably if @var{X} is not a number.".