Download presentation
Presentation is loading. Please wait.
Published byErnest Hawkins Modified over 9 years ago
1
Chapter 2 Syntax and meaning of prolog programs Part 1
2
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 )
3
“ 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
4
Arithmetic Operations 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 3 4. ? - 2 is 5 - 3. ? - - 2 is 3 - 5. ? - 2 is 4/2. ? - 1 is mod(7,2). ArithmeticProlog
5
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.
6
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.
7
Arithmetic Operations Example: ? - 277*37 > 10000. ? - 1+2 = 2+1 ? - 1+2 =:= 2+1 ? - X+4 =\= 5+7
8
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
9
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.
10
Matching - examples ? - 14+15 = 14+15. ? - 2*4 = 4*2. ? - 5 = 2 + 3. ? - X+1 = 5+Y
11
Data objects data objects variables constants structuressimple objects numbersatoms
12
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)
13
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 mayDay2015
14
Example (1) How to structures the following expression? (a + b) * (c - 5)
15
Example (1) Using symbols : *, +, and – as functors: 1.*(,) 2.*( +(a,b),). 3.*(+(a,b), - (c,5) ) * + - ab c5 How to structures the following expression? (a + b) * (c - 5)
16
Matching - examples ▫ date(D, M, 2001) and date(D1, may, Y1) ▫ date(D, M, 2001) and date(D1, M1, 1444) ▫ date(X, Y, Z) and point(X, Y, Z)
17
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.
18
Matching - examples (2,5) (3,1) (1,2) Triangle shape has: 3 segments Each segment is declared by two points. Define the shape in the drawing below:
19
Matching - examples point(1,2). point(2,5). point(3,1). 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)). (2,5) (3,1) (1,2)
20
Matching - examples triangle point A triangle X point 1231 2 Y ZB The result instantiation is: A = point(2,Y), X = point(1,2), Z =3, B=1.
21
Matching - examples vertical (seg(point(X,_), point(X,_))). horizontal (seg(point(_,Y), point(_,Y))). Declare vertical and horizontal relations
22
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?
23
Class exercise (1) ▫ Is the segment (1,1), (2,Y) vertical? ▫ Is the segment (1,1), (2,Y) horizontal
24
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
25
Meaning of Prolog programs Meaning of prolog programs Declarative (Goal true?) Procedural (How)
26
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.
27
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.
28
Class Exercise Define the relation: Max( X, Y, Max ). So that Max is the greater of two numbers X and Y.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.