This section describes a set of Elisp functions that let you invoke Prolog queries and interact with the embedded Prolog runtime:
Query the Prolog predicate mod:functor/2
in the
context of the module cxt. Convert input to a Prolog
term and use it as the first argument, unless reverse is
non-nil
, in which can use input as the second argument.
The other argument is called the output argument of the query,
it is expected to be unified with some output that the query wants to
return to Elisp. The output argument can be retrieved with
sweeprolog-next-solution
. This function always returns
t
when its arguments are valid, otherwise it returns
nil
.
Return the next solution of the last Prolog query. Return a cons cell
(det . output)
if the query succeeded, where
det is the symbol !
if no choice points remain and
t
otherwise, and output is the output argument of the
query converted to an Elisp S-expression. If there are no more
solutions, return nil
instead. If a Prolog exception was
thrown, return a cons cell (exception . exp)
where
exp is the exception term converted to Elisp.
Cut the last Prolog query. This releases any resources reserved for
it and makes further calls to sweeprolog-next-solution
invalid
until you open a new query.
Close the last Prolog query. Similar to sweeprolog-cut-query
expect that any unifications created by the last query are dropped.
Sweep provides the Elisp function sweeprolog-open-query
for
invoking Prolog predicates. The predicate you invoke via this
function must be of arity two, and it will be called in mode
p(+In, -Out)
—the predicate should treat the first argument as
input and expect a variable as the second argument, which it should
unify with some output. This restriction is placed in order to
facilitate a natural calling convention between Elisp, a functional
language, and Prolog, a logical one.
The sweeprolog-open-query
function takes five arguments, the
first three are strings which denote:
The fourth argument to sweeprolog-open-query
is converted into
a Prolog term and used as the first argument of the predicate
(see Conversion of Elisp objects to Prolog terms). The fifth argument is an optional
reverse flag—when this flag is set to non-nil
, the
order of the arguments is reversed such that the predicate is called
in mode p(-Out, +In)
rather than p(+In, -Out)
.
To examine th results of a Prolog query, use the function
sweeprolog-next-solution
. If the query succeeded,
sweeprolog-next-solution
returns a cons cell whose car
is either the symbol !
when the success was deterministic or
t
otherwise, and the cdr
is the current value of the
second (output) Prolog argument converted to an Elisp object
(see Conversion of Prolog terms to Elisp objects). If the query failed,
sweeprolog-next-solution
returns nil.
Sweep only executes one Prolog query at a given time, so you need to
close close queries that you open with sweeprolog-open-query
before opening new ones. When no more solutions are available for the
current query (sweeprolog-next-solution
returns nil
), or
when you’re otherwise not interested in more solutions, you must close
the query with either sweeprolog-cut-query
or
sweeprolog-close-query
. Both of these functions close the
current query, but sweeprolog-close-query
also destroys any
Prolog bindings that it created.