Laws for Monads with State #
This file defines a typeclass for MonadStateOf
with compatible get
and set
operations.
Note that we use MonadStateOf
over MonadState
as the first induces the second,
but we phrase things using MonadStateOf.set
and MonadState.get
as those are the
versions that are available at the top level namespace.
The namespaced MonadStateOf.get
is equal to the MonadState
provided get
.
The namespaced MonadStateOf.modifyGet
is equal to the MonadState
provided modifyGet
.
Class for well behaved state monads, extending the base MonadState
type.
Requires that modifyGet
is equal to the same definition with only get
and set
,
that get
is idempotent if the result isn't used, and that get
after set
returns
exactly the value that was previously set
.
- seq_assoc {α β γ : Type u_1} (x : m α) (g : m (α → β)) (h : m (β → γ)) : h <*> (g <*> x) = Function.comp <$> h <*> g <*> x
- modifyGet_eq {α : Type u_1} (f : σ → α × σ) : modifyGet f = do let z ← f <$> get set z.snd pure z.fst
modifyGet f
is equal to getting the state, modifying it, and returning a result. Discarding the result of
get
is the same as never getting the state.- get_bind_get_bind {α : Type u_1} (mx : σ → σ → m α) : (do let s ← get let s' ← get mx s s') = do let s ← get mx s s
Calling
get
twice is the same as just using the first retreived state value. - get_bind_set_bind {α : Type u_1} (mx : σ → PUnit → m α) : (do let s ← get let u ← set s mx s u) = do let s ← get mx s PUnit.unit
Setting the monad state to its current value has no effect.
Setting and then returning the monad state is the same as returning the set value.
Setting the monad twice is the same as just setting to the final state.
Instances
Version of modifyGet_eq
that preserves an call to modify
.
StateT
has lawful state operations if the underlying monad is lawful.
This is applied for StateM
as well due to the reducibility of that definition.
The continuation passing state monad variant StateCpsT
always has lawful state instance.
The EStateM
monad always has a lawful state instance.
If the underlying monad m
has a lawful state instance, then the induced state instance on
ReaderT ρ m
will also be lawful.