Move the instances of Functor and Monad IO to GHC.Base, to avoid orphans
authorSimon Marlow <marlowsd@gmail.com>
Wed, 22 Jul 2009 10:21:30 +0000 (10:21 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Wed, 22 Jul 2009 10:21:30 +0000 (10:21 +0000)
GHC/Base.lhs
GHC/IO.hs
GHC/IO.hs-boot [new file with mode: 0644]
GHC/IOBase.hs
GHC/Weak.lhs

index 449a861..71876d3 100644 (file)
@@ -103,6 +103,7 @@ import GHC.Ordering
 import GHC.Prim
 import {-# SOURCE #-} GHC.Show
 import {-# SOURCE #-} GHC.Err
+import {-# SOURCE #-} GHC.IO (failIO)
 
 -- These two are not strictly speaking required by this module, but they are
 -- implicit dependencies whenever () or tuples are mentioned, so adding them
@@ -710,6 +711,38 @@ asTypeOf                =  const
 
 %*********************************************************
 %*                                                      *
+\subsection{@Functor@ and @Monad@ instances for @IO@}
+%*                                                      *
+%*********************************************************
+
+\begin{code}
+instance  Functor IO where
+   fmap f x = x >>= (return . f)
+
+instance  Monad IO  where
+    {-# INLINE return #-}
+    {-# INLINE (>>)   #-}
+    {-# INLINE (>>=)  #-}
+    m >> k    = m >>= \ _ -> k
+    return    = returnIO
+    (>>=)     = bindIO
+    fail s    = GHC.IO.failIO s
+
+returnIO :: a -> IO a
+returnIO x = IO $ \ s -> (# s, x #)
+
+bindIO :: IO a -> (a -> IO b) -> IO b
+bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k a) new_s
+
+thenIO :: IO a -> IO b -> IO b
+thenIO (IO m) k = IO $ \ s -> case m s of (# new_s, _ #) -> unIO k new_s
+
+unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #))
+unIO (IO a) = a
+\end{code}
+
+%*********************************************************
+%*                                                      *
 \subsection{@getTag@}
 %*                                                      *
 %*********************************************************
index d1598ac..f2ccc7d 100644 (file)
--- a/GHC/IO.hs
+++ b/GHC/IO.hs
@@ -17,7 +17,7 @@
 
 -- #hide
 module GHC.IO (
-    IO(..), unIO, failIO, liftIO, bindIO, thenIO, returnIO, 
+    IO(..), unIO, failIO, liftIO,
     unsafePerformIO, unsafeInterleaveIO,
     unsafeDupablePerformIO, unsafeDupableInterleaveIO,
     noDuplicate,
@@ -65,40 +65,9 @@ Libraries - parts of hslibs/lang.
 --SDM
 -}
 
-unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #))
-unIO (IO a) = a
-
-instance  Functor IO where
-   fmap f x = x >>= (return . f)
-
-instance  Monad IO  where
-    {-# INLINE return #-}
-    {-# INLINE (>>)   #-}
-    {-# INLINE (>>=)  #-}
-    m >> k      =  m >>= \ _ -> k
-    return x    = returnIO x
-
-    m >>= k     = bindIO m k
-    fail s      = failIO s
-
 liftIO :: IO a -> State# RealWorld -> STret RealWorld a
 liftIO (IO m) = \s -> case m s of (# s', r #) -> STret s' r
 
-bindIO :: IO a -> (a -> IO b) -> IO b
-bindIO (IO m) k = IO ( \ s ->
-  case m s of 
-    (# new_s, a #) -> unIO (k a) new_s
-  )
-
-thenIO :: IO a -> IO b -> IO b
-thenIO (IO m) k = IO ( \ s ->
-  case m s of 
-    (# new_s, _ #) -> unIO k new_s
-  )
-
-returnIO :: a -> IO a
-returnIO x = IO (\ s -> (# s, x #))
-
 failIO :: String -> IO a
 failIO s = IO (raiseIO# (toException (userError s)))
 
diff --git a/GHC/IO.hs-boot b/GHC/IO.hs-boot
new file mode 100644 (file)
index 0000000..703fad5
--- /dev/null
@@ -0,0 +1,5 @@
+module GHC.IO where
+
+import GHC.Types
+
+failIO :: [Char] -> IO a
index cbadc87..dca72c0 100644 (file)
@@ -50,6 +50,7 @@ module GHC.IOBase {-# DEPRECATED "use GHC.IO instead" #-} (
     blockedOnDeadMVar, blockedIndefinitely
   ) where
 
+import GHC.Base
 import GHC.Exception
 import GHC.IO
 import GHC.IO.Handle.Types
index 23a3b01..4897123 100644 (file)
@@ -20,7 +20,6 @@ module GHC.Weak where
 
 import GHC.Base
 import Data.Maybe
-import GHC.IO          ( unIO )
 import Data.Typeable
 
 {-|