Proposal: Max and Min for Monoid

Maciej Piechotka uzytkownik2 at gmail.com
Thu Sep 23 14:48:20 EDT 2010


On Thu, 2010-09-23 at 13:14 -0300, Felipe Lessa wrote:
> 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.

The original version:
 - Uses newtype which would be more efficient
 - For many practical uses the Min/Max have concrete, 'natural' values 
 - It is analogy to Sum and Product from Data.Monoid
 - It is simpler to use fromMax :: Max a -> a then fromMax :: Max a ->
Maybe a
 - It shows that the bounded types form a monoid rather then wrappes
arbitrary type into monoid

Reagrds
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/libraries/attachments/20100923/e60d697b/attachment.bin


More information about the Libraries mailing list