Skip to content

Commit 466a248

Browse files
authored
chore: add ReaderT.mk and StateT.mk (#11470)
These complement the existing `ExceptT.mk` and `OptionT.mk`, and provide a symbol to key `simp` lemmas on, to prevent getting stuck on `StateT.run (fun s => f s) s`. A future PR could insert these new `mk`s into the implementation of many definitions, such that unfolding the definitions leaves appropriate casts behind; but this is invasive, and by itself having `mk` provides value.
1 parent acb7bc5 commit 466a248

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/Init/Control/Lawful/Instances.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ namespace ReaderT
234234
simp [run] at h
235235
exact funext h
236236

237+
@[simp, grind =] theorem run_mk (x : ρ → m α) (ctx : ρ) : run (.mk x : ReaderT ρ m α) ctx = x ctx :=
238+
rfl
239+
237240
@[simp, grind =] theorem run_pure [Monad m] (a : α) (ctx : ρ) : (pure a : ReaderT ρ m α).run ctx = pure a := rfl
238241

239242
@[simp, grind =] theorem run_bind [Monad m] (x : ReaderT ρ m α) (f : α → ReaderT ρ m β) (ctx : ρ)
@@ -295,6 +298,9 @@ namespace StateT
295298
@[ext, grind ext] theorem ext {x y : StateT σ m α} (h : ∀ s, x.run s = y.run s) : x = y :=
296299
funext h
297300

301+
@[simp, grind =] theorem run_mk [Monad m] (x : σ → m (α × σ)) (s : σ) : run (.mk x) s = x s :=
302+
rfl
303+
298304
@[simp, grind =] theorem run'_eq [Monad m] (x : StateT σ m α) (s : σ) : run' x s = (·.1) <$> run x s :=
299305
rfl
300306

src/Init/Control/State.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ of a value and a state.
2525
@[expose] def StateT (σ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) :=
2626
σ → m (α × σ)
2727

28+
/--
29+
Interpret `σ → m (α × σ)` as an element of `StateT σ m α`.
30+
-/
31+
@[always_inline, inline, expose]
32+
def StateT.mk {σ : Type u} {m : Type u → Type v} {α : Type u} (x : σ → m (α × σ)) : StateT σ m α := x
33+
2834
/--
2935
Executes an action from a monad with added state in the underlying monad `m`. Given an initial
3036
state, it returns a value paired with the final state.

src/Init/Prelude.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,12 @@ ordinary actions in `m`.
40474047
def ReaderT (ρ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) :=
40484048
ρ → m α
40494049

4050+
/--
4051+
Interpret `ρ → m α` as an element of `ReaderT ρ m α`.
4052+
-/
4053+
@[always_inline, inline]
4054+
def ReaderT.mk {ρ : Type u} {m : Type u → Type v} {α : Type u} (x : ρ → m α) : ReaderT ρ m α := x
4055+
40504056
instance (ρ : Type u) (m : Type u → Type v) (α : Type u) [Inhabited (m α)] : Inhabited (ReaderT ρ m α) where
40514057
default := fun _ => default
40524058

0 commit comments

Comments
 (0)