I want to create a clause that will hold if its two boolean parameters are equal and the third parameter is 1 or its two boolean parameters are not equal and the third parameter is 0. My first attempt:
equal_bools(X, Y, N) :-
X = Y,
N=1.
equal_bools(X, Y, N) :-
\+ X = Y,
N=0.
This seems to work:
?- equal_bools(true, true, 0).
false.
?- equal_bools(true, true, 1).
true.
?- equal_bools(true, false, 0).
true.
?- equal_bools(true, false, 1).
false.
The problem is, I really need Y to be bound to the correct value. For example, equal_bools(false, Y, 0). should give back Y = true .. Instead:
?- equal_bools(false, Y, 0).
false.
It seems \+ X = Y checks for equality, but doesn't bind X or Y to any values. How do I fix this? This is for learning purposes, so I'd like to understand exactly how to do this rather than get a built-in clause that does all this or another library.
Nicely deterministic (i.e. no unwanted choicepoints), using (as usual) first-argument indexing, and unification:
Results in swi-prolog: