gpt4 book ai didi

prolog - 序言中的命题逻辑几乎可以工作,异或有问题

转载 作者:行者123 更新时间:2023-12-04 21:58:36 27 4
gpt4 key购买 nike

我是 prolog 的新手,所以我正在尝试制作一个命题逻辑求解器,它可以解决这个问题:

enter image description here

想法是在像这样咨询时传递 p,q,r,s bool 参数(仅作为示例):

solvelogic(true,true,false,true).

所以我有这个知识库:

implies(X,Y) :- (not(X);Y).                       %that's implies formula and it works
xor(X,Y) :- not(X=Y). %that's xor formula and it works too
and(X,Y) :- X,Y.
or(X,Y) :- X;Y.

implies1(P,Q,R) :- implies(and(P,Q),R).
implies2(P,Q,R) :- implies(P,(or(not(Q),R))).
implies3(P,Q,R) :- implies(implies1(P,Q,R),implies2(P,Q,R)).

所以我可以通过咨询来测试我的代码是否有效:

enter image description here

现在还可以,但让我们尝试使用最终的异或,它接受我需要的整个命题:

enter image description here

它返回 true,这是错误的,因为如果 implies3(true,true,true) 返回 true,那么 xor(implies3(true,true,true),true) 应该返回 false,但实际上没有。

我可以证明 xor(X,Y) 有效:

enter image description here

没错,它返回了false,但是我在查询xor(implies3(true,true,true),true)时不知道发生了什么,它应该返回“false”但它没有,即使它与 xor(true,true) 相同。

可能是什么问题?我觉得我很接近!

最佳答案

因此,为了解决您的问题,您可以用这种方式重写您的 xor/2 谓词:

nand(A,B):- 
not(and(A,B)).

xor(A,B):-
or(A,B),
nand(A,B).

和查询:

?- xor(implies3(true,true,true),true) 
false

xor/2的实现遵循这样的思路:or的真值表是0111and的真值表1000 所以 nand0111。逗号 , 的作用类似于 and 所以 0110 and 01110110 这是 的真值表异或

关于prolog - 序言中的命题逻辑几乎可以工作,异或有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com