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

The only monad anyone ever talks about in my hearing is the IO monad. I sometimes imagine all monads reduce to the IO monad and some specified side effect by intent.

Except of course the whole point of monadic thinking appears to be "no side effects" so .. I'm stuck



People talk a lot about the IO monad because it's one of the most complex monads in general use (and referred to as a monad by a major language and a lot of developers; there are a lot of things that can be considered monads, follow the monad laws, but no one bothers calling them monads).

Monads don't have anything to do with IO or avoiding side effects, they are about composing/combining operations. At heart, the monad laws basically state that if you have two puzzle pieces with similar edges you can snap them together and start thinking about them as a larger puzzle piece in the same puzzle, and that eventually when you are done snapping together pieces you can admire whatever image they form as a result.

The fact that people talk a lot about the IO Monad in Haskell was that it was a jigsaw puzzle that solved a lot of problems in early Haskell, and led to a lot of exploration in monads.


If you know any linear algebra here's an example that has nothing to do with side effects:

Let X be any set. Let mX be the set of formal linear combinations of elements of X (that is, expressions like 3x₁+2x₂ where x₁ and x₂ are elements of X). Then m is a monad.

fmap is variable substitution. Ex. fmap [x₁->y₁, x₂->y₂] (3x₁ + 2x₂) = (3y₁ + 2y₂).

join is simplification of nested expressions. Ex. join (3(2x₁) + 2(x₁ + 2x₂)) = (8x₁ + 4x₂).

>>= is substitution and simplification. Ex. (3x₁ + 2x₂) >>= [x₁->3y₂, x₁->-y₂] = (3(3y₂) + 2(-y₂)) = 7y₂.

A Kleisli arrow X->mY is a system of linear equations, ie. a matrix

    x₁ = 3y₁ + 6y₂
    x₂ =  y₁ - 2y₂
>=> is composition of linear systems, ie. matrix multiplication.

Whereas in IO you think of f >=> g as meaning "first f, then g", here f >=> g is the matrix product fg, ie. "first g, then f".


I was like if m in mX is a monad, how is m not a function? Or if it is a function, how is it different from functions in general?

Googling, I found the following helpful comment:

"...while addition and multiplication are both monoids over the positive natural numbers, a monad is a monoid object in a category of endofunctors: return is the unit, and join is the binary operation. It couldn't be more simple. If that confuses you, it might be helpful to see a Monad as a lax functor from a terminal bicategory"


Wait, I think I may have found a good explanation:

"A few months ago Brent Yorgey complained about a certain class of tutorials which present monads by explaining how monads are like burritos.

At first I thought the choice of burritos was only a facetious reference to the peculiar and sometimes strained analogies these tutorials make. But then I realized that monads are like burritos."

https://blog.plover.com/prog/burritos.html


You can do it, or explain it but you can't do both: proof see above




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

Search: