2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
6 -- Module : GHC.Classes
7 -- Copyright : (c) The University of Glasgow, 1992-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC extensions)
16 -----------------------------------------------------------------------------
18 module GHC.Classes where
21 -- GHC.Magic is used in some derived instances
25 infix 4 ==, /=, <, <=, >=, >
29 default () -- Double isn't available yet
31 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
32 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
33 -- and 'Eq' may be derived for any datatype whose constituents are also
36 -- Minimal complete definition: either '==' or '/='.
39 (==), (/=) :: a -> a -> Bool
46 -- XXX This doesn't work:
47 -- deriving instance Eq Ordering
48 -- Illegal binding of built-in syntax: con2tag_Ordering#
49 instance Eq Ordering where
55 -- | The 'Ord' class is used for totally ordered datatypes.
57 -- Instances of 'Ord' can be derived for any user-defined
58 -- datatype whose constituent types are in 'Ord'. The declared order
59 -- of the constructors in the data declaration determines the ordering
60 -- in derived 'Ord' instances. The 'Ordering' datatype allows a single
61 -- comparison to determine the precise ordering of two objects.
63 -- Minimal complete definition: either 'compare' or '<='.
64 -- Using 'compare' can be more efficient for complex types.
66 class (Eq a) => Ord a where
67 compare :: a -> a -> Ordering
68 (<), (<=), (>), (>=) :: a -> a -> Bool
69 max, min :: a -> a -> a
71 compare x y = if x == y then EQ
72 -- NB: must be '<=' not '<' to validate the
73 -- above claim about the minimal things that
74 -- can be defined for an instance of Ord:
75 else if x <= y then LT
78 x < y = case compare x y of { LT -> True; _ -> False }
79 x <= y = case compare x y of { GT -> False; _ -> True }
80 x > y = case compare x y of { GT -> True; _ -> False }
81 x >= y = case compare x y of { LT -> False; _ -> True }
83 -- These two default methods use '<=' rather than 'compare'
84 -- because the latter is often more expensive
85 max x y = if x <= y then y else x
86 min x y = if x <= y then x else y
88 -- XXX This doesn't work:
89 -- deriving instance Ord Ordering
90 -- Illegal binding of built-in syntax: con2tag_Ordering#
91 instance Ord Ordering where
98 -- OK, so they're technically not part of a class...:
103 (&&) :: Bool -> Bool -> Bool
108 (||) :: Bool -> Bool -> Bool