@@ -0,0 +1,41 @@
1+module Control.Monad.Writer
4+import Control.Monad.Identity
6+import Prelude.Applicative
11+class (Monoid w, Monad m) => MonadWriter w (m : Type -> Type) where
14+record WriterT : Type -> (Type -> Type) -> Type -> Type where
15+ WR : {m : Type -> Type} ->
16+ (runWriterT : m (a, w)) -> WriterT w m a
18+instance Functor f => Functor (WriterT w f) where
19+ map f (WR g) = WR $ map (\w => (f . fst $ w, snd w)) g
21+instance (Monoid w, Applicative m) => Applicative (WriterT w m) where
22+ pure a = WR $ pure (a, neutral)
23+ (WR f) <$> (WR v) = WR $ liftA2 merge f v where
24+ merge (fn, w) (a, w') = (fn a, w <+> w')
26+instance (Monoid w, Alternative m) => Alternative (WriterT w m) where
28+ (WR m) <|> (WR n) = WR $ m <|> n
30+instance (Monoid w, Monad m) => Monad (WriterT w m) where
31+ (WR m) >>= k = WR $ do (a, w) <- m
36+instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
37+ tell w = WR $ return ((), w)
40+Writer : Type -> Type -> Type
41+Writer w a = WriterT w Identity a