12

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.

Vitali Pom
  • 602
  • 1
  • 8
  • 29
  • 2
    You might be interested to read also [this](http://stackoverflow.com/questions/8523608/logical-not-in-prolog/8523825#8523825) and [this](http://stackoverflow.com/a/6686911) answer – false Jul 15 '12 at 15:26

1 Answers1

19

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)

Thanos Tintinidis
  • 5,828
  • 1
  • 20
  • 31
  • But what about not(A==B)? From my short experience there is a difference between it & /= since my code works as expected with not and does not with \=. – Vitali Pom Jul 14 '12 at 17:54
  • @VitaliPom sorry if i was not clear; the difference between `not(A==B)` and `(A\=B)` is the same as between `A==B` and `A=B`: `not A==B` will true if the terms are not the same even if they can be unified while `\=` will return false – Thanos Tintinidis Jul 14 '12 at 19:10
  • 1
    @VitaliPom you can view unification as a generalisation of assignment: `X=1` X is unified with 1 and now it's 1. `[X,Y] = [1,2]` X is 1, Y is 2. `[X,2] = [4,Y]` X is 4, Y is 2. `1+X = Y+(Z+3)` Y is 1, X is Z+3. – Thanos Tintinidis Jul 14 '12 at 22:20