Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Syntax and meaning of prolog programs

Similar presentations


Presentation on theme: "Chapter 2 Syntax and meaning of prolog programs"— Presentation transcript:

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
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 34. ?- 2 is 5-3. ?- -2 is 3-5. ?- 2 is 4/2. ?- 1 is mod(7,2).

5 Arithmetic Operations
Example: ?- X = ?- X is ?- X is 5/2 , Y is 2-6 , Z is 5 mod 2 . ?- X is ( )/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 > ?- 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 ? = ?- 2*4 = 4*2. ?- 5 = ?- X+1 = 5+Y

11 Data objects data objects simple objects structures constants
variables atoms numbers

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 may Day 2015

14 Example (1) How to structures the following expression?
(a + b) * (c - 5)

15 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

16 Matching - examples date(D, M, 2001) and date(D1, may, Y1)
date(X, Y, Z) and point(X, Y, Z)

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

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

19 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)

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

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

22 Matching - examples Declare vertical and horizontal relations
vertical (seg(point(X,_), point(X,_))). horizontal (seg(point(_,Y), point(_,Y))).

23 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?

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

25 Class exercise (1) Is the segment (1,1), (2,Y) vertical?
Is the segment (1,1), (2,Y) horizontal

26 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

27 Meaning of Prolog programs
Declarative (Goal true?) Procedural (How)

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

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

30 Class Exercise Define the relation: Max( X , Y , Max ).
So that Max is the greater of two numbers X and Y.

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


Download ppt "Chapter 2 Syntax and meaning of prolog programs"

Similar presentations


Ads by Google