简体   繁体   English

理解Haskell的Bool派生Ord

[英]Understanding Haskell's Bool Deriving an Ord

Learn You a Haskell presents the Bool type: 了解一下Haskell提供了Bool类型:

data Bool = False | True deriving (Ord)

I don't understand the reason for comparing Bool 's. 我不明白比较Bool的原因。

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

What would be lost if Bool did not derive from Ord ? 如果Bool没有从Ord那里得到什么会失去什么?

Bool forms a bounded lattice * where False is bottom and True is top . Bool形成一个有界格子 *,其中False底部True顶部 This bounded lattice defines a (total) ordering where False really is strictly less than True . 这个有界点阵定义了一个(总)排序,其中False确实严格小于True (They are also the only elements of this lattice.) (它们也是这个晶格的唯一元素。)

The boolean operations and and or can also be looked at as meet and join , respectively, in this lattice. 布尔运算andor也可以分别在此晶格中看作是满足连接 Meet finds the greatest lower bound and join finds the least upper bound. Meet找到最大下限并且join找到最小上限。 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 这意味着a && False = False与底部和其他任何东西的下限都是底部,并且a || True = True a || True = True is the same thing as saying that the upper bound of top and anything is top. a || True = True与顶部和任何顶部的上限相同。 So meet and join, which use the ordering property of the booleans, are equivalent to the boolean operations you are familiar with. 因此,使用布尔值的排序属性的meet和join相当于您熟悉的布尔运算。

You can use min and max to show this in Haskell: 您可以使用minmax在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: 只是从派生的Ord实例:

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

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. 请注意,这些定义在存在不同类型的底部时不等效,因为(&&)(||)是短路的(当第一个分别为FalseTrue ,第二个参数中为非严格)而minmax不是。

Also, a small correction: The deriving clause does not say that Bool "derives from" Ord . 另外,一个小的修正: deriving条款并没有说Bool “来源于” Ord It instructs GHC to derive an instance of the typeclass Ord for the type Bool . 它指示GHC导出类型类的实例Ord 类型Bool

* More specifically, a complemented distributive lattice . *更具体地说,是补充的分配格 More specifically still, a boolean algebra . 更具体地说,还有一个布尔代数

The Ord instance for Bool becomes much more important when you need to compare values that contain Bool somewhere inside. 当您需要比较内部包含Bool值时, BoolOrd实例变得更加重要。 For example, without it we wouldn't be able to write expressions like: 例如,没有它我们将无法编写如下表达式:

[False,True] `compare` [False,True,False]

(3, False) < (3, True)

data Person = Person { name :: String, member :: Bool } deriving (Eq, Ord)

etc. 等等

It is because Haskell designers made a mistake! 这是因为Haskell设计师犯了一个错误! I never saw a mathematics textbook that mentioned ordering of booleans. 我从未见过一本提到布尔序列的数学教科书。 Just beacuse they can be it does not mean with should. 只是因为它们可能并不意味着应该。 Some of us use Haskell exactly because it disallows/protects us from confusing/nonsensical things in many cases but not this one. 我们中的一些人使用Haskell正是因为它在许多情况下不允许/保护我们免于混淆/荒谬的事情而不是这个。

instance Ord Bool causes a => b to mean what you expect a <= b to mean! instance Ord Bool导致a => b表示您期望a <= b表示的意思!

Earlier arguments in favour of instance Ord Bool where that you can make more types comparable implicitly. 早期的论点支持instance Ord Bool ,你可以隐式地使更多类型可比。 Continuing that line of argument some might want to make every type comparable impicitly and even have weak dynamic typing and omit type classes altogether. 继续这一论点,有些人可能希望使每种类型都具有可比性,甚至可以使用弱动态类型并省略类型类。 But we want strong typing exactly to disallow what is not obviously correct, and instance Ord Bool defeats that purpose. 但我们希望强打字能够完全禁止不明显正确的事情,而instance Ord Bool则无法实现这一目的。

As for the argument that Bool is a bounded lattice. 至于Bool是一个有界格子的论点。 Unlike boolean:={True,False}, what we have in Haskell is Bool:={True,False,bottom} is no longer a bounded lattice since neither True nor False are identity elements in the presense of bottom. 与boolean:= {True,False}不同,我们在Haskell中所拥有的是Bool:= {True,False,bottom}不再是有界点,因为True和False都不是底部存在的标识元素。 That is related to those comments discussing && vs min etc. 这与讨论&& vs min等的评论有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM