# 1. An Overview of Prolog.

## Presentation on theme: "1. An Overview of Prolog."— Presentation transcript:

1. An Overview of Prolog

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

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,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program ?- parent(bob,pat). yes ?- parent(liz,pat). no
?-parent(tom,ben). ?- parent(X,liz). X=tom. ?- parent(bob,X). X=ann; X=pat; parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

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=pam Y=bob; X=tom Y=liz; ... parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program 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=bob Y=pat X Y jim parent grandparent parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). ?- parent(X,Y),parent(Y,Jim). will produce the same result.

An Example Program Who are Tom’s grandchildren?
?- parent(tom,X),parent(X,Y). X=bob Y=ann; Y=pat parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program 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 parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

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.

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

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. offspring(Y,X):-parent(X,Y). For all X and Y, Y is an offspring of X if X is a parent of Y. A rule in Prolog

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.

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

Extending the Example Program by Rules
?- offspring(liz,tom). Applying the rule, it becomes offspring(liz,tom):-parent(tom,liz). parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). Prolog tries to find out whether the condition part is true.

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 if X is a parent of Y and X is a female. mother(X,Y):-parent(X,Y), female(X).

Extending the Example Program by Rules
The grandparent relation can be immediately written in Prolog as: grandparent(X,Z):-parent(X,Y),parent(Y,Z). X Y Z parent grandparent

Extending the Example Program by Rules
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 Z parent parent X Y sister female Pat is a sister of herself !

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

A Recursive Rule Definition
X X Z parent predecessor X parent parent Y1 Y1 parent predecessor(X,Z):- parent(X,Z). predecessor predecessor parent Y2 Y2 parent X parent parent Y3 Z predecessor parent Y parent predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Z). Z Z predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Y3), parent(Y3,Z). predecessor(X,Z):- parent(X,Y), parent(Y,Z).

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.

A Recursive Rule Definition
For all X and Z, X is a predecessor of Z if there 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).

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). parent(X,Y), predecessor(Y,Z). ?- predecessor(pam,X). X=bob; X=ann; X=pat; X=jim

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.

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.

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.