2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The FFI Task Force, 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The types 'Ratio' and 'Rational', and the classes 'Real', 'Fractional',
14 -- 'Integral', and 'RealFrac'.
16 -----------------------------------------------------------------------------
20 import {-# SOURCE #-} GHC.Err
28 infixl 7 /, `quot`, `rem`, `div`, `mod`
30 default () -- Double isn't available yet,
31 -- and we shouldn't be using defaults anyway
35 %*********************************************************
37 \subsection{The @Ratio@ and @Rational@ types}
39 %*********************************************************
42 data (Integral a) => Ratio a = !a :% !a deriving (Eq)
44 -- | Arbitrary-precision rational numbers, represented as a ratio of
45 -- two 'Integer' values. A rational number may be constructed using
47 type Rational = Ratio Integer
49 infinity, notANumber :: Rational
53 -- Use :%, not % for Inf/NaN; the latter would
54 -- immediately lead to a runtime error, because it normalises.
59 {-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
60 (%) :: (Integral a) => a -> a -> Ratio a
61 numerator, denominator :: (Integral a) => Ratio a -> a
64 \tr{reduce} is a subsidiary function used only in this module .
65 It normalises a ratio by dividing both numerator and denominator by
66 their greatest common divisor.
69 reduce :: (Integral a) => a -> a -> Ratio a
70 {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
71 reduce _ 0 = error "Ratio.%: zero denominator"
72 reduce x y = (x `quot` d) :% (y `quot` d)
77 x % y = reduce (x * signum y) (abs y)
79 numerator (x :% _) = x
80 denominator (_ :% y) = y
84 %*********************************************************
86 \subsection{Standard numeric classes}
88 %*********************************************************
91 class (Num a, Ord a) => Real a where
92 toRational :: a -> Rational
94 class (Real a, Enum a) => Integral a where
95 quot, rem, div, mod :: a -> a -> a
96 quotRem, divMod :: a -> a -> (a,a)
97 toInteger :: a -> Integer
99 n `quot` d = q where (q,_) = quotRem n d
100 n `rem` d = r where (_,r) = quotRem n d
101 n `div` d = q where (q,_) = divMod n d
102 n `mod` d = r where (_,r) = divMod n d
103 divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr
104 where qr@(q,r) = quotRem n d
106 class (Num a) => Fractional a where
109 fromRational :: Rational -> a
114 class (Real a, Fractional a) => RealFrac a where
115 properFraction :: (Integral b) => a -> (b,a)
116 truncate, round :: (Integral b) => a -> b
117 ceiling, floor :: (Integral b) => a -> b
119 truncate x = m where (m,_) = properFraction x
121 round x = let (n,r) = properFraction x
122 m = if r < 0 then n - 1 else n + 1
123 in case signum (abs r - 0.5) of
125 0 -> if even n then n else m
128 ceiling x = if r > 0 then n + 1 else n
129 where (n,r) = properFraction x
131 floor x = if r < 0 then n - 1 else n
132 where (n,r) = properFraction x
136 These 'numeric' enumerations come straight from the Report
139 numericEnumFrom :: (Fractional a) => a -> [a]
140 numericEnumFrom = iterate (+1)
142 numericEnumFromThen :: (Fractional a) => a -> a -> [a]
143 numericEnumFromThen n m = iterate (+(m-n)) n
145 numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
146 numericEnumFromTo n m = takeWhile (<= m + 1/2) (numericEnumFrom n)
148 numericEnumFromThenTo :: (Ord a, Fractional a) => a -> a -> a -> [a]
149 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
152 pred | e2 > e1 = (<= e3 + mid)
153 | otherwise = (>= e3 + mid)
157 %*********************************************************
159 \subsection{Instances for @Int@}
161 %*********************************************************
164 instance Real Int where
165 toRational x = toInteger x % 1
167 instance Integral Int where
168 toInteger i = int2Integer i -- give back a full-blown Integer
170 -- Following chks for zero divisor are non-standard (WDP)
171 a `quot` b = if b /= 0
173 else error "Prelude.Integral.quot{Int}: divide by 0"
174 a `rem` b = if b /= 0
176 else error "Prelude.Integral.rem{Int}: divide by 0"
178 x `div` y = x `divInt` y
179 x `mod` y = x `modInt` y
181 a `quotRem` b = a `quotRemInt` b
182 a `divMod` b = a `divModInt` b
186 %*********************************************************
188 \subsection{Instances for @Integer@}
190 %*********************************************************
193 instance Real Integer where
196 instance Integral Integer where
199 n `quot` d = n `quotInteger` d
200 n `rem` d = n `remInteger` d
202 n `div` d = q where (q,_) = divMod n d
203 n `mod` d = r where (_,r) = divMod n d
205 a `divMod` b = a `divModInteger` b
206 a `quotRem` b = a `quotRemInteger` b
210 %*********************************************************
212 \subsection{Instances for @Ratio@}
214 %*********************************************************
217 instance (Integral a) => Ord (Ratio a) where
218 {-# SPECIALIZE instance Ord Rational #-}
219 (x:%y) <= (x':%y') = x * y' <= x' * y
220 (x:%y) < (x':%y') = x * y' < x' * y
222 instance (Integral a) => Num (Ratio a) where
223 {-# SPECIALIZE instance Num Rational #-}
224 (x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
225 (x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
226 (x:%y) * (x':%y') = reduce (x * x') (y * y')
227 negate (x:%y) = (-x) :% y
228 abs (x:%y) = abs x :% y
229 signum (x:%_) = signum x :% 1
230 fromInteger x = fromInteger x :% 1
232 instance (Integral a) => Fractional (Ratio a) where
233 {-# SPECIALIZE instance Fractional Rational #-}
234 (x:%y) / (x':%y') = (x*y') % (y*x')
236 fromRational (x:%y) = fromInteger x :% fromInteger y
238 instance (Integral a) => Real (Ratio a) where
239 {-# SPECIALIZE instance Real Rational #-}
240 toRational (x:%y) = toInteger x :% toInteger y
242 instance (Integral a) => RealFrac (Ratio a) where
243 {-# SPECIALIZE instance RealFrac Rational #-}
244 properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
245 where (q,r) = quotRem x y
247 instance (Integral a) => Show (Ratio a) where
248 {-# SPECIALIZE instance Show Rational #-}
249 showsPrec p (x:%y) = showParen (p > ratio_prec)
250 (shows x . showString " % " . shows y)
255 instance (Integral a) => Enum (Ratio a) where
256 {-# SPECIALIZE instance Enum Rational #-}
260 toEnum n = fromInteger (int2Integer n) :% 1
261 fromEnum = fromInteger . truncate
263 enumFrom = numericEnumFrom
264 enumFromThen = numericEnumFromThen
265 enumFromTo = numericEnumFromTo
266 enumFromThenTo = numericEnumFromThenTo
270 %*********************************************************
272 \subsection{Coercions}
274 %*********************************************************
277 fromIntegral :: (Integral a, Num b) => a -> b
278 fromIntegral = fromInteger . toInteger
281 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
284 realToFrac :: (Real a, Fractional b) => a -> b
285 realToFrac = fromRational . toRational
288 "realToFrac/Int->Int" realToFrac = id :: Int -> Int
292 %*********************************************************
294 \subsection{Overloaded numeric functions}
296 %*********************************************************
299 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
300 showSigned showPos p x
301 | x < 0 = showParen (p > 6) (showChar '-' . showPos (-x))
302 | otherwise = showPos x
304 even, odd :: (Integral a) => a -> Bool
305 even n = n `rem` 2 == 0
308 -------------------------------------------------------
309 {-# SPECIALISE (^) ::
310 Integer -> Integer -> Integer,
311 Integer -> Int -> Integer,
312 Int -> Int -> Int #-}
313 (^) :: (Num a, Integral b) => a -> b -> a
315 x ^ n | n > 0 = f x (n-1) x
317 f a d y = g a d where
318 g b i | even i = g (b*b) (i `quot` 2)
319 | otherwise = f b (i-1) (b*y)
320 _ ^ _ = error "Prelude.^: negative exponent"
322 {-# SPECIALISE (^^) ::
323 Rational -> Int -> Rational #-}
324 (^^) :: (Fractional a, Integral b) => a -> b -> a
325 x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
328 -------------------------------------------------------
329 gcd :: (Integral a) => a -> a -> a
330 gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
331 gcd x y = gcd' (abs x) (abs y)
333 gcd' a b = gcd' b (a `rem` b)
335 lcm :: (Integral a) => a -> a -> a
336 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
339 lcm x y = abs ((x `quot` (gcd x y)) * y)
343 "gcd/Int->Int->Int" gcd = gcdInt
344 "gcd/Integer->Integer->Integer" gcd = gcdInteger
345 "lcm/Integer->Integer->Integer" lcm = lcmInteger
348 integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
349 integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]
351 integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
352 integralEnumFromThen n1 n2
353 | i_n2 >= i_n1 = map fromInteger [i_n1, i_n2 .. toInteger (maxBound `asTypeOf` n1)]
354 | otherwise = map fromInteger [i_n1, i_n2 .. toInteger (minBound `asTypeOf` n1)]
359 integralEnumFromTo :: Integral a => a -> a -> [a]
360 integralEnumFromTo n m = map fromInteger [toInteger n .. toInteger m]
362 integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
363 integralEnumFromThenTo n1 n2 m
364 = map fromInteger [toInteger n1, toInteger n2 .. toInteger m]