1. An Overview of Prolog. Contents An example program: defining family relations Extending the...

28
4 2 5 1 0011 0010 1010 1101 0001 0100 1011 1. An Overview of Prolog

Transcript of 1. An Overview of Prolog. Contents An example program: defining family relations Extending the...

42510011 0010 1010 1101 0001 0100 1011

1. An Overview of Prolog

4251

0011 0010 1010 1101 0001 0100 1011

Contents

• An example program: defining family relations

• Extending the example program by rules

• A recursive rule definition• How Prolog answers questions• Declarative and procedure meaning

of programs

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program• Prolog is a programming language for symbolic,

non-numeric computation.• It is specially well suited for solving problems

that involve objects and relations between objects.

pam tom

bob liz

ann pat

jim

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

?- parent(bob,pat).yes?- parent(liz,pat).no?-parent(tom,ben).no?- parent(X,liz).X=tom.?- parent(bob,X).X=ann;X=pat;no

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

Who is a parent of whom?Find X and Y such that X is a parent of Y.

?- parent(X,Y).

X=pamY=bob;

X=tomY=bob;

X=tomY=liz;...

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

Who is a grandparent of Jim?(1) Who is a parent of Jim? Assume that this is some Y.(2) Who is a parent of Y? Assume that this is some X.?- parent(Y,Jim), parent(X,Y).X=bobY=pat

X

Y

jim

parent

parent

grandparent

?- parent(X,Y),parent(Y,Jim).will produce the same result.

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

Who are Tom’s grandchildren?

?- parent(tom,X),parent(X,Y).

X=bobY=ann;

X=bobY=pat

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

Do Ann and Pat have a common parent?(1) Who is a parent, X, of Ann?(2) Is (this same) X a parent of Pat?

?- parent(X,ann), parent(X,pat).

X=bob

4251

0011 0010 1010 1101 0001 0100 1011

An Example Program

• It is easy in Prolog to define a relation by stating the n-tuples of objects that satisfy the relation.

• The user can query the prolog system about relations defined in the program.

• The arguments of relations can be:– concrete objects, or constants, or general objects.

• Questions to the system consist of one or more goals.• An answer to the question can either be positive or

negative.• If several answers satisfy the question than Prolog

will find as many of them as desired by the user.

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• Adding more facts

female(pam).male(tom).male(bob).female(liz).female(pat).female(ann).male(jim).

sex(pam, feminine).sex(tom, masculine).sex(bob, masculine).sex(liz , feminine).sex(pat , feminine).sex(ann , feminine).sex(jim, masculine).

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• We could define offspring in a similar way as the parent relation.

• However, the offspring relation can be defined much more elegantly by making use of the fact that it is the inverse of parent, and that parent has already been defined.

For all X and Y, Y is an offspring of X ifX is a parent of Y.

offspring(Y,X):-parent(X,Y).

A rule in Prolog

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• Difference between facts and rules:– A fact is something that is always,

unconditionally, true.– Rules specify things that are true if

some condition is satisfied.

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• Rules have:– a condition part (the LHS of the rule) and– a conclusion part (the RHS of the rule).

offspring(Y,X):-parent(X,Y).

head body

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

parent(tom,bob).parent(pam,bob).parent(tom,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).

?- offspring(liz,tom).

Applying the rule, it becomes

offspring(liz,tom):-parent(tom,liz).

Prolog tries to find out whetherthe condition part is true.

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• The mother relation can be based on the following logical statement:

For all X and Y,X is the mother of Y ifX is a parent of Y and X is a female.

mother(X,Y):-parent(X,Y), female(X).

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• The grandparent relation can be immediately written in Prolog as:

X

Y

Z

parent

parent

grandparent

grandparent(X,Z):-parent(X,Y),parent(Y,Z).

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

X Y

Z

parent parent

sisterfemale

For any X and Y,X is a sister of Y if(1) both X and Y have the same parent, and(2) X is a female.

sister(X,Y):-parent(Z,X),parent(Z,Y),female(Z).

?- sister(ann,pat).yes?- sister(X,pat).X=ann;X=pat

Pat is a sister of herself !

4251

0011 0010 1010 1101 0001 0100 1011

Extending the Example Program by Rules

• An improved rule for the sister relation can then be:

sister(X,Y):-parent(Z,X),parent(Z,Y),female(Z),different(X,Y).

different(X,Y).

4251

0011 0010 1010 1101 0001 0100 1011

A Recursive Rule Definition

X

Z

parentpredecessor

X

Z

parentpredecessorY

parent

X

Z

predecessorY1

Y2

parent

parent

parent

X

Z

predecessor

Y1

Y2

parent

parent

parentY3

parent

predecessor(X,Z):- parent(X,Z).

predecessor(X,Z):- parent(X,Y), parent(Y,Z).

predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Z).

predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Y3), parent(Y3,Z).

4251

0011 0010 1010 1101 0001 0100 1011

A Recursive Rule Definition

• There is, however, an elegant and correct formulation of the predecessor relation: it will be correct in the sense that it will work for predecessor at any depth.

• The key idea is to define the predecessor relation in terms of itself.

4251

0011 0010 1010 1101 0001 0100 1011

A Recursive Rule Definition

For all X and Z,X is a predecessor of Z ifthere is a Y such that(1) X is a parent of Y and(2) Y is a predecessor of Z

predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).

4251

0011 0010 1010 1101 0001 0100 1011

A Recursive Rule Definition

• We have thus constructed a complete program for the predecessor relation, which consists of two rules: – one for direct predecessor and – one for indirect predecessor.

predecessor(X,Z):-parent(X,Z).

predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).

?- predecessor(pam,X).X=bob;X=ann;X=pat;X=jim

4251

0011 0010 1010 1101 0001 0100 1011

A Recursive Rule Definition

• The use of predecessor itself may look surprising:– When defining something, can we use this

same thing that has not yet been completely defined?

• Such definitions are, in general, called recursive definitions.

4251

0011 0010 1010 1101 0001 0100 1011

How Prolog Answers Questions

• A question to Prolog is always a sequence of one or more goals.

• To answer a question, Prolog tries to satisfy all the goals.

• To satisfy a goal means to demonstrate that the goal 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.

4251

0011 0010 1010 1101 0001 0100 1011

How Prolog Answers Questions

• An appropriate view of the interpretation of a Prolog program in mathematical terms is then as follows: – Prolog accepts facts and rules as a set of

axioms, and the user’s question as a conjectured theorem; then it tries to prove this theorem.

4251

0011 0010 1010 1101 0001 0100 1011

How Prolog Answers Questions• Prolog starts with the goals and, using rules,

substitutes the current goals with new goals, until new goals happen to be simple facts.

predecessor(tom,pat)

parent(tom,pat)

no

by rule pr1parent(tom,Y)predecessor(Y,pat)

by rule pr1

parent(tom,Y)predecessor(Y,pat)

parent(tom,Y)predecessor(Y,pat)

Y=bob by fact parent(tom,bob)

by rule pr1

4251

0011 0010 1010 1101 0001 0100 1011

Declarative and Procedural Meaning

• The declarative meaning is concerned only with the relation defined by the program.

• The declarative meaning thus determines what will be the output of the program.

• The procedural meaning determines how this output is obtained; i.e., how are the relations actually evaluated by the Prolog system.

4251

0011 0010 1010 1101 0001 0100 1011

Declarative and Procedural Meaning

• The ability of Prolog to work out many procedural details on its own is considered to be one of its specific advantages.

• It encourages the programmer to consider the declarative meaning of program relatively independently of their procedural meaning.

• This is of practical importance because the declarative aspects of programs are usually easier to understand than the procedural details.