Skip to content

Commit fd066e6

Browse files
authored
chore: make trivial definitions unfold (#525)
This changes function like `toStateM` to be `abbrev`s since they are only a shorthand for `liftM stateInterp`. It also marks `stateInterp` and similar as `@[simp]`, since we want these to reduce when applied to a concrete query. This also fixes `contInterp` to include the missing cast functions, so that the `simp` equation lemma is not ill-formed. Split from #417, to minimize distractions from the actual change in that PR.
1 parent ba40c74 commit fd066e6

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

Cslib/Foundations/Control/Monad/Free/Effects.lean

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ lemma get_def : (get : FreeState σ σ) = .lift .get := rfl
7575
lemma set_def (s : σ) : (set s : FreeState σ PUnit) = .lift (.set s) := rfl
7676

7777
/-- Interpret `StateF` operations into `StateM`. -/
78+
@[simp]
7879
def stateInterp {α : Type u} : StateF σ α → StateM σ α
7980
| .get => MonadStateOf.get
8081
| .set s => MonadStateOf.set s
8182

8283
/-- Convert a `FreeState` computation into a `StateM` computation. This is the canonical
8384
interpreter derived from `liftM`. -/
84-
def toStateM {α : Type u} (comp : FreeState σ α) : StateM σ α :=
85+
abbrev toStateM {α : Type u} (comp : FreeState σ α) : StateM σ α :=
8586
comp.liftM stateInterp
8687

8788
/-- `toStateM` is the unique interpreter extending `stateInterp`. -/
@@ -176,12 +177,13 @@ open WriterF
176177
variable {ω : Type u} {α : Type u}
177178

178179
/-- Interpret `WriterF` operations into `WriterT`. -/
180+
@[simp]
179181
def writerInterp {α : Type u} : WriterF ω α → WriterT ω Id α
180182
| .tell w => MonadWriter.tell w
181183

182184
/-- Convert a `FreeWriter` computation into a `WriterT` computation. This is the canonical
183185
interpreter derived from `liftM`. -/
184-
def toWriterT {α : Type u} [Monoid ω] (comp : FreeWriter ω α) : WriterT ω Id α :=
186+
abbrev toWriterT {α : Type u} [Monoid ω] (comp : FreeWriter ω α) : WriterT ω Id α :=
185187
comp.liftM writerInterp
186188

187189
/-- `toWriterT` is the unique interpreter extending `writerInterp`. -/
@@ -235,12 +237,12 @@ theorem run_toWriterT {α : Type u} [Monoid ω] (comp : FreeWriter ω α) :
235237
(toWriterT comp).run = pure (run comp) := by
236238
ext : 1
237239
induction comp with
238-
| pure _ => simp only [toWriterT, liftM_pure, run_pure, pure, WriterT.run]
240+
| pure _ => simp only [liftM_pure, run_pure, pure, WriterT.run]
239241
| liftBind op cont ih =>
240242
cases op
241-
simp only [toWriterT, liftM_liftBind, run_liftBind_tell, Id.run_pure] at *
243+
simp only [liftM_liftBind, run_liftBind_tell, Id.run_pure] at *
242244
rw [ ← ih]
243-
simp [WriterT.run_bind, writerInterp]
245+
simp [WriterT.run_bind]
244246

245247
/--
246248
`listen` captures the log produced by a subcomputation incrementally. It traverses the computation,
@@ -304,12 +306,13 @@ namespace FreeCont
304306
variable {r : Type u} {α : Type v} {β : Type w}
305307

306308
/-- Interpret `ContF r` operations into `ContT r Id`. -/
309+
@[simp]
307310
def contInterp : ContF r α → ContT r Id α
308-
| .callCC g => g
311+
| .callCC g => .mk fun k => pure <| g fun a => (k a).run
309312

310313
/-- Convert a `FreeCont` computation into a `ContT` computation. This is the canonical
311314
interpreter derived from `liftM`. -/
312-
def toContT {α : Type u} (comp : FreeCont r α) : ContT r Id α :=
315+
abbrev toContT {α : Type u} (comp : FreeCont r α) : ContT r Id α :=
313316
comp.liftM contInterp
314317

315318
/-- `toContT` is the unique interpreter extending `contInterp`. -/
@@ -331,7 +334,7 @@ theorem run_toContT {α : Type u} (comp : FreeCont r α) (k : α → r) :
331334
induction comp with
332335
| pure a => rfl
333336
| liftBind op cont ih =>
334-
simp only [toContT, FreeM.liftM]
337+
simp only [FreeM.liftM]
335338
cases op
336339
simp only [ContT.run_bind]
337340
congr with x
@@ -398,12 +401,13 @@ lemma read_def : (read : FreeReader σ σ) = .lift .read := rfl
398401
instance : MonadReader σ (FreeReader σ) := inferInstance
399402

400403
/-- Interpret `ReaderF` operations into `ReaderM`. -/
404+
@[simp]
401405
def readInterp {α : Type u} : ReaderF σ α → ReaderM σ α
402406
| .read => MonadReaderOf.read
403407

404408
/-- Convert a `FreeReader` computation into a `ReaderM` computation. This is the canonical
405409
interpreter derived from `liftM`. -/
406-
def toReaderM {α : Type u} (comp : FreeReader σ α) : ReaderM σ α :=
410+
abbrev toReaderM {α : Type u} (comp : FreeReader σ α) : ReaderM σ α :=
407411
comp.liftM readInterp
408412

409413
/-- `toReaderM` is the unique interpreter extending `readInterp`. -/

0 commit comments

Comments
 (0)