Chapter 2 Syntax and meaning of prolog programs
description
Transcript of Chapter 2 Syntax and meaning of prolog programs
Chapter 2Syntax and meaning of prolog programs
Part 1
Outline•Data objects•Matching•Meaning of prolog programming.
Quick review •Objects •Relationship •Facts•Rules•Questions
Data objectsdata ob jects
simple objects
constants
atoms numbers
variable
structures
Data objects•Constants : start with a lower-case letter•Variable: start with an upper-case letter.
Data objects - constants•Atoms
o String of letters, digits and the underscore character ‘_’ starting with a lower-case letter
anna x25 x_34a x___y3 miss_Jones
o String of special characters =:: … >=== >----<
But be careful ! Some strings already havea predefined meaning (e.g. :- )
o String of characters enclosed in single quotes
‘ Tom’ ‘tom ’
Data objects - constants•Numberso Integer and real numbers.oReal number are not heavily used in prolog
program. (Prolog is for symbolic, non-numeric computation).
• Example of integer and real numbers: Integer numbers
e.g. 1 133 0 -97 Real numbers
e.g. 3.14 -0.0035 100.2
Data objects - variables•Variables Variables are strings of letters, digits and underscores
characters, starting with upper-case letter or an underscore
E.g. X , Result , Object2
•Anonymous variablesWhen a variable appear in a clause just once.
haschlid(X) : - parent(X ,_).somebody_has_child :- parent(_,_).
Lexical scope.• The lexical scope of atom names is the whole
program.
• The lexical scope of variable names is one clause except the anonymous variable; it signifies new variable each time it occurs even in the same clause
Data objects - structures▫Structures are objects that have several
components which can be structured themselves.
date (1, may, 2001)
▫To represent any day in may; day is variable
date (Day, may, 2001)
functor arguments
Data objects - structures▫All structured objects can be presented
as tree:date
may1 2001
Example (1)•How to structures the following
expression?( a + b( * ) - 5c)
- Using symbols : * , + , and – as functors:1. *(,)2. *( +(a,b) ,).3. *(+(a,b), -(c,5) )
*+ -
a b c 5
Class exercise (1)•Structure the following geometric
shapes:o2d point.oLine segmentoTriangle.
Matching •The most important operation on terms is
Matching.•Given two terms, we say that they match
when:1. They are identical, or2. The variables in both terms can be
instantiated to objects such a way that after the substitution of variables by these objects the terms become identical.
Matching - example▫ date(D, M, 2001) and date(D1, may, Y1)
D= D1M= may.Y1=2001
▫ date(D, M, 2001) and date(D1, M1, 1444)Not matched
▫ date(X, Y, Z) and point(X, Y, Z)Not matched.
Matching• Matching is a process that takes as inputs two terms and
check whether they match. If the terms do not match, we say this process fails. If they match, the process succeeds.
• To request Prolog matching operation, we use ‘=‘ :
?- date(D, M, 2001)=date(D1, may, Y1).D=D1M=mayY1=2001
• Matching in Prolog always results in the most general instantiation.
Example (2)•?- date(D, M, 2001)=date(D1, may,
Y1), date(D, M, 2001)=date(15, M, Y).To satisfy the first goal : date(D, M, 2001)=date(D1, may, Y1). , prolog instantiation will be:D=D1M = mayY1= 2001After specifying the second goal, the instantiation become more specific as follow:D= 15D1 = 13M = mayY1= 2001
Example (2)triangle
point A point
triangle
X point point
1 1 2 3 4 Y 2 Z
The result instantiation is:X = point(1,1)A = point(4,Y)Z =3.
Example (3)• Declare vertical and horizontal relationsvertical (seg(point(X,_), point(X,_))).horizontal (seg(point(_,Y), point(_,Y))).
Formulate the following questions and find Prolog answers:
▫ Is the segment (1,1), (1,4) is vertical?▫ Is the segment (1,1), (2,Y) vertical?▫ Is the segment (1,1), (2,Y) horizontal?▫ Are there any vertical segment that start at point(2,3)?▫ Is there a segment that is both vertical and horizontal?
Example (3) (cont.)▫Is the segment (1,1), (1,4) is vertical?
?- vertical (seg(point(1,1), point(1,4))).Yes
▫Are there any vertical segment that start at point(2,3)?
?-Vertical(seg(point(2,3),P).P= point(2,Y)
▫Is there a segment that is both vertical and horizontal?
?- vertical (S), horizontal (S)S= segment(point(X,Y),point(X,Y).
Class exercise (1)▫Is the segment (1,1), (2,Y) vertical?
▫Is the segment (1,1), (2,Y) horizontal
Class exercise (1) (cont.)▫Is the segment (1,1), (2,Y) vertical?
?- vertical (seg(point(1,1), point(2,Y))).No
▫Is the segment (1,1), (2,Y) horizontal ?- horizontal (seg(point(1,1),
point(2,Y))).Y=1
Meaning of Prolog programs
Meaning of prolog programs
Declarative(Goal true)?
Procedural(How)
Meaning of Prolog programs• Consider the clause: P :- Q, R.
▫ Declarative readings:- P is true if Q and R is true- From Q and R follows P
▫ Procedural readings:- To solve problem P, first solve the subproblem Q, and
then the subproblem R.- To satisfy P, first satisfy Q and then R.
Meaning of Prolog programs• Consider the clause: P :- Q; R.
▫ ; means OR▫Same as the following two clauses together:
P :- Q.P :- R.
• The comma binds stronger than the semicolon. ▫ P :- Q,R;S,T,U.is understood as:▫ P:- (Q,R);(S,T,U).and means the same as the clauses:▫ P :- Q,R.▫ P :- S,T,U.