The combined state and list monad transformer. #
The combined state and list monad transformer.
StateListT σ α
is equivalent to StateT σ (ListT α)
but more efficient.
WARNING: StateListT σ α m
is only a monad if m
is a commutative monad.
For example,
def problem : StateListT Unit (StateM (Array Nat)) Unit := do
Alternative.orElse (pure ()) (fun _ => pure ())
StateListT.lift $ modify (·.push 0)
StateListT.lift $ modify (·.push 1)
#eval ((problem.run' ()).run #[]).2
will yield either #[0,1,0,1]
, or #[0,0,1,1]
, depending on the order in which the actions
in the do block are combined.
v
The combined state and list monad transformer.
Equations
- StateListT σ m α = (σ → m (StateList σ α))
Instances For
@[inline]
def
StateListT.run
{α : Type u}
{σ : Type u}
{m : Type u → Type v}
[Functor m]
(x : StateListT σ m α)
(s : σ)
:
Run x
on a given state s
, returning the list of values with corresponding states.
Equations
- StateListT.run x s = StateList.toList <$> x s
Instances For
@[inline]
def
StateListT.run'
{α : Type u}
{σ : Type u}
{m : Type u → Type v}
[Functor m]
(x : StateListT σ m α)
(s : σ)
:
m (List α)
Run x
on a given state s
, returning the list of values.
Equations
- StateListT.run' x s = StateList.toList' <$> x s
Instances For
@[reducible]
The combined state and list monad.
Equations
- StateListM σ α = StateListT σ Id α
Instances For
@[always_inline]
instance
StateListT.instMonadStateListT
{σ : Type u}
{m : Type u → Type v}
[Monad m]
:
Monad (StateListT σ m)
Equations
- StateListT.instMonadStateListT = Monad.mk
instance
StateListT.instAlternativeStateListT
{σ : Type u}
{m : Type u → Type v}
[Monad m]
:
Alternative (StateListT σ m)
Equations
- StateListT.instAlternativeStateListT = Alternative.mk (fun {α : Type u} => StateListT.failure) fun {α : Type u} => StateListT.orElse
@[inline]
Return the state from StateListT σ m
.
Equations
- StateListT.get s = pure (StateList.cons s s StateList.nil)
Instances For
@[inline]
Set the state in StateListT σ m
.
Equations
- StateListT.set s' x = pure (StateList.cons PUnit.unit s' StateList.nil)
Instances For
@[inline]
def
StateListT.modifyGet
{α : Type u}
{σ : Type u}
{m : Type u → Type v}
[Monad m]
(f : σ → α × σ)
:
StateListT σ m α
Modify and get the state in StateListT σ m
.
Equations
- StateListT.modifyGet f s = let a := f s; pure (StateList.cons a.fst a.snd StateList.nil)
Instances For
@[inline]
def
StateListT.lift
{α : Type u}
{σ : Type u}
{m : Type u → Type v}
[Monad m]
(t : m α)
:
StateListT σ m α
Lift an action from m α
to StateListT σ m α
.
Equations
- StateListT.lift t s = do let a ← t pure (StateList.cons a s StateList.nil)
Instances For
instance
StateListT.instMonadLiftStateListT
{σ : Type u}
{m : Type u → Type v}
[Monad m]
:
MonadLift m (StateListT σ m)
@[always_inline]
instance
StateListT.instMonadFunctorStateListT
{σ : Type u}
{m : Type u → Type v}
:
MonadFunctor m (StateListT σ m)
Equations
- StateListT.instMonadFunctorStateListT = { monadMap := fun {α : Type u} (f : {β : Type u} → m β → m β) (x : StateListT σ m α) (s : σ) => f (x s) }
@[always_inline]
instance
StateListT.instMonadExceptOfStateListT
{σ : Type u}
{m : Type u → Type v}
[Monad m]
{ε : Type w}
[MonadExceptOf ε m]
:
MonadExceptOf ε (StateListT σ m)
Equations
- One or more equations did not get rendered due to their size.
instance
instMonadStateOfStateListT
{σ : Type u}
{m : Type u → Type v}
[Monad m]
:
MonadStateOf σ (StateListT σ m)
@[always_inline]
instance
StateListT.monadControl
{σ : Type u}
{m : Type u → Type v}
[Monad m]
:
MonadControl m (StateListT σ m)
Equations
- One or more equations did not get rendered due to their size.