fbN
fbN

Reputation: 25

Constructing XOR/3 in Prolog

I have the following clauses for /3 propositional logic.

statement(false).
statement(true).

not(false, true).
not(true,   false).

and(false, false, false).
and(false, true,   false).
and(true,   false, false).
and(true,   true,   true).

or(false, false, false).
or(false, true,   true).
or(true,   false, true).
or(true,   true,   true).

implying(X, Y, Z) :- not(X, Not_X) , or(Not_X, Y, Z).

How would I go about adding XOR clauses?

Upvotes: 2

Views: 332

Answers (2)

CapelliC
CapelliC

Reputation: 60034

You could use one of the equivalences: selecting the first one, (P ∨ Q) ∧ ¬ (P ∧ Q),

xor(P,Q,R) :-
    or(P,Q,O),
    and(P,Q,A),
    not(A,N),
    and(O,N,R).

you get:

?- forall(xor(P,Q,R),writeln(xor(P,Q,R))).
xor(false,false,false)
xor(false,true,true)
xor(true,false,true)
xor(true,true,false)
true.

Upvotes: 2

Guy Coder
Guy Coder

Reputation: 24986

This should work.

xor(false, false, false).
xor(false, true,  true).
xor(true,  false, true).
xor(true,  true,  false).

Upvotes: 1

Related Questions