[Haskell-beginners] Ord

Stanisław Findeisen stf-list at eisenbits.com
Fri Dec 30 12:51:13 CET 2011


Hi

Could anyone please explain to me what is going on here?

------------------------------------------------------------------------
-- | The 'Ord' class is used for totally ordered datatypes.
--
-- Instances of 'Ord' can be derived for any user-defined
-- datatype whose constituent types are in 'Ord'.  The declared order
-- of the constructors in the data declaration determines the ordering
-- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
-- comparison to determine the precise ordering of two objects.
--
-- Minimal complete definition: either 'compare' or '<='.
-- Using 'compare' can be more efficient for complex types.
--
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
                  -- NB: must be '<=' not '<' to validate the
                  -- above claim about the minimal things that
                  -- can be defined for an instance of Ord:
                  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 }

        -- These two default methods use '<=' rather than 'compare'
        -- because the latter is often more expensive
    max x y = if x <= y then y else x
    min x y = if x <= y then x else y

http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Classes.html#Ord
------------------------------------------------------------------------

AFAIU this is the definition of the Ord type class in ghc. But what is
this <= function that is used in the definition of compare? Here:

                  else if x <= y then LT

-- 
This e-mail address is invalid, see:
http://people.eisenbits.com/~stf/public-email-note.html .

OpenPGP: E3D9 C030 88F5 D254 434C  6683 17DD 22A0 8A3B 5CC0



More information about the Beginners mailing list