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