2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
4 -- XXX -fno-warn-unused-imports needed for the GHC.Tuple import below. Sigh.
5 {-# OPTIONS_HADDOCK hide #-}
6 -----------------------------------------------------------------------------
8 -- Module : GHC.Classes
9 -- Copyright : (c) The University of Glasgow, 1992-2002
10 -- License : see libraries/base/LICENSE
12 -- Maintainer : cvs-ghc@haskell.org
13 -- Stability : internal
14 -- Portability : non-portable (GHC extensions)
18 -----------------------------------------------------------------------------
20 module GHC.Classes where
23 -- GHC.Magic is used in some derived instances
31 infix 4 ==, /=, <, <=, >=, >
35 default () -- Double isn't available yet
37 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
38 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
39 -- and 'Eq' may be derived for any datatype whose constituents are also
42 -- Minimal complete definition: either '==' or '/='.
45 (==), (/=) :: a -> a -> Bool
52 deriving instance Eq ()
53 deriving instance (Eq a, Eq b) => Eq (a, b)
54 deriving instance (Eq a, Eq b, Eq c) => Eq (a, b, c)
55 deriving instance (Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d)
56 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (a, b, c, d, e)
57 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f)
58 => Eq (a, b, c, d, e, f)
59 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g)
60 => Eq (a, b, c, d, e, f, g)
61 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
63 => Eq (a, b, c, d, e, f, g, h)
64 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
66 => Eq (a, b, c, d, e, f, g, h, i)
67 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
69 => Eq (a, b, c, d, e, f, g, h, i, j)
70 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
71 Eq h, Eq i, Eq j, Eq k)
72 => Eq (a, b, c, d, e, f, g, h, i, j, k)
73 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
74 Eq h, Eq i, Eq j, Eq k, Eq l)
75 => Eq (a, b, c, d, e, f, g, h, i, j, k, l)
76 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
77 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m)
78 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m)
79 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
80 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n)
81 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
82 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
83 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n, Eq o)
84 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
86 instance (Eq a) => Eq [a] where
87 {-# SPECIALISE instance Eq [Char] #-}
89 (x:xs) == (y:ys) = x == y && xs == ys
92 -- XXX This doesn't work:
93 -- deriving instance Eq Bool
94 -- <wired into compiler>:
95 -- Illegal binding of built-in syntax: con2tag_Bool#
96 instance Eq Bool where
101 -- XXX This doesn't work:
102 -- deriving instance Eq Ordering
103 -- Illegal binding of built-in syntax: con2tag_Ordering#
104 instance Eq Ordering where
110 instance Eq Char where
111 (C# c1) == (C# c2) = c1 `eqChar#` c2
112 (C# c1) /= (C# c2) = c1 `neChar#` c2
114 -- | The 'Ord' class is used for totally ordered datatypes.
116 -- Instances of 'Ord' can be derived for any user-defined
117 -- datatype whose constituent types are in 'Ord'. The declared order
118 -- of the constructors in the data declaration determines the ordering
119 -- in derived 'Ord' instances. The 'Ordering' datatype allows a single
120 -- comparison to determine the precise ordering of two objects.
122 -- Minimal complete definition: either 'compare' or '<='.
123 -- Using 'compare' can be more efficient for complex types.
125 class (Eq a) => Ord a where
126 compare :: a -> a -> Ordering
127 (<), (<=), (>), (>=) :: a -> a -> Bool
128 max, min :: a -> a -> a
130 compare x y = if x == y then EQ
131 -- NB: must be '<=' not '<' to validate the
132 -- above claim about the minimal things that
133 -- can be defined for an instance of Ord:
134 else if x <= y then LT
137 x < y = case compare x y of { LT -> True; _ -> False }
138 x <= y = case compare x y of { GT -> False; _ -> True }
139 x > y = case compare x y of { GT -> True; _ -> False }
140 x >= y = case compare x y of { LT -> False; _ -> True }
142 -- These two default methods use '<=' rather than 'compare'
143 -- because the latter is often more expensive
144 max x y = if x <= y then y else x
145 min x y = if x <= y then x else y
147 -- XXX Deriving this doesn't work:
148 -- ghc-stage1: panic! (the 'impossible' happened)
149 -- (GHC version 6.13.20091123 for x86_64-unknown-linux):
150 -- TcGenDeriv:mk_FunBind
151 instance Ord () where
160 deriving instance (Ord a, Ord b) => Ord (a, b)
161 deriving instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
162 deriving instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
163 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
164 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f)
165 => Ord (a, b, c, d, e, f)
166 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g)
167 => Ord (a, b, c, d, e, f, g)
168 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
170 => Ord (a, b, c, d, e, f, g, h)
171 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
173 => Ord (a, b, c, d, e, f, g, h, i)
174 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
176 => Ord (a, b, c, d, e, f, g, h, i, j)
177 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
178 Ord h, Ord i, Ord j, Ord k)
179 => Ord (a, b, c, d, e, f, g, h, i, j, k)
180 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
181 Ord h, Ord i, Ord j, Ord k, Ord l)
182 => Ord (a, b, c, d, e, f, g, h, i, j, k, l)
183 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
184 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m)
185 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m)
186 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
187 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n)
188 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
189 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
190 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n, Ord o)
191 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
193 instance (Ord a) => Ord [a] where
194 {-# SPECIALISE instance Ord [Char] #-}
196 compare [] (_:_) = LT
197 compare (_:_) [] = GT
198 compare (x:xs) (y:ys) = case compare x y of
202 -- XXX This doesn't work:
203 -- deriving instance Ord Bool
204 -- <wired into compiler>:
205 -- Illegal binding of built-in syntax: con2tag_Bool#
206 instance Ord Bool where
207 compare False True = LT
208 compare True False = GT
211 -- XXX This doesn't work:
212 -- deriving instance Ord Ordering
213 -- Illegal binding of built-in syntax: con2tag_Ordering#
214 instance Ord Ordering where
221 -- We don't use deriving for Ord Char, because for Ord the derived
222 -- instance defines only compare, which takes two primops. Then
223 -- '>' uses compare, and therefore takes two primops instead of one.
224 instance Ord Char where
225 (C# c1) > (C# c2) = c1 `gtChar#` c2
226 (C# c1) >= (C# c2) = c1 `geChar#` c2
227 (C# c1) <= (C# c2) = c1 `leChar#` c2
228 (C# c1) < (C# c2) = c1 `ltChar#` c2
230 -- OK, so they're technically not part of a class...:
235 (&&) :: Bool -> Bool -> Bool
240 (||) :: Bool -> Bool -> Bool