假设我们有:
g(0,0).
g(0,1).
h(1,2).
f(Z,X):- g(Z,X).
% Match h IFF g does not match
f(Z,X):- \+ g(Z,X), h(Z,X).因此,我们有一个规则,f,它可以决定只从g,或者从h获得解,两者都不可能同时得到:
f(A,B) is true IF:
g(A,B) is true, or
h(A,B) is true and g(A,B) is false).?- f(0, X).
X=0 ; % solution from g
X=1 . % solution from g
?- f(1, X).
X=2 . % solution from h是否有更好的版本:
g,这可能会带来其他不必要的副作用(例如IO)。好的..。这不是完全异或,应该是
f(A,B) is true IFF:
g(A,B) is true and h(A,B) is false, or
h(A,B) is true and g(A,B) is false)...。但也有一个类似的问题:我们能否避免否定。我们能避免多余的指责吗?
发布于 2020-05-21 06:58:11
这是错误的:
g(0,0).
g(0,1).
h(1,2).
f(Z,X):- g(Z,X).
f(Z,X):- \+ g(Z,X), h(Z,X). 如果Z和Y在最后一行是新的,则查询为“如果有任何g(_,_),否则为h(Z,X)”。
观察:
?- f(0,1),f(0,0),f(1,2). % f(1,2) is TRUE
true ;
false.
?- bagof([X,Y],f(X,Y),Bag). % f(1,2) is NOT TRUE
Bag = [[0, 0], [0, 1]].程序不一致.
另一方面:
g(0,0).
g(0,1).
h(1,2).
f(Z,X):- g(Z,X).
f(Z,X):- h(Z,X), \+ g(Z,X).?- f(0,1),f(0,0),f(1,2).
true ;
false.
?- bagof([X,Y],f(X,Y),Bag).
Bag = [[0, 0], [0, 1], [1, 2]].宇宙已从全面毁灭中拯救出来了!
话虽如此:
g(Z,X)的真值,那么你就可以避免在那个时候“冗余地证明”g(Z,X) (或否定),但你不知道。人们也不担心for循环中的“冗余比较”。发布于 2020-05-21 05:12:49
你喜欢用刀子吗?
g(0,0).
g(0,1).
h(1,2).
f(Z,X):- g(Z,X), !.
% Match h IFF g does not match
f(Z,X):- h(Z,X).https://stackoverflow.com/questions/61927594
复制相似问题