Use an extensible-exceptions package when bootstrapping
[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 module IOEnv (
8         IOEnv, -- Instance of Monad
9
10         -- Monad utilities
11         module MonadUtils,
12
13         -- Errors
14         failM, failWithM,
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         IORef, newMutVar, readMutVar, writeMutVar, updMutVar
24   ) where
25
26 import Exception
27 import Panic
28
29 import Data.IORef       ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
30 import System.IO.Unsafe ( unsafeInterleaveIO )
31 import System.IO        ( fixIO )
32 import MonadUtils
33
34 ----------------------------------------------------------------------
35 -- Defining the monad type
36 ----------------------------------------------------------------------
37
38
39 newtype IOEnv env a = IOEnv (env -> IO a)
40
41 unIOEnv :: IOEnv env a -> (env -> IO a)
42 unIOEnv (IOEnv m) = m
43
44 instance Monad (IOEnv m) where
45     (>>=)  = thenM
46     (>>)   = thenM_
47     return = returnM
48     fail _ = failM -- Ignore the string
49
50 instance Applicative (IOEnv m) where
51     pure = returnM
52     IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
53
54 instance Functor (IOEnv m) where
55     fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
56
57 returnM :: a -> IOEnv env a
58 returnM a = IOEnv (\ _ -> return a)
59
60 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
61 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
62                                          unIOEnv (f r) env })
63
64 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
65 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
66
67 failM :: IOEnv env a
68 failM = IOEnv (\ _ -> ioError (userError "IOEnv failure"))
69
70 failWithM :: String -> IOEnv env a
71 failWithM s = IOEnv (\ _ -> ioError (userError s))
72
73
74
75 ----------------------------------------------------------------------
76 -- Fundmantal combinators specific to the monad
77 ----------------------------------------------------------------------
78
79
80 ---------------------------
81 runIOEnv :: env -> IOEnv env a -> IO a
82 runIOEnv env (IOEnv m) = m env
83
84
85 ---------------------------
86 {-# NOINLINE fixM #-}
87   -- Aargh!  Not inlining fixTc alleviates a space leak problem.
88   -- Normally fixTc is used with a lazy tuple match: if the optimiser is
89   -- shown the definition of fixTc, it occasionally transforms the code
90   -- in such a way that the code generator doesn't spot the selector
91   -- thunks.  Sigh.
92
93 fixM :: (a -> IOEnv env a) -> IOEnv env a
94 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
95
96
97 ---------------------------
98 tryM :: IOEnv env r -> IOEnv env (Either IOException r)
99 -- Reflect UserError exceptions (only) into IOEnv monad
100 -- Other exceptions are not caught; they are simply propagated as exns
101 --
102 -- The idea is that errors in the program being compiled will give rise
103 -- to UserErrors.  But, say, pattern-match failures in GHC itself should
104 -- not be caught here, else they'll be reported as errors in the program
105 -- begin compiled!
106 tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
107
108 -- XXX We shouldn't be catching everything, e.g. timeouts
109 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException 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 SomeException 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 instance MonadIO (IOEnv env) where
128     liftIO io = IOEnv (\ _ -> io)
129
130 newMutVar :: a -> IOEnv env (IORef a)
131 newMutVar val = liftIO (newIORef val)
132
133 writeMutVar :: IORef a -> a -> IOEnv env ()
134 writeMutVar var val = liftIO (writeIORef var val)
135
136 readMutVar :: IORef a -> IOEnv env a
137 readMutVar var = liftIO (readIORef var)
138
139 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
140 updMutVar var upd = liftIO (modifyIORef var upd)
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 -- | Perform a computation with a different environment
152 setEnv :: env' -> IOEnv env' a -> IOEnv env a
153 {-# INLINE setEnv #-}
154 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
155
156 -- | Perform a computation with an altered environment
157 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
158 {-# INLINE updEnv #-}
159 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
160
161
162 ----------------------------------------------------------------------
163 -- Standard combinators, but specialised for this monad
164 -- (for efficiency)
165 ----------------------------------------------------------------------
166
167 -- {-# SPECIALIZE mapM          :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
168 -- {-# SPECIALIZE mapM_         :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
169 -- {-# SPECIALIZE mapSndM       :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
170 -- {-# SPECIALIZE sequence      :: [IOEnv env a] -> IOEnv env [a] #-}
171 -- {-# SPECIALIZE sequence_     :: [IOEnv env a] -> IOEnv env () #-}
172 -- {-# SPECIALIZE foldlM        :: (a -> b -> IOEnv env a)  -> a -> [b] -> IOEnv env a #-}
173 -- {-# SPECIALIZE foldrM        :: (b -> a -> IOEnv env a)  -> a -> [b] -> IOEnv env a #-}
174 -- {-# SPECIALIZE mapAndUnzipM  :: (a -> IOEnv env (b,c))   -> [a] -> IOEnv env ([b],[c]) #-}
175 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
176 -- {-# SPECIALIZE zipWithM      :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
177 -- {-# SPECIALIZE zipWithM_     :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
178 -- {-# SPECIALIZE anyM          :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
179 -- {-# SPECIALIZE when          :: Bool -> IOEnv env a -> IOEnv env () #-}
180 -- {-# SPECIALIZE unless        :: Bool -> IOEnv env a -> IOEnv env () #-}