Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What, no one is giving the standard answer of "a monad is a monoid in the category of endofunctors?"

A category is something like a set, but combining the objects with allowed operations. (How very OOP!) The available functions are first-class citizens (called arrows). The objects are the domains and ranges of the arrows, and their internal structures are not exposed. Arrow composition is associative when it is defined. (You can only compose an arrow if the range of one is the domain of the next.)

A functor is just a (natural) function on categories mapping both objects and arrows in a compatible way. Endofunctors map to the same category.

As each object in this category of endofunctors is a function we want to compose, we have to be given a way to do that. This is what that the monoid here does -- it's a way to describe the allowed ways of combining natural transformations, including any systematic tweaks to be done during the combination.

(The classical definition of a monoid is just a set with an associative binary operation with an identity.)



The paragraph starting "As each object in this category..." made no sense to me. I can explain the what a monoid in the category of endofunctors is, how it relates to a Haskell monad and maybe you can explain what you meant.

OK, a monoid in the category of endofunctors is an endofunctor F together with a natural transformation (the multiplication of the monoid) from F composed with F to F, and another natural transformation from id to F (called the unit). In Haskell F is the type constructor of the monad, the unit is called return and the multiplication is usually called join (and in the case of the list monad is also known as concat).

(Note that the definition of monoid needed here is not that of a set with associative binary operation, but the more general "monoid object in a monoidal category". Instead of a set S and a multiplication S x S->S, where x denotes cartesian product, we want a functor F and a multiplication F o F->F where o denotes composition. This is an instance of the general sort of monoid since the category of endofunctors is monoidal with respect to composition.)


I didn't want to give the full categorical definition in terms of what a monoidal category allows, because I don't think that adds much, but I suppose I flubbed making it simpler. I just wanted to make the point that the monoidal portion determines how the composition occurs -- a given type constructor can be a monad multiple ways.


and here I was ready to make a snarky comment about a monad being a specific instance of a polyad


Actually, this made a lot more sense to me. (I guess I really am a math major!)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: