Search code examples
haskellfunctorcategory-theory

How are functors in Haskell related to functors in category theory?


For as far as I understand, a functor is a mapping between two categories, for example from objects in C to objects in D where C and D are categories.

In Haskell there is Hask in which the objects are Haskell types and the morphisms are Haskell functions. However, the Functor type class has a function fmap which maps between these types (which are thus objects and not categories themselves):

fmap :: (a -> b) -> f a -> f b

f a and f b are both objects in Hask. Does this mean every instance of Functor in Haskell is an endofunctor, and if not does Functor really represent a functor?

What am I missing here? Are types also categories in Haskell?


Solution

  • An instance of Functor specifies two things: a type constructor F of kind * -> *, that is, a mapping from objects of Hask to objects of Hask, and a function of type (a -> b) -> (F a -> F b), that is, a mapping from arrows of Hask to arrows of Hask compatible with the object-mapping F. So, yes, all instances of Functor are endofunctors. There are several generalizations available on Hackage, e.g. Control.Categorical.Functor.