Title: 2' Syntax and Meaning
12. Syntax and Meaning
2Contents
- Data Objects
- Matching
- Declarative meaning of Prolog
- Procedural meaning
- Example monkey and banana
- Order of clauses and goals
- The relation between Prolog and logic
3Data Objects
4Data 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
- lt---lt gt ... ..
- String of characters enclosed in single quote
- Tom South_America Sarah Jones ??
- Numbers used in Prolog include integer numbers
and real numbers.
5Data Objects
- Variables are strings of letters, digits and
underscore characters, starting with an
upper-case character - X Result Object2 _x23 _23
- Anonymous variable
- haschild(X)-parent(X,Y).
- haschild(X)-parent(X, _ )
- somebody_has_child-parent(_,_).
- somebody_has_child-parent(X,Y).
6Data 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)
date
may
1983
functor
arguments
1
- Any day in May 1983 can be represented as
- date(Day, may, 1983)
7Data Objects
P1point(1,1) P2point(2,3) Sseg(P1,P2)
seg(point(1,1),point(2,3)) Ttriangle(point(4,2),
point(6,4), point(7,1))
8Data 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.
9Data Objects
- All structured objects in Prolog are trees,
represented in the program by terms.
(ab)(c-5) ((a,b),-(c,5))
c
5
a
b
10Data Objects
seq
par
seq(r1,r2)
par(r1,r2)
r1
r2
r1
r2
par
r1
par
par(r1,par(r2,r3))
r2
r3
par
r1
seq
par(r1,seq(par(r2,r3),r4))
par
r3
r2
r3
11Matching
- 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.
12Matching
- 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.
13Matching
- 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).
DD1 Mmay Y11983
D1 D11 Mmay Y11983
Dthird D1third Mmay Y11983
Matching in Prolog always results in the most
general instantiation.
14Matching
?- date(D,M,1983)date(D1,may,Y1),
date(D,M,1983)date(15,M,Y).
To satisfy the first goal DD1 Mmay Y11983
After having satisfied the second
goal D15 D115 Mmay Y11983 Y1983
15Matching
- 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
16Matching
triangle
point
A
2
2
triangle
X
point
point
4
y
2
Z
17Matching
vertical(seg(point(X,Y),point(X,Y1)). horizontal(s
eg(point(X,Y),point(X1,Y)).
?-vertical(seg(point(1,1),point(1,2)). yes ?-verti
cal(seg(point(1,1),point(2,Y)). no ?-horizontal(se
g(point(1,1),point(2,Y)). Y1 ?-vertical(seg(point
(2,3),P)). Ppoint(2,Y) ?-vertical(S),horizontal(S
). Sseg(point(X,Y),point(X,Y))
18Declarative 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.
19Declarative 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.
20Procedural Meaning
21Procedural Meaning
22Procedural Meaning
- The procedural meaning specifies how Prolog
answers question.
23Example 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?
24Example 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.
25Example 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)
Vertical position of monkey
Position of box
Monkey has or has not banana
Horizontal position of monkey
26Example 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 around
- Such rules can be formalized in Prolog as a
3-place relation named move move(State1, Move,
State2)
27Example Monkey and Banana
canget(state(_, _, _, has)). canget(State1)-
move(State1,move State2), canget(State2).
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)).
State1
Statem
State2
move
canget
canget
has
move(state(P1,onfloor,B,H),
walk(P1,P2), state(P2,onfloor,B,H)).
28Example Monkey and Banana
state(atdoor,onfloor,arwindow,hasnot)
walk(atdoor,P2)
state(P2,onfloor,arwindow,hasnot)
push(P2,P2) P2atwindow
climb
backtrack
state(atwindow,onbox,arwindow,hasnot)
state(P2,onfloor,P2,hasnot)
No move possible
climb
state(P2,onbox,P2,hasnot)
grasp P2middle
state(middle,onbox,middle,has)
29Order of Clauses and Goals
- Danger of indefinite looping
- Program variation through reordering of clauses
and goals