1 / 28

1. An Overview of Prolog

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

sharla
Télécharger la présentation

1. An Overview of Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1. An Overview of Prolog

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

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

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

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

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

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

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

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

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

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

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

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

  14. 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,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). Prolog tries to find out whether the condition part is true.

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

  16. X parent grandparent Y parent Z 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).

  17. 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(ann,pat). yes ?- sister(X,pat). X=ann; X=pat • sister(X,Y):- • parent(Z,X), • parent(Z,Y), • female(Z). Z parent parent X Y sister female Pat is a sister of herself !

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

  19. X parent predecessor Z A Recursive Rule Definition X X parent parent Y1 Y1 parent predecessor predecessor predecessor(X,Z):- parent(X,Z). 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).

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

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

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

  23. 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 recursivedefinitions.

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

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

  26. 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) by rule pr1 by rule pr1 parent(tom,pat) parent(tom,Y) predecessor(Y,pat) no Y=bob by fact parent(tom,bob) parent(tom,Y) predecessor(Y,pat) by rule pr1 parent(tom,Y) predecessor(Y,pat)

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

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

More Related