Coherence and orphan instance rules

9 points by abhin4v


icefox

I look forward to more of this! But I also have some nitpicks:

Crucially, instances are not first-class values and they’re not named. This allows maintaining a useful property that we want to have when working with typeclasses: if we call the same method with the same type parameters in different parts of a program, they should all call the same method. This is absolutely essential...

It's very, very, very useful, but not essential. For example...

If we log/print a value with an overloaded function, the printed format for the same argument is the same, regardless of where it was printed...

...but the logging function often wants to be context dependent, so while log.write(thing) wants to write the same output for every thing, it might not want to write the same output for every log.write.

(If you’re familiar with OOP with subtyping, coherence exists in OOP languages as x.m() calling the same method m for the same type of x, everywhere in the program.)

But that doesn't happen when the method is virtual and overridden in a subclass of x, so the subtyping kinda kills coherence? And if you allow diamond inheritance it really kills it.

And I've heard hearsay that allowing non-coherent instances via language extensions is also pretty common in Haskell code and usually doesn't blow anything up.

However, interestingly, I couldn’t find any formal treatment of orphan instance rules, with proofs that the rules only allow a coherent system and examples of common use cases that they support.

Hmm, really? That IS interesting. I wonder if Rust Belt has one? I wouldn't think a formal proof of Rust's orphan rules would be too hard, I've done an informal one myself. But from the "Rebalancing Coherence" and "Re-rebalancing Coherence" RFC's I suppose they're working on the details still.