You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add MonadControl lemmas for ReaderT, OptionT, StateT, and ExceptT (#11591)
This PR adds missing lemmas about how `ReaderT.run`, `OptionT.run`,
`StateT.run`, and `ExceptT.run` interact with `MonadControl` operations.
This also leaves some comments noting that the lemmas may look less
general than expected; but this is because the instances are also not
very general.
@[simp, grind =]theoremrun_monadMap [MonadFunctorT n m] (f : {β : Type u} → n β → n β) (x : ExceptT ε m α)
64
+
: (monadMap @f x : ExceptT ε m α).run = monadMap @f (x.run) := rfl
65
+
60
66
protectedtheoremseq_eq {α β ε : Type u} [Monad m] (mf : ExceptT ε m (α → β)) (x : ExceptT ε m α) : mf <*> x = mf >>= fun f => f <$> x :=
61
67
rfl
62
68
@@ -99,6 +105,22 @@ instance [Monad m] [LawfulMonad m] : LawfulMonad (ExceptT ε m) where
99
105
simp only [ExceptT.instMonad, ExceptT.map, ExceptT.mk, throw, throwThe, MonadExceptOf.throw,
100
106
pure_bind]
101
107
108
+
/-! Note that the `MonadControl` instance for `ExceptT` is not monad-generic. -/
109
+
110
+
@[simp]theoremrun_restoreM [Monad m] (x : stM m (ExceptT ε m) α) :
111
+
ExceptT.run (restoreM x) = pure x := rfl
112
+
113
+
@[simp]theoremrun_liftWith [Monad m] (f : ({β : Type u} → ExceptT ε m β → m (stM m (ExceptT ε m) β)) → m α) :
114
+
ExceptT.run (liftWith f) = Except.ok <$> (f fun x => x.run) :=
115
+
rfl
116
+
117
+
@[simp]theoremrun_controlAt [Monad m] [LawfulMonad m] (f : ({β : Type u} → ExceptT ε m β → m (stM m (ExceptT ε m) β)) → m (stM m (ExceptT ε m) α)) :
118
+
ExceptT.run (controlAt m f) = f fun x => x.run := by
119
+
simp [controlAt, run_bind, bind_map_left]
120
+
121
+
@[simp]theoremrun_control [Monad m] [LawfulMonad m] (f : ({β : Type u} → ExceptT ε m β → m (stM m (ExceptT ε m) β)) → m (stM m (ExceptT ε m) α)) :
122
+
ExceptT.run (control f) = f fun x => x.run := run_controlAt f
123
+
102
124
end ExceptT
103
125
104
126
/-! # Except -/
@@ -152,6 +174,9 @@ namespace OptionT
152
174
apply bind_congr
153
175
intro a; cases a <;> simp [OptionT.pure, OptionT.mk]
154
176
177
+
@[simp, grind =]theoremrun_monadMap [MonadFunctorT n m] (f : {β : Type u} → n β → n β) (x : OptionT m α)
178
+
: (monadMap @f x : OptionT m α).run = monadMap @f (x.run) := rfl
179
+
155
180
protectedtheoremseq_eq {α β : Type u} [Monad m] (mf : OptionT m (α → β)) (x : OptionT m α) : mf <*> x = mf >>= fun f => f <$> x :=
156
181
rfl
157
182
@@ -213,6 +238,24 @@ instance [Monad m] [LawfulMonad m] : LawfulMonad (OptionT m) where
213
238
(x <|> y).run = Option.elimM x.run y.run (fun x => pure (some x)) :=
214
239
bind_congr fun | some _ => by rfl | none => by rfl
215
240
241
+
/-! Note that the `MonadControl` instance for `OptionT` is not monad-generic. -/
242
+
243
+
@[simp]theoremrun_restoreM [Monad m] (x : stM m (OptionT m) α) :
244
+
OptionT.run (restoreM x) = pure x := rfl
245
+
246
+
@[simp]theoremrun_liftWith [Monad m] [LawfulMonad m] (f : ({β : Type u} → OptionT m β → m (stM m (OptionT m) β)) → m α) :
247
+
OptionT.run (liftWith f) = Option.some <$> (f fun x => x.run) := by
248
+
dsimp [liftWith]
249
+
rw [← bind_pure_comp]
250
+
rfl
251
+
252
+
@[simp]theoremrun_controlAt [Monad m] [LawfulMonad m] (f : ({β : Type u} → OptionT m β → m (stM m (OptionT m) β)) → m (stM m (OptionT m) α)) :
253
+
OptionT.run (controlAt m f) = f fun x => x.run := by
254
+
simp [controlAt, Option.elimM, Option.elim]
255
+
256
+
@[simp]theoremrun_control [Monad m] [LawfulMonad m] (f : ({β : Type u} → OptionT m β → m (stM m (OptionT m) β)) → m (stM m (OptionT m) α)) :
257
+
OptionT.run (control f) = f fun x => x.run := run_controlAt f
258
+
216
259
end OptionT
217
260
218
261
/-! # Option -/
@@ -284,6 +327,22 @@ instance [Monad m] [LawfulMonad m] : LawfulMonad (ReaderT ρ m) where
284
327
pure_bind := by intros; apply ext; intros; simp
285
328
bind_assoc := by intros; apply ext; intros; simp
286
329
330
+
/-! Note that the `MonadControl` instance for `ReaderT` is not monad-generic. -/
331
+
332
+
@[simp]theoremrun_restoreM [Monad m] (x : stM m (ReaderT ρ m) α) (ctx : ρ) :
333
+
ReaderT.run (restoreM x) ctx = pure x := rfl
334
+
335
+
@[simp]theoremrun_liftWith [Monad m] (f : ({β : Type u} → ReaderT ρ m β → m (stM m (ReaderT ρ m) β)) → m α) (ctx : ρ) :
336
+
ReaderT.run (liftWith f) ctx = (f fun x => x.run ctx) :=
337
+
rfl
338
+
339
+
@[simp]theoremrun_controlAt [Monad m] [LawfulMonad m] (f : ({β : Type u} → ReaderT ρ m β → m (stM m (ReaderT ρ m) β)) → m (stM m (ReaderT ρ m) α)) (ctx : ρ) :
340
+
ReaderT.run (controlAt m f) ctx = f fun x => x.run ctx := by
341
+
simp [controlAt]
342
+
343
+
@[simp]theoremrun_control [Monad m] [LawfulMonad m] (f : ({β : Type u} → ReaderT ρ m β → m (stM m (ReaderT ρ m) β)) → m (stM m (ReaderT ρ m) α)) (ctx : ρ) :
344
+
ReaderT.run (control f) ctx = f fun x => x.run ctx := run_controlAt f ctx
345
+
287
346
end ReaderT
288
347
289
348
/-! # StateRefT -/
@@ -307,11 +366,11 @@ namespace StateT
307
366
@[simp, grind =]theoremrun_pure [Monad m] (a : α) (s : σ) : (pure a : StateT σ m α).run s = pure (a, s) := rfl
308
367
309
368
@[simp, grind =]theoremrun_bind [Monad m] (x : StateT σ m α) (f : α → StateT σ m β) (s : σ)
310
-
: (x >>= f).run s = x.run s >>= λ p => (f p.1).run p.2 := by
311
-
simp [bind, StateT.bind, run]
369
+
: (x >>= f).run s = x.run s >>= λ p => (f p.1).run p.2 := rfl
312
370
313
371
@[simp, grind =]theoremrun_map {α β σ : Type u} [Monad m] [LawfulMonad m] (f : α → β) (x : StateT σ m α) (s : σ) : (f <$> x).run s = (fun (p : α × σ) => (f p.1, p.2)) <$> x.run s := by
@[simp, grind =]theoremrun_lift {α σ : Type u} [Monad m] (x : m α) (s : σ) : (StateT.lift x : StateT σ m α).run s = x >>= fun a => pure (a, s) := rfl
326
385
327
386
@[grind =]
328
387
theoremrun_bind_lift {α σ : Type u} [Monad m] [LawfulMonad m] (x : m α) (f : α → StateT σ m β) (s : σ) : (StateT.lift x >>= f).run s = x >>= fun a => (f a).run s := by
329
-
simp [StateT.lift, StateT.run, bind, StateT.bind]
388
+
simp
330
389
331
390
@[simp, grind =]theoremrun_monadLift {α σ : Type u} [Monad m] [MonadLiftT n m] (x : n α) (s : σ) : (monadLift x : StateT σ m α).run s = (monadLift x : m α) >>= fun a => pure (a, s) := rfl
332
391
@@ -366,6 +425,24 @@ instance [Monad m] [LawfulMonad m] : LawfulMonad (StateT σ m) where
366
425
pure_bind := by intros; apply ext; intros; simp
367
426
bind_assoc := by intros; apply ext; intros; simp
368
427
428
+
/-! Note that the `MonadControl` instance for `StateT` is not monad-generic. -/
429
+
430
+
@[simp]theoremrun_restoreM [Monad m] [LawfulMonad m] (x : stM m (StateT σ m) α) (s : σ) :
431
+
StateT.run (restoreM x) s = pure x := by
432
+
simp [restoreM, MonadControl.restoreM]
433
+
rfl
434
+
435
+
@[simp]theoremrun_liftWith [Monad m] [LawfulMonad m] (f : ({β : Type u} → StateT σ m β → m (stM m (StateT σ m) β)) → m α) (s : σ) :
436
+
StateT.run (liftWith f) s = ((·, s) <$> f fun x => x.run s) := by
@[simp]theoremrun_controlAt [Monad m] [LawfulMonad m] (f : ({β : Type u} → StateT σ m β → m (stM m (StateT σ m) β)) → m (stM m (StateT σ m) α)) (s : σ) :
440
+
StateT.run (controlAt m f) s = f fun x => x.run s := by
441
+
simp [controlAt]
442
+
443
+
@[simp]theoremrun_control [Monad m] [LawfulMonad m] (f : ({β : Type u} → StateT σ m β → m (stM m (StateT σ m) β)) → m (stM m (StateT σ m) α)) (s : σ) :
444
+
StateT.run (control f) s = f fun x => x.run s := run_controlAt f s
0 commit comments