1 / 25

Chapter 2 Syntax and meaning of prolog programs

Chapter 2 Syntax and meaning of prolog programs. Part 1. Outline. Data objects Matching Meaning of prolog programming. Quick review . Objects Relationship Facts Rules Questions . Data objects. Data objects. Constants : start with a lower-case letter

media
Download Presentation

Chapter 2 Syntax and meaning of prolog programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2Syntax and meaning of prolog programs Part 1

  2. Outline • Data objects • Matching • Meaning of prolog programming.

  3. Quick review • Objects • Relationship • Facts • Rules • Questions

  4. Data objects

  5. Data objects • Constants : start with a lower-case letter • Variable: start with an upper-case letter.

  6. 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’

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

  8. 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(_,_).

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

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

  11. Data objects - structures • All structured objects can be presented as tree: date 1 may 2001

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

  13. Class exercise (1) • Structure the following geometric shapes: • 2d point. • Line segment • Triangle.

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

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

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

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

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

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

  20. 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).

  21. Class exercise (1) • Is the segment (1,1), (2,Y) vertical? • Is the segment (1,1), (2,Y) horizontal

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

  23. Meaning of Prolog programs

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

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

More Related