Questions regarding Bounded
Henning Thielemann
lemming at henning-thielemann.de
Mon May 4 13:29:56 UTC 2020
On Mon, 4 May 2020, Simon Jakobi via Libraries wrote:
> Possibly relatedly, I was looking for Bounded instances for Maybe and
> Either. To me the following instances seem sensible:
>
> instance Bounded a => Bounded (Maybe a) where
> minBound = Nothing
> maxBound = Just maxBound
>
> instance (Bounded a, Bounded b) => Bounded (Either a b) where
> minBound = Left minBound
> maxBound = Right maxBound
>
> Are these instances omitted for a reason? Interestingly, both types do have corresponding Ord instances.
Unfortunately, Ord is dual use:
1. Comparison for magnitude as suggested by the mathematical symbols (<)
and (>).
2. Some arbitrary but total order for use in Set and Map.
E.g. Set (Complex a) makes total sense, but 1:+0 > 0:+1 is unexpected. Ord
instances on pairs is sensible for Set and Map, but (max (1,0) (0,1) ==
(1,0)) is strange.
I would not interpret too much into (Bounded a => Maybe a). Nothing may
mean "smaller than any Just element", but it may also mean "larger than
any Just element" or "no regular element at all".
Consider a refactoring where a numeric value is extended by Maybe. Do we
want that comparisons and bounds are silently re-interpreted or would we
like to be alerted that comparisons and bounds don't make sense anymore?
I'd prefer the alert. Unfortunately, Ord Maybe is already defined.
For an extra largest or smallest element I would define a custom
Maybe-like datatype.
More information about the Libraries
mailing list