Proposal: add "writer" Monad instance (,) o (Ticket #1951)

I'd like to have a (,)-style writer instance alongside the (->)-based reader instance for Monad in Control.Monad.Instances. Here's the current reader: instance Monad ((->) r) where return = const f >>= k = \ r -> k (f r) r and my proposed writer: instance Monoid o => Monad ((,) o) where return = pure (o,a) >>= f = (o `mappend` o', a') where (o',a') = f a where the return definition relies on the Applicative instance of ((,) o). Written out explicitly, return a = (mempty,a) Control.Monad.Instances will also need two new imports: import Data.Monoid (Monoid(..)) import Control.Applicative (pure) Suggested review period: two weeks (ending Dec 16). Comments? - Conal

In State, the tuple order that is use is
newtype State s a = State { runState :: (s -> (a, s)) }
So I would expect you to use the same order for writer. This only changes (>>) below:
instance Monoid o => Monad ((,) o) where return = (a,mempty) (a,o) >>= f = (a',o `mappend` o') where (a',o') = f a
-- Chris

On Sun, Dec 02, 2007 at 09:27:06AM -0800, Conal Elliott wrote:
I'd like to have a (,)-style writer instance alongside the (->)-based reader instance for Monad in Control.Monad.Instances.
Might as well, as there is already a matching Applicative instance. Control.Monad.Instances is the right place to put the Monad instance, to avoid breaking H98 compatibility. (I'm not sure the Control.Applicative dependency is worth it, but that's an internal detail.)
participants (3)
-
Chris Kuklewicz
-
Conal Elliott
-
Ross Paterson