Download presentation

Presentation is loading. Please wait.

Published byNelson Scott Harvey Modified about 1 year ago

1
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

2
Comp 307 Lecture 4:2 min_number(+List, -Min) |?- min_number([3, 2, 6], X). X=2; min_number(, ):- new_min_number(,, ). new_min_number(,, ) :- new_min_number(,, ). new_min_number(,, ) :- new_min_number(,, ).

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

4
Comp 307 Lecture 4:4 Prolog and a small rule based system How Prolog works? Matching Backtracking Rule based system

5
Comp 307 Lecture 4:5 The first program % father(Father, Child) father(bob, liz). father(bob, jim). father(jim, pam). father(jim, pat). mother(ann, jim). mother(pat, tom). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). grandparent(X,Y) :- parent(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

6
Comp 307 Lecture 4:6 |?-parent(X, Y). X=ann, Y=jim; X=pat, Y=tom; X=bob, Y=liz; X=bob, Y=jim; X=jim, Y=pam; X=jim, Y=pat; no. | ?- father(bob, X), mother(ann, X). X=jim?; no Queries and Answers

7
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

8
Comp 307 Lecture 4:8 How Prolog answers a query |?- grandparent(bob, Y).

9
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

10
Comp 307 Lecture 4:10 Backtracking When – When a sub goal failed – Multiple answers, using “;” – ……., fail. How: – Undo variable bindings, try alternative clauses

11
Comp 307 Lecture 4:11 How Prolog answers a query |?-ancestor(bob, pat).

12
Comp 307 Simple Rule-based System in Prolog mammal:-f(hair). mammal:-f(milk). bird:-f(feathers). bird:-f(eggs), f(flies). carnivore:-mammal, f(meat). classify(tiger):-carnivore, f(tawny), f(striped). classify(puma):-carnivore, f(black). f(hair). f(black). f(meat). |?-classify(X). X=puma.

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

14
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)

15
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)

16
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)

17
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

18
Comp 307 Lecture 4:18 Common Errors – 3=1+2. – 3= =1+2. – X=1+2. – X = X+1 – X is X+1

19
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

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

21
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].

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google