[project @ 2001-02-28 00:01:01 by qrczak]
[ghc-hetmet.git] / ghc / lib / std / PrelReal.lhs
index 530f123..6748108 100644 (file)
@@ -1,5 +1,7 @@
+% ------------------------------------------------------------------------------
+% $Id: PrelReal.lhs,v 1.10 2001/02/28 00:01:03 qrczak Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1996
+% (c) The University of Glasgow, 1994-2000
 %
 
 \section[PrelReal]{Module @PrelReal@}
@@ -87,7 +89,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
@@ -126,6 +127,27 @@ class  (Real a, Fractional a) => RealFrac a  where
 \end{code}
 
 
+These 'numeric' enumerations come straight from the Report
+
+\begin{code}
+numericEnumFrom                :: (Fractional a) => a -> [a]
+numericEnumFrom                =  iterate (+1)
+
+numericEnumFromThen    :: (Fractional a) => a -> a -> [a]
+numericEnumFromThen n m        =  iterate (+(m-n)) n
+
+numericEnumFromTo       :: (Ord a, Fractional a) => a -> a -> [a]
+numericEnumFromTo n m   = takeWhile (<= m + 1/2) (numericEnumFrom n)
+
+numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
+numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
+                               where
+                                mid = (e2 - e1) / 2
+                                pred | e2 > e1   = (<= e3 + mid)
+                                     | otherwise = (>= e3 + mid)
+\end{code}
+
+
 %*********************************************************
 %*                                                     *
 \subsection{Instances for @Int@}
@@ -138,7 +160,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
@@ -168,7 +189,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
@@ -189,10 +209,12 @@ instance  Integral Integer where
 
 \begin{code}
 instance  (Integral a) => Ord (Ratio a)  where
+    {-# SPECIALIZE instance Ord Rational #-}
     (x:%y) <= (x':%y') =  x * y' <= x' * y
     (x:%y) <  (x':%y') =  x * y' <  x' * y
 
 instance  (Integral a) => Num (Ratio a)  where
+    {-# SPECIALIZE instance Num Rational #-}
     (x:%y) + (x':%y')  =  reduce (x*y' + x'*y) (y*y')
     (x:%y) - (x':%y')  =  reduce (x*y' - x'*y) (y*y')
     (x:%y) * (x':%y')  =  reduce (x * x') (y * y')
@@ -202,18 +224,22 @@ instance  (Integral a)    => Num (Ratio a)  where
     fromInteger x      =  fromInteger x :% 1
 
 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
     fromRational (x:%y) =  fromInteger x :% fromInteger y
 
 instance  (Integral a) => Real (Ratio a)  where
+    {-# SPECIALIZE instance Real Rational #-}
     toRational (x:%y)  =  toInteger x :% toInteger y
 
 instance  (Integral a) => RealFrac (Ratio a)  where
+    {-# SPECIALIZE instance RealFrac Rational #-}
     properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
                          where (q,r) = quotRem x y
 
 instance  (Integral a)  => Show (Ratio a)  where
+    {-# SPECIALIZE instance Show Rational #-}
     showsPrec p (x:%y) =  showParen (p > ratio_prec)
                               (shows x . showString " % " . shows y)
 
@@ -221,28 +247,54 @@ ratio_prec :: Int
 ratio_prec = 7
 
 instance  (Integral a) => Enum (Ratio a)  where
+    {-# SPECIALIZE instance Enum Rational #-}
     succ x             =  x + 1
     pred x             =  x - 1
 
-    toEnum n            =  fromInt n :% 1
+    toEnum n            =  fromInteger (int2Integer n) :% 1
     fromEnum            =  fromInteger . truncate
 
-    enumFrom           =  bounded_iterator True (1)
-    enumFromThen n m   =  bounded_iterator (diff >= 0) diff n 
-                       where diff = m - n
-
-bounded_iterator :: (Ord a, Num a) => Bool -> a -> a -> [a]
-bounded_iterator inc step v 
-   | inc      && v > new_v = [v]  -- oflow
-   | not inc  && v < new_v = [v]  -- uflow
-   | otherwise             = v : bounded_iterator inc step new_v
-  where
-   new_v = v + step
+    enumFrom           =  numericEnumFrom
+    enumFromThen       =  numericEnumFromThen
+    enumFromTo         =  numericEnumFromTo
+    enumFromThenTo     =  numericEnumFromThenTo
 \end{code}
 
 
 %*********************************************************
 %*                                                     *
+\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}
 %*                                                     *
 %*********************************************************
@@ -271,7 +323,7 @@ x ^ n | n > 0       =  f x (n-1) x
                                         | otherwise = f b (i-1) (b*y)
 _ ^ _          = error "Prelude.^: negative exponent"
 
-{- SPECIALISE (^^) ::
+{-# SPECIALISE (^^) ::
        Rational -> Int -> Rational #-}
 (^^)           :: (Fractional a, Integral b) => a -> b -> a
 x ^^ n         =  if n >= 0 then x^n else recip (x^(negate n))
@@ -292,8 +344,19 @@ lcm x y            =  abs ((x `quot` (gcd x y)) * y)
 
 
 {-# RULES
-"Int.gcd"      forall a b . gcd  a b = gcdInt a b
-"Integer.gcd"  forall a b . gcd  a b = gcdInteger  a b
-"Integer.lcm"  forall a b . lcm  a b = lcmInteger  a b
+"gcd/Int->Int->Int"             gcd = gcdInt
+"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
 \end{code}