+ All Categories
Home > Documents > Introduction to Prolog - Sharif University of...

Introduction to Prolog - Sharif University of...

Date post: 12-Jun-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
57
Introduction to Prolog Artificial Intelligence Course Computer Engineering Department Sharif University of Technology
Transcript
Page 1: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Introduction to Prolog

Artificial Intelligence Course

Computer Engineering Department

Sharif University of Technology

Page 2: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline2

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 3: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline3

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 4: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Preface

Prolog

Programming in logic!

Is a declarative language!

Well suited for problems including

object and

Relations

Has small set of basic mechanisms:

pattern matching

tree-based data structuring

automatic backtracking

4

Page 5: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline5

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 6: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts6

pam

lizbob

tom

patann

jim

Page 7: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts

The fact that Tom is a

parent of Bob can be

written in Prolog as:

7

pam

lizbob

tom

patann

jim

parent ( tom , bob ).

Page 8: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts

The fact that Tom is a

parent of Bob can be

written in Prolog as:

8

pam

lizbob

tom

patann

jim

parent ( tom , bob ).

Fact

Page 9: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts

The fact that Tom is a

parent of Bob can be

written in Prolog as:

9

pam

lizbob

tom

patann

jim

parent ( tom , bob ).

Name of

the relation

Page 10: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts

The fact that Tom is a

parent of Bob can be

written in Prolog as:

10

pam

lizbob

tom

patann

jim

parent ( tom , bob ).

Argument

Page 11: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Facts

The fact that Tom is a

parent of Bob can be

written in Prolog as:

11

pam

lizbob

tom

patann

jim

Let’s write a program that

specifies the family

relationship

by facts!

Page 12: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Example

parent( pam, bob).

parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

12

Page 13: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Example

parent( pam, bob).

parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

consists of six clauses.

Each declares one fact

about the parent

relation.

13

Page 14: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Example

parent( pam, bob).

parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

a particular instance of

the parent relation.

14

Page 15: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Querying

?- 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

15

Page 16: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Querying

?- parent( X, Y).

X=pam

Y = bob;

X= tom

Y = bob;

X= tom

Y = liz;

16

Page 17: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Querying

?- parent( X, Y).

X=pam

Y = bob;

X= tom

Y = bob;

X= tom

Y = liz;

17

“return” vs. “semicolon”

Page 18: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

More Complicated Questions:

Grandparent relationship:

Same Parents:

18

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

X = bob

Y = pat

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

X = bob

Page 19: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Arguments

The arguments of relations can (among other things)

be:

concrete objects (constants)

general objects such as X and Y

19

atoms

variables

Page 20: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Questions

Questions to the system consist of one or more

goals.

A sequence of goals, means the conjunction of the

goals.

Why 'goals‘? Prolog accepts questions as goals that

are to be satisfied.

20

Page 21: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Answers

An answer to a question:

positive:

the goal was satisfiable and the goal succeeded.

negative:

the goal was unsatisfiable and it failed.

If several answers satisfy the question then Prolog

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

21

Page 22: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Let’s extend our program!

22

female( pam).

male( tom).

gender( pam, feminine).

gender( tom, masculine).

Page 23: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline23

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 24: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules

Example: offspring relationFor all X and Y,

if X is a parent of Y then

Y is an offspring of X.

24

Page 25: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules

Example: offspring relationFor all X and Y,

if X is a parent of Y then

Y is an offspring of X.

25

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

Page 26: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules26

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

Rule

Page 27: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules27

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

Conclusion

part

(head)

Page 28: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules28

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

Condition

part

(body)

Page 29: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules29

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

?- offspring( liz, tom).

yes

Page 30: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules30

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

A fact is always, unconditionally, true.

rules are true if some condition is satisfied.

Facts vs. Rules:

Page 31: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Defining Relations by Rules31

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

conjunction of

the conditions,

(both conditions

have to be true.)

Page 32: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Clauses

Prolog clauses are of three types:

facts,

rules and

questions.

Facts: unconditionally true.

Rules: true depending on a given condition.

Questions: ask the program what things are true.

32

Page 33: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Clauses

Prolog clauses consist of:

head

body : a list of goals, separated by commas.

Facts: a head and the empty body.

Questions: only have the body.

Rules: the head and the (non-empty) body.

33

Page 34: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Variables

A variable can be substituted by another object.

We say that a variable becomes instantiated.

Variables are assumed to be universally quantified

and are read as 'for all'.

34

Page 35: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Variables

Alternative readings for variables that appear only

in the body:

35

hasachild( X) :- parent( X, Y)

Page 36: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Variables

Alternative readings for variables that appear only

in the body:

36

hasachild( X) :- parent( X, Y)

For all X and Y,

if X is a parent of Y

then

X has a child.

Page 37: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Variables

Alternative readings for variables that appear only

in the body:

37

hasachild( X) :- parent( X, Y)

For all X,

X has a child if

there is some Y such

that X is a parent of

Y.

Page 38: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline38

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 39: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Recursive Rules

Example: the predecessor relation:

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.

39

Page 40: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Recursive Rules

Example: the predecessor relation:

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.

40

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

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

Page 41: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Procedure and Comments

the whole set of clauses about the same relation.

Such a set of clauses is called a procedure.

/* This is a comment! */

% This is also a comment!

41

Page 42: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline42

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 43: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

How Prolog answers questions

A question: a sequence of one or more goals.

Prolog tries to satisfy all the goals.

43

Page 44: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

How Prolog answers questions

A question: a sequence of one or more goals.

Prolog tries to satisfy all the goals.

44

to satisfy a goal means to demonstrate that the goal logically follows

from the facts and rules in the program

Page 45: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

How Prolog answers questions

A question: a sequence of one or more goals.

Prolog tries to satisfy all the goals.

If the question contains variables, Prolog also has to

find what are the particular objects (in place of

variables) for which the goals are satisfied.

45

to satisfy a goal means to demonstrate that the goal logically follows

from the facts and rules in the program

Page 46: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Interpretation of a Prolog program

Prolog accepts facts and rules as a set of axioms.

user's question as a conjectured theorem;

then it tries to prove this theorem - that is, to

demonstrate that it can be logically derived from the

axioms.

46

Page 47: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Example: predecessor( tom, pat).47

Predecessor(X, Z):- %pr1

parent( X, Z).

predecessor( X, Z):- %pr2

parent( X, Y),

predecessor( Y, Z).

Page 48: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Example: predecessor( tom, pat).48

predecessor( tom, pat)

predecessor( bob, pat)

parent( bob, pat)

predecessor( bob, pat)parent( tom, pat)

parent( bob, pat)

Yes

By rule pr1

By rule pr1 By rule pr1

Y= bob By fact parent(tom, bob)No

Predecessor(X, Z):- %pr1

parent( X, Z).

predecessor( X, Z):- %pr2

parent( X, Y),

predecessor( Y, Z).

Page 49: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Prolog starts with the goals and, using rules,

substitutes the current goals with new goals, until

new goals happen to be simple facts.

Prolog will try to satisfy this goal. In order to do so

it will try to find a clause in the program from which

the above goal could immediately follow.

Prolog first tries that clause which appears first in

the program.

49

Page 50: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

During the search Prolog may enter an unsuccessful

branch. When Prolog discovers that a branch fails it

automatically backtracks to the previous node and

tries to apply an alternative clause at that node.

50

Page 51: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline51

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 52: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Declarative and procedural meaning of

programs

two levels of meaning of Prolog programs:

declarative meaning: what will be the output of the program.

Procedural meaning: how this output is obtained.

The declarative view is advantageous from the

programming point of view.

The procedural details often have to be considered

as well.

52

Page 53: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Outline53

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Page 54: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Summary

Prolog programming consists of:

defining relations

querying about relations

A program consists of clauses.

A relation can be specified by:

facts, simply stating the n-tuples of objects that satisfy

the relation.

by stating rules about the relation.

A procedure is a set of clauses about the same

relation.

54

Page 55: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Summary

Querying by means of questions, resembles querying

a database.

Prolog's answer: a set of objects that satisfy the

question.

Process to establish whether an object satisfies a

query:

logical inference,

exploring among alternatives,

possibly backtracking.

55

Page 56: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Summary

The following concepts have been introduced in this

session: clause, fact, rule, question

the head of a clause, the body of a clause

recursive rule, recursive definition

procedure

atom, variable

instantiation of a variable

goal

goal is satisfiable, goal succeeds

goal is unsatisfiable, goal fails

backtracking

declarative meaning, procedural meaning

56

Page 57: Introduction to Prolog - Sharif University of Technologyce.sharif.edu/.../root/prolog/Prolog-lecture1.pdf · Prolog starts with the goals and, using rules, substitutes the current

Any question?!

57


Recommended