Go to the first, previous, next, last section, table of contents.


Foreign Language Interface

Author(s): Jose Morales, Manuel Carro.

Version: 1.10#7 (2006/4/26, 19:22:13 CEST)

Version of last change: 1.9#47 (2003/1/7, 14:22:36 CET)

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:

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.

Declaration of Types

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:

     :- true pred prolog_predicate(m1(Arg1), ... mN(ArgN)) :: 
                  type1 * ... * typeN + 
                  (foreign(foreign_function_name), returns(ArgR)).

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:

     :- true pred prolog_predicate(m1(Arg1), ... mN(ArgN)) :: 
                  type1 * ... * typeN + foreign.

Equivalence between Ciao Prolog and C types

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):

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.

Equivalence between Ciao Prolog and C modes

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.

Custom access to Prolog from C

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.

Term construction

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.

Testing the Type of a Term

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).

Term navigation

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

Testing for Equality and Performing Unification

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.

Raising Exceptions

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.

Creating and disposing of memory chunks

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:

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

Calling Prolog from C

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:

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.

Examples

Mathematical functions

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).

File math.pl:

:- module(math, [sin/2, cos/2, fabs/2], [foreign_interface]).

:- true pred sin(in(X),go(Y)) :: num * num + (foreign,returns(Y)). :- true pred cos(in(X),go(Y)) :: num * num + (foreign,returns(Y)). :- true pred fabs(in(X),go(Y)) :: num * num + (foreign,returns(Y)).

:- extra_compiler_opts(['-O2']). :- extra_compiler_opts('LINUXi86',['-ffast-math']). :- use_foreign_library('LINUXi86', m).

Addresses and C pointers

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

:- use_foreign_source(objects_c).

directive.

File objects.pl:

:- module(objects, [object/2, show_object/1], [foreign_interface]).

:- true pred object(in(N),go(Object)) :: int * address + (foreign,returns(Object)).

:- true pred show_object(in(Object)) :: address + foreign.

:- use_foreign_source(objects_c). :- extra_compiler_opts('-O2').

File objects_c.c:

#include <stdio.h>

struct object { char *name; char *colour; };

#define OBJECTS 3

struct object objects[OBJECTS] = { {"ring","golden"}, {"table","brown"}, {"bottle","green"} };

struct object *object(int n) { return @&objects[n % OBJECTS]; }

void show_object(struct object *o) { printf("I show you a %s %s\n", o->colour, o->name); }

Lists of bytes and buffers

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.

File byte_lists.pl:

:- module(byte_lists, [obtain_list/3, show_list/2], [foreign_interface]). :- true pred obtain_list(in(N),go(Length),go(List)) :: int * int * byte_list + (foreign,size_of(List,Length)). :- true pred show_list(in(Length),in(List)) :: int * byte_list + (foreign,size_of(List,Length)).

:- use_foreign_source(bytes_op).

File bytes_op.c:

#include <stdlib.h> #include <stdio.h>

void obtain_list(int n, int *l, char **s) { int i; int c; if (n < 0) n = 0; *l = n; *s = (char *)malloc(*l); for (i = 0; i < *l; i++) { (*s)[i] = i; } }

void show_list(int l, char *s) { if (s) { int n; printf("From C:"); for (n = 0; n < l; n++) { printf(" %d", s[n]); } printf(".\n"); } else { printf("From C: []\n"); } }

Lists of integers

File int_lists.pl:

:- module(int_lists, [obtain_list/3, show_list/2], [foreign_interface]). :- true pred obtain_list(in(N),go(Length),go(List)) :: int * int * int_list + (foreign,size_of(List,Length)). :- true pred show_list(in(Length),in(List)) :: int * int_list + (foreign,size_of(List,Length)).

:- use_foreign_source(ints_op).

File ints_op.c:

#include <stdlib.h> #include <stdio.h>

void obtain_list(int n, int *l, int **s) { int i; int c; if (n < 0) n = 0; *l = n; *s = (int *)malloc((*l) * sizeof(int)); for (i = 0; i < *l; i++) { (*s)[i] = i; } }

void show_list(int l, int *s) { if (s) { int n; printf("From C:"); for (n = 0; n < l; n++) { printf(" %d", s[n]); } printf(".\n"); } else { printf("From C: []\n"); } }

Strings and atoms

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. File strings_and_atoms.pl:

:- module(strings_and_atoms, [ lookup_string/2, lookup_atom/2, a_string/1, show_string/1, show_atom/1 ], [foreign_interface]).

:- true pred a_string(go(S)) :: string + (foreign(get_static_str),returns(S),do_not_free(S)). :- true pred lookup_string(in(N),go(S)) :: int * string + (foreign(get_str),returns(S)). :- true pred lookup_atom(in(N),go(S)) :: int * atm + (foreign(get_str),returns(S)).

:- true pred show_string(in(S)) :: string + foreign(put_str). :- true pred show_atom(in(S)) :: atm + foreign(put_str).

:- use_foreign_source(str_op).

File str_op.c:

#include <stdlib.h> #include <stdio.h>

char *get_static_str() { return "this is a string Prolog should not free"; }

char *get_str(int n) { char *s; int size; int i; int c; if (n < 0) n = -n; size = (n%4) + 5; s = (char *)malloc(size+1); for (i = 0, c = ((i + n) % ('z' - 'a' + 1)) + 'a'; i < size; i++,c++) { if (c > 'z') c = 'a'; s[i] = c; } s[i] = 0; return s; }

void put_str(char *s) { if (s) { printf("From C: \"%s\"\n", s); } else { printf("From C: null\n"); } }

Arbitrary Terms

This example shows how data Prolog can be passed untouched to C code, and how it can be manipulated there. File any_term.pl:

:- module(any_term, [custom_display_term/1, custom_create_term/2 ], [foreign_interface]).

:- true pred custom_display_term(in(X)) :: any_term + foreign. :- true pred custom_create_term(in(L), go(X)) :: int * any_term + (foreign,returns(X)).

:- use_foreign_source(any_term_c). :- extra_compiler_opts('-O2').

File any_term_c.c:

#include <stdio.h> #include "ciao_prolog.h"

ciao_term custom_create_term(int n) { ciao_term t; t = ciao_empty_list(); while (n > 0) { t = ciao_list(ciao_integer(n), t); n--; } return t; }

void custom_display_term(ciao_term term) { if (ciao_is_atom(term)) { printf("<atom name=\"%s\"/>", ciao_atom_name(term)); } else if (ciao_is_structure(term)) { int i; int a; a = ciao_structure_arity(term); printf("<structure name=\"%s\" arity=\"%d\">", ciao_structure_name(term), a); for (i = 1; i <= a; i++) { printf("<argument number=\"%d\">", i); custom_display_term(ciao_structure_arg(term, i)); printf("</argument>"); } printf("</structure>"); } else if (ciao_is_list(term)) { printf("<list>"); printf("<head>"); custom_display_term(ciao_list_head(term)); printf("</head>"); printf("<tail>"); custom_display_term(ciao_list_tail(term)); printf("</tail>"); printf("</list>"); } else if (ciao_is_empty_list(term)) { printf("<empty_list/>"); } else if (ciao_is_integer(term)) { printf("<integer value=\"%d\"/>", ciao_to_integer(term)); } else if (ciao_is_number(term)) { printf("<float value=\"%f\"/>", ciao_to_float(term)); } else { printf("<unknown/>"); } }

Exceptions

The following example defines a predicate in C that converts a list of codes into a number using strtol(). If this conversion fails, then a exception is raised. File exceptions_example.pl:

:- module(exceptions_example, [codes_to_number_c/2, safe_codes_to_number/2 ], [foreign_interface]).

:- use_module(library(format)).

% If the string is not a number raises an exception. :- true pred codes_to_number_c(in(X), go(Y)) :: string * int + (foreign, returns(Y)).

safe_codes_to_number(X, Y) :- catch(codes_to_number_c(X, Y), Error, handle_exception(Error)).

handle_exception(Error) :- format("Exception caught ~w~n", [Error]).

:- use_foreign_source(exceptions_c). :- extra_compiler_opts('-O2').

File exceptions_c.c:

#include <string.h> #include "ciao_prolog.h"

int codes_to_number_c(char *s) { char *endptr; int n; n = strtol(s, @&endptr, 10); if (endptr == NULL || *endptr != '\0') { ciao_raise_exception(ciao_structure("codes_to_number_exception", 1, ciao_atom(s))); } return n; }

Testing number types and using unbound length integers

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.

File bigints.pl:

:- module(bigints, [ make_smart_conversion/3, % Checks and uses convenient format force_string_conversion/2 % Passes around using strings ], [foreign_interface]).

:- 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.".

:- use_foreign_source(bigints_c).

make_smart_conversion(A, B, C):- number(A), % Safety test make_smart_conversion_c(A, B, C).

force_string_conversion(A, B):- number(A), % Safety test force_string_conversion_c(A, B).

File bigints_c.c:

#include "ciao_prolog.h"

void make_smart_conversion_c(ciao_term number_in, ciao_term *number_out, ciao_term *how_converted) { int inter_int; double inter_float; char * inter_str;

if (ciao_fits_in_int(number_in)) {/* Includes the case of being a float */ inter_int = ciao_to_integer(number_in); *number_out = ciao_integer(inter_int); *how_converted = ciao_atom("machine_integer"); } else if (ciao_is_integer(number_in)) { /* Big number */ inter_str = ciao_get_number_chars(number_in); *number_out = ciao_put_number_chars(inter_str); ciao_free(inter_str); *how_converted = ciao_atom("string"); } else { /* Must be a float */ inter_float = ciao_to_float(number_in); *number_out = ciao_float(inter_float); *how_converted = ciao_atom("float"); } }

void force_string_conversion_c(ciao_term number_in, ciao_term *number_out) { char *inter_str; inter_str = ciao_get_number_chars(number_in); *number_out = ciao_put_number_chars(inter_str); ciao_free(inter_str); }

Usage and interface (foreign_interface)


Go to the first, previous, next, last section, table of contents.