add GHC.HetMet.{hetmet_kappa,hetmet_kappa_app}
[ghc-base.git] / GHC / Num.lhs
1 \begin{code}
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 -----------------------------------------------------------------------------
8 -- |
9 -- Module      :  GHC.Num
10 -- Copyright   :  (c) The University of Glasgow 1994-2002
11 -- License     :  see libraries/base/LICENSE
12 --
13 -- Maintainer  :  cvs-ghc@haskell.org
14 -- Stability   :  internal
15 -- Portability :  non-portable (GHC Extensions)
16 --
17 -- The 'Num' class and the 'Integer' type.
18 --
19 -----------------------------------------------------------------------------
20
21 #include "MachDeps.h"
22 #if SIZEOF_HSWORD == 4
23 #define DIGITS       9
24 #define BASE         1000000000
25 #elif SIZEOF_HSWORD == 8
26 #define DIGITS       18
27 #define BASE         1000000000000000000
28 #else
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.
33 #endif
34
35 -- #hide
36 module GHC.Num (module GHC.Num, module GHC.Integer) where
37
38 import GHC.Base
39 import GHC.Enum
40 import GHC.Show
41 import GHC.Integer
42
43 infixl 7  *
44 infixl 6  +, -
45
46 default ()              -- Double isn't available yet,
47                         -- and we shouldn't be using defaults anyway
48 \end{code}
49
50 %*********************************************************
51 %*                                                      *
52 \subsection{Standard numeric class}
53 %*                                                      *
54 %*********************************************************
55
56 \begin{code}
57 -- | Basic numeric class.
58 --
59 -- Minimal complete definition: all except 'negate' or @(-)@
60 class  (Eq a, Show a) => Num a  where
61     (+), (-), (*)       :: a -> a -> a
62     -- | Unary negation.
63     negate              :: a -> a
64     -- | Absolute value.
65     abs                 :: a -> a
66     -- | Sign of a number.
67     -- The functions 'abs' and 'signum' should satisfy the law:
68     --
69     -- > abs x * signum x == x
70     --
71     -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
72     -- or @1@ (positive).
73     signum              :: a -> a
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
79
80     {-# INLINE (-) #-}
81     {-# INLINE negate #-}
82     x - y               = x + negate y
83     negate x            = 0 - x
84
85 -- | the same as @'flip' ('-')@.
86 --
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
92 subtract x y = y - x
93 \end{code}
94
95
96 %*********************************************************
97 %*                                                      *
98 \subsection{Instances for @Int@}
99 %*                                                      *
100 %*********************************************************
101
102 \begin{code}
103 instance  Num Int  where
104     (+)    = plusInt
105     (-)    = minusInt
106     negate = negateInt
107     (*)    = timesInt
108     abs n  = if n `geInt` 0 then n else negateInt n
109
110     signum n | n `ltInt` 0 = negateInt 1
111              | n `eqInt` 0 = 0
112              | otherwise   = 1
113
114     {-# INLINE fromInteger #-}   -- Just to be sure!
115     fromInteger i = I# (toInt# i)
116
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)
120
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)
124 \end{code}
125
126 %*********************************************************
127 %*                                                      *
128 \subsection{The @Integer@ instances for @Show@}
129 %*                                                      *
130 %*********************************************************
131
132 \begin{code}
133 instance Show Integer where
134     showsPrec p n r
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
138         -- is a constant
139         | otherwise = integerToString n r
140     showList = showList__ (showsPrec 0)
141
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
147     where
148     integerToString' :: Integer -> String -> String
149     integerToString' n cs
150         | n < BASE  = jhead (fromInteger n) cs
151         | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
152
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]
159     jsplitf p n
160         | p > n     = [n]
161         | otherwise = jsplith p (jsplitf (p*p) n)
162
163     jsplith :: Integer -> [Integer] -> [Integer]
164     jsplith p (n:ns) =
165         case n `quotRemInteger` p of
166         (# q, r #) ->
167             if q > 0 then q : r : jsplitb p ns
168                      else     r : jsplitb p ns
169     jsplith _ [] = error "jsplith: []"
170
171     jsplitb :: Integer -> [Integer] -> [Integer]
172     jsplitb _ []     = []
173     jsplitb p (n:ns) = case n `quotRemInteger` p of
174                        (# q, r #) ->
175                            q : r : jsplitb p ns
176
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
181     jprinth (n:ns) cs =
182         case n `quotRemInteger` BASE of
183         (# q', r' #) ->
184             let q = fromInteger q'
185                 r = fromInteger r'
186             in if q > 0 then jhead q $ jblock r $ jprintb ns cs
187                         else jhead r $ jprintb ns cs
188     jprinth [] _ = error "jprinth []"
189
190     jprintb :: [Integer] -> String -> String
191     jprintb []     cs = cs
192     jprintb (n:ns) cs = case n `quotRemInteger` BASE of
193                         (# q', r' #) ->
194                             let q = fromInteger q'
195                                 r = fromInteger r'
196                             in jblock q $ jblock r $ jprintb ns cs
197
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
200     -- (jblock)
201     jhead :: Int -> String -> String
202     jhead n cs
203         | n < 10    = case unsafeChr (ord '0' + n) of
204             c@(C# _) -> c : cs
205         | otherwise = case unsafeChr (ord '0' + r) of
206             c@(C# _) -> jhead q (c : cs)
207         where
208         (q, r) = n `quotRemInt` 10
209
210     jblock = jblock' {- ' -} DIGITS
211
212     jblock' :: Int -> Int -> String -> String
213     jblock' d n cs
214         | d == 1    = case unsafeChr (ord '0' + n) of
215              c@(C# _) -> c : cs
216         | otherwise = case unsafeChr (ord '0' + r) of
217              c@(C# _) -> jblock' (d - 1) q (c : cs)
218         where
219         (q, r) = n `quotRemInt` 10
220 \end{code}
221
222
223 %*********************************************************
224 %*                                                      *
225 \subsection{The @Integer@ instances for @Num@}
226 %*                                                      *
227 %*********************************************************
228
229 \begin{code}
230 instance  Num Integer  where
231     (+) = plusInteger
232     (-) = minusInteger
233     (*) = timesInteger
234     negate         = negateInteger
235     fromInteger x  =  x
236
237     abs = absInteger
238     signum = signumInteger
239 \end{code}
240
241
242 %*********************************************************
243 %*                                                      *
244 \subsection{The @Integer@ instance for @Enum@}
245 %*                                                      *
246 %*********************************************************
247
248 \begin{code}
249 instance  Enum Integer  where
250     succ x               = x + 1
251     pred x               = x - 1
252     toEnum (I# n)        = smallInteger n
253     fromEnum n           = I# (toInt# n)
254
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
263
264 {-# RULES
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
269  #-}
270
271 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
272 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
273
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 .. ]
278 -- works
279
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
287
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
292
293 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
294 up_fb c n x0 delta lim = go (x0 :: Integer)
295                       where
296                         go x | x > lim   = n
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)
300                       where
301                         go x | x < lim   = n
302                              | otherwise = x `c` go (x+delta)
303
304 up_list :: Integer -> Integer -> Integer -> [Integer]
305 up_list x0 delta lim = go (x0 :: Integer)
306                     where
307                         go x | x > lim   = []
308                              | otherwise = x : go (x+delta)
309 dn_list :: Integer -> Integer -> Integer -> [Integer]
310 dn_list x0 delta lim = go (x0 :: Integer)
311                     where
312                         go x | x < lim   = []
313                              | otherwise = x : go (x+delta)
314 \end{code}
315