1 / 13

Non-determinism and Cut

Learn about non-determinism, cut, and backtracking in Prolog programming, including how to control backtracking and use cut to improve efficiency. Explore examples and understand the advantages and limitations of these concepts.

klovato
Download Presentation

Non-determinism and Cut

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Non-determinism and Cut Non-determinism Backtracking Controlling Backtracking

  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. 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. 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. 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. An example – finding a common element between two lists

  6. f(X, 0):- X < 3. f(X, 2):- 3=<X, X<6. f(X, 4):- 6=<X. ?- f(1,Y), 2 < Y. Sometimes, backtracking is not necessary ……

  7. 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, … ……we need to tell Prolog explicitly not to backtrack

  8. "!" 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. Logical Meaning

  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. 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. 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. 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. 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).

More Related