Say I have the following (erroneous) code.
data A a b where
APure :: (A a b)
AApply :: A (A b c) c
test :: (A a b) -> a -> b
test (APure) a = a
test AApply a = undefined
GHC will then give me this error:
Couldn't match type `b' with `A b1 b'
`b' is a rigid type variable bound by
the type signature for test :: A a b -> a -> b
Inaccessible code in
a pattern with constructor
AApply :: forall c b. A (A b c) c,
in an equation for `test'
In the pattern: AApply
In an equation for `test': test AApply a = undefined
Isn't this error message completely wrong? The error has nothing to do with AApply.
Not completely. It's arguably a bug that you get that error message, but it's not completely off base.
Look at the whole thing together after looking at the pieces.
says we have a function
Put that together with the signature
and unify, ignoring the type error from the first equation, the type is refined to
Then look at the equation
and see how that is inaccessible under the refined type, since
would entail
if
AApply
were a valid first argument.