@@ -5,11 +5,16 @@ import Control.Monad.Identity
6%access public
6%access public
8+||| A monad representing a computation that produces a stream of output
8class (Monoid w, Monad m) => MonadWriter w (m : Type -> Type) where
9class (Monoid w, Monad m) => MonadWriter w (m : Type -> Type) where
10+ ||| tell w produces the output w
9 tell : w -> m ()
11 tell : w -> m ()
12+ ||| Execute an action and add it's output to the value of the computation
10 listen : m a -> m (a, w)
13 listen : m a -> m (a, w)
14+ ||| Execute an action and apply the returned function to the output
11 pass : m (a, w -> w) -> m a
15 pass : m (a, w -> w) -> m a
17+||| The transformer base of the Writer monad
13record WriterT : Type -> (Type -> Type) -> Type -> Type where
18record WriterT : Type -> (Type -> Type) -> Type -> Type where
14 WR : {m : Type -> Type} ->
19 WR : {m : Type -> Type} ->
15 (runWriterT : m (a, w)) -> WriterT w m a
20 (runWriterT : m (a, w)) -> WriterT w m a
@@ -39,14 +44,17 @@ instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
39 pass (WR m) = WR $ do ((a, f), w) <- m
44 pass (WR m) = WR $ do ((a, f), w) <- m
40 return (a, f w)
45 return (a, f w)
47+||| Run an action in the Writer monad and transform the written output
42listens : MonadWriter w m => (w -> b) -> m a -> m (a, b)
48listens : MonadWriter w m => (w -> b) -> m a -> m (a, b)
43listens f m = listens' f (listen m) where
49listens f m = listens' f (listen m) where
44 listens' f' ma = do (a, w) <- ma
50 listens' f' ma = do (a, w) <- ma
45 return (a, f' w)
51 return (a, f' w)
53+||| Run an action, apply a function to it's output, and return it's value
47censor : MonadWriter w m => (w -> w) -> m a -> m a
54censor : MonadWriter w m => (w -> w) -> m a -> m a
48censor f m = pass $ do a <- m
55censor f m = pass $ do a <- m
49 return (a, f)
56 return (a, f)
58+||| The Writer monad itself. See the MonadWriter class
51Writer : Type -> Type -> Type
59Writer : Type -> Type -> Type
52Writer w a = WriterT w Identity a
60Writer w a = WriterT w Identity a