2 -- (c) The University of Glasgow 2002-2006
4 -- The IO Monad with an environment
6 {-# LANGUAGE UndecidableInstances #-}
9 IOEnv, -- Instance of Monad
18 -- Getting at the environment
19 getEnv, setEnv, updEnv,
21 runIOEnv, unsafeInterleaveM,
22 tryM, tryAllM, tryMostM, fixM,
25 IORef, newMutVar, readMutVar, writeMutVar, updMutVar,
26 atomicUpdMutVar, atomicUpdMutVar'
32 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef,
35 import System.IO.Unsafe ( unsafeInterleaveIO )
36 import System.IO ( fixIO )
40 ----------------------------------------------------------------------
41 -- Defining the monad type
42 ----------------------------------------------------------------------
45 newtype IOEnv env a = IOEnv (env -> IO a)
47 unIOEnv :: IOEnv env a -> (env -> IO a)
50 instance Monad (IOEnv m) where
54 fail _ = failM -- Ignore the string
56 instance Applicative (IOEnv m) where
58 IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
60 instance Functor (IOEnv m) where
61 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
63 returnM :: a -> IOEnv env a
64 returnM a = IOEnv (\ _ -> return a)
66 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
67 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
70 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
71 thenM_ (IOEnv m) f = IOEnv (\ env -> do { _ <- m env ; unIOEnv f env })
74 failM = IOEnv (\ _ -> throwIO IOEnvFailure)
76 failWithM :: String -> IOEnv env a
77 failWithM s = IOEnv (\ _ -> ioError (userError s))
79 data IOEnvFailure = IOEnvFailure
82 instance Show IOEnvFailure where
83 show IOEnvFailure = "IOEnv failure"
85 instance Exception IOEnvFailure
87 ----------------------------------------------------------------------
88 -- Fundmantal combinators specific to the monad
89 ----------------------------------------------------------------------
92 ---------------------------
93 runIOEnv :: env -> IOEnv env a -> IO a
94 runIOEnv env (IOEnv m) = m env
97 ---------------------------
99 -- Aargh! Not inlining fixTc alleviates a space leak problem.
100 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
101 -- shown the definition of fixTc, it occasionally transforms the code
102 -- in such a way that the code generator doesn't spot the selector
105 fixM :: (a -> IOEnv env a) -> IOEnv env a
106 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
109 ---------------------------
110 tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
111 -- Reflect UserError exceptions (only) into IOEnv monad
112 -- Other exceptions are not caught; they are simply propagated as exns
114 -- The idea is that errors in the program being compiled will give rise
115 -- to UserErrors. But, say, pattern-match failures in GHC itself should
116 -- not be caught here, else they'll be reported as errors in the program
118 tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
120 tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
121 tryIOEnvFailure = try
123 -- XXX We shouldn't be catching everything, e.g. timeouts
124 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
125 -- Catch *all* exceptions
126 -- This is used when running a Template-Haskell splice, when
127 -- even a pattern-match failure is a programmer error
128 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
130 tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
131 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
133 ---------------------------
134 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
135 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
138 ----------------------------------------------------------------------
140 ----------------------------------------------------------------------
142 -- For use if the user has imported Control.Monad.Error from MTL
143 -- Requires UndecidableInstances
144 instance MonadPlus IO => MonadPlus (IOEnv env) where
145 mzero = IOEnv (const mzero)
146 m `mplus` n = IOEnv (\env -> unIOEnv m env `mplus` unIOEnv n env)
148 ----------------------------------------------------------------------
149 -- Accessing input/output
150 ----------------------------------------------------------------------
152 instance MonadIO (IOEnv env) where
153 liftIO io = IOEnv (\ _ -> io)
155 newMutVar :: a -> IOEnv env (IORef a)
156 newMutVar val = liftIO (newIORef val)
158 writeMutVar :: IORef a -> a -> IOEnv env ()
159 writeMutVar var val = liftIO (writeIORef var val)
161 readMutVar :: IORef a -> IOEnv env a
162 readMutVar var = liftIO (readIORef var)
164 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
165 updMutVar var upd = liftIO (modifyIORef var upd)
167 -- | Atomically update the reference. Does not force the evaluation of the
168 -- new variable contents. For strict update, use 'atomicUpdMutVar''.
169 atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
170 atomicUpdMutVar var upd = liftIO (atomicModifyIORef var upd)
172 -- | Strict variant of 'atomicUpdMutVar'.
173 atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
174 atomicUpdMutVar' var upd = do
175 r <- atomicUpdMutVar var upd
176 _ <- liftIO . evaluate =<< readMutVar var
179 ----------------------------------------------------------------------
180 -- Accessing the environment
181 ----------------------------------------------------------------------
183 getEnv :: IOEnv env env
184 {-# INLINE getEnv #-}
185 getEnv = IOEnv (\ env -> return env)
187 -- | Perform a computation with a different environment
188 setEnv :: env' -> IOEnv env' a -> IOEnv env a
189 {-# INLINE setEnv #-}
190 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
192 -- | Perform a computation with an altered environment
193 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
194 {-# INLINE updEnv #-}
195 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
198 ----------------------------------------------------------------------
199 -- Standard combinators, but specialised for this monad
201 ----------------------------------------------------------------------
203 -- {-# SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
204 -- {-# SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
205 -- {-# SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
206 -- {-# SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
207 -- {-# SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
208 -- {-# SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
209 -- {-# SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
210 -- {-# SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
211 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
212 -- {-# SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
213 -- {-# SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
214 -- {-# SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
215 -- {-# SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
216 -- {-# SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}