@@ -281,6 +281,81 @@ theorem withTheReader_MonadWithReaderOf [MonadWithReaderOf ρ m] [WP m ps] (f :
281281
282282end MonadFunctor
283283
284+ /-! ## `MonadControl`
285+
286+ The definitions that follow interpret `liftWith` and thus instances of, e.g., `MonadReaderWithOf`.
287+
288+ -/
289+
290+ section MonadControl
291+
292+ @[simp]
293+ theorem liftWith_StateT [Monad m] [WPMonad m ps]
294+ (f : (∀{β}, StateT σ m β → m (β × σ)) → m α) :
295+ wp⟦MonadControl.liftWith (m:=m) f⟧ Q = fun s => wp⟦f (fun x => x.run s)⟧ (fun a => Q.1 a s, Q.2 ) := by
296+ simp [MonadControl.liftWith, StateT.run]
297+
298+ @[simp]
299+ theorem liftWith_ReaderT [Monad m] [WPMonad m ps]
300+ (f : (∀{β}, ReaderT ρ m β → m β) → m α) :
301+ wp⟦MonadControl.liftWith (m:=m) f⟧ Q = fun s => wp⟦f (fun x => x.run s)⟧ (fun a => Q.1 a s, Q.2 ) := by
302+ simp [wp, MonadControl.liftWith, ReaderT.run]
303+
304+ @[simp]
305+ theorem liftWith_ExceptT [Monad m] [WPMonad m ps]
306+ (f : (∀{β}, ExceptT ε m β → m (Except ε β)) → m α) :
307+ wp⟦MonadControl.liftWith (m:=m) f⟧ Q = wp⟦f (fun x => x.run)⟧ (Q.1 , Q.2 .2 ) := by
308+ -- For some reason, the spec for `liftM` does not apply.
309+ simp [wp, MonadControl.liftWith, ExceptT.run, liftM, monadLift, MonadLift.monadLift, ExceptT.lift, ExceptT.mk]
310+
311+ @[simp]
312+ theorem liftWith_trans [WP o ps] [MonadControl n o] [MonadControlT m n]
313+ (f : (∀{β}, o β → m (stM m o β)) → m α) :
314+ wp⟦MonadControlT.liftWith f : o α⟧ Q = wp⟦MonadControl.liftWith (m:=n) fun x₂ => MonadControlT.liftWith fun x₁ => f (x₁ ∘ x₂) : o α⟧ Q := rfl
315+
316+ @[simp]
317+ theorem liftWith_refl [WP m ps] [Pure m]
318+ (f : (∀{β}, m β → m β) → m α) :
319+ wp⟦MonadControlT.liftWith (m:=m) f : m α⟧ Q = wp⟦f (fun x => x) : m α⟧ Q := rfl
320+
321+ @[simp]
322+ theorem restoreM_StateT [Monad m] [WPMonad m ps] (x : m (α × σ)) :
323+ wp⟦MonadControl.restoreM (m:=m) x : StateT σ m α⟧ Q = fun _ => wp⟦x⟧ (fun (a, s) => Q.1 a s, Q.2 ) := by
324+ simp [MonadControl.restoreM]
325+
326+ @[simp]
327+ theorem restoreM_ReaderT [Monad m] [WPMonad m ps] (x : m α) :
328+ wp⟦MonadControl.restoreM (m:=m) x : ReaderT ρ m α⟧ Q = fun s => wp⟦x⟧ (fun a => Q.1 a s, Q.2 ) := by
329+ simp [wp, MonadControl.restoreM]
330+
331+ @[simp]
332+ theorem restoreM_ExceptT [Monad m] [WPMonad m ps] (x : m (Except ε α)) :
333+ wp⟦MonadControl.restoreM (m:=m) x : ExceptT ε m α⟧ Q = wp⟦x⟧ (fun | .ok a => Q.1 a | .error e => Q.2 .1 e, Q.2 .2 ) := by
334+ simp [wp, MonadControl.restoreM]
335+ congr
336+ ext
337+ split <;> rfl
338+
339+ @[simp]
340+ theorem restoreM_trans [WP o ps] [MonadControl n o] [MonadControlT m n] (x : stM m o α) :
341+ wp⟦MonadControlT.restoreM x : o α⟧ Q = wp⟦MonadControl.restoreM (m:=n) (MonadControlT.restoreM (m:=m) x) : o α⟧ Q := rfl
342+
343+ @[simp]
344+ theorem restoreM_refl [Pure m] [WP m ps] (x : stM m m α) :
345+ wp⟦MonadControlT.restoreM x : m α⟧ Q = wp⟦Pure.pure x : m α⟧ Q := rfl
346+
347+ @[simp]
348+ theorem controlAt_MonadControlT [Bind n] [WP n ps] [MonadControlT m n]
349+ (f : (∀{β}, n β → m (stM m n β)) → m (stM m n α)) :
350+ wp⟦controlAt m f⟧ Q = wp⟦liftWith f >>= restoreM⟧ Q := rfl
351+
352+ @[simp]
353+ theorem control_MonadControlT [Bind n] [WP n ps] [MonadControlT m n]
354+ (f : (∀{β}, n β → m (stM m n β)) → m (stM m n α)) :
355+ wp⟦control f⟧ Q = wp⟦liftWith f >>= restoreM⟧ Q := rfl
356+
357+ end MonadControl
358+
284359/-! ## `MonadExceptOf`
285360
286361The definitions that follow interpret `throw`, `throwThe`, `tryCatch`, etc.
0 commit comments