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