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

Tom Ellis tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Sun Feb 16 15:21:04 UTC 2020

On Sun, Feb 16, 2020 at 02:44:21PM +0000, Juan Casanova wrote:
> until you provide me with an example of code that, if the compiler
> worked the way I want it to, would have an ambiguous instance, or an
> ambiguous implementation of some function at all, I'll still believe
> that the compiler is flagging up the overlapping instances error too
> soon without really checking whether they are really valid
> instances.

Didn't Arjen already provide such an example?  Should `g "Hello"`
return a `Left` or a `Right`?

  g :: String -> Either String String
  g x = foo x

I think the difficulty may be due to the error being flagged in a
misleading way.  There's nothing wrong with your intended usage of the
type classes, individually.  The problem is that the instances both
exist in the same codebase.  The error message about overlap *appears*
at the use sites but I believe that this is an implementation detail.

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.


More information about the Haskell-Cafe mailing list