Module header tidyup #2
[ghc-hetmet.git] / compiler / utils / IOEnv.hs
1 --
2 -- (c) The University of Glasgow 2002-2006
3 --
4 -- The IO Monad with an environment
5 --
6
7 module IOEnv (
8         IOEnv,  -- Instance of Monad
9
10         -- Standard combinators, specialised
11         returnM, thenM, thenM_, failM, failWithM,
12         mappM, mappM_, mapSndM, sequenceM, sequenceM_, 
13         foldlM, foldrM,
14         mapAndUnzipM, mapAndUnzip3M, 
15         checkM, ifM, zipWithM, zipWithM_,
16
17         -- Getting at the environment
18         getEnv, setEnv, updEnv,
19
20         runIOEnv, unsafeInterleaveM,                    
21         tryM, tryAllM, tryMostM, fixM, 
22
23         -- I/O operations
24         ioToIOEnv,
25         IORef, newMutVar, readMutVar, writeMutVar, updMutVar
26   ) where
27 #include "HsVersions.h"
28
29 import Panic            ( try, tryUser, tryMost, Exception(..) )
30
31 import Data.IORef       ( IORef, newIORef, readIORef, writeIORef )
32 import System.IO.Unsafe ( unsafeInterleaveIO )
33 import System.IO        ( fixIO )
34
35
36 ----------------------------------------------------------------------
37 --              Defining the monad type
38 ----------------------------------------------------------------------
39
40
41 newtype IOEnv env a = IOEnv (env -> IO a)
42 unIOEnv (IOEnv m) = m
43
44 instance Monad (IOEnv m) where
45   (>>=)  = thenM
46   (>>)   = thenM_
47   return = returnM
48   fail s = failM        -- Ignore the string
49
50 returnM :: a -> IOEnv env a
51 returnM a = IOEnv (\ env -> return a)
52
53 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
54 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
55                                        unIOEnv (f r) env })
56
57 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
58 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
59
60 failM :: IOEnv env a
61 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
62
63 failWithM :: String -> IOEnv env a
64 failWithM s = IOEnv (\ env -> ioError (userError s))
65
66
67
68 ----------------------------------------------------------------------
69 --      Fundmantal combinators specific to the monad
70 ----------------------------------------------------------------------
71
72
73 ---------------------------
74 runIOEnv :: env -> IOEnv env a -> IO a
75 runIOEnv env (IOEnv m) = m env
76
77
78 ---------------------------
79 {-# NOINLINE fixM #-}
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
84   -- thunks.  Sigh.
85
86 fixM :: (a -> IOEnv env a) -> IOEnv env a
87 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
88
89
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 
96 -- begin compiled!
97 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
98
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))
104
105 tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
106 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
107
108 ---------------------------
109 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
110 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
111
112
113 ----------------------------------------------------------------------
114 --      Accessing input/output
115 ----------------------------------------------------------------------
116
117 ioToIOEnv :: IO a -> IOEnv env a
118 ioToIOEnv io = IOEnv (\ env -> io)
119
120 newMutVar :: a -> IOEnv env (IORef a)
121 newMutVar val = IOEnv (\ env -> newIORef val)
122
123 writeMutVar :: IORef a -> a -> IOEnv env ()
124 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
125
126 readMutVar :: IORef a -> IOEnv env a
127 readMutVar var = IOEnv (\ env -> readIORef var)
128
129 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
130 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
131
132
133 ----------------------------------------------------------------------
134 --      Accessing the environment
135 ----------------------------------------------------------------------
136
137 getEnv :: IOEnv env env
138 {-# INLINE getEnv #-}
139 getEnv = IOEnv (\ env -> return env)
140
141 setEnv :: env' -> IOEnv env' a -> IOEnv env a
142 {-# INLINE setEnv #-}
143 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
144
145 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
146 {-# INLINE updEnv #-}
147 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
148
149
150 ----------------------------------------------------------------------
151 --      Standard combinators, but specialised for this monad
152 --                      (for efficiency)
153 ----------------------------------------------------------------------
154
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
167
168 mappM f []     = return []
169 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
170
171 mapSndM f []     = return []
172 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
173
174 mappM_ f []     = return ()
175 mappM_ f (x:xs) = f x >> mappM_ f xs
176
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) } 
181
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 } 
186
187 sequenceM [] = return []
188 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
189
190 sequenceM_ []     = return ()
191 sequenceM_ (x:xs) = do { x; sequenceM_ xs }
192
193 foldlM k z [] = return z
194 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
195
196 foldrM k z [] = return z
197 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
198
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) }
203
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) }
208
209 checkM True  err = return ()
210 checkM False err = do { err; return () }
211
212 ifM True  do_it = do { do_it; return () }
213 ifM False do_it = return ()