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
26 import Panic ( try, tryUser, tryMost, Exception(..) )
28 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
29 import System.IO.Unsafe ( unsafeInterleaveIO )
30 import System.IO ( fixIO )
33 ----------------------------------------------------------------------
34 -- Defining the monad type
35 ----------------------------------------------------------------------
38 newtype IOEnv env a = IOEnv (env -> IO a)
40 unIOEnv :: IOEnv env a -> (env -> IO a)
43 instance Monad (IOEnv m) where
47 fail _ = failM -- Ignore the string
49 instance Applicative (IOEnv m) where
51 IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
53 instance Functor (IOEnv m) where
54 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
56 returnM :: a -> IOEnv env a
57 returnM a = IOEnv (\ _ -> return a)
59 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
60 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
63 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
64 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
67 failM = IOEnv (\ _ -> ioError (userError "IOEnv failure"))
69 failWithM :: String -> IOEnv env a
70 failWithM s = IOEnv (\ _ -> ioError (userError s))
74 ----------------------------------------------------------------------
75 -- Fundmantal combinators specific to the monad
76 ----------------------------------------------------------------------
79 ---------------------------
80 runIOEnv :: env -> IOEnv env a -> IO a
81 runIOEnv env (IOEnv m) = m env
84 ---------------------------
86 -- Aargh! Not inlining fixTc alleviates a space leak problem.
87 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
88 -- shown the definition of fixTc, it occasionally transforms the code
89 -- in such a way that the code generator doesn't spot the selector
92 fixM :: (a -> IOEnv env a) -> IOEnv env a
93 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
96 ---------------------------
97 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
98 -- Reflect UserError exceptions (only) into IOEnv monad
99 -- Other exceptions are not caught; they are simply propagated as exns
101 -- The idea is that errors in the program being compiled will give rise
102 -- to UserErrors. But, say, pattern-match failures in GHC itself should
103 -- not be caught here, else they'll be reported as errors in the program
105 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
107 tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
108 -- Catch *all* exceptions
109 -- This is used when running a Template-Haskell splice, when
110 -- even a pattern-match failure is a programmer error
111 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
113 tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
114 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
116 ---------------------------
117 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
118 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
121 ----------------------------------------------------------------------
122 -- Accessing input/output
123 ----------------------------------------------------------------------
125 instance MonadIO (IOEnv env) where
126 liftIO io = IOEnv (\ _ -> io)
128 newMutVar :: a -> IOEnv env (IORef a)
129 newMutVar val = liftIO (newIORef val)
131 writeMutVar :: IORef a -> a -> IOEnv env ()
132 writeMutVar var val = liftIO (writeIORef var val)
134 readMutVar :: IORef a -> IOEnv env a
135 readMutVar var = liftIO (readIORef var)
137 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
138 updMutVar var upd = liftIO (modifyIORef var upd)
141 ----------------------------------------------------------------------
142 -- Accessing the environment
143 ----------------------------------------------------------------------
145 getEnv :: IOEnv env env
146 {-# INLINE getEnv #-}
147 getEnv = IOEnv (\ env -> return env)
149 -- | Perform a computation with a different environment
150 setEnv :: env' -> IOEnv env' a -> IOEnv env a
151 {-# INLINE setEnv #-}
152 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
154 -- | Perform a computation with an altered environment
155 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
156 {-# INLINE updEnv #-}
157 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
160 ----------------------------------------------------------------------
161 -- Standard combinators, but specialised for this monad
163 ----------------------------------------------------------------------
165 {-# -- SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
166 {-# -- SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
167 {-# -- SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
168 {-# -- SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
169 {-# -- SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
170 {-# -- SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
171 {-# -- SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
172 {-# -- SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
173 {-# -- SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
174 {-# -- SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
175 {-# -- SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
176 {-# -- SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
177 {-# -- SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
178 {-# -- SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}