Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between functors and endofunctors

Can someone explain in simple terms the difference between the two? I'm not fully understanding the part where monads are endofunctors versus being just functors.

like image 799
Jonathan Dunlap Avatar asked Apr 26 '12 23:04

Jonathan Dunlap


People also ask

What are functors and monads?

A functor takes a pure function (and a functorial value) whereas a monad takes a Kleisli arrow, i.e. a function that returns a monad (and a monadic value). Hence you can chain two monads and the second monad can depend on the result of the previous one.

Why are functors important?

Functors are also important because they are a building block for applicatives and monads, which are coming in future posts.


1 Answers

A functor may go from one category to a different one, an endofunctor is a functor for which start and target category are the same.

Same as with endomorphisms versus morphisms.

Now, why must monads be endofunctors?

There is the famous quote that "Monads are just monoids in the category of endofunctors". Fortunately, somebody else has already explained that rather well in this answer.

The key point why a monad has to be an endofunctor, is that join, as it is called in Haskell, or µ, as it is usually called in category theory, is part of the definition¹ of a monad. Now

Prelude Control.Monad> :t join join :: Monad m => m (m a) -> m a 

so the result of applying the functor m to an object (in Hask, the category of Haskell types as objects and functions as morphisms, a type) must be an object that m can again be applied to. That means it must belong to the category that is the domain of the functor m.

A functor can only be composed with itself if its domain and codomain are the same [strictly, if its codomain is a subcategory of its domain], in other words, if it is an endofunctor. Since composability with itself is part of the definition of a monad, monads are a fortiori endofunctors.

¹ One definition, one can alternatively define a monad using (>>=) or bind and have join as a derived property.

like image 143
Daniel Fischer Avatar answered Sep 29 '22 16:09

Daniel Fischer