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
83 -- | the same as @'flip' ('-')@.
85 -- Because @-@ is treated specially in the Haskell grammar,
86 -- @(-@ /e/@)@ is not a section, but an application of prefix negation.
87 -- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.
88 {-# INLINE subtract #-}
89 subtract :: (Num a) => a -> a -> a
94 %*********************************************************
96 \subsection{Instances for @Int@}
98 %*********************************************************
101 instance Num Int where
106 abs n = if n `geInt` 0 then n else negateInt n
108 signum n | n `ltInt` 0 = negateInt 1
112 fromInteger i = I# (toInt# i)
114 quotRemInt :: Int -> Int -> (Int, Int)
115 quotRemInt a@(I# _) b@(I# _) = (a `quotInt` b, a `remInt` b)
116 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
118 divModInt :: Int -> Int -> (Int, Int)
119 divModInt x@(I# _) y@(I# _) = (x `divInt` y, x `modInt` y)
120 -- Stricter. Sorry if you don't like it. (WDP 94/10)
123 %*********************************************************
125 \subsection{The @Integer@ instances for @Eq@, @Ord@}
127 %*********************************************************
130 instance Eq Integer where
134 ------------------------------------------------------------------------
135 instance Ord Integer where
140 compare = compareInteger
144 %*********************************************************
146 \subsection{The @Integer@ instances for @Show@}
148 %*********************************************************
151 instance Show Integer where
153 | p > 6 && n < 0 = '(' : integerToString n (')' : r)
154 -- Minor point: testing p first gives better code
155 -- in the not-uncommon case where the p argument
157 | otherwise = integerToString n r
158 showList = showList__ (showsPrec 0)
160 -- Divide an conquer implementation of string conversion
161 integerToString :: Integer -> String -> String
162 integerToString n0 cs0
163 | n0 < 0 = '-' : integerToString' (- n0) cs0
164 | otherwise = integerToString' n0 cs0
166 integerToString' :: Integer -> String -> String
167 integerToString' n cs
168 | n < BASE = jhead (fromInteger n) cs
169 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
171 -- Split n into digits in base p. We first split n into digits
172 -- in base p*p and then split each of these digits into two.
173 -- Note that the first 'digit' modulo p*p may have a leading zero
174 -- in base p that we need to drop - this is what jsplith takes care of.
175 -- jsplitb the handles the remaining digits.
176 jsplitf :: Integer -> Integer -> [Integer]
179 | otherwise = jsplith p (jsplitf (p*p) n)
181 jsplith :: Integer -> [Integer] -> [Integer]
183 case n `quotRemInteger` p of
185 if q > 0 then q : r : jsplitb p ns
186 else r : jsplitb p ns
187 jsplith _ [] = error "jsplith: []"
189 jsplitb :: Integer -> [Integer] -> [Integer]
191 jsplitb p (n:ns) = case n `quotRemInteger` p of
195 -- Convert a number that has been split into digits in base BASE^2
196 -- this includes a last splitting step and then conversion of digits
197 -- that all fit into a machine word.
198 jprinth :: [Integer] -> String -> String
200 case n `quotRemInteger` BASE of
202 let q = fromInteger q'
204 in if q > 0 then jhead q $ jblock r $ jprintb ns cs
205 else jhead r $ jprintb ns cs
206 jprinth [] _ = error "jprinth []"
208 jprintb :: [Integer] -> String -> String
210 jprintb (n:ns) cs = case n `quotRemInteger` BASE of
212 let q = fromInteger q'
214 in jblock q $ jblock r $ jprintb ns cs
216 -- Convert an integer that fits into a machine word. Again, we have two
217 -- functions, one that drops leading zeros (jhead) and one that doesn't
219 jhead :: Int -> String -> String
221 | n < 10 = case unsafeChr (ord '0' + n) of
223 | otherwise = case unsafeChr (ord '0' + r) of
224 c@(C# _) -> jhead q (c : cs)
226 (q, r) = n `quotRemInt` 10
228 jblock = jblock' {- ' -} DIGITS
230 jblock' :: Int -> Int -> String -> String
232 | d == 1 = case unsafeChr (ord '0' + n) of
234 | otherwise = case unsafeChr (ord '0' + r) of
235 c@(C# _) -> jblock' (d - 1) q (c : cs)
237 (q, r) = n `quotRemInt` 10
241 %*********************************************************
243 \subsection{The @Integer@ instances for @Num@}
245 %*********************************************************
248 instance Num Integer where
252 negate = negateInteger
256 signum = signumInteger
260 %*********************************************************
262 \subsection{The @Integer@ instance for @Enum@}
264 %*********************************************************
267 instance Enum Integer where
270 toEnum (I# n) = smallInteger n
271 fromEnum n = I# (toInt# n)
273 {-# INLINE enumFrom #-}
274 {-# INLINE enumFromThen #-}
275 {-# INLINE enumFromTo #-}
276 {-# INLINE enumFromThenTo #-}
277 enumFrom x = enumDeltaInteger x 1
278 enumFromThen x y = enumDeltaInteger x (y-x)
279 enumFromTo x lim = enumDeltaToInteger x 1 lim
280 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
283 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
284 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
285 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
286 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
289 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
290 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
292 enumDeltaInteger :: Integer -> Integer -> [Integer]
293 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
294 -- strict accumulator, so
295 -- head (drop 1000000 [1 .. ]
298 {-# NOINLINE [0] enumDeltaToIntegerFB #-}
299 -- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
300 enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
301 -> Integer -> Integer -> Integer -> a
302 enumDeltaToIntegerFB c n x delta lim
303 | delta >= 0 = up_fb c n x delta lim
304 | otherwise = dn_fb c n x delta lim
306 enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
307 enumDeltaToInteger x delta lim
308 | delta >= 0 = up_list x delta lim
309 | otherwise = dn_list x delta lim
311 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
312 up_fb c n x0 delta lim = go (x0 :: Integer)
315 | otherwise = x `c` go (x+delta)
316 dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
317 dn_fb c n x0 delta lim = go (x0 :: Integer)
320 | otherwise = x `c` go (x+delta)
322 up_list :: Integer -> Integer -> Integer -> [Integer]
323 up_list x0 delta lim = go (x0 :: Integer)
326 | otherwise = x : go (x+delta)
327 dn_list :: Integer -> Integer -> Integer -> [Integer]
328 dn_list x0 delta lim = go (x0 :: Integer)
331 | otherwise = x : go (x+delta)