Download presentation
Presentation is loading. Please wait.
1
1 Non-determinism and Cut Non-determinism Backtracking Controlling Backtracking
2
2 Non-determinism in Prolog A Prolog program p :- a,b,c. can be read as: to solve p, we need to do a, do b, then do c. How about p :- a,b,c. p :- a,f,g. It reads as, there are 2 ways to solve: try a,b,c first; if failed,then try a,f,g.
3
3 Choice point Whenever there is more than one alternative ways to proceed, Prolog tries the first one, and creates a choicepoint to remember remaining alternatives. Choicepoint is like a tree node, from which different paths are extended.
4
4 Backtracking The execution of Prolog programs is the searching for a successfully terminated path. When Prolog discovers an unsuccessful branch, it automatically backtracks to the previous choicepoint and tries to apply an alternative stored at that node.
5
5 An example – finding a common element between two lists member(X, [X|_]). member(X, [_|L]):- member(X,L). intersect(X, L1, L2):- member(X, L1), member(X, L2). ?-intersect(S,[3,1,4],[2,4,5]). S=4 How many backtrackings? How about ?-read_list(L1), read_list(L2), read_list(L3), read_list(L4), intersect(X, L1, L2), intersect(Y, L3, L4), X is 2*Y.
6
6 Sometimes, backtracking is not necessary …… f(X, 0):- X < 3. f(X, 2):- 3=<X, X<6. f(X, 4):- 6=<X. ?- f(1,Y), 2 < Y.
7
7 ……we need to tell Prolog explicitly not to backtrack The 3 rules about the f function are mutually exclusive so that one of them at most will succeed. We know that as soon as one rule succeed there is no point in trying to use the others (Prolog doesn’t know!) How to tell Prolog about this? f(X, 0) := X < 3, !. f(X, 2):- 3=<X, X<6, !. f(X, 2):- 6=<X. The symbol ! Is called ‘cut’. It means, …
8
8 Logical Meaning "!" is a special built- in. When it is used in a program as below: % program 1 g:- p, !, q. g:- r, !, s. g:- t. Logically, program 1 is equivalent to the following program: % program 2 g:- p, q. g:- not(p), r, s. g:- not(p), not(r), t.
9
9 Ex. max(X,Y,Max) means Max is the max value among X and Y. max(X,Y,X):- X >= Y. max(X,Y,Y):- X < Y. Using cut, the above program can be written as : max(X,Y,X):- X >= Y, !. max(X,Y,Y).
10
10 Procedural Meaning The first (or second) cut in program 1 means that if the computation can reach it then all the remaining alternative clauses in p (or r) and g are discarded. % program 1 g:- p, !, q. g:- r, !, s. g:- t.
11
11 Example - How to use cut in this program? rule_for_plus(X, 0, X). rule_for_plus(0, X, X). rule_for_plus(X, Y, X+Y) :- X \== 0, Y \== 0. % a new version which uses cut rule_for_plus(X, 0, X):- !. rule_for_plus(0, X, X):- !. rule_for_plus(X, Y, X+Y).
12
12 Example - How to use cut in this program? has4c([A1,A2,A3,A4|_]):- % found it A1 == A2, A1 == A3, A1 == A4. has4c([A1,A2,A3,A4|T]):- % otherwise (A1 \== A2; A1 \== A3; A1 \== A4), has4c([A2,A3,A4|T]). %---------- a new version which uses cut --------- has4c([A1,A2,A3,A4|_]):- % found it, CUT here A1 == A2, A1 == A3, A1 == A4, !. has4c([_|T]):- % otherwise has4c(T).
13
13 What is the advantage to use this cut? How to remove the cut used here? % checking4(L) - go through every element % in L, stop if find a 4c checking4([H|_]):- has4c(H), !. checking4([_|T]):- checking4(T).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.