2 {-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples #-}
3 -- We believe we could deorphan this module, by moving lots of things
4 -- around, but we haven't got there yet:
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 {-# OPTIONS_HADDOCK hide #-}
7 -----------------------------------------------------------------------------
10 -- Copyright : (c) The University of Glasgow 1994-2002
11 -- License : see libraries/base/LICENSE
13 -- Maintainer : cvs-ghc@haskell.org
14 -- Stability : internal
15 -- Portability : non-portable (GHC Extensions)
17 -- The 'Num' class and the 'Integer' type.
19 -----------------------------------------------------------------------------
22 #if SIZEOF_HSWORD == 4
24 #define BASE 1000000000
25 #elif SIZEOF_HSWORD == 8
27 #define BASE 1000000000000000000
29 #error Please define DIGITS and BASE
30 -- DIGITS should be the largest integer such that
31 -- 10^DIGITS < 2^(SIZEOF_HSWORD * 8 - 1)
32 -- BASE should be 10^DIGITS. Note that ^ is not available yet.
36 module GHC.Num (module GHC.Num, module GHC.Integer) where
46 default () -- Double isn't available yet,
47 -- and we shouldn't be using defaults anyway
50 %*********************************************************
52 \subsection{Standard numeric class}
54 %*********************************************************
57 -- | Basic numeric class.
59 -- Minimal complete definition: all except 'negate' or @(-)@
60 class (Eq a, Show a) => Num a where
61 (+), (-), (*) :: a -> a -> a
66 -- | Sign of a number.
67 -- The functions 'abs' and 'signum' should satisfy the law:
69 -- > abs x * signum x == x
71 -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
74 -- | Conversion from an 'Integer'.
75 -- An integer literal represents the application of the function
76 -- 'fromInteger' to the appropriate value of type 'Integer',
77 -- so such literals have type @('Num' a) => a@.
78 fromInteger :: Integer -> a
85 -- | the same as @'flip' ('-')@.
87 -- Because @-@ is treated specially in the Haskell grammar,
88 -- @(-@ /e/@)@ is not a section, but an application of prefix negation.
89 -- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.
90 {-# INLINE subtract #-}
91 subtract :: (Num a) => a -> a -> a
96 %*********************************************************
98 \subsection{Instances for @Int@}
100 %*********************************************************
103 instance Num Int where
108 abs n = if n `geInt` 0 then n else negateInt n
110 signum n | n `ltInt` 0 = negateInt 1
114 {-# INLINE fromInteger #-} -- Just to be sure!
115 fromInteger i = I# (toInt# i)
117 quotRemInt :: Int -> Int -> (Int, Int)
118 quotRemInt a@(I# _) b@(I# _) = (a `quotInt` b, a `remInt` b)
119 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
121 divModInt :: Int -> Int -> (Int, Int)
122 divModInt x@(I# _) y@(I# _) = (x `divInt` y, x `modInt` y)
123 -- Stricter. Sorry if you don't like it. (WDP 94/10)
126 %*********************************************************
128 \subsection{The @Integer@ instances for @Show@}
130 %*********************************************************
133 instance Show Integer where
135 | p > 6 && n < 0 = '(' : integerToString n (')' : r)
136 -- Minor point: testing p first gives better code
137 -- in the not-uncommon case where the p argument
139 | otherwise = integerToString n r
140 showList = showList__ (showsPrec 0)
142 -- Divide an conquer implementation of string conversion
143 integerToString :: Integer -> String -> String
144 integerToString n0 cs0
145 | n0 < 0 = '-' : integerToString' (- n0) cs0
146 | otherwise = integerToString' n0 cs0
148 integerToString' :: Integer -> String -> String
149 integerToString' n cs
150 | n < BASE = jhead (fromInteger n) cs
151 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
153 -- Split n into digits in base p. We first split n into digits
154 -- in base p*p and then split each of these digits into two.
155 -- Note that the first 'digit' modulo p*p may have a leading zero
156 -- in base p that we need to drop - this is what jsplith takes care of.
157 -- jsplitb the handles the remaining digits.
158 jsplitf :: Integer -> Integer -> [Integer]
161 | otherwise = jsplith p (jsplitf (p*p) n)
163 jsplith :: Integer -> [Integer] -> [Integer]
165 case n `quotRemInteger` p of
167 if q > 0 then q : r : jsplitb p ns
168 else r : jsplitb p ns
169 jsplith _ [] = error "jsplith: []"
171 jsplitb :: Integer -> [Integer] -> [Integer]
173 jsplitb p (n:ns) = case n `quotRemInteger` p of
177 -- Convert a number that has been split into digits in base BASE^2
178 -- this includes a last splitting step and then conversion of digits
179 -- that all fit into a machine word.
180 jprinth :: [Integer] -> String -> String
182 case n `quotRemInteger` BASE of
184 let q = fromInteger q'
186 in if q > 0 then jhead q $ jblock r $ jprintb ns cs
187 else jhead r $ jprintb ns cs
188 jprinth [] _ = error "jprinth []"
190 jprintb :: [Integer] -> String -> String
192 jprintb (n:ns) cs = case n `quotRemInteger` BASE of
194 let q = fromInteger q'
196 in jblock q $ jblock r $ jprintb ns cs
198 -- Convert an integer that fits into a machine word. Again, we have two
199 -- functions, one that drops leading zeros (jhead) and one that doesn't
201 jhead :: Int -> String -> String
203 | n < 10 = case unsafeChr (ord '0' + n) of
205 | otherwise = case unsafeChr (ord '0' + r) of
206 c@(C# _) -> jhead q (c : cs)
208 (q, r) = n `quotRemInt` 10
210 jblock = jblock' {- ' -} DIGITS
212 jblock' :: Int -> Int -> String -> String
214 | d == 1 = case unsafeChr (ord '0' + n) of
216 | otherwise = case unsafeChr (ord '0' + r) of
217 c@(C# _) -> jblock' (d - 1) q (c : cs)
219 (q, r) = n `quotRemInt` 10
223 %*********************************************************
225 \subsection{The @Integer@ instances for @Num@}
227 %*********************************************************
230 instance Num Integer where
234 negate = negateInteger
238 signum = signumInteger
242 %*********************************************************
244 \subsection{The @Integer@ instance for @Enum@}
246 %*********************************************************
249 instance Enum Integer where
252 toEnum (I# n) = smallInteger n
253 fromEnum n = I# (toInt# n)
255 {-# INLINE enumFrom #-}
256 {-# INLINE enumFromThen #-}
257 {-# INLINE enumFromTo #-}
258 {-# INLINE enumFromThenTo #-}
259 enumFrom x = enumDeltaInteger x 1
260 enumFromThen x y = enumDeltaInteger x (y-x)
261 enumFromTo x lim = enumDeltaToInteger x 1 lim
262 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
265 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
266 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
267 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
268 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
271 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
272 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
274 enumDeltaInteger :: Integer -> Integer -> [Integer]
275 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
276 -- strict accumulator, so
277 -- head (drop 1000000 [1 .. ]
280 {-# NOINLINE [0] enumDeltaToIntegerFB #-}
281 -- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
282 enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
283 -> Integer -> Integer -> Integer -> a
284 enumDeltaToIntegerFB c n x delta lim
285 | delta >= 0 = up_fb c n x delta lim
286 | otherwise = dn_fb c n x delta lim
288 enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
289 enumDeltaToInteger x delta lim
290 | delta >= 0 = up_list x delta lim
291 | otherwise = dn_list x delta lim
293 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
294 up_fb c n x0 delta lim = go (x0 :: Integer)
297 | otherwise = x `c` go (x+delta)
298 dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
299 dn_fb c n x0 delta lim = go (x0 :: Integer)
302 | otherwise = x `c` go (x+delta)
304 up_list :: Integer -> Integer -> Integer -> [Integer]
305 up_list x0 delta lim = go (x0 :: Integer)
308 | otherwise = x : go (x+delta)
309 dn_list :: Integer -> Integer -> Integer -> [Integer]
310 dn_list x0 delta lim = go (x0 :: Integer)
313 | otherwise = x : go (x+delta)