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`
31 default () -- Double isn't available yet,
32 -- and we shouldn't be using defaults anyway
36 %*********************************************************
38 \subsection{The @Ratio@ and @Rational@ types}
40 %*********************************************************
43 -- | Rational numbers, with numerator and denominator of some 'Integral' type.
44 data (Integral a) => Ratio a = !a :% !a deriving (Eq)
46 -- | Arbitrary-precision rational numbers, represented as a ratio of
47 -- two 'Integer' values. A rational number may be constructed using
49 type Rational = Ratio Integer
51 ratioPrec, ratioPrec1 :: Int
52 ratioPrec = 7 -- Precedence of ':%' constructor
53 ratioPrec1 = ratioPrec + 1
55 infinity, notANumber :: Rational
59 -- Use :%, not % for Inf/NaN; the latter would
60 -- immediately lead to a runtime error, because it normalises.
65 -- | Forms the ratio of two integral numbers.
66 {-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
67 (%) :: (Integral a) => a -> a -> Ratio a
69 -- | Extract the numerator of the ratio in reduced form:
70 -- the numerator and denominator have no common factor and the denominator
72 numerator :: (Integral a) => Ratio a -> a
74 -- | Extract the denominator of the ratio in reduced form:
75 -- the numerator and denominator have no common factor and the denominator
77 denominator :: (Integral a) => Ratio a -> a
80 \tr{reduce} is a subsidiary function used only in this module .
81 It normalises a ratio by dividing both numerator and denominator by
82 their greatest common divisor.
85 reduce :: (Integral a) => a -> a -> Ratio a
86 {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
87 reduce _ 0 = error "Ratio.%: zero denominator"
88 reduce x y = (x `quot` d) :% (y `quot` d)
93 x % y = reduce (x * signum y) (abs y)
95 numerator (x :% _) = x
96 denominator (_ :% y) = y
100 %*********************************************************
102 \subsection{Standard numeric classes}
104 %*********************************************************
107 class (Num a, Ord a) => Real a where
108 toRational :: a -> Rational
110 class (Real a, Enum a) => Integral a where
111 quot, rem, div, mod :: a -> a -> a
112 quotRem, divMod :: a -> a -> (a,a)
113 toInteger :: a -> Integer
115 n `quot` d = q where (q,_) = quotRem n d
116 n `rem` d = r where (_,r) = quotRem n d
117 n `div` d = q where (q,_) = divMod n d
118 n `mod` d = r where (_,r) = divMod n d
119 divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr
120 where qr@(q,r) = quotRem n d
122 class (Num a) => Fractional a where
125 fromRational :: Rational -> a
130 class (Real a, Fractional a) => RealFrac a where
131 properFraction :: (Integral b) => a -> (b,a)
132 truncate, round :: (Integral b) => a -> b
133 ceiling, floor :: (Integral b) => a -> b
135 truncate x = m where (m,_) = properFraction x
137 round x = let (n,r) = properFraction x
138 m = if r < 0 then n - 1 else n + 1
139 in case signum (abs r - 0.5) of
141 0 -> if even n then n else m
144 ceiling x = if r > 0 then n + 1 else n
145 where (n,r) = properFraction x
147 floor x = if r < 0 then n - 1 else n
148 where (n,r) = properFraction x
152 These 'numeric' enumerations come straight from the Report
155 numericEnumFrom :: (Fractional a) => a -> [a]
156 numericEnumFrom = iterate (+1)
158 numericEnumFromThen :: (Fractional a) => a -> a -> [a]
159 numericEnumFromThen n m = iterate (+(m-n)) n
161 numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
162 numericEnumFromTo n m = takeWhile (<= m + 1/2) (numericEnumFrom n)
164 numericEnumFromThenTo :: (Ord a, Fractional a) => a -> a -> a -> [a]
165 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
168 pred | e2 >= e1 = (<= e3 + mid)
169 | otherwise = (>= e3 + mid)
173 %*********************************************************
175 \subsection{Instances for @Int@}
177 %*********************************************************
180 instance Real Int where
181 toRational x = toInteger x % 1
183 instance Integral Int where
184 toInteger i = int2Integer i -- give back a full-blown Integer
186 a `quot` 0 = divZeroError
187 a `quot` b = a `quotInt` b
189 a `rem` 0 = divZeroError
190 a `rem` b = a `remInt` b
192 a `div` 0 = divZeroError
193 a `div` b = a `divInt` b
195 a `mod` 0 = divZeroError
196 a `mod` b = a `modInt` b
198 a `quotRem` 0 = divZeroError
199 a `quotRem` b = a `quotRemInt` b
201 a `divMod` 0 = divZeroError
202 a `divMod` b = a `divModInt` b
206 %*********************************************************
208 \subsection{Instances for @Integer@}
210 %*********************************************************
213 instance Real Integer where
216 instance Integral Integer where
219 a `quot` 0 = divZeroError
220 n `quot` d = n `quotInteger` d
222 a `rem` 0 = divZeroError
223 n `rem` d = n `remInteger` d
225 a `divMod` 0 = divZeroError
226 a `divMod` b = a `divModInteger` b
228 a `quotRem` 0 = divZeroError
229 a `quotRem` b = a `quotRemInteger` b
231 -- use the defaults for div & mod
235 %*********************************************************
237 \subsection{Instances for @Ratio@}
239 %*********************************************************
242 instance (Integral a) => Ord (Ratio a) where
243 {-# SPECIALIZE instance Ord Rational #-}
244 (x:%y) <= (x':%y') = x * y' <= x' * y
245 (x:%y) < (x':%y') = x * y' < x' * y
247 instance (Integral a) => Num (Ratio a) where
248 {-# SPECIALIZE instance Num Rational #-}
249 (x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
250 (x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
251 (x:%y) * (x':%y') = reduce (x * x') (y * y')
252 negate (x:%y) = (-x) :% y
253 abs (x:%y) = abs x :% y
254 signum (x:%_) = signum x :% 1
255 fromInteger x = fromInteger x :% 1
257 instance (Integral a) => Fractional (Ratio a) where
258 {-# SPECIALIZE instance Fractional Rational #-}
259 (x:%y) / (x':%y') = (x*y') % (y*x')
261 fromRational (x:%y) = fromInteger x :% fromInteger y
263 instance (Integral a) => Real (Ratio a) where
264 {-# SPECIALIZE instance Real Rational #-}
265 toRational (x:%y) = toInteger x :% toInteger y
267 instance (Integral a) => RealFrac (Ratio a) where
268 {-# SPECIALIZE instance RealFrac Rational #-}
269 properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
270 where (q,r) = quotRem x y
272 instance (Integral a) => Show (Ratio a) where
273 {-# SPECIALIZE instance Show Rational #-}
274 showsPrec p (x:%y) = showParen (p > ratioPrec) $
275 showsPrec ratioPrec1 x .
276 showString "%" . -- H98 report has spaces round the %
277 -- but we removed them [May 04]
278 showsPrec ratioPrec1 y
280 instance (Integral a) => Enum (Ratio a) where
281 {-# SPECIALIZE instance Enum Rational #-}
285 toEnum n = fromInteger (int2Integer n) :% 1
286 fromEnum = fromInteger . truncate
288 enumFrom = numericEnumFrom
289 enumFromThen = numericEnumFromThen
290 enumFromTo = numericEnumFromTo
291 enumFromThenTo = numericEnumFromThenTo
295 %*********************************************************
297 \subsection{Coercions}
299 %*********************************************************
302 fromIntegral :: (Integral a, Num b) => a -> b
303 fromIntegral = fromInteger . toInteger
306 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
309 realToFrac :: (Real a, Fractional b) => a -> b
310 realToFrac = fromRational . toRational
313 "realToFrac/Int->Int" realToFrac = id :: Int -> Int
317 %*********************************************************
319 \subsection{Overloaded numeric functions}
321 %*********************************************************
324 -- | Converts a possibly-negative 'Real' value to a string.
325 showSigned :: (Real a)
326 => (a -> ShowS) -- ^ a function that can show unsigned values
327 -> Int -- ^ the precedence of the enclosing context
328 -> a -- ^ the value to show
330 showSigned showPos p x
331 | x < 0 = showParen (p > 6) (showChar '-' . showPos (-x))
332 | otherwise = showPos x
334 even, odd :: (Integral a) => a -> Bool
335 even n = n `rem` 2 == 0
338 -------------------------------------------------------
339 {-# SPECIALISE (^) ::
340 Integer -> Integer -> Integer,
341 Integer -> Int -> Integer,
342 Int -> Int -> Int #-}
343 (^) :: (Num a, Integral b) => a -> b -> a
345 x ^ n | n > 0 = f x (n-1) x
347 f a d y = g a d where
348 g b i | even i = g (b*b) (i `quot` 2)
349 | otherwise = f b (i-1) (b*y)
350 _ ^ _ = error "Prelude.^: negative exponent"
352 {-# SPECIALISE (^^) ::
353 Rational -> Int -> Rational #-}
354 (^^) :: (Fractional a, Integral b) => a -> b -> a
355 x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
358 -------------------------------------------------------
359 gcd :: (Integral a) => a -> a -> a
360 gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
361 gcd x y = gcd' (abs x) (abs y)
363 gcd' a b = gcd' b (a `rem` b)
365 lcm :: (Integral a) => a -> a -> a
366 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
369 lcm x y = abs ((x `quot` (gcd x y)) * y)
373 "gcd/Int->Int->Int" gcd = gcdInt
374 "gcd/Integer->Integer->Integer" gcd = gcdInteger
375 "lcm/Integer->Integer->Integer" lcm = lcmInteger
378 integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
379 integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]
381 integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
382 integralEnumFromThen n1 n2
383 | i_n2 >= i_n1 = map fromInteger [i_n1, i_n2 .. toInteger (maxBound `asTypeOf` n1)]
384 | otherwise = map fromInteger [i_n1, i_n2 .. toInteger (minBound `asTypeOf` n1)]
389 integralEnumFromTo :: Integral a => a -> a -> [a]
390 integralEnumFromTo n m = map fromInteger [toInteger n .. toInteger m]
392 integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
393 integralEnumFromThenTo n1 n2 m
394 = map fromInteger [toInteger n1, toInteger n2 .. toInteger m]