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.
Prolog not equal is an operator to compare the two values and operand using a programming language. It is a function of the arithmetic operation using a high-level programming language. The prolog not equal is operators to determine two values are not the same or unmatchable.
Prolog uses the unification technique, and it is a very general form of matching technique. In unification, one or more variables being given value to make the two call terms identical. This process is called binding the variables to values.
The = "operator" in Prolog is actually a predicate (with infix notation) =/2 that succeeds when the two terms are unified. Thus X = 2 or 2 = X amount to the same thing, a goal to unify X with 2. The == "operator" differs in that it succeeds only if the two terms are already identical without further unification.
The conjunct X=Y first unifies the variables X and Y . Thus when the second conjunct X==Y is evaluated, the two variables are exactly the same Prolog object, and the second conjunct succeeds as well.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With