Data.Monoid

Ross Paterson ross at soi.city.ac.uk
Wed Sep 14 05:06:19 EDT 2005


On Tue, Sep 13, 2005 at 08:35:39PM -0400, Cale Gibbard wrote:
> On 13/09/05, Ross Paterson <ross at soi.city.ac.uk> wrote:
> > (regurgitating
> > http://www.haskell.org/pipermail/libraries/2005-July/004057.html)
> > 
> > I propose to replace the instance
> > 
> >         instance Monoid (a -> a) where
> >                 mempty  = id
> >                 mappend = (.)
> > 
> > with
> > 
> >         newtype Endo a = Endo { runEndo :: a -> a }
> > 
> >         instance Monoid (Endo a) where
> >                 mempty = Endo id
> >                 Endo f `mappend` Endo g = Endo (f . g)
> > 
> >         instance Monoid b => Monoid (a -> b) where
> >                 mempty _ = mempty
> >                 mappend f g x = f x `mappend` g x
> 
> I don't think it's clear which of the two instances is more useful. I
> would actually probably consider the existing instance more
> fundamental, and get more use out of it. (It's occasionally quite
> handy together with WriterT), but who knows - perhaps it's best if
> they're both wrapped in newtypes for the time being.

The old instance is handy with the writer monad, but it's a dead end;
the proposed (pointwise) one lets you lift through a series of function
types, just as you can with tuple types.  Fiddling with the Endo newtype
will be some bother, but simulating the pointwise lifting by hand is
much more awkward.



More information about the Libraries mailing list