1 / 23

Cs461: Prolog programming

Cs461: Prolog programming. Lab 2: Introduction to Prolog. What is Prolog programming?. Prolog is a programming language for symbolic , non-numeric commutation. It solves problems that involve objects and relations between objects. Defining relations by facts.

peterd
Download Presentation

Cs461: Prolog programming

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. Cs461: Prolog programming Computer Science Department Lab 2: Introduction to Prolog

  2. What is Prolog programming? • Prolog is a programming language for symbolic , non-numeric commutation. • It solves problems that involve objects and relations between objects. Computer Science Department

  3. Defining relations by facts • The fact that Tom is a parent of Bob can be written in prolog as: Parent(tom, bob). • tom and bob are objects, • Parent(tom, bob) is a relation between these objects. Computer Science Department

  4. Defining relations by facts • The whole family tree can be described by the following facts: Computer Science Department

  5. Defining relations by facts • The whole family tree can be described by the following facts: Parent(pam,bob). Parent(tom,bob). Parent(tom,liz). Parent(bob,ann). Parent(bob,pat). Parent(pat,jim). Computer Science Department

  6. Defining relations by facts • We can also add information about the sex of the people by the following facts: female(pam). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim). Computer Science Department

  7. Defining relations by facts • female(pam) is a Unary relation • parent(bob,pat) is a Binary relation. Think about this relation: Sex(pam, feminist). Computer Science Department

  8. Questions in prolog. • Prolog can posed questions about any relation. • So, we can ask a question about Parent relation, • For example : Is bob a parent of Pat? ?- Parent(bob,pat). The answer will be YES. Computer Science Department

  9. Questions in prolog. • Another question can be asked: Who is Liz’s parent? ?- Parent(X, liz). The answer here will not be Yes/No, rather, prolog will find thevalue of X. So the answer is : tom Computer Science Department

  10. Class exercise(1) • Who are bob’s children? • Who is parent of whom? Computer Science Department

  11. Questions in prolog. • In prolog, we can ask more complicated questions such as: Who is a grandparent of jim? ?- Parent(Y,jim) , Parent( X,Y). The answer will be: X=bob Y=pat Computer Science Department

  12. Class exercise(2) • Who are Tom’s grandchildren? Computer Science Department

  13. Class exercise(3) • Do ann and pat have a common parents? ?- Parent(X,ann) , Parent(X, pat). The answer will be : X = bob. Computer Science Department

  14. Some important points • A prolog program consists of clauses, each clause ends with a full stop. • The arguments of relation can be concrete objects, constant objects (such as bob and pat) or general object (such as X and Y). • Questions in prolog consist of one or more goals. parent(X,ann),parent(X,pat) means the conjunctions of the goals: X is parent of ann , and X is parent of Pat. Computer Science Department

  15. Defining relations by rules. • Consider the relation offspring: offspring(liz, tom) • This relation can be defined by making use of the fact that : the parent is the inverse of offspring. • The alternative way can be based of the following logical statements: For all X and Y, Y is an offspring of X if X is a parent of Y. Computer Science Department

  16. Defining relations by rules. • In prolog we can define the offspring relationin the following way: Offspring(Y,X) :- Parent(X, Y) • This clause is called a rule • The differences between rule and fact: • Fact: is a something that always, unconditionally true. • Rule: specify things that are true if some condition is satisfied. Computer Science Department

  17. Defining relations by rules. • Each rule has two parts: • Condition. (The right hand side) • Conclusion. (The left hand side) • offspring(X,Y):-parent(Y,X). offspring(X,Y) -> is a conclusion part. parent(X,Y) -> is a condition part. Computer Science Department

  18. Defining relations by rules. • How rules are actually used by prolog program? Consider the following question: ?- offspring(liz, tom) There are no facts in the program about the offspring, therefore, the only way to find the answer is to apply the rule about offspring relation. Computer Science Department

  19. Defining relations by rules. • Now we want to apply this rule: Offspring(Y,X) :- Parent(X, Y) to find the answer of this question: ?- offspring(liz, tom). • X= tom, Y= liz. • The initial goal is: offspring(liz,tom). • The condition part become: parent(tom,liz). • The initial goal will be replaced with the subgoalparent(tom,liz). • Program can find the answer of this subgoal by using the facts about parent relation. • After finding that the condition part is true, the conclusion part will be also true. Computer Science Department

  20. Class exercise(4) • Write a rule to describe the grandparent relation? • Write a rule to describe the sister relation? Computer Science Department

  21. Recursive rule • Let’s describe the predecessor relation , this relation will be defined in term of parent relation. • For all X and Z X is a predecessor of Z if X is a parent of Z predecessor(X,Z):- parent(X,Z). Computer Science Department

  22. Recursive rule • What about indirect predecessor? The rule will be as the following: predecessor(X,Z):- parent(X,Y), parent(Y,Z) predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2). parent(y2,Z). ……. ……. Computer Science Department

  23. Recursive rule For all X and Z there is a Y such that • X is the parent of Y , and • Y is a predecessor of Z. predecessor(X, Z) parent(X,Y), predecessor(Y,Z). Computer Science Department

More Related