Terminating a Nondeterminate Prolog Query

QP_close_query() and QP_cut_query() are functions that are used to terminate an open nondeterminate Prolog query. They differ only in their effect on the Prolog heap, which can be reflected in the solutions Prolog has returned to C.

The difference between QP_close_query() and QP_cut_query() can best be understood with reference to Prolog's control flow. QP_close_query() is equivalent to the Prolog call !, fail. The cut renders the current computation determinate, removing the possibility of future backtracking. The following call to fail/0 then initiates backtracking to the next previous parent goal with outstanding alternatives. In doing so it pops the Prolog heap to its state when the parent goal succeeded, in effect throwing away any terms created since that parent goal.

In contrast, just calling ! in Prolog renders the computation determinate without initiating backtracking. Any terms created since the parent goal are retained.

In the context of calling Prolog from foreign languages, terminating a query using QP_close_query() generally means throwing away the most recent solution that was calculated, unless that solution has been copied into a more permanent place. (Of course, any previous solutions must also be assumed to have been overwritten by subsequent solutions unless copied elsewhere!) The converse of this behavior is that closing a query using QP_close_query() automatically frees up the Prolog memory that holds the last solution.

Terminating a query using QP_cut_query() renders the computation determinate, but as it is not failed over the Prolog heap is not popped. Thus when terminating a query using QP_cut_query() more space is retained, but so is the most recent solution.