3 ms·
It means I don't need to know what a monad does before I can begin to characterize what it means to, say, traverse a data structure with that monad. Then once I
by puffoflogic 4y ago
It means I don't need to know what a monad does before I can begin to characterize what it means to, say, traverse a data structure with that monad. Then once I do know what a monad does, the problem of figuring out what it means to traverse a data structure - or do any other operation generic over monads - becomes compositional: the combined knowledge is not more than the sum of its parts.
For example, let's take the Maybe monad and tree traversals. (a) Tree traversals take monadic actions at each node of a tree, nest/sequence them in a certain way, and apply the monad's rules for how to flatten that nesting, with the output being the whole tree with the same structure. (b) The maybe monad is the environment where we can give up on a computation. Now what does it mean to do a tree traversal in the Maybe monad? I don't need to do any additional reasoning, I can simply merge the definitios: "(a) it does some computation at each node and produces a same-shaped tree, (b) unless some computation gives up then the whole thing gives up". There is no novel or sui generis interaction possible between (a) and (b). So reasoning about the program is easier.
Or, for Applicatives: what does it mean to apply a (Nondeterminism (A -> B)) to a (Nondeterminism A)? Well, I don't need to worry about "function in Nondeterminism monad" being some spooky new entity I need to learn or memorize: because I can reason generally about applicatives, I know that the exact same sequencing effect must be used as when I apply a 2-ary pure function to two effectful values. And I know what that is for Nondeterminism: do each possible thing in parallel.
I hope that makes sense, although I think only experience can truly motivate monads. It seems like a bunch of abstract woo because the whole point of monads is to become blasé about monads. When confronted with some bizarre monad, one can shrug and say, "Whatever, it's just a monad, nothing new to see here."