views:

728

answers:

3

Hi,

There are some special operators in Prolog, one of them is "is", however, recently I came across the =:= operators, and I have no idea how it works.

Can someone explain what the operator does, and also where can I find a predefined list of such special operators and what they do?

Thanks.

+1  A: 

I found my own answer, http://www.cse.unsw.edu.au/~billw/prologdict.html

nubela
+3  A: 
?- 2+3 =:= 6-1.
true.

?- 2+3 is 6-1.
false.

Also please see docs http://www.swi-prolog.org/pldoc/man?predicate=is/2

Xonix
+4  A: 

I think the question deserves a few words of answer here nevertheless.

A short note in advance: Arithmetic expressions in Prolog are just terms ("everything is a term" in Prolog), which are not evaluated automatically. (If you have a Lisp background, think of quoted lists). So 3 + 4 is just the same as +(3,4), which does nothing on its own. It is the responsibility of individual predicates to evaluate those terms.

Several built-in predicates do implicit evaluation, among them the arithmetic comparsion operators like =:= and is. While =:= evaluates both arguments and compares the result, is accepts and evaluates only its right argument as an arithmetic expression. The left argument has to be an atom, either a numeric constant (which is then compared to the result of the evaluation of the right operand), or a variable. If it is a bound variable, its value has to be numeric and is compared to the right operand as in the former case. If it is an unbound variable, the result of the evaluation of the right operand is bound to that variable. is is often used in this latter case, to bind variables.

To pick up on an example from the above linked Prolog Dictionary: To test if a number N is even, you could use both operators:

0 is N mod 2  % true if N is even
0 =:= N mod 2 % dito

But if you want to capture the result of the test you can only use the first variant:

X is N mod 2   % X will be true if N is even
X =:= N mode 2 % !will bomb with argument error!

Rule of thumb: If you just need arithmetic comparison, use =:=; if you want to capture the result of an evaluation, use is.

ThomasH