1 / 18

Non-determinism and Cut

Non-determinism and Cut . Non-determinism Backtracking Controlling Backtracking. 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 :- e ,f,g .

gale
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 :- e,f,g. It reads as, there are 2 ways to solve p: try a,b,c first; if failed, then try e,f,g.

  3. Example for last slide % our classic example cango(X,Y):- arrow(X,Y). cango(X,Z):- arrow(X,Y), cango(Y,Z). % What if we change % the order? cango(X,Z):- arrow(X,Y), cango(Y,Z). cango(X,Y):- arrow(X,Y).

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

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

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

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

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

  9. "!" 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

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

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

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

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

  14. Similar to ‘has4c’, in our chatbot,when try to extract info for ‘cango’… new pattern_tofm([to, X, from, Y|_], X, Y):- ! . pattern_tofm([to,room,X,from,room,Y|_], X, Y):- ! . …… pattern_tofm([_|T], X, Y):- % carry on checking pattern_tofm(T, X, Y). is_valid_loc(X):-next(X,_,_,_,_). • Use ‘cut’ for deterministic program. • Watch out the order: recursion is always at the end.

  15. More Examples – be careful when dealing with IO write_list([]):- nl. write([H|T]):- write(H), write_list(T). % what if we change it to write_list(X):- nl, X=[]. write(X):- X=[H|T], write(H), write_list(T). write_list([X]):- write(X), nl. write([H|T]):- write(H), write_list(T).

  16. chatbot:- print_welcome, conversations. conversations:- repeat, % prolog built-in print_prompt(you), readin(S), % defined in file % readin.pl gen_reply(S,R), print_prompt(me), write_list(R), is_quit(S). is_quit(S):- member(bye, S). gen_reply(S, R):- % totally random respones_db(random, Res), % create a random number length(Res, Length), Upper is Length+1, random(1, Upper, Rand), nth_item(Res, Rand, R). Top level of Chatbot (from lec 6)what you need to do? - add gen_reply

  17. Compulsory questions in demo • ‘what is your name?’, • ‘what are you studying?’, • ‘where is the reception?’, • ‘is there a café in this building?’, • ‘how to get out of this building?’. • Need to add a lot of pattern checking or use a parser (last week’s contents) • Add more information, e.g. locker, vending machines etc • Can make a guess, ‘where is exit?’ you can assume that he is at reception and wants to go exit4 • (optional) Correct typos. ‘similar(X,Y)’ check if X and Y only have one letter difference.

  18. You also need to collect at least three pieces of information To do this, we need to change conversation from passive (question by user) to active (question by us) …… gen_reply(S, R):- not_question(S), !,% or any other conditions get_alevel_info_loop, R = [‘Thank’, you, very, much, ‘!’]. get_alevel_info_loop:- print_prompt(me), write_list([‘What’, subjects, are , you, taking, ‘?’), print_prompt(you), readin(S), get_alevel_info_loop(S). get_alevel_info_loop(S):- is_valid_alevel_list(S), !, assert( .... ). get_alevel_info_loop(_):- get_alevel_info_loop.

More Related