260 likes | 419 Views
This chapter explores the syntax and semantics of Prolog programming, covering key concepts such as data objects, including constants, variables, and structures. Readers will learn how to match terms and understand the meanings behind Prolog clauses. The chapter includes practical examples, exercise problems, and insights into the declarative and procedural interpretations of Prolog statements. Gain a solid foundation in Prolog programming, essential for symbolic computation and logical reasoning.
E N D
Outline • Data objects • Matching • Meaning of prolog programming.
Quick review • Objects • Relationship • Facts • Rules • Questions
Data objects • Constants : start with a lower-case letter • Variable: start with an upper-case letter.
Data objects - constants • Atoms • String of letters, digits and the underscore character ‘_’ starting with a lower-case letter anna x25 x_34a x___y3 miss_Jones • String of special characters <----> ===> … ::= But be careful ! Somestrings already havea predefined meaning (e.g. :- ) • String of characters enclosed in single quotes ‘Tom’ ‘tom’
Data objects - constants • Numbers • Integer and real numbers. • Real 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 variables When 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 clauseexcept 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 1 may 2001
Example (1) • How to structures the following expression? (a + b) * (c - 5) • Using symbols : * , + , and – as functors: • *(,) • *( +(a,b) ,). • *(+(a,b), -(c,5) ) * - + c 5 a b
Class exercise (1) • Structure the following geometric shapes: • 2d point. • Line segment • Triangle.
Matching • The most important operation on terms is Matching. • Given two terms, we say that they match when: • They are identical, or • 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= D1 M= 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=D1 M=may Y1=2001 • Matchingin 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=D1 M = may Y1= 2001 After specifying the second goal, the instantiation become more specific as follow: D= 15 D1 = 13 M = may Y1= 2001
Example (2) triangle triangle point point X point A point 1 1 2 3 4 2 Z Y The result instantiation is: X = point(1,1) A = point(4,Y) Z =3.
Example (3) • Declare vertical and horizontal relations vertical (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 • 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.