Download presentation

Presentation is loading. Please wait.

Published byUlises Grenfell Modified about 1 year ago

1
2. Syntax and Meaning

2
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

3
Data Objects Data objects in Prolog: Data objects Simple objects Structures Constants Variables Atoms Numbers

4
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 objects Structures Constants Variables Atoms Numbers

5
Data 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). Data objects Simple objects Structures Constants Variables Atoms Numbers

6
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 functorarguments date 1 may1983 Any day in May 1983 can be represented as –date(Day, may, 1983)

7
Data Objects P1=(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))

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

9
Data Objects All structured objects in Prolog are trees, represented in the program by terms. (a+b)*(c-5) *(+(a,b),-(c,5)) * +– c5ab

10
Data Objects r1r2 seq r1r2 seq(r1,r2) r1 r2 par r1 r2 par(r1,r2) r1 r3 r2 par r1 r2r3 par par(r1,par(r2,r3)) r1 r3 r2r4 par r1 r2 r3 seq par r3 par(r1,seq(par(r2,r3),r4))

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

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

13
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=D1 M=may Y1=1983 D=1 D1=1 M=may Y1=1983 D=third D1=third M=may Y1=1983 Less general Matching in Prolog always results in the most general instantiation.

14
Matching ?- date(D,M,1983)=date(D1,may,Y1), date(D,M,1983)=date(15,M,Y). To satisfy the first goal: D=D1 M=may Y1=1983 After having satisfied the second goal: D=15 D1=15 M=may Y1=1983 Y=1983

15
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

16
Matching triangle pointA triangle Xpoint 4y2Z

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

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

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

20
Procedural Meaning

21

22
The procedural meaning specifies how Prolog answers question.

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

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

25
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 position of monkey Vertical position of monkey Position of box Monkey has or has not banana

26
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 around Such rules can be formalized in Prolog as a 3-place relation named move: move(State1, Move, State2)

27
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 canget move State2 has State m

28
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 move possible climb backtrack push(P2,P2’) P2=atwindow state(P2’,onbox,P2’,hasnot) climb state(middle,onbox,middle,has) grasp P2’=middle

29
Order of Clauses and Goals Danger of indefinite looping Program variation through reordering of clauses and goals

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google