Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not equal and not unify in Prolog

Tags:

prolog

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.

like image 818
Vitali Pom Avatar asked Jul 14 '12 16:07

Vitali Pom


People also ask

What is not equal in Prolog?

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.

What is unify in Prolog?

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.

What is the difference between and == in Prolog?

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.

How do you know if two variables are equal in Prolog?

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.


1 Answers

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)

like image 178
Thanos Tintinidis Avatar answered Oct 11 '22 00:10

Thanos Tintinidis