Why is this code correct
instance Functor IO where -- note that IO isn't parametrized, and it's correct
fmap f action = do
result <- action
return (f result)
but the following code has a compiler error?
class Print a where
print :: a -> String
data A t = A t
instance Print A where -- error: expecting one more argument to `A'
print a = "abc"
This is because the kinds don’t match. Regular types have kind
*, while type constructors such asAorIOhave kind* -> *, indicating that they need a type parameter in order to return a type.In the definition of the
Printclass, the compiler infers that sinceais used as a plain type, it must have kind*. However,Functorworks on type constructors of kind* -> *:Here,
fis not used as a plain type, but as a type constructor, so it’s inferred kind is* -> *. You can verify this with the:kindcommand in GHCi: