Cont as Monoid

Iavor Diatchki iavor.diatchki at gmail.com
Fri Sep 21 17:01:24 EDT 2007


Hi,

On 9/18/07, Benjamin Franksen <benjamin.franksen at bessy.de> wrote:
> Ashley Yakeley wrote:
> > Conor McBride wrote:
> >> My usual rule of thumb is that inherent natural monoidal structure
> >> should have
> >> a higher priority than just applicative lifting of monoidal structure
> >> from the value type.
> >
> > Monoid is a bit ridiculous as a class, as there are frequently several
> > useful monoids on a type, leading to a collection of ugly wrapper
> newtypes.
> >
> > Monoid ought to be a type, in my view. And each of those wrapper classes
> > can be replaced by a value in that type.
>
> Would you care to elaborate this idea? Do you mean a record with two
> functions?

Here is how you can do that:

data Monoid a = Monoid { mempty :: a, mappend :: a -> a -> a }

int_add :: Monoid Int
int_add = Monoid { mempty = 0, mappend = (+) }

int_mul :: Monoid Int
int_mul = Monoid { mempty = 1, mappend = (*) }

-- etc...

-Iavor


More information about the Libraries mailing list