2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The 'Num' class and the 'Integer' type.
15 -----------------------------------------------------------------------------
18 #if SIZEOF_HSWORD == 4
19 #define LEFTMOST_BIT 2147483648
21 #define BASE 1000000000
22 #elif SIZEOF_HSWORD == 8
23 #define LEFTMOST_BIT 9223372036854775808
25 #define BASE 1000000000000000000
27 #error Please define LEFTMOST_BIT to be 2^(SIZEOF_HSWORD*8-1)
28 -- DIGITS should be the largest integer such that 10^DIGITS < LEFTMOST_BIT
29 -- BASE should be 10^DIGITS. Note that ^ is not available yet.
35 import {-# SOURCE #-} GHC.Err
43 default () -- Double isn't available yet,
44 -- and we shouldn't be using defaults anyway
47 %*********************************************************
49 \subsection{Standard numeric class}
51 %*********************************************************
54 -- | Basic numeric class.
56 -- Minimal complete definition: all except 'negate' or @(-)@
57 class (Eq a, Show a) => Num a where
58 (+), (-), (*) :: a -> a -> a
63 -- | Sign of a number.
64 -- The functions 'abs' and 'signum' should satisfy the law:
66 -- > abs x * signum x == x
68 -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
71 -- | Conversion from an 'Integer'.
72 -- An integer literal represents the application of the function
73 -- 'fromInteger' to the appropriate value of type 'Integer',
74 -- so such literals have type @('Num' a) => a@.
75 fromInteger :: Integer -> a
80 -- | the same as @'flip' ('-')@.
82 -- Because @-@ is treated specially in the Haskell grammar,
83 -- @(-@ /e/@)@ is not a section, but an application of prefix negation.
84 -- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.
85 {-# INLINE subtract #-}
86 subtract :: (Num a) => a -> a -> a
91 %*********************************************************
93 \subsection{Instances for @Int@}
95 %*********************************************************
98 instance Num Int where
103 abs n = if n `geInt` 0 then n else negateInt n
105 signum n | n `ltInt` 0 = negateInt 1
109 fromInteger = integer2Int
111 quotRemInt :: Int -> Int -> (Int, Int)
112 quotRemInt a@(I# _) b@(I# _) = (a `quotInt` b, a `remInt` b)
113 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
115 divModInt :: Int -> Int -> (Int, Int)
116 divModInt x@(I# _) y@(I# _) = (x `divInt` y, x `modInt` y)
117 -- Stricter. Sorry if you don't like it. (WDP 94/10)
120 %*********************************************************
122 \subsection{The @Integer@ type}
124 %*********************************************************
127 -- | Arbitrary-precision integers.
129 = S# Int# -- small integers
131 | J# Int# ByteArray# -- large integers
133 | J# Void BigInteger -- .NET big ints
135 foreign type dotnet "BigInteger" BigInteger
139 Convenient boxed Integer PrimOps.
142 zeroInteger :: Integer
145 int2Integer :: Int -> Integer
146 {-# INLINE int2Integer #-}
147 int2Integer (I# i) = S# i
149 integer2Int :: Integer -> Int
150 integer2Int (S# i) = I# i
151 integer2Int (J# s d) = case (integer2Int# s d) of { n# -> I# n# }
153 toBig (S# i) = case int2Integer# i of { (# s, d #) -> J# s d }
158 %*********************************************************
160 \subsection{Dividing @Integers@}
162 %*********************************************************
165 quotRemInteger :: Integer -> Integer -> (Integer, Integer)
166 quotRemInteger a@(S# (-LEFTMOST_BIT#)) b = quotRemInteger (toBig a) b
167 quotRemInteger (S# i) (S# j)
168 = case quotRemInt (I# i) (I# j) of ( I# i, I# j ) -> ( S# i, S# j )
169 quotRemInteger i1@(J# _ _) i2@(S# _) = quotRemInteger i1 (toBig i2)
170 quotRemInteger i1@(S# _) i2@(J# _ _) = quotRemInteger (toBig i1) i2
171 quotRemInteger (J# s1 d1) (J# s2 d2)
172 = case (quotRemInteger# s1 d1 s2 d2) of
174 -> (J# s3 d3, J# s4 d4)
176 divModInteger a@(S# (-LEFTMOST_BIT#)) b = divModInteger (toBig a) b
177 divModInteger (S# i) (S# j)
178 = case divModInt (I# i) (I# j) of ( I# i, I# j ) -> ( S# i, S# j)
179 divModInteger i1@(J# _ _) i2@(S# _) = divModInteger i1 (toBig i2)
180 divModInteger i1@(S# _) i2@(J# _ _) = divModInteger (toBig i1) i2
181 divModInteger (J# s1 d1) (J# s2 d2)
182 = case (divModInteger# s1 d1 s2 d2) of
184 -> (J# s3 d3, J# s4 d4)
186 remInteger :: Integer -> Integer -> Integer
188 | ib == 0 = error "Prelude.Integral.rem{Integer}: divide by 0"
189 remInteger a@(S# (-LEFTMOST_BIT#)) b = remInteger (toBig a) b
190 remInteger (S# a) (S# b) = S# (remInt# a b)
191 {- Special case doesn't work, because a 1-element J# has the range
192 -(2^32-1) -- 2^32-1, whereas S# has the range -2^31 -- (2^31-1)
193 remInteger ia@(S# a) (J# sb b)
194 | sb ==# 1# = S# (remInt# a (word2Int# (integer2Word# sb b)))
195 | sb ==# -1# = S# (remInt# a (0# -# (word2Int# (integer2Word# sb b))))
197 | otherwise = S# (0# -# a)
199 remInteger ia@(S# _) ib@(J# _ _) = remInteger (toBig ia) ib
200 remInteger (J# sa a) (S# b)
201 = case int2Integer# b of { (# sb, b #) ->
202 case remInteger# sa a sb b of { (# sr, r #) ->
203 S# (integer2Int# sr r) }}
204 remInteger (J# sa a) (J# sb b)
205 = case remInteger# sa a sb b of (# sr, r #) -> J# sr r
207 quotInteger :: Integer -> Integer -> Integer
209 | ib == 0 = error "Prelude.Integral.quot{Integer}: divide by 0"
210 quotInteger a@(S# (-LEFTMOST_BIT#)) b = quotInteger (toBig a) b
211 quotInteger (S# a) (S# b) = S# (quotInt# a b)
212 {- Special case disabled, see remInteger above
213 quotInteger (S# a) (J# sb b)
214 | sb ==# 1# = S# (quotInt# a (word2Int# (integer2Word# sb b)))
215 | sb ==# -1# = S# (quotInt# a (0# -# (word2Int# (integer2Word# sb b))))
216 | otherwise = zeroInteger
218 quotInteger ia@(S# _) ib@(J# _ _) = quotInteger (toBig ia) ib
219 quotInteger (J# sa a) (S# b)
220 = case int2Integer# b of { (# sb, b #) ->
221 case quotInteger# sa a sb b of (# sq, q #) -> J# sq q }
222 quotInteger (J# sa a) (J# sb b)
223 = case quotInteger# sa a sb b of (# sg, g #) -> J# sg g
229 gcdInteger :: Integer -> Integer -> Integer
230 -- SUP: Do we really need the first two cases?
231 gcdInteger a@(S# (-LEFTMOST_BIT#)) b = gcdInteger (toBig a) b
232 gcdInteger a b@(S# (-LEFTMOST_BIT#)) = gcdInteger a (toBig b)
233 gcdInteger (S# a) (S# b) = case gcdInt (I# a) (I# b) of { I# c -> S# c }
234 gcdInteger ia@(S# 0#) ib@(J# 0# _) = error "GHC.Num.gcdInteger: gcd 0 0 is undefined"
235 gcdInteger ia@(S# a) ib@(J# sb b)
238 | otherwise = S# (gcdIntegerInt# absSb b absA)
239 where absA = if a <# 0# then negateInt# a else a
240 absSb = if sb <# 0# then negateInt# sb else sb
241 gcdInteger ia@(J# _ _) ib@(S# _) = gcdInteger ib ia
242 gcdInteger (J# 0# _) (J# 0# _) = error "GHC.Num.gcdInteger: gcd 0 0 is undefined"
243 gcdInteger (J# sa a) (J# sb b)
244 = case gcdInteger# sa a sb b of (# sg, g #) -> J# sg g
246 lcmInteger :: Integer -> Integer -> Integer
252 = (divExact aa (gcdInteger aa ab)) * ab
256 divExact :: Integer -> Integer -> Integer
257 divExact a@(S# (-LEFTMOST_BIT#)) b = divExact (toBig a) b
258 divExact (S# a) (S# b) = S# (quotInt# a b)
259 divExact (S# a) (J# sb b)
260 = S# (quotInt# a (integer2Int# sb b))
261 divExact (J# sa a) (S# b)
262 = case int2Integer# b of
263 (# sb, b #) -> case divExactInteger# sa a sb b of (# sd, d #) -> J# sd d
264 divExact (J# sa a) (J# sb b)
265 = case divExactInteger# sa a sb b of (# sd, d #) -> J# sd d
269 %*********************************************************
271 \subsection{The @Integer@ instances for @Eq@, @Ord@}
273 %*********************************************************
276 instance Eq Integer where
277 (S# i) == (S# j) = i ==# j
278 (S# i) == (J# s d) = cmpIntegerInt# s d i ==# 0#
279 (J# s d) == (S# i) = cmpIntegerInt# s d i ==# 0#
280 (J# s1 d1) == (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
282 (S# i) /= (S# j) = i /=# j
283 (S# i) /= (J# s d) = cmpIntegerInt# s d i /=# 0#
284 (J# s d) /= (S# i) = cmpIntegerInt# s d i /=# 0#
285 (J# s1 d1) /= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) /=# 0#
287 ------------------------------------------------------------------------
288 instance Ord Integer where
289 (S# i) <= (S# j) = i <=# j
290 (J# s d) <= (S# i) = cmpIntegerInt# s d i <=# 0#
291 (S# i) <= (J# s d) = cmpIntegerInt# s d i >=# 0#
292 (J# s1 d1) <= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) <=# 0#
294 (S# i) > (S# j) = i ># j
295 (J# s d) > (S# i) = cmpIntegerInt# s d i ># 0#
296 (S# i) > (J# s d) = cmpIntegerInt# s d i <# 0#
297 (J# s1 d1) > (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ># 0#
299 (S# i) < (S# j) = i <# j
300 (J# s d) < (S# i) = cmpIntegerInt# s d i <# 0#
301 (S# i) < (J# s d) = cmpIntegerInt# s d i ># 0#
302 (J# s1 d1) < (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) <# 0#
304 (S# i) >= (S# j) = i >=# j
305 (J# s d) >= (S# i) = cmpIntegerInt# s d i >=# 0#
306 (S# i) >= (J# s d) = cmpIntegerInt# s d i <=# 0#
307 (J# s1 d1) >= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) >=# 0#
309 compare (S# i) (S# j)
313 compare (J# s d) (S# i)
314 = case cmpIntegerInt# s d i of { res# ->
315 if res# <# 0# then LT else
316 if res# ># 0# then GT else EQ
318 compare (S# i) (J# s d)
319 = case cmpIntegerInt# s d i of { res# ->
320 if res# ># 0# then LT else
321 if res# <# 0# then GT else EQ
323 compare (J# s1 d1) (J# s2 d2)
324 = case cmpInteger# s1 d1 s2 d2 of { res# ->
325 if res# <# 0# then LT else
326 if res# ># 0# then GT else EQ
331 %*********************************************************
333 \subsection{The @Integer@ instances for @Num@}
335 %*********************************************************
338 instance Num Integer where
342 negate = negateInteger
345 -- ORIG: abs n = if n >= 0 then n else -n
346 abs (S# (-LEFTMOST_BIT#)) = LEFTMOST_BIT
347 abs (S# i) = case abs (I# i) of I# j -> S# j
348 abs n@(J# s d) = if (s >=# 0#) then n else J# (negateInt# s) d
350 signum (S# i) = case signum (I# i) of I# j -> S# j
353 cmp = cmpIntegerInt# s d 0#
355 if cmp ># 0# then S# 1#
356 else if cmp ==# 0# then S# 0#
357 else S# (negateInt# 1#)
359 plusInteger i1@(S# i) i2@(S# j) = case addIntC# i j of { (# r, c #) ->
360 if c ==# 0# then S# r
361 else toBig i1 + toBig i2 }
362 plusInteger i1@(J# _ _) i2@(S# _) = i1 + toBig i2
363 plusInteger i1@(S# _) i2@(J# _ _) = toBig i1 + i2
364 plusInteger (J# s1 d1) (J# s2 d2) = case plusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
366 minusInteger i1@(S# i) i2@(S# j) = case subIntC# i j of { (# r, c #) ->
367 if c ==# 0# then S# r
368 else toBig i1 - toBig i2 }
369 minusInteger i1@(J# _ _) i2@(S# _) = i1 - toBig i2
370 minusInteger i1@(S# _) i2@(J# _ _) = toBig i1 - i2
371 minusInteger (J# s1 d1) (J# s2 d2) = case minusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
373 timesInteger i1@(S# i) i2@(S# j) = if mulIntMayOflo# i j ==# 0#
375 else toBig i1 * toBig i2
376 timesInteger i1@(J# _ _) i2@(S# _) = i1 * toBig i2
377 timesInteger i1@(S# _) i2@(J# _ _) = toBig i1 * i2
378 timesInteger (J# s1 d1) (J# s2 d2) = case timesInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
380 negateInteger (S# (-LEFTMOST_BIT#)) = LEFTMOST_BIT
381 negateInteger (S# i) = S# (negateInt# i)
382 negateInteger (J# s d) = J# (negateInt# s) d
386 %*********************************************************
388 \subsection{The @Integer@ instance for @Enum@}
390 %*********************************************************
393 instance Enum Integer where
396 toEnum n = int2Integer n
397 fromEnum n = integer2Int n
399 {-# INLINE enumFrom #-}
400 {-# INLINE enumFromThen #-}
401 {-# INLINE enumFromTo #-}
402 {-# INLINE enumFromThenTo #-}
403 enumFrom x = enumDeltaInteger x 1
404 enumFromThen x y = enumDeltaInteger x (y-x)
405 enumFromTo x lim = enumDeltaToInteger x 1 lim
406 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
409 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
410 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
411 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
412 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
415 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
416 enumDeltaIntegerFB c x d = x `c` enumDeltaIntegerFB c (x+d) d
418 enumDeltaInteger :: Integer -> Integer -> [Integer]
419 enumDeltaInteger x d = x : enumDeltaInteger (x+d) d
421 enumDeltaToIntegerFB c n x delta lim
422 | delta >= 0 = up_fb c n x delta lim
423 | otherwise = dn_fb c n x delta lim
425 enumDeltaToInteger x delta lim
426 | delta >= 0 = up_list x delta lim
427 | otherwise = dn_list x delta lim
429 up_fb c n x delta lim = go (x::Integer)
432 | otherwise = x `c` go (x+delta)
433 dn_fb c n x delta lim = go (x::Integer)
436 | otherwise = x `c` go (x+delta)
438 up_list x delta lim = go (x::Integer)
441 | otherwise = x : go (x+delta)
442 dn_list x delta lim = go (x::Integer)
445 | otherwise = x : go (x+delta)
450 %*********************************************************
452 \subsection{The @Integer@ instances for @Show@}
454 %*********************************************************
457 instance Show Integer where
459 | p > 6 && n < 0 = '(' : jtos n (')' : r)
460 -- Minor point: testing p first gives better code
461 -- in the not-uncommon case where the p argument
463 | otherwise = jtos n r
464 showList = showList__ (showsPrec 0)
466 -- Divide an conquer implementation of string conversion
467 jtos :: Integer -> String -> String
469 | n < 0 = '-' : jtos' (-n) cs
470 | otherwise = jtos' n cs
472 jtos' :: Integer -> String -> String
474 | n < BASE = jhead (fromInteger n) cs
475 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
477 -- Split n into digits in base p. We first split n into digits
478 -- in base p*p and then split each of these digits into two.
479 -- Note that the first 'digit' modulo p*p may have a leading zero
480 -- in base p that we need to drop - this is what jsplith takes care of.
481 -- jsplitb the handles the remaining digits.
482 jsplitf :: Integer -> Integer -> [Integer]
485 | otherwise = jsplith p (jsplitf (p*p) n)
487 jsplith :: Integer -> [Integer] -> [Integer]
489 if q > 0 then fromInteger q : fromInteger r : jsplitb p ns
490 else fromInteger r : jsplitb p ns
492 (q, r) = n `quotRemInteger` p
494 jsplitb :: Integer -> [Integer] -> [Integer]
496 jsplitb p (n:ns) = q : r : jsplitb p ns
498 (q, r) = n `quotRemInteger` p
500 -- Convert a number that has been split into digits in base BASE^2
501 -- this includes a last splitting step and then conversion of digits
502 -- that all fit into a machine word.
503 jprinth :: [Integer] -> String -> String
505 if q > 0 then jhead q $ jblock r $ jprintb ns cs
506 else jhead r $ jprintb ns cs
508 (q', r') = n `quotRemInteger` BASE
512 jprintb :: [Integer] -> String -> String
514 jprintb (n:ns) cs = jblock q $ jblock r $ jprintb ns cs
516 (q', r') = n `quotRemInteger` BASE
520 -- Convert an integer that fits into a machine word. Again, we have two
521 -- functions, one that drops leading zeros (jhead) and one that doesn't
523 jhead :: Int -> String -> String
525 | n < 10 = case unsafeChr (ord '0' + n) of
527 | otherwise = case unsafeChr (ord '0' + r) of
528 c@(C# _) -> jhead q (c : cs)
530 (q, r) = n `quotRemInt` 10
532 jblock = jblock' {- ' -} DIGITS
534 jblock' :: Int -> Int -> String -> String
536 | d == 1 = case unsafeChr (ord '0' + n) of
538 | otherwise = case unsafeChr (ord '0' + r) of
539 c@(C# _) -> jblock' (d - 1) q (c : cs)
541 (q, r) = n `quotRemInt` 10