5 ms·
Callbacks are not the same as monads if that is what you are implying.
by bazoom42 2y ago
Callbacks are not the same as monads if that is what you are implying.
- mrkeen 2y agoThat's right, but only because it's a 1-to-many comparison error. Callbacks are one particular monad, not monads in general: https://jsdw.me/posts/haskell-cont-monad/ https://jsdw.me/posts/haskell-cont-monad/
- dumbo-octopus 2y agoBut then CPS machinery is often used to implement Monads. > Programming with monads strongly reminiscent of continuation—passing style (CPS), and this paper explores the relationship between the two. In a sense they are equivalent: CPS arises as a special case of a monad, and any monad may be embedded in CPS by changing the answer type. https://cs.uwaterloo.ca/~david/cs442/monads.pdf https://cs.uwaterloo.ca/~david/cs442/monads.pdf
- bazoom42 2y agoPromises in JavaScript are not monads as far as I can tell. While they have a somewhat similar interface, they do not appear to obey the monad laws.
- explaininjs 2y agoThere is one particular edge case in which they do not satisfy the laws. That happens to make them much more practical in day to day coding than a strict interpretation would be.
- bazoom42 2y agoSo having Promises be monads would make them less useful? This is an interesting point. It seems to confirm the argument that monads are not that useful or pervasive outside of Haskell.
- explaininjs 2y agoWe might consider promises “applied monads” or “engineered monads”. Monodic in inspiration and they solve the same core problem, but they aren’t straightjacketed into the ivory tower “laws” in absolutely every edge case (they do satisfy them in the vast majority of cases). Which is good, because it means we never need to write things like “await await await await await foo()”
- explaininjs 2y agoThis is all besides the root topic though, which is that CPS and Monads are isomorphic. Which is true. Promises are a different thing.
- mrkeen 2y ago> Which is good, because it means we never need to write things like “await await await await await foo() But what your describing is the actual value proposition of monad though! It's literally the construct you use to avoid excessive unwraps. Look at the type signature of bind: m a -> (a -> m b) -> m b It returns m b. Not m (m b). Not m (m (m b)). > but they aren’t straightjacketed into the ivory tower “laws” in absolutely every edge case In other words, you can't abstract over them. If you have libraries that manipulate monads, they will be incompatible or buggy, because they will assume (by design) behaviour that a non-monad will not live up to.
- explaininjs 2y agoI should not need to explain to you that the problem is when b itself is internally an (m b’). Or when it is a sum of many types, of varying m-nests. I can assure you that JS libraries work with Promises just fine.
- acchow 2y agoI was not actually thinking of Javascript (or any particular language) when I mentioned async-await. I was just referring to the concept.