[Haskell-cafe] (Feeling that) GHCi is trying to protect me from myself with overlapping instances and existential types

Olaf Klinke olf at aatal-apotheke.de
Sun Feb 16 20:27:47 UTC 2020

> the ones at Stack  
> Overflow basically told me that it does to "protect me from being too  
> dumb and not understanding my own code", the way I interpret it. I  
> felt that was totally against the principles of Haskell

On the contrary! Protecing me against writing dumb code is one of the things where Haskell shines, and I am infinitely grateful for that.  

> Having GHCi protect me from that highly hypothetical situation while  
> preventing me from compiling theoretically correct programs seems  
> strange at best.
But those theoretically correct programs may suddenly cease to be correct in very mysterious and little hypothetical ways, as explained below. 

I agree with Arien. Making the semantics of a function dependent on how many type class instances are in scope is totally against the principles of Haskell, IMHO. Even worse the function signature of createBar does not mention Class1 so at first glance there is no hint that type classes are at work here. Type class instances are hard to contain, in the sense that there is currently no mechanism of selectively importing or not importing them from some module. Change in semantics through orphan instances would be a nightmare to debug:

Suppose you have written a package that defines Class1 and only the first incoherent instance. Alice comes along and uses your package to define a function

useFoo :: Monoid a => a -> Either a a
useFoo = foo

and henceforth Alice relies on the semantics useFoo a = Right (a <> a), because that's what your package intends. Alice's code also depends on a package written by Bob, but she only uses some part of that package which is totally unrelated to useFoo and Class1. In an update of his package, Bob defines your second Class1 instance as orphan instance. Alice never explicitly imported any Class1 stuff from Bob's module, but the new instance will leak anyways. Suddenly the semantics of Alice's function changes, she gets useFoo a = Left a without any changes in her own code! If Bob did not adhere to the versioning policy then Alice has little chance of knowing that Bob's package is the cause. The only hint for Alice is the INCOHERENT pragma that signals danger, and you propose to take that away.  


More information about the Haskell-Cafe mailing list