This module exports:
MonadStatetype class and its operations
- Instances of
MonadStatefor the relevant monad transformers from the
- A universal pass-through instance of
MonadStatefor any existing
MonadStatewrapped by a
- The utility operations
A pure functional language cannot update values in place because it violates referential transparency. A common idiom to simulate such stateful computations is to "thread" a state parameter through a sequence of functions:
This approach works, but such code can be error-prone, messy and difficult
to maintain. The
MonadState interface hides the threading of the state
parameter inside the binding operation, simultaneously making the code
easier to write, easier to read and easier to modify.
Embed a simple state action into the monad.
Return the state from the internals of the monad.
Replace the state inside the monad.