3 ms·
A structure is a monad if you can nest one of itself inside another without a visible seam. If you have a binary tree whose leaves are also binary trees, is th
by Twisol 5y ago
A structure is a monad if you can nest one of itself inside another without a visible seam.
If you have a binary tree whose leaves are also binary trees, is that meaningfully different from a single grafted binary tree? Not really.
If you have a sequence (list) of sequences, is that meaningfully different from having a single sequence? Not really.
If you have an option of option of bool, is that meaningfully different from having a single optional? Not really.
More precisely, if you don't care about the difference, structures that are monads let you smooth over the seam between nesting levels.
The flatMap operator lets you extend a data structure at each of its leaves. That is, you can map over each leaf and replace it with another structure of the same kind, then smooth over the seam.
The connection with computation comes about by imagining the data structure as the history of outputs of a process; a trace. You can extend a program trace by specifying what to do next (via flatMap). Clever choices of data structure give you interesting alternative models of computation. But, this is an application of modeling with monads. It's not what a monad is.