Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lecture 2 - Syntax and Meaning of Prolog Programs

Uploaded by

karimterelbr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 2 - Syntax and Meaning of Prolog Programs

Uploaded by

karimterelbr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Part 1 The Prolog Language

Chapter 2
Syntax and Meaning of
Prolog programs

1
2.1 Data Objects
 In Chapter 1:
 Variables start with data objects
upper-case letters
 Atoms start with lower-

case letters simple objects structures

constants variables

atoms numbers

2
2.1.1 Atoms and numbers
 Characters:
 Upper-case letter A, B,…, Z
 Lower-case letter a, b,…, z
 Digits 0,1,2,…,9
 Special characters such as +-*/<>=:.&_~

 Atoms can be constructed in three ways:


 Strings of letters, digits and the underscore
character,’_’, starting with a lower case letter
 anna, x25, x_35AB, x___y, miss_Jones
 Strings of special characters
 <--->, ===>, …,::=,.:., (except :- )
 Strings of characters enclosed in single quotes
 ‘Tom’, ‘South_America’, ‘Sarah Jones’

3
2.1.1 Atoms and numbers
 Number:
 Numbers used in Prolog include integer
numbers and real numbers.
 Integer numbers: 1313, 0, -97

 Real numbers: 3.14, -0.0035, 100.2

 In symbolic computation, integers are often


used.

4
2.1.2 Variables
 Variables start with an upper-case letter or an
underscore character.
 Examples: X, Result, _x23, _23

 Anonymous variables:
 Examples:

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


hasachild( X) :- parent( X, _).
somebody_has_child :- parent( X, Y).
somebody_has_child :- parent( _, _).
somebody_has_child :- parent( X, X).
?- parent( X, _)
 We are interested in people who have children,
but not in the names of the children.
5
2.1.2 Variables
 The lexical scope of variable names is one clause.
 If the name X occurs in two clauses, then it signifies
two different variables.
hasachild(X) :- parent( X, Y).
isapoint(X) :- point( X, Y, Z).

 But each occurrence of X with in the same clause


means the same variables.
hasachild( X) :- parent( X, Y).

 The same atom always means the same object in


any clause throughout the whole program.

6
2.1.3 Structures
 Structured objects are objects that have several
components.
 All structured objects can be pictured as trees.
 The root of the tree is the functor.
 The offsprings of the root are the components.
 Components can also be variables or other
structures.
date( Day, may, 2001)
 Example: date( 1, may, 2001)
data objects

date (functor, root)


simple objects structures

1 may 2001 (arguments) constants variables

 All data objects in Prolog are terms.


atoms numbers

7
2.1.3 Structures
 Let
a point be defined by its two coordinates,
a line segment be defined by two points, and
a triangle be defined by three points.
 Choose the following functors:
point for points,
seg for line segments, and
triangle for triangles. (6,4)
P2=(2,3)
 Representation:
P1 = point( 1, 1) S T
P2 = point( 2, 3) (4,2)
S = seg( P1, P2) P1=(1,1) (7,1)
= seg( point(1,1), point(2,3))
T = triangle( point(4,2), point(6,4), point(7,1))

8
2.1.3 Structures
 Tree representation of the objects:
P1 = point( 1, 1)
S = seg( P1, P2)
= seg( point(1,1), point(2,3))
T = triangle( point(4,2), point(6,4), point(7,1))

Principal functor
P1=point S=seg T=triangle

1 1 point point point point point

1 1 2 3 4 2 6 4 7 1

9
2.1.3 Structures
 Each functor is defined by two things:
 The name, whose syntax is that of atoms;
 The arity—that is, the number of arguments.

 For example:
point( X1, Y1) and point( X, Y, Z) are different.
 The Prolog system will recognize the difference by
the number of arguments, and will interpret this
name as two functors.

10
2.1.3 Structures
 The tree structure corresponding to the arithmetic
expression (a + b)*(c - 5). infix notation
 Using the simples ‘*’,’+’ and ‘-’ as functors
*(+( a, b), -( c, 5))
?- X = *(+( a, b), -( c, 5)).
?- X is *(+( 3, 4), -( 6, 5)).
* *

+ - + -

a b c 5 3 4 6 5
 In fact, Prolog also allows us to use the infix notation.
(Details will be discussed in Chapter3)

11
Exercise 2.2
 Suggest a representation for
rectangles, squares or circles as
structured Prolog objects.
 For example, a rectangle can be T=triangle
represented by four points (or
maybe three points only).
point point point
 Write some example terms that
represent some concrete objects
of there types using the
4 2 6 4 7 1
suggested representation.

12
2.2 Matching
 The most important operation on terms is matching.
 Matching is a process that takes as input two terms and
checks whether they match.
 Fails: if the terms do not match
 Succeeds: if the terms do match
 Given two terms, we say that they match if:
 they are identical , or
 the variable 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.
 For example:
 the terms date( D, M, 2001) and date( D1, may, Y1)
match
 the terms date( D, M, 2001) and date( D1, M1, 1444)
do not match

13
2.2 Matching
 The request for matching, using the operator ‘=‘:
| ?- date( D, M, 2001) = date(D1, may, Y1).

D1 = D
M = may
Y1 = 2001
Yes

| ?- date( D, M, 2001) = date(D1, may, Y1),


date( D, M, 2001) = date( 15, M, Y).

D = 15
D1 = 15
M = may
Y = 2001
Y1 = 2001
yes

14
2.2 Matching
 Matching in Prolog always
 results in the most general instantiation
 leaves the greatest possible freedom for
further instantiations if further matching is
required

15
2.2 Matching
 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

object.
| ?- date( D, M, 2001) = date(D1, may, 2001).
D1 = D
M = may
yes

 If S is a variable and T is anything, then they match, and S is


instantiated to T.

 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.
| ?- date( date(D, M1, 2003), may, 2001) = date( date(D, D, F), may, 2001).
F = 2003
M1 = D
yes
The resulting instantiation is determined by the matching of the
components. 16
2.2 Matching
 Matching
| ?- triangle( point(1,1), A, point(2,3))=
triangle( X, point(4,Y), point(2,Z)).

X = point(1,1)
A = point(4,Y)
Z=3 triangle triangle
yes

point A point X point point

1 1 2 3 4 Y 2 Z

17
2.2 Matching
 Vertical and horizontal line segments
 ‘Vertical’ is a unary relation.
 A segment is vertical if the x-coordinates of its end-
points are equal.
 The property ‘horizontal’ is similarly formulated, with
x and y interchanged.
vertical( seg( point(X,Y), point(X, Y1))).
horizontal( seg( point(X,Y), point(X1, Y))).

y point(X,Y1)

point(X,Y)

point(X1,Y)
point(X,Y)
x
18
2.2 Matching
 An example:
point(1,1).
point(1,2).
point(2,4).
seg(point(1,1), point(1,2)).
seg(point(1,1), point(2,4)).
seg(point(1,2), point(2,4)).
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
yes
19
2.2 Matching
| ?- horizontal( seg( point(1,1), P)).
P = point(_,1)
Yes

| ?- vertical( seg( point(1,1), P)).


P = point(1,_)
Yes
 The answer means: Yes, any segment that ends at any point (1,_),
which means anywhere on the vertical line x =1.

| ?- vertical( S), horizontal( S).


S = seg( point( A,B), point( A,B))
yes
 The answer means: Yes, any segment that is degenerated to a
point has the property of being vertical and horizontal at the same
time.

20
Exercise
Given some ‘point’ facts as follows.
point(1,1). point(1,2). point(1,3).
point(2,1). point(2,2). point(2,3).
point(3,1). point(3,2). point(3,3).
Please define a rectangle relation to check if certain four
points can construct a rectangle. For example,

|?- rectangle(point(1,1), point( 1, 2), point(2,1), point(2,2)).


Yes
|?- rectangle(point(1,1), point( 1, 2), point(1,3), point(2,2)).
no

21
2.3 Declarative meaning of Prolog
programs
 Consider a clause:
P :- Q, R.
 Some declarative reading of this clause are:
 P is true if Q and R are true.
 From Q and R follows P.
 Two procedural reading of this clause are:
 To solve problem P, first solve the subproblem Q
and then the subproblem R.
 To satisfy P, first satisfy Q and then R.
 Difference:
 The procedural readings do not only define the
logical relations between the head of the clause
and the goals in the body, but also the order in
which the goals are processed.
22
2.3 Declarative meaning of Prolog
programs
 The declarative meaning:
 The declarative meaning of programs determines
whether a given goal is true, and if so, for what
values of variables it is true.
 Instance v.s. variant:
 An instance of a clause C is the clause C with
each of its variables substituted by some term.
 A variant of a clause C is such an instance of the
clause C where each variable is substituted by
another variable.

23
2.3 Declarative meaning of Prolog
programs
 For example, consider the clause:
hasachild( X) :- parent( X, Y).

 Two variants of this clause are:


hasachild( A) :- parent( A, B).
hasachild( X1) :- parent( X1, X2).

 Instances of this clause are:


hasachild( peter) :- parent( peter, X).
hasachild( barry) :- parent( barry,
small(caroline)).

24
2.3 Declarative meaning of Prolog
programs
 A goal G is true if and only if
(1) there is a clause C in the program such that
(2) there is a clause instance I of C such that
(a) the head of I is identical to G, and
(b) all the goals in the body of I are true.

 In general, a question to the Prolog system is a


list of goals separated by commas (,) (the
conjunction of goals).
 A list of goals is true if all the goals in the list are
true for the same instantiation of variables.

25
2.3 Declarative meaning of Prolog
programs
 Prolog also accepts the disjunction of goals:
 Any one of the goals in a disjunction has to be
true.
P :- Q; R.
P is true if Q is true or R is true.
 Example:
P :- Q, R; S, T, U.
P :- (Q, R); (S, T, U).
P :- Q, R.
P :- S, T, U.

26
Exercise 2.6
 Consider the following program:
f( 1, one).
f( s(1), two).
f( s(s(1)), three).
f( s(s(s(X))), N) :- f( X, N).
f( s(s(X)), N) :- f( s(X), N).

How the Prolog answer the following questions?


 ?- f( s(1), A). A= two

 ?- f( s(s(1)), two). = true

 ?- f( s(s(s(1))), C). C=one , two , three

 ?- f( D, three). =infinite

27
2.4 Procedural meaning
 The procedural meaning:
 The procedural meaning specifies how Prolog
answers questions.
 The procedural meaning of Prolog is a procedure
for executing a list of goals with respect to a
given program.
The success/failure indicator is
program ‘yes’ if the goals are satisfiable
and ‘no’ otherwise.

Success/failure indicator
goal list execute
Instantiation of variables

An instantiation of variables is
only produced in the case of a
successful termination. 28
2.4 Procedural meaning
 Procedure execute:
 If the goal list G1,…,Gm is empty then terminate with
success.
 If the goal list is not empty then called SCANNING.
 SCANNING:
 Scan through the clause in the program from top to
bottom until the first clause, C, is found such that the
head of C matches the first goal G1. If there is no such
clause then terminate with failure.
 If there is such a clause C, then rename the variables
in C to obtain a variant C’ of C, such that C’ and the list
G1, …,Gm have no common variables.
 Match G1 and the head of C’. Replace G1 with the
body of C’ (except the facts) to obtain a new goal list.
 Execute this new goal list.

29
2.4 Procedural meaning
 Program: {trace}
| ?- dark(X), big(X). (goal list: dark(X), big(X))
1 1 Call: dark(_16) ? (dark(Z):- black(Z);
big( bear).
goal list: black(X), big(X))
big( elephant). 2 2 Call: black(_16) ?
small( cat). 2 2 Exit: black(cat) ? (yes)
brown( bear). 1 1 Exit: dark(cat) ? (X = cat; goal list: big(X))
black( cat). 3 1 Call: big(cat) ? (no)
gray( elephant). 3 1 Fail: big(cat) ?
1 1 Redo: dark(cat) ? (X != cat, backtrack;
dark(Z):- black(Z).
goal list: dark(X), big(X))
dark(Z):- brown(Z). 2 2 Call: brown(_16) ? (dark(Z):- brown(Z);
goal list: brown(X), big(X))
2 2 Exit: brown(bear) ?
1 1 Exit: dark(bear) ? (yes, X= bear; goal list: big(X))
3 1 Call: big(bear) ? (yes)
3 1 Exit: big(bear) ?
X = bear
yes
{trace}

30
2.4 Procedural meaning
 Whenever a recursive call to execute fails, the execution
returns to SCANNING, continuing at the program clause C that
had been last used before.
 Prolog abandons the whole part of the unsuccessful execution
and backtracks to the point where this failed branch of the
execution was start.
 When the procedure backtracks to a certain point, all the
variable instantiations that were done after that point are
undone.
 Even after a successful termination the user can force the
system to backtrack to search for more solutions.

31
Exercise 2.9
big( bear).
big( elephant).
small( cat).
brown( bear).
black( cat).
gray( elephant).
dark(Z):- black(Z).
dark(Z):- brown(Z).

 In which of the two cases does Prolog have to do more


work before the answer is found?
|?- big(X), dark(X).
|?- dark(X), big(X).

32
33

You might also like