Add `{-# MINIMAL #-}` to `class Eq` and `class Ord`
[ghc.git] / libraries / ghc-prim / GHC / Classes.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude, MagicHash, StandaloneDeriving, BangPatterns #-}
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 -----------------------------------------------------------------------------
7 -- |
8 -- Module : GHC.Classes
9 -- Copyright : (c) The University of Glasgow, 1992-2002
10 -- License : see libraries/base/LICENSE
11 --
12 -- Maintainer : cvs-ghc@haskell.org
13 -- Stability : internal
14 -- Portability : non-portable (GHC extensions)
15 --
16 -- Basic classes.
17 --
18 -----------------------------------------------------------------------------
19
20 -- #hide
21 module GHC.Classes where
22
23 -- GHC.Magic is used in some derived instances
24 import GHC.Magic ()
25 import GHC.Prim
26 import GHC.PrimWrappers
27 import GHC.Tuple
28 import GHC.Types
29
30
31 infix 4 ==, /=, <, <=, >=, >
32 infixr 3 &&
33 infixr 2 ||
34
35 default () -- Double isn't available yet
36
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
40 -- instances of 'Eq'.
41 --
42 -- Minimal complete definition: either '==' or '/='.
43 --
44 class Eq a where
45 (==), (/=) :: a -> a -> Bool
46
47 {-# INLINE (/=) #-}
48 {-# INLINE (==) #-}
49 x /= y = not (x == y)
50 x == y = not (x /= y)
51 {-# MINIMAL (==) | (/=) #-}
52
53 deriving instance Eq ()
54 deriving instance (Eq a, Eq b) => Eq (a, b)
55 deriving instance (Eq a, Eq b, Eq c) => Eq (a, b, c)
56 deriving instance (Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d)
57 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (a, b, c, d, e)
58 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f)
59 => Eq (a, b, c, d, e, f)
60 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g)
61 => Eq (a, b, c, d, e, f, g)
62 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
63 Eq h)
64 => Eq (a, b, c, d, e, f, g, h)
65 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
66 Eq h, Eq i)
67 => Eq (a, b, c, d, e, f, g, h, i)
68 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
69 Eq h, Eq i, Eq j)
70 => Eq (a, b, c, d, e, f, g, h, i, j)
71 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
72 Eq h, Eq i, Eq j, Eq k)
73 => Eq (a, b, c, d, e, f, g, h, i, j, k)
74 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
75 Eq h, Eq i, Eq j, Eq k, Eq l)
76 => Eq (a, b, c, d, e, f, g, h, i, j, k, l)
77 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
78 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m)
79 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m)
80 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
81 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n)
82 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
83 deriving instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g,
84 Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n, Eq o)
85 => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
86
87 instance (Eq a) => Eq [a] where
88 {-# SPECIALISE instance Eq [Char] #-}
89 [] == [] = True
90 (x:xs) == (y:ys) = x == y && xs == ys
91 _xs == _ys = False
92
93 deriving instance Eq Bool
94 deriving instance Eq Ordering
95 deriving instance Eq Word
96
97 instance Eq Char where
98 (C# c1) == (C# c2) = c1 `eqChar#` c2
99 (C# c1) /= (C# c2) = c1 `neChar#` c2
100
101 instance Eq Float where
102 (F# x) == (F# y) = x `eqFloat#` y
103
104 instance Eq Double where
105 (D# x) == (D# y) = x ==## y
106
107 instance Eq Int where
108 (==) = eqInt
109 (/=) = neInt
110
111 {-# INLINE eqInt #-}
112 {-# INLINE neInt #-}
113 eqInt, neInt :: Int -> Int -> Bool
114 (I# x) `eqInt` (I# y) = x ==# y
115 (I# x) `neInt` (I# y) = x /=# y
116
117 -- | The 'Ord' class is used for totally ordered datatypes.
118 --
119 -- Instances of 'Ord' can be derived for any user-defined
120 -- datatype whose constituent types are in 'Ord'. The declared order
121 -- of the constructors in the data declaration determines the ordering
122 -- in derived 'Ord' instances. The 'Ordering' datatype allows a single
123 -- comparison to determine the precise ordering of two objects.
124 --
125 -- Minimal complete definition: either 'compare' or '<='.
126 -- Using 'compare' can be more efficient for complex types.
127 --
128 class (Eq a) => Ord a where
129 compare :: a -> a -> Ordering
130 (<), (<=), (>), (>=) :: a -> a -> Bool
131 max, min :: a -> a -> a
132
133 compare x y = if x == y then EQ
134 -- NB: must be '<=' not '<' to validate the
135 -- above claim about the minimal things that
136 -- can be defined for an instance of Ord:
137 else if x <= y then LT
138 else GT
139
140 x < y = case compare x y of { LT -> True; _ -> False }
141 x <= y = case compare x y of { GT -> False; _ -> True }
142 x > y = case compare x y of { GT -> True; _ -> False }
143 x >= y = case compare x y of { LT -> False; _ -> True }
144
145 -- These two default methods use '<=' rather than 'compare'
146 -- because the latter is often more expensive
147 max x y = if x <= y then y else x
148 min x y = if x <= y then x else y
149 {-# MINIMAL compare | (<=) #-}
150
151 deriving instance Ord ()
152 deriving instance (Ord a, Ord b) => Ord (a, b)
153 deriving instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
154 deriving instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
155 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
156 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f)
157 => Ord (a, b, c, d, e, f)
158 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g)
159 => Ord (a, b, c, d, e, f, g)
160 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
161 Ord h)
162 => Ord (a, b, c, d, e, f, g, h)
163 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
164 Ord h, Ord i)
165 => Ord (a, b, c, d, e, f, g, h, i)
166 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
167 Ord h, Ord i, Ord j)
168 => Ord (a, b, c, d, e, f, g, h, i, j)
169 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
170 Ord h, Ord i, Ord j, Ord k)
171 => Ord (a, b, c, d, e, f, g, h, i, j, k)
172 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
173 Ord h, Ord i, Ord j, Ord k, Ord l)
174 => Ord (a, b, c, d, e, f, g, h, i, j, k, l)
175 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
176 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m)
177 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m)
178 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
179 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n)
180 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
181 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
182 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n, Ord o)
183 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
184
185 instance (Ord a) => Ord [a] where
186 {-# SPECIALISE instance Ord [Char] #-}
187 compare [] [] = EQ
188 compare [] (_:_) = LT
189 compare (_:_) [] = GT
190 compare (x:xs) (y:ys) = case compare x y of
191 EQ -> compare xs ys
192 other -> other
193
194 deriving instance Ord Bool
195 deriving instance Ord Ordering
196 deriving instance Ord Word
197
198 -- We don't use deriving for Ord Char, because for Ord the derived
199 -- instance defines only compare, which takes two primops. Then
200 -- '>' uses compare, and therefore takes two primops instead of one.
201 instance Ord Char where
202 (C# c1) > (C# c2) = c1 `gtChar#` c2
203 (C# c1) >= (C# c2) = c1 `geChar#` c2
204 (C# c1) <= (C# c2) = c1 `leChar#` c2
205 (C# c1) < (C# c2) = c1 `ltChar#` c2
206
207 instance Ord Float where
208 (F# x) `compare` (F# y)
209 = if x `ltFloat#` y then LT
210 else if x `eqFloat#` y then EQ
211 else GT
212
213 (F# x) < (F# y) = x `ltFloat#` y
214 (F# x) <= (F# y) = x `leFloat#` y
215 (F# x) >= (F# y) = x `geFloat#` y
216 (F# x) > (F# y) = x `gtFloat#` y
217
218 instance Ord Double where
219 (D# x) `compare` (D# y)
220 = if x <## y then LT
221 else if x ==## y then EQ
222 else GT
223
224 (D# x) < (D# y) = x <## y
225 (D# x) <= (D# y) = x <=## y
226 (D# x) >= (D# y) = x >=## y
227 (D# x) > (D# y) = x >## y
228
229 instance Ord Int where
230 compare = compareInt
231 (<) = ltInt
232 (<=) = leInt
233 (>=) = geInt
234 (>) = gtInt
235
236 {-# INLINE gtInt #-}
237 {-# INLINE geInt #-}
238 {-# INLINE ltInt #-}
239 {-# INLINE leInt #-}
240 gtInt, geInt, ltInt, leInt :: Int -> Int -> Bool
241 (I# x) `gtInt` (I# y) = x ># y
242 (I# x) `geInt` (I# y) = x >=# y
243 (I# x) `ltInt` (I# y) = x <# y
244 (I# x) `leInt` (I# y) = x <=# y
245
246 compareInt :: Int -> Int -> Ordering
247 (I# x#) `compareInt` (I# y#) = compareInt# x# y#
248
249 compareInt# :: Int# -> Int# -> Ordering
250 compareInt# x# y#
251 | x# <# y# = LT
252 | x# ==# y# = EQ
253 | True = GT
254
255 -- OK, so they're technically not part of a class...:
256
257 -- Boolean functions
258
259 -- | Boolean \"and\"
260 (&&) :: Bool -> Bool -> Bool
261 True && x = x
262 False && _ = False
263
264 -- | Boolean \"or\"
265 (||) :: Bool -> Bool -> Bool
266 True || _ = True
267 False || x = x
268
269 -- | Boolean \"not\"
270 not :: Bool -> Bool
271 not True = False
272 not False = True
273
274
275 ------------------------------------------------------------------------
276 -- These don't really belong here, but we don't have a better place to
277 -- put them
278
279 divInt# :: Int# -> Int# -> Int#
280 x# `divInt#` y#
281 -- Be careful NOT to overflow if we do any additional arithmetic
282 -- on the arguments... the following previous version of this
283 -- code has problems with overflow:
284 -- | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y#
285 -- | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y#
286 = if (x# ># 0#) && (y# <# 0#) then ((x# -# 1#) `quotInt#` y#) -# 1#
287 else if (x# <# 0#) && (y# ># 0#) then ((x# +# 1#) `quotInt#` y#) -# 1#
288 else x# `quotInt#` y#
289
290 modInt# :: Int# -> Int# -> Int#
291 x# `modInt#` y#
292 = if (x# ># 0#) && (y# <# 0#) ||
293 (x# <# 0#) && (y# ># 0#)
294 then if r# /=# 0# then r# +# y# else 0#
295 else r#
296 where
297 !r# = x# `remInt#` y#