2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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 fromInteger i = I# (toInt# i)
116 quotRemInt :: Int -> Int -> (Int, Int)
117 quotRemInt a@(I# _) b@(I# _) = (a `quotInt` b, a `remInt` b)
118 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
120 divModInt :: Int -> Int -> (Int, Int)
121 divModInt x@(I# _) y@(I# _) = (x `divInt` y, x `modInt` y)
122 -- Stricter. Sorry if you don't like it. (WDP 94/10)
125 %*********************************************************
127 \subsection{The @Integer@ instances for @Show@}
129 %*********************************************************
132 instance Show Integer where
134 | p > 6 && n < 0 = '(' : integerToString n (')' : r)
135 -- Minor point: testing p first gives better code
136 -- in the not-uncommon case where the p argument
138 | otherwise = integerToString n r
139 showList = showList__ (showsPrec 0)
141 -- Divide an conquer implementation of string conversion
142 integerToString :: Integer -> String -> String
143 integerToString n0 cs0
144 | n0 < 0 = '-' : integerToString' (- n0) cs0
145 | otherwise = integerToString' n0 cs0
147 integerToString' :: Integer -> String -> String
148 integerToString' n cs
149 | n < BASE = jhead (fromInteger n) cs
150 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
152 -- Split n into digits in base p. We first split n into digits
153 -- in base p*p and then split each of these digits into two.
154 -- Note that the first 'digit' modulo p*p may have a leading zero
155 -- in base p that we need to drop - this is what jsplith takes care of.
156 -- jsplitb the handles the remaining digits.
157 jsplitf :: Integer -> Integer -> [Integer]
160 | otherwise = jsplith p (jsplitf (p*p) n)
162 jsplith :: Integer -> [Integer] -> [Integer]
164 case n `quotRemInteger` p of
166 if q > 0 then q : r : jsplitb p ns
167 else r : jsplitb p ns
168 jsplith _ [] = error "jsplith: []"
170 jsplitb :: Integer -> [Integer] -> [Integer]
172 jsplitb p (n:ns) = case n `quotRemInteger` p of
176 -- Convert a number that has been split into digits in base BASE^2
177 -- this includes a last splitting step and then conversion of digits
178 -- that all fit into a machine word.
179 jprinth :: [Integer] -> String -> String
181 case n `quotRemInteger` BASE of
183 let q = fromInteger q'
185 in if q > 0 then jhead q $ jblock r $ jprintb ns cs
186 else jhead r $ jprintb ns cs
187 jprinth [] _ = error "jprinth []"
189 jprintb :: [Integer] -> String -> String
191 jprintb (n:ns) cs = case n `quotRemInteger` BASE of
193 let q = fromInteger q'
195 in jblock q $ jblock r $ jprintb ns cs
197 -- Convert an integer that fits into a machine word. Again, we have two
198 -- functions, one that drops leading zeros (jhead) and one that doesn't
200 jhead :: Int -> String -> String
202 | n < 10 = case unsafeChr (ord '0' + n) of
204 | otherwise = case unsafeChr (ord '0' + r) of
205 c@(C# _) -> jhead q (c : cs)
207 (q, r) = n `quotRemInt` 10
209 jblock = jblock' {- ' -} DIGITS
211 jblock' :: Int -> Int -> String -> String
213 | d == 1 = case unsafeChr (ord '0' + n) of
215 | otherwise = case unsafeChr (ord '0' + r) of
216 c@(C# _) -> jblock' (d - 1) q (c : cs)
218 (q, r) = n `quotRemInt` 10
222 %*********************************************************
224 \subsection{The @Integer@ instances for @Num@}
226 %*********************************************************
229 instance Num Integer where
233 negate = negateInteger
237 signum = signumInteger
241 %*********************************************************
243 \subsection{The @Integer@ instance for @Enum@}
245 %*********************************************************
248 instance Enum Integer where
251 toEnum (I# n) = smallInteger n
252 fromEnum n = I# (toInt# n)
254 {-# INLINE enumFrom #-}
255 {-# INLINE enumFromThen #-}
256 {-# INLINE enumFromTo #-}
257 {-# INLINE enumFromThenTo #-}
258 enumFrom x = enumDeltaInteger x 1
259 enumFromThen x y = enumDeltaInteger x (y-x)
260 enumFromTo x lim = enumDeltaToInteger x 1 lim
261 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
264 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
265 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
266 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
267 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
270 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
271 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
273 enumDeltaInteger :: Integer -> Integer -> [Integer]
274 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
275 -- strict accumulator, so
276 -- head (drop 1000000 [1 .. ]
279 {-# NOINLINE [0] enumDeltaToIntegerFB #-}
280 -- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
281 enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
282 -> Integer -> Integer -> Integer -> a
283 enumDeltaToIntegerFB c n x delta lim
284 | delta >= 0 = up_fb c n x delta lim
285 | otherwise = dn_fb c n x delta lim
287 enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
288 enumDeltaToInteger x delta lim
289 | delta >= 0 = up_list x delta lim
290 | otherwise = dn_list x delta lim
292 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
293 up_fb c n x0 delta lim = go (x0 :: Integer)
296 | otherwise = x `c` go (x+delta)
297 dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
298 dn_fb c n x0 delta lim = go (x0 :: Integer)
301 | otherwise = x `c` go (x+delta)
303 up_list :: Integer -> Integer -> Integer -> [Integer]
304 up_list x0 delta lim = go (x0 :: Integer)
307 | otherwise = x : go (x+delta)
308 dn_list :: Integer -> Integer -> Integer -> [Integer]
309 dn_list x0 delta lim = go (x0 :: Integer)
312 | otherwise = x : go (x+delta)