2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
8 -- Copyright : (c) The University of Glasgow 1994-2002
9 -- License : see libraries/base/LICENSE
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
15 -- The 'Num' class and the 'Integer' type.
17 -----------------------------------------------------------------------------
20 #if SIZEOF_HSWORD == 4
22 #define BASE 1000000000
23 #elif SIZEOF_HSWORD == 8
25 #define BASE 1000000000000000000
27 #error Please define DIGITS and BASE
28 -- DIGITS should be the largest integer such that
29 -- 10^DIGITS < 2^(SIZEOF_HSWORD * 8 - 1)
30 -- BASE should be 10^DIGITS. Note that ^ is not available yet.
34 module GHC.Num (module GHC.Num, module GHC.Integer) where
44 default () -- Double isn't available yet,
45 -- and we shouldn't be using defaults anyway
48 %*********************************************************
50 \subsection{Standard numeric class}
52 %*********************************************************
55 -- | Basic numeric class.
57 -- Minimal complete definition: all except 'negate' or @(-)@
58 class (Eq a, Show a) => Num a where
59 (+), (-), (*) :: a -> a -> a
64 -- | Sign of a number.
65 -- The functions 'abs' and 'signum' should satisfy the law:
67 -- > abs x * signum x == x
69 -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
72 -- | Conversion from an 'Integer'.
73 -- An integer literal represents the application of the function
74 -- 'fromInteger' to the appropriate value of type 'Integer',
75 -- so such literals have type @('Num' a) => a@.
76 fromInteger :: Integer -> a
81 -- | the same as @'flip' ('-')@.
83 -- Because @-@ is treated specially in the Haskell grammar,
84 -- @(-@ /e/@)@ is not a section, but an application of prefix negation.
85 -- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.
86 {-# INLINE subtract #-}
87 subtract :: (Num a) => a -> a -> a
92 %*********************************************************
94 \subsection{Instances for @Int@}
96 %*********************************************************
99 instance Num Int where
104 abs n = if n `geInt` 0 then n else negateInt n
106 signum n | n `ltInt` 0 = negateInt 1
110 fromInteger i = I# (toInt# i)
112 quotRemInt :: Int -> Int -> (Int, Int)
113 quotRemInt a@(I# _) b@(I# _) = (a `quotInt` b, a `remInt` b)
114 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
116 divModInt :: Int -> Int -> (Int, Int)
117 divModInt x@(I# _) y@(I# _) = (x `divInt` y, x `modInt` y)
118 -- Stricter. Sorry if you don't like it. (WDP 94/10)
121 %*********************************************************
123 \subsection{The @Integer@ instances for @Eq@, @Ord@}
125 %*********************************************************
128 instance Eq Integer where
132 ------------------------------------------------------------------------
133 instance Ord Integer where
138 compare = compareInteger
142 %*********************************************************
144 \subsection{The @Integer@ instances for @Show@}
146 %*********************************************************
149 instance Show Integer where
151 | p > 6 && n < 0 = '(' : integerToString n (')' : r)
152 -- Minor point: testing p first gives better code
153 -- in the not-uncommon case where the p argument
155 | otherwise = integerToString n r
156 showList = showList__ (showsPrec 0)
158 -- Divide an conquer implementation of string conversion
159 integerToString :: Integer -> String -> String
160 integerToString n0 cs0
161 | n0 < 0 = '-' : integerToString' (- n0) cs0
162 | otherwise = integerToString' n0 cs0
164 integerToString' :: Integer -> String -> String
165 integerToString' n cs
166 | n < BASE = jhead (fromInteger n) cs
167 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
169 -- Split n into digits in base p. We first split n into digits
170 -- in base p*p and then split each of these digits into two.
171 -- Note that the first 'digit' modulo p*p may have a leading zero
172 -- in base p that we need to drop - this is what jsplith takes care of.
173 -- jsplitb the handles the remaining digits.
174 jsplitf :: Integer -> Integer -> [Integer]
177 | otherwise = jsplith p (jsplitf (p*p) n)
179 jsplith :: Integer -> [Integer] -> [Integer]
181 case n `quotRemInteger` p of
183 if q > 0 then q : r : jsplitb p ns
184 else r : jsplitb p ns
185 jsplith _ [] = error "jsplith: []"
187 jsplitb :: Integer -> [Integer] -> [Integer]
189 jsplitb p (n:ns) = case n `quotRemInteger` p of
193 -- Convert a number that has been split into digits in base BASE^2
194 -- this includes a last splitting step and then conversion of digits
195 -- that all fit into a machine word.
196 jprinth :: [Integer] -> String -> String
198 case n `quotRemInteger` BASE of
200 let q = fromInteger q'
202 in if q > 0 then jhead q $ jblock r $ jprintb ns cs
203 else jhead r $ jprintb ns cs
204 jprinth [] _ = error "jprinth []"
206 jprintb :: [Integer] -> String -> String
208 jprintb (n:ns) cs = case n `quotRemInteger` BASE of
210 let q = fromInteger q'
212 in jblock q $ jblock r $ jprintb ns cs
214 -- Convert an integer that fits into a machine word. Again, we have two
215 -- functions, one that drops leading zeros (jhead) and one that doesn't
217 jhead :: Int -> String -> String
219 | n < 10 = case unsafeChr (ord '0' + n) of
221 | otherwise = case unsafeChr (ord '0' + r) of
222 c@(C# _) -> jhead q (c : cs)
224 (q, r) = n `quotRemInt` 10
226 jblock = jblock' {- ' -} DIGITS
228 jblock' :: Int -> Int -> String -> String
230 | d == 1 = case unsafeChr (ord '0' + n) of
232 | otherwise = case unsafeChr (ord '0' + r) of
233 c@(C# _) -> jblock' (d - 1) q (c : cs)
235 (q, r) = n `quotRemInt` 10
239 %*********************************************************
241 \subsection{The @Integer@ instances for @Num@}
243 %*********************************************************
246 instance Num Integer where
250 negate = negateInteger
254 signum = signumInteger
258 %*********************************************************
260 \subsection{The @Integer@ instance for @Enum@}
262 %*********************************************************
265 instance Enum Integer where
268 toEnum (I# n) = smallInteger n
269 fromEnum n = I# (toInt# n)
271 {-# INLINE enumFrom #-}
272 {-# INLINE enumFromThen #-}
273 {-# INLINE enumFromTo #-}
274 {-# INLINE enumFromThenTo #-}
275 enumFrom x = enumDeltaInteger x 1
276 enumFromThen x y = enumDeltaInteger x (y-x)
277 enumFromTo x lim = enumDeltaToInteger x 1 lim
278 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
281 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
282 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
283 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
284 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
287 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
288 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
290 enumDeltaInteger :: Integer -> Integer -> [Integer]
291 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
292 -- strict accumulator, so
293 -- head (drop 1000000 [1 .. ]
296 {-# NOINLINE [0] enumDeltaToIntegerFB #-}
297 -- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
298 enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
299 -> Integer -> Integer -> Integer -> a
300 enumDeltaToIntegerFB c n x delta lim
301 | delta >= 0 = up_fb c n x delta lim
302 | otherwise = dn_fb c n x delta lim
304 enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
305 enumDeltaToInteger x delta lim
306 | delta >= 0 = up_list x delta lim
307 | otherwise = dn_list x delta lim
309 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
310 up_fb c n x0 delta lim = go (x0 :: Integer)
313 | otherwise = x `c` go (x+delta)
314 dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
315 dn_fb c n x0 delta lim = go (x0 :: Integer)
318 | otherwise = x `c` go (x+delta)
320 up_list :: Integer -> Integer -> Integer -> [Integer]
321 up_list x0 delta lim = go (x0 :: Integer)
324 | otherwise = x : go (x+delta)
325 dn_list :: Integer -> Integer -> Integer -> [Integer]
326 dn_list x0 delta lim = go (x0 :: Integer)
329 | otherwise = x : go (x+delta)