12
votes

What is the difference between A \= B and not(A==B) in Prolog?

I found this http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse5 and this wiki page http://en.wikibooks.org/wiki/Prolog/Built-in_predicates but it didn't help me since there is no clarification to the difference, nor short meaning for \=.

Thanks.

1
You might be interested to read also this and this answer - false

1 Answers

19
votes

A \= B is equivalent to not (A = B)

So lets compare =/2 and ==/2 first; from the swi-prolog manual:

?Term1 = ?Term2
Unify Term1 with Term2. True if the unification succeeds

@Term1 == @Term2
True if Term1 is equivalent to Term2.

Notice that =/2 tries to unify the terms and if it succeeds it's true while ==/2 just performs a check:

?- X = 1.
X = 1.
(implicit true.)

while

?- X == 1.
false.

and also:

?- X = Y.
X = Y.

?- X == Y.
false.

now, not/1 will invert the result and be true if =/2 or ==/2 was false.
for==/2 there is nothing complicated; if the terms were equivalent now it will return false otherwise true.
for =/2 you should remember that all unifications will be temporary:

?- \+ (\+ X = 1), print(X).
_G399
true.

(_G399 indicates that X is non-instantiated)