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
27 infix 4 ==, /=, <, <=, >=, >
31 default () -- Double isn't available yet
33 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
34 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
35 -- and 'Eq' may be derived for any datatype whose constituents are also
38 -- Minimal complete definition: either '==' or '/='.
41 (==), (/=) :: a -> a -> Bool
48 -- XXX This doesn't work:
49 -- deriving instance Eq Bool
50 -- <wired into compiler>:
51 -- Illegal binding of built-in syntax: con2tag_Bool#
52 instance Eq Bool where
57 -- XXX This doesn't work:
58 -- deriving instance Eq Ordering
59 -- Illegal binding of built-in syntax: con2tag_Ordering#
60 instance Eq Ordering where
66 instance Eq Char where
67 (C# c1) == (C# c2) = c1 `eqChar#` c2
68 (C# c1) /= (C# c2) = c1 `neChar#` c2
70 -- | The 'Ord' class is used for totally ordered datatypes.
72 -- Instances of 'Ord' can be derived for any user-defined
73 -- datatype whose constituent types are in 'Ord'. The declared order
74 -- of the constructors in the data declaration determines the ordering
75 -- in derived 'Ord' instances. The 'Ordering' datatype allows a single
76 -- comparison to determine the precise ordering of two objects.
78 -- Minimal complete definition: either 'compare' or '<='.
79 -- Using 'compare' can be more efficient for complex types.
81 class (Eq a) => Ord a where
82 compare :: a -> a -> Ordering
83 (<), (<=), (>), (>=) :: a -> a -> Bool
84 max, min :: a -> a -> a
86 compare x y = if x == y then EQ
87 -- NB: must be '<=' not '<' to validate the
88 -- above claim about the minimal things that
89 -- can be defined for an instance of Ord:
90 else if x <= y then LT
93 x < y = case compare x y of { LT -> True; _ -> False }
94 x <= y = case compare x y of { GT -> False; _ -> True }
95 x > y = case compare x y of { GT -> True; _ -> False }
96 x >= y = case compare x y of { LT -> False; _ -> True }
98 -- These two default methods use '<=' rather than 'compare'
99 -- because the latter is often more expensive
100 max x y = if x <= y then y else x
101 min x y = if x <= y then x else y
103 -- XXX This doesn't work:
104 -- deriving instance Ord Bool
105 -- <wired into compiler>:
106 -- Illegal binding of built-in syntax: con2tag_Bool#
107 instance Ord Bool where
108 compare False True = LT
109 compare True False = GT
112 -- XXX This doesn't work:
113 -- deriving instance Ord Ordering
114 -- Illegal binding of built-in syntax: con2tag_Ordering#
115 instance Ord Ordering where
122 -- We don't use deriving for Ord Char, because for Ord the derived
123 -- instance defines only compare, which takes two primops. Then
124 -- '>' uses compare, and therefore takes two primops instead of one.
125 instance Ord Char where
126 (C# c1) > (C# c2) = c1 `gtChar#` c2
127 (C# c1) >= (C# c2) = c1 `geChar#` c2
128 (C# c1) <= (C# c2) = c1 `leChar#` c2
129 (C# c1) < (C# c2) = c1 `ltChar#` c2
131 -- OK, so they're technically not part of a class...:
136 (&&) :: Bool -> Bool -> Bool
141 (||) :: Bool -> Bool -> Bool