lean4-htt/src/Init/Control/MonadControl.lean
2020-10-27 18:29:19 -07:00

45 lines
1.7 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich, Leonardo de Moura
See: https://lexi-lambda.github.io/blog/2019/09/07/demystifying-monadbasecontrol/
-/
prelude
import Init.Control.MonadLift
universes u v w
class MonadControl (m : Type u → Type v) (n : Type u → Type w) :=
(stM : Type u → Type u)
(liftWith : {α : Type u} → (({β : Type u} → n β → m (stM β)) → m α) → n α)
(restoreM : {α : Type u} → m (stM α) → n α)
class MonadControlT (m : Type u → Type v) (n : Type u → Type w) :=
(stM : Type u → Type u)
(liftWith : {α : Type u} → (({β : Type u} → n β → m (stM β)) → m α) → n α)
(restoreM {α : Type u} : stM α → n α)
export MonadControlT (stM liftWith restoreM)
instance (m n o) [MonadControlT m n] [MonadControl n o] : MonadControlT m o := {
stM := fun α => stM m n (MonadControl.stM n o α),
liftWith := fun f => MonadControl.liftWith fun x₂ => liftWith fun x₁ => f (x₁ ∘ x₂),
restoreM := MonadControl.restoreM ∘ restoreM
}
instance (m : Type u → Type v) [Pure m] : MonadControlT m m := {
stM := fun α => α,
liftWith := fun f => f fun x => x,
restoreM := fun x => pure x
}
@[inline]
def controlAt (m : Type u → Type v) {n : Type u → Type w} [s1 : MonadControlT m n] [s2 : Bind n] {α : Type u}
(f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α :=
liftWith f >>= restoreM
@[inline]
def control {m : Type u → Type v} {n : Type u → Type w} [MonadControlT m n] [Bind n] {α : Type u}
(f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α :=
controlAt m f