Ordering Bool Types (i.e. True> False) - Why?

Can someone explain the following conclusion?

Prelude> compare True False GT it :: Ordering Prelude> compare False True LT it :: Ordering 

Why are values ​​of type Bool ordered in Haskell - especially since we can demonstrate that the values ​​True and False are not exactly 1 and 0 (unlike many other languages)?

+5
source share
4 answers

Here's how a derived instance of Ord works:

 data D = A | B | C deriving Ord 

Given this data type, we get C > B > A Bool is defined as False | True False | True , and this makes sense when you look at other examples, such as:

  • Maybe a = Nothing | Just a
  • Either ab = Left a | Right b

In each case, when the value of "some" ("true") is greater than no value at all (or the value is "left" or "bad" or "falsy").

+8
source

As long as Bool not Int , it can be converted to 0,1 fragment Int , since it is an Enum type.

 fromEnum False = 0 fromEnum True = 1 

Now Enum could be different, changing 0 and 1 , but this is probably surprising for most programmers who think about bits.

Since it has an Enum type, all other things being equal, it is best to define an Ord instance that follows the same order that satisfies

 compare xy = compare (fromEnum x) (fromEnum y) 

In fact, each instance generated from deriving (Eq, Ord, Enum) follows this property.

In a more theoretical note, logicians seek to order sentences from the strongest to the weakest (forming a lattice). In this structure, False (as a sentence) is the bottom, that is, the smallest element, and True is the top. Although this is only a convention (the theory would be just as good if we chose the opposite order), it’s good to be consistent.

A minor flaw: the logical connection implication is actually p <= q , expressing that p implies q instead of the opposite, as indicated by the arrow.

+5
source

Let me answer your question with a question: why is there an instance of Ord for () ?

Unlike Bool , () has only one possible value: () . So why the hell do you ever want to compare it? There is only one meaning!

In principle, it is useful if all or most of the standard base types have instances for common classes. This makes it easy to output instances for your own types. If Foo does not have an Ord instance, and your new type has one Foo field, you cannot automatically extract the Ord instance.

You may have, for example, some type of tree where we can attach several elements of information to the leaves. Something like Tree xyz . And you might want to have an Eq instance to compare trees. It would be unpleasant if Tree () Int String did not have an Eq instance just because () does not. So why () has Eq (and Ord and several others).

Similar comments apply to Bool . It might not be very useful to compare the two bool values, but it would be annoying if your Ord instance disappears as soon as you put the bool there.

(Another difficult factor is that sometimes we want Ord because there is a logically meaningful order for things, and sometimes we just want some sort of arbitrary order, usually so that we can use something as a key for Data.Map or the like. Perhaps for this & hellip there should be two separate classes, but none.)

+2
source

Basically, it comes from mathematics. In set theory or category theory, Boolean functions are usually regarded as classifiers of subsets / subobjects. In a simple expression, the function f :: a -> Bool identified using filter f :: [a] -> [a] . So, if we change a single value from False to True , then as a result of the filtered list (subset, subobject, whatever) there will be more elements. Therefore, True is considered "more" than False .

0
source

Source: https://habr.com/ru/post/1235880/


All Articles