Module docstring
{"Extends the theory on functors, applicatives and monads. "}
{"Extends the theory on functors, applicatives and monads. "}
zipWithM
definition
{α₁ α₂ φ : Type u} (f : α₁ → α₂ → F φ) : ∀ (_ : List α₁) (_ : List α₂), F (List φ)
zipWithM'
definition
(f : α → β → F γ) : List α → List β → F PUnit
pure_id'_seq
theorem
(x : F α) : (pure fun x => x) <*> x = x
@[simp]
theorem pure_id'_seq (x : F α) : (pure fun x => x) <*> x = x :=
pure_id_seq x
seq_map_assoc
theorem
(x : F (α → β)) (f : γ → α) (y : F γ) : x <*> f <$> y = (· ∘ f) <$> x <*> y
@[functor_norm]
theorem seq_map_assoc (x : F (α → β)) (f : γ → α) (y : F γ) :
x <*> f <$> y = (· ∘ f) <$> x(· ∘ f) <$> x <*> y := by
simp only [← pure_seq]
simp only [seq_assoc, Function.comp, seq_pure, ← comp_map]
simp [pure_seq]
rfl
map_seq
theorem
(f : β → γ) (x : F (α → β)) (y : F α) : f <$> (x <*> y) = (f ∘ ·) <$> x <*> y
@[functor_norm]
theorem map_seq (f : β → γ) (x : F (α → β)) (y : F α) :
f <$> (x <*> y) = (f ∘ ·) <$> x(f ∘ ·) <$> x <*> y := by
simp only [← pure_seq]; simp [seq_assoc]
seq_bind_eq
theorem
(x : m α) {g : β → m γ} {f : α → β} : f <$> x >>= g = x >>= g ∘ f
theorem seq_bind_eq (x : m α) {g : β → m γ} {f : α → β} :
f <$> xf <$> x >>= g = x >>= g ∘ f :=
show bind (f <$> x) g = bind x (g ∘ f) by
rw [← bind_pure_comp, bind_assoc]
simp [pure_bind, Function.comp_def]
fish_pure
theorem
{α β} (f : α → m β) : f >=> pure = f
@[functor_norm]
theorem fish_pure {α β} (f : α → m β) : f >=> pure = f := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
fish_pipe
theorem
{α β} (f : α → m β) : pure >=> f = f
@[functor_norm]
theorem fish_pipe {α β} (f : α → m β) : purepure >=> f = f := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
fish_assoc
theorem
{α β γ φ} (f : α → m β) (g : β → m γ) (h : γ → m φ) : (f >=> g) >=> h = f >=> g >=> h
@[functor_norm]
theorem fish_assoc {α β γ φ} (f : α → m β) (g : β → m γ) (h : γ → m φ) :
(f >=> g) >=> h = f >=> g >=> h := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
List.mapAccumRM
definition
(f : α → β' → m' (β' × γ')) : β' → List α → m' (β' × List γ')
/-- Takes a value `β` and `List α` and accumulates pairs according to a monadic function `f`.
Accumulation occurs from the right (i.e., starting from the tail of the list). -/
def List.mapAccumRM (f : α → β' → m' (β' × γ')) : β' → List α → m' (β' × List γ')
| a, [] => pure (a, [])
| a, x :: xs => do
let (a', ys) ← List.mapAccumRM f a xs
let (a'', y) ← f x a'
pure (a'', y :: ys)
List.mapAccumLM
definition
(f : β' → α → m' (β' × γ')) : β' → List α → m' (β' × List γ')
/-- Takes a value `β` and `List α` and accumulates pairs according to a monadic function `f`.
Accumulation occurs from the left (i.e., starting from the head of the list). -/
def List.mapAccumLM (f : β' → α → m' (β' × γ')) : β' → List α → m' (β' × List γ')
| a, [] => pure (a, [])
| a, x :: xs => do
let (a', y) ← f a x
let (a'', ys) ← List.mapAccumLM f a' xs
pure (a'', y :: ys)
joinM_map_map
theorem
{α β : Type u} (f : α → β) (a : m (m α)) : joinM (Functor.map f <$> a) = f <$> joinM a
theorem joinM_map_map {α β : Type u} (f : α → β) (a : m (m α)) :
joinM (Functor.mapFunctor.map f <$> a) = f <$> joinM a := by
simp only [joinM, (· ∘ ·), id, ← bind_pure_comp, bind_assoc, map_bind, pure_bind]
joinM_map_joinM
theorem
{α : Type u} (a : m (m (m α))) : joinM (joinM <$> a) = joinM (joinM a)
theorem joinM_map_joinM {α : Type u} (a : m (m (m α))) : joinM (joinMjoinM <$> a) = joinM (joinM a) := by
simp only [joinM, (· ∘ ·), id, map_bind, ← bind_pure_comp, bind_assoc, pure_bind]
joinM_map_pure
theorem
{α : Type u} (a : m α) : joinM (pure <$> a) = a
@[simp]
theorem joinM_map_pure {α : Type u} (a : m α) : joinM (purepure <$> a) = a := by
simp only [joinM, (· ∘ ·), id, map_bind, ← bind_pure_comp, bind_assoc, pure_bind, bind_pure]
joinM_pure
theorem
{α : Type u} (a : m α) : joinM (pure a) = a
@[simp]
theorem joinM_pure {α : Type u} (a : m α) : joinM (pure a) = a :=
LawfulMonad.pure_bind a id
succeeds
definition
{α} (x : F α) : F Bool
tryM
definition
{α} (x : F α) : F Unit
try?
definition
{α} (x : F α) : F (Option α)
guard_true
theorem
{h : Decidable True} : @guard F _ True h = pure ()
guard_false
theorem
{h : Decidable False} : @guard F _ False h = failure
Sum.bind
definition
{α β} : e ⊕ α → (α → e ⊕ β) → e ⊕ β
Sum.instMonad_mathlib
instance
: Monad (Sum.{v, u} e)
Sum.instLawfulFunctor_mathlib
instance
: LawfulFunctor (Sum.{v, u} e)
instance : LawfulFunctor (Sum.{v, u} e) := by
constructor <;> intros <;> (try casesm Sum _ _) <;> rfl
Sum.instLawfulMonad_mathlib
instance
: LawfulMonad (Sum.{v, u} e)
instance : LawfulMonad (Sum.{v, u} e) where
seqRight_eq := by
intros
casesm Sum _ _ <;> casesm Sum _ _ <;> rfl
seqLeft_eq := by
intros
casesm Sum _ _ <;> rfl
pure_seq := by
intros
rfl
bind_assoc := by
intros
casesm Sum _ _ <;> rfl
pure_bind := by
intros
rfl
bind_pure_comp := by
intros
casesm Sum _ _ <;> rfl
bind_map := by
intros
casesm Sum _ _ <;> rfl
CommApplicative
structure
(m : Type u → Type v) [Applicative m] : Prop extends LawfulApplicative m
/-- A `CommApplicative` functor `m` is a (lawful) applicative functor which behaves identically on
`α × β` and `β × α`, so computations can occur in either order. -/
class CommApplicative (m : Type u → Type v) [Applicative m] : Prop extends LawfulApplicative m where
/-- Computations performed first on `a : α` and then on `b : β` are equal to those performed in
the reverse order. -/
commutative_prod : ∀ {α β} (a : m α) (b : m β),
Prod.mkProd.mk <$> aProd.mk <$> a <*> b = (fun (b : β) a => (a, b)) <$> b(fun (b : β) a => (a, b)) <$> b <*> a
CommApplicative.commutative_map
theorem
{m : Type u → Type v} [h : Applicative m] [CommApplicative m] {α β γ} (a : m α) (b : m β) {f : α → β → γ} :
f <$> a <*> b = flip f <$> b <*> a
theorem CommApplicative.commutative_map {m : Type u → Type v} [h : Applicative m]
[CommApplicative m] {α β γ} (a : m α) (b : m β) {f : α → β → γ} :
f <$> af <$> a <*> b = flipflip f <$> bflip f <$> b <*> a :=
calc
f <$> af <$> a <*> b = (fun p : α × β => f p.1 p.2) <$> (Prod.mk <$> a <*> b) := by
simp only [map_seq, map_map, Function.comp_def]
_ = (fun b a => f a b) <$> b(fun b a => f a b) <$> b <*> a := by
rw [@CommApplicative.commutative_prod m h]
simp [seq_map_assoc, map_seq, seq_assoc, seq_pure, map_map, (· ∘ ·)]
rfl