Function equality again

86 views
Skip to first unread message

Mark Hamburg

unread,
Oct 12, 2016, 12:48:12 PM10/12/16
to elm-d...@googlegroups.com
As discussed elsewhere, the runtime exception related to function equality comparisons is a lurking time bomb for code that is really only addressed on a multi-developer project by being paranoid both about functions in data structures and use of the equality operator. Both points of paranoia get in the way of writing "good" code. There are other arguments around things like serialization for avoiding using functions in some places, but there are lots of other places where it is extremely useful to put functions in data structures. (Imagine writing an effects manager if it couldn't store functions in its data structures.)

Full equality tests are, as I understand it, undecidable. That doesn't mean, however, that some limited analysis can't prove equality in most of the conditions that matter. In fact, the existing code doesn't always throw an exception. It only throws an exception if an "allocation identity" test fails. One could extend this to looking at the "code pointer" and allocation identity for the closure values and probably get many of the remaining cases that mattered. It's just a matter of how far one wants to push when trying to prove or disprove equality and I'm not arguing for a particular limit here.

Rather, I think the central issue is around what happens when we give up. At that point, there are two choices: throw a runtime exception as happens now or return false. As mentioned above and discussed at further length elsewhere, the runtime exception leads to paranoid coding. What are the issues with returning false? The big problem with returning false is that this means that some compiler optimizations might then change cases that returned false without the optimizations into cases that returned true and people become understandably uncomfortable with the possibility that compiler optimizations could change the meaning of programs. And with that, I've more or less wanted a "return false" answer but convinced myself that throwing an exception wasn't unjustified and that maybe what we needed instead was type system support to avoid the issue.

But then this morning's discussion of the virtual DOM had me realizing that runtime optimizations around when the render-diff-patch algorithm gets run create significant potential for variable program behavior. We embrace these optimizations even though they introduce non-determinism. Now, its non-determinism at the level of the physical DOM rather than the values produced in Elm itself — i.e., runtime v linguistic non-determinism — but its non-determinism in one of the most fundamental parts about how Elm is generally used so that distinction seems somewhat arbitrary.

I think Elm makes the right call on the DOM. Maybe it should log cases where the DOM logic can recognize that there might be a problem, but I think this is overall a tradeoff worth making.

But by the same argument, I think Elm programs would be better if the runtime exception for function equality comparisons was replaced with a false result even though it might actually be true. And if it's really irksome, then it could generate a log statement as well.

Mark



Kasey Speakman

unread,
Oct 12, 2016, 1:40:33 PM10/12/16
to Elm Discuss
So what's the specific case where you need to verify function reference equality?

I suspect that needing this is a symptom of some other problem.

James Wilson

unread,
Oct 12, 2016, 1:42:06 PM10/12/16
to Elm Discuss
I basically agree - something as basic as using an equality test should not have any chance to cause a runtime error in any case; in the case of functions it should be based on referential equality, which I think does the right thing in any sane case.

Zinggi

unread,
Oct 12, 2016, 3:43:58 PM10/12/16
to Elm Discuss
I also think that comparing functions should just compare them by reference, this should cover the most common cases.

Another possible solution: What about a compile time error? Is this possible?

Kasey Speakman

unread,
Oct 12, 2016, 3:59:43 PM10/12/16
to Elm Discuss
For context for future readers, this has to do with animation easing functions from another thread. I have not constructed such a library, so I won't comment.

Joey Eremondi

unread,
Oct 12, 2016, 4:04:51 PM10/12/16
to elm-d...@googlegroups.com
A compile-time error is definitely possible. Haskell has it, but it uses the full power of type classes.

To catch it at compile time, we'd either need to treat equality like comparable, which sucks for user-defined types, or do something that looks more like Haskell's type classes, with a bunch of automatic deriving. I'd advocate the latter, although I think it could be done in a way that hides most of the typeclassy stuff from the user.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Hamburg

unread,
Oct 12, 2016, 8:49:47 PM10/12/16
to elm-d...@googlegroups.com
I gather there has been trepidation about implementing an equatable built in "type class" akin to the existing "comparable", but I agree that if the plan is never to support function comparison, then equatable seems to be necessary if Elm is to live up to its "no runtime errors" goal.

As for examples of where this matters, easing functions are an example. Needing to keep tagger functions around would be an example. (For example, a web socket based request/response system would send the request and then watch the message subscription for a matching response and post it back as a message using a tagger function much as HTTP-based commands do.) There are a lot of places where hanging onto a function in the model is useful and the alternatives are convoluted. Note that all of those would likely work fine with reference equality and would be even more likely to work fine with one level of reference equality checks on the code and the closure values. Most of those cases also don't inherently need equality checks, but without compiler support to prevent them, such checks can come further up in the model logic possibly in an effort to suppress updates that didn't change anything.

Mark
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages