|
|
8 Others Elements of Prolog
8.1 Operators
For the arithmetic operators, prolog has already a list of predefined predicates. These are :
=,is,<,>,=<,>=,==,=:=,/,*,+,-,mod,div
In Prolog, the calculations don't write like we have learned.They are written as a binary tree.That is to say that :
y*5+10*x are written in Prolog : +(*(y,5),*(10,x)).
Because we don't have learned to write like this, Prolog accept our way of writing calculations.Nevertheless, we have to say to him the rules of priority on the operators.For instance, we have to say to Prolog that * is prior on +.We have also to indicate to Prolog the prior calculations with '()'.Prolog allows the programmer to define his own operators with the relations next :
Op(P,xfy,name).
Where P is the priority of the operators(between 1 and 1200), xfy indicates if the operator is infix(xfx,xfy,yfx) or postfix(fx,fy).The name is, of course, the name of the operator.Note that the prior operator has the lowest priority.
Prolog has already these predefined operators :
Op(1200,xfx,':-').
Op(1200,fx,[:-,?-]).
Op(1100,xfy,';').
Op(1000,xfy,',').
Op(700,xfx,[=,is,<,>,=<,>=,==,=:=]).
Op(500,yfx,[+.-]).
Op(500,fx,[+,-,not]).
Op(400,yfx,[*,/,div]).
Op(300,xfx,mod).
8.2 Arithmetic
Prolog use the infix operator 'is' to give the result of an arithmetic operation to a variable.is
X is 3 + 4.
Prolog responds
X = 7
yes
When the goal operator 'is' is used the variables on the right must have been unified to numbers before.
Prolog is not oriented calculations, so, after a certain point, he approximates
the calculations and he don't answer the exact number but his approximation
Fo example :
?- X is 1000 + .0001
X = 1000
yes
|
|