ソースコード:
/- The State monad transformer. -/ prelude import Init.Control.Basic import Init.Control.Id import Init.Control.Except universe u v w def StateT (σ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) := σ → m (α × σ) @[always_inline, inline] def StateT.run {σ : Type u} {m : Type u → Type v} {α : Type u} (x : StateT σ m α) (s : σ) : m (α × σ) := x s @[always_inline, inline] def StateT.run' {σ : Type u} {m : Type u → Type v} [Functor m] {α : Type u} (x : StateT σ m α) (s : σ) : m α := (·.1) <$> x s @[reducible] def StateM (σ α : Type u) : Type u := StateT σ Id α instance {σ α} [Subsingleton σ] [Subsingleton α] : Subsingleton (StateM σ α) where allEq x y := by apply funext intro s match x s, y s with | (a₁, s₁), (a₂, s₂) => rw [Subsingleton.elim a₁ a₂, Subsingleton.elim s₁ s₂] namespace StateT section variable {σ : Type u} {m : Type u → Type v} variable [Monad m] {α β : Type u} @[always_inline, inline] protected def pure (a : α) : StateT σ m α := fun s => pure (a, s) @[always_inline, inline] protected def bind (x : StateT σ m α) (f : α → StateT σ m β) : StateT σ m β := fun s => do let (a, s) ← x s; f a s @[always_inline, inline] protected def map (f : α → β) (x : StateT σ m α) : StateT σ m β := fun s => do let (a, s) ← x s; pure (f a, s) @[always_inline] instance : Monad (StateT σ m) where pure := StateT.pure bind := StateT.bind map := StateT.map // 以下省略