Use a proper exception for IOEnvFailure, not just a UserError
[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         IOEnvFailure(..),
16
17         -- Getting at the environment
18         getEnv, setEnv, updEnv,
19
20         runIOEnv, unsafeInterleaveM,
21         tryM, tryAllM, tryMostM, fixM,
22
23         -- I/O operations
24         IORef, newMutVar, readMutVar, writeMutVar, updMutVar
25   ) where
26
27 import Exception
28 import Panic
29
30 import Data.IORef       ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
31 import Data.Typeable
32 import System.IO.Unsafe ( unsafeInterleaveIO )
33 import System.IO        ( fixIO )
34 import MonadUtils
35
36 ----------------------------------------------------------------------
37 -- Defining the monad type
38 ----------------------------------------------------------------------
39
40
41 newtype IOEnv env a = IOEnv (env -> IO a)
42
43 unIOEnv :: IOEnv env a -> (env -> IO a)
44 unIOEnv (IOEnv m) = m
45
46 instance Monad (IOEnv m) where
47     (>>=)  = thenM
48     (>>)   = thenM_
49     return = returnM
50     fail _ = failM -- Ignore the string
51
52 instance Applicative (IOEnv m) where
53     pure = returnM
54     IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
55
56 instance Functor (IOEnv m) where
57     fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
58
59 returnM :: a -> IOEnv env a
60 returnM a = IOEnv (\ _ -> return a)
61
62 thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
63 thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
64                                          unIOEnv (f r) env })
65
66 thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
67 thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })
68
69 failM :: IOEnv env a
70 failM = IOEnv (\ _ -> throwIO IOEnvFailure)
71
72 failWithM :: String -> IOEnv env a
73 failWithM s = IOEnv (\ _ -> ioError (userError s))
74
75 data IOEnvFailure = IOEnvFailure
76     deriving Typeable
77
78 instance Show IOEnvFailure where
79     show IOEnvFailure = "IOEnv failure"
80
81 instance Exception IOEnvFailure
82
83 ----------------------------------------------------------------------
84 -- Fundmantal combinators specific to the monad
85 ----------------------------------------------------------------------
86
87
88 ---------------------------
89 runIOEnv :: env -> IOEnv env a -> IO a
90 runIOEnv env (IOEnv m) = m env
91
92
93 ---------------------------
94 {-# NOINLINE fixM #-}
95   -- Aargh!  Not inlining fixTc alleviates a space leak problem.
96   -- Normally fixTc is used with a lazy tuple match: if the optimiser is
97   -- shown the definition of fixTc, it occasionally transforms the code
98   -- in such a way that the code generator doesn't spot the selector
99   -- thunks.  Sigh.
100
101 fixM :: (a -> IOEnv env a) -> IOEnv env a
102 fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
103
104
105 ---------------------------
106 tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
107 -- Reflect UserError exceptions (only) into IOEnv monad
108 -- Other exceptions are not caught; they are simply propagated as exns
109 --
110 -- The idea is that errors in the program being compiled will give rise
111 -- to UserErrors.  But, say, pattern-match failures in GHC itself should
112 -- not be caught here, else they'll be reported as errors in the program
113 -- begin compiled!
114 tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
115
116 tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
117 tryIOEnvFailure = try
118
119 -- XXX We shouldn't be catching everything, e.g. timeouts
120 tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
121 -- Catch *all* exceptions
122 -- This is used when running a Template-Haskell splice, when
123 -- even a pattern-match failure is a programmer error
124 tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
125
126 tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
127 tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
128
129 ---------------------------
130 unsafeInterleaveM :: IOEnv env a -> IOEnv env a
131 unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
132
133
134 ----------------------------------------------------------------------
135 -- Accessing input/output
136 ----------------------------------------------------------------------
137
138 instance MonadIO (IOEnv env) where
139     liftIO io = IOEnv (\ _ -> io)
140
141 newMutVar :: a -> IOEnv env (IORef a)
142 newMutVar val = liftIO (newIORef val)
143
144 writeMutVar :: IORef a -> a -> IOEnv env ()
145 writeMutVar var val = liftIO (writeIORef var val)
146
147 readMutVar :: IORef a -> IOEnv env a
148 readMutVar var = liftIO (readIORef var)
149
150 updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
151 updMutVar var upd = liftIO (modifyIORef var upd)
152
153
154 ----------------------------------------------------------------------
155 -- Accessing the environment
156 ----------------------------------------------------------------------
157
158 getEnv :: IOEnv env env
159 {-# INLINE getEnv #-}
160 getEnv = IOEnv (\ env -> return env)
161
162 -- | Perform a computation with a different environment
163 setEnv :: env' -> IOEnv env' a -> IOEnv env a
164 {-# INLINE setEnv #-}
165 setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
166
167 -- | Perform a computation with an altered environment
168 updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
169 {-# INLINE updEnv #-}
170 updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))
171
172
173 ----------------------------------------------------------------------
174 -- Standard combinators, but specialised for this monad
175 -- (for efficiency)
176 ----------------------------------------------------------------------
177
178 -- {-# SPECIALIZE mapM          :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
179 -- {-# SPECIALIZE mapM_         :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
180 -- {-# SPECIALIZE mapSndM       :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
181 -- {-# SPECIALIZE sequence      :: [IOEnv env a] -> IOEnv env [a] #-}
182 -- {-# SPECIALIZE sequence_     :: [IOEnv env a] -> IOEnv env () #-}
183 -- {-# SPECIALIZE foldlM        :: (a -> b -> IOEnv env a)  -> a -> [b] -> IOEnv env a #-}
184 -- {-# SPECIALIZE foldrM        :: (b -> a -> IOEnv env a)  -> a -> [b] -> IOEnv env a #-}
185 -- {-# SPECIALIZE mapAndUnzipM  :: (a -> IOEnv env (b,c))   -> [a] -> IOEnv env ([b],[c]) #-}
186 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
187 -- {-# SPECIALIZE zipWithM      :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
188 -- {-# SPECIALIZE zipWithM_     :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
189 -- {-# SPECIALIZE anyM          :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
190 -- {-# SPECIALIZE when          :: Bool -> IOEnv env a -> IOEnv env () #-}
191 -- {-# SPECIALIZE unless        :: Bool -> IOEnv env a -> IOEnv env () #-}