An Overview of Prolog 1 What is Prolog

  • Slides: 17
Download presentation
An Overview of Prolog 1

An Overview of Prolog 1

What is Prolog � Prolog is a powerful language for AI and nonnumerical programming

What is Prolog � Prolog is a powerful language for AI and nonnumerical programming � Stands for programming in logic � Centered around a small set of basic mechanisms, including: � Pattern matching � Tree-based data structuring � Automatic back tracking � Well suited for problems that involves structured objects and relations between them. 2

Chapter One: Introduction to Prolog 1. 2. 3. 4. 5. 3 Defining relations by

Chapter One: Introduction to Prolog 1. 2. 3. 4. 5. 3 Defining relations by facts Defining relations by rules Recursive rules How Prolog answers questions Declarative and procedural meaning of programs

Chapter one: 1. Defining relations by facts 2. Defining relations by rules 4

Chapter one: 1. Defining relations by facts 2. Defining relations by rules 4

1. 1 Defining relations by facts Example The fact that tom is a parent

1. 1 Defining relations by facts Example The fact that tom is a parent of bob parent(tom, bob). the name of a relation argument 1 argument 2 Fact clause pam tom 5 bob liz ann pat jim

1. 1 Defining relations by facts � The family tree is defined by the

1. 1 Defining relations by facts � The family tree is defined by the following Prolog program: parent(pam, parent(tom, parent(bob, parent(pat, bob) liz) ann) pat) jim) pam tom bob liz ann pat jim This program consists of six clauses. � Each clause declares one fact about parent relation (particular instance of the parent relation) * a relation is the set of all its instances 6

1. 1 Defining relations by facts When the program communicated to Prolog system, Prolog

1. 1 Defining relations by facts When the program communicated to Prolog system, Prolog can be posed some questions about the parent relation: Is bob a parent of pat? pam ? - parent(bob, pat) Prolog answer: True ? - parent(liz, pat) Prolog answer: No solutions ? - parent(tom, pat) Prolog answer: No solutions 7 tom bob liz ann pat jim

1. 1 Defining relations by facts Who is liz’s parent? ? - parent(X, liz)

1. 1 Defining relations by facts Who is liz’s parent? ? - parent(X, liz) Prolog answer: X = tom pam Who are bob’s children? ? - parent(bob, X) Prolog answer: X = ann. X = pat. 8 tom bob liz ann pat jim

1. 1 Defining relations by facts Find X and Y such that X is

1. 1 Defining relations by facts Find X and Y such that X is a parent of Y. ? - parent (X, Y). Prolog answers: X = pam. Y = bob. X = tom. Y = liz. X = bob. Y = ann. X = bob. Y = pat. X = pat. Y = jim. 9 pam tom bob liz ann pat jim

1. 1 Defining relations by facts Who is a grandparent of jim? Who is

1. 1 Defining relations by facts Who is a grandparent of jim? Who is the parent of jim? Assume that this is Y Who is the parent of Y? Assume that this is X pam tom ? - parent(Y, jim), parent (X, Y) Prolog answer: X = bob Y = pat bob liz ann pat √ • Changing the order of the requirements will affect the result. x ? - parent(X, jim), parent (Y, X) Prolog answer: X = pat. Y = bob. x ? - parent(X, jim), parent (X, Y) Prolog answer: X = pat. 10 Y = jim

1. 1 Defining relations by facts Do ann and pat have a common parent?

1. 1 Defining relations by facts Do ann and pat have a common parent? Who is a parent X of ann? Is X a parent of pat? pam tom bob liz ? - parent (X, ann), parent(X, pat) Prolog answer: X = bob 11 ann pat jim

1. 2 Defining relations by rules � We can add the information on the

1. 2 Defining relations by rules � We can add the information on the sex of the people that occur in the parent relation: female(pam). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim). pam tom bob liz ann pat jim Gender(pam, female). Gender(tom, male). Gender(bob, male). male and female are unary relations, whereas parent is a binary relation. 12

1. 2 Defining relations by rules Let us introduce the offspring relation (inverse of

1. 2 Defining relations by rules Let us introduce the offspring relation (inverse of parent) Fact offspring(liz, tom). offspring(bob, tom). . caluses pam tom bob liz The logical statement is: For all X and Y, Y is an offspring of X is a parent of Y offspring(Y, X) : - parent(X, Y). Rule clause 13 ann pat jim

1. 2 Defining relations by rules There is an important difference between facts and

1. 2 Defining relations by rules There is an important difference between facts and rules: � Facts is something that is always, unconditionally, true parent(tom, liz). � Rules specify things that are true if some condition is satisfied. Rules have: � Condition part (right-hand side) = body � Conclusion part (left-hand side) = head offspring(Y, X) : - parent(X, Y). head 14 body

1. 2 Defining relations by rules Questions � Is liz an offspring of tom?

1. 2 Defining relations by rules Questions � Is liz an offspring of tom? ? - offspring(liz, tom). Prolog answer: True How Prolog answer this question using the rule: Offspring(Y, X) : - parent(X, Y). Prolog answer: True 15

1. 2 Defining relations by rules � How to express: � Mother relation �

1. 2 Defining relations by rules � How to express: � Mother relation � Grandparent relation � Sister relation in Prolog? 16

Important Points 17

Important Points 17