Reputation: 1523
Could Ord
and Enum
be one typeclass? Why doesn't Enum
require Eq
?
Upvotes: 10
Views: 2080
Reputation: 821
Though I'm not sure to what extent this is generally accepted, but I've always viewed Enum
as a typeclass describing types with canonical total preorders (i.e. there might be a loop) such that the sequence of terms "between" any pair of terms is finite (witnessed by enumFromTo
).
Ord
, on the other hand, describes types with canonical total orders, without any requirement that the set of elements between any pair of terms be finite.
Upvotes: 2
Reputation: 26167
There are things that can be enumerated without an order. For example:
data Color = Red | Green | Blue deriving Enum
Which order should colors have? There is no inherent order, even though the colors can be enuemrated.
There are also things that can be enumerated but not compared for equality. Floats, for example, have a NaN value that isn't equal to anything. Floats can be enumerated, however.
Upvotes: 12
Reputation: 185691
Enum
represents types that can be mapped to/from integers. This doesn't say anything about how those types should be sorted, merely that you can represent them with integers.
Ord
represents ordered types. This is different than types that can be mapped to integers. For example, you can't map arbitrary-precision floating point values to integers, but you can order them. And while you technically could try and map Floats to integers, nobody in their right mind would do so.
As for Eq
, Ord
requires this because it doesn't make sense to have a totally ordered datatype that doesn't support equality. However, Enum
has no need for Eq
. Since Enum
doesn't provide any ordering guarantees, it doesn't provide equality guarantees either.
Upvotes: 9