Though you were using those fancy categorical terms in your question and should be completely satisfied with the existing answers, here is an attempt for a rather trivial explanation:
Suppose there would be a function return
(or pure
or unit
or ...
) in the Functor type class.
Now try to define some common instances of Functor: []
(Lists), Maybe
, ((,) a)
(Tuples with a left component)
Easy enough, eh?
Here are the ordinary Functor instances:
instance Functor [] where
fmap f (x : xs) = f x : fmap xs
fmap _ [] = []
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap _ Nothing = Nothing
instance Functor ((,) a) where
fmap f (x, y) = (x, f y)
What about return
for Functor now?
Lists:
instance Functor [] where
return x = [x]
Alright. What about Maybe?
instance Functor Maybe where
return x = Just x
Okay. Now Tuples:
instance Functor ((,) a) where
return x = (??? , x)
You see, it is unknown which value should be filled into the left component of that tuple. The instance declaration says it has a type a
but we do not know a value from that type. Maybe the type a is the Unit
type with only one value. But if its Bool
, should we take True
or False
? If it is Either Int Bool
should we take Left 0
or Right False
or Left 1
?
So you see, if you had a return
on Functors, you could not define a lot of valid functor instances in general (You would need to impose a constraint of something like a FunctorEmpty type class).
If you look at the documentation for Functor
and Monad
you will see that there are indeed instances for Functor ((,) a)
but not for Monad ((,) a)
. This is because you just can't define return
for that thing.
Functor
and forMonad
classes"? Since monads are functors, it can't be opposite. — As for "why category theory argument is applicable to Haskell type theory": type theory has nothing to do with this whatsoever. It's just the Haskell standard libraries, they implement type classes which are modelled after category theory concepts. – Abcreturn
is representing the natural transformation η: 1 → T, which is defined for every monad but not for general functors. So... what's still not clear to you? – Abcinstance Functor f where ...
,f
is something that takes a type and gives you another type (so it is a mapping between types). For exampleMaybe
is a type constructor that takes a type and gives you a type. If you give itInt
, then it gives youMaybe Int
.fmap
mapsf a
values tof b
values given a functiona -> b
. – Essentialityfmap
takes a function froma -> b
and maps it to a function fromf a -> f b
. As I understand it, this is analogous to the arrow mapping done by category theoretic functors. It takes an arrow in the category Hask, and it maps it to an arrow in the category Hask where all types have anf
applied to them. So in theMaybe
example, it maps arrows from Hask to the category of Haskell types where all types are applied toMaybe
:fmap :: (a -> b) -> (Maybe a -> Maybe b)
. – Essentiality