r/functionalprogramming • u/Master-Reception9062 • Jun 02 '23
FP Functional Equality: Why 2+2 does not equal 4.0
https://jonathanwarden.com/functional-equality/
14
Upvotes
r/functionalprogramming • u/Master-Reception9062 • Jun 02 '23
2
u/Inconstant_Moo Jun 03 '23
Have you seen how Go handles constants?
https://go.dev/blog/constants
2
u/tinytinypenguin Jun 04 '23 edited Jun 04 '23
I guess I'm a bit confused how this isn't already handled by type-checking and eager evaluation. In your time example, I think a local time type and a universal time type should not be comparable, unless you casted one to the other at which point it should be the case that
f(x) == f(y). If you have some f which is polymorphic over time zones then sure it might be the case that f applied to a universal time is different from f applied to a local time of the same underlying universal value, but that's fine because the two values are not definitionally equal. I'm not familiar enough with scala to know why the string example is different, but if it's an issue of int vs. float then it's already resolved and if it's an issue of interpreting the "2+2" as a string literal "2 + 2" then eager evaluation resolves that issue.This seems like an interesting idea, but I don't really see why the problem isn't already solved by methods that exist. Am I missing something?
Edit: I guess it could be a "feature" that
(2 : int) == (1.0 + 1.0 : float), but I think it should be the programmer's responsibility to do that type casting, not the compiler.Edit2: The other thing I guess is you would want
f(.1 + .2) == f(.3), but that might fail because of float precision issues. In this case I can see why we would want a representationless numeric type.2
u/Purlox Jun 05 '23
Not quite sure what you are trying to say in those brackets. Are you saying that people can implement equality operator incorrectly? If so, then that's correct of any language really. Or are you saying that you can overload the Eq typeclass to do an equality check on two different types? Because that is impossible afaik
5
u/[deleted] Jun 03 '23
In your example
2+2 == 4.0the left side is not 4 but 4.0, so far as the equality check is concerned. Implicit conversion happens. It is implicit conversion that is your problem, not something with equality.But note also: this is a bad example, because floating point number types should not be equatable.
I agree with your 'time' example.