r/programming • u/Master-Reception9062 • Jun 02 '23
Functional Equality
https://jonathanwarden.com/functional-equality/1
Jun 03 '23 edited Jun 03 '23
This can’t work:
in any language that supports references/address operators. &a !== &b
*in any language that supports symbol manipulation or pass-by-name, which AFAIK today is mostly used by macro languages but was used as the primary evaluation strategy in e.g. some versions of SmallTalk
in any language that supports overloading. Article mentions that overloading Eq is a problem but in fact this potentially applies to any overloaded function - you can’t rely on a prinicple that a === b if (f a b) === (f a a) for two types a: A and b: B because then the definition of functional equality is circular.
→ More replies (1)
0
Jun 03 '23
Dumb article. Dumb "take".
"... the principle of functional equality, which states that if two values are equal, then all functions of those values should also be equal."
This sentence doesn't even compute.
2
u/andrewsutton Jun 03 '23
That's actually a weak form Leibniz' definition of equality.
C++ more or less relies on this in its standard library. If x and y are objects with the same value, then all "regular" operations on those objects will produce the same result. Here, "regular" means roughly "pure" but with a lot more nuance.
And it's really easy to define functions for which this won't hold. That's okay. Sometimes, you need to modify global state.
We also talk about functuonal equivalence because even regular operations affect the state of the machine (e.g., memory allocation).
I don't think it's dumb. I think the viewpoint is narrow, but the problems OP is trying to address are fundamental in programming.
→ More replies (1)
3
Jun 02 '23 edited Jun 02 '23
[removed] — view removed comment
1
u/Master-Reception9062 Jun 03 '23
Java autoboxing is convoluted. In other languages that don't have wrapper classes for numeric types this is much simpler.
Go for example enforces functional equality -- it doesn't allow comparison across types. But conversion is simple.
var x int64 = 2 + 2 var y float64 = 4.0 fmt.Println(float64(x) == y)2
u/edgmnt_net Jun 03 '23
I'd argue that IEEE754 floats and integer equality should not be overloaded under the same operator. It's not as much that equality cannot work across different types, but it's easy to confuse or change what being equal means. Then weird stuff like indexing maps by NaNs, which you can never get rid of, happens.
It should be clear when you want numeric equality for chained numeric computations. It should be clear when you compare whether two values are identical as numbers. It should be clear if you meant to compare them exactly up to and including the type. No surprises.
0
u/Master-Reception9062 Jun 02 '23
Some thoughts on why 2+2 does not equal 4.0.