Understanding Haskell's Bool Deriving an Ord

前端 未结 3 1386
借酒劲吻你
借酒劲吻你 2021-01-03 19:38

Learn You a Haskell presents the Bool type:

data Bool = False | True deriving (Ord)

I don\'t understand the reason for comparing

3条回答
  •  感动是毒
    2021-01-03 19:54

    It is because Haskell designers made a mistake! 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.

    instance Ord Bool causes a => b to mean what you expect a <= b to mean!

    Earlier arguments in favour of instance Ord Bool where that you can make more types comparable implicitly. 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.

    As for the argument that Bool is a bounded lattice. 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. That is related to those comments discussing && vs min etc.

提交回复
热议问题