Lack of type information in GHC error messages when the liberage coverage condition is unsatisfied
Hello! The problem is not a bug related to proper code execution, but it affects the development in such way, we can treat it as a bug.
Lets think about such simple code (it can be further simplified of course):
{{{#!haskell
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UndecidableInstances #-}
data UnsafeBase base err val = Value val
| Error err
| Other (base val)
deriving (Show, Eq)
class MagicMerge m1 m2 | m1 -> m2 where
magicMerge :: m1 a -> m2 a
instance MagicMerge (UnsafeBase (UnsafeBase base err) err) (UnsafeBase base err) where
magicMerge ma = case ma of
Value a -> Value a
Error e -> Error e
Other o -> o
instance MagicMerge (UnsafeBase (UnsafeBase base err1) err2) (UnsafeBase dstBase err1) where
magicMerge (ma | UnsafeBase (UnsafeBase base err1) err2 a) = case ma of |
---|---|
Value a -\> Value a | |
Error e -\> Other $ magicMerge (Error e | UnsafeBase base err2 a) |
Other o -\> case o of | |
Value a -\> Other $ magicMerge (Value a | UnsafeBase base err2 a) |
Error e -\> Error e | |
Other o' -\> Other $ magicMerge (Other o' | UnsafeBase base err2 a) |
main = print "help me world!"
}}}
When trying to compile it using GHC-7.8, we get following error message:
Illegal instance declaration for ‘MagicMerge (UnsafeBase (UnsafeBase base err1) err2) (UnsafeBase dstBase err1)’ The liberal coverage condition fails in class ‘MagicMerge’ for functional dependency: ‘m1 -> m2’ Reason: lhs type ‘UnsafeBase (UnsafeBase base err1) err2’ does not determine rhs type ‘UnsafeBase dstBase err1’ In the instance declaration for ‘MagicMerge (UnsafeBase (UnsafeBase base err1) err2) (UnsafeBase dstBase err1)’
Which is of course right, but is completely unhelpfull! When we run the same program against GHC-7.6, we get another message:
No instance for (MagicMerge (UnsafeBase base err2) dstBase) arising from a use of
magicMerge' Possible fix: add an instance declaration for (MagicMerge (UnsafeBase base err2) dstBase) In the second argument of($)', namely
magicMerge (Error e :: UnsafeBase base err2 a)' In the expression: Other $ magicMerge (Error e :: UnsafeBase base err2 a) In a case alternative: Error e -> Other $ magicMerge (Error e :: UnsafeBase base err2 a) `
And it contains a very helpfull information about inferred types. The information is very usefull, especialy when developing more complex instances.
It is VERY IMPORTANT to notice, that when we use the inferred context, the code compiles in both, GHC-7.8 and GHC-7.6! Right now I have to use both versions of GHC side by side, to be able to develop my instances faster.
So If we add the context inferred by GHC-7.6:
{{{#!haskell
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UndecidableInstances #-}
data UnsafeBase base err val = Value val
| Error err
| Other (base val)
deriving (Show, Eq)
class MagicMerge m1 m2 | m1 -> m2 where
magicMerge :: m1 a -> m2 a
instance MagicMerge (UnsafeBase (UnsafeBase base err) err) (UnsafeBase base err) where
magicMerge ma = case ma of
Value a -> Value a
Error e -> Error e
Other o -> o
instance (MagicMerge (UnsafeBase base err2) dstBase) => MagicMerge (UnsafeBase (UnsafeBase base err1) err2) (UnsafeBase dstBase err1) where
magicMerge (ma | UnsafeBase (UnsafeBase base err1) err2 a) = case ma of |
---|---|
Value a -\> Value a | |
Error e -\> Other $ magicMerge (Error e | UnsafeBase base err2 a) |
Other o -\> case o of | |
Value a -\> Other $ magicMerge (Value a | UnsafeBase base err2 a) |
Error e -\> Error e | |
Other o' -\> Other $ magicMerge (Other o' | UnsafeBase base err2 a) |
main = print "help me world!"
}}}
The code compiles and works ok in both GHC-7.6 and GHC-7.8. In such cases, GHC-7.8 should inform about lacking premises.