The State monad is reminiscent of the IO monad. The IO monad hides world remaking functions whereas the State monad wraps state remaking functions. But hiding is more complete than wrapping. The IO monad hides details from the programmer. The State monad allows the programmer to wrap details in an abstraction.
In both monads this
works by evaluating mon1 and passing the remade world to mon2.
Both an IO a and a State a wrap a function. The function creates a new world or state and an interior object of type a. Haskell lets us describe such a thing with
where s is the state and a is the interior object. With this in mind we can describe a State monad this way
The State monad wraps a state remaking function and gives it a name, runState.
Note:
There are two parameters. Like a monad transformer, the so-called State monad is not a monad but rather a constructor of monads. We need to provide an argument for the state parameter, as in State s before we have an actual monad. However, this won't prevent us from saying “State monad”.
Evaluation of
creates a new State monad with a new state remaking function. That function can later be applied to some initial state. This is much like evaluating (>>=) in the IO monad where a new world remaking function is created that is then evaluated by the run time system when the program is executed.