IOEnv.hs 7.58 KB
Newer Older
1
{-# OPTIONS -w #-}
2
3
4
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
Ian Lynagh's avatar
Ian Lynagh committed
5
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
6
7
-- for details

8
9
10
11
12
13
--
-- (c) The University of Glasgow 2002-2006
--
-- The IO Monad with an environment
--

14
15
16
17
module IOEnv (
	IOEnv,	-- Instance of Monad

	-- Standard combinators, specialised
18
	returnM, thenM, thenM_, failM, failWithM,
19
	mappM, mappM_, mapSndM, sequenceM, sequenceM_, 
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
20
	foldlM, foldrM, anyM,
21
	mapAndUnzipM, mapAndUnzip3M, 
22
	checkM, ifM, zipWithM, zipWithM_,
23
24
25
26
27

	-- Getting at the environment
	getEnv, setEnv, updEnv,

	runIOEnv, unsafeInterleaveM,			
28
	tryM, tryAllM, tryMostM, fixM, 
29
30
31
32
33
34
35

	-- I/O operations
	ioToIOEnv,
	IORef, newMutVar, readMutVar, writeMutVar, updMutVar
  ) where
#include "HsVersions.h"

36
import Panic		( try, tryUser, tryMost, Exception(..) )
Simon Marlow's avatar
Simon Marlow committed
37

twanvl's avatar
twanvl committed
38
import Data.IORef	( IORef, newIORef, readIORef, writeIORef, modifyIORef )
Simon Marlow's avatar
Simon Marlow committed
39
40
import System.IO.Unsafe	( unsafeInterleaveIO )
import System.IO	( fixIO )
twanvl's avatar
twanvl committed
41
import MonadUtils
42
43
44
45
46
47
48
49
50
51

----------------------------------------------------------------------
--		Defining the monad type
----------------------------------------------------------------------


newtype IOEnv env a = IOEnv (env -> IO a)
unIOEnv (IOEnv m) = m

instance Monad (IOEnv m) where
twanvl's avatar
twanvl committed
52
53
54
55
56
57
58
59
    (>>=)  = thenM
    (>>)   = thenM_
    return = returnM
    fail s = failM	-- Ignore the string

instance Applicative (IOEnv m) where
    pure = returnM
    IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
60

61
instance Functor (IOEnv m) where
twanvl's avatar
twanvl committed
62
    fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
63

64
65
66
67
68
69
70
71
72
73
74
75
76
returnM :: a -> IOEnv env a
returnM a = IOEnv (\ env -> return a)

thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
				       unIOEnv (f r) env })

thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
thenM_ (IOEnv m) f = IOEnv (\ env -> do { m env ; unIOEnv f env })

failM :: IOEnv env a
failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))

77
78
79
failWithM :: String -> IOEnv env a
failWithM s = IOEnv (\ env -> ioError (userError s))

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105


----------------------------------------------------------------------
--	Fundmantal combinators specific to the monad
----------------------------------------------------------------------


---------------------------
runIOEnv :: env -> IOEnv env a -> IO a
runIOEnv env (IOEnv m) = m env


---------------------------
{-# NOINLINE fixM #-}
  -- Aargh!  Not inlining fixTc alleviates a space leak problem.
  -- Normally fixTc is used with a lazy tuple match: if the optimiser is
  -- shown the definition of fixTc, it occasionally transforms the code
  -- in such a way that the code generator doesn't spot the selector
  -- thunks.  Sigh.

fixM :: (a -> IOEnv env a) -> IOEnv env a
fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))


---------------------------
tryM :: IOEnv env r -> IOEnv env (Either Exception r)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
106
107
108
-- Reflect UserError exceptions (only) into IOEnv monad
-- Other exceptions are not caught; they are simply propagated as exns
--
109
110
111
112
113
114
115
116
117
118
119
-- The idea is that errors in the program being compiled will give rise
-- to UserErrors.  But, say, pattern-match failures in GHC itself should
-- not be caught here, else they'll be reported as errors in the program 
-- begin compiled!
tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))

tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
-- Catch *all* exceptions
-- This is used when running a Template-Haskell splice, when
-- even a pattern-match failure is a programmer error
tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
120

121
122
123
tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))

124
125
126
127
128
129
130
131
132
---------------------------
unsafeInterleaveM :: IOEnv env a -> IOEnv env a
unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))


----------------------------------------------------------------------
--	Accessing input/output
----------------------------------------------------------------------

twanvl's avatar
twanvl committed
133
134
135
instance MonadIO (IOEnv env) where
    liftIO io = IOEnv (\ env -> io)

136
137
138
139
ioToIOEnv :: IO a -> IOEnv env a
ioToIOEnv io = IOEnv (\ env -> io)

newMutVar :: a -> IOEnv env (IORef a)
twanvl's avatar
twanvl committed
140
newMutVar val = liftIO (newIORef val)
141
142

writeMutVar :: IORef a -> a -> IOEnv env ()
twanvl's avatar
twanvl committed
143
writeMutVar var val = liftIO (writeIORef var val)
144
145

readMutVar :: IORef a -> IOEnv env a
twanvl's avatar
twanvl committed
146
readMutVar var = liftIO (readIORef var)
147

twanvl's avatar
twanvl committed
148
149
updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
updMutVar var upd = liftIO (modifyIORef var upd)
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


----------------------------------------------------------------------
--	Accessing the environment
----------------------------------------------------------------------

getEnv :: IOEnv env env
{-# INLINE getEnv #-}
getEnv = IOEnv (\ env -> return env)

setEnv :: env' -> IOEnv env' a -> IOEnv env a
{-# INLINE setEnv #-}
setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)

updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
{-# INLINE updEnv #-}
updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))


----------------------------------------------------------------------
--	Standard combinators, but specialised for this monad
--			(for efficiency)
----------------------------------------------------------------------

mappM  	      :: (a -> IOEnv env b) -> [a] -> IOEnv env [b]
mappM_ 	      :: (a -> IOEnv env b) -> [a] -> IOEnv env ()
176
mapSndM       :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)]
177
178
	-- Funny names to avoid clash with Prelude
sequenceM     :: [IOEnv env a] -> IOEnv env [a]
179
sequenceM_    :: [IOEnv env a] -> IOEnv env ()
180
foldlM        :: (a -> b -> IOEnv env a)  -> a -> [b] -> IOEnv env a
181
foldrM        :: (b -> a -> IOEnv env a)  -> a -> [b] -> IOEnv env a
182
183
mapAndUnzipM  :: (a -> IOEnv env (b,c))   -> [a] -> IOEnv env ([b],[c])
mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
184
185
checkM	      :: Bool -> IOEnv env a -> IOEnv env ()	-- Perform arg if bool is False
ifM	      :: Bool -> IOEnv env a -> IOEnv env ()	-- Perform arg if bool is True
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
186
anyM	      :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool
187
188
189
190

mappM f []     = return []
mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }

191
192
193
mapSndM f []     = return []
mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }

194
195
196
mappM_ f []     = return ()
mappM_ f (x:xs) = f x >> mappM_ f xs

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
197
198
199
200
anyM f [] = return False
anyM f (x:xs) = do { b <- f x; if b then return True 
				    else anyM f xs }

201
202
203
204
205
206
207
208
209
210
zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
zipWithM f [] bs = return []
zipWithM f as [] = return []
zipWithM f (a:as) (b:bs) = do { r <- f a b; rs <- zipWithM f as bs; return (r:rs) } 

zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env ()
zipWithM_ f [] bs = return ()
zipWithM_ f as [] = return ()
zipWithM_ f (a:as) (b:bs) = do { f a b; zipWithM_ f as bs } 

211
212
213
sequenceM [] = return []
sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }

214
215
216
sequenceM_ []     = return ()
sequenceM_ (x:xs) = do { x; sequenceM_ xs }

217
218
219
foldlM k z [] = return z
foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }

220
221
222
foldrM k z [] = return z
foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }

223
224
225
226
227
228
229
230
231
232
233
mapAndUnzipM f []     = return ([],[])
mapAndUnzipM f (x:xs) = do { (r,s) <- f x; 
			     (rs,ss) <- mapAndUnzipM f xs; 
			     return (r:rs, s:ss) }

mapAndUnzip3M f []     = return ([],[], [])
mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x; 
			      (rs,ss,ts) <- mapAndUnzip3M f xs; 
			      return (r:rs, s:ss, t:ts) }

checkM True  err = return ()
234
checkM False err = do { err; return () }
235

236
ifM True  do_it = do { do_it; return () }
237
ifM False do_it = return ()