Documentation

Init.Control.State

@[inline]
def StateT.run {σ : Type u} {m : Type u → Type v} {α : Type u} (x : StateT σ m α) (s : σ) :
m (α × σ)

Executes an action from a monad with added state in the underlying monad m. Given an initial state, it returns a value paired with the final state.

Equations
@[inline]
def StateT.run' {σ : Type u} {m : Type u → Type v} [Functor m] {α : Type u} (x : StateT σ m α) (s : σ) :
m α

Executes an action from a monad with added state in the underlying monad m. Given an initial state, it returns a value, discarding the final state.

Equations
@[reducible]
def StateM (σ α : Type u) :

A tuple-based state monad.

Actions in StateM σ are functions that take an initial state and return a value paired with a final state.

Equations
Instances For
@[inline]
def StateT.pure {σ : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (a : α) :
StateT σ m α

Returns the given value without modifying the state. Typically used via Pure.pure.

Equations
@[inline]
def StateT.bind {σ : Type u} {m : Type u → Type v} [Monad m] {α β : Type u} (x : StateT σ m α) (f : αStateT σ m β) :
StateT σ m β

Sequences two actions. Typically used via the >>= operator.

Equations
  • x.bind f s = do let __discrx s match __discr with | (a, s) => f a s
@[inline]
def StateT.map {σ : Type u} {m : Type u → Type v} [Monad m] {α β : Type u} (f : αβ) (x : StateT σ m α) :
StateT σ m β

Modifies the value returned by a computation. Typically used via the <$> operator.

Equations
@[always_inline]
instance StateT.instMonad {σ : Type u} {m : Type u → Type v} [Monad m] :
Monad (StateT σ m)
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def StateT.orElse {σ : Type u} {m : Type u → Type v} [Alternative m] {α : Type u} (x₁ : StateT σ m α) (x₂ : UnitStateT σ m α) :
StateT σ m α

Recovers from errors. The state is rolled back on error recovery. Typically used via the <|> operator.

Equations
@[inline]
def StateT.failure {σ : Type u} {m : Type u → Type v} [Alternative m] {α : Type u} :
StateT σ m α

Fails with a recoverable error. The state is rolled back on error recovery.

Equations
instance StateT.instAlternative {σ : Type u} {m : Type u → Type v} [Monad m] [Alternative m] :
Equations
@[inline]
def StateT.get {σ : Type u} {m : Type u → Type v} [Monad m] :
StateT σ m σ

Retrieves the current value of the monad's mutable state.

This increments the reference count of the state, which may inhibit in-place updates.

Equations
@[inline]
def StateT.set {σ : Type u} {m : Type u → Type v} [Monad m] :
σStateT σ m PUnit

Replaces the mutable state with a new value.

Equations
@[inline]
def StateT.modifyGet {σ : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (f : σα × σ) :
StateT σ m α

Applies a function to the current state that both computes a new state and a value. The new state replaces the current state, and the value is returned.

It is equivalent to do let (a, s) := f (← StateT.get); StateT.set s; pure a. However, using StateT.modifyGet may lead to better performance because it doesn't add a new reference to the state value, and additional references can inhibit in-place updates of data.

Equations
@[inline]
def StateT.lift {σ : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (t : m α) :
StateT σ m α

Runs an action from the underlying monad in the monad with state. The state is not modified.

This function is typically implicitly accessed via a MonadLiftT instance as part of automatic lifting.

Equations
instance StateT.instMonadLift {σ : Type u} {m : Type u → Type v} [Monad m] :
MonadLift m (StateT σ m)
Equations
@[always_inline]
instance StateT.instMonadFunctor (σ : Type u_1) (m : Type u_1 → Type u_2) :
Equations
@[always_inline]
instance StateT.instMonadExceptOf {σ : Type u} {m : Type u → Type v} [Monad m] (ε : Type u_1) [MonadExceptOf ε m] :
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def ForM.forIn {m : Type u_1 → Type u_2} {β : Type u_1} {ρ : Type u_3} {α : Type u_4} [Monad m] [ForM (StateT β (ExceptT β m)) ρ α] (x : ρ) (b : β) (f : αβm (ForInStep β)) :
m β

Creates a suitable implementation of ForIn.forIn from a ForM instance.

Equations
  • One or more equations did not get rendered due to their size.
instance instMonadStateOfStateTOfMonad {σ : Type u} {m : Type u → Type v} [Monad m] :
Equations
@[always_inline]
instance StateT.monadControl (σ : Type u) (m : Type u → Type v) [Monad m] :
Equations
  • One or more equations did not get rendered due to their size.
@[always_inline]
instance StateT.tryFinally {m : Type u → Type v} {σ : Type u} [MonadFinally m] [Monad m] :
Equations
  • One or more equations did not get rendered due to their size.