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

Tikhon Jelvis tikhon at jelv.is
Mon Jan 11 20:15:06 UTC 2021


This seems good for consistency since we provide corresponding functions
for other basic types (bool, maybe, either). Sticking to consistent
patterns would make Haskell's libraries easier to learn.

On Mon, Jan 11, 2021 at 1:11 AM George Wilson <george at wils.online> wrote:

> I am weakly in favour of this.
> Having catamorphisms for these basic types seems handy, although I
> think I would personally almost always use a pattern match in the case
> of Ordering.
>
> Cheers,
> George
>
> On Sat, 2 Jan 2021 at 12:15, Ian Treyball <ict2102 at columbia.edu> wrote:
> >
> > 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
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20210111/83ddc85e/attachment.html>


More information about the Libraries mailing list