5
Chapter1 - continued 1

Chapter1 - continued

Embed Size (px)

DESCRIPTION

Chapter1 - continued. 1.3 Recursive rules. How to express predecessor relation in terms of parent relation? It is expressed with two rules: Direct (immediate) predecessors. predecessor (X, Z):- parent (X, Z), - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter1 - continued

1

Chapter1 - continued

Page 2: Chapter1 - continued

2

1.3 Recursive rules How to express predecessor relation in

terms of parent relation? It is expressed with two rules:

a. Direct (immediate) predecessors.predecessor (X, Z):-

parent (X, Z),

b. Indirect predecessors ( X is an indirect predecessor of some Z if there is a parentship chain of people between X and Z) For all X and Z, X is a predecessor of Z ifthere is Y such that(1) X is a parent of Y(2) Y is a parent of Z

predecessor (X, Z):-

parent (X, Y),

predecessor(Y, Z).

X

Z

Parent (a)Predecessor

X

Z

Parent

Parent

Parent

(b)Predecessor

(Recursion)

Page 3: Chapter1 - continued

3

parent( pam, bob). % Pam is a parent of Bobparent( tom, bob).parent( tom, liz).parent( bob, ann).parent( bob, pat).parent( pat, jim).

female( pam). % Pam is femalemale( tom). % Tom is malemale( bob).female( liz).female( ann).female( pat).male( jim).

offspring( Y, X) :- % Y is an offspring of X if parent( X, Y). % X is a parent of Y

mother( X, Y) :- /* X is the mother of Y if parent( X, Y), X is a parent of Y and female( X). X is female */

grandparent( X, Z) :- % X is a grandparent parent( X, Y), % of Z if X is a parent of Y parent( Y, Z). % and Y is a parent of Z

sister( X, Y) :- % X is a sister of Y if parent( Z, X), parent( Z, Y), % X and Y have the same parent and

female( X), % X is female and different( X, Y). % X and Y are different

predecessor( X, Z) :- /* Rule prl: X is a parent( X, Z). predecessor of Z*/

predecessor( X, Z) :- % Rule pr2: X is a parent( X, Y), %predecessor of Z predecessor( Y, Z).

Page 4: Chapter1 - continued

4

1.4 How Prolog answers questions A question is a sequence of one or more goals.

To answer questions, Prolog tries to satisfy all the goals

=To demonstrate that the goals are true, assuming that the relations in the program are true

=To demonstrate that the goal is logically follows from the facts and rules in the program.

If the question contains variables Prolog also has to find what are the particular

objects for which the goals are satisfied. If Prolog cannot find some instantiation of

variables that the goals logically follow from the program, Prolog answer will be ‘no’

Page 5: Chapter1 - continued

5

Homework

Read section 1.4 and 1.5 in your textbook and ask me please if there is any ambiguity.