2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
9 -- (c) The University of Glasgow 2002-2006
11 -- The IO Monad with an environment
15 IOEnv, -- Instance of Monad
17 -- Standard combinators, specialised
18 returnM, thenM, thenM_, failM, failWithM,
19 mappM, mappM_, mapSndM, sequenceM, sequenceM_,
21 mapAndUnzipM, mapAndUnzip3M,
22 checkM, ifM, zipWithM, zipWithM_,
24 -- Getting at the environment
25 getEnv, setEnv, updEnv,
27 runIOEnv, unsafeInterleaveM,
28 tryM, tryAllM, tryMostM, fixM,
32 IORef, newMutVar, readMutVar, writeMutVar, updMutVar
34 #include "HsVersions.h"
36 import Panic ( try, tryUser, tryMost, Exception(..) )
38 import Data.IORef ( IORef, newIORef, readIORef, writeIORef )
39 import System.IO.Unsafe ( unsafeInterleaveIO )
40 import System.IO ( fixIO )
43 ----------------------------------------------------------------------
44 -- Defining the monad type
45 ----------------------------------------------------------------------
48 newtype IOEnv env a = IOEnv (env -> IO a)
51 instance Monad (IOEnv m) where
55 fail s = failM -- Ignore the string
57 instance Functor (IOEnv m) where
58 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
60 returnM :: a -> IOEnv env a
61 returnM a = IOEnv (\ env -> return a)
63 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
64 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
67 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
68 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
71 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
73 failWithM :: String -> IOEnv env a
74 failWithM s = IOEnv (\ env -> ioError (userError s))
78 ----------------------------------------------------------------------
79 -- Fundmantal combinators specific to the monad
80 ----------------------------------------------------------------------
83 ---------------------------
84 runIOEnv :: env -> IOEnv env a -> IO a
85 runIOEnv env (IOEnv m) = m env
88 ---------------------------
90 -- Aargh! Not inlining fixTc alleviates a space leak problem.
91 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
92 -- shown the definition of fixTc, it occasionally transforms the code
93 -- in such a way that the code generator doesn't spot the selector
96 fixM :: (a -> IOEnv env a) -> IOEnv env a
97 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
100 ---------------------------
101 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
102 -- Reflect UserError exceptions (only) into IOEnv monad
103 -- Other exceptions are not caught; they are simply propagated as exns
105 -- The idea is that errors in the program being compiled will give rise
106 -- to UserErrors. But, say, pattern-match failures in GHC itself should
107 -- not be caught here, else they'll be reported as errors in the program
109 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
111 tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
112 -- Catch *all* exceptions
113 -- This is used when running a Template-Haskell splice, when
114 -- even a pattern-match failure is a programmer error
115 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
117 tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
118 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
120 ---------------------------
121 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
122 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
125 ----------------------------------------------------------------------
126 -- Accessing input/output
127 ----------------------------------------------------------------------
129 ioToIOEnv :: IO a -> IOEnv env a
130 ioToIOEnv io = IOEnv (\ env -> io)
132 newMutVar :: a -> IOEnv env (IORef a)
133 newMutVar val = IOEnv (\ env -> newIORef val)
135 writeMutVar :: IORef a -> a -> IOEnv env ()
136 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
138 readMutVar :: IORef a -> IOEnv env a
139 readMutVar var = IOEnv (\ env -> readIORef var)
141 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
142 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
145 ----------------------------------------------------------------------
146 -- Accessing the environment
147 ----------------------------------------------------------------------
149 getEnv :: IOEnv env env
150 {-# INLINE getEnv #-}
151 getEnv = IOEnv (\ env -> return env)
153 setEnv :: env' -> IOEnv env' a -> IOEnv env a
154 {-# INLINE setEnv #-}
155 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
157 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
158 {-# INLINE updEnv #-}
159 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
162 ----------------------------------------------------------------------
163 -- Standard combinators, but specialised for this monad
165 ----------------------------------------------------------------------
167 mappM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
168 mappM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
169 mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)]
170 -- Funny names to avoid clash with Prelude
171 sequenceM :: [IOEnv env a] -> IOEnv env [a]
172 sequenceM_ :: [IOEnv env a] -> IOEnv env ()
173 foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a
174 foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a
175 mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c])
176 mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
177 checkM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is False
178 ifM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is True
180 mappM f [] = return []
181 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
183 mapSndM f [] = return []
184 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
186 mappM_ f [] = return ()
187 mappM_ f (x:xs) = f x >> mappM_ f xs
189 zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
190 zipWithM f [] bs = return []
191 zipWithM f as [] = return []
192 zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) }
194 zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
195 zipWithM_ f [] bs = return ()
196 zipWithM_ f as [] = return ()
197 zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs }
199 sequenceM [] = return []
200 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
202 sequenceM_ [] = return ()
203 sequenceM_ (x:xs) = do { x; sequenceM_ xs }
205 foldlM k z [] = return z
206 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
208 foldrM k z [] = return z
209 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
211 mapAndUnzipM f [] = return ([],[])
212 mapAndUnzipM f (x:xs) = do { (r,s) <- f x;
213 (rs,ss) <- mapAndUnzipM f xs;
214 return (r:rs, s:ss) }
216 mapAndUnzip3M f [] = return ([],[], [])
217 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x;
218 (rs,ss,ts) <- mapAndUnzip3M f xs;
219 return (r:rs, s:ss, t:ts) }
221 checkM True err = return ()
222 checkM False err = do { err; return () }
224 ifM True do_it = do { do_it; return () }
225 ifM False do_it = return ()