2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
7 -- Copyright : (c) The University of Glasgow 1994-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
14 -- The 'Num' class and the 'Integer' type.
16 -----------------------------------------------------------------------------
19 #if SIZEOF_HSWORD == 4
21 #define BASE 1000000000
22 #elif SIZEOF_HSWORD == 8
24 #define BASE 1000000000000000000
26 #error Please define DIGITS and BASE
27 -- DIGITS should be the largest integer such that
28 -- 10^DIGITS < 2^(SIZEOF_HSWORD * 8 - 1)
29 -- BASE should be 10^DIGITS. Note that ^ is not available yet.
33 module GHC.Num (module GHC.Num, module GHC.Integer) where
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 i = I# (toInt# i)
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@ instances for @Eq@, @Ord@}
124 %*********************************************************
127 instance Eq Integer where
131 ------------------------------------------------------------------------
132 instance Ord Integer where
137 compare = compareInteger
141 %*********************************************************
143 \subsection{The @Integer@ instances for @Show@}
145 %*********************************************************
148 instance Show Integer where
150 | p > 6 && n < 0 = '(' : integerToString n (')' : r)
151 -- Minor point: testing p first gives better code
152 -- in the not-uncommon case where the p argument
154 | otherwise = integerToString n r
155 showList = showList__ (showsPrec 0)
157 -- Divide an conquer implementation of string conversion
158 integerToString :: Integer -> String -> String
160 | n < 0 = '-' : integerToString' (-n) cs
161 | otherwise = integerToString' n cs
163 integerToString' :: Integer -> String -> String
164 integerToString' n cs
165 | n < BASE = jhead (fromInteger n) cs
166 | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
168 -- Split n into digits in base p. We first split n into digits
169 -- in base p*p and then split each of these digits into two.
170 -- Note that the first 'digit' modulo p*p may have a leading zero
171 -- in base p that we need to drop - this is what jsplith takes care of.
172 -- jsplitb the handles the remaining digits.
173 jsplitf :: Integer -> Integer -> [Integer]
176 | otherwise = jsplith p (jsplitf (p*p) n)
178 jsplith :: Integer -> [Integer] -> [Integer]
180 case n `quotRemInteger` p of
182 if q > 0 then fromInteger q : fromInteger r : jsplitb p ns
183 else fromInteger r : jsplitb p ns
185 jsplitb :: Integer -> [Integer] -> [Integer]
187 jsplitb p (n:ns) = case n `quotRemInteger` p of
191 -- Convert a number that has been split into digits in base BASE^2
192 -- this includes a last splitting step and then conversion of digits
193 -- that all fit into a machine word.
194 jprinth :: [Integer] -> String -> String
196 case n `quotRemInteger` BASE of
198 let q = fromInteger q'
200 in if q > 0 then jhead q $ jblock r $ jprintb ns cs
201 else jhead r $ jprintb ns cs
203 jprintb :: [Integer] -> String -> String
205 jprintb (n:ns) cs = case n `quotRemInteger` BASE of
207 let q = fromInteger q'
209 in jblock q $ jblock r $ jprintb ns cs
211 -- Convert an integer that fits into a machine word. Again, we have two
212 -- functions, one that drops leading zeros (jhead) and one that doesn't
214 jhead :: Int -> String -> String
216 | n < 10 = case unsafeChr (ord '0' + n) of
218 | otherwise = case unsafeChr (ord '0' + r) of
219 c@(C# _) -> jhead q (c : cs)
221 (q, r) = n `quotRemInt` 10
223 jblock = jblock' {- ' -} DIGITS
225 jblock' :: Int -> Int -> String -> String
227 | d == 1 = case unsafeChr (ord '0' + n) of
229 | otherwise = case unsafeChr (ord '0' + r) of
230 c@(C# _) -> jblock' (d - 1) q (c : cs)
232 (q, r) = n `quotRemInt` 10
236 %*********************************************************
238 \subsection{The @Integer@ instances for @Num@}
240 %*********************************************************
243 instance Num Integer where
247 negate = negateInteger
251 signum = signumInteger
255 %*********************************************************
257 \subsection{The @Integer@ instance for @Enum@}
259 %*********************************************************
262 instance Enum Integer where
265 toEnum (I# n) = smallInteger n
266 fromEnum n = I# (toInt# n)
268 {-# INLINE enumFrom #-}
269 {-# INLINE enumFromThen #-}
270 {-# INLINE enumFromTo #-}
271 {-# INLINE enumFromThenTo #-}
272 enumFrom x = enumDeltaInteger x 1
273 enumFromThen x y = enumDeltaInteger x (y-x)
274 enumFromTo x lim = enumDeltaToInteger x 1 lim
275 enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
278 "enumDeltaInteger" [~1] forall x y. enumDeltaInteger x y = build (\c _ -> enumDeltaIntegerFB c x y)
279 "efdtInteger" [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
280 "enumDeltaInteger" [1] enumDeltaIntegerFB (:) = enumDeltaInteger
281 "enumDeltaToInteger" [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
284 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
285 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
287 enumDeltaInteger :: Integer -> Integer -> [Integer]
288 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
289 -- strict accumulator, so
290 -- head (drop 1000000 [1 .. ]
293 enumDeltaToIntegerFB c n x delta lim
294 | delta >= 0 = up_fb c n x delta lim
295 | otherwise = dn_fb c n x delta lim
297 enumDeltaToInteger x delta lim
298 | delta >= 0 = up_list x delta lim
299 | otherwise = dn_list x delta lim
301 up_fb c n x delta lim = go (x::Integer)
304 | otherwise = x `c` go (x+delta)
305 dn_fb c n x delta lim = go (x::Integer)
308 | otherwise = x `c` go (x+delta)
310 up_list x delta lim = go (x::Integer)
313 | otherwise = x : go (x+delta)
314 dn_list x delta lim = go (x::Integer)
317 | otherwise = x : go (x+delta)