9f309a9c1008adb61aaea533d72e0d5ab0094d55
[ghc-hetmet.git] / ghc / lib / std / PrelReal.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: PrelReal.lhs,v 1.8 2001/02/22 13:17:59 simonpj Exp $
3 %
4 % (c) The University of Glasgow, 1994-2000
5 %
6
7 \section[PrelReal]{Module @PrelReal@}
8
9 The types
10
11         Ratio, Rational
12
13 and the classes
14
15         Real
16         Integral
17         Fractional
18         RealFrac
19
20
21 \begin{code}
22 {-# OPTIONS -fno-implicit-prelude #-}
23
24 module PrelReal where
25
26 import {-# SOURCE #-} PrelErr
27 import PrelBase
28 import PrelNum
29 import PrelList
30 import PrelEnum
31 import PrelShow
32
33 infixr 8  ^, ^^
34 infixl 7  /, `quot`, `rem`, `div`, `mod`
35
36 default ()              -- Double isn't available yet, 
37                         -- and we shouldn't be using defaults anyway
38 \end{code}
39
40
41 %*********************************************************
42 %*                                                      *
43 \subsection{The @Ratio@ and @Rational@ types}
44 %*                                                      *
45 %*********************************************************
46
47 \begin{code}
48 data  (Integral a)      => Ratio a = !a :% !a  deriving (Eq)
49 type  Rational          =  Ratio Integer
50 \end{code}
51
52
53 \begin{code}
54 {-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
55 (%)                     :: (Integral a) => a -> a -> Ratio a
56 numerator, denominator  :: (Integral a) => Ratio a -> a
57 \end{code}
58
59 \tr{reduce} is a subsidiary function used only in this module .
60 It normalises a ratio by dividing both numerator and denominator by
61 their greatest common divisor.
62
63 \begin{code}
64 reduce ::  (Integral a) => a -> a -> Ratio a
65 reduce _ 0              =  error "Ratio.%: zero denominator"
66 reduce x y              =  (x `quot` d) :% (y `quot` d)
67                            where d = gcd x y
68 \end{code}
69
70 \begin{code}
71 x % y                   =  reduce (x * signum y) (abs y)
72
73 numerator   (x :% _)    =  x
74 denominator (_ :% y)    =  y
75 \end{code}
76
77
78 %*********************************************************
79 %*                                                      *
80 \subsection{Standard numeric classes}
81 %*                                                      *
82 %*********************************************************
83
84 \begin{code}
85 class  (Num a, Ord a) => Real a  where
86     toRational          ::  a -> Rational
87
88 class  (Real a, Enum a) => Integral a  where
89     quot, rem, div, mod :: a -> a -> a
90     quotRem, divMod     :: a -> a -> (a,a)
91     toInteger           :: a -> Integer
92     toInt               :: a -> Int -- partain: Glasgow extension
93
94     n `quot` d          =  q  where (q,_) = quotRem n d
95     n `rem` d           =  r  where (_,r) = quotRem n d
96     n `div` d           =  q  where (q,_) = divMod n d
97     n `mod` d           =  r  where (_,r) = divMod n d
98     divMod n d          =  if signum r == negate (signum d) then (q-1, r+d) else qr
99                            where qr@(q,r) = quotRem n d
100
101 class  (Num a) => Fractional a  where
102     (/)                 :: a -> a -> a
103     recip               :: a -> a
104     fromRational        :: Rational -> a
105
106     recip x             =  1 / x
107     x / y               = x * recip y
108
109 class  (Real a, Fractional a) => RealFrac a  where
110     properFraction      :: (Integral b) => a -> (b,a)
111     truncate, round     :: (Integral b) => a -> b
112     ceiling, floor      :: (Integral b) => a -> b
113
114     truncate x          =  m  where (m,_) = properFraction x
115     
116     round x             =  let (n,r) = properFraction x
117                                m     = if r < 0 then n - 1 else n + 1
118                            in case signum (abs r - 0.5) of
119                                 -1 -> n
120                                 0  -> if even n then n else m
121                                 1  -> m
122     
123     ceiling x           =  if r > 0 then n + 1 else n
124                            where (n,r) = properFraction x
125     
126     floor x             =  if r < 0 then n - 1 else n
127                            where (n,r) = properFraction x
128 \end{code}
129
130
131 These 'numeric' enumerations come straight from the Report
132
133 \begin{code}
134 numericEnumFrom         :: (Fractional a) => a -> [a]
135 numericEnumFrom         =  iterate (+1)
136
137 numericEnumFromThen     :: (Fractional a) => a -> a -> [a]
138 numericEnumFromThen n m =  iterate (+(m-n)) n
139
140 numericEnumFromTo       :: (Ord a, Fractional a) => a -> a -> [a]
141 numericEnumFromTo n m   = takeWhile (<= m + 1/2) (numericEnumFrom n)
142
143 numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
144 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
145                                 where
146                                  mid = (e2 - e1) / 2
147                                  pred | e2 > e1   = (<= e3 + mid)
148                                       | otherwise = (>= e3 + mid)
149 \end{code}
150
151
152 %*********************************************************
153 %*                                                      *
154 \subsection{Instances for @Int@}
155 %*                                                      *
156 %*********************************************************
157
158 \begin{code}
159 instance  Real Int  where
160     toRational x        =  toInteger x % 1
161
162 instance  Integral Int  where
163     toInteger i = int2Integer i  -- give back a full-blown Integer
164     toInt x     = x
165
166     -- Following chks for zero divisor are non-standard (WDP)
167     a `quot` b  =  if b /= 0
168                    then a `quotInt` b
169                    else error "Prelude.Integral.quot{Int}: divide by 0"
170     a `rem` b   =  if b /= 0
171                    then a `remInt` b
172                    else error "Prelude.Integral.rem{Int}: divide by 0"
173
174     x `div` y = x `divInt` y
175     x `mod` y = x `modInt` y
176
177     a `quotRem` b = a `quotRemInt` b
178     a `divMod`  b = a `divModInt`  b
179 \end{code}
180
181
182 %*********************************************************
183 %*                                                      *
184 \subsection{Instances for @Integer@}
185 %*                                                      *
186 %*********************************************************
187
188 \begin{code}
189 instance  Real Integer  where
190     toRational x        =  x % 1
191
192 instance  Integral Integer where
193     toInteger n      = n
194     toInt n          = integer2Int n
195
196     n `quot` d = n `quotInteger` d
197     n `rem`  d = n `remInteger`  d
198
199     n `div` d   =  q  where (q,_) = divMod n d
200     n `mod` d   =  r  where (_,r) = divMod n d
201
202     a `divMod` b = a `divModInteger` b
203     a `quotRem` b = a `quotRemInteger` b
204 \end{code}
205
206
207 %*********************************************************
208 %*                                                      *
209 \subsection{Instances for @Ratio@}
210 %*                                                      *
211 %*********************************************************
212
213 \begin{code}
214 instance  (Integral a)  => Ord (Ratio a)  where
215     {-# SPECIALIZE instance Ord Rational #-}
216     (x:%y) <= (x':%y')  =  x * y' <= x' * y
217     (x:%y) <  (x':%y')  =  x * y' <  x' * y
218
219 instance  (Integral a)  => Num (Ratio a)  where
220     {-# SPECIALIZE instance Num Rational #-}
221     (x:%y) + (x':%y')   =  reduce (x*y' + x'*y) (y*y')
222     (x:%y) - (x':%y')   =  reduce (x*y' - x'*y) (y*y')
223     (x:%y) * (x':%y')   =  reduce (x * x') (y * y')
224     negate (x:%y)       =  (-x) :% y
225     abs (x:%y)          =  abs x :% y
226     signum (x:%_)       =  signum x :% 1
227     fromInteger x       =  fromInteger x :% 1
228
229 instance  (Integral a)  => Fractional (Ratio a)  where
230     {-# SPECIALIZE instance Fractional Rational #-}
231     (x:%y) / (x':%y')   =  (x*y') % (y*x')
232     recip (x:%y)        =  if x < 0 then (-y) :% (-x) else y :% x
233     fromRational (x:%y) =  fromInteger x :% fromInteger y
234
235 instance  (Integral a)  => Real (Ratio a)  where
236     {-# SPECIALIZE instance Real Rational #-}
237     toRational (x:%y)   =  toInteger x :% toInteger y
238
239 instance  (Integral a)  => RealFrac (Ratio a)  where
240     {-# SPECIALIZE instance RealFrac Rational #-}
241     properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
242                           where (q,r) = quotRem x y
243
244 instance  (Integral a)  => Show (Ratio a)  where
245     {-# SPECIALIZE instance Show Rational #-}
246     showsPrec p (x:%y)  =  showParen (p > ratio_prec)
247                                (shows x . showString " % " . shows y)
248
249 ratio_prec :: Int
250 ratio_prec = 7
251
252 instance  (Integral a)  => Enum (Ratio a)  where
253     {-# SPECIALIZE instance Enum Rational #-}
254     succ x              =  x + 1
255     pred x              =  x - 1
256
257     toEnum n            =  fromInteger (int2Integer n) :% 1
258     fromEnum            =  fromInteger . truncate
259
260     enumFrom            =  numericEnumFrom
261     enumFromThen        =  numericEnumFromThen
262     enumFromTo          =  numericEnumFromTo
263     enumFromThenTo      =  numericEnumFromThenTo
264 \end{code}
265
266
267 %*********************************************************
268 %*                                                      *
269 \subsection{Overloaded numeric functions}
270 %*                                                      *
271 %*********************************************************
272
273 \begin{code}
274 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
275 showSigned showPos p x 
276    | x < 0     = showParen (p > 6) (showChar '-' . showPos (-x))
277    | otherwise = showPos x
278
279 even, odd       :: (Integral a) => a -> Bool
280 even n          =  n `rem` 2 == 0
281 odd             =  not . even
282
283 -------------------------------------------------------
284 {-# SPECIALISE (^) ::
285         Integer -> Integer -> Integer,
286         Integer -> Int -> Integer,
287         Int -> Int -> Int #-}
288 (^)             :: (Num a, Integral b) => a -> b -> a
289 _ ^ 0           =  1
290 x ^ n | n > 0   =  f x (n-1) x
291                    where f _ 0 y = y
292                          f a d y = g a d  where
293                                    g b i | even i  = g (b*b) (i `quot` 2)
294                                          | otherwise = f b (i-1) (b*y)
295 _ ^ _           = error "Prelude.^: negative exponent"
296
297 {-# SPECIALISE (^^) ::
298         Rational -> Int -> Rational #-}
299 (^^)            :: (Fractional a, Integral b) => a -> b -> a
300 x ^^ n          =  if n >= 0 then x^n else recip (x^(negate n))
301
302
303 -------------------------------------------------------
304 gcd             :: (Integral a) => a -> a -> a
305 gcd 0 0         =  error "Prelude.gcd: gcd 0 0 is undefined"
306 gcd x y         =  gcd' (abs x) (abs y)
307                    where gcd' a 0  =  a
308                          gcd' a b  =  gcd' b (a `rem` b)
309
310 lcm             :: (Integral a) => a -> a -> a
311 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
312 lcm _ 0         =  0
313 lcm 0 _         =  0
314 lcm x y         =  abs ((x `quot` (gcd x y)) * y)
315
316
317 {-# RULES
318 "gcd/Int->Int->Int"             gcd = gcdInt
319 "gcd/Integer->Integer->Integer" gcd = gcdInteger
320 "lcm/Integer->Integer->Integer" lcm = lcmInteger
321  #-}
322 \end{code}