IIRC I ended up at the rule of a == b ⇒ a.clone() == b.clone(), which works for f32 and anything else I can think of, though that's not sufficient in general and I think it can be at most a SHOULD since there might be types that don't meet it today.
Irreflexivity and transitivity together imply asymmetry. Also, asymmetry implies irreflexivity. In other words, a transitive relation is asymmetric if and only if it is irreflexive. So the definition is the same if it omits either irreflexivity or asymmetry (but not both).
So we need transitivity and either irreflexivity or asymmetry.
Let's try to show that PartialOrd is a partial order:
Checking the docs on PartialOrd, I don't see either irreflexivity or asymmetry. But there is a demand that a < b if and only if b > a:
The comparison must satisfy, for all a, b and c :
transitivity: a < b and b < c implies a < c. The same must hold for both == and >.
duality: a < b if and only if b > a.
Now if we set b to a (which we can, because the "duality" must hold for all a and b), we get:
a < a if and only if a > a. Since PartialOrd::partial_cmp cannot return both Some(Less) and Some(Greater), we can deduce that a < a is always false. Hence irreflexivity is fulfilled (and then also asymmetry), and we have a partial order. q.e.d.
Eh.. alright, technically < is a strict partial order here. But in the context of strict partial orders, the corresponding non-strict partial order is gained by taking true equality into consideration, i.e. you define a ≤ b by a < b || a = b. In other words, the notion of “equality/equivalence” in the context of strict partial orders is mathematical equality, which is – in particular – an equivalence relation. (And also fulfills concruence axioms like: a = b and b < c and c = d implies a < d).
In Rust, there is not implicit, always present, already-defined true equality with these properties that we can use, so discussing the question of whether we have a strict partial order stops making all that much sense once we also want to have a corresponding ≤ relation. (Which we do want in the case of PartialOrd; it’s the method le after all.)
In other words: You cannot define ≤ from < alone, when you have a strict partial order <. Instead, you’d need to talk about the strict partial order <and also the way it relates to the partial equivalence relation ==. (You can define all the remaining operations in terms of these two.)
In order to get all the requirements of PartialOrd, you need to add the following axioms in order to specify how < and == interact:
at most one of a < b, a == b or b < a is true (this axiom is present - implicitly - in the PartialOrd docs, also this axiom immediately implies irreflexivity as you’ve noticed)
a == b and b < c and c == d implies a < d (i.e. the congruence axiom that is missing from the PartialOrd docs)
Now, if you want to, you can verify that having a partial equivalence relation == and a strict partial order < with these two additional axioms is cryptomorphic to having a single, arbitrary, reflexive relation ≤,
using the definitions
(≤ in terms of < and ==):
a ≤ b iff a < b || a == b
(== and < in terms of ≤):
a == b iff a ≤ b || b ≤ a
a < b iff a ≤ b && !(a == b)
I.e. verify that using the axioms for < and == described above1, and the definition for ≤, you can derive that ≤ is reflexive, and the definitions of < and == in terms of ≤ are true; and vice-versa, using a reflexive relation ≤ and the definitions of < and == in terms of ≤, verify that < and == fulfill the axioms described above1, and verify that the definition of ≤ in terms of < and == is correct.
1Those axioms were: < is a strict partial order, == a partial equivalence relation, and the two additional axioms
Which is funny, of course, given that AFAIK the whole reason PartialOrd exists is the IEEE 754 floating-point semantics. Is there even precedence for objects that behave like IEEE floats in mathematics? Are they weird enough to not match any more-or-less commonly used terminology?
NULL in SQL has similar semantics but seems more sane in that it embraces true three-valued logic: its "PartialEq" actually returns "Option<bool>" in Rust terms. So NULL == NULL is neither true nor false, it is itself NULL, as in unknown, just like NULL < NULL.
Yes, I did find "partial preorder" which matches, as mentioned somewhere above in this thread.. ah here it is:
Admitted, one cannot really necessarily call this notion
if it isn't even (really, AFAICT) possible to find the definition in the English Wikipedia.
But alas, "reflexive relation" isn't too out-of-the-world either, and that's apparently (according to my interpretation of the docs) what PartialOrd is all about. Of course the alternative interpretation of "if you leave out this NaN element, it's a total order, but NaN is incomparable with everything and not even equal to itself" is a totally mathematical apt description.
Might be more sane; but note that (as far as my mathematical experience goes) three-valued logic is not generally the most common thing used in "mathematics". Your average mathematician would probably be happier with the "transitive relation" or even the "partial preorder".
But note that number.minimum(other) was also just added (in nightly), for the other "minimum" defined in IEEE 754.
I'm not really a fan, because you end up with the whole "well, where do the NANs go?" problem when defining said function. For example, do they go at the beginning or the end? And does the NAN payload matter?
I think having people do .sort_by(f32::total_cmp) would be fine (once we eventually stabilize that, or something like it).
And note that it puts NANs at both ends, depending on their sign bits.
So, what does it mean to sort something by PartialOrd? It seems like a reasonable expectation that for any two indices i < j, it holds that v[i].partial_cmp(v[j]) != Some(Greater). For the general (worst) case, consider sorting a haystack with two needles and a million pieces of hay in it:
NeedleA < NeedleB
PieceOfHay(k) not comparable to anything but itself
Everything equal to itself
The needles have to end up correctly ordered, but the only way to get any useful information (including recognizing if it's a needle or not) is to compare the two needles to each other. Therefore we have to make O(n²) comparisons!
What might happen in reality is that someone tries to sort a large array that accidentally ended up containing only NaNs. If the algorithm is to be correct and generic for T: PartialOrd, it may effectively hang the program at that point. Of course for actually sorting floats, f32::total_cmp (or anything that can e.g. consider all NaNs equal) will have no issues.
The point is that in a T: PartialOrd generic function the only thing you can do with a T is to try to compare it to another T. I suppose it could try something like examining the bit patterns, and if they match you know that the two things are interchangeable. But even that won't help if they never do match. It could equivalently be the integers 0..n with two randomly chosen as the needles. The algorithm can't know which ones they are until it finds both at the same time and compares them.
Floats are saved by the property that the incomparable elements (NaNs) are not comparable to anything, but that's not required by PartialOrd.
the first answer cites a paper that contains algorithms that are more efficient depending on a parameter "w" which is (AFAIR) something like the largest size of a set of mutually uncomparable items.
On the other hand, if very few items are comparable at all (and you somehow know beforehand which are) then topological sort might be efficient; otherwise a O(n²) time might be correct for the best possible approach, in a fully general setting.