GHC doesn't infer all constrains
Hello! Let's consider following signatures:
class (IndexOf a cont ~ idx, ElementByIdx idx cont ~ a, Measurable cont) => Container cont idx a
class Container cont idx a => Appendable cont idx a
class HasContainer a cont | a -> cont
class PtrFrom p i | p -> i
class Monad m => RefBuilder3 a m ref | a m -> ref where
mkRef3 :: a -> m (ref a)
Now when I define following instance:
instance (PtrFrom idx i, Appendable cont idx a, HasContainer g cont, Monad m)
=> RefBuilder3 a (GraphBuilderT g m) (Ptr i) where
mkRef3 = undefined
I get an error:
Illegal instance declaration for
‘RefBuilder3 a (GraphBuilderT g m) (Ptr i)’
The liberal coverage condition fails in class ‘RefBuilder3’
for functional dependency: ‘a m -> ref’
Reason: lhs types ‘a’, ‘GraphBuilderT g m’
do not jointly determine rhs type ‘Ptr i’
In the instance declaration for
‘RefBuilder3 a (GraphBuilderT g m) (Ptr i)’
But when I add the constraint to the instance head:
instance (PtrFrom idx i, Appendable cont idx a, HasContainer g cont, Monad m, IndexOf a cont ~ idx)
=> RefBuilder3 a (GraphBuilderT g m) (Ptr i) where
mkRef3 a = fmap ptrFrom . withGraph . append $ a
It compiles fine. What's interesting, the constraint should be inferred by GHC, because we've got following fundeps here:
g -> cont -- from HasContainer
cont a -> idx -- from Appendable -> Container
idx -> i -- from PtrFrom
Is this a GHC bug or am I missing something? If thats a bug, I will try to clean the code and make a minimal example.