Cannot derive well-kinded instance [GHC-62016]
This error means that the mentioned type class in the deriving
clause does not accept the data type you are defining as argument.
More concretely, when GHC finds a declaration of the form
data T ... = C ... | D ...
deriving Klass
there’s a small procedure which tries to drop type arguments
from T
until it matches the kind expected by Klass
.
If this procedure fails, the error message appears.
Examples
Deriving Functor for a type with an argument of non-Type kind
Error message
Main.hs:5:12: error: [GHC-62016]
• Cannot derive well-kinded instance of form ‘Functor (AppliedToInt ...)’
Class ‘Functor’ expects an argument of kind ‘* -> *’
• In the data declaration for ‘AppliedToInt’
Explanation
The Functor
type class applies to type constructors of
exactly the kind Type -> Type
. However, the kind of AppliedToInt
is (Type -> Type) -> Type
instead. There is thus a mismatch
between the expected and given kinds.
Main.hs
{-# language DeriveFunctor #-}
module Main where
data AppliedToInt f = AppliedToInt (f Int)
deriving Functor
Deriving Functor for a ground type
Error message
Main.hs:5:12: error: [GHC-62016]
• Cannot derive well-kinded instance of form ‘Functor (Pet ...)’
Class ‘Functor’ expects an argument of kind ‘* -> *’
• In the data declaration for ‘Pet’
Explanation
The Functor
type class applies to type constructors; that is,
data types which take one other type as a parameter. The Pet
data type
defined in the code does not take any type parameters.
As a result, GHC cannot write the Functor
instance for you.
Main.hs
{-# language DeriveFunctor #-}
module Main where
data Pet = Cat | Dog
deriving Functor