Lecture 5 - Operators and Arithmetic in Prolog
Lecture 5 - Operators and Arithmetic in Prolog
Chapter 3
Operators and Arithmetic in
Prolog
1
3.3 Operator notation
In particular, + and * are said to be infix
operators because they appear between the +
two arguments.
2*a+b*c
Such expressions can be represented as trees, * *
and can be written as Prolog terms with +
and * as functors:
+(*(2,a),*(b,c)) 2 a b c
The general rule is that the operator with the
highest precedence is the principal functor of
the term.
If ‘+’ has a higher precedence value than ‘*’,
then the expression a+ b*c means the same
as
a + (b*c). (+(a, *(b,c)))
If ‘*’ has a higher precedence value than ‘+’,
then the expression a+ b*c means the same
as
(a + b)*c. (*(+(a,b),c))
2
3.3 Operator notation
A programmer can define his or her own
operators.
For example:
We can define the atoms has and supports as
infix operators and then write in the program
facts like:
peter has information.
floor supports table.
The facts are exactly equivalent to:
has( peter, information).
supports( floor, table).
3
3.3 Operator notation
Define new operators by inserting into the program
special kinds of clauses, called directives:
:- op(600, xfx, has).
The precedence of ‘has’ is 600.
4
3.3 Operator notation
There are three groups of operator types:
(1) Infix operators of three types:
xfx xfy yfx
(2) Prefix operators of two types:
fx fy
(3) postfix operators of two types:
xf yf
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.
5
3.3 Operator notation
Precedence of arguments:
This rules help to disambiguate expressions with several
operators of the same precedence.
For example:
Assume that ‘–’ has precedence 500. If ‘–’ is of type yfx, then
the interpretation on the right is invalid because the
precedence of b-c is not less than the precedence of ‘–’.
So, a – b – c is (a – b) – c not a –(b – c)
yfx yfx
– –
yfx – c a –
prec. 0 prec. 0
a b b c
7
3.3 Operator notation
Another example: operator not
If not is defined as fy then the expression
not not p
is legal.
If not is defined as fx then the expression
not not p
is illegal, because the argument to the first not is not
p. here not (not p) is legal.
not
:- op( 900, fy, not). prec. 900
| ?- X = not(not(P)). not
X = (not not P)
prec. 900
Yes
:- op( 900, fx, not). p
| ?- X = not(not(P)). prec. 0
X = (not (not P))
precedence 900
Yes
8
3.3 Operator notation
A set of predefined operators in the Prolog standard.
:- op( 1200, xfx, [:-, -->]).
:- op( 1200, fx, [:-, ?-]).
:- op( 1050, xfy, ->).
:- op( 900, fy, not).
:- op( 700, xfx, [=, \=, ==, \==, =..]).
:- op( 700, xfx, [is, =:=, =\=, <, =<, >, >=, @<, @=<,
@>, @>=]).
:- op( 500, yfx, [+, -]).
:- op( 400, yfx, [*, /, //, mod]).
:- op( 200, xfx, **).
:- op( 200, xfy, ^).
:- op( 200, fy, -).
9
3.3 Operator notation
Boolean expressions
de Morgan’s theorem:
~(A & B) <===> ~A v ~B
One way to state this in Prolog is
equivalence( not( and(A, B)), or( not(A), not(B))).
If we define a suitable set of operators:
:- op( 800, xfx, <===>). <===>
:- op( 700, xfy, v).
:- op( 600, xfy, &). ~ v
:- op( 500, fy, ~).
Then the de Morgan’s theorem & ~ ~
can be written as the fact.
~(A & B) <===> ~A v ~B A B A B
11
Exercise
Consider the program:
t( 0+1, 1+0).
t( X+0+1, X+1+0).
t( X+1+1, Z) :- t( X+1, X1), t( X1+1, Z).
How will this program answer the following questions if ‘+’
is an infix operator of type yfx (as usual):
(a) ?- t(0+1, A).
(b) ?- t(0+1+1, B).
(c) ?- t(1+0+1+1+1, C).
(d) ?- t(D, 1+1+1+0).
(e) ?- t(1+1+1, E).
12
3.4 Arithmetic
Predefined basic arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
** power
// integer division
mod modulo, the remainder of integer division
| ?- X = 1+2.
Operator ‘is’ is a
X= 1+2
built-in procedure.
yes
| ?- X is 1+2.
X= 3
yes
13
3.4 Arithmetic
Another example:
| ?- X is 5/2,
Y is 5//2,
Z is 5 mod 2.
X = 2.5
Y=2
Z=1
Since X is 5-2-1 X is (5-2)-1, parentheses can be used to
indicate different associations. For example, X is 5-(2-1).
Prolog implementations usually also provide standard
functions such as sin(X), cos(X), atan(X), log(X), exp(X),
etc.
| ?- X is sin(3).
X = 0.14112000805986721
Example:
| ?- 277*37 > 10000.
yes
14
3.4 Arithmetic
Predefined comparison operators:
X >Y X is greater than Y
X <Y X is less than Y
X >= Y X is greater than or equal to Y
X =< Y X is less than or equal to Y
X =:= Y the values of X and Y are equal
X =\= Y the values of X and Y are not equal
17
3.4 Arithmetic
Length counting problem:
(Note: length is a build-in procedure)
Define procedure length( List, N) which will count the
elements in a list List and instantiate N to their
number.
(1) If the list is empty then its length is 0.
(2) If the list is not empty then List = [Head|Tail];
then its length is equal to 1 plus the length of the tail
Tail.
These two cases correspond to the following program:
length1( [], 0).
length1( [ _| Tail], N) :- length1( Tail, N1),
N is 1 + N1.
?- length1( [a, b, [c, d], e], N).
N=4
18
3.4 Arithmetic
Another programs:
length2( [], 0).
length2( [_ | Tail], N) :- length2( Tail, N1),
N = 1 + N1.
| ?- length3([a,b,c],N), Length is N.
Length = 3
N = 1+(1+(1+0))
19
3.4 Arithmetic
Summarize:
Built-in procedures can be used for doing arithmetic.
Arithmetic operations have to be explicitly
requested by the built-in procedure is.
There are built-in procedures associated with the
predefined operators +, -, *, /, div and mod.
At the time that evaluation is carried out, all
arguments must be already instantiated to numbers.
The values of arithmetic expressions can be
compared by operators such as <, =<, etc. These
operators force the evaluation of their arguments.
20
Exercise
Define the predicate
sumlist( List, Sum)
so that Sum is the sum of a given list of numbers
List.
21