3b4f9834dd4f3aa0c44ceaef88b93bf7b997095e
[ghc-hetmet.git] / ghc / 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,
11         mappM, mappM_, sequenceM, foldlM, 
12         mapAndUnzipM, mapAndUnzip3M, 
13         checkM, ifM, 
14
15         -- Getting at the environment
16         getEnv, setEnv, updEnv,
17
18         runIOEnv, unsafeInterleaveM,                    
19         tryM, fixM, 
20
21         -- I/O operations
22         ioToIOEnv,
23         IORef, newMutVar, readMutVar, writeMutVar, updMutVar
24   ) where
25 #include "HsVersions.h"
26
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 )
33
34
35 ----------------------------------------------------------------------
36 --              Defining the monad type
37 ----------------------------------------------------------------------
38
39
40 newtype IOEnv env a = IOEnv (env -> IO a)
41 unIOEnv (IOEnv m) = m
42
43 instance Monad (IOEnv m) where
44   (>>=)  = thenM
45   (>>)   = thenM_
46   return = returnM
47   fail s = failM        -- Ignore the string
48
49 returnM :: a -> IOEnv env a
50 returnM a = IOEnv (\ env -> return a)
51
52 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
53 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
54                                        unIOEnv (f r) env })
55
56 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
57 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
58
59 failM :: IOEnv env a
60 failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
61
62
63
64 ----------------------------------------------------------------------
65 --      Fundmantal combinators specific to the monad
66 ----------------------------------------------------------------------
67
68
69 ---------------------------
70 runIOEnv :: env -> IOEnv env a -> IO a
71 runIOEnv env (IOEnv m) = m env
72
73
74 ---------------------------
75 {-# NOINLINE fixM #-}
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
80   -- thunks.  Sigh.
81
82 fixM :: (a -> IOEnv env a) -> IOEnv env a
83 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
84
85
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))
90   where 
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
95 #else 
96         tc_errors e@(IOException ioe) | isUserError e = Just e
97 #endif
98         tc_errors _other = Nothing
99         -- type checker failures show up as UserErrors only
100
101
102 ---------------------------
103 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
104 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
105
106
107 ----------------------------------------------------------------------
108 --      Accessing input/output
109 ----------------------------------------------------------------------
110
111 ioToIOEnv :: IO a -> IOEnv env a
112 ioToIOEnv io = IOEnv (\ env -> io)
113
114 newMutVar :: a -> IOEnv env (IORef a)
115 newMutVar val = IOEnv (\ env -> newIORef val)
116
117 writeMutVar :: IORef a -> a -> IOEnv env ()
118 writeMutVar var val = IOEnv (\ env -> writeIORef var val)
119
120 readMutVar :: IORef a -> IOEnv env a
121 readMutVar var = IOEnv (\ env -> readIORef var)
122
123 updMutVar :: IORef a -> (a->a) -> IOEnv env ()
124 updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) })
125
126
127 ----------------------------------------------------------------------
128 --      Accessing the environment
129 ----------------------------------------------------------------------
130
131 getEnv :: IOEnv env env
132 {-# INLINE getEnv #-}
133 getEnv = IOEnv (\ env -> return env)
134
135 setEnv :: env' -> IOEnv env' a -> IOEnv env a
136 {-# INLINE setEnv #-}
137 setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
138
139 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
140 {-# INLINE updEnv #-}
141 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
142
143
144 ----------------------------------------------------------------------
145 --      Standard combinators, but specialised for this monad
146 --                      (for efficiency)
147 ----------------------------------------------------------------------
148
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
158
159 mappM f []     = return []
160 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
161
162 mappM_ f []     = return ()
163 mappM_ f (x:xs) = f x >> mappM_ f xs
164
165 sequenceM [] = return []
166 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
167
168 foldlM k z [] = return z
169 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
170
171 mapAndUnzipM f []     = return ([],[])
172 mapAndUnzipM f (x:xs) = do { (r,s) <- f x; 
173                              (rs,ss) <- mapAndUnzipM f xs; 
174                              return (r:rs, s:ss) }
175
176 mapAndUnzip3M f []     = return ([],[], [])
177 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x; 
178                               (rs,ss,ts) <- mapAndUnzip3M f xs; 
179                               return (r:rs, s:ss, t:ts) }
180
181 checkM True  err = return ()
182 checkM False err = err
183
184 ifM True  do_it = do_it
185 ifM False do_it = return ()