Move divInt#/modInt# from base
[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.Tuple
27 import GHC.Types
28 -- For defining instances for the generic deriving mechanism
29 import GHC.Generics (Arity(..), Associativity(..), Fixity(..))
30
31
32 infix 4 ==, /=, <, <=, >=, >
33 infixr 3 &&
34 infixr 2 ||
35
36 default () -- Double isn't available yet
37
38 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
39 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
40 -- and 'Eq' may be derived for any datatype whose constituents are also
41 -- instances of 'Eq'.
42 --
43 -- Minimal complete definition: either '==' or '/='.
44 --
45 class Eq a where
46 (==), (/=) :: a -> a -> Bool
47
48 {-# INLINE (/=) #-}
49 {-# INLINE (==) #-}
50 x /= y = not (x == y)
51 x == y = not (x /= y)
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
150 deriving instance Ord ()
151 deriving instance (Ord a, Ord b) => Ord (a, b)
152 deriving instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
153 deriving instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
154 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
155 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f)
156 => Ord (a, b, c, d, e, f)
157 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g)
158 => Ord (a, b, c, d, e, f, g)
159 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
160 Ord h)
161 => Ord (a, b, c, d, e, f, g, h)
162 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
163 Ord h, Ord i)
164 => Ord (a, b, c, d, e, f, g, h, i)
165 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
166 Ord h, Ord i, Ord j)
167 => Ord (a, b, c, d, e, f, g, h, i, j)
168 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
169 Ord h, Ord i, Ord j, Ord k)
170 => Ord (a, b, c, d, e, f, g, h, i, j, k)
171 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
172 Ord h, Ord i, Ord j, Ord k, Ord l)
173 => Ord (a, b, c, d, e, f, g, h, i, j, k, l)
174 deriving instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
175 Ord h, Ord i, Ord j, Ord k, Ord l, Ord m)
176 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m)
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, Ord l, Ord m, Ord n)
179 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
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, Ord m, Ord n, Ord o)
182 => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
183
184 instance (Ord a) => Ord [a] where
185 {-# SPECIALISE instance Ord [Char] #-}
186 compare [] [] = EQ
187 compare [] (_:_) = LT
188 compare (_:_) [] = GT
189 compare (x:xs) (y:ys) = case compare x y of
190 EQ -> compare xs ys
191 other -> other
192
193 deriving instance Ord Bool
194 deriving instance Ord Ordering
195 deriving instance Ord Word
196
197 -- We don't use deriving for Ord Char, because for Ord the derived
198 -- instance defines only compare, which takes two primops. Then
199 -- '>' uses compare, and therefore takes two primops instead of one.
200 instance Ord Char where
201 (C# c1) > (C# c2) = c1 `gtChar#` c2
202 (C# c1) >= (C# c2) = c1 `geChar#` c2
203 (C# c1) <= (C# c2) = c1 `leChar#` c2
204 (C# c1) < (C# c2) = c1 `ltChar#` c2
205
206 instance Ord Float where
207 (F# x) `compare` (F# y)
208 = if x `ltFloat#` y then LT
209 else if x `eqFloat#` y then EQ
210 else GT
211
212 (F# x) < (F# y) = x `ltFloat#` y
213 (F# x) <= (F# y) = x `leFloat#` y
214 (F# x) >= (F# y) = x `geFloat#` y
215 (F# x) > (F# y) = x `gtFloat#` y
216
217 instance Ord Double where
218 (D# x) `compare` (D# y)
219 = if x <## y then LT
220 else if x ==## y then EQ
221 else GT
222
223 (D# x) < (D# y) = x <## y
224 (D# x) <= (D# y) = x <=## y
225 (D# x) >= (D# y) = x >=## y
226 (D# x) > (D# y) = x >## y
227
228 instance Ord Int where
229 compare = compareInt
230 (<) = ltInt
231 (<=) = leInt
232 (>=) = geInt
233 (>) = gtInt
234
235 {-# INLINE gtInt #-}
236 {-# INLINE geInt #-}
237 {-# INLINE ltInt #-}
238 {-# INLINE leInt #-}
239 gtInt, geInt, ltInt, leInt :: Int -> Int -> Bool
240 (I# x) `gtInt` (I# y) = x ># y
241 (I# x) `geInt` (I# y) = x >=# y
242 (I# x) `ltInt` (I# y) = x <# y
243 (I# x) `leInt` (I# y) = x <=# y
244
245 compareInt :: Int -> Int -> Ordering
246 (I# x#) `compareInt` (I# y#) = compareInt# x# y#
247
248 compareInt# :: Int# -> Int# -> Ordering
249 compareInt# x# y#
250 | x# <# y# = LT
251 | x# ==# y# = EQ
252 | True = GT
253
254 -- OK, so they're technically not part of a class...:
255
256 -- Boolean functions
257
258 -- | Boolean \"and\"
259 (&&) :: Bool -> Bool -> Bool
260 True && x = x
261 False && _ = False
262
263 -- | Boolean \"or\"
264 (||) :: Bool -> Bool -> Bool
265 True || _ = True
266 False || x = x
267
268 -- | Boolean \"not\"
269 not :: Bool -> Bool
270 not True = False
271 not False = True
272
273
274 ------------------------------------------------------------------------
275 -- Generic deriving
276 ------------------------------------------------------------------------
277
278 -- We need instances for some basic datatypes, but some of those use Int,
279 -- so we have to put the instances here
280 deriving instance Eq Arity
281 deriving instance Eq Associativity
282 deriving instance Eq Fixity
283
284 deriving instance Ord Arity
285 deriving instance Ord Associativity
286 deriving instance Ord Fixity
287
288 ------------------------------------------------------------------------
289 -- These don't really belong here, but we don't have a better place to
290 -- put them
291
292 divInt# :: Int# -> Int# -> Int#
293 x# `divInt#` y#
294 -- Be careful NOT to overflow if we do any additional arithmetic
295 -- on the arguments... the following previous version of this
296 -- code has problems with overflow:
297 -- | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y#
298 -- | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y#
299 = if (x# ># 0#) && (y# <# 0#) then ((x# -# 1#) `quotInt#` y#) -# 1#
300 else if (x# <# 0#) && (y# ># 0#) then ((x# +# 1#) `quotInt#` y#) -# 1#
301 else x# `quotInt#` y#
302
303 modInt# :: Int# -> Int# -> Int#
304 x# `modInt#` y#
305 = if (x# ># 0#) && (y# <# 0#) ||
306 (x# <# 0#) && (y# ># 0#)
307 then if r# /=# 0# then r# +# y# else 0#
308 else r#
309 where
310 !r# = x# `remInt#` y#
311