4 ms·
Ok that all makes sense (I think). OK after sleeping on it, I had an epiphany about monads: A monad is analogous to the imaginary number i. So in FP, a monad
by zackmorris 8y ago
Ok that all makes sense (I think). OK after sleeping on it, I had an epiphany about monads:
A monad is analogous to the imaginary number i.
So in FP, a monad gets passed along as a placeholder until it can be computed. Here is a pretty good description of how they work:
https://medium.com/@newhouseb/motivating-monads-f93c9ddf7014 https://medium.com/@newhouseb/motivating-monads-f93c9ddf7014
I agree with his analysis except that in his "MAGIC TRICK" example, I would have used something that is UNDEFINED in the interim but can be computed to a real value at the end (just like how we use i as a placeholder device in math). His example:
f(x) = x^2 + x + 1
g(x) = 2x
h(x) = sqrt(x)
m(x, f) = UNDEFINED, if x is UNDEFINED f(x), if it isn’t
m(m(h(-1), g), f) ~= (2(sqrt(-1)))^2 + 2(sqrt(-1)) + 1 ~= -3 + 2i ~= UNDEFINED
In this case, the 2(sqrt(x)) is still undefined for -1, so can't be simplified further, so the answer is UNDEFINED.
But I probably would have used an example like:
f(x) = x^2 + 1 # <- note that his was x^2 + x + 1
g(x) = 2x
h(x) = sqrt(x)
m(x, f) = UNDEFINED, if x is UNDEFINED f(x), if it isn’t
m(m(h(-1), g), f) ~= (2(sqrt(-1)))^2 + 1 ~= (2(i))^2 + 1 = -3
See, using i or a monad as a placeholder allows us to compute a real answer.
In FP, all computable parts of the stack could be collapsed (optimized). Then if anything is left uncomputed, the runtime could block on any involved monad. Then when the monad is set to a real value from the input stream, it could be collapsed completely and return the final answer.
I haven't succeeded in learning Haskell yet, but when I tried, I got stuck on its mutability implementation, which seems to use monads:
https://en.wikibooks.org/wiki/Haskell/Mutable_objects https://en.wikibooks.org/wiki/Haskell/Mutable_objects
Honestly today, I would prefer to use pure FP. I think that allowing mutability anywhere in the language is a huge cost, because we can no longer visualize the whole thing as a spreadsheet.
I'd really like to know if another FP language solved the mutating state problem by doing something more like ClojureScript (CS), where it stops execution until the monad gets set from the runtime. Unfortunately, it looks like CS has mutable variables via reset!:
https://stackoverflow.com/a/36847124/539149 https://stackoverflow.com/a/36847124/539149
So it's not pure either :-/
Anyway, I'm still learning. Hope this helps someone.
- cannabis_sam 8y agoI’m not sure that discussion of monads is super helpful. It’s not neccessarily wrong, but it doesn’t get to the meat of the matter. Monads doesn’t represent uncomputable values like sqrt(-1), it’s more like they represent a context, where computations take place. So you can separate all computations involving IO into the parameterized function type ’Monad a’, where a is your result type
- cannabis_sam 8y ago(In the IO case that would be ’IO a’)