Ambiguous tyvar when deriving a class with default signature involving type synonym to rank-N type
Summary
Classes with default signatures involving type synonym to rank-N type cannot be derived using non-standalone anyclass deriving.
Steps to reproduce
{-# LANGUAGE RankNTypes, DerivingStrategies, DeriveAnyClass, DefaultSignatures #-}
module Bug where
import Data.Proxy
type Method a = forall b . Eq b => Proxy (a, b)
class Class a where
method :: Method a
default method :: Method a
method = Proxy
data Data
deriving anyclass Class
fails with
src/Bug.hs:17:21: error:
• Could not deduce (Eq b0)
arising from the 'deriving' clause of a data type declaration
from the context: Eq b
bound by the deriving clause for ‘Class Data’
at src/Bug.hs:17:21-25
The type variable ‘b0’ is ambiguous
These potential instances exist:
instance forall k (s :: k). Eq (Proxy s) -- Defined in ‘Data.Proxy’
instance Eq Ordering -- Defined in ‘GHC.Classes’
instance Eq Integer -- Defined in ‘GHC.Num.Integer’
...plus 22 others
...plus four instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Class Data)
|
17 | deriving anyclass Class
Using standalone deriving or instance Class Data where {}
, removing default signature for method
or expanding Method
synonym by hand solves the problem.
But the most interesting thing is that compiling the original program with -fdefer-type-errors
leads to a warning about the type error, but then -ddump-ds
output does not contain any typeError
invocation, and the relevant part of core looks as expected:
$dmmethod :: forall a. Class a => Method a
$dmmethod = \ (@a_ais) _ (@b_a1rX) _ -> Proxy @(*) @(a_ais, b_a1rX)
Rec {
$fClassData :: Class Data
$fClassData = $cmethod_a1s4 `cast` <Co:3>
$cmethod_a1s4 :: Method Data
$cmethod_a1s4 = \ (@b_a1s7) ($dEq_a1s8 :: Eq b_a1s7) -> $dmmethod @Data $fClassData @b_a1s7 $dEq_a1s8
end Rec }
Expected behavior
The example above compiles without an error, as it does when using standalone deriving instead.
Environment
- GHC version used: 8.10.7, 9.0.1, 9.2.1
- Operating System: Linux (NixOS)
- System Architecture: x86-64