I am a C++/Rust programmer, and out of curiosity, I am currently learning Haskell. As expected, I encountered some obstacles while trying to understand Monads:
I have already read about the definition of Monads on Wikibook, including their relationship to the mathematical definition in category theory and the three Monad laws. I also understand that the do
notation is syntactic sugar for Monads, but certain behaviors in do
blocks continue to puzzle me.
In some tutorials, I’ve noticed that bind
(>>=
) is often explained as applying the second argument (a function) to the value contained within the first argument and return the result of applying the function. However, from the perspective of Monad laws, this doesn’t seem to be the only possible implementation. I’m not sure if bind
must always follow this pattern or if it’s just a common implementation.
If this pattern is indeed common, then I have to consider other implementations. What confuses me the most is that we know:
do { v <- y; ... } = y >>= (v -> ...)
So the meaning of <-
here should be directly related to the implementation of >>=
. But if it’s not the common implementation mentioned above, then <-
might no longer carry the semantics of C-style assignment or binding. How should we understand it in such cases?
Unfortunately, as a beginner, I haven’t yet thought of an example of an uncommon but law-abiding implementation of bind
, despite my efforts.
Here’s the summary of my questions:
- Does there exist an uncommon implementation of
bind
(>>=
) that still satisfies the Monad laws? - Under what I believe to be an uncommon implementation of
bind
, How to understand the meaning of<-
in ado
notation?
feipiao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.