2 -- (c) The University of Glasgow 2002-2006
4 -- The IO Monad with an environment
8 IOEnv, -- Instance of Monad
10 -- Standard combinators, specialised
11 returnM, thenM, thenM_, failM, failWithM,
12 mappM, mappM_, mapSndM, sequenceM, sequenceM_,
14 mapAndUnzipM, mapAndUnzip3M,
15 checkM, ifM, zipWithM, zipWithM_,
17 -- Getting at the environment
18 getEnv, setEnv, updEnv,
20 runIOEnv, unsafeInterleaveM,
21 tryM, tryAllM, tryMostM, fixM,
25 IORef, newMutVar, readMutVar, writeMutVar, updMutVar
27 #include "HsVersions.h"
29 import Panic ( try, tryUser, tryMost, Exception(..) )
31 import Data.IORef ( IORef, newIORef, readIORef, writeIORef )
32 import System.IO.Unsafe ( unsafeInterleaveIO )
33 import System.IO ( fixIO )
36 ----------------------------------------------------------------------
37 -- Defining the monad type
38 ----------------------------------------------------------------------
41 newtype IOEnv env a = IOEnv (env -> IO a)
44 instance Monad (IOEnv m) where
48 fail s = failM -- Ignore the string
50 returnM :: a -> IOEnv env a
51 returnM a = IOEnv (\ env -> return a)
53 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
54 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
57 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
58 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
61 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
63 failWithM :: String -> IOEnv env a
64 failWithM s = IOEnv (\ env -> ioError (userError s))
68 ----------------------------------------------------------------------
69 -- Fundmantal combinators specific to the monad
70 ----------------------------------------------------------------------
73 ---------------------------
74 runIOEnv :: env -> IOEnv env a -> IO a
75 runIOEnv env (IOEnv m) = m env
78 ---------------------------
80 -- Aargh! Not inlining fixTc alleviates a space leak problem.
81 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
82 -- shown the definition of fixTc, it occasionally transforms the code
83 -- in such a way that the code generator doesn't spot the selector
86 fixM :: (a -> IOEnv env a) -> IOEnv env a
87 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
90 ---------------------------
91 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
92 -- Reflect UserError exceptions into IOEnv monad
93 -- The idea is that errors in the program being compiled will give rise
94 -- to UserErrors. But, say, pattern-match failures in GHC itself should
95 -- not be caught here, else they'll be reported as errors in the program
97 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
99 tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
100 -- Catch *all* exceptions
101 -- This is used when running a Template-Haskell splice, when
102 -- even a pattern-match failure is a programmer error
103 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
105 tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
106 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
108 ---------------------------
109 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
110 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
113 ----------------------------------------------------------------------
114 -- Accessing input/output
115 ----------------------------------------------------------------------
117 ioToIOEnv :: IO a -> IOEnv env a
118 ioToIOEnv io = IOEnv (\ env -> io)
120 newMutVar :: a -> IOEnv env (IORef a)
121 newMutVar val = IOEnv (\ env -> newIORef val)
123 writeMutVar :: IORef a -> a -> IOEnv env ()
124 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
126 readMutVar :: IORef a -> IOEnv env a
127 readMutVar var = IOEnv (\ env -> readIORef var)
129 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
130 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
133 ----------------------------------------------------------------------
134 -- Accessing the environment
135 ----------------------------------------------------------------------
137 getEnv :: IOEnv env env
138 {-# INLINE getEnv #-}
139 getEnv = IOEnv (\ env -> return env)
141 setEnv :: env' -> IOEnv env' a -> IOEnv env a
142 {-# INLINE setEnv #-}
143 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
145 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
146 {-# INLINE updEnv #-}
147 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
150 ----------------------------------------------------------------------
151 -- Standard combinators, but specialised for this monad
153 ----------------------------------------------------------------------
155 mappM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
156 mappM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
157 mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)]
158 -- Funny names to avoid clash with Prelude
159 sequenceM :: [IOEnv env a] -> IOEnv env [a]
160 sequenceM_ :: [IOEnv env a] -> IOEnv env ()
161 foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a
162 foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a
163 mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c])
164 mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
165 checkM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is False
166 ifM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is True
168 mappM f [] = return []
169 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
171 mapSndM f [] = return []
172 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
174 mappM_ f [] = return ()
175 mappM_ f (x:xs) = f x >> mappM_ f xs
177 zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
178 zipWithM f [] bs = return []
179 zipWithM f as [] = return []
180 zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) }
182 zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
183 zipWithM_ f [] bs = return ()
184 zipWithM_ f as [] = return ()
185 zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs }
187 sequenceM [] = return []
188 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
190 sequenceM_ [] = return ()
191 sequenceM_ (x:xs) = do { x; sequenceM_ xs }
193 foldlM k z [] = return z
194 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
196 foldrM k z [] = return z
197 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
199 mapAndUnzipM f [] = return ([],[])
200 mapAndUnzipM f (x:xs) = do { (r,s) <- f x;
201 (rs,ss) <- mapAndUnzipM f xs;
202 return (r:rs, s:ss) }
204 mapAndUnzip3M f [] = return ([],[], [])
205 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x;
206 (rs,ss,ts) <- mapAndUnzip3M f xs;
207 return (r:rs, s:ss, t:ts) }
209 checkM True err = return ()
210 checkM False err = do { err; return () }
212 ifM True do_it = do { do_it; return () }
213 ifM False do_it = return ()