Cannot apply a method of a class to an associated data type of that class in a polymorphic context.
Summary
If D α
is an associated data type of a class C α
, then methods of type D α → X
should be parametrically polymorphic.
Steps to reproduce
class FantasticClass α where
data FantasticData α
showFantasticData ∷ FantasticData α → Text
instance FantasticClass Text where
data FantasticData Text = FantasticText Text
showFantasticData (FantasticText text) = text
data Problematic α = Problematic
{ parametricallyPolymorphicField ∷ [α]
, problematicallyPolymorphicField ∷ FantasticData α
}
type Repaired = Coyoneda Problematic
repair ∷ [α] → FantasticData α → Repaired α
repair list fantasticData = liftCoyoneda Problematic
{ parametricallyPolymorphicField = list
, problematicallyPolymorphicField = fantasticData
}
extractWithExtractor ∷ (∀ α. prefunctor α → ξ) → Coyoneda prefunctor α → ξ
extractWithExtractor projection = getConst ∘ lowerCoyoneda ∘ hoistCoyoneda (Const ∘ projection)
extractProblematicallyPolymorphicField ∷ Repaired α → Text
extractProblematicallyPolymorphicField = extractWithExtractor
\ Problematic {..} → showFantasticData problematicallyPolymorphicField
Expected behavior
I expected this example to compile. Specifically, I expected showFantasticData
to apply to problematicallyPolymorphicField
.
The method:
showFantasticData ∷ FantasticClass α ⇒ FantasticData α → Text
— Is a constrained polymorphic function, but it should be parametrically polymorphic. See: for the constraint not to resolve, there must be given a value problematicallyPolymorphicField ∷ FantasticData X
such that FantasticClass X
is not in scope. But this can never happen because FantasticData X
is an associated data type of the instance FantasticClass X
. The instances and the associated data types are in one to one correspondence, so there cannot be ambiguity either. In other words, the value of type FantasticData X
is a witness that the constraint FantasticClass X
is uniquely satisfiable.
Environment
- GHC version used: The Glorious Glasgow Haskell Compilation System, version 9.2.2