2 -- (c) The University of Glasgow 2002-2006
4 -- The IO Monad with an environment
8 IOEnv, -- Instance of Monad
16 -- Getting at the environment
17 getEnv, setEnv, updEnv,
19 runIOEnv, unsafeInterleaveM,
20 tryM, tryAllM, tryMostM, fixM,
23 IORef, newMutVar, readMutVar, writeMutVar, updMutVar
29 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
30 import System.IO.Unsafe ( unsafeInterleaveIO )
31 import System.IO ( fixIO )
34 ----------------------------------------------------------------------
35 -- Defining the monad type
36 ----------------------------------------------------------------------
39 newtype IOEnv env a = IOEnv (env -> IO a)
41 unIOEnv :: IOEnv env a -> (env -> IO a)
44 instance Monad (IOEnv m) where
48 fail _ = failM -- Ignore the string
50 instance Applicative (IOEnv m) where
52 IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
54 instance Functor (IOEnv m) where
55 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
57 returnM :: a -> IOEnv env a
58 returnM a = IOEnv (\ _ -> return a)
60 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
61 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
64 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
65 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
68 failM = IOEnv (\ _ -> ioError (userError "IOEnv failure"))
70 failWithM :: String -> IOEnv env a
71 failWithM s = IOEnv (\ _ -> ioError (userError s))
75 ----------------------------------------------------------------------
76 -- Fundmantal combinators specific to the monad
77 ----------------------------------------------------------------------
80 ---------------------------
81 runIOEnv :: env -> IOEnv env a -> IO a
82 runIOEnv env (IOEnv m) = m env
85 ---------------------------
87 -- Aargh! Not inlining fixTc alleviates a space leak problem.
88 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
89 -- shown the definition of fixTc, it occasionally transforms the code
90 -- in such a way that the code generator doesn't spot the selector
93 fixM :: (a -> IOEnv env a) -> IOEnv env a
94 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
97 ---------------------------
98 #if __GLASGOW_HASKELL__ < 609
99 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
101 tryM :: IOEnv env r -> IOEnv env (Either ErrorCall r)
103 -- Reflect UserError exceptions (only) into IOEnv monad
104 -- Other exceptions are not caught; they are simply propagated as exns
106 -- The idea is that errors in the program being compiled will give rise
107 -- to UserErrors. But, say, pattern-match failures in GHC itself should
108 -- not be caught here, else they'll be reported as errors in the program
110 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
112 -- XXX We shouldn't be catching everything, e.g. timeouts
113 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
114 -- Catch *all* exceptions
115 -- This is used when running a Template-Haskell splice, when
116 -- even a pattern-match failure is a programmer error
117 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
119 tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
120 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
122 ---------------------------
123 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
124 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
127 ----------------------------------------------------------------------
128 -- Accessing input/output
129 ----------------------------------------------------------------------
131 instance MonadIO (IOEnv env) where
132 liftIO io = IOEnv (\ _ -> io)
134 newMutVar :: a -> IOEnv env (IORef a)
135 newMutVar val = liftIO (newIORef val)
137 writeMutVar :: IORef a -> a -> IOEnv env ()
138 writeMutVar var val = liftIO (writeIORef var val)
140 readMutVar :: IORef a -> IOEnv env a
141 readMutVar var = liftIO (readIORef var)
143 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
144 updMutVar var upd = liftIO (modifyIORef var upd)
147 ----------------------------------------------------------------------
148 -- Accessing the environment
149 ----------------------------------------------------------------------
151 getEnv :: IOEnv env env
152 {-# INLINE getEnv #-}
153 getEnv = IOEnv (\ env -> return env)
155 -- | Perform a computation with a different environment
156 setEnv :: env' -> IOEnv env' a -> IOEnv env a
157 {-# INLINE setEnv #-}
158 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
160 -- | Perform a computation with an altered environment
161 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
162 {-# INLINE updEnv #-}
163 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
166 ----------------------------------------------------------------------
167 -- Standard combinators, but specialised for this monad
169 ----------------------------------------------------------------------
171 -- {-# SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
172 -- {-# SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
173 -- {-# SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
174 -- {-# SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
175 -- {-# SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
176 -- {-# SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
177 -- {-# SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
178 -- {-# SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
179 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
180 -- {-# SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
181 -- {-# SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
182 -- {-# SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
183 -- {-# SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
184 -- {-# SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}