Skip to content

GHC doesn't notice some overlapping instances (regression)

If I have

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}

module Bug6 where

type family F a

inject :: a -> F a
inject = undefined

class C a where
  meth :: a -> ()

instance C a where
  meth _ = ()

g :: C a => a -> ()
g x = meth (inject x)

then GHC 8.10.7 will say

• Overlapping instances for C (F a) arising from a use of ‘meth’
  Matching instances:
    instance C a
      -- Defined at /Users/rae/pCloud Drive/work/temp/Bug6.hs:14:10
  There exists a (perhaps superclass) match:
    from the context: C a
      bound by the type signature for:
                 g :: forall a. C a => a -> ()
      at /Users/rae/pCloud Drive/work/temp/Bug6.hs:17:1-19
  (The choice depends on the instantiation of ‘a’
   To pick the first instance above, use IncoherentInstances
   when compiling the other instance declarations)
• In the expression: meth (inject x)
  In an equation for ‘g’: g x = meth (inject x)

I think this is quite sensible behavior: the local C a might apply, if F a ~ a for some a, overriding the global instance forall a. C a.

But GHC 9.2.1 and HEAD accept!

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information