Use OPTIONS rather than OPTIONS_GHC for pragmas
[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 {-# OPTIONS -w #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 --     http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
12 -- for details
13
14 module IOEnv (
15         IOEnv,  -- Instance of Monad
16
17         -- Standard combinators, specialised
18         returnM, thenM, thenM_, failM, failWithM,
19         mappM, mappM_, mapSndM, sequenceM, sequenceM_, 
20         foldlM, foldrM,
21         mapAndUnzipM, mapAndUnzip3M, 
22         checkM, ifM, zipWithM, zipWithM_,
23
24         -- Getting at the environment
25         getEnv, setEnv, updEnv,
26
27         runIOEnv, unsafeInterleaveM,                    
28         tryM, tryAllM, tryMostM, fixM, 
29
30         -- I/O operations
31         ioToIOEnv,
32         IORef, newMutVar, readMutVar, writeMutVar, updMutVar
33   ) where
34 #include "HsVersions.h"
35
36 import Panic            ( try, tryUser, tryMost, Exception(..) )
37
38 import Data.IORef       ( IORef, newIORef, readIORef, writeIORef )
39 import System.IO.Unsafe ( unsafeInterleaveIO )
40 import System.IO        ( fixIO )
41
42
43 ----------------------------------------------------------------------
44 --              Defining the monad type
45 ----------------------------------------------------------------------
46
47
48 newtype IOEnv env a = IOEnv (env -> IO a)
49 unIOEnv (IOEnv m) = m
50
51 instance Monad (IOEnv m) where
52   (>>=)  = thenM
53   (>>)   = thenM_
54   return = returnM
55   fail s = failM        -- Ignore the string
56
57 instance Functor (IOEnv m) where
58   fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
59
60 returnM :: a -> IOEnv env a
61 returnM a = IOEnv (\ env -> return a)
62
63 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
64 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
65                                        unIOEnv (f r) env })
66
67 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
68 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
69
70 failM :: IOEnv env a
71 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
72
73 failWithM :: String -> IOEnv env a
74 failWithM s = IOEnv (\ env -> ioError (userError s))
75
76
77
78 ----------------------------------------------------------------------
79 --      Fundmantal combinators specific to the monad
80 ----------------------------------------------------------------------
81
82
83 ---------------------------
84 runIOEnv :: env -> IOEnv env a -> IO a
85 runIOEnv env (IOEnv m) = m env
86
87
88 ---------------------------
89 {-# NOINLINE fixM #-}
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
94   -- thunks.  Sigh.
95
96 fixM :: (a -> IOEnv env a) -> IOEnv env a
97 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
98
99
100 ---------------------------
101 tryM :: IOEnv env r -> IOEnv env (Either Exception r)
102 -- Reflect UserError exceptions into IOEnv monad
103 -- The idea is that errors in the program being compiled will give rise
104 -- to UserErrors.  But, say, pattern-match failures in GHC itself should
105 -- not be caught here, else they'll be reported as errors in the program 
106 -- begin compiled!
107 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
108
109 tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
110 -- Catch *all* exceptions
111 -- This is used when running a Template-Haskell splice, when
112 -- even a pattern-match failure is a programmer error
113 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
114
115 tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
116 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
117
118 ---------------------------
119 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
120 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
121
122
123 ----------------------------------------------------------------------
124 --      Accessing input/output
125 ----------------------------------------------------------------------
126
127 ioToIOEnv :: IO a -> IOEnv env a
128 ioToIOEnv io = IOEnv (\ env -> io)
129
130 newMutVar :: a -> IOEnv env (IORef a)
131 newMutVar val = IOEnv (\ env -> newIORef val)
132
133 writeMutVar :: IORef a -> a -> IOEnv env ()
134 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
135
136 readMutVar :: IORef a -> IOEnv env a
137 readMutVar var = IOEnv (\ env -> readIORef var)
138
139 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
140 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
141
142
143 ----------------------------------------------------------------------
144 --      Accessing the environment
145 ----------------------------------------------------------------------
146
147 getEnv :: IOEnv env env
148 {-# INLINE getEnv #-}
149 getEnv = IOEnv (\ env -> return env)
150
151 setEnv :: env' -> IOEnv env' a -> IOEnv env a
152 {-# INLINE setEnv #-}
153 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
154
155 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
156 {-# INLINE updEnv #-}
157 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
158
159
160 ----------------------------------------------------------------------
161 --      Standard combinators, but specialised for this monad
162 --                      (for efficiency)
163 ----------------------------------------------------------------------
164
165 mappM         :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
166 mappM_        :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
167 mapSndM       :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)]
168         -- Funny names to avoid clash with Prelude
169 sequenceM     :: [IOEnv env a] -> IOEnv env [a]
170 sequenceM_    :: [IOEnv env a] -> IOEnv env ()
171 foldlM        :: (a -> b -> IOEnv env a)  -> a -> [b] -> IOEnv env a
172 foldrM        :: (b -> a -> IOEnv env a)  -> a -> [b] -> IOEnv env a
173 mapAndUnzipM  :: (a -> IOEnv env (b,c))   -> [a] -> IOEnv env ([b],[c])
174 mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
175 checkM        :: Bool -> IOEnv env a -> IOEnv env ()    -- Perform arg if bool is False
176 ifM           :: Bool -> IOEnv env a -> IOEnv env ()    -- Perform arg if bool is True
177
178 mappM f []     = return []
179 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
180
181 mapSndM f []     = return []
182 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
183
184 mappM_ f []     = return ()
185 mappM_ f (x:xs) = f x >> mappM_ f xs
186
187 zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
188 zipWithM f [] bs = return []
189 zipWithM f as [] = return []
190 zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) } 
191
192 zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
193 zipWithM_ f [] bs = return ()
194 zipWithM_ f as [] = return ()
195 zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs } 
196
197 sequenceM [] = return []
198 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
199
200 sequenceM_ []     = return ()
201 sequenceM_ (x:xs) = do { x; sequenceM_ xs }
202
203 foldlM k z [] = return z
204 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
205
206 foldrM k z [] = return z
207 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
208
209 mapAndUnzipM f []     = return ([],[])
210 mapAndUnzipM f (x:xs) = do { (r,s) <- f x; 
211                              (rs,ss) <- mapAndUnzipM f xs; 
212                              return (r:rs, s:ss) }
213
214 mapAndUnzip3M f []     = return ([],[], [])
215 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x; 
216                               (rs,ss,ts) <- mapAndUnzip3M f xs; 
217                               return (r:rs, s:ss, t:ts) }
218
219 checkM True  err = return ()
220 checkM False err = do { err; return () }
221
222 ifM True  do_it = do { do_it; return () }
223 ifM False do_it = return ()