Home > Net >  How was the minimal complete definition Ord chosen?
How was the minimal complete definition Ord chosen?

Time:02-22

Data.Ord includes these methods:

compare :: a -> a -> Ordering

(<) :: a -> a -> Bool

(<=) :: a -> a -> Bool

(>) :: a -> a -> Bool

(>=) :: a -> a -> Bool

max :: a -> a -> a

min :: a -> a -> a

The minimal complete definition is compare | (<=).

I understand how the other methods can be determined from either of these two methods.

I don't understand why (>) (for example) can't also be used as a minimal complete definition. not (a > b) is equivalent to a <= b.

Was the decision for (>) to be excluded as a minimal complete definition arbitrary, or am I missing something?

CodePudding user response:

I think the source sheds a little bit of light on this:

class  (Eq a) => Ord a  where
    compare              :: a -> a -> Ordering
    (<), (<=), (>), (>=) :: a -> a -> Bool
    max, min             :: a -> a -> a

    compare x y = if x == y then EQ
                  else if x <= y then LT
                  else GT

    x <  y = case compare x y of { LT -> True;  _ -> False }
    x <= y = case compare x y of { GT -> False; _ -> True }
    x >  y = case compare x y of { GT -> True;  _ -> False }
    x >= y = case compare x y of { LT -> False; _ -> True }

As you can see all comparisons are implemented in terms of compare and compare itself is implemented in terms of <=. The choice is kind of arbitrary (although as Willen van Onsem writes it probably originates from mathematical tradition), but this does rule out allowing an additional operator to be used as minimal definition. There can only be one default definition of compare.

  • Related