3 ms·
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
by edflsafoiewq 7y ago
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".
- ggm 7y agoYou can do it, or explain it but you can't do both: proof see above
- perl4ever 7y agoI 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"
- perl4ever 7y agoWait, 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 https://blog.plover.com/prog/burritos.html