1 / 26

Chapter 6

Chapter 6. Control Backtracking. 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

Download Presentation

Chapter 6

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. Chapter 6 Control Backtracking

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

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

  4. Cut LP24.pro 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. Chapter 6

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

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

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

  8. 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) Chapter 6

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

  10. 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 thefirst subgoal in the compound query, then when it is fail, the compound query fail as well. Chapter 6

  11. LP Lec20.pro 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). Chapter 6

  12. LP Lec20.pro 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. Chapter 6

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

  14. Recursive procedures • case 1 ancestor(A,C):-parent(A,C)./*base case */ ancestor(A,C):-parent(A,B),ancestor(B,C). OK • case 2 • ancestor(A,C):-parent(A,C)./*base case */ • ancestor(A,C):-ancestor(B,C),parent(A,B). OK Chapter 6

  15. Q1 ?ancestor1(ellen,tom) Q2 ?ancestor1(ellen,mark) Q3 ?ancestor1(ellen,sherry) Yes Yes Yes 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 */ Chapter 6

  16. ellen Lisa john sherry tom eric mark Family Tree TOP L1 L2 L3 L4 L5 case 1 case 2 Bottom Chapter 6

  17. Q1 ?ancestor2(ellen,tom) Q2 ?ancestor2(ellen,mark) Q3 ?ancestor2(ellen,sherry) Yes Yes Yes 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 */ Chapter 6

  18. 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). Chapter 6

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

  20. 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). Chapter 6

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

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

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

  24. จงเขียนโปรแกรมภาษาโปรล็อกเพื่อพิสูจน์ว่าประโยคRecursiveในแบบที่ 1 และ แบบที่ 2 ที่กำหนดให้ว่าสามารถใช้พิสูจน์การทำงานakoได้เหมือนหรือแตกต่างกันอย่างไร ให้นักศึกษายกตัวอย่างข้อเท็จจริงขึ้นเอง พร้อมอธิบายผลการทำงานที่ได้จากการรันโปรแกรมโดยละเอียด 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 */

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

  26. The end

More Related