1 -- (c) The University of Glasgow 2002
3 -- The IO Monad with an environment
7 IOEnv, -- Instance of Monad
9 -- Standard combinators, specialised
10 returnM, thenM, thenM_, failM, failWithM,
11 mappM, mappM_, mapSndM, sequenceM, sequenceM_,
13 mapAndUnzipM, mapAndUnzip3M,
14 checkM, ifM, zipWithM, zipWithM_,
16 -- Getting at the environment
17 getEnv, setEnv, updEnv,
19 runIOEnv, unsafeInterleaveM,
24 IORef, newMutVar, readMutVar, writeMutVar, updMutVar
26 #include "HsVersions.h"
28 import Panic ( try, tryUser, Exception(..) )
29 import DATA_IOREF ( IORef, newIORef, readIORef, writeIORef )
30 import UNSAFE_IO ( unsafeInterleaveIO )
31 import FIX_IO ( fixIO )
34 ----------------------------------------------------------------------
35 -- Defining the monad type
36 ----------------------------------------------------------------------
39 newtype IOEnv env a = IOEnv (env -> IO a)
42 instance Monad (IOEnv m) where
46 fail s = failM -- Ignore the string
48 returnM :: a -> IOEnv env a
49 returnM a = IOEnv (\ env -> return a)
51 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
52 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
55 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
56 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
59 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
61 failWithM :: String -> IOEnv env a
62 failWithM s = IOEnv (\ env -> ioError (userError s))
66 ----------------------------------------------------------------------
67 -- Fundmantal combinators specific to the monad
68 ----------------------------------------------------------------------
71 ---------------------------
72 runIOEnv :: env -> IOEnv env a -> IO a
73 runIOEnv env (IOEnv m) = m env
76 ---------------------------
78 -- Aargh! Not inlining fixTc alleviates a space leak problem.
79 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
80 -- shown the definition of fixTc, it occasionally transforms the code
81 -- in such a way that the code generator doesn't spot the selector
84 fixM :: (a -> IOEnv env a) -> IOEnv env a
85 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
88 ---------------------------
89 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
90 -- Reflect UserError exceptions into IOEnv monad
91 -- The idea is that errors in the program being compiled will give rise
92 -- to UserErrors. But, say, pattern-match failures in GHC itself should
93 -- not be caught here, else they'll be reported as errors in the program
95 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
97 tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
98 -- Catch *all* exceptions
99 -- This is used when running a Template-Haskell splice, when
100 -- even a pattern-match failure is a programmer error
101 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
103 ---------------------------
104 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
105 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
108 ----------------------------------------------------------------------
109 -- Accessing input/output
110 ----------------------------------------------------------------------
112 ioToIOEnv :: IO a -> IOEnv env a
113 ioToIOEnv io = IOEnv (\ env -> io)
115 newMutVar :: a -> IOEnv env (IORef a)
116 newMutVar val = IOEnv (\ env -> newIORef val)
118 writeMutVar :: IORef a -> a -> IOEnv env ()
119 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
121 readMutVar :: IORef a -> IOEnv env a
122 readMutVar var = IOEnv (\ env -> readIORef var)
124 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
125 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
128 ----------------------------------------------------------------------
129 -- Accessing the environment
130 ----------------------------------------------------------------------
132 getEnv :: IOEnv env env
133 {-# INLINE getEnv #-}
134 getEnv = IOEnv (\ env -> return env)
136 setEnv :: env' -> IOEnv env' a -> IOEnv env a
137 {-# INLINE setEnv #-}
138 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
140 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
141 {-# INLINE updEnv #-}
142 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
145 ----------------------------------------------------------------------
146 -- Standard combinators, but specialised for this monad
148 ----------------------------------------------------------------------
150 mappM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
151 mappM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
152 mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)]
153 -- Funny names to avoid clash with Prelude
154 sequenceM :: [IOEnv env a] -> IOEnv env [a]
155 sequenceM_ :: [IOEnv env a] -> IOEnv env ()
156 foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a
157 foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a
158 mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c])
159 mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
160 checkM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is False
161 ifM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is True
163 mappM f [] = return []
164 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
166 mapSndM f [] = return []
167 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
169 mappM_ f [] = return ()
170 mappM_ f (x:xs) = f x >> mappM_ f xs
172 zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
173 zipWithM f [] bs = return []
174 zipWithM f as [] = return []
175 zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) }
177 zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
178 zipWithM_ f [] bs = return ()
179 zipWithM_ f as [] = return ()
180 zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs }
182 sequenceM [] = return []
183 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
185 sequenceM_ [] = return ()
186 sequenceM_ (x:xs) = do { x; sequenceM_ xs }
188 foldlM k z [] = return z
189 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
191 foldrM k z [] = return z
192 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
194 mapAndUnzipM f [] = return ([],[])
195 mapAndUnzipM f (x:xs) = do { (r,s) <- f x;
196 (rs,ss) <- mapAndUnzipM f xs;
197 return (r:rs, s:ss) }
199 mapAndUnzip3M f [] = return ([],[], [])
200 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x;
201 (rs,ss,ts) <- mapAndUnzip3M f xs;
202 return (r:rs, s:ss, t:ts) }
204 checkM True err = return ()
205 checkM False err = do { err; return () }
207 ifM True do_it = do { do_it; return () }
208 ifM False do_it = return ()