Search code examples
haskellderivingderivingvia

Deriving Via: Cannot derive well-kinded instance


I am encountering this error when I try to derive an instance.

Cannot derive well-kinded instance of form ‘HFunctor (ControlFlowCMD ...)’
    Class ‘HFunctor’ expects an argument of kind ‘(* -> *, *)
                                                  -> * -> *’
• In the newtype declaration for ‘ControlFlowCMD’

I am trying to do this:

newtype ControlFlowCMD fs a = ControlFlowCMD (ControlCMD fs a)
  deriving HFunctor via (ControlCMD fs a)

You can see the data type and instance I am basing my type on and trying to derive here, on line 278. I am not that used to using deriving via - could anyone explain what this error means and how I would fix it?


Solution

  • The problem was that (* -> *, *) or, equivalently, (Type -> Type, Type) is a kind-level tuple, and one must enable the DataKinds and PolyKinds extensions in order to handle it. (I'm not completely sure why PolyKinds is needed though; maybe it's to allow more general kind inference.)

    With datatypes having complex kinds, it's often a good idea to enable StandaloneKindSignatures and give the kind signature explicitly:

    import Data.Kind
    type ControlFlowCMD :: (Type -> Type, Type) -> Type -> Type
    newtype ControlFlowCMD fs a = ...