DerivingVia: Kinding information isn't propagated
Summary
Using DerivingVia
unexpectedly restricts the flow of kinding information. This is problematic in modules with PolyKinds
enabled, since it limits what sources of kinding information are available
Steps to reproduce
Consider the following snippet.
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
class C t
newtype Tag t a = T a
newtype Tag' t a = T' a
instance (C t, Eq t) => Eq (Tag t a)
deriving via (Tag t a) instance (C t, Eq t) => Eq (Tag' t a)
Clearly, from the context Eq t
, we infer t :: Type
. GHC disagrees:
• Expected a type, but ‘t’ has kind ‘k’
• In the first argument of ‘Eq’, namely ‘t’
In the stand-alone deriving instance for
‘(C t, Eq t) => Eq (Tag' t a)’
deriving via (Tag t a) instance (C t, Eq t) => Eq (Tag' t a)
^
OK, fine. Maybe GHC doesn't take the context into consideration. Let's try monomorphising other indeces involved. Surprise, both writing class C (t :: *)
and newtype Tag' (t :: *) a
fail to give enough kinding information to satisfy GHC.
However, writing newtype Tag (t :: *) a
unexpectedly works. This leads me to the hypothesis that DerivingVia
only uses kinding information of the via type, even with StandaloneDeriving
providing extra context.
Expected behavior
DerivingVia
should use all the kinding information available in the declaration. If this is too much, at least have it do so for standalone derivations.
Environment
- GHC version used: 8.8.1