[bp-users]Re: once/1 for tabled predicates in B-Prolog

Neng-Fa Zhou nzhou@acm.org
Wed, 18 Feb 2004 17:20:54 -0500


Hi, some users of B-Prolog have asked about how to implement "once/1" for
tabled predicates in B-Prolog. Here is my response. Hope it is of interest
to you.

Since version 6.4, B-Prolog has adopted a new strategy called lazy answer
consumption for tabled subgoals. Under this strategy, answer consumption is
postponed for tabled subgoals until all clauses are tried (for top-most
looping subgoals answer consumption is postponed until the fixpoints are
reached). This strategy is similar to the local scheduling strategy adopted
in XSB. This strategy is more efficient in terms of both time and space than
the eager consumption strategy for applications that require all-solution
search such as deductive databases and machine learning systems .
Nevertheless, for certain other applications such as planning and theorem
proving, it is unrealistic to find all answers either because the search
space is huge or because only one answer is needed.

B-Prolog provides a predicate called "table_find_one(Call)" which succeeds
if there is a variant of Call registered in the table that has at least one
answer in the answer table. Call is unified with the first answer in the
answer table after the call. This non-logical built-in can be used to
implement once/1. In concrete, let p/1 be a tabled predicate in a program
for which only one answer is needed. We replace each call p(X) in the
program with once_p(X)  and define once_p/1 as follows:

once_p(X):-table_find_one(p(X)),!.
once_p(X):-p(X).

In this way, p(X) will stop producing any new answers once an answer has
been produced. But notice that you cannot expect p(X) to return you all the
answers in the search space after this transformation. If you want to find
all answers some times and one answer some other times, you need to have two
versions of the predicate. See the examples "farmer.pl" and "water.pl" in
the directory  "examples/tabling/" (you need to re-download the package from
www.probp.com if you have an old version).

I'll change the definition of  "once/1" in the next version so that this
kind of transformation will become unnecessary. For the time being, you can
use this technique to solve possible problems.

Regards,
Neng-Fa Zhou