prolog - radford university€¦ · 2 chapter 2 syntax and meaning of prolog programs data objects...
TRANSCRIPT
![Page 1: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/1.jpg)
1/16/2010
1
Prolog Programming
Chapter 2
Syntax and Meaning of Prolog Programs
![Page 2: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/2.jpg)
2
Chapter 2Syntax and Meaning of Prolog Programs
Data Objects
Matching
Declarative meaning of Prolog programs
Procedural meaning
Example : monkey and banana
Order of clauses and goals
The relation between Prolog and logic
![Page 3: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/3.jpg)
3
2.1 Data objects
Atoms and Variables can be strings of the following:
upper-case letters A,B,…,Z
lower-case letters a,b,…,z
digit 0,1,…,9
special characters such as + - * / < > = : . & _ ~
![Page 4: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/4.jpg)
4
Data Objects in Prolog
![Page 5: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/5.jpg)
5
Atoms
Atoms can be constructed in three ways:
String of letters, digits and the underscore character, „_‟,
starting with a lower-case letter :
anna nil x_25 x_25AB x_ miss_Jones
Strings of special characters:
<---> ======> … .:. ::=
Strings of character enclosed in single quotes
„Tom‟ „South_America‟ „Sarah Jones‟
![Page 6: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/6.jpg)
6
Numbers
Integer Numbers (limited range)
1 1313 0 -97
Real Numbers (not used heavily)
3.14 -0.0035 100.2
![Page 7: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/7.jpg)
7
Variables
Variables are strings of letters, digits and underscore
characters. They start with an upper-case letter or an
underscore character:
X Result Object2 Participant_list _x23 _23
![Page 8: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/8.jpg)
8
Anonymous variable
When a variable appears in a clause once only, we do
not have to invent a name for it.
Example,
For all X, X has a child if X is a parent of some Y. In this
case, it does not depend on the name of the child.
In Prolog,
hasachild(X) :- parent(X,Y). can be written as
hasachild(X) :- parent(X,_).
![Page 9: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/9.jpg)
9
More Anonymous
Example
There is somebody who has a child if there are two
objects such that one is a parent of the other:
somenody_has_child :- parent(_,_).
is equivalent to
somenody_has_child :- parent(X,Y).
![Page 10: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/10.jpg)
10
Questions with Anonymous
If the anonymous variable appears in a question
clause then its value is not output
Example
to find the people who have children but not in the
names of children
?- parent(X,_).
![Page 11: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/11.jpg)
11
Lexical Scope of Variables and Constants
lexical scope of variable names is one clause
That means, if X1 occurs in two clauses, then it signifies
two different variables.
Each occurrence of X1 within the same clause means
the same variable
The same atom always means the same object in any
clause, that is, throughout the whole program
![Page 12: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/12.jpg)
12
Structures
Structured objects (structures) are objects that have
several components
Components themselves can, in turn, be structures.
Structures treated in the program as single objects
Functor
to combine the components into a single object
Example, date is a functor
date(1, may, 2003)
![Page 13: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/13.jpg)
13
More Structures
Components can also be variables or other structures
Example, any day in the month of May
date(Day, may, 2003)
Day is a variable and can be instantiated in the execution.
![Page 14: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/14.jpg)
14
terms
All data objects in Prolog are terms
Example,
may and date(1,may,2003) are terms
![Page 15: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/15.jpg)
15
Example ---Structured Objects
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 16: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/16.jpg)
16
Note about functors
If the same name is defined for two different roles such
as point(X1,Y1) and point(X,Y,Z)
Prolog system will recognize the difference by the
number of arguments and will interpret this name as
two funtors: one of them with two arguments and the
other with three arguments.
![Page 17: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/17.jpg)
17
Structures Representation
For arithmetic expression such as
(a+b) * (c-5) can be written as
*(+ (a,b) - (c,5) )
![Page 18: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/18.jpg)
18
Tree structure for (a+b) * (c - 5)
![Page 19: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/19.jpg)
19
More Tree Structures
![Page 20: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/20.jpg)
20
Tree structure representations
For (a) (b) (c) and (d) can be written as
seq(r1,r2)
par(r1,r2)
par(r1,par(r2,r3))
par(r1,seq(par(r2,r3),r4))
![Page 21: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/21.jpg)
21
Chapter 2Syntax and Meaning of Prolog Programs
Data Objects
Matching
Declarative meaning of Prolog programs
Procedural meaning
Example : monkey and banana
Order of clauses and goals
The relation between Prolog and logic
![Page 22: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/22.jpg)
22
2.2 Matching
Given two terms 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 these objects the terms become
identical
![Page 23: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/23.jpg)
23
Example
?- date(D,M,2001) = date(15,may,Y1).
Prolog system will answer:
D = 15
M = may
Y1 = 2001
yes
![Page 24: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/24.jpg)
24
The general rules to decide whether two terms, S and T match are as follows:
If S and T are constants then S and T match only if
they are the same objects
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 principle functor and
all their corresponding components match
The resulting instantiation is determined by the
matching of the components.
![Page 25: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/25.jpg)
25
Illustration for matching
Facts
vertical(seg(point(X,Y),point(X,Y1))). % same X value
horizontal(seg(point(X,Y),point(X1,Y))). % same Y value
Questions
?- 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
yes
![Page 26: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/26.jpg)
26
Chapter 2Syntax and Meaning of Prolog Programs
Data Objects
Matching
Declarative meaning of Prolog programs
Procedural meaning
Example : monkey and banana
Order of clauses and goals
The relation between Prolog and logic
![Page 27: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/27.jpg)
27
2.3 Declarative meaning of Prolog programs
Declarative meaning
To determine whether a given goal is true and if so, for what values
it is true
Instance
An instance of a clause C is the clause C with each of its variables
substituted by some term
Variant
Variant of a clause C is such an instance of the clause C where
each variable is substituted by another variable
![Page 28: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/28.jpg)
28
Example
For a clause
hasachild(X) :- parent(X,Y).
Two variants of the clause are:
hasachild(A) :- parent(A,B).
hasachild(X1) :- parent(X1,X2).
Instances of this clause are:
hasachild(peter) :- parent(peter,Z).
hasachild(barry) :- parent(barry,small(caroline)).
![Page 29: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/29.jpg)
29
The declarative meaning
Given a program and a goal
A goal G is true ( that is, 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 30: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/30.jpg)
30
Difference between declarative and procedural meanings
For a clause
P :- Q , R.
Declarative reading (two alternatives)
P is true if Q and R is true
From Q and R follows P
Procedural reading (two alternatives)
to solve problem P, first solve the sub-problem Q and then the
sub-problem R
to satisfy P, first satisfy Q and then R
![Page 31: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/31.jpg)
31
Conjunction and Disjunction
Conjunction (comma)
P :- Q , R.
both Q and R must be true to satisfy P
Disjunction (semicolon)
P :- Q ; R.
any one of the goals, that is, Q or R has to be true
![Page 32: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/32.jpg)
32
Comma is stronger than Semicolon
P :- Q , R ; S , T , U.
That means,
P :- (Q , R) ; (S , T , U).
Another way with two clauses
P :- Q , R.
P :- S , T , U.
![Page 33: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/33.jpg)
33
Chapter 2Syntax and Meaning of Prolog Programs
Data Objects
Matching
Declarative meaning of Prolog programs
Procedural meaning
Example : monkey and banana
Order of clauses and goals
The relation between Prolog and logic
![Page 34: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/34.jpg)
34
2.4 Procedural meaning
How Prolog answers questions
to answer a question means to try to satisfy a list of
goals
executing a list of goals with respect to a given
program
execute goals means to try to satisfy them
![Page 35: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/35.jpg)
35
Procedure execution
Input :
a program and a goal list
Output
a success/failure indicator and
an instantiation of variables
![Page 36: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/36.jpg)
36
The meaning of the two output results
The success/failure indicator is “yes” if the goals are
satisfiable and “no” otherwise
An instantiation of variables is only produced in the
case of a successful termination: in the case of failure
there is no instantiation
![Page 37: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/37.jpg)
37
Example to illustrate the procedural meaning
Program
big(bear). % Clause 1
big(elephant). % Clause 2
small(cat). % Clause 3
brown(bear). % Clause 4
black(cat). % Clause 5
gray(elephant). % Clause 6
dark(Z) :- black(Z). % Clause 7
dark(Z) :- brown(Z). % Clause 8
Question
?- dark(X), big(X). % who is dark and big?
![Page 38: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/38.jpg)
38
Execution Trace
Step (1)
Initial goal list :
dark(X), big(X).
Step (2)
Scan the program from top to bottom
match the first goal dark(X) in Clause 7
dark(Z) :- black(Z).
replace the first goal by the instantiated body of
clause 7, giving a new goal list:
black(X), big(X).
![Page 39: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/39.jpg)
39
Execution Trace Continue:
Step (3)
Scan the program to find a match with black(X). Clause 5
found black(cat). Since there is no body, the goal is
instantiated X = cat and the goal list shrinks to :
big(cat).
Step (4)
Scan the program for the goal big(cat). No Clause found.
Backtrack to step 3 and undo the instantiation X = cat.
Now the goal list is again :
black(X), big(X).
![Page 40: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/40.jpg)
40
Execution Trace Continue ...
Step (4) continue ...
Continue scanning the program below clause 5. No
clause found. Backtrack to step (2) and continue
scanning below clause 7. Clause 8 is found.
dark(Z) :- brown(Z).
Replace the first goal in the goal list by brown(X) :
brown(X), big(X).
![Page 41: Prolog - Radford University€¦ · 2 Chapter 2 Syntax and Meaning of Prolog Programs Data Objects Matching Declarative meaning of Prolog programs Procedural meaning Example : monkey](https://reader034.vdocuments.net/reader034/viewer/2022042622/5f840bcf6bba3e79b76367e7/html5/thumbnails/41.jpg)
41
Execution Trace Continue ...
Step (5)
Scan the program to match brown(X), finding
brown(bear). Since this clause has no body, the goal list
shrinks to :
big(bear).
Step (6)
Scan the program and find clause big(bear). It has no
body, so the goal list shrinks to empty. This indicates
successful termination and the variable instantiation is :
X = bear