Presentation on theme: "Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,"— Presentation transcript:
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms, lists Tail Recursion & Real Recursion U-bend & rope ladder to collect results Reading: Ch2: How to run Prolog, Ch3: The Prolog Language Ch5: Loading Programs Ch6: Debugging Ch7: Built-In Predicates
Comp 307 Lecture 4:3 min_number(+List, -Min) min_number(, ). min_number(, ):- min_number(, ), ………….. min_number(, ):- min_number(, ), …………………. Notes: it should be simplified by defining min2(+number1, +number2, Min) Be careful with “is” Tutorial and help desk details on the Web page.
Comp 307 Lecture 4:4 Prolog and a small rule based system How Prolog works? Matching Backtracking Rule based system
Comp 307 Lecture 4:7 Prolog answers a query by matching the query with FACTs and the HEADs of the rules – If the query matches a fact, then succeed – If the query matches the head of a rule, then the body of the rule is now treated as a query, and prolog tries to solve that – Top-down, left-to-right, Depth-first If Prolog’s search fails, it backtracks to try alternative solutions, if possible. How Prolog answers a query
Comp 307 Lecture 4:8 How Prolog answers a query |?- grandparent(bob, Y).
Comp 307 Lecture 4:9 When – Arguments at the same place – The same variable in one clause – X = Y How – Propagate variable bindings forward – Once a variable has a binding, it cannot be changed -- there is no destructive assignment Matching
Comp 307 Lecture 4:10 Backtracking When – When a sub goal failed – Multiple answers, using “;” – ……., fail. How: – Undo variable bindings, try alternative clauses
Comp 307 Lecture 4:11 How Prolog answers a query |?-ancestor(bob, pat).
Comp 307 Lecture 4:13 Term Term Possible Instantiation a 6 no a Z Z=a A C A=C date(2, 3, 2000) Z Z=date(2, 3, 2000) date(D, M, Y) Z Z=date(D, M, Y) date(X, Y, Z) date(Y, X, Z) X=Y date(D, 5, Y) date(D1, M, 2000) D=D1, M=5, Y=2000 date(6, 5, 2000) date(6, may, 2000) no date(D, M, Y) date1(D, M, Y) no Matching (Unification)
Comp 307 Lecture 4:14 Term Term Possible Instantiation   yes [a, b, c] [X, Y, c] X=a, Y=b [1, [2, 3], 4] [X, Y, Z] X=1, Y=[2, 3], Z=4 [X,Y] [a,b,c] no [H|T] [a,b] H=a, T=[b] [H|T] [1,[2,3],4] H=1, T=[[2,3],4] Matching (Unification)
Comp 307 Lecture 4:15 Matching (Unification) If S and T are constants, then S and T match only if they are the same object If S is a variable and T is anything, then they match, and S is instantiated to T (and vice-versa) If S and T are structures, then they match only if – S and T have the same principal functor, and – all their corresponding components match (the recursive part) If there are more than one bindings, we always compute the most general unifier: e.g. f(X,Y)=f(a,Z) X=a,Y=Z or X=a,Z=Y (Not X=a, Y=b, Z=b)
Comp 307 Lecture 4:16 terms: To match = or to compare == To match X=Y To compare X==Y, X\==Y e.g: [a,b] == [a,b] yes [a,X] == [a,b] no [a,X] = [a,b] X=b Note: X\==Y is always true if there are free variables (X, Y must not be free variable)
Comp 307 Lecture 4:17 Arithmetic: =:=, =\=,, >=, =<, is To compare the values: =:=, =\=, >=, =< – 3 =:= 1+2 yes To evaluate and assign: is – |?- 3 is 2+1. Yes – At the time that evaluation is carried out, all arguments must be already instantiated to numbers. |?-X is 2+Y. error Arithmetic: =:=, =/=, is
Comp 307 Lecture 4:18 Common Errors – 3=1+2. – 3= =1+2. – X=1+2. – X = X+1 – X is X+1
Comp 307 Lecture 4:19 Exercises |?- 1+A = B+2. A=2, B=1. |?- 1+2 = 2+1. no |?- 1+2 =:= 2+1. Yes |?- 1+2 = = 2+1. no |?-X=2+3. X=2+3 |?-X is 2+3. X=5 |?-X=2, X is X+3. no
Lecture 5:20 Comp 307 Caching facts in Prolog All variables are local variables. Longer memories: caching facts retractall/1, retract/1, assert/1 :-dynamic rectangle/2. rectangle_example3:- retractall(rectangle(_,_)), write('Enter the width: '), read(W), write('Enter the height: '), read(H), assert(rectangle(W, H)), calculate_area(Area), format("The area is ~w~n", [Area]). calculate_area(X):- rectangle(W, H), X is 2*W*H.
Lecture 5:21 Comp 307 Build in predicates: findall findall(X, P, XList) produces a list of object X that satisfy P. age(ann, 5). age(tom,5). age(peter,7). age(pat,8). ?-findall(Child, age(Child,5), List). List=[ann, tom]. ?-findall(Child, age(Child, Age), List). List=[ann, tom, peter, pat].