8515803e17b399592f466dcfaadf466a4f20baff
[darcs-mirrors/transformers.git] / Control / Monad / Trans / RWS / Lazy.hs
1 {-# LANGUAGE CPP #-}
2 #if __GLASGOW_HASKELL__ >= 702
3 {-# LANGUAGE Safe #-}
4 #endif
5 #if __GLASGOW_HASKELL__ >= 710
6 {-# LANGUAGE AutoDeriveTypeable #-}
7 #endif
8 -----------------------------------------------------------------------------
9 -- |
10 -- Module : Control.Monad.Trans.RWS.Lazy
11 -- Copyright : (c) Andy Gill 2001,
12 -- (c) Oregon Graduate Institute of Science and Technology, 2001
13 -- License : BSD-style (see the file LICENSE)
14 --
15 -- Maintainer : R.Paterson@city.ac.uk
16 -- Stability : experimental
17 -- Portability : portable
18 --
19 -- A monad transformer that combines 'ReaderT', 'WriterT' and 'StateT'.
20 -- This version is lazy; for a strict version with the same interface,
21 -- see "Control.Monad.Trans.RWS.Strict".
22 -----------------------------------------------------------------------------
23
24 module Control.Monad.Trans.RWS.Lazy (
25 -- * The RWS monad
26 RWS,
27 rws,
28 runRWS,
29 evalRWS,
30 execRWS,
31 mapRWS,
32 withRWS,
33 -- * The RWST monad transformer
34 RWST(..),
35 evalRWST,
36 execRWST,
37 mapRWST,
38 withRWST,
39 -- * Reader operations
40 reader,
41 ask,
42 local,
43 asks,
44 -- * Writer operations
45 writer,
46 tell,
47 listen,
48 listens,
49 pass,
50 censor,
51 -- * State operations
52 state,
53 get,
54 put,
55 modify,
56 gets,
57 -- * Lifting other operations
58 liftCallCC,
59 liftCallCC',
60 liftCatch,
61 ) where
62
63 import Control.Monad.IO.Class
64 import Control.Monad.Signatures
65 import Control.Monad.Trans.Class
66 import Data.Functor.Identity
67
68 import Control.Applicative
69 import Control.Monad
70 import Control.Monad.Fix
71 import Data.Monoid
72
73 -- | A monad containing an environment of type @r@, output of type @w@
74 -- and an updatable state of type @s@.
75 type RWS r w s = RWST r w s Identity
76
77 -- | Construct an RWS computation from a function.
78 -- (The inverse of 'runRWS'.)
79 rws :: (r -> s -> (a, s, w)) -> RWS r w s a
80 rws f = RWST (\ r s -> Identity (f r s))
81
82 -- | Unwrap an RWS computation as a function.
83 -- (The inverse of 'rws'.)
84 runRWS :: RWS r w s a -> r -> s -> (a, s, w)
85 runRWS m r s = runIdentity (runRWST m r s)
86
87 -- | Evaluate a computation with the given initial state and environment,
88 -- returning the final value and output, discarding the final state.
89 evalRWS :: RWS r w s a -- ^RWS computation to execute
90 -> r -- ^initial environment
91 -> s -- ^initial value
92 -> (a, w) -- ^final value and output
93 evalRWS m r s = let
94 (a, _, w) = runRWS m r s
95 in (a, w)
96
97 -- | Evaluate a computation with the given initial state and environment,
98 -- returning the final state and output, discarding the final value.
99 execRWS :: RWS r w s a -- ^RWS computation to execute
100 -> r -- ^initial environment
101 -> s -- ^initial value
102 -> (s, w) -- ^final state and output
103 execRWS m r s = let
104 (_, s', w) = runRWS m r s
105 in (s', w)
106
107 -- | Map the return value, final state and output of a computation using
108 -- the given function.
109 --
110 -- * @'runRWS' ('mapRWS' f m) r s = f ('runRWS' m r s)@
111 mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
112 mapRWS f = mapRWST (Identity . f . runIdentity)
113
114 -- | @'withRWS' f m@ executes action @m@ with an initial environment
115 -- and state modified by applying @f@.
116 --
117 -- * @'runRWS' ('withRWS' f m) r s = 'uncurry' ('runRWS' m) (f r s)@
118 withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
119 withRWS = withRWST
120
121 -- ---------------------------------------------------------------------------
122 -- | A monad transformer adding reading an environment of type @r@,
123 -- collecting an output of type @w@ and updating a state of type @s@
124 -- to an inner monad @m@.
125 newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }
126
127 -- | Evaluate a computation with the given initial state and environment,
128 -- returning the final value and output, discarding the final state.
129 evalRWST :: (Monad m)
130 => RWST r w s m a -- ^computation to execute
131 -> r -- ^initial environment
132 -> s -- ^initial value
133 -> m (a, w) -- ^computation yielding final value and output
134 evalRWST m r s = do
135 ~(a, _, w) <- runRWST m r s
136 return (a, w)
137
138 -- | Evaluate a computation with the given initial state and environment,
139 -- returning the final state and output, discarding the final value.
140 execRWST :: (Monad m)
141 => RWST r w s m a -- ^computation to execute
142 -> r -- ^initial environment
143 -> s -- ^initial value
144 -> m (s, w) -- ^computation yielding final state and output
145 execRWST m r s = do
146 ~(_, s', w) <- runRWST m r s
147 return (s', w)
148
149 -- | Map the inner computation using the given function.
150 --
151 -- * @'runRWST' ('mapRWST' f m) r s = f ('runRWST' m r s)@
152 mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
153 mapRWST f m = RWST $ \ r s -> f (runRWST m r s)
154
155 -- | @'withRWST' f m@ executes action @m@ with an initial environment
156 -- and state modified by applying @f@.
157 --
158 -- * @'runRWST' ('withRWST' f m) r s = 'uncurry' ('runRWST' m) (f r s)@
159 withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
160 withRWST f m = RWST $ \ r s -> uncurry (runRWST m) (f r s)
161
162 instance (Functor m) => Functor (RWST r w s m) where
163 fmap f m = RWST $ \ r s ->
164 fmap (\ ~(a, s', w) -> (f a, s', w)) $ runRWST m r s
165
166 instance (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) where
167 pure = return
168 (<*>) = ap
169
170 instance (Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) where
171 empty = mzero
172 (<|>) = mplus
173
174 instance (Monoid w, Monad m) => Monad (RWST r w s m) where
175 return a = RWST $ \ _ s -> return (a, s, mempty)
176 m >>= k = RWST $ \ r s -> do
177 ~(a, s', w) <- runRWST m r s
178 ~(b, s'',w') <- runRWST (k a) r s'
179 return (b, s'', w `mappend` w')
180 fail msg = RWST $ \ _ _ -> fail msg
181
182 instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
183 mzero = RWST $ \ _ _ -> mzero
184 m `mplus` n = RWST $ \ r s -> runRWST m r s `mplus` runRWST n r s
185
186 instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
187 mfix f = RWST $ \ r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s
188
189 instance (Monoid w) => MonadTrans (RWST r w s) where
190 lift m = RWST $ \ _ s -> do
191 a <- m
192 return (a, s, mempty)
193
194 instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
195 liftIO = lift . liftIO
196
197 -- ---------------------------------------------------------------------------
198 -- Reader operations
199
200 -- | Constructor for computations in the reader monad (equivalent to 'asks').
201 reader :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
202 reader = asks
203
204 -- | Fetch the value of the environment.
205 ask :: (Monoid w, Monad m) => RWST r w s m r
206 ask = RWST $ \ r s -> return (r, s, mempty)
207
208 -- | Execute a computation in a modified environment
209 --
210 -- * @'runRWST' ('local' f m) r s = 'runRWST' m (f r) s@
211 local :: (r -> r) -> RWST r w s m a -> RWST r w s m a
212 local f m = RWST $ \ r s -> runRWST m (f r) s
213
214 -- | Retrieve a function of the current environment.
215 --
216 -- * @'asks' f = 'liftM' f 'ask'@
217 asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
218 asks f = RWST $ \ r s -> return (f r, s, mempty)
219
220 -- ---------------------------------------------------------------------------
221 -- Writer operations
222
223 -- | Construct a writer computation from a (result, output) pair.
224 writer :: (Monad m) => (a, w) -> RWST r w s m a
225 writer (a, w) = RWST $ \ _ s -> return (a, s, w)
226
227 -- | @'tell' w@ is an action that produces the output @w@.
228 tell :: (Monad m) => w -> RWST r w s m ()
229 tell w = RWST $ \ _ s -> return ((),s,w)
230
231 -- | @'listen' m@ is an action that executes the action @m@ and adds its
232 -- output to the value of the computation.
233 --
234 -- * @'runRWST' ('listen' m) r s = 'liftM' (\\ (a, w) -> ((a, w), w)) ('runRWST' m r s)@
235 listen :: (Monad m) => RWST r w s m a -> RWST r w s m (a, w)
236 listen m = RWST $ \ r s -> do
237 ~(a, s', w) <- runRWST m r s
238 return ((a, w), s', w)
239
240 -- | @'listens' f m@ is an action that executes the action @m@ and adds
241 -- the result of applying @f@ to the output to the value of the computation.
242 --
243 -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@
244 --
245 -- * @'runRWST' ('listens' f m) r s = 'liftM' (\\ (a, w) -> ((a, f w), w)) ('runRWST' m r s)@
246 listens :: (Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
247 listens f m = RWST $ \ r s -> do
248 ~(a, s', w) <- runRWST m r s
249 return ((a, f w), s', w)
250
251 -- | @'pass' m@ is an action that executes the action @m@, which returns
252 -- a value and a function, and returns the value, applying the function
253 -- to the output.
254 --
255 -- * @'runRWST' ('pass' m) r s = 'liftM' (\\ ((a, f), w) -> (a, f w)) ('runRWST' m r s)@
256 pass :: (Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a
257 pass m = RWST $ \ r s -> do
258 ~((a, f), s', w) <- runRWST m r s
259 return (a, s', f w)
260
261 -- | @'censor' f m@ is an action that executes the action @m@ and
262 -- applies the function @f@ to its output, leaving the return value
263 -- unchanged.
264 --
265 -- * @'censor' f m = 'pass' ('liftM' (\\ x -> (x,f)) m)@
266 --
267 -- * @'runRWST' ('censor' f m) r s = 'liftM' (\\ (a, w) -> (a, f w)) ('runRWST' m r s)@
268 censor :: (Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
269 censor f m = RWST $ \ r s -> do
270 ~(a, s', w) <- runRWST m r s
271 return (a, s', f w)
272
273 -- ---------------------------------------------------------------------------
274 -- State operations
275
276 -- | Construct a state monad computation from a state transformer function.
277 state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a
278 state f = RWST $ \ _ s -> let (a,s') = f s in return (a, s', mempty)
279
280 -- | Fetch the current value of the state within the monad.
281 get :: (Monoid w, Monad m) => RWST r w s m s
282 get = RWST $ \ _ s -> return (s, s, mempty)
283
284 -- | @'put' s@ sets the state within the monad to @s@.
285 put :: (Monoid w, Monad m) => s -> RWST r w s m ()
286 put s = RWST $ \ _ _ -> return ((), s, mempty)
287
288 -- | @'modify' f@ is an action that updates the state to the result of
289 -- applying @f@ to the current state.
290 --
291 -- * @'modify' f = 'get' >>= ('put' . f)@
292 modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m ()
293 modify f = RWST $ \ _ s -> return ((), f s, mempty)
294
295 -- | Get a specific component of the state, using a projection function
296 -- supplied.
297 --
298 -- * @'gets' f = 'liftM' f 'get'@
299 gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a
300 gets f = RWST $ \ _ s -> return (f s, s, mempty)
301
302 -- | Uniform lifting of a @callCC@ operation to the new monad.
303 -- This version rolls back to the original state on entering the
304 -- continuation.
305 liftCallCC :: (Monoid w) =>
306 CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
307 liftCallCC callCC f = RWST $ \ r s ->
308 callCC $ \ c ->
309 runRWST (f (\ a -> RWST $ \ _ _ -> c (a, s, mempty))) r s
310
311 -- | In-situ lifting of a @callCC@ operation to the new monad.
312 -- This version uses the current state on entering the continuation.
313 liftCallCC' :: (Monoid w) =>
314 CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
315 liftCallCC' callCC f = RWST $ \ r s ->
316 callCC $ \ c ->
317 runRWST (f (\ a -> RWST $ \ _ s' -> c (a, s', mempty))) r s
318
319 -- | Lift a @catchE@ operation to the new monad.
320 liftCatch :: Catch e m (a,s,w) -> Catch e (RWST r w s m) a
321 liftCatch catchE m h =
322 RWST $ \ r s -> runRWST m r s `catchE` \ e -> runRWST (h e) r s