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 data (Integral a) => Ratio a = !a :% !a deriving (Eq)
45 -- | Arbitrary-precision rational numbers, represented as a ratio of
46 -- two 'Integer' values. A rational number may be constructed using
48 type Rational = Ratio Integer
50 ratioPrec, ratioPrec1 :: Int
51 ratioPrec = 7 -- Precedence of ':%' constructor
52 ratioPrec1 = ratioPrec + 1
54 infinity, notANumber :: Rational
58 -- Use :%, not % for Inf/NaN; the latter would
59 -- immediately lead to a runtime error, because it normalises.
64 {-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
65 (%) :: (Integral a) => a -> a -> Ratio a
66 numerator, denominator :: (Integral a) => Ratio a -> a
69 \tr{reduce} is a subsidiary function used only in this module .
70 It normalises a ratio by dividing both numerator and denominator by
71 their greatest common divisor.
74 reduce :: (Integral a) => a -> a -> Ratio a
75 {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
76 reduce _ 0 = error "Ratio.%: zero denominator"
77 reduce x y = (x `quot` d) :% (y `quot` d)
82 x % y = reduce (x * signum y) (abs y)
84 numerator (x :% _) = x
85 denominator (_ :% y) = y
89 %*********************************************************
91 \subsection{Standard numeric classes}
93 %*********************************************************
96 class (Num a, Ord a) => Real a where
97 toRational :: a -> Rational
99 class (Real a, Enum a) => Integral a where
100 quot, rem, div, mod :: a -> a -> a
101 quotRem, divMod :: a -> a -> (a,a)
102 toInteger :: a -> Integer
104 n `quot` d = q where (q,_) = quotRem n d
105 n `rem` d = r where (_,r) = quotRem n d
106 n `div` d = q where (q,_) = divMod n d
107 n `mod` d = r where (_,r) = divMod n d
108 divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr
109 where qr@(q,r) = quotRem n d
111 class (Num a) => Fractional a where
114 fromRational :: Rational -> a
119 class (Real a, Fractional a) => RealFrac a where
120 properFraction :: (Integral b) => a -> (b,a)
121 truncate, round :: (Integral b) => a -> b
122 ceiling, floor :: (Integral b) => a -> b
124 truncate x = m where (m,_) = properFraction x
126 round x = let (n,r) = properFraction x
127 m = if r < 0 then n - 1 else n + 1
128 in case signum (abs r - 0.5) of
130 0 -> if even n then n else m
133 ceiling x = if r > 0 then n + 1 else n
134 where (n,r) = properFraction x
136 floor x = if r < 0 then n - 1 else n
137 where (n,r) = properFraction x
141 These 'numeric' enumerations come straight from the Report
144 numericEnumFrom :: (Fractional a) => a -> [a]
145 numericEnumFrom = iterate (+1)
147 numericEnumFromThen :: (Fractional a) => a -> a -> [a]
148 numericEnumFromThen n m = iterate (+(m-n)) n
150 numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
151 numericEnumFromTo n m = takeWhile (<= m + 1/2) (numericEnumFrom n)
153 numericEnumFromThenTo :: (Ord a, Fractional a) => a -> a -> a -> [a]
154 numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
157 pred | e2 > e1 = (<= e3 + mid)
158 | otherwise = (>= e3 + mid)
162 %*********************************************************
164 \subsection{Instances for @Int@}
166 %*********************************************************
169 instance Real Int where
170 toRational x = toInteger x % 1
172 instance Integral Int where
173 toInteger i = int2Integer i -- give back a full-blown Integer
175 a `quot` 0 = divZeroError
176 a `quot` b = a `quotInt` b
178 a `rem` 0 = divZeroError
179 a `rem` b = a `remInt` b
181 a `div` 0 = divZeroError
182 a `div` b = a `divInt` b
184 a `mod` 0 = divZeroError
185 a `mod` b = a `modInt` b
187 a `quotRem` 0 = divZeroError
188 a `quotRem` b = a `quotRemInt` b
190 a `divMod` 0 = divZeroError
191 a `divMod` b = a `divModInt` b
195 %*********************************************************
197 \subsection{Instances for @Integer@}
199 %*********************************************************
202 instance Real Integer where
205 instance Integral Integer where
208 a `quot` 0 = divZeroError
209 n `quot` d = n `quotInteger` d
211 a `rem` 0 = divZeroError
212 n `rem` d = n `remInteger` d
214 a `divMod` 0 = divZeroError
215 a `divMod` b = a `divModInteger` b
217 a `quotRem` 0 = divZeroError
218 a `quotRem` b = a `quotRemInteger` b
220 -- use the defaults for div & mod
224 %*********************************************************
226 \subsection{Instances for @Ratio@}
228 %*********************************************************
231 instance (Integral a) => Ord (Ratio a) where
232 {-# SPECIALIZE instance Ord Rational #-}
233 (x:%y) <= (x':%y') = x * y' <= x' * y
234 (x:%y) < (x':%y') = x * y' < x' * y
236 instance (Integral a) => Num (Ratio a) where
237 {-# SPECIALIZE instance Num Rational #-}
238 (x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
239 (x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
240 (x:%y) * (x':%y') = reduce (x * x') (y * y')
241 negate (x:%y) = (-x) :% y
242 abs (x:%y) = abs x :% y
243 signum (x:%_) = signum x :% 1
244 fromInteger x = fromInteger x :% 1
246 instance (Integral a) => Fractional (Ratio a) where
247 {-# SPECIALIZE instance Fractional Rational #-}
248 (x:%y) / (x':%y') = (x*y') % (y*x')
250 fromRational (x:%y) = fromInteger x :% fromInteger y
252 instance (Integral a) => Real (Ratio a) where
253 {-# SPECIALIZE instance Real Rational #-}
254 toRational (x:%y) = toInteger x :% toInteger y
256 instance (Integral a) => RealFrac (Ratio a) where
257 {-# SPECIALIZE instance RealFrac Rational #-}
258 properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
259 where (q,r) = quotRem x y
261 instance (Integral a) => Show (Ratio a) where
262 {-# SPECIALIZE instance Show Rational #-}
263 showsPrec p (x:%y) = showParen (p > ratioPrec) $
264 showsPrec ratioPrec1 x .
266 showsPrec ratioPrec1 y
268 instance (Integral a) => Enum (Ratio a) where
269 {-# SPECIALIZE instance Enum Rational #-}
273 toEnum n = fromInteger (int2Integer n) :% 1
274 fromEnum = fromInteger . truncate
276 enumFrom = numericEnumFrom
277 enumFromThen = numericEnumFromThen
278 enumFromTo = numericEnumFromTo
279 enumFromThenTo = numericEnumFromThenTo
283 %*********************************************************
285 \subsection{Coercions}
287 %*********************************************************
290 fromIntegral :: (Integral a, Num b) => a -> b
291 fromIntegral = fromInteger . toInteger
294 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
297 realToFrac :: (Real a, Fractional b) => a -> b
298 realToFrac = fromRational . toRational
301 "realToFrac/Int->Int" realToFrac = id :: Int -> Int
305 %*********************************************************
307 \subsection{Overloaded numeric functions}
309 %*********************************************************
312 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
313 showSigned showPos p x
314 | x < 0 = showParen (p > 6) (showChar '-' . showPos (-x))
315 | otherwise = showPos x
317 even, odd :: (Integral a) => a -> Bool
318 even n = n `rem` 2 == 0
321 -------------------------------------------------------
322 {-# SPECIALISE (^) ::
323 Integer -> Integer -> Integer,
324 Integer -> Int -> Integer,
325 Int -> Int -> Int #-}
326 (^) :: (Num a, Integral b) => a -> b -> a
328 x ^ n | n > 0 = f x (n-1) x
330 f a d y = g a d where
331 g b i | even i = g (b*b) (i `quot` 2)
332 | otherwise = f b (i-1) (b*y)
333 _ ^ _ = error "Prelude.^: negative exponent"
335 {-# SPECIALISE (^^) ::
336 Rational -> Int -> Rational #-}
337 (^^) :: (Fractional a, Integral b) => a -> b -> a
338 x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
341 -------------------------------------------------------
342 gcd :: (Integral a) => a -> a -> a
343 gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
344 gcd x y = gcd' (abs x) (abs y)
346 gcd' a b = gcd' b (a `rem` b)
348 lcm :: (Integral a) => a -> a -> a
349 {-# SPECIALISE lcm :: Int -> Int -> Int #-}
352 lcm x y = abs ((x `quot` (gcd x y)) * y)
356 "gcd/Int->Int->Int" gcd = gcdInt
357 "gcd/Integer->Integer->Integer" gcd = gcdInteger
358 "lcm/Integer->Integer->Integer" lcm = lcmInteger
361 integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
362 integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]
364 integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
365 integralEnumFromThen n1 n2
366 | i_n2 >= i_n1 = map fromInteger [i_n1, i_n2 .. toInteger (maxBound `asTypeOf` n1)]
367 | otherwise = map fromInteger [i_n1, i_n2 .. toInteger (minBound `asTypeOf` n1)]
372 integralEnumFromTo :: Integral a => a -> a -> [a]
373 integralEnumFromTo n m = map fromInteger [toInteger n .. toInteger m]
375 integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
376 integralEnumFromThenTo n1 n2 m
377 = map fromInteger [toInteger n1, toInteger n2 .. toInteger m]