Prolog 中的逻辑“非"是什么? [英] What is the logical 'not' in Prolog?
问题描述
我面临的问题有点微不足道.我想在 Prolog 中使用逻辑 not,但似乎 not/1
不是我想要的东西:
The problem that I face, is a bit trivial.
I want to use logical not in Prolog, but it seems that not/1
is not the thing that I want:
course(ai).
course(pl).
course(os).
have(X,Y) :- course(X),course(Y),not(X = Y).
我查询:
have(X,Y), write(X-Y), nl , fail.
我没有得到我想要的结果:(
And I do not get the result I want :(
推荐答案
代替 not(X = Y)
你需要写 \+ X = Y
或X \= Y
.但考虑使用 dif(X,Y)
代替.dif/2
存在于 B、SWI、YAP、SICStus 中.查看差异:
In place of not(X = Y)
you need to write \+ X = Y
or X \= Y
. But consider to use dif(X,Y)
instead. dif/2
is present in B, SWI, YAP, SICStus. To see the difference:
?- X = b, dif(a, X).
X = b.
?- X = b, \+ a = X.
X = b.
所以到目前为止一切似乎都很好.但是,如果我们只是交换两个目标的顺序?
So up to now everything seems to be fine. But what, if we simply exchange the order of the two goals?
?- \+ a = X, X = b.
false.
?- dif(a, X), X = b.
X = b.
(\+)/1
现在给了我们不同的结果,因为有答案对于 a = X
,目标 \+ a = X
将失败.
(\+)/1
now gives us a different result, because there is an answer
for a = X
, the goal \+ a = X
will fail.
(\+)/1
因此不是否定,但意味着 此时不可证明及时.
(\+)/1
is thus not negation, but means not provable at this point
in time.
dif/2
的安全近似在 ISO Prolog 中也是可能的.
A safe approximation of dif/2
is possible in ISO Prolog, too.
这篇关于Prolog 中的逻辑“非"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!