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
31 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
33 import System.IO.Unsafe ( unsafeInterleaveIO )
34 import System.IO ( fixIO )
38 ----------------------------------------------------------------------
39 -- Defining the monad type
40 ----------------------------------------------------------------------
43 newtype IOEnv env a = IOEnv (env -> IO a)
45 unIOEnv :: IOEnv env a -> (env -> IO a)
48 instance Monad (IOEnv m) where
52 fail _ = failM -- Ignore the string
54 instance Applicative (IOEnv m) where
56 IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
58 instance Functor (IOEnv m) where
59 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
61 returnM :: a -> IOEnv env a
62 returnM a = IOEnv (\ _ -> return a)
64 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
65 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
68 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
69 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
72 failM = IOEnv (\ _ -> throwIO IOEnvFailure)
74 failWithM :: String -> IOEnv env a
75 failWithM s = IOEnv (\ _ -> ioError (userError s))
77 data IOEnvFailure = IOEnvFailure
80 instance Show IOEnvFailure where
81 show IOEnvFailure = "IOEnv failure"
83 instance Exception IOEnvFailure
85 ----------------------------------------------------------------------
86 -- Fundmantal combinators specific to the monad
87 ----------------------------------------------------------------------
90 ---------------------------
91 runIOEnv :: env -> IOEnv env a -> IO a
92 runIOEnv env (IOEnv m) = m env
95 ---------------------------
97 -- Aargh! Not inlining fixTc alleviates a space leak problem.
98 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
99 -- shown the definition of fixTc, it occasionally transforms the code
100 -- in such a way that the code generator doesn't spot the selector
103 fixM :: (a -> IOEnv env a) -> IOEnv env a
104 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
107 ---------------------------
108 tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
109 -- Reflect UserError exceptions (only) into IOEnv monad
110 -- Other exceptions are not caught; they are simply propagated as exns
112 -- The idea is that errors in the program being compiled will give rise
113 -- to UserErrors. But, say, pattern-match failures in GHC itself should
114 -- not be caught here, else they'll be reported as errors in the program
116 tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
118 tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
119 tryIOEnvFailure = try
121 -- XXX We shouldn't be catching everything, e.g. timeouts
122 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
123 -- Catch *all* exceptions
124 -- This is used when running a Template-Haskell splice, when
125 -- even a pattern-match failure is a programmer error
126 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
128 tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
129 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
131 ---------------------------
132 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
133 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
136 ----------------------------------------------------------------------
138 ----------------------------------------------------------------------
140 -- For use if the user has imported Control.Monad.Error from MTL
141 -- Requires UndecidableInstances
142 #if __GLASGOW_HASKELL__ > 606
143 -- for some reason, this doesn't compile with GHC 6.6:
144 -- utils/IOEnv.hs:144:33:
145 -- No instance for (MonadPlus IO)
146 -- arising from use of `mplus' at utils/IOEnv.hs:144:33-67
147 instance MonadPlus IO => MonadPlus (IOEnv env) where
148 mzero = IOEnv (const mzero)
149 m `mplus` n = IOEnv (\env -> unIOEnv m env `mplus` unIOEnv n env)
152 ----------------------------------------------------------------------
153 -- Accessing input/output
154 ----------------------------------------------------------------------
156 instance MonadIO (IOEnv env) where
157 liftIO io = IOEnv (\ _ -> io)
159 newMutVar :: a -> IOEnv env (IORef a)
160 newMutVar val = liftIO (newIORef val)
162 writeMutVar :: IORef a -> a -> IOEnv env ()
163 writeMutVar var val = liftIO (writeIORef var val)
165 readMutVar :: IORef a -> IOEnv env a
166 readMutVar var = liftIO (readIORef var)
168 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
169 updMutVar var upd = liftIO (modifyIORef var upd)
172 ----------------------------------------------------------------------
173 -- Accessing the environment
174 ----------------------------------------------------------------------
176 getEnv :: IOEnv env env
177 {-# INLINE getEnv #-}
178 getEnv = IOEnv (\ env -> return env)
180 -- | Perform a computation with a different environment
181 setEnv :: env' -> IOEnv env' a -> IOEnv env a
182 {-# INLINE setEnv #-}
183 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
185 -- | Perform a computation with an altered environment
186 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
187 {-# INLINE updEnv #-}
188 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
191 ----------------------------------------------------------------------
192 -- Standard combinators, but specialised for this monad
194 ----------------------------------------------------------------------
196 -- {-# SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
197 -- {-# SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
198 -- {-# SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
199 -- {-# SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
200 -- {-# SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
201 -- {-# SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
202 -- {-# SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
203 -- {-# SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
204 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
205 -- {-# SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
206 -- {-# SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
207 -- {-# SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
208 -- {-# SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
209 -- {-# SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}