首页 > 解决方案 > 在 Prolog 中构造 XOR/3

问题描述

对于 /3 命题逻辑,我有以下条款。

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).

我将如何添加 XOR 子句?

标签: prologxor

解决方案


您可以使用其中一个等价物:选择第一个,(P ∨ Q) ∧ ¬ (P ∧ Q),

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

你得到:

?- 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.

推荐阅读