
On Sun, Feb 16, 2020 at 03:39:24PM +0000, Juan Casanova wrote:
The instances you have written can cause problems in *other* parts of the codebase, perhaps completely unrelated parts that have merely transitively imported your module through dependencies (someone may define `g` above, for example). Thefore the instances should not be both allowed to exist in the same codebase. This particular piece of information is flagged at the use sites but perhaps should be flagged at the instance definition sites.
I simply do not agree with this, and I don't think (?) that GHCi implementors and designers agree with you either, as exemplified by the fact that indeed overlapping instances appear when using instances and not when defining them. While it doesn't clearly agree with what I said, the general phrasing and approach followed here: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts... makes me think indeed the approach is that potentially overlapping instances are allowed so long as each of their uses are unambiguous.
Potentially overlapping typeclasses are most certainly *not* allowed by default, which is why need pragmas to enable them. Enabling them can be dangerous and violate properties that one would otherwise assume to hold.
Rest assured, if it is like this by design as you claim it may be, I would have a big amount of complaints about that as well. Things that are absolutely sensible to do that would be made very noticeably harder to do by this design decision. But it would certainly address this particular issue.
The design is called "global typeclass coherence" and it is very important for certain desirable properties to hold. There are other ways of approaching things, but this is the one chosen in Haskell. I'm not very knowledgeable about this area but or futher information I suggest the following resources, in approximate order of importance: * http://blog.ezyang.com/2014/07/type-classes-confluence-coherence-global-uniq... * https://www.reddit.com/r/haskell/comments/2w4ctt/boston_haskell_edward_kmett... * https://www.reddit.com/r/haskell/comments/2agy14/type_classes_confluence_coh... * https://www.reddit.com/r/haskell/comments/765ogm/multiple_type_class_instanc... I realise that your interest may be more on the theoretical side, but to add a small codicil of practical advice I highly recommend staying away from any clever uses of typeclasses. Passing around first class values is much more composable and leads to far fewer headaches. Tom