←back to thread

A list is a monad

(alexyorke.github.io)
153 points polygot | 3 comments | | HN request time: 0.813s | source
Show context
kevinventullo ◴[] No.44450020[source]
So I come at this from a math background but I’ve always found these explanations to be overly complex. In the parlance of C++, I think of a monad as a template class T with the following properties:

1. For any class X, there is a canonical method

  F: X -> T<X>
2. For any class X, there is a canonical method

  G: T<T<X>> -> T<X>.
3. For classes X and Y, and any method

  f: X -> Y, 
there is a corresponding method

  “T<f>”: T<X> -> T<Y>.

—————-

Here “any type” means any type that is compatible with the template.

And then there’s some additional rules which make all these methods compatible, based on the different ways of stacking nested T’s and the “canonical” maps you get. Admittedly there is some confusing accounting here, but I also think most natural ways of constructing the above three requirements are going to satisfy them anyway. For List and Maybe it’s fairly obvious what the above methods are.

I dunno, maybe I have it wrong and someone can correct my understanding.

replies(4): >>44450173 #>>44450198 #>>44453789 #>>44457984 #
1. gpderetta ◴[] No.44453789[source]
A monad would not be a template class in C++, it would a concept (more or less the c++ equivalent of an Haskell type class).
replies(1): >>44453911 #
2. feelamee ◴[] No.44453911[source]
monad like an idea - yes, it will be a concept. But a concrete monad - like list or maybe - will be a template class. Probably comment author means this
replies(1): >>44454415 #
3. gpderetta ◴[] No.44454415[source]
Good point. In the same way we say that a pointer is an iterator or a vector is a container, when more precisely we should say that they model the iterator concept and container concept respectively.