了解Haskell的布尔派生的奥德 [英] Understanding Haskell's Bool Deriving an Ord

查看:145
本文介绍了了解Haskell的布尔派生的奥德的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解你哈斯克尔 presents的布尔类型:

Learn You a Haskell presents the Bool type:

数据布尔=假|真正的推导(ORD)

我不明白其中的道理比较布尔

I don't understand the reason for comparing Bool's.

> False `compare` True
LT
> True `compare` False
GT

如果布尔并没有从奥德

推荐答案

布尔形成的界格 *,其中最常见的。这界格的定义(总)订购,其中真的是严格小于少。 (他们也是这个格子的唯一元素。)

Bool forms a bounded lattice* where False is bottom and True is top. This bounded lattice defines a (total) ordering where False really is strictly less than True. (They are also the only elements of this lattice.)

布尔运算,也可以在看起来的满足的和加入的,分别在这个格。符合认定的最大下界,并加入找到最上限。这意味着 A和&安培;假=假是同样的事情的话称,下界底部和其他任何的底部, A ||真= TRUE 是同样的事情的话称,上限顶什么是顶部。因此,满足并加入,其中使用布尔值的顺序性,相当于你所熟悉的布尔运算。

The boolean operations and and or can also be looked at as meet and join, respectively, in this lattice. Meet finds the greatest lower bound and join finds the least upper bound. This means that a && False = False is the same thing as saying that the lower bound of bottom and anything else is bottom, and a || True = True is the same thing as saying that the upper bound of top and anything is top. So meet and join, which use the ordering property of the booleans, are equivalent to the boolean operations you are familiar with.

您可以使用最大来显示这个在Haskell:

You can use min and max to show this in Haskell:

False `min` True = False -- this is the greatest lower bound
False  &&   True = False -- so is this

False `max` True = True  -- this is the least upper bound
False  ||   True = True  -- so is this

这表明,你可以定义&放大器;&安培; || 只是的从派生奥德实例:

This shows that you can define && and || just from the derived Ord instance:

(&&) = min
(||) = max

请注意,这些定义不在的presence别样的底部的等效的,因为(安培;&安培;)(||)的短路(非严格的第二个参数的时候,首先是,分别),而最大都没有。

Note that these definitions are not equivalent in the presence of a different kind of bottom because (&&) and (||) are short-circuiting (non-strict in the second argument when the first is False or True, respectively) while min and max are not.

此外,小更正:导出子句不说布尔派生自奥德。它指示GHC派生类型类的一个实例奥德的类型布尔

Also, a small correction: The deriving clause does not say thatBool "derives from" Ord. It instructs GHC to derive an instance of the typeclass Ord for the type Bool.

*更具体地,一补充分配格的。还更具体地,一个布尔代数

* More specifically, a complemented distributive lattice. More specifically still, a boolean algebra.

这篇关于了解Haskell的布尔派生的奥德的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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