Title: Controlling Backtracking
1Controlling Backtracking
2Contents
- Preventing backtracking
- Examples using cut
- Negation as failure
- Problems with cut and negation
3Preventing Backtracking
- Automatic backtracking is a useful programming
concepts because it relieves the programmer of
the burden of programming backtracking
explicitly. - However, uncontrolled backtracking may cause
inefficiency. - Prolog provides cut to control or prevent
backtracking.
4Preventing Backtracking
- Consider the double step function
- Rule 1 if Xlt3 then Y0
- Rule 1 if 3?X and Xlt6 then Y2
- Rule 3 if 6 ?X then Y4
- f(X,0)- Xlt3.
- f(X,2)- 3ltX,Xlt6.
- f(X,4)- 6ltX.
5Preventing Backtracking
- Experiment 1
- ?- f(1,Y),2ltY.
f(1,Y) 2ltY
f(X,0)- Xlt3,!. f(X,2)- 3ltX,Xlt6,!. f(X,4)-
6ltX.
Rule 1 Y0
Rule 3 Y4
Rule 2 Y2
1lt3 2lt0
f(1,Y) 2ltY
f(1,Y) 2ltY
Cut
2lt0
6Preventing Backtracking
- Experiment 2
- ?-f(7,Y).
- Y4
- Try Rule 1 7lt3 fails backtrack.
- Try Rule 2 7?3 succeeds, but 7lt6 fails
backtrack. - Try Rule 3 6?7 succeeds.
- The third version becomes
Multiple results will be resulted, if the cuts
are removed.
f(X,0)- Xlt3,!. f(X,2)- Xlt6,!. f(X,4).
7Preventing Backtracking
- Consider a clause of the form
- H-B1, B2, ?Bm, !, ?, Bn.
- Assume that this clause was invoked by a goal G
that match H. At the moment that the cut is
encountered, the system has already found some
solution of the goal B1, ?Bm. When the cut is
executed, this (current) solution of B1, ?Bm
becomes frozen and all possible remaining
alternatives are discarded.
8Preventing Backtracking
- C-P, Q, R, !, S, T, U.
- C-V.
- A-B, C, D.
- Backtracking will be possible within P, Q, R
alternative solution of P, Q, R are suppressed,
as soon as the cut is encountered. The
alternative clause of C will also be discarded. - Backtracking within S, T, U will still be
possible.
9Examples Using Cut
max(X,Y,X)- XgtY. max(X,Y,Y)-
XltY. max(X,Y,X)- XgtY, !. max(X,Y,Y).
10Examples Using Cut
- Single-solution membership
member(X,X_)- !. member(X,YL)-
menmber(X,L). ?- member(X,a,b,c). Xa no
11Examples Using Cut
- Adding an element to a list without duplication
add(X,L,L)-member(X,L), !. add(X,L,XL). ?-add
(a,b,c,L). La,b,c ?-add(X,b,c,L). Lb,c X
b
12Examples Using Cut
- Classification into categories
beat(tom,jim). beat(ann,tom). beat(pat,jim). clas
s(X,fighter)- beat(X,_),beat(_,X),
!. class(X,winner)- beat(X,_),
!. class(X,sportsman)- beat(_,X).
13Negation as Failure
- How to represent Mary likes all animals but
snakes in Prolog?
likes(mar,X)- snake(X), !, fail animal(X).
- The same idea can be applied to define the
relation different(X,Y).
different(X,Y)- XY, !, fail true.
14Negation as Failure
- Define not for the above relations
not(P)- P, !, fail true likes(mary,X)-
animal(X), not(snake(X)). different(X,Y)-
not(XY).
15Problems with Cut and Negation
- Advantages of using cut
- Improve the efficiency of the program
- The idea is to tell Prolog do not try other
alternatives because they are already found. - Specify exclusive rules.
16Problems with Cut and Negation
- Disadvantages of using cut
- Lose the valuable correspondence between the
declarative and procedural meaning of program. - The declarative meaning of
p-a, b. p- c.
is p?(ab)?c, while
p-a, !, b. p- c.
becomes p?(ab)?(ac), and
p-a, !, b. p- c.
p?c ?(ab).
17Problems with Cut and Negation
- The relation not does not correspond to negation
in mathematics. - For the query
- ?-not human(mary).
- Prolog answers yes means there is not enough
information in the program to prove that Mary is
human. - Such reasoning is based on the closed-world
assumption.
18Problems with Cut and Negation
good_standard(jeanluis). expensive(jeanluis). good
_standard(francesco).reasonable(Restaurant)-
not expensive(X). ?- good_standard(X),reasonable(
X). X francesco ?- reasonable(X),
good_standard(X). no