←back to thread

A list is a monad

(alexyorke.github.io)
153 points polygot | 5 comments | | HN request time: 0.667s | source
1. robinhouston ◴[] No.44445900[source]
I expect the author has done this knowingly, but the title is rather painful for a mathematician to read.

A list is not a monad. List is a monad. A list is an algebra for the List monad.

replies(3): >>44445994 #>>44446493 #>>44448322 #
2. leoh ◴[] No.44445994[source]
I appreciate you mentioning this because I think it’s actually an important point
3. Garlef ◴[] No.44446493[source]
What you said is not correct!

In detail:

* "A list is not a monad" - True!

* "List is a monad" - True. But I think "`List` is a monad" might be clearer.

* "A list is an algebra for the `List` monad." - False!

What's correct is the following:

* "An algebra for the `List` Monad is precisely a monoid."

Sketch of the construction:

(an algebra for the list monad is a monoid): Recall that an algebra is a set/type `A` together with a function `mult: List A -> A` together with some axioms. Think of such a function `mult: List A -> A` as the function that assigns to each list with elements in `A` the product over all those elements. The aforementioned axioms boil down to: (1) `mult([])` is a neutral element and (2) `mult` is an associative binary operation when restricted to two-element lists.

(a monoid is an algebra for the list monad): Same Idea - Given a monoid, we define a function `mult: List A -> A` by assigning to a list of elements of `A` the product of these elements. And the empty list we assign to the neutral element. Then we can use the associativity and properties of the neutral element to show that this function constitutes an algebra for the list monad.

replies(1): >>44447421 #
4. robinhouston ◴[] No.44447421[source]
You're quite right! Thanks for the correction. I should have said that a list is an element of a free algebra over the List monad, which is less pithy.
5. polygot ◴[] No.44448322[source]
Thanks for the feedback! I can definitely rename the post soon as a first step, although this may require rewriting a chunk of the article to more accurately reflect the fact that List is a monad, and not "a" list.

I could make this distinction in part 3 (not written yet) although I want to balance not misleading readers, but not overcomplicating it too early on.