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