The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Control.Basic

29 declarations. Mathlib docs · Source

def zipWithM E1 #

{F : Type u → Type v} → [Applicative F] → {α₁ α₂ φ : Type u} → (α₁ → α₂ → F φ) → List α₁ → List α₂ → F (List φ)

A generalization of List.zipWith which combines list elements with an Applicative.

def zipWithM' E1 #

{α β γ : Type u} → {F : Type u → Type v} → [Applicative F] → (α → β → F γ) → List α → List β → F PUnit.{u + 1}

Like zipWithM but evaluates the result as it traverses the lists using *>.

theorem pure_id'_seq E0 #

∀ {α : Type u} {F : Type u → Type v} [inst : Applicative F] [LawfulApplicative F] (x : F α), (pure fun x ↦ x) <*> x = x

theorem seq_map_assoc E1 #

∀ {α β γ : Type u} {F : Type u → Type v} [inst : Applicative F] [LawfulApplicative F] (x : F (α → β)) (f : γ → α)
  (y : F γ), x <*> f <$> y = (fun x ↦ x ∘ f) <$> x <*> y

theorem map_seq E0 #

∀ {α β γ : Type u} {F : Type u → Type v} [inst : Applicative F] [LawfulApplicative F] (f : β → γ) (x : F (α → β))
  (y : F α), f <$> (x <*> y) = (fun x ↦ f ∘ x) <$> x <*> y

theorem seq_bind_eq E0 #

∀ {α β γ : Type u} {m : Type u → Type v} [inst : Monad m] [LawfulMonad m] (x : m α) {g : β → m γ} {f : α → β},
  f <$> x >>= g = x >>= g ∘ f

theorem fish_pure E1 #

∀ {m : Type u → Type v} [inst : Monad m] [LawfulMonad m] {α : Type u_1} {β : Type u} (f : α → m β), f >=> pure = f

theorem fish_pipe E1 #

∀ {m : Type u → Type v} [inst : Monad m] [LawfulMonad m] {α β : Type u} (f : α → m β), pure >=> f = f

theorem fish_assoc E1 #

∀ {m : Type u → Type v} [inst : Monad m] [LawfulMonad m] {α : Type u_1} {β γ φ : Type u} (f : α → m β) (g : β → m γ)
  (h : γ → m φ), (f >=> g) >=> h = f >=> g >=> h

def List.mapAccumRM E1 #

{α : Type u} →
  {β' γ' : Type v} → {m' : Type v → Type w} → [Monad m'] → (α → β' → 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.mapAccumLM E1 #

{α : Type u} →
  {β' γ' : Type v} → {m' : Type v → Type w} → [Monad m'] → (β' → α → 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).

theorem joinM_map_map E1 #

∀ {m : Type u → Type u} [inst : Monad m] [LawfulMonad m] {α β : Type u} (f : α → β) (a : m (m α)),
  joinM (Functor.map f <$> a) = f <$> joinM a

theorem joinM_map_joinM E1 #

∀ {m : Type u → Type u} [inst : Monad m] [LawfulMonad m] {α : Type u} (a : m (m (m α))),
  joinM (joinM <$> a) = joinM (joinM a)

theorem joinM_map_pure E0 #

∀ {m : Type u → Type u} [inst : Monad m] [LawfulMonad m] {α : Type u} (a : m α), joinM (pure <$> a) = a

theorem joinM_pure E0 #

∀ {m : Type u → Type u} [inst : Monad m] [LawfulMonad m] {α : Type u} (a : m α), joinM (pure a) = a

def succeeds E1 #

{F : Type → Type v} → [Alternative F] → {α : Type} → F α → F Bool

Returns pure true if the computation succeeds and pure false otherwise.

def tryM E1 #

{F : Type → Type v} → [Alternative F] → {α : Type} → F α → F Unit

Attempts to perform the computation, but fails silently if it doesn't succeed.

def try? E1 #

{F : Type → Type v} → [Alternative F] → {α : Type} → F α → F (Option α)

Attempts to perform the computation, and returns none if it doesn't succeed.

theorem guard_true E0 #

∀ {F : Type → Type v} [inst : Alternative F] {h : Decidable True}, guard True = pure ()

theorem guard_false E0 #

∀ {F : Type → Type v} [inst : Alternative F] {h : Decidable False}, guard False = failure

def Sum.bind E1 #

{e : Type v} → {α : Type u_1} → {β : Type u_2} → e ⊕ α → (α → e ⊕ β) → e ⊕ β

The monadic bind operation for Sum.

instance Sum.instMonad_mathlib E0 #

{e : Type v} → Monad (Sum e)

instance Sum.instLawfulFunctor_mathlib E0 #

∀ {e : Type v}, LawfulFunctor (Sum e)

instance Sum.instLawfulMonad_mathlib E0 #

∀ {e : Type v}, LawfulMonad (Sum e)

class CommApplicative E1 #

(m : Type u → Type v) → [Applicative m] → Prop

A CommApplicative functor m is a (lawful) applicative functor which behaves identically on α × β and β × α, so computations can occur in either order.

ctor CommApplicative.mk E0 #

∀ {m : Type u → Type v} [inst : Applicative m] [toLawfulApplicative : LawfulApplicative m],
  (∀ {α β : Type u} (a : m α) (b : m β), Prod.mk <$> a <*> b = (fun b a ↦ (a, b)) <$> b <*> a) → CommApplicative m

instance CommApplicative.toLawfulApplicative E0 #

∀ {m : Type u → Type v} {inst : Applicative m} [self : CommApplicative m], LawfulApplicative m

theorem CommApplicative.commutative_prod E1 #

∀ {m : Type u → Type v} {inst : Applicative m} [self : CommApplicative m] {α β : Type u} (a : m α) (b : m β),
  Prod.mk <$> a <*> b = (fun b a ↦ (a, b)) <$> b <*> a

Computations performed first on a : α and then on b : β are equal to those performed in the reverse order.

theorem CommApplicative.commutative_map E1 #

∀ {m : Type u → Type v} [h : Applicative m] [CommApplicative m] {α β γ : Type u} (a : m α) (b : m β) {f : α → β → γ},
  f <$> a <*> b = flip f <$> b <*> a