Proposal: Max and Min for Monoid

Felipe Lessa felipe.lessa at gmail.com
Thu Sep 23 12:14:10 EDT 2010


On Thu, Sep 23, 2010 at 12:58 PM, Jake McArthur <jake.mcarthur at gmail.com> wrote:
> [1] http://hackage.haskell.org/trac/ghc/ticket/1951

I think this isn't the right link.  It should be

http://hackage.haskell.org/trac/ghc/ticket/1952

>>    -- | 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)

Why should we prefer this monoid over

> data Max a = Minimum | Max a
>              deriving (Eq, Ord, Read, Show)
>
> instance Ord a => Monoid (Max a) where
>   mempty = Minimum
>   Minimum `mappend` x   = x
>   x `mappend` Minimum   = x
>   Max a `mappend` Max b = Max (a `max` b)

Or should we have both variants?  Or should we have something like

> data AddBounds a = Minimum | This a | Maximum
>                    deriving (Eq, Ord, Read, Show)
>
> instance Bounded (AddBounds a) where
>   minBound = Minimum
>   maxBound = Maximum

Cheers! =)

--
Felipe.


More information about the Libraries mailing list