Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Control Backtracking. 344-302 LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack.

Similar presentations


Presentation on theme: "Chapter 6 Control Backtracking. 344-302 LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack."— Presentation transcript:

1 Chapter 6 Control Backtracking

2 344-302 LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack beyond the point at which it occurs. The cut is used to reduce the size of the search space of a query. Cut may effect : on a compound query or on the set of clauses.

3 344-302 LP and Prolog Chapter 63 Cut Compound query Get a value of X from “ a ” Get a value of Y from “ b ” Get a value of X,Y,Z from “ c ” Goal : a(X), b(Y), !, c(X,Y,Z). It will not go beyond ! command

4 344-302 LP and Prolog Chapter 64 predicates a(integer) b(integer) c(integer,integer,integer) run1 /* when success then stop */ run2 /* find all solution, backtrack all "b" value first before backtrack "a" value */ run3 /* not go further than ! */ run4 /* fail and cut work together */ run5 /* fail and cut work together, not go to "a" value */ clauses a(3). a(7). a(9). b(10). b(200). c(S,T,U) :- U = S + T. run1 :- a(X), b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl. run2 :- a(X), b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl, fail. run3 :- a(X), b(Y), !, c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl. run4 :- a(X), b(Y), !, c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl, fail. run5 :- a(X),!, b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ", Z), nl, fail. LP24.proCut

5 344-302 LP and Prolog Chapter 65 Factorial Ex07EX03.pro /* Recursive program to compute factorials. */ predicates factorial(integer, real) clauses factorial(1, 1) :- !. factorial(X, FactX) :- Y = X-1, factorial(Y, FactY), FactX = X*FactY.

6 344-302 LP and Prolog Chapter 66 Factorial (2) /* Recursive program to compute factorials. */ predicates factorial(integer, integer) clauses factorial(1, 1) :- !. factorial(FactX, X ) :- Y = X-1, factorial( FactY, Y), FactX = X*FactY.

7 344-302 LP and Prolog Chapter 67 Evaluation of a query Recursive cycle of unification (pattern matching) Subgoal evaluation Evaluate the body of a clause If unification fail, go on to the next clause Ex: uncle(john,daniel) :- brother(john,bill), father(bill, daniel)

8 344-302 LP and Prolog Chapter 68 Unification 1. A variable unifies with a constant ? X = john 2. A variable unifies with a variable ? X = Y 3. _ unifies with anything ? likes(_,What)

9 344-302 LP and Prolog Chapter 69 Unification 4. A constant unifies with a constant, if they are identical ? likes(john, dog) yes 5. A structure unifies with a structure if the structure names are the same and if the arguments can be unified. ? father(john) = father(X) X = john

10 344-302 LP and Prolog Chapter 610 Failure and backtracking If the active query is part of compound query (a list of subgoal) and is not the first subgoal in the compound query then the interpreter backtracks to reconsider the previous subgoal in the compound query. If the active query is the first subgoal in the compound query, then when it is fail, the compound query fail as well.

11 344-302 LP and Prolog Chapter 611 predicates uncle(symbol,symbol) brother(symbol,symbol) father(symbol,symbol) clauses brother(daniel, kenneth). brother(daniel, jim). brother(john,jim). father(bill, daniel). father(kenneth, bill ). uncle(U,N) :- brother(U,B), father(B,N). LP Lec20.pro

12 344-302 LP and Prolog Chapter 612 predicates register(symbol) age(symbol,integer) male(symbol) clauses male(brain). male(mike). male(steve). age(brain, 18). age(mile, 17). age(steve, 18). register(X) :- male(X), age(X,Y), Y = 18. LP Lec20.pro

13 344-302 LP and Prolog Chapter 613 Recursive procedures 1. A nonrecursive clause defining the base case of the procedure, that is where the recursion stops. 2. A recursive rule. In the body of this rule, the first subgoals generate new argument values. Then follows a recursive subgoal utilizing the new argument values.

14 344-302 LP and Prolog Chapter 614 case 1 ancestor(A,C) :- parent(A,C). /*base case */ ancestor(A,C) :- parent(A,B),ancestor(B,C). Recursive procedures case 2 ancestor(A,C) :- parent(A,C). /*base case */ ancestor(A,C) :- ancestor(B,C), parent(A,B). OK

15 344-302 LP and Prolog Chapter 615 Case 1 : lp23c1.pro : Top down predicates ancestor1(symbol,symbol) parent(symbol,symbol) clauses parent(ellen, john). parent(ellen, lisa). parent(lisa, sherry). parent(john, tom). parent(tom, eric). parent(eric, mark). /*......... case 1.............. */ ancestor1(A,C) :- parent(A,C). /* base case */ ancestor1(A,C) :- parent(A,B), ancestor1(B,C). /* recursive clause */ Q1 ?ancestor1(ellen,tom) Q2 ?ancestor1(ellen,mark) Q3 ?ancestor1(ellen,sherry) Yes

16 344-302 LP and Prolog Chapter 616 Family Tree ellen john Lisa tom sherry eric mark L1 L2 L3 L4 L5 TOP Bottom case 1 case 2

17 344-302 LP and Prolog Chapter 617 Case 2 : lp23c2.pro : Bottom up predicates ancestor2(symbol,symbol) parent(symbol,symbol) clauses parent(ellen, john). parent(ellen, lisa). parent(lisa, sherry). parent(john, tom). parent(tom, eric). parent(eric, mark). /*......... case 2..............*/ ancestor2(A,C) :- parent(A,C). /* base case */ ancestor2(A,C) :- ancestor2(B,C), parent(A,B). /* recursive clause */ Q1 ?ancestor2(ellen,tom) Q2 ?ancestor2(ellen,mark) Q3 ?ancestor2(ellen,sherry) Yes

18 344-302 LP and Prolog Chapter 618 Both people like the same thing predicates both_like(symbol,symbol,symbol) like(symbol,symbol) same_thing clauses both_like(First,Second, Thing) :- like(First,Thing), like(Second,Thing), First <> Second. same_thing :- both_like(First,Second,Thing), write(First, " and ",Second," like ",Thing, "\n \n"). like(kosin,water). like(dum,wine). like(dum,salad). like(nipa,salad). like(nipa,ice_cream). like(nipa,wine).

19 344-302 LP and Prolog Chapter 619 Count down case 1 predicates countdown1. numb(integer). clauses numb(5). numb(4). numb(3). numb(2). numb(1). numb(0). countdown1 :- numb(X), write(X," "), fail.

20 344-302 LP and Prolog Chapter 620 Count down case 2 (p.44 thai bk) predicates countdown(integer). clauses countdown(0) :- write("0 "). countdown(N) :- write(N," "), M = N - 1, countdown(M).

21 344-302 LP and Prolog Chapter 621 Count up case 1 (p.48 thai bk) predicates countup1. numb(integer). clauses numb(0). numb(1). numb(2). numb(3). numb(4). numb(5). countup1 :- numb(X), write(X," "), fail.

22 344-302 LP and Prolog Chapter 622 Count up case 2 (p.49 thai bk) predicates countup2. numb(integer). clauses countup2 :- numb(X), write(X," "), fail. numb(0). numb(A) :- numb(B), A = B + 1. Use Fail Loop until ………… infinity

23 344-302 LP and Prolog Chapter 623 Count up case 3 (p.49 thai bk) domains X = integer predicates countup3(integer). numb(integer). clauses countup3(N) :- numb(X), write(X," "), X = N. numb(0). numb(A) :- numb(B), A = B + 1. Do until X = N Loop until ………… N

24 LAB : Recursive clause /*......... แบบที่ 1.............. */ ako1(A,C) :- isa(A,C). /* base case */ ako1(A,C) :- isa(A,B), ako1(B,C). /* recursive clause */ /*......... แบบที่ 2..............*/ ako2(A,C) :- isa(A,C). /* base case */ ako2(A,C) :- ako2(B,C), isa(A,B). /* recursive clause */ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อพิสูจน์ว่าประโยค Recursive ในแบบที่ 1 และ แบบที่ 2 ที่กำหนดให้ว่า สามารถใช้พิสูจน์การทำงาน ako ได้เหมือนหรือแตกต่างกัน อย่างไร ให้นักศึกษายกตัวอย่างข้อเท็จจริงขึ้นเอง พร้อม อธิบายผลการทำงานที่ได้จากการรันโปรแกรมโดยละเอียด

25 LAB :count up / count down จงเขียนโปรแกรมภาษาโปรล็อก 1) เพื่อพิมพ์เลขคู่เพิ่มขึ้นจาก จาก 0 ถึง 30 2) พิมพ์เลขคี่ลดลงจาก 29 ถึง 1

26 The end


Download ppt "Chapter 6 Control Backtracking. 344-302 LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack."

Similar presentations


Ads by Google