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.
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.
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.
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).
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).
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); }
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"); } }
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"); } }
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"); } }
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/>"); } }
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; }
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); }
foreign_interface
)foreign_interface
in the include list of a module, or by means of an explicit :- use_package(foreign_interface)
.
Go to the first, previous, next, last section, table of contents.