3

Herb Sutter implemented only a subset of chained comparisons in his cppfront, stating in his keynote (https://youtu.be/fJvPBHErF2U?si=RqcR661yBzcQ8r5_&t=850) that a >= b < c is "mathematically unsound". He shows three examples of chained comparisons. One would be supported but two of them would not would not be supported in c++2 even though they are supported in Python. He does not go on to expand upon the reasons other than to point out the how this decision is addressed in Python and this is related to his point about simplicity, safety and efficiency.

simplicity, safety and efficiency -- chained comparisons examples screen shot fragment

Mathematical textbooks are full of expressions like a > b < c to express a minimum or a >= b < c for a one-side minimum. What is mathematically unsound about these chained comparisons?

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • In the video he says that the comparisons are not transitive, here e.g. `a >= b < c` does not imply anything about the relationship of `a` and `c`. – mkrieger1 Aug 26 '23 at 19:06
  • 1
    @mkrieger1 right, I think "unsound" is the wrong word, it implies something much stronger. I'll note that the [Python docs](https://docs.python.org/3/reference/expressions.html#comparisons) just call it 'not pretty' : "Note that `a op1 b op2 c` doesn’t imply any kind of comparison between `a` and `c`, so that, e.g., `x < y > z` is perfectly legal (though perhaps not pretty)." – juanpa.arrivillaga Aug 26 '23 at 19:14
  • 9
    "Mathematical textbooks are full of expressions like `a > b < c`". I never seen something like this, do you have an example? – Marek R Aug 26 '23 at 19:20
  • 1
    @Eljay You are using C++ example while he is talking about a new language (Cpp2) and referring to Python semantics. `a, b, c = 5, 6, 3; print(a >= b < c)` will print `False`. – Paul Jurczak Aug 26 '23 at 19:33
  • @MarekR You are right. My memory served me wrong. I've spent a few minutes scanning *Introduction to Real Analysis, 4th Edition* and didn't find a single case to support my claim. I found a lot of transitive chained comparisons, though. – Paul Jurczak Aug 26 '23 at 19:47
  • This appears to be a math question. Consider asking on Math.SE instead. – TylerH Aug 28 '23 at 15:10

2 Answers2

1
d != e != f

This contains only "not equal operators" between operands. So it might be counterintuitive and unexpected that d == f can satisfy this condition. This happens because the chain is not transitive.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    It might be unexpected for a careless reader, but most people would not infer any relation between `d` and `f` from it. – Paul Jurczak Aug 26 '23 at 19:53
  • @PaulJurczak if we want to make the language more accessible for new users we can't just dismiss them as "careless readers". – bolov Aug 26 '23 at 20:25
  • "careless readers" != "new users". I was referring to readers making unsound inferences. I don't know the statistics of new users doing so in this case. If it is prevalent enough, then banning it may be justified. – Paul Jurczak Aug 26 '23 at 21:35
  • most users that make "unsound inferences" are inexperienced programmers – bolov Aug 26 '23 at 23:35
  • The word is "transitive", not "transient". – Peter Aug 27 '23 at 06:57
0

Typically we write a < b < c as a shorthand for a < b, b < c, and a < c. Assuming this is the case and <, >, <=, >= are transitive, which is usually the case, writing a < b > c would imply that a < b, b < c, and a > c, which is always false. Writing a <= b > c could be true if and only if a = b and b > c, but this is still not the interpretation that Python has.

In Python, writing a < b > c means a < b and b > c, no comparisons are made between a and c, meaning the statement could be true. Similarly, the interpretation of a <= b > c is completely different as there are many cases where a != b and the statement is still true.

print(3 < 5 > 4) # Prints `True`

While the a op c comparison in the a op b op c example is not something we can actually infer, and the correct mathematical interpretation is, in fact, a op b and b op c, this is an expectation that many people have when they see a chained comparison (as demonstrated by how unpopular the a < b > c notation is).

This notation is not technically mathematically unsound, but it is uncommon and uncomfortable. When we are habituated in thinking by the reasoning presented in the first paragraph, this notation makes us stop and think just a little bit harder even if it is by a split second, creating friction and making it harder to understand the actual logic of the algorithm. We must remember code is read quite a lot. The subversion of expectations when seeing a a < b > c comparison makes it really hard to track the code logic and could be more clearly represented with a < b and c < b

João Areias
  • 1,192
  • 11
  • 41
  • 2
    *Typically we write a < b < c as a shorthand for a < b, b < c, and a < c.* I strongly disagree. We write `a op1 b op2 c` to express `a op1 b` and `b op2 c`. We say nothing about the relationship between `a` and `c`. – Paul Jurczak Aug 26 '23 at 19:57
  • Frankly, I've never seen an example where this was not the assumption, and if your question is particularly about the keynote, he explicitly mentions at time `13:26` *and that means the ones that are transitively true* – João Areias Aug 26 '23 at 20:02
  • 5
    @PaulJurczak the very fact that you disagree with João here sort of proves the point, the statement can be interpreted in more than one obvious way, whichever way the compiler chooses to interpret it a subset of users will be surprised by the behaviour, its therefore simpler to just not allow it, as mentioned in Herb's presentation, even the python authors wished they'd not allowed it – Alan Birtles Aug 26 '23 at 20:37
  • 3
    *writing a < b > c would imply that a < b, b < c*: this doesn't make any sense to me. It implies `a < b, b > c`. – Paul Jurczak Aug 26 '23 at 21:40
  • @PaulJurczak and that's the point, it is confusing. One may stop, think, and realize that it doesn't make any sense and that you're actually expressing `a < b and b > c` but this subverts our expectations of chained comparisons, creates friction, and impairs understanding. – João Areias Aug 26 '23 at 23:16
  • @PaulJurczak We must remember that code is read quite a lot and the ideas must be clearly expressed. Writing `a < b and c < b` is much more clear. Saying `a < b > c` is not incorrect by rigorous mathematics, but it would be nonsensical if we took the definition that I described above, which is a common interpretation as seen by the number of people commenting and by the interpretation in the keynote you on your question. – João Areias Aug 26 '23 at 23:22
  • @PaulJurczak I've edited the response to reflect my last 2 comments – João Areias Aug 26 '23 at 23:45