Documentation

Mathlib.Control.Basic

Extends the theory on functors, applicatives and monads.

theorem Functor.map_map {α : Type u} {β : Type u} {γ : Type u} {f : Type u → Type v} [Functor f] [LawfulFunctor f] (m : α → β) (g : β → γ) (x : f α) :
g <$> m <$> x = (g ∘ m) <$> x
def zipWithM {F : Type u → Type v} [Applicative F] {α₁ : Type u} {α₂ : Type u} {φ : Type u} (f : α₁ → α₂ → F φ) :
List α₁ → List α₂ → F (List φ)

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

Equations
Instances For
    def zipWithM' {α : Type u} {β : Type u} {γ : Type u} {F : Type u → Type v} [Applicative F] (f : α → β → F γ) :
    List α → List β → F PUnit

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

    Equations
    Instances For
      @[simp]
      theorem pure_id'_seq {α : Type u} {F : Type u → Type v} [Applicative F] [LawfulApplicative F] (x : F α) :
      (Seq.seq (pure fun x => x) fun x => x) = x
      theorem seq_map_assoc {α : Type u} {β : Type u} {γ : Type u} {F : Type u → Type v} [Applicative F] [LawfulApplicative F] (x : F (α → β)) (f : γ → α) (y : F γ) :
      (Seq.seq x fun x => f <$> y) = Seq.seq ((fun x => x ∘ f) <$> x) fun x => y
      theorem map_seq {α : Type u} {β : Type u} {γ : Type u} {F : Type u → Type v} [Applicative F] [LawfulApplicative F] (f : β → γ) (x : F (α → β)) (y : F α) :
      (f <$> Seq.seq x fun x => y) = Seq.seq ((fun x => f ∘ x) <$> x) fun x => y
      theorem map_bind {α : Type u} {β : Type u} {γ : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] (x : m α) {g : α → m β} {f : β → γ} :
      f <$> (x >>= g) = do let a ← x f <$> g a
      theorem seq_bind_eq {α : Type u} {β : Type u} {γ : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] (x : m α) {g : β → m γ} {f : α → β} :
      f <$> x >>= g = x >>= g ∘ f
      theorem fish_pure {m : Type u → Type v} [Monad m] [LawfulMonad m] {α : Type u_1} {β : Type u} (f : α → m β) :
      f >=> pure = f
      theorem fish_pipe {m : Type u → Type v} [Monad m] [LawfulMonad m] {α : Type u} {β : Type u} (f : α → m β) :
      pure >=> f = f
      theorem fish_assoc {m : Type u → Type v} [Monad m] [LawfulMonad m] {α : Type u_1} {β : Type u} {γ : Type u} {φ : Type u} (f : α → m β) (g : β → m γ) (h : γ → m φ) :
      (f >=> g) >=> h = f >=> g >=> h
      def List.mapAccumRM {α : Type u} {β' : Type v} {γ' : Type v} {m' : Type v → Type w} [Monad m'] (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).

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

        Equations
        Instances For
          theorem joinM_map_map {m : Type u → Type u} [Monad m] [LawfulMonad m] {α : Type u} {β : Type u} (f : α → β) (a : m (m α)) :
          theorem joinM_map_joinM {m : Type u → Type u} [Monad m] [LawfulMonad m] {α : Type u} (a : m (m (m α))) :
          joinM (joinM <$> a) = joinM (joinM a)
          @[simp]
          theorem joinM_map_pure {m : Type u → Type u} [Monad m] [LawfulMonad m] {α : Type u} (a : m α) :
          joinM (pure <$> a) = a
          @[simp]
          theorem joinM_pure {m : Type u → Type u} [Monad m] [LawfulMonad m] {α : Type u} (a : m α) :
          joinM (pure a) = a
          def succeeds {F : Type → Type v} [Alternative F] {α : Type} (x : F α) :

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

          Equations
          Instances For
            def tryM {F : Type → Type v} [Alternative F] {α : Type} (x : F α) :

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

            Equations
            Instances For
              def try? {F : Type → Type v} [Alternative F] {α : Type} (x : F α) :
              F (Option α)

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

              Equations
              Instances For
                @[simp]
                theorem guard_true {F : Type → Type v} [Alternative F] {h : Decidable True} :
                @[simp]
                theorem guard_false {F : Type → Type v} [Alternative F] {h : Decidable False} :
                guard False = failure
                def Sum.bind {e : Type v} {α : Type u_1} {β : Type u_2} :
                e ⊕ α → (α → e ⊕ β) → e ⊕ β

                The monadic bind operation for Sum.

                Equations
                Instances For
                  instance Sum.instMonadSum {e : Type v} :
                  Equations
                  • Sum.instMonadSum = Monad.mk
                  class CommApplicative (m : Type u → Type v) [Applicative m] extends LawfulApplicative :
                  • map_const : ∀ {α β : Type u}, Functor.mapConst = Functor.map ∘ Function.const β
                  • id_map : ∀ {α : Type u} (x : m α), id <$> x = x
                  • comp_map : ∀ {α β γ : Type u} (g : α → β) (h : β → γ) (x : m α), (h ∘ g) <$> x = h <$> g <$> x
                  • seqLeft_eq : ∀ {α β : Type u} (x : m α) (y : m β), (SeqLeft.seqLeft x fun x => y) = Seq.seq (Function.const β <$> x) fun x => y
                  • seqRight_eq : ∀ {α β : Type u} (x : m α) (y : m β), (SeqRight.seqRight x fun x => y) = Seq.seq (Function.const α id <$> x) fun x => y
                  • pure_seq : ∀ {α β : Type u} (g : α → β) (x : m α), (Seq.seq (pure g) fun x_1 => x) = g <$> x
                  • map_pure : ∀ {α β : Type u} (g : α → β) (x : α), g <$> pure x = pure (g x)
                  • seq_pure : ∀ {α β : Type u} (g : m (α → β)) (x : α), (Seq.seq g fun x_1 => pure x) = (fun h => h x) <$> g
                  • seq_assoc : ∀ {α β γ : Type u} (x : m α) (g : m (α → β)) (h : m (β → γ)), (Seq.seq h fun x_1 => Seq.seq g fun x_2 => x) = Seq.seq (Seq.seq (Function.comp <$> h) fun x => g) fun x_1 => x
                  • commutative_prod : ∀ {α β : Type u} (a : m α) (b : m β), (Seq.seq (Prod.mk <$> a) fun x => b) = Seq.seq ((fun b a => (a, b)) <$> b) fun x => a

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

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

                  Instances
                    theorem CommApplicative.commutative_map {m : Type u → Type v} [h : Applicative m] [CommApplicative m] {α : Type u} {β : Type u} {γ : Type u} (a : m α) (b : m β) {f : α → β → γ} :
                    (Seq.seq (f <$> a) fun x => b) = Seq.seq (flip f <$> b) fun x => a