Proposal for Max/Min Monoids Trac ticket 3036

William Pearson wil.pearson at gmail.com
Fri Feb 20 08:57:22 EST 2009


2009/2/20 David Menendez <dave at zednenem.com>:
> I like the Min/Max monoids, and the possibility of adding something
> like AddBounds (although I'd prefer a name like "Bounded"; "Bounded
> Integer" reads better than "AddBounds Integer").

We already have a Bounded typeclass, so things would get confusing.
How about BoundsAdded Integer?

> One possible snag is that, so far as I know, there's no explicit
> requirement that a type that instantiates Ord and Bounded should
> satisfy the laws,
>
>    min maxBound a = a = min a maxBound
>    max minBound a = a = max a minBound
>
> We're just assuming (reasonably) that instances will always work out
> that way. (Although, I guess we're making the same assumption with Sum
> and Prod.)
>

You could do something like

class (Bounded a, Ord a) => BoundedOrd a where
     minB :: a -> a -> a
     minB a b | b == maxBound =  a
              | a == maxBound =  b
              | (a == minBound) || (b == minBound) =  minBound
              | otherwise = min a b

     maxB :: a -> a -> a
     maxB a b | b == minBound =  a
              | a == minBound =  b
              | (a == maxBound) || (b == maxBound) =  maxBound
              | otherwise = max a b

And have the types inside Min and Max monoids need to be  instances of
this type. It depends how much we are trusting the implementors of min
and max... It also doesn't stop people reimplementing them, perhaps
we'll have to wait for conal's type class morphisms (I haven't fully
digested these yet)?

  Will Pearson


More information about the Libraries mailing list