Inline more default methods
[ghc-base.git] / GHC / Num.lhs
1 \begin{code}
2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module      :  GHC.Num
8 -- Copyright   :  (c) The University of Glasgow 1994-2002
9 -- License     :  see libraries/base/LICENSE
10 -- 
11 -- Maintainer  :  cvs-ghc@haskell.org
12 -- Stability   :  internal
13 -- Portability :  non-portable (GHC Extensions)
14 --
15 -- The 'Num' class and the 'Integer' type.
16 --
17 -----------------------------------------------------------------------------
18
19 #include "MachDeps.h"
20 #if SIZEOF_HSWORD == 4
21 #define DIGITS       9
22 #define BASE         1000000000
23 #elif SIZEOF_HSWORD == 8
24 #define DIGITS       18
25 #define BASE         1000000000000000000
26 #else
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.
31 #endif
32
33 -- #hide
34 module GHC.Num (module GHC.Num, module GHC.Integer) where
35
36 import GHC.Base
37 import GHC.Enum
38 import GHC.Show
39 import GHC.Integer
40
41 infixl 7  *
42 infixl 6  +, -
43
44 default ()              -- Double isn't available yet, 
45                         -- and we shouldn't be using defaults anyway
46 \end{code}
47
48 %*********************************************************
49 %*                                                      *
50 \subsection{Standard numeric class}
51 %*                                                      *
52 %*********************************************************
53
54 \begin{code}
55 -- | Basic numeric class.
56 --
57 -- Minimal complete definition: all except 'negate' or @(-)@
58 class  (Eq a, Show a) => Num a  where
59     (+), (-), (*)       :: a -> a -> a
60     -- | Unary negation.
61     negate              :: a -> a
62     -- | Absolute value.
63     abs                 :: a -> a
64     -- | Sign of a number.
65     -- The functions 'abs' and 'signum' should satisfy the law: 
66     --
67     -- > abs x * signum x == x
68     --
69     -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
70     -- or @1@ (positive).
71     signum              :: a -> a
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
77
78     {-# INLINE (-) #-}
79     {-# INLINE negate #-}
80     x - y               = x + negate y
81     negate x            = 0 - x
82
83 -- | the same as @'flip' ('-')@.
84 --
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
90 subtract x y = y - x
91 \end{code}
92
93
94 %*********************************************************
95 %*                                                      *
96 \subsection{Instances for @Int@}
97 %*                                                      *
98 %*********************************************************
99
100 \begin{code}
101 instance  Num Int  where
102     (+)    = plusInt
103     (-)    = minusInt
104     negate = negateInt
105     (*)    = timesInt
106     abs n  = if n `geInt` 0 then n else negateInt n
107
108     signum n | n `ltInt` 0 = negateInt 1
109              | n `eqInt` 0 = 0
110              | otherwise   = 1
111
112     fromInteger i = I# (toInt# i)
113
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)
117
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)
121 \end{code}
122
123 %*********************************************************
124 %*                                                      *
125 \subsection{The @Integer@ instances for @Eq@, @Ord@}
126 %*                                                      *
127 %*********************************************************
128
129 \begin{code}
130 instance  Eq Integer  where
131     (==) = eqInteger
132     (/=) = neqInteger
133
134 ------------------------------------------------------------------------
135 instance Ord Integer where
136     (<=) = leInteger
137     (>)  = gtInteger
138     (<)  = ltInteger
139     (>=) = geInteger
140     compare = compareInteger
141 \end{code}
142
143
144 %*********************************************************
145 %*                                                      *
146 \subsection{The @Integer@ instances for @Show@}
147 %*                                                      *
148 %*********************************************************
149
150 \begin{code}
151 instance Show Integer where
152     showsPrec p n r
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
156         -- is a constant
157         | otherwise = integerToString n r
158     showList = showList__ (showsPrec 0)
159
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
165     where
166     integerToString' :: Integer -> String -> String
167     integerToString' n cs
168         | n < BASE  = jhead (fromInteger n) cs
169         | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
170
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]
177     jsplitf p n
178         | p > n     = [n]
179         | otherwise = jsplith p (jsplitf (p*p) n)
180
181     jsplith :: Integer -> [Integer] -> [Integer]
182     jsplith p (n:ns) =
183         case n `quotRemInteger` p of
184         (# q, r #) ->
185             if q > 0 then q : r : jsplitb p ns
186                      else     r : jsplitb p ns
187     jsplith _ [] = error "jsplith: []"
188
189     jsplitb :: Integer -> [Integer] -> [Integer]
190     jsplitb _ []     = []
191     jsplitb p (n:ns) = case n `quotRemInteger` p of
192                        (# q, r #) ->
193                            q : r : jsplitb p ns
194
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
199     jprinth (n:ns) cs =
200         case n `quotRemInteger` BASE of
201         (# q', r' #) ->
202             let q = fromInteger q'
203                 r = fromInteger r'
204             in if q > 0 then jhead q $ jblock r $ jprintb ns cs
205                         else jhead r $ jprintb ns cs
206     jprinth [] _ = error "jprinth []"
207
208     jprintb :: [Integer] -> String -> String
209     jprintb []     cs = cs
210     jprintb (n:ns) cs = case n `quotRemInteger` BASE of
211                         (# q', r' #) ->
212                             let q = fromInteger q'
213                                 r = fromInteger r'
214                             in jblock q $ jblock r $ jprintb ns cs
215
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
218     -- (jblock)
219     jhead :: Int -> String -> String
220     jhead n cs
221         | n < 10    = case unsafeChr (ord '0' + n) of
222             c@(C# _) -> c : cs
223         | otherwise = case unsafeChr (ord '0' + r) of
224             c@(C# _) -> jhead q (c : cs)
225         where
226         (q, r) = n `quotRemInt` 10
227
228     jblock = jblock' {- ' -} DIGITS
229
230     jblock' :: Int -> Int -> String -> String
231     jblock' d n cs
232         | d == 1    = case unsafeChr (ord '0' + n) of
233              c@(C# _) -> c : cs
234         | otherwise = case unsafeChr (ord '0' + r) of
235              c@(C# _) -> jblock' (d - 1) q (c : cs)
236         where
237         (q, r) = n `quotRemInt` 10
238 \end{code}
239
240
241 %*********************************************************
242 %*                                                      *
243 \subsection{The @Integer@ instances for @Num@}
244 %*                                                      *
245 %*********************************************************
246
247 \begin{code}
248 instance  Num Integer  where
249     (+) = plusInteger
250     (-) = minusInteger
251     (*) = timesInteger
252     negate         = negateInteger
253     fromInteger x  =  x
254
255     abs = absInteger
256     signum = signumInteger
257 \end{code}
258
259
260 %*********************************************************
261 %*                                                      *
262 \subsection{The @Integer@ instance for @Enum@}
263 %*                                                      *
264 %*********************************************************
265
266 \begin{code}
267 instance  Enum Integer  where
268     succ x               = x + 1
269     pred x               = x - 1
270     toEnum (I# n)        = smallInteger n
271     fromEnum n           = I# (toInt# n)
272
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
281
282 {-# RULES
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
287  #-}
288
289 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
290 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
291
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 .. ]
296 -- works
297
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
305
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
310
311 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
312 up_fb c n x0 delta lim = go (x0 :: Integer)
313                       where
314                         go x | x > lim   = n
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)
318                       where
319                         go x | x < lim   = n
320                              | otherwise = x `c` go (x+delta)
321
322 up_list :: Integer -> Integer -> Integer -> [Integer]
323 up_list x0 delta lim = go (x0 :: Integer)
324                     where
325                         go x | x > lim   = []
326                              | otherwise = x : go (x+delta)
327 dn_list :: Integer -> Integer -> Integer -> [Integer]
328 dn_list x0 delta lim = go (x0 :: Integer)
329                     where
330                         go x | x < lim   = []
331                              | otherwise = x : go (x+delta)
332 \end{code}
333