[project @ 2001-02-22 16:48:24 by qrczak]
[ghc-hetmet.git] / ghc / lib / std / PrelReal.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: PrelReal.lhs,v 1.9 2001/02/22 16:48:24 qrczak 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
93     n `quot` d          =  q  where (q,_) = quotRem n d
94     n `rem` d           =  r  where (_,r) = quotRem n d
95     n `div` d           =  q  where (q,_) = divMod n d
96     n `mod` d           =  r  where (_,r) = divMod n d
97     divMod n d          =  if signum r == negate (signum d) then (q-1, r+d) else qr
98                            where qr@(q,r) = quotRem n d
99
100 toInt :: Integral a => a -> Int
101 -- For backward compatibility
102 toInt i = fromInteger (toInteger i)
103
104 class  (Num a) => Fractional a  where
105     (/)                 :: a -> a -> a
106     recip               :: a -> a
107     fromRational        :: Rational -> a
108
109     recip x             =  1 / x
110     x / y               = x * recip y
111
112 class  (Real a, Fractional a) => RealFrac a  where
113     properFraction      :: (Integral b) => a -> (b,a)
114     truncate, round     :: (Integral b) => a -> b
115     ceiling, floor      :: (Integral b) => a -> b
116
117     truncate x          =  m  where (m,_) = properFraction x
118     
119     round x             =  let (n,r) = properFraction x
120                                m     = if r < 0 then n - 1 else n + 1
121                            in case signum (abs r - 0.5) of
122                                 -1 -> n
123                                 0  -> if even n then n else m
124                                 1  -> m
125     
126     ceiling x           =  if r > 0 then n + 1 else n
127                            where (n,r) = properFraction x
128     
129     floor x             =  if r < 0 then n - 1 else n
130                            where (n,r) = properFraction x
131 \end{code}
132
133
134 These 'numeric' enumerations come straight from the Report
135
136 \begin{code}
137 numericEnumFrom         :: (Fractional a) => a -> [a]
138 numericEnumFrom         =  iterate (+1)
139
140 numericEnumFromThen     :: (Fractional a) => a -> a -> [a]
141 numericEnumFromThen n m =  iterate (+(m-n)) n
142
143 numericEnumFromTo       :: (Ord a, Fractional a) => a -> a -> [a]
144 numericEnumFromTo n m   = takeWhile (<= m + 1/2) (numericEnumFrom n)
145
146 numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
147 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
148                                 where
149                                  mid = (e2 - e1) / 2
150                                  pred | e2 > e1   = (<= e3 + mid)
151                                       | otherwise = (>= e3 + mid)
152 \end{code}
153
154
155 %*********************************************************
156 %*                                                      *
157 \subsection{Instances for @Int@}
158 %*                                                      *
159 %*********************************************************
160
161 \begin{code}
162 instance  Real Int  where
163     toRational x        =  toInteger x % 1
164
165 instance  Integral Int  where
166     toInteger i = int2Integer i  -- give back a full-blown Integer
167
168     -- Following chks for zero divisor are non-standard (WDP)
169     a `quot` b  =  if b /= 0
170                    then a `quotInt` b
171                    else error "Prelude.Integral.quot{Int}: divide by 0"
172     a `rem` b   =  if b /= 0
173                    then a `remInt` b
174                    else error "Prelude.Integral.rem{Int}: divide by 0"
175
176     x `div` y = x `divInt` y
177     x `mod` y = x `modInt` y
178
179     a `quotRem` b = a `quotRemInt` b
180     a `divMod`  b = a `divModInt`  b
181 \end{code}
182
183
184 %*********************************************************
185 %*                                                      *
186 \subsection{Instances for @Integer@}
187 %*                                                      *
188 %*********************************************************
189
190 \begin{code}
191 instance  Real Integer  where
192     toRational x        =  x % 1
193
194 instance  Integral Integer where
195     toInteger n      = n
196
197     n `quot` d = n `quotInteger` d
198     n `rem`  d = n `remInteger`  d
199
200     n `div` d   =  q  where (q,_) = divMod n d
201     n `mod` d   =  r  where (_,r) = divMod n d
202
203     a `divMod` b = a `divModInteger` b
204     a `quotRem` b = a `quotRemInteger` b
205 \end{code}
206
207
208 %*********************************************************
209 %*                                                      *
210 \subsection{Instances for @Ratio@}
211 %*                                                      *
212 %*********************************************************
213
214 \begin{code}
215 instance  (Integral a)  => Ord (Ratio a)  where
216     {-# SPECIALIZE instance Ord Rational #-}
217     (x:%y) <= (x':%y')  =  x * y' <= x' * y
218     (x:%y) <  (x':%y')  =  x * y' <  x' * y
219
220 instance  (Integral a)  => Num (Ratio a)  where
221     {-# SPECIALIZE instance Num Rational #-}
222     (x:%y) + (x':%y')   =  reduce (x*y' + x'*y) (y*y')
223     (x:%y) - (x':%y')   =  reduce (x*y' - x'*y) (y*y')
224     (x:%y) * (x':%y')   =  reduce (x * x') (y * y')
225     negate (x:%y)       =  (-x) :% y
226     abs (x:%y)          =  abs x :% y
227     signum (x:%_)       =  signum x :% 1
228     fromInteger x       =  fromInteger x :% 1
229
230 instance  (Integral a)  => Fractional (Ratio a)  where
231     {-# SPECIALIZE instance Fractional Rational #-}
232     (x:%y) / (x':%y')   =  (x*y') % (y*x')
233     recip (x:%y)        =  if x < 0 then (-y) :% (-x) else y :% x
234     fromRational (x:%y) =  fromInteger x :% fromInteger y
235
236 instance  (Integral a)  => Real (Ratio a)  where
237     {-# SPECIALIZE instance Real Rational #-}
238     toRational (x:%y)   =  toInteger x :% toInteger y
239
240 instance  (Integral a)  => RealFrac (Ratio a)  where
241     {-# SPECIALIZE instance RealFrac Rational #-}
242     properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
243                           where (q,r) = quotRem x y
244
245 instance  (Integral a)  => Show (Ratio a)  where
246     {-# SPECIALIZE instance Show Rational #-}
247     showsPrec p (x:%y)  =  showParen (p > ratio_prec)
248                                (shows x . showString " % " . shows y)
249
250 ratio_prec :: Int
251 ratio_prec = 7
252
253 instance  (Integral a)  => Enum (Ratio a)  where
254     {-# SPECIALIZE instance Enum Rational #-}
255     succ x              =  x + 1
256     pred x              =  x - 1
257
258     toEnum n            =  fromInteger (int2Integer n) :% 1
259     fromEnum            =  fromInteger . truncate
260
261     enumFrom            =  numericEnumFrom
262     enumFromThen        =  numericEnumFromThen
263     enumFromTo          =  numericEnumFromTo
264     enumFromThenTo      =  numericEnumFromThenTo
265 \end{code}
266
267
268 %*********************************************************
269 %*                                                      *
270 \subsection{Overloaded numeric functions}
271 %*                                                      *
272 %*********************************************************
273
274 \begin{code}
275 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
276 showSigned showPos p x 
277    | x < 0     = showParen (p > 6) (showChar '-' . showPos (-x))
278    | otherwise = showPos x
279
280 even, odd       :: (Integral a) => a -> Bool
281 even n          =  n `rem` 2 == 0
282 odd             =  not . even
283
284 -------------------------------------------------------
285 {-# SPECIALISE (^) ::
286         Integer -> Integer -> Integer,
287         Integer -> Int -> Integer,
288         Int -> Int -> Int #-}
289 (^)             :: (Num a, Integral b) => a -> b -> a
290 _ ^ 0           =  1
291 x ^ n | n > 0   =  f x (n-1) x
292                    where f _ 0 y = y
293                          f a d y = g a d  where
294                                    g b i | even i  = g (b*b) (i `quot` 2)
295                                          | otherwise = f b (i-1) (b*y)
296 _ ^ _           = error "Prelude.^: negative exponent"
297
298 {-# SPECIALISE (^^) ::
299         Rational -> Int -> Rational #-}
300 (^^)            :: (Fractional a, Integral b) => a -> b -> a
301 x ^^ n          =  if n >= 0 then x^n else recip (x^(negate n))
302
303
304 -------------------------------------------------------
305 gcd             :: (Integral a) => a -> a -> a
306 gcd 0 0         =  error "Prelude.gcd: gcd 0 0 is undefined"
307 gcd x y         =  gcd' (abs x) (abs y)
308                    where gcd' a 0  =  a
309                          gcd' a b  =  gcd' b (a `rem` b)
310
311 lcm             :: (Integral a) => a -> a -> a
312 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
313 lcm _ 0         =  0
314 lcm 0 _         =  0
315 lcm x y         =  abs ((x `quot` (gcd x y)) * y)
316
317
318 {-# RULES
319 "gcd/Int->Int->Int"             gcd = gcdInt
320 "gcd/Integer->Integer->Integer" gcd = gcdInteger
321 "lcm/Integer->Integer->Integer" lcm = lcmInteger
322  #-}
323 \end{code}