1 % ------------------------------------------------------------------------------
2 % $Id: PrelReal.lhs,v 1.9 2001/02/22 16:48:24 qrczak Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[PrelReal]{Module @PrelReal@}
22 {-# OPTIONS -fno-implicit-prelude #-}
26 import {-# SOURCE #-} PrelErr
34 infixl 7 /, `quot`, `rem`, `div`, `mod`
36 default () -- Double isn't available yet,
37 -- and we shouldn't be using defaults anyway
41 %*********************************************************
43 \subsection{The @Ratio@ and @Rational@ types}
45 %*********************************************************
48 data (Integral a) => Ratio a = !a :% !a deriving (Eq)
49 type Rational = Ratio Integer
54 {-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
55 (%) :: (Integral a) => a -> a -> Ratio a
56 numerator, denominator :: (Integral a) => Ratio a -> a
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.
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)
71 x % y = reduce (x * signum y) (abs y)
73 numerator (x :% _) = x
74 denominator (_ :% y) = y
78 %*********************************************************
80 \subsection{Standard numeric classes}
82 %*********************************************************
85 class (Num a, Ord a) => Real a where
86 toRational :: a -> Rational
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
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
100 toInt :: Integral a => a -> Int
101 -- For backward compatibility
102 toInt i = fromInteger (toInteger i)
104 class (Num a) => Fractional a where
107 fromRational :: Rational -> a
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
117 truncate x = m where (m,_) = properFraction x
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
123 0 -> if even n then n else m
126 ceiling x = if r > 0 then n + 1 else n
127 where (n,r) = properFraction x
129 floor x = if r < 0 then n - 1 else n
130 where (n,r) = properFraction x
134 These 'numeric' enumerations come straight from the Report
137 numericEnumFrom :: (Fractional a) => a -> [a]
138 numericEnumFrom = iterate (+1)
140 numericEnumFromThen :: (Fractional a) => a -> a -> [a]
141 numericEnumFromThen n m = iterate (+(m-n)) n
143 numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
144 numericEnumFromTo n m = takeWhile (<= m + 1/2) (numericEnumFrom n)
146 numericEnumFromThenTo :: (Ord a, Fractional a) => a -> a -> a -> [a]
147 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
150 pred | e2 > e1 = (<= e3 + mid)
151 | otherwise = (>= e3 + mid)
155 %*********************************************************
157 \subsection{Instances for @Int@}
159 %*********************************************************
162 instance Real Int where
163 toRational x = toInteger x % 1
165 instance Integral Int where
166 toInteger i = int2Integer i -- give back a full-blown Integer
168 -- Following chks for zero divisor are non-standard (WDP)
169 a `quot` b = if b /= 0
171 else error "Prelude.Integral.quot{Int}: divide by 0"
172 a `rem` b = if b /= 0
174 else error "Prelude.Integral.rem{Int}: divide by 0"
176 x `div` y = x `divInt` y
177 x `mod` y = x `modInt` y
179 a `quotRem` b = a `quotRemInt` b
180 a `divMod` b = a `divModInt` b
184 %*********************************************************
186 \subsection{Instances for @Integer@}
188 %*********************************************************
191 instance Real Integer where
194 instance Integral Integer where
197 n `quot` d = n `quotInteger` d
198 n `rem` d = n `remInteger` d
200 n `div` d = q where (q,_) = divMod n d
201 n `mod` d = r where (_,r) = divMod n d
203 a `divMod` b = a `divModInteger` b
204 a `quotRem` b = a `quotRemInteger` b
208 %*********************************************************
210 \subsection{Instances for @Ratio@}
212 %*********************************************************
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
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
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
236 instance (Integral a) => Real (Ratio a) where
237 {-# SPECIALIZE instance Real Rational #-}
238 toRational (x:%y) = toInteger x :% toInteger y
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
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)
253 instance (Integral a) => Enum (Ratio a) where
254 {-# SPECIALIZE instance Enum Rational #-}
258 toEnum n = fromInteger (int2Integer n) :% 1
259 fromEnum = fromInteger . truncate
261 enumFrom = numericEnumFrom
262 enumFromThen = numericEnumFromThen
263 enumFromTo = numericEnumFromTo
264 enumFromThenTo = numericEnumFromThenTo
268 %*********************************************************
270 \subsection{Overloaded numeric functions}
272 %*********************************************************
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
280 even, odd :: (Integral a) => a -> Bool
281 even n = n `rem` 2 == 0
284 -------------------------------------------------------
285 {-# SPECIALISE (^) ::
286 Integer -> Integer -> Integer,
287 Integer -> Int -> Integer,
288 Int -> Int -> Int #-}
289 (^) :: (Num a, Integral b) => a -> b -> a
291 x ^ n | n > 0 = f x (n-1) x
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"
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))
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)
309 gcd' a b = gcd' b (a `rem` b)
311 lcm :: (Integral a) => a -> a -> a
312 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
315 lcm x y = abs ((x `quot` (gcd x y)) * y)
319 "gcd/Int->Int->Int" gcd = gcdInt
320 "gcd/Integer->Integer->Integer" gcd = gcdInteger
321 "lcm/Integer->Integer->Integer" lcm = lcmInteger