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,
11 mappM, mappM_, sequenceM, foldlM,
12 mapAndUnzipM, mapAndUnzip3M,
13 checkM, ifM, zipWithM, zipWithM_,
15 -- Getting at the environment
16 getEnv, setEnv, updEnv,
18 runIOEnv, unsafeInterleaveM,
23 IORef, newMutVar, readMutVar, writeMutVar, updMutVar
25 #include "HsVersions.h"
27 import Panic ( tryJust )
28 import DATA_IOREF ( IORef, newIORef, readIORef, writeIORef )
29 import UNSAFE_IO ( unsafeInterleaveIO )
30 import FIX_IO ( fixIO )
31 import EXCEPTION ( Exception(..) )
32 import IO ( isUserError )
35 ----------------------------------------------------------------------
36 -- Defining the monad type
37 ----------------------------------------------------------------------
40 newtype IOEnv env a = IOEnv (env -> IO a)
43 instance Monad (IOEnv m) where
47 fail s = failM -- Ignore the string
49 returnM :: a -> IOEnv env a
50 returnM a = IOEnv (\ env -> return a)
52 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
53 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
56 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
57 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
60 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
64 ----------------------------------------------------------------------
65 -- Fundmantal combinators specific to the monad
66 ----------------------------------------------------------------------
69 ---------------------------
70 runIOEnv :: env -> IOEnv env a -> IO a
71 runIOEnv env (IOEnv m) = m env
74 ---------------------------
76 -- Aargh! Not inlining fixTc alleviates a space leak problem.
77 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
78 -- shown the definition of fixTc, it occasionally transforms the code
79 -- in such a way that the code generator doesn't spot the selector
82 fixM :: (a -> IOEnv env a) -> IOEnv env a
83 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
86 ---------------------------
87 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
88 -- Reflect exception into IOEnv envonad
89 tryM (IOEnv thing) = IOEnv (\ env -> tryJust tc_errors (thing env))
91 #if __GLASGOW_HASKELL__ > 504 || __GLASGOW_HASKELL__ < 500
92 tc_errors e@(IOException ioe) | isUserError ioe = Just e
93 #elif __GLASGOW_HASKELL__ == 502
94 tc_errors e@(UserError _) = Just e
96 tc_errors e@(IOException ioe) | isUserError e = Just e
98 tc_errors _other = Nothing
99 -- type checker failures show up as UserErrors only
102 ---------------------------
103 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
104 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
107 ----------------------------------------------------------------------
108 -- Accessing input/output
109 ----------------------------------------------------------------------
111 ioToIOEnv :: IO a -> IOEnv env a
112 ioToIOEnv io = IOEnv (\ env -> io)
114 newMutVar :: a -> IOEnv env (IORef a)
115 newMutVar val = IOEnv (\ env -> newIORef val)
117 writeMutVar :: IORef a -> a -> IOEnv env ()
118 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
120 readMutVar :: IORef a -> IOEnv env a
121 readMutVar var = IOEnv (\ env -> readIORef var)
123 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
124 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
127 ----------------------------------------------------------------------
128 -- Accessing the environment
129 ----------------------------------------------------------------------
131 getEnv :: IOEnv env env
132 {-# INLINE getEnv #-}
133 getEnv = IOEnv (\ env -> return env)
135 setEnv :: env' -> IOEnv env' a -> IOEnv env a
136 {-# INLINE setEnv #-}
137 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
139 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
140 {-# INLINE updEnv #-}
141 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
144 ----------------------------------------------------------------------
145 -- Standard combinators, but specialised for this monad
147 ----------------------------------------------------------------------
149 mappM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
150 mappM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
151 -- Funny names to avoid clash with Prelude
152 sequenceM :: [IOEnv env a] -> IOEnv env [a]
153 foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a
154 mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c])
155 mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
156 checkM :: Bool -> IOEnv env () -> IOEnv env () -- Perform arg if bool is False
157 ifM :: Bool -> IOEnv env () -> IOEnv env () -- Perform arg if bool is True
159 mappM f [] = return []
160 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
162 mappM_ f [] = return ()
163 mappM_ f (x:xs) = f x >> mappM_ f xs
165 zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
166 zipWithM f [] bs = return []
167 zipWithM f as [] = return []
168 zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) }
170 zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
171 zipWithM_ f [] bs = return ()
172 zipWithM_ f as [] = return ()
173 zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs }
175 sequenceM [] = return []
176 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
178 foldlM k z [] = return z
179 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
181 mapAndUnzipM f [] = return ([],[])
182 mapAndUnzipM f (x:xs) = do { (r,s) <- f x;
183 (rs,ss) <- mapAndUnzipM f xs;
184 return (r:rs, s:ss) }
186 mapAndUnzip3M f [] = return ([],[], [])
187 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x;
188 (rs,ss,ts) <- mapAndUnzip3M f xs;
189 return (r:rs, s:ss, t:ts) }
191 checkM True err = return ()
192 checkM False err = err
194 ifM True do_it = do_it
195 ifM False do_it = return ()