Proposal: Max and Min for Monoid (ticket # 1952)

Conal Elliott conal at conal.net
Mon Dec 3 15:38:09 EST 2007


Similarly, I'm using the following to get around type parameters that don't
provide Bounded:

data AddBounds a = MinBound | NoBound a | MaxBound
  deriving (Eq, Ord, Read, Show)

instance Bounded (AddBounds a) where
  minBound = MinBound
  maxBound = MaxBound


On Dec 2, 2007 3:25 PM, Ross Paterson <ross at soi.city.ac.uk> wrote:

> On Sun, Dec 02, 2007 at 09:57:50AM -0800, Conal Elliott wrote:
> > My proposed addition:
> >
> > -- | Ordered monoid under 'max'.
> > newtype Max a = Max { getMax :: a }
> >       deriving (Eq, Ord, Read, Show, Bounded)
> >
> > instance (Ord a, Bounded a) => Monoid (Max a) where
> >       mempty = Max minBound
> >       Max a `mappend` Max b = Max (a `max` b)
>
> Funny, I was thinking of proposing a Max type that adjoined a synthetic
> identity, as we did in the finger tree paper:
>
> data Max a = NoMax | Max a
>        deriving (Eq, Ord, Read, Show)
>
> instance Ord a => Monoid (Max a) where
>        mempty = NoMax
>        NoMax `mappend` b = b
>        a `mappend` NoMax = a
>        Max x `mappend` Max y = Max (x `max` y)
>
> and similarly for Min.  One could even define
>
>        getMax :: Bounded a => Max a -> a
>        getMax NoMax = minBound
>        getMax (Max x) = x
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/libraries/attachments/20071203/3241aa54/attachment.htm


More information about the Libraries mailing list