Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong instance Functor Int where

Tags:

haskell

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

like image 745
김강태 Avatar asked Feb 27 '26 22:02

김강태


1 Answers

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.

like image 159
Zeta Avatar answered Mar 02 '26 15:03

Zeta