2. syntax and meaning. contents data objects matching declarative meaning of prolog procedural...

29
4 2 5 1 0011 0010 1010 1101 0001 0100 1011 2. Syntax and Meaning

Upload: ulises-grenfell

Post on 15-Dec-2015

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

42510011 0010 1010 1101 0001 0100 1011

2. Syntax and Meaning

Page 2: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Contents

• Data Objects• Matching• Declarative meaning of Prolog• Procedural meaning• Example: monkey and banana• Order of clauses and goals• The relation between Prolog and logic

Page 3: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

• Data objects in Prolog:

Data objects

Simple objects Structures

Constants Variables

Atoms Numbers

Page 4: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

• Atoms can be constructed in three ways:– Strings of letters, digits and ‘_’, starting with

a lower-case letter. • anna nil x25 x_25 x_ x_ _y

– Strings of special characters• <---< ======> ... .:.

– String of characters enclosed in single quote• ‘Tom’ ‘South_America’ ‘Sarah Jones’ ‘大同’

• Numbers used in Prolog include integer numbers and real numbers.

Data objects

Simple objectsStructures

Constants Variables

Atoms Numbers

Page 5: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

• Variables are strings of letters, digits and underscore characters, starting with an upper-case character:– X Result Object2 _x23 _23

• Anonymous variablehaschild(X):-parent(X,Y).

haschild(X):-parent(X, _ )

somebody_has_child:-parent(_,_).

somebody_has_child:-parent(X,Y).

Data objects

Simple objects Structures

Constants Variables

Atoms Numbers

Page 6: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects• Structured objects (or simply structure)

are objects that have several components.• The components themselves can, in turn,

be structures.– The date can be viewed as a structure with

three components: day, month, year.– date(1,may,1983) Data objects

Simple objects Structures

Constants Variables

Atoms Numbers

functor arguments

date

1 may 1983

• Any day in May 1983 can be represented as – date(Day, may, 1983)

Page 7: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

1234

5

1 2 3 4 5 6 7 8 9P1=(1,1)

P2=(2,3)

S

(6,4)

(4,2)(7,1)

P1=point(1,1)

P2=point(2,3)

S=seg(P1,P2)

=seg(point(1,1),point(2,3))

T=triangle(point(4,2),

point(6,4),

point(7,1))

Page 8: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

• We can use the same name, point, for points in both 2D and 3D:– point(X,Y) point(X,Y,Z)

• Prolog will recognize the difference because each functor is defined by two things:– the name, whose syntax is that of atoms;– the arity - i.e., the number of arguments.

Page 9: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objects

• All structured objects in Prolog are trees, represented in the program by terms.

(a+b)*(c-5) *(+(a,b),-(c,5))

*+ –

c 5a b

Page 10: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Data Objectsr1 r2 seq

r1 r2seq(r1,r2)

r1

r2par

r1 r2par(r1,r2)

r1

r3

r2

par

r1

r2 r3

parpar(r1,par(r2,r3))

r1

r3

r2 r4

par

r1

r2

r3

seq

par

r3

par(r1,seq(par(r2,r3),r4))

Page 11: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

• The most important operation on terms is matching.

• Given two terms, we say that they match if:– they are identical or– the variables in both terms can be instantiated to objects

in such a way that after the substitution of variables by those objects the terms become identical.

– For example, the following instantiation makes the terms date(D,M,1983) and date(D1,may,Y1) identical:

• D is instantiated to D1• M is instantiated to may• Y1 is instantiated to 1983.

Page 12: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

• Matching is a process that takes as input two terms and checks whether they match.– If the terms do not match we say that this

process fails.– If they do match then the process succeeds

and it also instantiates the variables in both terms to such value that the terms become identical.

Page 13: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

• The request for the matching operation can be communicated to the Prolog system by using the operator ‘=’.

• ?- date(D,M,1983)=date(D1,may,Y1).D=D1M=mayY1=1983

D=1D1=1M=mayY1=1983

D=thirdD1=thirdM=mayY1=1983

Less general

Matching in Prolog alwaysresults in the most general instantiation.

Page 14: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

?- date(D,M,1983)=date(D1,may,Y1), date(D,M,1983)=date(15,M,Y).

To satisfy the first goal:D=D1M=mayY1=1983

After having satisfied the second goal:D=15D1=15M=mayY1=1983Y=1983

Page 15: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

• The general rules to decide whether two terms, S and T, match:– If S and T are constants the S and T match

only if they are the same object.– IF S is a variable and T is anything, then they

match, and S is instantiated to T. Conversely, if T is a variable then T is instantiated to S.

– If S and T are structures then they match only if

• S and T have the same principal functor, and• all their corresponding components match

Page 16: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

triangle

pointA

2 2

point

1 1triangle

X pointpoint

4 y 2 Z

Page 17: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Matching

vertical(seg(point(X,Y),point(X,Y1)).horizontal(seg(point(X,Y),point(X1,Y)).

?-vertical(seg(point(1,1),point(1,2)).yes?-vertical(seg(point(1,1),point(2,Y)).no?-horizontal(seg(point(1,1),point(2,Y)).Y=1?-vertical(seg(point(2,3),P)).P=point(2,Y)?-vertical(S),horizontal(S).S=seg(point(X,Y),point(X,Y))

Page 18: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Declarative Meaning of Prolog Programs

• Given a program and a goal G, the declarative meaning says:– A goal G is true (i.e., satisfiable, or logically

follows from the program) if and only if• there is a clause C in the program such

that• there is a clause instance I of C such that

– the head of I is identical to G, and– all the goals in the body of I are true.

Page 19: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Declarative Meaning of Prolog Programs

• In general, a question to the Prolog system is a list of goals separated by commas.

• A list of goals is true if all the goals in the list are true for the same instantiation of variables.

• A comma between goals thus denotes the conjunction of goals: they all have to be true.

• The disjunction of goals: any one of the goals in a disjunction has to be true.

Page 20: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Procedural Meaning

Page 21: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Procedural Meaning

Page 22: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Procedural Meaning

• The procedural meaning specifies how Prolog answers question.

Page 23: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

• The problem:– There is a monkey at the door into a room. In

the middle of the room a banana is hanging from the ceiling. The monkey is hungry and and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use. The monkey can perform the following actions: walk on the floor, climb the box, push the box around and grasp the banana if standing on the box directly under the banana. Can the monkey get the banana?

Page 24: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

• Finding a representation of the problem:– We can think of the ‘monkey world’ as always

being in some state that can change in time.– The current state is determined by the

positions of the objects.– For example, the initial state is determined

by:• Monkey is at door.• Monkey is on the floor.• Box is at window.• Monkey does not have banana.

Page 25: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

• It is convenient to combine all these four pieces of information into one structured object.

• Let us choose the word ‘state’ as the functor to hold the four components together.– The initial state becomes

state(atdoor,onflorr,atwindow,hasnot)

Horizontal positionof monkey

Vertical positionof monkey

Position of box

Monkey has or has not banana

Page 26: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

• Formalize the rules of the game:– The goal is a situation in which the

monkey has the banana.state(_, _, _, has)

– What are the allowed moves that change the world from one state to another?grasp banana, climb box, push box, walk aroundSuch rules can be formalized in Prolog as a 3-

place relation named move: move(State1, Move, State2)

Page 27: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

move(state(middle,onbox,middle,hasnot), grasp, state(middle,onbox,middle,has)).

move(state(P,onfloor,P,H), climb, state(P,onbox,P,H)).

move(state(P1,onfloor,P1,H), push(P1,P2), state(P2,onfloor,P2,H)).

move(state(P1,onfloor,B,H), walk(P1,P2), state(P2,onfloor,B,H)).

canget(state(_, _, _, has)).canget(State1):- move(State1,move State2), canget(State2).

State1

cangetcanget

moveState2

has

Statem

Page 28: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Example: Monkey and Banana

state(atdoor,onfloor,arwindow,hasnot)

state(P2,onfloor,arwindow,hasnot)

walk(atdoor,P2)

state(atwindow,onbox,arwindow,hasnot) state(P2’,onfloor,P2’,hasnot)

No movepossible

climbbacktrack

push(P2,P2’)P2=atwindow

state(P2’,onbox,P2’,hasnot)

climb

state(middle,onbox,middle,has)

graspP2’=middle

Page 29: 2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and

4251

0011 0010 1010 1101 0001 0100 1011

Order of Clauses and Goals

• Danger of indefinite looping• Program variation through reordering

of clauses and goals