ARITHMETIC EXPRESSION AND OPERATORS IN PROLOG Artificial Intelligence

















- Slides: 17

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.

ARITHMETIC EXPRESSION AND OPERATORS Arithmetic Evolution: (is) Operator Again, We can query like, ? - X is 3+5. X=8 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.

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.

ARITHMETIC EXPRESSION AND OPERATORS Predefined Arithmetic Functions and Relations: The arithmetic operators available in Prolog: v. Functions, v. 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: vmin/2 ( minimum ) , vabs/1 ( absolute value ) , vsqrt/1 ( square root ) , vsin/1 ( sine ) , vround/1 ( round a float number to an integer) , vmod/2 ( module ). 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 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. (is) operator isn’t needed Arguments are evaluated whenever an arithmetic relation is used.

ARITHMETIC EXPRESSION AND OPERATORS Relations: Besides > (greater) the operator < (lower), Available operator are, v=< (lower or equal) , v>= (greater or equal) , v== (non equal) , v=: = (equal).

ARITHMETIC EXPRESSION AND OPERATORS Relations: The former compares two evaluated arithmetic expressions and later performs logical pattern matching. Example: Differentiation of =: = ? - 2 ** 3 =: = 3+5. and = is crucial. True. Again, False. Because the ? - 2 ** 3 =: = 2+5. matching returns False. false.

ARITHMETIC EXPRESSION AND OPERATORS Relations: ? - 2 ** 5 == 2+4. True ? - 2 ** 5 >= 2+4. True ? - 2 ** 5 =< 2+4. False.

END OF ARITHMETIC EXPRESSION AND OPERATORS THANK YOU!