ARITHMETIC EXPRESSION AND OPERATORS IN PROLOG
Artificial Intelligence Lab Md. Tarek Habib
Department of Computer Science and Engineering Daffodil International University
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution:
Simple arithmetic operators such as ( + or * ) are valid Prolog atoms.
Therefore, expressions like +(3,5) are valid Prolog terms.
They can also be written as infix operators , like in 3+5.
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution:
Without specifically telling Prolog that we are interested in the arithmetic properties of such a term, these expressions are treated purely syntactically i.e. their values are not evaluated.
That means, using ( = ) won’t work the way that you might have expected.
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution:
Example:
?- 3 + 5 = 8.
False.
Here, the term 3+5 and 8 don’t match, the former is compound term whereas the letter is a number
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution: (is) Operator
To check the result, we first have to tell Prolog to arithmetically evaluate the term ( 3 + 5 ).
This is done by using built-in operator (is).
Example:
?- 8 is 3+5.
True.
Again, we can match by the variable with another number.
?- X is 3+5 , X = 8.
X = 8.
?- X is 3+5 , X = 8.
X = 8.
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution: (is) Operator
Again,
We can query like,
If we write like,
?- 3+5 is X False.
Because, is only cause the argument to it’s right and tries to match the result with the left hand argument.
?- X is 3+5.
X = 8
?- X is 3+5.
X = 8
ARITHMETIC EXPRESSION AND OPERATORS
Arithmetic Evolution: (is) Operator
For Multiplication,
?- X is 3 * 8.
X = 24.
For Subtraction,
?- X is 3 - 5.
X = -2
For Division,
?- X is 6/2.
X = 3
Use // for integer division.
Use // for integer division.
ARITHMETIC EXPRESSION AND OPERATORS
Predefined Arithmetic Functions and Relations:
The arithmetic operators available in Prolog:
Functions,
Relations
ARITHMETIC EXPRESSION AND OPERATORS
Functions:
Consider an Expression like,
2 + ( -3.2 * X – max ( 17, X ) ) / 2 * * 5
Here, max /2 expression evaluates to the largest of it’s two arguments
and
2 * * 5 means 2 to the power 5.
ARITHMETIC EXPRESSION AND OPERATORS
Some other Functions:
min/2 ( minimum ) ,
abs/1 ( absolute value ) ,
sqrt/1 ( square root ) ,
sin/1 ( sine ) ,
round/1 ( round a float number to an integer) ,
mod/2 ( module ).
Can be used only on the right side is
operator.
Can be used only on the right side is
operator.
ARITHMETIC EXPRESSION AND OPERATORS
Query with Functions:
max/2:
?- X is max (2 , 4).
X = 4.
min/2:
?- X is min ( 2 , 4 ).
X = 2.
abs/1:
?- X is abs( 2.4) X = 2.4
ARITHMETIC EXPRESSION AND OPERATORS
Query with Functions:
sqrt/1:
?- X is sqrt( 4 ).
X = 2.
round/1:
?- X is round(2.44).
X = 2.
mod/2:
?- X is mod(4 , 2).
X = 0.
?- X is round(2.64).
X = 3.
?- X is round(2.64).
X = 3.
?- X is mod(4 , 3).
X = 1.
?- X is mod(4 , 3).
X = 1.
ARITHMETIC EXPRESSION AND OPERATORS
Relations:
Arithmetic relations are used to compare two evaluated arithmetic expressions.
Example: The goal X > Y succeeds .if expression X evaluates to be a greater number than expression Y.
Arguments are evaluated whenever an arithmetic relation is used.
(is) operator isn’tneeded (is) operator isn’tneeded
ARITHMETIC EXPRESSION AND OPERATORS
Relations:
Besides > (greater) the operator < (lower), Available operator are,
=< (lower or equal) ,
>= (greater or equal) ,
=\= (non equal) ,
=:= (equal).
ARITHMETIC EXPRESSION AND OPERATORS
Relations:
The former compares two evaluated arithmetic expressions and later performs logical pattern matching.
Example:
?- 2 ** 3 =:= 3+5.
True.
Again,
?- 2 ** 3 =:= 2+5.
False.
Differentiation of =:=
and = is crucial.
Differentiation of =:=
and = is crucial.
False. Because the matching returns
false.
False. Because the matching returns
false.
ARITHMETIC EXPRESSION AND OPERATORS
Relations:
?- 2 ** 5 =\= 2+4.True
?- 2 ** 5 =\= 2+4.
True
?- 2 ** 5 >= 2+4.
True
?- 2 ** 5 >= 2+4.
True
?- 2 ** 5 >= 2+4.
True
?- 2 ** 5 >= 2+4.
True
?- 2 ** 5 =< 2+4.
False.
?- 2 ** 5 =< 2+4.
False.
END OF ARITHMETIC EXPRESSION AND OPERATORS
THANK YOU!