Module docstring
{}
{}
EST
definition
(ε : Type) (σ : Type) : Type → Type
ST
abbrev
(σ : Type)
instMonadEST
instance
(ε σ : Type) : Monad (EST ε σ)
instMonadExceptOfEST
instance
(ε σ : Type) : MonadExceptOf ε (EST ε σ)
instance (ε σ : Type) : MonadExceptOf ε (EST ε σ) := inferInstanceAs (MonadExceptOf ε (EStateM _ _))
instInhabitedEST
instance
{ε σ : Type} {α : Type} [Inhabited ε] : Inhabited (EST ε σ α)
instMonadST
instance
(σ : Type) : Monad (ST σ)
STWorld
structure
(σ : outParam Type) (m : Type → Type)
instSTWorldOfMonadLift
instance
{σ m n} [MonadLift m n] [STWorld σ m] : STWorld σ n
instSTWorldEST
instance
{ε σ} : STWorld σ (EST ε σ)
runEST
definition
{ε α : Type} (x : (σ : Type) → EST ε σ α) : Except ε α
/--
Runs an `EST` computation, in which mutable state and exceptions are the only side effects.
-/
@[noinline, nospecialize]
def runEST {ε α : Type} (x : (σ : Type) → EST ε σ α) : Except ε α :=
match x Unit () with
| EStateM.Result.okEStateM.Result.ok a _ => Except.ok a
| EStateM.Result.errorEStateM.Result.error ex _ => Except.error ex
runST
definition
{α : Type} (x : (σ : Type) → ST σ α) : α
/--
Runs an `ST` computation, in which mutable state via `ST.Ref` is the only side effect.
-/
@[noinline, nospecialize]
def runST {α : Type} (x : (σ : Type) → ST σ α) : α :=
match x Unit () with
| EStateM.Result.okEStateM.Result.ok a _ => a
| EStateM.Result.errorEStateM.Result.error ex _ => nomatch ex
instMonadLiftSTEST
instance
{ε σ} : MonadLift (ST σ) (EST ε σ)
@[always_inline]
instance {ε σ} : MonadLift (ST σ) (EST ε σ) := ⟨fun x s =>
match x s with
| EStateM.Result.ok a s => EStateM.Result.ok a s
| EStateM.Result.error ex _ => nomatch ex⟩
ST.RefPointed
opaque
: NonemptyType.{0}
/-- References -/
opaque RefPointed : NonemptyTypeNonemptyType.{0}
ST.Ref
structure
(σ : Type) (α : Type)
ST.instNonemptyRef
instance
{σ α} [s : Nonempty α] : Nonempty (Ref σ α)
instance {σ α} [s : Nonempty α] : Nonempty (Ref σ α) :=
Nonempty.intro { ref := Classical.choice RefPointed.property, h := s }
ST.Prim.mkRef
opaque
{σ α} (a : α) : ST σ (Ref σ α)
@[extern "lean_st_mk_ref"]
opaque mkRef {σ α} (a : α) : ST σ (Ref σ α) := pure { ref := Classical.choice RefPointed.property, h := Nonempty.intro a }
ST.Prim.Ref.get
opaque
{σ α} (r : @& Ref σ α) : ST σ α
ST.Prim.Ref.set
opaque
{σ α} (r : @& Ref σ α) (a : α) : ST σ Unit
ST.Prim.Ref.swap
opaque
{σ α} (r : @& Ref σ α) (a : α) : ST σ α
ST.Prim.Ref.take
opaque
{σ α} (r : @& Ref σ α) : ST σ α
ST.Prim.Ref.ptrEq
opaque
{σ α} (r1 r2 : @& Ref σ α) : ST σ Bool
ST.Prim.Ref.modifyUnsafe
definition
{σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit
@[inline] unsafe def Ref.modifyUnsafe {σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit := do
let v ← Ref.take r
Ref.set r (f v)
ST.Prim.Ref.modifyGetUnsafe
definition
{σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β
@[inline] unsafe def Ref.modifyGetUnsafe {σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β := do
let v ← Ref.take r
let (b, a) := f v
Ref.setRef.set r a
pure b
ST.Prim.Ref.modify
definition
{σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit
@[implemented_by Ref.modifyUnsafe]
def Ref.modify {σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit := do
let v ← Ref.get r
Ref.set r (f v)
ST.Prim.Ref.modifyGet
definition
{σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β
@[implemented_by Ref.modifyGetUnsafe]
def Ref.modifyGet {σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β := do
let v ← Ref.get r
let (b, a) := f v
Ref.setRef.set r a
pure b
ST.mkRef
definition
{α : Type} (a : α) : m (Ref σ α)
/--
Creates a new mutable reference that contains the provided value `a`.
-/
@[inline] def mkRef {α : Type} (a : α) : m (Ref σ α) := liftM <| Prim.mkRef a
ST.Ref.get
definition
{α : Type} (r : Ref σ α) : m α
/--
Reads the value of a mutable reference.
-/
@[inline] def Ref.get {α : Type} (r : Ref σ α) : m α := liftM <| Prim.Ref.get r
ST.Ref.set
definition
{α : Type} (r : Ref σ α) (a : α) : m Unit
/--
Replaces the value of a mutable reference.
-/
@[inline] def Ref.set {α : Type} (r : Ref σ α) (a : α) : m Unit := liftM <| Prim.Ref.set r a
ST.Ref.swap
definition
{α : Type} (r : Ref σ α) (a : α) : m α
/--
Atomically swaps the value of a mutable reference cell with another value. The reference cell's
original value is returned.
-/
@[inline] def Ref.swap {α : Type} (r : Ref σ α) (a : α) : m α := liftM <| Prim.Ref.swap r a
ST.Ref.take
definition
{α : Type} (r : Ref σ α) : m α
/--
Reads the value of a mutable reference cell, removing it.
This causes subsequent attempts to read from or take the reference cell to block until a new value
is written using `ST.Ref.set`.
-/
@[inline] unsafe def Ref.take {α : Type} (r : Ref σ α) : m α := liftM <| Prim.Ref.take r
ST.Ref.ptrEq
definition
{α : Type} (r1 r2 : Ref σ α) : m Bool
/--
Checks whether two reference cells are in fact aliases for the same cell.
Even if they contain the same value, two references allocated by different executions of `IO.mkRef`
or `ST.mkRef` are distinct. Modifying one has no effect on the other. Likewise, a single reference
cell may be aliased, and modifications to one alias also modify the other.
-/
@[inline] def Ref.ptrEq {α : Type} (r1 r2 : Ref σ α) : m Bool := liftM <| Prim.Ref.ptrEq r1 r2
ST.Ref.modify
definition
{α : Type} (r : Ref σ α) (f : α → α) : m Unit
/--
Atomically modifies a mutable reference cell by replacing its contents with the result of a function
call.
-/
@[inline] def Ref.modify {α : Type} (r : Ref σ α) (f : α → α) : m Unit := liftM <| Prim.Ref.modify r f
ST.Ref.modifyGet
definition
{α : Type} {β : Type} (r : Ref σ α) (f : α → β × α) : m β
/--
Atomically modifies a mutable reference cell by replacing its contents with the result of a function
call that simultaneously computes a value to return.
-/
@[inline] def Ref.modifyGet {α : Type} {β : Type} (r : Ref σ α) (f : α → β × α) : m β := liftM <| Prim.Ref.modifyGet r f
ST.Ref.toMonadStateOf
definition
(r : Ref σ α) : MonadStateOf α m
/--
Creates a `MonadStateOf` instance from a reference cell.
This allows programs written against the [state monad](lean-manual://section/state-monads) API to
be executed using a mutable reference cell to track the state.
-/
def Ref.toMonadStateOf (r : Ref σ α) : MonadStateOf α m where
get := r.get
set := r.set
modifyGet := r.modifyGet