[project @ 2002-02-12 11:44:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelReal.lhs
index 1745d12..cd2c1c0 100644 (file)
@@ -1,5 +1,5 @@
 % ------------------------------------------------------------------------------
-% $Id: PrelReal.lhs,v 1.7 2000/12/16 17:46:57 qrczak Exp $
+% $Id: PrelReal.lhs,v 1.16 2001/09/26 16:27:04 simonpj Exp $
 %
 % (c) The University of Glasgow, 1994-2000
 %
@@ -62,6 +62,7 @@ their greatest common divisor.
 
 \begin{code}
 reduce ::  (Integral a) => a -> a -> Ratio a
+{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
 reduce _ 0             =  error "Ratio.%: zero denominator"
 reduce x y             =  (x `quot` d) :% (y `quot` d)
                           where d = gcd x y
@@ -89,7 +90,6 @@ class  (Real a, Enum a) => Integral a  where
     quot, rem, div, mod        :: a -> a -> a
     quotRem, divMod    :: a -> a -> (a,a)
     toInteger          :: a -> Integer
-    toInt              :: a -> Int -- partain: Glasgow extension
 
     n `quot` d         =  q  where (q,_) = quotRem n d
     n `rem` d          =  r  where (_,r) = quotRem n d
@@ -161,7 +161,6 @@ instance  Real Int  where
 
 instance  Integral Int where
     toInteger i = int2Integer i  -- give back a full-blown Integer
-    toInt x    = x
 
     -- Following chks for zero divisor are non-standard (WDP)
     a `quot` b =  if b /= 0
@@ -191,7 +190,6 @@ instance  Real Integer  where
 
 instance  Integral Integer where
     toInteger n             = n
-    toInt n         = integer2Int n
 
     n `quot` d = n `quotInteger` d
     n `rem`  d = n `remInteger`  d
@@ -229,7 +227,7 @@ instance  (Integral a)      => Num (Ratio a)  where
 instance  (Integral a) => Fractional (Ratio a)  where
     {-# SPECIALIZE instance Fractional Rational #-}
     (x:%y) / (x':%y')  =  (x*y') % (y*x')
-    recip (x:%y)       =  if x < 0 then (-y) :% (-x) else y :% x
+    recip (x:%y)       =  y % x
     fromRational (x:%y) =  fromInteger x :% fromInteger y
 
 instance  (Integral a) => Real (Ratio a)  where
@@ -254,7 +252,7 @@ instance  (Integral a)      => Enum (Ratio a)  where
     succ x             =  x + 1
     pred x             =  x - 1
 
-    toEnum n            =  fromInt n :% 1
+    toEnum n            =  fromInteger (int2Integer n) :% 1
     fromEnum            =  fromInteger . truncate
 
     enumFrom           =  numericEnumFrom
@@ -266,6 +264,38 @@ instance  (Integral a)     => Enum (Ratio a)  where
 
 %*********************************************************
 %*                                                     *
+\subsection{Coercions}
+%*                                                     *
+%*********************************************************
+
+\begin{code}
+fromIntegral :: (Integral a, Num b) => a -> b
+fromIntegral = fromInteger . toInteger
+
+{-# RULES
+"fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
+    #-}
+
+realToFrac :: (Real a, Fractional b) => a -> b
+realToFrac = fromRational . toRational
+
+{-# RULES
+"realToFrac/Int->Int" realToFrac = id :: Int -> Int
+    #-}
+
+-- For backward compatibility
+{-# DEPRECATED fromInt "use fromIntegral instead" #-}
+fromInt :: Num a => Int -> a
+fromInt = fromIntegral
+
+-- For backward compatibility
+{-# DEPRECATED toInt "use fromIntegral instead" #-}
+toInt :: Integral a => a -> Int
+toInt = fromIntegral
+\end{code}
+
+%*********************************************************
+%*                                                     *
 \subsection{Overloaded numeric functions}
 %*                                                     *
 %*********************************************************
@@ -319,4 +349,22 @@ lcm x y            =  abs ((x `quot` (gcd x y)) * y)
 "gcd/Integer->Integer->Integer" gcd = gcdInteger
 "lcm/Integer->Integer->Integer" lcm = lcmInteger
  #-}
+
+integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
+integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]
+
+integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
+integralEnumFromThen n1 n2
+  | i_n2 >= i_n1  = map fromInteger [i_n1, i_n2 .. toInteger (maxBound `asTypeOf` n1)]
+  | otherwise     = map fromInteger [i_n1, i_n2 .. toInteger (minBound `asTypeOf` n1)]
+  where
+    i_n1 = toInteger n1
+    i_n2 = toInteger n2
+
+integralEnumFromTo :: Integral a => a -> a -> [a]
+integralEnumFromTo n m = map fromInteger [toInteger n .. toInteger m]
+
+integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
+integralEnumFromThenTo n1 n2 m
+  = map fromInteger [toInteger n1, toInteger n2 .. toInteger m]
 \end{code}