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

Edward Kmett ekmett at gmail.com
Thu Sep 23 14:49:36 EDT 2010


+1, especially since the Applicative instance is already there.

On Sep 23, 2010, at 11:58 AM, Jake McArthur <jake.mcarthur at gmail.com> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries


More information about the Libraries mailing list