Unnecessary constraints when using overlapping instances
The following code compiles just fine:
class Foo a where foo :: a -> Int
instance Foo a where foo _ = 5
f :: a -> Int
f = foo
But if we modify the code to use overlapping instances like so:
class Foo a where foo :: a -> Int
instance {-# OVERLAPS #-} Foo Int where foo = id
instance {-# OVERLAPPABLE #-} Foo a where foo _ = 5
f:: a -> Int
f = foo
We get the following error message:
Overlapping instances for Foo a arising from a use of ‘foo’
Matching instances:
instance [overlappable] Foo a
-- Defined at ...
instance [overlap ok] Foo Int
-- Defined at ...
(The choice depends on the instantiation of ‘a’
To pick the first instance above, use IncoherentInstances
when compiling the other instance declarations)
Including the constraint in the type signature makes the problem go away, but the constraint shouldn't be needed. In most use cases this wouldn't be a big deal, but it's making my type signatures much messier looking than they actually are.
Adding IncoherentInstances as the error message suggests gives the instance I don't want. Weirdly, I get different instances depending on whether the constraint is in the type signature or not.