[comp.lang.prolog] "Delayed cut" without redundancy and databases

chomicki@aramis.UUCP (04/04/87)

The same trick with the current universe of atoms (see my previous
posting) solves the second problem: a complex backtracking schema.

   Given a predicate
      P :- A, B, C, D.
   Is it possible to rewrite P to obtain the following behavior:
   If the goal A initially succeeds, then
      if B doesn't succeed, backtrack into A.  (This is quite normal.)
   However, if A initially succeeds, and B does too,
      then prevent backtracking into A.

I will consider the following predicate p/2:
p(X,Y):-a(X), b(X,Y).
which illustrates all the salient points of the problem except the
presence of C and D. That will be dealt with at the end.
The difficulty here is the position of the cut: we'd like to cut the
computation once the first binding for X (from a/1) and all the Y
corresponding to it are obtained (from b/2), provided there is at
least one such Y. Looking at the text, there is apparently no place
where we can put the cut to ensure the above behavior. But:

p(X,Y):- q(X,Y), Y\=='$$$$$'.
q(X,Y):- pick(String),
	a(X),
		( b(X,Y), there_was_once(String);
		  was_there_once(String) -> Y='$$$$$', ! ).

pick(String):-new_string(String), \+ was_there_once(String), !.
there_was_once(String):- name(_,String).
was_there_once(String):- current_atom(Atom), name(Atom,String).

new_string([112]).
new_string([112|L]):-new_string(L).

NOTE 1: The cut is delayed: it appears only after b/2 has succeeded
at least once.

NOTE 2: '$$$$$' is a null value which gives a void success(i.e. without
answer bindings). If C is present (as in the original problem),
we have to propagate this void success through it. This should
do:
q(X,Y,Z):- pick(String),
        a(X),
                ( b(X,Y), there_was_once(String) ;
                  was_there_once(String) -> Y='$$$$$', ! ),
			( Y\=='$$$$$' -> c(Y,Z);
			  otherwise  -> true).	
We can not put c/2 in the same Or-branch as b/2, because
backtracking to a/1 depends upon the success/failure of b/2 only.
-- 
Jan Chomicki (chomicki@aramis.rutgers.edu)	Phone: (201) 932-3999
Dept.of Computer Science, Rutgers University, New Brunswick, NJ 08903
Usenet: {...harvard,pyramid,seismo,nike}!rutgers!aramis!chomicki
Arpanet: chomicki@aramis.rutgers.edu