expand definitions of Applicative and Alternative methods (fixes #4)
[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 a = RWST $ \ _ s -> return (a, s, mempty)
168 RWST mf <*> RWST mx = RWST $ \ r s -> do
169 ~(f, s', w) <- mf r s
170 ~(x, s'',w') <- mx r s'
171 return (f x, s'', w `mappend` w')
172
173 instance (Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) where
174 empty = RWST $ \ _ _ -> mzero
175 RWST m <|> RWST n = RWST $ \ r s -> m r s `mplus` n r s
176
177 instance (Monoid w, Monad m) => Monad (RWST r w s m) where
178 return a = RWST $ \ _ s -> return (a, s, mempty)
179 m >>= k = RWST $ \ r s -> do
180 ~(a, s', w) <- runRWST m r s
181 ~(b, s'',w') <- runRWST (k a) r s'
182 return (b, s'', w `mappend` w')
183 fail msg = RWST $ \ _ _ -> fail msg
184
185 instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
186 mzero = RWST $ \ _ _ -> mzero
187 RWST m `mplus` RWST n = RWST $ \ r s -> m r s `mplus` n r s
188
189 instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
190 mfix f = RWST $ \ r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s
191
192 instance (Monoid w) => MonadTrans (RWST r w s) where
193 lift m = RWST $ \ _ s -> do
194 a <- m
195 return (a, s, mempty)
196
197 instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
198 liftIO = lift . liftIO
199
200 -- ---------------------------------------------------------------------------
201 -- Reader operations
202
203 -- | Constructor for computations in the reader monad (equivalent to 'asks').
204 reader :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
205 reader = asks
206
207 -- | Fetch the value of the environment.
208 ask :: (Monoid w, Monad m) => RWST r w s m r
209 ask = RWST $ \ r s -> return (r, s, mempty)
210
211 -- | Execute a computation in a modified environment
212 --
213 -- * @'runRWST' ('local' f m) r s = 'runRWST' m (f r) s@
214 local :: (r -> r) -> RWST r w s m a -> RWST r w s m a
215 local f m = RWST $ \ r s -> runRWST m (f r) s
216
217 -- | Retrieve a function of the current environment.
218 --
219 -- * @'asks' f = 'liftM' f 'ask'@
220 asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
221 asks f = RWST $ \ r s -> return (f r, s, mempty)
222
223 -- ---------------------------------------------------------------------------
224 -- Writer operations
225
226 -- | Construct a writer computation from a (result, output) pair.
227 writer :: (Monad m) => (a, w) -> RWST r w s m a
228 writer (a, w) = RWST $ \ _ s -> return (a, s, w)
229
230 -- | @'tell' w@ is an action that produces the output @w@.
231 tell :: (Monad m) => w -> RWST r w s m ()
232 tell w = RWST $ \ _ s -> return ((),s,w)
233
234 -- | @'listen' m@ is an action that executes the action @m@ and adds its
235 -- output to the value of the computation.
236 --
237 -- * @'runRWST' ('listen' m) r s = 'liftM' (\\ (a, w) -> ((a, w), w)) ('runRWST' m r s)@
238 listen :: (Monad m) => RWST r w s m a -> RWST r w s m (a, w)
239 listen m = RWST $ \ r s -> do
240 ~(a, s', w) <- runRWST m r s
241 return ((a, w), s', w)
242
243 -- | @'listens' f m@ is an action that executes the action @m@ and adds
244 -- the result of applying @f@ to the output to the value of the computation.
245 --
246 -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@
247 --
248 -- * @'runRWST' ('listens' f m) r s = 'liftM' (\\ (a, w) -> ((a, f w), w)) ('runRWST' m r s)@
249 listens :: (Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
250 listens f m = RWST $ \ r s -> do
251 ~(a, s', w) <- runRWST m r s
252 return ((a, f w), s', w)
253
254 -- | @'pass' m@ is an action that executes the action @m@, which returns
255 -- a value and a function, and returns the value, applying the function
256 -- to the output.
257 --
258 -- * @'runRWST' ('pass' m) r s = 'liftM' (\\ ((a, f), w) -> (a, f w)) ('runRWST' m r s)@
259 pass :: (Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a
260 pass m = RWST $ \ r s -> do
261 ~((a, f), s', w) <- runRWST m r s
262 return (a, s', f w)
263
264 -- | @'censor' f m@ is an action that executes the action @m@ and
265 -- applies the function @f@ to its output, leaving the return value
266 -- unchanged.
267 --
268 -- * @'censor' f m = 'pass' ('liftM' (\\ x -> (x,f)) m)@
269 --
270 -- * @'runRWST' ('censor' f m) r s = 'liftM' (\\ (a, w) -> (a, f w)) ('runRWST' m r s)@
271 censor :: (Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
272 censor f m = RWST $ \ r s -> do
273 ~(a, s', w) <- runRWST m r s
274 return (a, s', f w)
275
276 -- ---------------------------------------------------------------------------
277 -- State operations
278
279 -- | Construct a state monad computation from a state transformer function.
280 state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a
281 state f = RWST $ \ _ s -> let (a,s') = f s in return (a, s', mempty)
282
283 -- | Fetch the current value of the state within the monad.
284 get :: (Monoid w, Monad m) => RWST r w s m s
285 get = RWST $ \ _ s -> return (s, s, mempty)
286
287 -- | @'put' s@ sets the state within the monad to @s@.
288 put :: (Monoid w, Monad m) => s -> RWST r w s m ()
289 put s = RWST $ \ _ _ -> return ((), s, mempty)
290
291 -- | @'modify' f@ is an action that updates the state to the result of
292 -- applying @f@ to the current state.
293 --
294 -- * @'modify' f = 'get' >>= ('put' . f)@
295 modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m ()
296 modify f = RWST $ \ _ s -> return ((), f s, mempty)
297
298 -- | Get a specific component of the state, using a projection function
299 -- supplied.
300 --
301 -- * @'gets' f = 'liftM' f 'get'@
302 gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a
303 gets f = RWST $ \ _ s -> return (f s, s, mempty)
304
305 -- | Uniform lifting of a @callCC@ operation to the new monad.
306 -- This version rolls back to the original state on entering the
307 -- continuation.
308 liftCallCC :: (Monoid w) =>
309 CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
310 liftCallCC callCC f = RWST $ \ r s ->
311 callCC $ \ c ->
312 runRWST (f (\ a -> RWST $ \ _ _ -> c (a, s, mempty))) r s
313
314 -- | In-situ lifting of a @callCC@ operation to the new monad.
315 -- This version uses the current state on entering the continuation.
316 liftCallCC' :: (Monoid w) =>
317 CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
318 liftCallCC' callCC f = RWST $ \ r s ->
319 callCC $ \ c ->
320 runRWST (f (\ a -> RWST $ \ _ s' -> c (a, s', mempty))) r s
321
322 -- | Lift a @catchE@ operation to the new monad.
323 liftCatch :: Catch e m (a,s,w) -> Catch e (RWST r w s m) a
324 liftCatch catchE m h =
325 RWST $ \ r s -> runRWST m r s `catchE` \ e -> runRWST (h e) r s