@@ -5,11 +5,16 @@ import Control.Monad.Identity
55
66%access public
77
8+||| A monad representing a computation that produces a stream of output
89class (Monoid w, Monad m) => MonadWriter w (m : Type -> Type) where
10+ ||| tell w produces the output w
911 tell : w -> m ()
12+ ||| Execute an action and add it's output to the value of the computation
1013 listen : m a -> m (a, w)
14+ ||| Execute an action and apply the returned function to the output
1115 pass : m (a, w -> w) -> m a
1216
17+||| The transformer base of the Writer monad
1318record WriterT : Type -> (Type -> Type) -> Type -> Type where
1419 WR : {m : Type -> Type} ->
1520 (runWriterT : m (a, w)) -> WriterT w m a
@@ -39,14 +44,17 @@ instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
3944 pass (WR m) = WR $ do ((a, f), w) <- m
4045 return (a, f w)
4146
47+||| Run an action in the Writer monad and transform the written output
4248listens : MonadWriter w m => (w -> b) -> m a -> m (a, b)
4349listens f m = listens' f (listen m) where
4450 listens' f' ma = do (a, w) <- ma
4551 return (a, f' w)
4652
53+||| Run an action, apply a function to it's output, and return it's value
4754censor : MonadWriter w m => (w -> w) -> m a -> m a
4855censor f m = pass $ do a <- m
4956 return (a, f)
5057
58+||| The Writer monad itself. See the MonadWriter class
5159Writer : Type -> Type -> Type
5260Writer w a = WriterT w Identity a