Prolog中==和=的区别是什么? [英] What is the difference between == and = in Prolog?

查看:80
本文介绍了Prolog中==和=的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 Prolog 中 === 运算符之间的区别?我知道 X = Y 意味着 X 与 Y 统一,并且如果 X 已经与 Y 统一或可以实现,则为真,但我不明白这与 ==.

Can someone explain the difference between the == and the = operator in Prolog? I know that X = Y means X unifies with Y and is true if X already unifies with Y or can be made to, but I don't understand how this differs from ==.

跟进:那(见接受的答案)是有道理的.还有一个问题,是否存在 X \= Y 为真而 X \== Y 为假(或反之亦然)的情况?也就是说,X \= Y 是否测试它们是不能统一还是当前没有统一?

Follow up: That (see Accepted Answer) makes sense. One more question though, is there ever a situation where X \= Y is true and X \== Y is false (or vice-versa)? That is, does X \= Y test if they cannot be unified or if they are not currently unified?

推荐答案

= operator";在 Prolog 中实际上是一个谓词(带有中缀符号)=/2,当这两个术语统一时成功.因此 X = 22 = X 等同于同一件事,目标是将 X 与 2 统一起来.

The = "operator" in Prolog is actually a predicate (with infix notation) =/2 that succeeds when the two terms are unified. Thus X = 2 or 2 = X amount to the same thing, a goal to unify X with 2.

==运算符"不同之处在于它仅在两个术语已经相同而无需进一步统一时才成功.因此 X == 2 只有在变量 X 之前被赋值为 2 时才为真.

The == "operator" differs in that it succeeds only if the two terms are already identical without further unification. Thus X == 2 is true only if the variable X had previously been assigned the value 2.

补充:解决不"时会发生什么很有趣.根据下面 JohnS 的评论,混入这些目标中.请参阅 Amzi 中漂亮的示例集!Prolog 文档.

Added: It's interesting to work through what happens when "not" gets mixed into these goals, per the comment by JohnS below. See the nice set of examples in the Amzi! Prolog documentation.

\= 表示这两个词不能统一,即统一失败.与所有否定的失败应用一样,不统一"是错误的.不会(也不能)导致术语之间的任何统一.

\= means the two terms cannot be unified, i.e. that unification fails. As with all applications of negation as failure, "not unified" does not (and cannot) result in any unification between terms.

\== 表示这两个术语不相同.即使成功,这里也不会统一.

\== means the two terms are not identical. Here also no unification takes place even if this succeeds.

最后想想not(not(X = Y))会做什么.如果 X 和 Y(可以是任意项)可以统一,那么内在目标就成功了,对它的双重否定也是如此.然而,将内在目标包裹在双重否定中会产生一个成功的目标,如果这两个术语可以统一但没有统一这些术语.

Finally think about what not(not(X = Y)) will do. The inner goal succeeds if X and Y (which can be arbitrary terms) can be unified, and so will the double negation of that. However wrapping the inner goal inside the double negation produces a goal that succeeds if the two terms can be unified but without unifying those terms.

留给读者思考 not(not(X == Y)) 是否有任何类似的效用.

It is left as an exercise for the reader to contemplate whether not(not(X == Y)) has any similar utility.

这篇关于Prolog中==和=的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆