Data.Monoid

Ashley Yakeley ashley at semantic.org
Thu Oct 27 00:55:07 EDT 2005


In article <20051024094010.GA4888 at soi.city.ac.uk>,
 Ross Paterson <ross at soi.city.ac.uk> wrote:

> To recap: with Haskell's class system, we can have only one (->) instance,
> so we have to choose.

There are many possibilities:

  instance Monoid [a] where
    mempty = []
    mappend = (++)

  instance Monoid (a -> a) where
    mempty  = id
    mappend = (.)

  instance Monoid b => Monoid (a -> b) where
    mempty _ = mempty
    mappend f g x = f x `mappend` g x

  instance Monad m => Monoid (m ()) where
    mempty = return ()
    mappend = (>>)

  instance MonadPlus m => Monoid (m a) where
    mempty = mzero
    mappend = mplus

  instance Arrow arr => Monoid (arr p p) where
    mempty = arr id
    mappend = (>>>)

  instance Monoid a => Monoid a where
    mempty = mempty
    mappend p q = mappend q p

OK, so the last one's silly. But maybe Monoid should be a datatype 
rather than a class?

-- 
Ashley Yakeley, Seattle WA



More information about the Libraries mailing list