Some questions about monads in Haskell
haskell, monads
Solution
Is the ability to remove the 'M' from 'M t' something that is only possible inside such a bind operator. Does this bind operator have some special priviliges or something?
Bind is not in any way a special case, but usually it will be defined in the same module as the monads data type. Therefore it might know about (and use) details that are not exported by the module. The usual case would be that the module exports a data type, but not it's constructors or other details about the types inner structure. Then for code that uses the module the inner workings of the data type are invisible and that code cannot directly modify values of this type.
Opposed to that functions defined inside the module, like for example some bind operator `>>=`, can access whatever they like from the module they are defined in. So such functions might be able to do things "external" functions cannot do.
A special case is the `IO` monad, since it's not defined by a module, but built into the runtime system/compiler. Here the compiler knows about the inner details of it's implementation and exposes functions like `IO`'s `>>=`. The implementations of these functions indeed are specially privileged since they live "outside of the program", but this is a special case and this fact should not be observable from within Haskell.
What does it have to do with state changes? I understand (I think) that the goal of monads is to 'wrap' side-effects so that they are isolated from the rest of the program. But what is the role of the bind operator in this?
It doesn't really need to have to do with state changes, this is just one problem that can be handled with moands. The `IO` monad is used to have IO executed in a certain order, but generally monads are just ways of combining functions together.
Generally a monad (specifically it's bind function) defines a way in which certain functions should be composed together to bigger functions. This method of combining functions is abstracted in the monad. How exactly this combining works or why you would want to combine functions in such a way is not important, a monad just specifies a way of combining certain functions in a certain way. (See also this "Monads for C# programmers" answer where I basically repeat that a few times with examples.)
Problem
I'm learning about monads and have a few questions. This is where I am right now. Please correct me where I am wrong. The `>>=` symbol is an infix operator. Infix operators are functions that take two arguments (left-hand side and right-hand side) and return a value. The `>>=` symbol is called the bind operator and has signature `Monad m => m t -> (t -> m u) -> m u`. However, the types don't seem to line up here. We get a value of type `m t` and the second argument is a function that takes a `t`. (I don't see how to connect the dots.) This must mean that the bind function is somehow able to remove the `m` from the `m t` in order to get the `t` and pass it to the function. Here are my questions: Is the ability to remove the `m` from `m t` something that is only possible inside such a bind operator. Does this bind operator have some special priviliges or something? What does it have to do with state changes? I understand (I think) that the goal of monads is to 'wrap' side-effects so that they are isolated from the rest of the program. But what is the role of the bind operator in this?