Consideration to add `ordering` function to `Data.Ord`

Ian Treyball ict2102 at columbia.edu
Sat Jan 2 02:14:31 UTC 2021


Good day,

In short, I think it would be nice to add the following ordering
function to the Data.Ord module:

-- | Case analysis for the 'Ordering' type. @'ordering' x y z o @
evaluates to @x@
-- when @o@ is 'LT', @y@ when @o@ is EQ, and evaluates to @z@ when @o@ is 'GT'.
ordering :: a -> a -> a -> Ordering -> a
ordering lt _  _  LT = lt
ordering _  eq _  EQ = eq
ordering _  _  gt GT = gt

This would be essentially analogous to the bool function from Data.Bool:

bool :: a -> a -> Bool -> a
bool f _ False = f
bool _ t True  = t

Because the bool function is the case-analysis for the Bool data type,
and the order of the parameters matches the order in which the
constructors are defined:

Data Bool = False | True

I decided it would be consistent to follow the same approach for the
ordering implementation and so I use the particular ordering (no pun
intended) for the parameters to also match the order of the
constructors:

Data Ordering = LT | EQ | GT

I will also add an example use case, here is how show could be
implemented using the proposed function (of course, we momentarily
assume it is not derived, for sake of simplicity):

instance Show Ordering where
  show :: Ordering -> String
  show = ordering "LT" "EQ" "GT"

Please let me know if you would wish for me to share more compelling
examples, I have a few, but they are longer (so not here included for
brevity's sake).

I hope the formatting comes out okay, I will link to a gist[1], just in case.

If this sounds acceptable, please let me know how to proceed (would
this fall under "Core Libraries Proposal"[2]? I don't necessarily see
adding this function as a breaking change, so I'm not sure, but I can
surely fill one out if it's appropriate); otherwise, thank you for
your time and consideration.

Respectfully,

Ian Treyball

[1] https://gist.github.com/subttle/b49762a929f25e349381ef161bbc33d0

[2] https://github.com/haskell-core/core-libraries-proposals


More information about the Libraries mailing list