I would like to implement a simple if and only if relation in my Prolog code. Here is a simple example of what I would like to have:
?- a_iff_b(a, b).
true.
?- a_iff_b(x, x).
true.
?- a_iff_b(a, x).
false.
?- a_iff_b(x, b).
false.
You get the point—the predicate should be true if either the first argument is a
and the second argument is b
or if neither is the first argument a
nor is the second b
. I hope that makes sense. Mathematically speaking, X = a <==> Y = b
.
Here is my attempt:
a_iff_b(X, Y) :-
X = a, Y = b;
X \= a, Y \= b.
But this code introduces a choice point:
?- a_iff_b(a, b).
true ;
false.
Why? It seems that the following works, I mean it does not introduce the choice point:
a_iff_b(X, Y) :- (X = a -> Y = b; Y \= b).
However, I find this a little less readable, especially because in my actual predicate, the two sides of the equivalence are more complex (the are also predicates).