什么永远不等于自己? [英] What is never equal to itself?

查看:82
本文介绍了什么永远不等于自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Prolog 中是否存在不等于自身的值?我写answer到一些问题关于树的最小,这个答案还说,如果树为空,则最小为空.起初听起来不错,但现在我认为这听起来很糟糕.

Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea.

如果 null <>null,没问题.但在 Prolog 中,我看到 null 只是原子所以......

It is kinda OK if null <> null, no problem. But in Prolog I see null is just atom so....

?- null = null.
true.

?- null == null.
true.

?- dif(null, null).
false.

我怎样才能在 Prolog 中创建一些总是这样说的术语:

How can I make some term in Prolog that always say:

?- dif(Something, Something).
true.

但是如果它是任何其他事物而不是这个词是 null 的东西仍然说 false.?

But if it is any other thing and not this term that is the null thing still say false.?

或者,如果这不是我在 Prolog 中应该如何思考,那么我应该如何考虑不是 true. 也不是 false. 而是既不是真的也不是假的,因为某些事情不见了"?

Or if this is not how I should think in Prolog then how should I think about not true. and also not false. but "neither true nor false because something is missing"?

推荐答案

只是为了好玩,并不是您真正想要的答案,从字面上看问题标题:

Just for fun, not really the answer you're looking for, taking the question title quite literally:

?- _ == _ .
false.

但是 dif/2 没有被污染(提示:匿名变量的每次出现都代表一个不同的变量):

But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):

?- dif(_, _).
true.

现在,说真的.从树最小谓词示例开始,有一个简单的替代方法:当树为空时,谓词可能会失败.更好的选择可能是使用可选预期 术语库.这些库背后的概念可以在多种编程语言中找到,它们为 null 提供了更好的替代方案.Logtalk 中有这两个库,可以与大多数 Prolog 系统一起使用.见:

Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:

您使用一个或另一个库取决于您对缺失"的解释,意思是可选(没有值很好)或预期(没有一个值是一个错误).例如,假设在您的特定应用程序中,在进行 特定 计算(例如,最小值的总和)时,使用 0 作为空树的最小值是有意义的.一组树).如果树最小谓词返回一个可选的术语引用,Ref,而不是一个整数,你可以做例如

You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.

...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...

与使用 if-then-else 结构相比,这是一个更简洁的解决方案:

This is a cleaner solution compared with using an if-then-else construct:

...,
(   tree_minimum(Tree, Minimum) ->
    Sum1 is Sum0 + Minimum
;   Sum1 is Sum0
),
...

它还允许您为不同的计算使用不同的默认值.例如:

It also allows you to use different defaults for different computations. For example:

...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...

更重要的是,它不会像默认值那样掩盖您正在处理空树的事实.比如下面的代码只会写出非空树的最小值:

More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:

print_tree_minimums(Refs) :-
    meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
    optional(Ref)::if_present(write).

或者,使用 lambda 表达式:

or, using a lambda expression:

print_tree_minimums(Refs) :-
    meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).

这个答案越来越长,我不想把它变成对可选预期的利弊的一般性讨论.但是关于概念和库的描述很容易找到.例如

This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.

https://en.wikipedia.org/wiki/Option_type

https://youtu.be/NhcHwkUPX7w

这篇关于什么永远不等于自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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