1db15537c7dd921e50f263753e5946fa2cff409c
[ghc.git] / compiler / utils / IOEnv.hs
1 {-# LANGUAGE DeriveDataTypeable, UndecidableInstances #-}
2
3 --
4 -- (c) The University of Glasgow 2002-2006
5 --
6 -- The IO Monad with an environment
7 --
8 -- The environment is passed around as a Reader monad but
9 -- as its in the IO monad, mutable references can be used
10 -- for updating state.
11 --
12
13 module IOEnv (
14 IOEnv, -- Instance of Monad
15
16 -- Monad utilities
17 module MonadUtils,
18
19 -- Errors
20 failM, failWithM,
21 IOEnvFailure(..),
22
23 -- Getting at the environment
24 getEnv, setEnv, updEnv,
25
26 runIOEnv, unsafeInterleaveM, uninterruptibleMaskM_,
27 tryM, tryAllM, tryMostM, fixM,
28
29 -- I/O operations
30 IORef, newMutVar, readMutVar, writeMutVar, updMutVar,
31 atomicUpdMutVar, atomicUpdMutVar'
32 ) where
33
34 import DynFlags
35 import Exception
36 import Module
37 import Panic
38
39 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef,
40 atomicModifyIORef )
41 import Data.Typeable
42 import System.IO.Unsafe ( unsafeInterleaveIO )
43 import System.IO ( fixIO )
44 import Control.Monad
45 import MonadUtils
46 import Control.Applicative (Alternative(..))
47
48 ----------------------------------------------------------------------
49 -- Defining the monad type
50 ----------------------------------------------------------------------
51
52
53 newtype IOEnv env a = IOEnv (env -> IO a)
54
55 unIOEnv :: IOEnv env a -> (env -> IO a)
56 unIOEnv (IOEnv m) = m
57
58 instance Monad (IOEnv m) where
59 (>>=) = thenM
60 (>>) = thenM_
61 return = returnM
62 fail _ = failM -- Ignore the string
63
64 instance Applicative (IOEnv m) where
65 pure = returnM
66 IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
67
68 instance Functor (IOEnv m) where
69 fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
70
71 returnM :: a -> IOEnv env a
72 returnM a = IOEnv (\ _ -> return a)
73
74 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
75 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
76 unIOEnv (f r) env })
77
78 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
79 thenM_ (IOEnv m) f = IOEnv (\ env -> do { _ <- m env ; unIOEnv f env })
80
81 failM :: IOEnv env a
82 failM = IOEnv (\ _ -> throwIO IOEnvFailure)
83
84 failWithM :: String -> IOEnv env a
85 failWithM s = IOEnv (\ _ -> ioError (userError s))
86
87 data IOEnvFailure = IOEnvFailure
88 deriving Typeable
89
90 instance Show IOEnvFailure where
91 show IOEnvFailure = "IOEnv failure"
92
93 instance Exception IOEnvFailure
94
95 instance ContainsDynFlags env => HasDynFlags (IOEnv env) where
96 getDynFlags = do env <- getEnv
97 return $ extractDynFlags env
98
99 instance ContainsModule env => HasModule (IOEnv env) where
100 getModule = do env <- getEnv
101 return $ extractModule env
102
103 ----------------------------------------------------------------------
104 -- Fundmantal combinators specific to the monad
105 ----------------------------------------------------------------------
106
107
108 ---------------------------
109 runIOEnv :: env -> IOEnv env a -> IO a
110 runIOEnv env (IOEnv m) = m env
111
112
113 ---------------------------
114 {-# NOINLINE fixM #-}
115 -- Aargh! Not inlining fixTc alleviates a space leak problem.
116 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
117 -- shown the definition of fixTc, it occasionally transforms the code
118 -- in such a way that the code generator doesn't spot the selector
119 -- thunks. Sigh.
120
121 fixM :: (a -> IOEnv env a) -> IOEnv env a
122 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
123
124
125 ---------------------------
126 tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
127 -- Reflect UserError exceptions (only) into IOEnv monad
128 -- Other exceptions are not caught; they are simply propagated as exns
129 --
130 -- The idea is that errors in the program being compiled will give rise
131 -- to UserErrors. But, say, pattern-match failures in GHC itself should
132 -- not be caught here, else they'll be reported as errors in the program
133 -- begin compiled!
134 tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
135
136 tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
137 tryIOEnvFailure = try
138
139 -- XXX We shouldn't be catching everything, e.g. timeouts
140 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
141 -- Catch *all* exceptions
142 -- This is used when running a Template-Haskell splice, when
143 -- even a pattern-match failure is a programmer error
144 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
145
146 tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
147 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
148
149 ---------------------------
150 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
151 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
152
153 uninterruptibleMaskM_ :: IOEnv env a -> IOEnv env a
154 uninterruptibleMaskM_ (IOEnv m) = IOEnv (\ env -> uninterruptibleMask_ (m env))
155
156 ----------------------------------------------------------------------
157 -- Alternative/MonadPlus
158 ----------------------------------------------------------------------
159
160 instance MonadPlus IO => Alternative (IOEnv env) where
161 empty = mzero
162 (<|>) = mplus
163
164 -- For use if the user has imported Control.Monad.Error from MTL
165 -- Requires UndecidableInstances
166 instance MonadPlus IO => MonadPlus (IOEnv env) where
167 mzero = IOEnv (const mzero)
168 m `mplus` n = IOEnv (\env -> unIOEnv m env `mplus` unIOEnv n env)
169
170 ----------------------------------------------------------------------
171 -- Accessing input/output
172 ----------------------------------------------------------------------
173
174 instance MonadIO (IOEnv env) where
175 liftIO io = IOEnv (\ _ -> io)
176
177 newMutVar :: a -> IOEnv env (IORef a)
178 newMutVar val = liftIO (newIORef val)
179
180 writeMutVar :: IORef a -> a -> IOEnv env ()
181 writeMutVar var val = liftIO (writeIORef var val)
182
183 readMutVar :: IORef a -> IOEnv env a
184 readMutVar var = liftIO (readIORef var)
185
186 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
187 updMutVar var upd = liftIO (modifyIORef var upd)
188
189 -- | Atomically update the reference. Does not force the evaluation of the
190 -- new variable contents. For strict update, use 'atomicUpdMutVar''.
191 atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
192 atomicUpdMutVar var upd = liftIO (atomicModifyIORef var upd)
193
194 -- | Strict variant of 'atomicUpdMutVar'.
195 atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
196 atomicUpdMutVar' var upd = do
197 r <- atomicUpdMutVar var upd
198 _ <- liftIO . evaluate =<< readMutVar var
199 return r
200
201 ----------------------------------------------------------------------
202 -- Accessing the environment
203 ----------------------------------------------------------------------
204
205 getEnv :: IOEnv env env
206 {-# INLINE getEnv #-}
207 getEnv = IOEnv (\ env -> return env)
208
209 -- | Perform a computation with a different environment
210 setEnv :: env' -> IOEnv env' a -> IOEnv env a
211 {-# INLINE setEnv #-}
212 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
213
214 -- | Perform a computation with an altered environment
215 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
216 {-# INLINE updEnv #-}
217 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
218