<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Calling Prolog from C </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual068.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual065.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual070.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66dbff"><DIV ALIGN=center><TABLE> <TR><TD><FONT SIZE=4><B><A NAME="htoc374">9.4</A></B></FONT></TD> <TD WIDTH="100%" ALIGN=center><FONT SIZE=4><B>Calling Prolog from C</B></FONT></TD> </TR></TABLE></DIV></TD> </TR></TABLE><UL> <LI><A HREF="manual069.html#toc307"> Introduction</A> <LI><A HREF="manual069.html#toc308"> Example: <TT>my_call/1</TT> - a <TT>call/1</TT> clone</A> <LI><A HREF="manual069.html#toc309"> Example: recovering the list of all operators</A> </UL> <BR> <A NAME="toc307"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc375">9.4.1</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Introduction</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> The following functions allows a C function to call a Prolog predicate: <DL COMPACT=compact><DT><DD> <PRE> void Pl_Query_Begin (Bool recoverable) int Pl_Query_Call (int functor, int arity, PlTerm *arg) int Pl_Query_Next_Solution(void) void Pl_Query_End (int op) PlTerm Pl_Get_Exception (void) void Pl_Exec_Continuation (int functor, int arity, PlTerm *arg) </PRE></DL> The invocation of a Prolog predicate should be done as follows: <UL><LI>open a query using <TT>Pl_Query_Begin()</TT><BR> <BR> <LI>compute the first solution using <TT>Pl_Query_Call()</TT><BR> <BR> <LI>eventually compute next solutions using <TT>Pl_Query_Next_Solution()</TT><BR> <BR> <LI>close the query using <TT>Pl_Query_End()</TT></UL> The function <TT>Pl_Query_Begin(recoverable)</TT> is used to initialize a query. The argument <TT>recoverable</TT> shall be set to <TT>TRUE</TT> if the user wants to recover, at the end of the query, the memory space consumed by the query (in that case an additional choice-point is created). All terms created in the heap, e.g. using <TT>Mk_...</TT> family functions (section <A HREF="manual067.html#Creating-Prolog-terms">9.2.5</A>), after the invocation of <TT>Pl_Query_Begin()</TT> can be recovered when calling <TT>Pl_Query_End(TRUE)</TT> (see below).<BR> <BR> The function <TT>Pl_Query_Call(functor, arity, arg)</TT> calls a predicate passing arguments. It is then used to compute the first solution. The arguments <TT>functor</TT>, <TT>arity</TT> and <TT>arg</TT> are similar to those of the functions handling complex terms (section <A HREF="manual067.html#Introduction:(Manipulating-Prolog-terms)">9.2.1</A>). This function returns: <UL><LI><TT>PL_FAILURE</TT> (a constant equal to <TT>FALSE</TT>, i.e. 0) if the query fails.<BR> <BR> <LI><TT>PL_SUCCESS</TT> (a constant equal to <TT>TRUE</TT>, i.e. 1) in case of success. In that case the argument array <TT>arg</TT> can be used to obtain the unification performed by the query.<BR> <BR> <LI><TT>PL_EXCEPTION</TT> (a constant equal to 2). In that case function <TT>Pl_Get_Exception()</TT> can be used to obtained the exceptional term raised by <TT>throw/1</TT> (section <A HREF="manual022.html#catch/3">6.2.4</A>).</UL> The function <TT>Pl_Query_Next_Solution()</TT> is used to compute a new solution. It must be only used if the result of the previous solution was <TT>PL_SUCCESS</TT>. This functions returns the same kind of values as <TT>Pl_Query_Call()</TT> (see above).<BR> <BR> The function <TT>Pl_Query_End(op)</TT> is used to finish a query. This function mainly manages the remaining alternatives of the query. However, even if the query has no alternatives this function must be used to correctly finish the query. The value of <TT>op</TT> is: <UL><LI><TT>PL_RECOVER</TT>: to recover the memory space consumed by the query. After that the state of Prolog stacks is exactly the same as before opening the query. To use this option the query must have been initialized specifying <TT>TRUE</TT> for <TT>recoverable</TT> (see above).<BR> <BR> <LI><TT>PL_CUT</TT>: to cut remaining alternatives. The effect of this option is similar to a cut after the query.<BR> <BR> <LI><TT>PL_KEEP_FOR_PROLOG</TT>: to keep the alternatives for Prolog. This is useful when the query was invoked in a foreign C function. In that case, when the predicate corresponding to the C foreign function is invoked a query is executed and the remaining alternatives are then available as alternatives of that predicate.</UL> Note that several queries can be nested since a stack of queries is maintained. For instance, it is possible to call a query and before terminating it to call another query. In that case the first execution of <TT>Pl_Query_End()</TT> will finish the second query (i.e. the inner) and the next execution of <TT>Pl_Query_End()</TT> will finish the first query.<BR> <BR> Finally, the function <TT>Pl_Exec_Continuation(functor, arity, arg)</TT> replaces the current calculus by the execution of the specified predicate. The arguments <TT>functor</TT>, <TT>arity</TT> and <TT>arg</TT> are similar to those of the functions handling complex terms (section <A HREF="manual067.html#Introduction:(Manipulating-Prolog-terms)">9.2.1</A>).<BR> <BR> <A NAME="toc308"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc376">9.4.2</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Example: <TT>my_call/1</TT> - a <TT>call/1</TT> clone</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> We here define a predicate <TT>my_call(Goal)</TT> which acts like <TT>call(Goal)</TT> except that we do not handle exceptions (if an exception occurs the goal simply fails):<BR> <BR> In the prolog file <TT>examp.pl</TT>: <DL COMPACT=compact><DT><DD><TT>:- foreign(my_call(term)).</TT></DL> In the C file <TT>examp_c.c</TT>: <DL COMPACT=compact><DT><DD> <PRE> #include <string.h> #include "gprolog.h" Bool my_call(PlTerm goal) { PlTerm *arg; int functor, arity; int result; arg = Rd_Callable_Check(goal, &functor, &arity); Pl_Query_Begin(FALSE); result = Pl_Query_Call(functor, arity, arg); Pl_Query_End(PL_KEEP_FOR_PROLOG); return (result == PL_SUCCESS); } </PRE></DL> The compilation produces an executable called <TT>examp</TT>: <DL COMPACT=compact><DT><DD><TT>% gplc examp.pl examp_c.c</TT></DL> Examples of use: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- my_call(write(hello)).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>hello</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- my_call(for(X,1,3)).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 1 ?</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 2 ?</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 3</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user is not prompted since there is no more alternative)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- my_call(1).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>{exception: error(type_error(callable,1),my_call/1)}</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- my_call(call(1)).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>no</TT></TD> </TR></TABLE></DL> When <TT>my_call(1)</TT> is called an error is raised due to the use of <TT>Rd_Callable_Check()</TT>. However the error raised by <TT>my_call(call(1))</TT> is ignored and <TT>FALSE</TT> (i.e. a failure) is returned by the foreign function. <BR> <BR> To really simulate the behavior of <TT>call/1</TT> when an exception is recovered it should be re-raised to be captured by an earlier handler. The idea is then to execute a <TT>throw/1</TT> as the continuation. This is what it is done by the following code: <DL COMPACT=compact><DT><DD> <PRE> #include <string.h> #include "gprolog.h" Bool my_call(PlTerm goal) { PlTerm *args; int functor, arity; int result; args = Rd_Callable_Check(goal, &functor, &arity); Pl_Query_Begin(FALSE); result = Pl_Query_Call(functor, arity, args); Pl_Query_End(PL_KEEP_FOR_PROLOG); if (result == PL_EXCEPTION) { PlTerm except = Pl_Get_Exception(); Pl_Exec_Continuation(Find_Atom("throw"), 1, &except); } return result; } </PRE></DL> The following code propagates the error raised by <TT>call/1</TT>. <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- my_call(call(1)).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>{exception: error(type_error(callable,1),my_call/1)}</TT></TD> </TR></TABLE></DL> Finally note that a simpler way to define <TT>my_call/1</TT> is to use <TT>Pl_Exec_Continuation()</TT> as follows: <DL COMPACT=compact><DT><DD> <PRE> #include <string.h> #include "gprolog.h" Bool my_call(PlTerm goal) { PlTerm *args; int functor, arity; args = Rd_Callable_Check(goal, &functor, &arity); Pl_Exec_Continuation(functor, arity, args); return TRUE; } </PRE></DL> <A NAME="toc309"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc377">9.4.3</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Example: recovering the list of all operators</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> We here define a predicate <TT>all_op(List)</TT> which unifies <TT>List</TT> with the list of all currently defined operators as would be done by: <TT>findall(X,current_op(_,_,X),List)</TT>.<BR> <BR> In the prolog file <TT>examp.pl</TT>: <DL COMPACT=compact><DT><DD><TT>:- foreign(all_op(term)).</TT></DL> In the C file <TT>examp_c.c</TT>: <DL COMPACT=compact><DT><DD> <PRE> #include <string.h> #include "gprolog.h" Bool all_op(PlTerm list) { PlTerm op[1024]; PlTerm args[3]; int n = 0; int result; Pl_Query_Begin(TRUE); args[0] = Mk_Variable(); args[1] = Mk_Variable(); args[2] = Mk_Variable(); result = Pl_Query_Call(Find_Atom("current_op"), 3, args); while (result) { op[n++] = Mk_Atom(Rd_Atom(args[2])); /* arg #2 is the name of the op */ result = Pl_Query_Next_Solution(); } Pl_Query_End(PL_RECOVER); return Un_Proper_List_Check(n, op, list); } </PRE></DL> Note that we know here that there is no source for exception. In that case the result of <TT>Pl_Query_Call</TT> and <TT>Pl_Query_Next_Solution</TT> can be considered as a boolean.<BR> <BR> The compilation produces an executable called <TT>examp</TT>: <DL COMPACT=compact><DT><DD><TT>% gplc examp.pl examp_c.c</TT></DL> Example of use: <DL COMPACT=compact><DT><DD> <PRE> | ?- all_op(L). L = [:-,:-,\=,=:=,#>=,#<#,@>=,-->,mod,#>=#,**,*,+,+,',',...] | ?- findall(X,current_op(_,_,X),L). L = [:-,:-,\=,=:=,#>=,#<#,@>=,-->,mod,#>=#,**,*,+,+,',',...] </PRE></DL> <HR SIZE=2> Copyright (C) 1999-2002 Daniel Diaz <BR> <BR> Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. <BR> <BR> <A HREF="index.html#copyright">More about the copyright</A> <HR> <A HREF="manual068.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual065.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual070.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>