introduction to prolog - sharif university of...

Post on 12-Jun-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to Prolog

Artificial Intelligence Course

Computer Engineering Department

Sharif University of Technology

Outline2

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Outline3

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

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

Outline5

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

Defining Relations by Facts6

pam

lizbob

tom

patann

jim

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

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

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

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

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!

Example

parent( pam, bob).

parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

12

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

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

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

Querying

?- parent( X, Y).

X=pam

Y = bob;

X= tom

Y = bob;

X= tom

Y = liz;

16

Querying

?- parent( X, Y).

X=pam

Y = bob;

X= tom

Y = bob;

X= tom

Y = liz;

17

“return” vs. “semicolon”

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

Arguments

The arguments of relations can (among other things)

be:

concrete objects (constants)

general objects such as X and Y

19

atoms

variables

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

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

Let’s extend our program!

22

female( pam).

male( tom).

gender( pam, feminine).

gender( tom, masculine).

Outline23

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

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

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

Defining Relations by Rules26

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

Rule

Defining Relations by Rules27

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

Conclusion

part

(head)

Defining Relations by Rules28

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

Condition

part

(body)

Defining Relations by Rules29

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

?- offspring( liz, tom).

yes

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:

Defining Relations by Rules31

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

conjunction of

the conditions,

(both conditions

have to be true.)

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

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

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

Variables

Alternative readings for variables that appear only

in the body:

35

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

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.

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.

Outline38

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

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

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

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

Outline42

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

How Prolog answers questions

A question: a sequence of one or more goals.

Prolog tries to satisfy all the goals.

43

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

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

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

Example: predecessor( tom, pat).47

Predecessor(X, Z):- %pr1

parent( X, Z).

predecessor( X, Z):- %pr2

parent( X, Y),

predecessor( Y, Z).

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

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

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

Outline51

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

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

Outline53

Preface

Defining relations by facts

Defining relations by rules

Recursive rules

How Prolog answers questions

Declarative and procedural meaning of programs

Summary

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

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

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

Any question?!

57

top related