An unusual definition of total ordering

One of the things that will show up in the imminently forthcoming Scala 2.7.1 release candidate, is the addition of traits for representing equivalence relations, partial orderings, and total orderings. Previously, the trait

  1. Ordered
was used for representing totally ordered things:

  1. trait Ordered[A] {
  2. def compare(that: A): Int
  3. def < (that: A): Boolean = (this compare that) < 0
  4. def > (that: A): Boolean = (this compare that) > 0
  5. def <= (that: A): Boolean = (this compare that) <= 0
  6. def >= (that: A): Boolean = (this compare that) >= 0
  7. def compareTo(that: A): Int = compare(that)
  8. }

However, the

  1. Ordered
trait does not provide a representation of a total ordering. Therefore, the new trait Ordering:

  1.  
  2. trait Ordering[T] extends PartialOrdering[T] {
  3. def compare(x: T, y: T): Int
  4. override def lteq(x: T, y: T): Boolean = compare(x, y) <= 0
  5. override def gteq(x: T, y: T): Boolean = compare(x, y) >= 0
  6. override def lt(x: T, y: T): Boolean = compare(x, y) < 0
  7. override def gt(x: T, y: T): Boolean = compare(x, y) > 0
  8. override def equiv(x: T, y: T): Boolean = compare(x, y) == 0
  9. }
  10.  

The tricky part however, was writing description of the properties required of something that implements the

  1. Ordering
trait. When one normally thinks of a total ordering one thinks of a relation that is

  • anti-symmetric,
  • transitive,
  • and total.

The problem is that

  1. Ordering
is not defined in terms of a binary relation, but a binary function producing integers (
  1. compare
). If the first argument is less than the second the function returns a negative integer, if they are equal in the ordering the function returns zero, and if the second argument is less than the first the function returns a positive integer. Therefore, it is not straightforward to express these same properties. The best I could come up with was

    1. compare(x, x) == 0
    , for any
    1. x
    of type
    1. T
    .
    1. compare(x, y) == z
    and
    1. compare(y, x) == w
    then
    1. Math.signum(z) == -Math.signum(w)
    , for any
    1. x
    and
    1. y
    of type
    1. T
    and
    1. z
    and
    1. w
    of type
    1. Int
    .
  • if
    1. compare(x, y) == z
    and
    1. lteq(y, w) == v
    and
    1. Math.signum(z) >= 0
    and
    1. Math.signum(v) >= 0
    then
    1. compare(x, w) == u
    and
    1. Math.signum(z + v) == Math.signum(u)
    ,
    for any
    1. x
    ,
    1. y
    , and
    1. w
    of type
    1. T
    and
    1. z
    ,
    1. v
    , and
    1. u
    of type
    1. Int
    .

Where

  1. Math.signum
returns
  1. -1
if its input is negative,
  1. 0
if its input is
  1. 0
, and
  1. 1
if its input is positive.

The first property is clearly reflexivity. I call the third property transitivity. I am not sure what to call the second property. I do not think a notion of totality is required because it is assumed you will always get an integer back from

  1. compare
rather than it throwing an exception or going into an infinite loop.

It would probably be a good exercise to prove that given these properties on

  1. compare
hold if and only if
  1. lteq
(defined above in terms of
  1. compare
) has all the normal properties of a total ordering.

Comments