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