Fix warnings
[ghc-base.git] / GHC / Num.lhs
1 \begin{code}
2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module      :  GHC.Num
7 -- Copyright   :  (c) The University of Glasgow 1994-2002
8 -- License     :  see libraries/base/LICENSE
9 -- 
10 -- Maintainer  :  cvs-ghc@haskell.org
11 -- Stability   :  internal
12 -- Portability :  non-portable (GHC Extensions)
13 --
14 -- The 'Num' class and the 'Integer' type.
15 --
16 -----------------------------------------------------------------------------
17
18 #include "MachDeps.h"
19 #if SIZEOF_HSWORD == 4
20 #define DIGITS       9
21 #define BASE         1000000000
22 #elif SIZEOF_HSWORD == 8
23 #define DIGITS       18
24 #define BASE         1000000000000000000
25 #else
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.
30 #endif
31
32 -- #hide
33 module GHC.Num (module GHC.Num, module GHC.Integer) where
34
35 import GHC.Base
36 import GHC.Enum
37 import GHC.Show
38 import GHC.Integer
39
40 infixl 7  *
41 infixl 6  +, -
42
43 default ()              -- Double isn't available yet, 
44                         -- and we shouldn't be using defaults anyway
45 \end{code}
46
47 %*********************************************************
48 %*                                                      *
49 \subsection{Standard numeric class}
50 %*                                                      *
51 %*********************************************************
52
53 \begin{code}
54 -- | Basic numeric class.
55 --
56 -- Minimal complete definition: all except 'negate' or @(-)@
57 class  (Eq a, Show a) => Num a  where
58     (+), (-), (*)       :: a -> a -> a
59     -- | Unary negation.
60     negate              :: a -> a
61     -- | Absolute value.
62     abs                 :: a -> a
63     -- | Sign of a number.
64     -- The functions 'abs' and 'signum' should satisfy the law: 
65     --
66     -- > abs x * signum x == x
67     --
68     -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
69     -- or @1@ (positive).
70     signum              :: a -> a
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
76
77     x - y               = x + negate y
78     negate x            = 0 - x
79
80 -- | the same as @'flip' ('-')@.
81 --
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
87 subtract x y = y - x
88 \end{code}
89
90
91 %*********************************************************
92 %*                                                      *
93 \subsection{Instances for @Int@}
94 %*                                                      *
95 %*********************************************************
96
97 \begin{code}
98 instance  Num Int  where
99     (+)    = plusInt
100     (-)    = minusInt
101     negate = negateInt
102     (*)    = timesInt
103     abs n  = if n `geInt` 0 then n else negateInt n
104
105     signum n | n `ltInt` 0 = negateInt 1
106              | n `eqInt` 0 = 0
107              | otherwise   = 1
108
109     fromInteger i = I# (toInt# i)
110
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)
114
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)
118 \end{code}
119
120 %*********************************************************
121 %*                                                      *
122 \subsection{The @Integer@ instances for @Eq@, @Ord@}
123 %*                                                      *
124 %*********************************************************
125
126 \begin{code}
127 instance  Eq Integer  where
128     (==) = eqInteger
129     (/=) = neqInteger
130
131 ------------------------------------------------------------------------
132 instance Ord Integer where
133     (<=) = leInteger
134     (>)  = gtInteger
135     (<)  = ltInteger
136     (>=) = geInteger
137     compare = compareInteger
138 \end{code}
139
140
141 %*********************************************************
142 %*                                                      *
143 \subsection{The @Integer@ instances for @Show@}
144 %*                                                      *
145 %*********************************************************
146
147 \begin{code}
148 instance Show Integer where
149     showsPrec p n r
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
153         -- is a constant
154         | otherwise = integerToString n r
155     showList = showList__ (showsPrec 0)
156
157 -- Divide an conquer implementation of string conversion
158 integerToString :: Integer -> String -> String
159 integerToString n0 cs0
160     | n0 < 0    = '-' : integerToString' (- n0) cs0
161     | otherwise = integerToString' n0 cs0
162     where
163     integerToString' :: Integer -> String -> String
164     integerToString' n cs
165         | n < BASE  = jhead (fromInteger n) cs
166         | otherwise = jprinth (jsplitf (BASE*BASE) n) cs
167
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]
174     jsplitf p n
175         | p > n     = [n]
176         | otherwise = jsplith p (jsplitf (p*p) n)
177
178     jsplith :: Integer -> [Integer] -> [Integer]
179     jsplith p (n:ns) =
180         case n `quotRemInteger` p of
181         (# q, r #) ->
182             if q > 0 then fromInteger q : fromInteger r : jsplitb p ns
183                      else fromInteger r : jsplitb p ns
184     jsplith _ [] = error "jsplith: []"
185
186     jsplitb :: Integer -> [Integer] -> [Integer]
187     jsplitb _ []     = []
188     jsplitb p (n:ns) = case n `quotRemInteger` p of
189                        (# q, r #) ->
190                            q : r : jsplitb p ns
191
192     -- Convert a number that has been split into digits in base BASE^2
193     -- this includes a last splitting step and then conversion of digits
194     -- that all fit into a machine word.
195     jprinth :: [Integer] -> String -> String
196     jprinth (n:ns) cs =
197         case n `quotRemInteger` BASE of
198         (# q', r' #) ->
199             let q = fromInteger q'
200                 r = fromInteger r'
201             in if q > 0 then jhead q $ jblock r $ jprintb ns cs
202                         else jhead r $ jprintb ns cs
203     jprinth [] _ = error "jprinth []"
204
205     jprintb :: [Integer] -> String -> String
206     jprintb []     cs = cs
207     jprintb (n:ns) cs = case n `quotRemInteger` BASE of
208                         (# q', r' #) ->
209                             let q = fromInteger q'
210                                 r = fromInteger r'
211                             in jblock q $ jblock r $ jprintb ns cs
212
213     -- Convert an integer that fits into a machine word. Again, we have two
214     -- functions, one that drops leading zeros (jhead) and one that doesn't
215     -- (jblock)
216     jhead :: Int -> String -> String
217     jhead n cs
218         | n < 10    = case unsafeChr (ord '0' + n) of
219             c@(C# _) -> c : cs
220         | otherwise = case unsafeChr (ord '0' + r) of
221             c@(C# _) -> jhead q (c : cs)
222         where
223         (q, r) = n `quotRemInt` 10
224
225     jblock = jblock' {- ' -} DIGITS
226
227     jblock' :: Int -> Int -> String -> String
228     jblock' d n cs
229         | d == 1    = case unsafeChr (ord '0' + n) of
230              c@(C# _) -> c : cs
231         | otherwise = case unsafeChr (ord '0' + r) of
232              c@(C# _) -> jblock' (d - 1) q (c : cs)
233         where
234         (q, r) = n `quotRemInt` 10
235 \end{code}
236
237
238 %*********************************************************
239 %*                                                      *
240 \subsection{The @Integer@ instances for @Num@}
241 %*                                                      *
242 %*********************************************************
243
244 \begin{code}
245 instance  Num Integer  where
246     (+) = plusInteger
247     (-) = minusInteger
248     (*) = timesInteger
249     negate         = negateInteger
250     fromInteger x  =  x
251
252     abs = absInteger
253     signum = signumInteger
254 \end{code}
255
256
257 %*********************************************************
258 %*                                                      *
259 \subsection{The @Integer@ instance for @Enum@}
260 %*                                                      *
261 %*********************************************************
262
263 \begin{code}
264 instance  Enum Integer  where
265     succ x               = x + 1
266     pred x               = x - 1
267     toEnum (I# n)        = smallInteger n
268     fromEnum n           = I# (toInt# n)
269
270     {-# INLINE enumFrom #-}
271     {-# INLINE enumFromThen #-}
272     {-# INLINE enumFromTo #-}
273     {-# INLINE enumFromThenTo #-}
274     enumFrom x             = enumDeltaInteger  x 1
275     enumFromThen x y       = enumDeltaInteger  x (y-x)
276     enumFromTo x lim       = enumDeltaToInteger x 1     lim
277     enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
278
279 {-# RULES
280 "enumDeltaInteger"      [~1] forall x y.  enumDeltaInteger x y     = build (\c _ -> enumDeltaIntegerFB c x y)
281 "efdtInteger"           [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)
282 "enumDeltaInteger"      [1] enumDeltaIntegerFB   (:)    = enumDeltaInteger
283 "enumDeltaToInteger"    [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger
284  #-}
285
286 enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
287 enumDeltaIntegerFB c x d = x `seq` (x `c` enumDeltaIntegerFB c (x+d) d)
288
289 enumDeltaInteger :: Integer -> Integer -> [Integer]
290 enumDeltaInteger x d = x `seq` (x : enumDeltaInteger (x+d) d)
291 -- strict accumulator, so
292 --     head (drop 1000000 [1 .. ]
293 -- works
294
295 enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
296                      -> Integer -> Integer -> Integer -> a
297 enumDeltaToIntegerFB c n x delta lim
298   | delta >= 0 = up_fb c n x delta lim
299   | otherwise  = dn_fb c n x delta lim
300
301 enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
302 enumDeltaToInteger x delta lim
303   | delta >= 0 = up_list x delta lim
304   | otherwise  = dn_list x delta lim
305
306 up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
307 up_fb c n x0 delta lim = go (x0 :: Integer)
308                       where
309                         go x | x > lim   = n
310                              | otherwise = x `c` go (x+delta)
311 dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
312 dn_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
317 up_list :: Integer -> Integer -> Integer -> [Integer]
318 up_list x0 delta lim = go (x0 :: Integer)
319                     where
320                         go x | x > lim   = []
321                              | otherwise = x : go (x+delta)
322 dn_list :: Integer -> Integer -> Integer -> [Integer]
323 dn_list x0 delta lim = go (x0 :: Integer)
324                     where
325                         go x | x < lim   = []
326                              | otherwise = x : go (x+delta)
327 \end{code}
328