[project @ 2000-05-11 13:15:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / Prelude.lhs
index 8dcb1fe..237db71 100644 (file)
@@ -74,6 +74,7 @@ import PrelList
 #ifndef USE_REPORT_PRELUDE
      hiding ( takeUInt_append )
 #endif
+import PrelIO
 import PrelIOBase
 import PrelException
 import PrelRead
@@ -181,91 +182,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}
-
-
-%*********************************************************
-%*                                                      *
-\subsection{Standard IO}
-%*                                                      *
-%*********************************************************
-
-The Prelude has from Day 1 provided a collection of common
-IO functions. We define these here, but let the Prelude
-export them.
-
-\begin{code}
-putChar         :: Char -> IO ()
-putChar c       =  hPutChar stdout c
-
-putStr          :: String -> IO ()
-putStr s        =  hPutStr stdout s
-
-putStrLn        :: String -> IO ()
-putStrLn s      =  do putStr s
-                      putChar '\n'
-
-print           :: Show a => a -> IO ()
-print x         =  putStrLn (show x)
-
-getChar         :: IO Char
-getChar         =  hGetChar stdin
-
-getLine         :: IO String
-getLine         =  hGetLine stdin
-            
-getContents     :: IO String
-getContents     =  hGetContents stdin
-
-interact        ::  (String -> String) -> IO ()
-interact f      =   do s <- getContents
-                       putStr (f s)
-
-readFile        :: FilePath -> IO String
-readFile name  =  openFile name ReadMode >>= hGetContents
-
-writeFile       :: FilePath -> String -> IO ()
-writeFile name str = do
-    hdl <- openFile name WriteMode
-    hPutStr hdl str
-    hClose hdl
-
-appendFile      :: FilePath -> String -> IO ()
-appendFile name str = do
-    hdl <- openFile name AppendMode
-    hPutStr hdl str
-    hClose hdl
 
-readLn          :: Read a => IO a
-readLn          =  do l <- getLine
-                      r <- readIO l
-                      return r
+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}