Proposal: Add "writer" Monad instance (, ) o to Control.Monad.Instances

This proposal [1] was originally submitted to Trac by Conal Elliott, but it was apparently abandoned and closed after some time. I've picked it back up and written out a patch. Here is the text of the proposal:
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)

On Thu, Sep 23, 2010 at 12:58 PM, Jake McArthur
where the return definition relies on the Applicative instance of ((,) o). Written out explicitly,
return a = (mempty,a)
I didn't realize we had an applicative instance for ((,) a). That being the case, +1 for this proposal. I would just like to change ((,) o) to ((,) a) to be consistent with the Applicative instance [1]. [1] http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Control-Appli... Cheers! =) -- Felipe.

On Thu, Sep 23, 2010 at 5:58 PM, Jake McArthur
I'd like to have a (,)-style writer instance alongside the (->)-based reader instance for Monad in Control.Monad.Instances.
+1
On Thu, Sep 23, 2010 at 6:19 PM, Felipe Lessa
I would just like to change ((,) o) to ((,) a) to be consistent with the Applicative instance [1].
Not that I find this terribly important, but I'm more in favor of making the variable names descriptive and consistent with mtl/transformers. So: ((->) r) a i.e. Reader r a ((,) w) a i.e. Writer w a Also the 'a' in ((,) a) is confusing since the 'value' parameter of a Monad is usually also called 'a', as in 'Reader r a'. Regards, Bas

+1, especially since the Applicative instance is already there.
On Sep 23, 2010, at 11:58 AM, Jake McArthur
This proposal [1] was originally submitted to Trac by Conal Elliott, but it was apparently abandoned and closed after some time. I've picked it back up and written out a patch. Here is the text of the proposal:
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)
- Jake
[1] http://hackage.haskell.org/trac/ghc/ticket/1951 _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1
I'd also prefer the variable to be called 'w' like for the writer
Monad. But that's a bikeshed, so I don't mind either way.
On 23 September 2010 16:58, Jake McArthur
This proposal [1] was originally submitted to Trac by Conal Elliott, but it was apparently abandoned and closed after some time. I've picked it back up and written out a patch. Here is the text of the proposal:
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)
- Jake
[1] http://hackage.haskell.org/trac/ghc/ticket/1951 _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Push the envelope. Watch it bend.

FWIW, I think I used "o" for "output". I'd go with (->) i and (,) o .
On Fri, Sep 24, 2010 at 2:44 AM, Thomas Schilling
+1
I'd also prefer the variable to be called 'w' like for the writer Monad. But that's a bikeshed, so I don't mind either way.
On 23 September 2010 16:58, Jake McArthur
wrote: This proposal [1] was originally submitted to Trac by Conal Elliott, but it was apparently abandoned and closed after some time. I've picked it back up and written out a patch. Here is the text of the proposal:
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)
- Jake
[1] http://hackage.haskell.org/trac/ghc/ticket/1951 _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Push the envelope. Watch it bend. _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
participants (6)
-
Bas van Dijk
-
Conal Elliott
-
Edward Kmett
-
Felipe Lessa
-
Jake McArthur
-
Thomas Schilling