pretzelhammer / rust-blog

Educational blog posts for Rust beginners
Apache License 2.0
7.57k stars 400 forks source link

`asymmetry` requirements in `PartialOrd` #54

Closed VitalyAnkh closed 2 months ago

VitalyAnkh commented 3 years ago

In the blog post tour of rust standard library traits, I feel the paragraph of asymmetry requirements in partial_cmp is not right:

All PartialOrd impls must ensure that comparisons are asymmetric and transitive. That means for all a, b, and c:

  • a < b implies !(a > b) (asymmetry)
  • a < b && b < c implies a < c (transitivity)

In the document for PartialOrd, there is no part about asymmetry. The requirements for partial_cmp is:

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.

Maybe the document is updated and we should catch up?

pretzelhammer commented 2 months ago

these two statements:

  1. a < b implies !(a > b)
  2. a < b if and only if b > a

seem logically equivalent to me. the first one implies the second, and the second implies the first.

the reason why i'd like to keep the use of the word "asymmetry" in the PartialOrd section is because prior to reaching that section "symmetry" is covered in the PartialEq section. Also, "asymmetry" is covered again later on in the Ord section. I like that these sections use similar language/concepts, helps them reinforce each other.

for that reason i'll keep the description as-is and close this ticket, but people are welcome to re-open this ticket if they feel strongly otherwise.