[Haskell-cafe] A question about GHC test coverage
Richard A. O'Keefe
ok at cs.otago.ac.nz
Mon Feb 20 22:05:16 UTC 2017
On 15/02/17 10:35 AM, Olaf Klinke wrote:
> You should not have to write tests for functions you did not define.
Correct me if I'm wrong, but any property of max can be derived from the
properties of <=.
Unfortunately, this isn't quite true.
Suppose we have two values x y such that x <= y && y <= x && x == y
*BUT* x and y are distinguishable some other way. For example,
suppose we are modelling rational numbers by pairs (n,d) *without*
insisting that gcd(n,d) == 0. Then we have
(n1,d1) == (n2,d2) = n1*d2 == n2*d1
compare (n1,d1) (n2,d2) = compare (n1*d2) (n2*d1)
BUT (1,2) and (2,4), while ==, are none-the-less distinguishable.
Now ask about max x y. If we have
max a b = if a > b then a else b
then max x y delivers y. But if we have
max a b = if a < b then b else a
then max x y delivers x, and these two results can be distinguished.
I know and freely admit that if I want a version of max that satisfies
stronger conditions than Ord can guarantee, it is up to me to write it.
So you could say that the bug was that I *should* have written my own
max, and didn't. But that's basically my point.
(For an example of two values that can't be distinguished by compare
or ==, consider -0.0 and +0.0. If I give those to max, what do I
get back? No, that wasn't the test case I needed.)
More information about the Haskell-Cafe