improvements to Data.Fixed: instances for Typeable and Data, more predefined types
[ghc-base.git] / Data / Fixed.hs
1 {-# OPTIONS -Wall -fno-warn-unused-binds #-}
2
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  Data.Fixed
6 -- Copyright   :  (c) Ashley Yakeley 2005, 2006, 2009
7 -- License     :  BSD-style (see the file libraries/base/LICENSE)
8 -- 
9 -- Maintainer  :  Ashley Yakeley <ashley@semantic.org>
10 -- Stability   :  experimental
11 -- Portability :  portable
12 --
13 -- This module defines a \"Fixed\" type for fixed-precision arithmetic.
14 -- The parameter to Fixed is any type that's an instance of HasResolution.
15 -- HasResolution has a single method that gives the resolution of the Fixed type.
16 --
17 -- This module also contains generalisations of div, mod, and divmod to work
18 -- with any Real instance.
19 --
20 -----------------------------------------------------------------------------
21
22 module Data.Fixed
23 (
24     div',mod',divMod',
25
26     Fixed,HasResolution(..),
27     showFixed,
28     E0,Uni,
29     E1,Deci,
30     E2,Centi,
31     E3,Milli,
32     E6,Micro,
33     E9,Nano,
34     E12,Pico
35 ) where
36
37 import Prelude -- necessary to get dependencies right
38 import Data.Typeable
39 import Data.Data
40
41 default () -- avoid any defaulting shenanigans
42
43 -- | generalisation of 'div' to any instance of Real
44 div' :: (Real a,Integral b) => a -> a -> b
45 div' n d = floor ((toRational n) / (toRational d))
46
47 -- | generalisation of 'divMod' to any instance of Real
48 divMod' :: (Real a,Integral b) => a -> a -> (b,a)
49 divMod' n d = (f,n - (fromIntegral f) * d) where
50     f = div' n d
51
52 -- | generalisation of 'mod' to any instance of Real
53 mod' :: (Real a) => a -> a -> a
54 mod' n d = n - (fromInteger f) * d where
55     f = div' n d
56
57 -- | The type parameter should be an instance of 'HasResolution'.
58 newtype Fixed a = MkFixed Integer deriving (Eq,Ord,Typeable)
59
60 -- We do this because the automatically derived Data instance requires (Data a) context.
61 -- Our manual instance has the more general (Typeable a) context.
62 tyFixed :: DataType
63 tyFixed = mkDataType "Data.Fixed.Fixed" [conMkFixed]
64 conMkFixed :: Constr
65 conMkFixed = mkConstr tyFixed "MkFixed" [] Prefix
66 instance (Typeable a) => Data (Fixed a) where
67     gfoldl k z (MkFixed a) = k (z MkFixed) a
68     gunfold k z _ = k (z MkFixed)
69     dataTypeOf _ = tyFixed
70     toConstr _ = conMkFixed
71
72 class HasResolution a where
73     resolution :: p a -> Integer
74
75 withType :: (p a -> f a) -> f a
76 withType foo = foo undefined
77
78 withResolution :: (HasResolution a) => (Integer -> f a) -> f a
79 withResolution foo = withType (foo . resolution)
80
81 instance Enum (Fixed a) where
82     succ (MkFixed a) = MkFixed (succ a)
83     pred (MkFixed a) = MkFixed (pred a)
84     toEnum = MkFixed . toEnum
85     fromEnum (MkFixed a) = fromEnum a
86     enumFrom (MkFixed a) = fmap MkFixed (enumFrom a)
87     enumFromThen (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromThen a b)
88     enumFromTo (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromTo a b)
89     enumFromThenTo (MkFixed a) (MkFixed b) (MkFixed c) = fmap MkFixed (enumFromThenTo a b c)
90
91 instance (HasResolution a) => Num (Fixed a) where
92     (MkFixed a) + (MkFixed b) = MkFixed (a + b)
93     (MkFixed a) - (MkFixed b) = MkFixed (a - b)
94     fa@(MkFixed a) * (MkFixed b) = MkFixed (div (a * b) (resolution fa))
95     negate (MkFixed a) = MkFixed (negate a)
96     abs (MkFixed a) = MkFixed (abs a)
97     signum (MkFixed a) = fromInteger (signum a)
98     fromInteger i = withResolution (\res -> MkFixed (i * res))
99
100 instance (HasResolution a) => Real (Fixed a) where
101     toRational fa@(MkFixed a) = (toRational a) / (toRational (resolution fa))
102
103 instance (HasResolution a) => Fractional (Fixed a) where
104     fa@(MkFixed a) / (MkFixed b) = MkFixed (div (a * (resolution fa)) b)
105     recip fa@(MkFixed a) = MkFixed (div (res * res) a) where
106         res = resolution fa
107     fromRational r = withResolution (\res -> MkFixed (floor (r * (toRational res))))
108
109 instance (HasResolution a) => RealFrac (Fixed a) where
110     properFraction a = (i,a - (fromIntegral i)) where
111         i = truncate a
112     truncate f = truncate (toRational f)
113     round f = round (toRational f)
114     ceiling f = ceiling (toRational f)
115     floor f = floor (toRational f)
116
117 chopZeros :: Integer -> String
118 chopZeros 0 = ""
119 chopZeros a | mod a 10 == 0 = chopZeros (div a 10)
120 chopZeros a = show a
121
122 -- only works for positive a
123 showIntegerZeros :: Bool -> Int -> Integer -> String
124 showIntegerZeros True _ 0 = ""
125 showIntegerZeros chopTrailingZeros digits a = replicate (digits - length s) '0' ++ s' where
126     s = show a
127     s' = if chopTrailingZeros then chopZeros a else s
128
129 withDot :: String -> String
130 withDot "" = ""
131 withDot s = '.':s
132
133 -- | First arg is whether to chop off trailing zeros
134 showFixed :: (HasResolution a) => Bool -> Fixed a -> String
135 showFixed chopTrailingZeros fa@(MkFixed a) | a < 0 = "-" ++ (showFixed chopTrailingZeros (asTypeOf (MkFixed (negate a)) fa))
136 showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZeros chopTrailingZeros digits fracNum)) where
137     res = resolution fa
138     (i,d) = divMod a res
139     -- enough digits to be unambiguous
140     digits = ceiling (logBase 10 (fromInteger res) :: Double)
141     maxnum = 10 ^ digits
142     fracNum = div (d * maxnum) res
143
144 instance (HasResolution a) => Show (Fixed a) where
145     show = showFixed False
146
147
148 data E0 = E0 deriving (Typeable)
149 instance HasResolution E0 where
150     resolution _ = 1
151 -- | resolution of 1, this works the same as Integer
152 type Uni = Fixed E0
153
154 data E1 = E1 deriving (Typeable)
155 instance HasResolution E1 where
156     resolution _ = 10
157 -- | resolution of 10^-1 = .1
158 type Deci = Fixed E1
159
160 data E2 = E2 deriving (Typeable)
161 instance HasResolution E2 where
162     resolution _ = 100
163 -- | resolution of 10^-2 = .01, useful for many monetary currencies
164 type Centi = Fixed E2
165
166 data E3 = E3 deriving (Typeable)
167 instance HasResolution E3 where
168     resolution _ = 1000
169 -- | resolution of 10^-3 = .001
170 type Milli = Fixed E3
171
172 data E6 = E6 deriving (Typeable)
173 instance HasResolution E6 where
174     resolution _ = 1000000
175 -- | resolution of 10^-6 = .000001
176 type Micro = Fixed E6
177
178 data E9 = E9 deriving (Typeable)
179 instance HasResolution E9 where
180     resolution _ = 1000000000
181 -- | resolution of 10^-9 = .000000001
182 type Nano = Fixed E9
183
184 data E12 = E12 deriving (Typeable)
185 instance HasResolution E12 where
186     resolution _ = 1000000000000
187 -- | resolution of 10^-12 = .000000000001
188 type Pico = Fixed E12