0
votes

Given the following:

R(p):-
    a(p),
    b(p),
    c(p).

When c fails, it will backtrack to b and unbind b. However, I want it to backtrack from a and unbind b as well as a instead. Is it possible to do this?

1

1 Answers

0
votes

You may use an if->then construct to commit the changes from the goal you do not want to backtrack (put them on the left hand side of ->/2).

For example if you want to skip backtracking for the b/1 goal you may do this:

r(P):-
  a(P),
  ( 
     b(P) 
  -> c(P)
  ).

with these test facts:

a([a1|_]).
a([a2|_]).

b([_,b1|_]).
b([_,b2|_]).

c([_,_,c1]).
c([_,_,c2]).

and this sample query:

?- r(P).
P = [a1, b1, c1] ;
P = [a1, b1, c2] ;
P = [a2, b1, c1] ;
P = [a2, b1, c2].