Chapter 6-1. Chapter 6-2 Chapter 6 Inventories Accounting Principles, Ninth Edition.
Chapter 6
-
Upload
muireann-chesney -
Category
Documents
-
view
22 -
download
3
description
Transcript of Chapter 6
Chapter 6
Control Backtracking
Chapter 6 2344-302 LP and Prolog
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 oron the set of clauses.
Chapter 6 3344-302 LP and Prolog
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 4344-302 LP and Prolog
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.pro
Cut
Chapter 6 5344-302 LP and Prolog
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 6344-302 LP and Prolog
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 7344-302 LP and Prolog
Evaluation of a query
Recursive cycle of unification (pattern matching)
Subgoal evaluationEvaluate the body of a clauseIf unification fail, go on to the next clause
Ex: uncle(john,daniel) :- brother(john,bill),
father(bill, daniel)
Chapter 6 8344-302 LP and Prolog
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 9344-302 LP and Prolog
Unification
4. A constant unifies with a constant, if they are identical? likes(john, dog)
yes5. 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 10344-302 LP and Prolog
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.
Chapter 6 11344-302 LP and Prolog
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
Chapter 6 12344-302 LP and Prolog
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
Chapter 6 13344-302 LP and Prolog
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 14344-302 LP and Prolog
case 1ancestor(A,C) :- parent(A,C). /*base case */
ancestor(A,C) :- parent(A,B),ancestor(B,C).
Recursive procedures
case 2ancestor(A,C) :- parent(A,C). /*base case */
ancestor(A,C) :- ancestor(B,C), parent(A,B).
OK
OK
Chapter 6 15344-302 LP and Prolog
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
Yes
Yes
Chapter 6 16344-302 LP and Prolog
Family Tree
ellen
john Lisa
tom sherry
eric
mark
L1
L2
L3
L4
L5
TOP
Bottom
case 1
case 2
Chapter 6 17344-302 LP and Prolog
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
Yes
Yes
Chapter 6 18344-302 LP and Prolog
Both people like the same thingpredicates both_like(symbol,symbol,symbol) like(symbol,symbol) same_thingclauses 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 19344-302 LP and Prolog
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 20344-302 LP and Prolog
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 21344-302 LP and Prolog
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 22344-302 LP and Prolog
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 23344-302 LP and Prolog
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
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 ได&เหม�อนหร�อแตกต�างก+นอย�างไรให&น+กศึ-กษายกต+ว่อย�างขี&อเที่�จจร�งขี-.นเอง พื่ร&อมอธิ�บายผล็การที่#างานที่��ได&จากการร+นโปรแกรมโดยล็ะเอ�ยด
LAB :count up / count down
จงเขี�ยนโปรแกรมภาษาโปรล็�อก 1) เพื่��อพื่�มพื่�เล็ขีค��เพื่��มขี-.นจาก จาก 0 ถ-ง 30
2) พื่�มพื่�เล็ขีค��ล็ดล็งจาก 29 ถ-ง 1
The end