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)
#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); }
| ?- my_call(write(hello)). | ||
hello | ||
| ?- my_call(for(X,1,3)). | ||
X = 1 ? | (here the user presses ; to compute another solution) | |
X = 2 ? | (here the user presses ; to compute another solution) | |
X = 3 | (here the user is not prompted since there is no more alternative) | |
| ?- my_call(1). | ||
{exception: error(type_error(callable,1),my_call/1)} | ||
| ?- my_call(call(1)). | ||
no |
#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; }
| ?- my_call(call(1)). | ||
{exception: error(type_error(callable,1),my_call/1)} |
#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; }
#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); }
| ?- all_op(L). L = [:-,:-,\=,=:=,#>=,#<#,@>=,-->,mod,#>=#,**,*,+,+,',',...] | ?- findall(X,current_op(_,_,X),L). L = [:-,:-,\=,=:=,#>=,#<#,@>=,-->,mod,#>=#,**,*,+,+,',',...]