3
votes

What's wrong instance Functor Int where

instance Functor Int where
  fmap f a = f a

Expected kind ...

I can't make monad int , applicative int, functor int

2
On of the slightly more confusing things with Functor, Applicative and Monad is that the type you give it actually has to have kind * -> * - it needs to be expecting a type variable. In vanilla Haskell, kinds are simply inferred. Looking at the Functor class, you'll notice that f is always applied to a type variable. That let's the compiler infer that f has kind * -> *. That means things like Maybe, lists, IO, etc. NOT Bool, Int, (), etc. - Alec
Identity has functor/applicative/monad instances similar to this. For example, runIdentity (fmap (+ 5) (pure 2 :: Identity Int)) == 7. - Jon Purdy

2 Answers

9
votes

What is so interesting about Maybe a, [a], Either e a, IO a? The type takes an additional parameter. That is, Maybe on its own isn't a type. You have to use another type e.g. Int to actually get a type: Maybe Int.

Let's have a look at Functor's definition:

class Functor f where
    fmap :: (a -> b) -> f a -> f b
              --        ^^^    ^^^

Whatever you use for f must be able to use a type. And Int Int or Int () is not a type, because Int is already at kind *. You cannot construct another type by applying Int on something else.

Maybe on the other hand is of kind * -> *. It takes a type (e.g. Double) and returns a type, Maybe Double:

-- using pseudo kind-signatures
Maybe        :: * -> *
Double       :: *
Maybe Double ::      *

All that because our f takes an a in the signature of fmap.

So no. You cannot make any regular type (of kind *) an instance of Functor.

0
votes
class Functor f where
    fmap :: (a -> b) -> f a -> f b

functor f maps types to types - it maps a to f a and b to f b. That means f can't be a concrete type such as Int; it has to be a type that takes another type as a parameter. You can think of f as a function on types.