[project @ 2000-10-24 10:36:08 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / Prelude.lhs
index 01e55fc..0f9aa1a 100644 (file)
@@ -1,9 +1,17 @@
+% ------------------------------------------------------------------------------
+% $Id: Prelude.lhs,v 1.22 2000/06/30 13:39:36 simonmar Exp $
+%
+% (c) The University of Glasgow, 1992-2000
+%
+
+\section[Prelude]{Module @Prelude@}
+
 We add the option -fno-implicit-prelude here to tell the reader that
 special names such as () and -> shouldn't be resolved to Prelude.()
 and Prelude.-> (as they are normally). -- SDM 8/10/97
 
 \begin{code}
-{-# OPTIONS -fcompiling-prelude -fno-implicit-prelude #-}
+{-# OPTIONS -fno-implicit-prelude #-}
 
 module Prelude (
 
@@ -182,31 +190,47 @@ mapM_ f as      =  sequence_ (map f as)
 %*********************************************************
 
 \begin{code}
-{-# SPECIALIZE fromIntegral ::
-    Int                -> Rational,
-    Integer    -> Rational,
-    Int        -> Int,
-    Int        -> Integer,
-    Int                -> Float,
-    Int                -> Double,
-    Integer    -> Int,
-    Integer    -> Integer,
-    Integer    -> Float,
-    Integer    -> Double #-}
+{-# RULES
+"fromIntegral/Int->Int"                     fromIntegral   = id :: Int     -> Int
+"fromIntegral/Integer->Integer"             fromIntegral   = id :: Integer -> Integer
+"fromIntegral/Int->Integer"                 fromIntegral   = int2Integer
+"fromIntegral/Integer->Int"                 fromIntegral   = integer2Int
+"fromIntegral/Int->Rational"     forall n . fromIntegral n = int2Integer n :% 1
+"fromIntegral/Integer->Rational" forall n . fromIntegral n = n :% (1 :: Integer)
+"fromIntegral/Int->Float"                   fromIntegral   = int2Float
+"fromIntegral/Int->Double"                  fromIntegral   = int2Double
+"fromIntegral/Integer->Float"    forall n . fromIntegral n = encodeFloat n 0 :: Float
+"fromIntegral/Integer->Double"   forall n . fromIntegral n = encodeFloat n 0 :: Double
+ #-}
 fromIntegral   :: (Integral a, Num b) => a -> b
 fromIntegral   =  fromInteger . toInteger
 
-{-# SPECIALIZE realToFrac ::
-    Double     -> Rational, 
-    Rational   -> Double,
-    Float      -> Rational,
-    Rational   -> Float,
-    Rational   -> Rational,
-    Double     -> Double,
-    Double     -> Float,
-    Float      -> Float,
-    Float      -> Double #-}
+{-# RULES
+"realToFrac/Float->Double"      realToFrac = floatToDouble
+"realToFrac/Double->Float"      realToFrac = doubleToFloat
+"realToFrac/Float->Float"       realToFrac = id      :: Float    -> Float
+"realToFrac/Double->Double"     realToFrac = id      :: Double   -> Double
+"realToFrac/Rational->Rational" realToFrac = id      :: Rational -> Rational
+"realToFrac/Float->Rational"    realToFrac = rf2rat  :: Float    -> Rational
+"realToFrac/Double->Rational"   realToFrac = rf2rat  :: Double   -> Rational
+"realToFrac/Rational->Float"    realToFrac = fromRat :: Rational -> Float
+"realToFrac/Rational->Double"   realToFrac = fromRat :: Rational -> Double
+ #-}
 realToFrac     :: (Real a, Fractional b) => a -> b
 realToFrac     =  fromRational . toRational
-\end{code}
 
+doubleToFloat :: Double -> Float
+doubleToFloat (D# d) = F# (double2Float# d)
+
+floatToDouble :: Float -> Double
+floatToDouble (F# f) = D# (float2Double# f)
+
+{-# SPECIALIZE rf2rat ::
+    Float  -> Rational,
+    Double -> Rational
+ #-}
+rf2rat :: RealFloat a => a -> Rational
+rf2rat x = if n >= 0 then (m * (b ^ n)) :% 1 else m :% (b ^ (-n))
+   where (m,n) = decodeFloat x
+         b     = floatRadix  x
+\end{code}