PartialOrdering: the less loved brother of Ordering

106 views
Skip to first unread message

Paolo Giarrusso

unread,
Jul 1, 2012, 9:42:12 PM7/1/12
to scala-l...@googlegroups.com
Hi all.

in the Scala standard library PartialOrdering has much less support than Ordering. Since I don't see why, I would like to add more support or understand why it's not possible.

I was implementing some code which relies on Ordering; I then realized that I had in fact to do with a partial ordering, since my ordering is based on PartialOrderingTuple2 (see below). Switching to PartialOrdering was hard because the standard library misses basic combinators to manipulate first-class orders, like Ordering.on. Furthermore, it's annoying that PartialOrdering has two interdefinable abstract methods (lteq and tryCompare) but the implementations are not provided. I did not find improvements in Scalaz, because it reimplements Ordering but not PartialOrdering.

Therefore, I've sketched some improvements for my use (untested code at https://gist.github.com/3030361), and I'd propose a (possibly revised) version for merging in the standard library. If the idea is accepted, I would add more combinators—everything which could be ported from Ordering. I would propose PartialOrderingExt for merging into scala.math.PartialOrdering, while PartialOrderingFromLteq and PartialOrderingFromTryCompare are helper traits for implementations. Since the two methods are interdefinable, it's better to leave them as separate traits, otherwise one could too easily produce a PartialOrdering instance with only non-terminating methods, like happens with some Haskell typeclasses with similarly interdefinable methods, where all definitions are provided by default.

Here's the client code, for reference - shown only to see how I'm using these combinators.
Please ignore the implementation of flagsFrom, it distracts from the main issue at hand, so please bear with me—the answer is quite off-topic, but I could summarize it if anybody is interested.

  /*
   * This ordering expresses the ordering between monoids described by Fegaras and Maier (2001) in
   * "Optimizing Object Queries Using an Effective Calculus", Sec. 2.3.
   */
  implicit def cbfOrdering[From, Elem, To]: PartialOrderingExt[CanBuildFrom[From, Elem, To]] = {
    def flagsFrom(cbf: CanBuildFrom[From, Elem, To]): (Boolean, Boolean) = //omitted
    PartialOrderingExt[(Boolean, Boolean)].on(flagsFrom)
  }

Cheers,
Paolo Giarrusso

Scott Morrison

unread,
Jul 1, 2012, 10:01:39 PM7/1/12
to scala-l...@googlegroups.com
Dear Paolo,

I would love to see better support for PartialOrdering in the library,
as I've already written less-than-optimal code several times to
provide pieces of what you have in mind.

best,
Scott Morrison
Reply all
Reply to author
Forward
0 new messages