[go: up one dir, main page]

0% found this document useful (0 votes)
25 views27 pages

Chapter 4 - Operators and Arithmetic in Prolog

Logic Programming 4

Uploaded by

uompradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views27 pages

Chapter 4 - Operators and Arithmetic in Prolog

Logic Programming 4

Uploaded by

uompradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter 4

Operators and Arithmetic In Prolog

Prof. N. G. J. Dias
Senior Professor
Department of Computer Systems Engineering,
Faculty of Computing and Technology,
University of Kelaniya,
Kelaniya
4.1Introduction
• Prolog is mainly a language for symbolic computations, arithmetic
operations can be carried out in a simple way.

Arithmetic operators

Pre-defined operators User defined operators


(Built-in operators)

Basic arithmetic operators Comparison operators

10 May 2025 NGJD@FCT-UoK 2


4.2 Basic Arithmetic Operators
• The following predefined operators can be used for basic arithmetic
operations.
+ - addition
- - subtraction
* - multiplication
/ - division
** - power
// - integer division, i.e., X // Y returns the integer part of
division of X by Y.
Example: 5 // 2 =2.
mod - modulo, the remainder of integer division,
i.e., X mod Y returns the remainder after division of X by Y.
Example: 5 mod 2 =1.
10 May 2025 NGJD@FCT-UoK 3
• As in Mathematics, the usual rules for precedence of operators will be
assumed in a mixed operator expression, the evaluation will be
carried out in the following order.

Low precedence (i) Bracketed expressions always evaluating the


inner most brackets first.
(ii) mod
(iii) *, /, //
High precedence (iv) +, -
• For operators of equal precedence, evaluation of the expression will
be from left to right.
• In a mixed operator expression, operator with the lowest precedence
(i.e., binds most strongly) is evaluated first.
• As a rule, the operator with the highest precedence is the principal
functor of a term.

10 May 2025 NGJD@FCT-UoK 4


Examples:
1. 4+(3.1-2+(5*3.2-1)) will be evaluated as
4+((3.1-2)+((5*3.2)-1)) = 20.1
2. 3*7 // 3 will be evaluated as (3*7) // 3= 21 // 3= 7
3. 120+7/2 will be evaluated as 120+(7/2)=120+3.5= 123.5

• In Prolog, there are built-in procedures or built-in predicates


associated with pre-defined operators +, -, *, /, **, // and mod.
• Note that, the precedence of operators decides what is the correct
interpretation of expression.

10 May 2025 NGJD@FCT-UoK 5


4.3 Evaluation using ‘is’
• Arithmetic expressions are not automatically evaluated in Prolog.
• To force the evaluation of an expression, special predefined operator
called ‘is’ is provided.
• If we try the goal
|?_X is 5+4.
Prolog will answer X=9
• Note that the answer to the query,
|?_X=5+4.
is X=5+4 as this merely instantiate X to the expression 5+4 without
evaluating the expression.

10 May 2025 NGJD@FCT-UoK 6


• The left argument of the ‘is’ operator must be a simple object.
• The right argument is evaluated and unified with the left argument.
• In this case the right argument is an arithmetic expression composed
of arithmetic operators, numbers and variables.
• Since the ‘is’ operator will forced the evaluation, all the variables in
the expression must already be instantiated to numbers at the time of
execution of this goal.
Examples:
1. The goal
|?_12 is 7+5.
succeeds, as 7+5 is evaluated and unifies with 12.
2. The goal
|?_3+5 is 5+3.
fail, as the left argument 3+5 does not unify with 8 which is the
result of evaluating the expression on the right.
10 May 2025 NGJD@FCT-UoK 7
3. The goal
|?_N is N+1.
Always fails because the result of evaluating N+1 never
unifies with N.
4. Answer to the query
|?_X is 3/2,Y is 3 // 2.
is
X=1.5
Y=1

10 May 2025 NGJD@FCT-UoK 8


4.4 Comparison operators
• Arithmetic is also involved when comparing numerical values, i.e., the
values of arithmetic expressions can be compared using comparison
operators.
• Following are some predefined comparison operators in Prolog,
where T1 and T2 denotes arithmetic expressions.

Notation Description
1. T1=:=T2 The value of T1 is equal to the value of T2.
2. T1=\=T2 The value of T1 is not equal to the value of T2.
3. T1 > T2 The value of T1 is greater than the value of T2.
4. T1 < T2 The value of T1 is less than the value of T2.
5. T1 >=T2 The value of T1 is greater than or equal the value of T2.
6. T1 =<T2 The value of T1 is less than or equal to that of T2.

10 May 2025 NGJD@FCT-UoK 9


• Each operator force the evaluation of the arithmetic expression T1
and T2 then compared.
• Note:
In the answer to the query.
|?_A<B.
A and B are evaluated as arithmetic expressions and the resulting
numbers are compared.
If A or B is not an arithmetic expression, the goal will fail.
Other comparison operators work in the same way.

Examples:
1. 5>3-succeeds.
2. 7-2<5+1 succeeds.
3. 3+4=:=2+5 succeeds.
4. 2<1 fails.
5. A<1 gives an error when A is a variable.
10 May 2025 NGJD@FCT-UoK 10
• Note that the following set of predefined comparison predicates do
not evaluate the terms associated with them, but either try to unify the
terms, or compare the terms as symbolic expressions.

Notation Description
1 T1 = T2 T1 unifies with (or matches with) T2
2 T1 \= T2 T1 does not unifies with T2.
3 T1 == T2 T1 and T2 are identical expressions
4 T1 \== T2 T1 and T2 are not identical expressions

10 May 2025 NGJD@FCT-UoK 11


Example
Query Response
1 X = 3 + 5. X=3+5
2 3 + 5 = 3 + 5. yes
3 1 + 2 = 2 + 1. no
4 6 + 4 \= 5 + 5. yes
5 9 – 4 == 10 – 5. no
6 9 – 4 == 9 – 4. yes
7 [a, b, c] \== [a, b, c]. no
8 [a, b, c] \== X. yes
9 1 + A = B + 2. A = 2 and B = 1.

10 May 2025 NGJD@FCT-UoK 12


4.5 Some examples involving arithmetic operations
4.5.1 Greatest Common Divisor of two Positive Integers
• Given two positive integers, X and Y, their greatest common devisor
D can be found according to three cases:
(1) If X and Y are equal then D is equal to X.
(2) If X<Y then D is equal to the greatest common devisor of X
and difference Y-X, i.e., gcd(X,Y)=gcd(X,Y-X).
(3) If Y<X then do the same as in case (2) with X and Y
interchanged.
• It can be easily shown by an example that these three rules actually
work.
Choosing, for example, X=20 and Y=25 the above rules would give
D=5 after sequence of subtractions.
10 May 2025 NGJD@FCT-UoK 13
• These rules can be formulated into a Prolog program by defining
three argument relation (3-place predicate),
gcd(X,Y,D) .
The three rules are then expressed as three clauses as follows.
gcd(X,X,X).
gcd(X,Y,D):-X<Y, Y1 is Y-X, gcd(X,Y1,D).
gcd(X,Y,D):-Y<X, gcd(Y,X,D).

• Note that the last goal of the third clause would be replaced by the
following two goal:
X1 is X-Y, gcd(X1,Y,D).

10 May 2025 NGJD@FCT-UoK 14


4.5.2 The length of a list
• In this case we have to count the number of items in a list.
• To define the predicate, length(L,N), which is true iff N is the number
of elements in the list L.
• Note that,
1. If the list is empty, then the length of L is zero.
2. If the list is non-empty, then L=[Head|Tail] and its length is equal
to 1 plus the length of the tail Tail.
• These two cases correspond to the following program.
length([ ],0).
length([_|Tail],N):-length(Tail,N1),N is N1+1.
Example
The Prolog query,
|?_length([a,b,[c,d],e],N).
produce the response N= 4.
10 May 2025 NGJD@FCT-UoK 15
• Note 1:
The second clause of length, the two goal of the body cannot be
swapped. The reason for this is that N1 has to be instantiated before
the goal N is N1+1 can be proceeded.
• Note 2:
It is interesting to see what happens if we try to program the length
relation without the use of ‘is’.
Consider the following program:
length1([ ],0).
length1([_|Tail ],N):- length1(Tail,N1), N = 1+N1.
Now the goal
|?_length1([a,b,[c,d],e],N)
will now produce the answer
N=1+(1+(1+(1+0))).
10 May 2025 NGJD@FCT-UoK 16
The addition was never explicitly forced and therefore was not carried
out at all.
But in ‘length1’ we can, unlike ‘length’, swap the goal in the second
clause.
length1([ ],0).
length1([_|Tail],N):-N=1+N1, length1(Tail,N1).
This version of length1 will produce the same result as the original.
It can also be written by shorter as follows.
length1([ ],0).
length1([_|Tail],1+N1):-length1(Tail,N1).
still producing the same result.

10 May 2025 NGJD@FCT-UoK 17


We can, however, use length1 to find the number of elements in a list
as follows:
|?_length1([a,b,c],N), Length is N.
N=1+(1+(1+0))
Length=3.

10 May 2025 NGJD@FCT-UoK 18


4.6 Using Operators
4.6.1Types of Operators
• Operators are classified into 3 groups, namely: prefix, infix and
postfix according to their position in an expression.
• The valid operator types in each group is given below:
Prefix (2 types) : fx, fy
Infix (3 types) : xfx, xfy, yfx
Postfix (2 types) : xf, yf
Here, f denotes the operator and, x and y represent the arguments.
• Note 1
(i) Infix operator appears between two arguments.
(ii) The prefix and postfix operators have only one argument,
which follows or precedes the operator respectively.
10 May 2025 NGJD@FCT-UoK 19
• Note 2
There is a difference between ‘x’ and ‘y’.
To explain this we need to introduce the notion of the precedence of
argument.
If an argument is enclosed in parentheses or it is an unstructured
object then its precedence is 0; if an argument is a structure then its
precedence is equal to the precedence of its principal functor.
‘x’ represents an argument whose precedence must be strictly lower
than that of the operator.
‘y’ represents an argument whose precedence is lower or equal to
that of the operator.

10 May 2025 NGJD@FCT-UoK 20


Example: Consider the expression 𝑎 − 𝑏 − 𝑐.
Normally this expression is evaluated as (𝑎 − 𝑏) − 𝑐 and not as 𝑎 − (𝑏 − 𝑐).
To achieve the normal interpretation the operator ‘-’ has to be defined as yfx.
If ‘-’ is defined as an infix operator with the notation xfy, then the expression will be
evaluated from right to left, as 𝑎 − (𝑏 − 𝑐).

Following figure shows the two interpretations of the expression a – b – c assuming


that ‘-’ has precedence 500. −

− 𝑐 𝑎 −
prec. 0 prec. 0
𝑎 𝑏 𝑏 𝑐

precedence 500 precedence 500


Interpretation 1: 𝑎 − 𝑏 − 𝑐 Interpretation 2: 𝑎 − (𝑏 − 𝑐)
10 May 2025 NGJD@FCT-UoK 21
4.6.2 Precedence of an Operator
• The precedence of an operator is an integer (in the range 1-1200 in
Wisdom Prolog) which determines which operator in an expression is
the principal functor.
• The operator with the highest precedence is the principal functor of
an expression, while operators with low precedence bind more tightly
than operators with higher precedence.
Example: consider the expression 2*a+b*c.
Since + has the highest precedence than *, + is the principal functor.
Furthermore, * binds the arguments stronger than +.
Hence, the tree diagram for 2*a+b*c is
+
* *
2 a b c
10 May 2025 NGJD@FCT-UoK 22
4.6.3 User defined Types
• A programmer can define his or her own operators.
• Each operator is defined by its precedence, type and name.
• In order to define a new operator, we insert a special kind of clause,
sometimes called a directive, to the program.
• This clause (directive) act as an operator definition.
• Such a clause (directive) has the form
:-op(Precedence, Type, Name).
• An operator definition must appear in the program before any
expression containing that operator.

10 May 2025 NGJD@FCT-UoK 23


Examples
1. Following are the definitions of some predefined operators in Prolog:
:- op( 1200, xfx, [:-, -->] ).
:- op( 1200, fx, [:-, ?-] ).
:- op( 1100, xfy, ‘:’ ).
:- op( 1050, xfy, -> ).
:- op( 1000, xfy, ‘,’ ).
:- op( 900, fy, [not, \+] ).
:- op( 700, xfx, [=, \=, ==, \==, =..] ).
:- op( 700, xfx, [is, =:=, =\=, <, =<, >, >=, @<, @=<, @>, @>=] ).
:- op( 500, yfx, [+, -] ).
:- op( 400, yfx, [*, /, //, mod] ).
:- op( 200, xfx, ** ).
:- op( 200, xfx, ^ ).
:- op( 200, fy, - ).
10 May 2025 NGJD@FCT-UoK 24
2. Consider the following de Morgan’s theorem:
∼ 𝐴 & 𝐵 < === > ∼ 𝐴 ∨ ∼ 𝐵.

One way to state this in Prolog is by the clause:


𝑒𝑞𝑢𝑖𝑣𝑎𝑙𝑒𝑛𝑐𝑒 𝑛𝑜𝑡 𝑎𝑛𝑑 𝐴, 𝐵 , 𝑜𝑟 𝑛𝑜𝑡 𝐴 , 𝑛𝑜𝑡 𝐵 .

By defining a suitable set of operators:


:- op( 800, xfx, < === > ). % Logical equivalence
:- op( 700, xfy, ∨ ). % Disjunction
:- op( 600, xfy, & ). % Conjuction
:- op( 500, fy, ∼ ). % Negation
we can write the de Morgan’s theorem as the fact:
∼ 𝐴 & 𝐵 < === > ∼ 𝐴 ∨ ∼ 𝐵.

According to our specification of operators above, this term is understood as


shown below.
10 May 2025 NGJD@FCT-UoK 25
< === >

∼ ∨

& ∼ ∼

𝐴 𝐵 𝐴 B

10 May 2025 NGJD@FCT-UoK 26


3. We can define the atoms ‘has’ and ‘supports’ as infix operators and then write facts
like
peter has information.
floor supports table.
as
has(peter, information).
supports(floor, table).

The required operator definitions are


:-op(600, xfx, has).
:-op(600, xfx, supports).

10 May 2025 NGJD@FCT-UoK 27

You might also like