I describe four sources of information that you can use to learn about >>= behavior for specific monads.
Type >>=
Type >>= always the same. It is specified in a class like Monad . See the documentation . A type:
(>>=) :: forall a b. ma -> (a -> mb) -> mb
Where m is a placeholder for the particular monad you are interested in. For example, for a list monad, type >>= :
(>>=) :: forall a b. [a] -> (a -> [b]) -> [b]
Note that I just replaced m ... with [...] .
Monad Laws
The implementation >>= is different for each monad, but all monads must adhere to the laws of the monad. These laws are indicated in the class documentation of the Monad type. See more details. The laws:
return a >>= k == kam >>= return == mm >>= (\x -> kx >>= h) == (m >>= k) >>= h
So, whatever the implementation of any particular monad, you can use these laws to talk about your code. For example, if your code contains a code similar to the left side of the law, you can replace this code with the corresponding right side of the law, and the behavior should not change.
Here is an example of how to use the law of the monad. Suppose I wrote this code:
foo = do x <- bar return x
We don’t even know that a monad is used here, but we know that there is a monad, because we see the designation. To apply the monad law, we must drop the designation for calls >>= :
foo = bar >>= (\x -> return x)
Note that \x -> return x matches just return (by η-reduction.
foo = bar >>= return
According to the second law of the monad, this code means the same as the calling bar.
foo = bar
So it seems that >>= in the original function foo cannot do anything at all, because the laws of the monad allow us to just leave it. We understood this without even knowing which concrete monad the >>= operator provides here.
Specific Monad Documentation
If you need to learn more about the behavior >>= for a particular monad, you should report the documentation for that particular monad. You can use hoogle to search for documentation. For example, StateT documentation tells you:
The return function leaves the state unchanged, and >>= uses the final state of the first calculation as the initial state of the second.
The implementation of a specific monad
If you want to know more details about the implementation of a particular monad, you may have to look at the actual implementation. Find the instance Monad ... ad. For example, consider implementing StateT . The implementation of the list monad is somewhere in this file , look for instance Monad [] or look at this, except:
instance Monad [] where m >>= k = foldr ((++) . k) [] m m >> k = foldr ((++) . (\ _ -> k)) [] m return x = [x] fail _ = []
This may not be the most obvious definition, but what exactly happens if you call >>= for the list monad.
Summary
All monads share type signatures for >>= and return and monad laws. Aparat of these restrictions, each monad provides a different implementation of >>= and return , and if you want to know all the details, you will have to study the source code of the instance Monad ... declaration. If you just want to learn how to use a specific monad, try finding documentation on this.