6.5.5 Reading a goal from a string

A simple way to call arbitrary Prolog code is to use SP_read_from_string() (see Creating Prolog Terms) to create an argument to call/1. It is a good idea to always explicitly specify the module context when using call/1 or other meta-predicates from C.

This example calls a compound goal (without error checking):

SP_pred_ref call_pred = SP_predicate("call", 1, "prolog");
SP_term_ref x = SP_new_term_ref();
SP_term_ref goal = SP_new_term_ref();
SP_term_ref vals[] = {x, 0 /* zero termination */};
SP_integer len;

/* The X=_ is a trick to ensure that X is the first variable
   in the depth-first order and thus corresponds to vals[0] (x).
   There are no entries in vals for _,L1,L2.
   "user:(X=_, length([0,1,2],L1), length([3,4],L2), X is L1+L2).", vals);

SP_query(call_pred, goal);
SP_get_integer(x, &len);
/* here len is 5 */

Send feedback on this subject.