state package:capability

state @tag f lifts a pure state computation f to a monadic action in an arbitrary monad m with capability HasState. Given the current state s of the state capability tag and (a, s') = f s, update the state to s' and return a.
Defines a capability type class for a state effect. A state capability provides a state which can be retrieved with get and set with put. As an analogy, each state capability is equivalent to making one IORef available in an IO computation (except, of course, that a state capability does not have to be provided by IO). This is a very expressive capability. It is often preferable to restrict to less powerful capabilities such as Capability.Reader, Capability.Writer, or Capability.Stream.
For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of 'HasState. Otherwise, you will want to use state. See state for more documentation.
Convert a state monad into a reader monad. Use this if the monad stack allows catching exceptions. See ReadStatePure.
Convert a pure state monad into a reader monad. Pure meaning that the monad stack does not allow catching exceptions. Otherwise, an exception occurring in the action passed to local could cause the context to remain modified outside of the call to local. E.g.
local @tag (const r') (throw MyException)
`catch` \MyException -> ask @tag
returns r' instead of the previous value. Note, that no MonadIO instance is provided, as this would allow catching exceptions. See ReadState.
Derive HasState from m's MonadState instance.
State capability An instance should fulfill the following laws. At this point these laws are not definitive, see https://github.com/haskell/mtl/issues/5.
get @t >>= \s1 -> get @t >>= \s2 -> pure (s1, s2) = get @t >>= \s -> pure (s, s)
get @t >>= \_ -> put @t s = put @t s
put @t s1 >> put @t s2 = put @t s2
put @t s >> get @t = put @t s >> pure s
state @t f = get @t >>= \s -> let (a, s') = f s in put @t s' >> pure a
Type synonym using the TypeOf type family to specify HasState constraints without having to specify the type associated to a tag.