Chapter 2 Syntax and meaning of prolog programs Part 1
Arithmetic Operations Prolog is mainly a language for symbolic computation. Some of the predefined operators can be used for basic arithmetic operations : Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Division - integer result - ( // ) Power ( ** ) Modulation ( mod )
“ is “ Operator we can get the answers of the arithmetic questions by using variables. The left argument of the is operator is a simple object (variable and number) , the right argument is an arithmetic expression. Var_name is Expression
Arithmetic Operations Prolog 2 + 3 = 5 3 x 4 = 12 5 – 3 = 2 3 – 5 = -2 4 / 2 = 2 1 is the remainder when 7 is divided by 2 ?- 5 is 2+3. ?- 12 is 34. ?- 2 is 5-3. ?- -2 is 3-5. ?- 2 is 4/2. ?- 1 is mod(7,2).
Arithmetic Operations Example: ?- X = 1 + 2 . ?- X is 1 + 2 . ?- X is 5/2 , Y is 2-6 , Z is 5 mod 2 . ?- X is (14 + 16)/3 , X + 3 = Y.
Arithmetic Operations Arithmetic operations also uses comparing numerical values. The comparison operators are as follows: =:= Equal. =\= Not equal. < Less than. > Greater than. >= Greater than or equal to. =< Less than or equal to.
Arithmetic Operations Example: ?- 277*37 > 10000. ?- 1+2 = 2+1 ?- 1+2 =:= 2+1 ?- X+4 =\= 5+7
Matching The most important operation on terms is Matching. Two terms are matching when they are equivalent to each other . To request Prolog matching operation, we use ‘=‘ : 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. Expression1 = Expression2
Matching The general rules to decide whether two terms ( S and T) match are as follows: If S and T are constants , they match only if they are the same object. If S is a variable and T is anything , they match only if S is equivalent to an object in T.
Matching - examples ?- 14+15 = 14+15. ?- 2*4 = 4*2. ?- 5 = 2 + 3. ?- X+1 = 5+Y
Data objects data objects simple objects structures constants variables atoms numbers
Structured Data objects A structure object is a complex data types composed of a functor and a fixed number of arguments. Syntax : Functor (arg1,arg2,...) Example : create a structured object of this date : 1 / may / 2015 date (1, may, 2001)
Structured Data objects To represent any day in may (assuming Day is variable) date (Day, may, 2015) All structured objects can be presented as tree: date may Day 2015
Example (1) How to structures the following expression? (a + b) * (c - 5)
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
Matching - examples date(D, M, 2001) and date(D1, may, Y1) date(X, Y, Z) and point(X, Y, Z)
Matching - examples date(D, M, 2001) and date(D1, may, Y1) M= may. Y1=2001 date(D, M, 2001) and date(D1, M1, 1444) Not matched date(X, Y, Z) and point(X, Y, Z)
Matching - examples ?- 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= D1, D1 = 15, M = may, Y1= Y , Y = 2001.
Matching - examples Define the shape in the drawing below: (2,5) Triangle shape has: 3 segments Each segment is declared by two points. (1,2) (3,1)
Matching - examples (2,5) (1,2) (3,1) point(1,2). point(2,5). triangle(point(1,2) , point(2,5) , point(3,1)). seg(point(1,2),point(2,5)). seg(point(2,5),point(3,1)). seg(point(1,2),point(3,1)). triangle(seg(point(1,2),point(2,5)),seg(point(2,5),point(3,1)),seg(point(1,2),point(3,1)). (3,1)
Matching - examples triangle triangle point point X point A point 1 2 3 1 2 Z B Y The result instantiation is: A = point(2,Y), X = point(1,2), Z =3, B=1.
Matching - examples Declare vertical and horizontal relations vertical (seg(point(X,_), point(X,_))). horizontal (seg(point(_,Y), point(_,Y))).
Matching – examples (cont.) Is the segment (1,1), (1,4) is vertical? Are there any vertical segment that start at point(2,3)? Is there a segment that is both vertical and horizontal?
Example (3) (cont.) ?- vertical (seg(point(1,1), point(1,4))). Yes 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))). false Is the segment (1,1), (2,Y) horizontal ? - horizontal (seg(point(1,1), point(2,Y))). Y=1
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. 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 .
Class Exercise Define the relation: Max( X , Y , Max ). So that Max is the greater of two numbers X and Y.
Class Exercise max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X. Define the relation: Max( X , Y , Max ). So that Max is the greater of two numbers X and Y. max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X.