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 / V34. The tactics of Mathlib

Mathlib.Tactic.ComputeAsymptotics.Multiseries.Corecursion

45 declarations. Mathlib docs · Source

Non-primitive corecursion for sequences

Primitive corecursive definition of the form

def foo (x : X) := hd x :: foo (tlArg x)

(where hd and tlArg are arbitrary functions) can be encoded via the corecursor Seq.corec.

It is not enough, however, to define multiplication and powser operation for multiseries.

This file implements a more general form of corecursion in the spirit of [blanchette2015]. This is a bare minimum that needed for the tactic, it justifies a weaker class of corecursive definitions than [blanchette2015] does, and only works for Seq.

A function f : Seq α → Seq α is called friendly if for all n : ℕ the n-prefix of its result f s depends only on the n-prefix of its input s.

In this file we develop a theory that justifies corecursive definitions of the form

def foo (x : X) := hd x :: f (foo (tlArg x))

where f is friendly.

Main definitions

  • FriendlyOperation f means that f is friendly.
  • FriendlyOperationClass is a typeclass meaning that some indexed family of operations are friendly.
  • gcorec: a generalization of Seq.corec that allows a corecursive call to be guarded by a friendly function.
  • FriendlyOperation.coind, FriendlyOperation.coind_comp_friend_left, FriendlyOperation.coind_comp_friend_right: coinduction principles for proving that an operation is friendly.
  • FriendlyOperation.eq_of_bisim: a generalisation of Seq.eq_of_bisim' that allows using a friendly operation in the tail of the sequences.

Implementation notes

To prove that the definition of the form

def foo (x : X) := hd x :: f (foo (tlArg x))

is correct we prove that there exists a function satisfying this equation. For that we employ a Banach fixed point theorem. We treat Seq α as a metric space here with the metric d(s, t) := 2 ^ (-n) where n is the minimal index where s and t differ.

Then f is friendly iff it is 1-Lipschitz.

theorem Tactic.ComputeAsymptotics.Seq.gcorec.congr_simp E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} (F F_1 : β → Option (α × γ × β)),
  F = F_1 →
    ∀ (op op_1 : γ → Stream'.Seq α → Stream'.Seq α) (e_op : op = op_1)
      [inst : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass op] (a a_1 : β),
      a = a_1 → Tactic.ComputeAsymptotics.Seq.gcorec F op a = Tactic.ComputeAsymptotics.Seq.gcorec F_1 op_1 a_1

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.unfold.congr_simp E1 #

∀ {α : Type u_1} {op op_1 : Stream'.Seq α → Stream'.Seq α} (e_op : op = op_1)
  (h : Tactic.ComputeAsymptotics.Seq.FriendlyOperation op) (hd? hd?_1 : Option α),
  hd? = hd?_1 → h.unfold hd? = ⋯.unfold hd?_1

def Tactic.ComputeAsymptotics.Seq.instMetricSpaceStream' E1 #

{α : Type u_1} → MetricSpace (Stream' α)

Metric space structure on Stream' α considering α as a discrete metric space.

def Tactic.ComputeAsymptotics.Seq.instMetricSpaceSeq E1 #

{α : Type u_1} → MetricSpace (Stream'.Seq α)

Metric space structure on Seq α considering α as a discrete metric space.

theorem Tactic.ComputeAsymptotics.Seq.instCompleteSpaceStream' E1 #

∀ {α : Type u_1}, CompleteSpace (Stream' α)

theorem Tactic.ComputeAsymptotics.Seq.instCompleteSpaceSeq E1 #

∀ {α : Type u_1}, CompleteSpace (Stream'.Seq α)

theorem Tactic.ComputeAsymptotics.Seq.Stream'.dist_le_one E1 #

∀ {α : Type u_1} (s t : Stream' α), dist s t ≤ 1

theorem Tactic.ComputeAsymptotics.Seq.dist_le_one E0 #

∀ {α : Type u_1} (s t : Stream'.Seq α), dist s t ≤ 1

theorem Tactic.ComputeAsymptotics.Seq.instBoundedSpaceStream' E1 #

∀ {α : Type u_1}, BoundedSpace (Stream' α)

theorem Tactic.ComputeAsymptotics.Seq.instBoundedSpaceSeq E1 #

∀ {α : Type u_1}, BoundedSpace (Stream'.Seq α)

theorem Tactic.ComputeAsymptotics.Seq.dist_eq_two_inv_pow E1 #

∀ {α : Type u_1} {s t : Stream'.Seq α}, s ≠ t → ∃ n, dist s t = 2⁻¹ ^ n

theorem Tactic.ComputeAsymptotics.Seq.dist_cons_cons E0 #

∀ {α : Type u_1} (x : α) (s t : Stream'.Seq α), dist (Stream'.Seq.cons x s) (Stream'.Seq.cons x t) = 2⁻¹ * dist s t

theorem Tactic.ComputeAsymptotics.Seq.dist_eq_half_of_head E1 #

∀ {α : Type u_1} {s t : Stream'.Seq α}, s.head = t.head → dist s t = 2⁻¹ * dist s.tail t.tail

theorem Tactic.ComputeAsymptotics.Seq.dist_eq_one_of_head E1 #

∀ {α : Type u_1} {s t : Stream'.Seq α}, s.head ≠ t.head → dist s t = 1

theorem Tactic.ComputeAsymptotics.Seq.dist_cons_cons_eq_one E1 #

∀ {α : Type u_1} {x y : α} {s t : Stream'.Seq α}, x ≠ y → dist (Stream'.Seq.cons x s) (Stream'.Seq.cons y t) = 1

theorem Tactic.ComputeAsymptotics.Seq.dist_cons_nil E0 #

∀ {α : Type u_1} (x : α) (s : Stream'.Seq α), dist (Stream'.Seq.cons x s) Stream'.Seq.nil = 1

theorem Tactic.ComputeAsymptotics.Seq.dist_nil_cons E0 #

∀ {α : Type u_1} (x : α) (s : Stream'.Seq α), dist Stream'.Seq.nil (Stream'.Seq.cons x s) = 1

theorem Tactic.ComputeAsymptotics.Seq.dist_le_half_iff E0 #

∀ {α : Type u_1} {s t : Stream'.Seq α},
  dist s t ≤ 2⁻¹ ↔
    s = Stream'.Seq.nil ∧ t = Stream'.Seq.nil ∨ ∃ hd s' t', s = Stream'.Seq.cons hd s' ∧ t = Stream'.Seq.cons hd t'

def Tactic.ComputeAsymptotics.Seq.FriendlyOperation E1 #

{α : Type u_1} → (Stream'.Seq α → Stream'.Seq α) → Prop

A function on sequences is called a "friend" if any n-prefix of its output depends only on the n-prefix of the input. Such functions can be used in the tail of (non-primitive) corecursive definitions.

class Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass E3 #

{α : Type u_1} → {γ : Type u_3} → (γ → Stream'.Seq α → Stream'.Seq α) → Prop

A family of friendly operations on sequences indexed by a type γ.

ctor Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass.mk E0 #

∀ {α : Type u_1} {γ : Type u_3} {F : γ → Stream'.Seq α → Stream'.Seq α},
  (∀ (c : γ), Tactic.ComputeAsymptotics.Seq.FriendlyOperation (F c)) →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass F

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass.friend E1 #

∀ {α : Type u_1} {γ : Type u_3} {F : γ → Stream'.Seq α → Stream'.Seq α}
  [self : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass F] (c : γ),
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation (F c)

theorem Tactic.ComputeAsymptotics.Seq.friendlyOperation_iff_dist_le_dist E1 #

∀ {α : Type u_1} (op : Stream'.Seq α → Stream'.Seq α),
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op ↔ ∀ (s t : Stream'.Seq α), dist (op s) (op t) ≤ dist s t

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.id E1 #

∀ {α : Type u_1}, Tactic.ComputeAsymptotics.Seq.FriendlyOperation id

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.comp E1 #

∀ {α : Type u_1} {op op' : Stream'.Seq α → Stream'.Seq α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperation op' → Tactic.ComputeAsymptotics.Seq.FriendlyOperation (op ∘ op')

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.const E1 #

∀ {α : Type u_1} {s : Stream'.Seq α}, Tactic.ComputeAsymptotics.Seq.FriendlyOperation fun x ↦ s

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass.comp E1 #

∀ {α : Type u_1} {γ : Type u_3} {γ' : Type u_4} (F : γ → Stream'.Seq α → Stream'.Seq α) (g : γ' → γ)
  [h : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass F],
  Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass fun c ↦ F (g c)

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.ite E1 #

∀ {α : Type u_1} {op₁ op₂ : Stream'.Seq α → Stream'.Seq α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op₁ →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperation op₂ →
      ∀ {P : Option α → Prop} [inst : DecidablePred P],
        Tactic.ComputeAsymptotics.Seq.FriendlyOperation fun s ↦ if P s.head then op₁ s else op₂ s

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.dist_le E1 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op → ∀ {s t : Stream'.Seq α}, dist (op s) (op t) ≤ dist s t

theorem Tactic.ComputeAsymptotics.Seq.exists_fixed_point_of_contractible E1 #

∀ {α : Type u_1} {β : Type u_2} (F : UniformFun β (Stream'.Seq α) → UniformFun β (Stream'.Seq α)),
  LipschitzWith 2⁻¹ F → ∃ f, Function.IsFixedPt F f

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.exists_fixed_point E2 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} (F : β → Option (α × γ × β)) (op : γ → Stream'.Seq α → Stream'.Seq α)
  [h : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass op],
  ∃ f,
    ∀ (b : β),
      match F b with
      | none => f b = Stream'.Seq.nil
      | some (a, c, b') => f b = Stream'.Seq.cons a (op c (f b'))

Main theorem of this file. It shows that there exists a function satisfying the corecursive definition of the form def foo (x : X) := hd x :: op (foo (tlArg x)) where f is friendly.

def Tactic.ComputeAsymptotics.Seq.gcorec E3 #

{α : Type u_1} →
  {β : Type u_2} →
    {γ : Type u_3} →
      (β → Option (α × γ × β)) →
        (op : γ → Stream'.Seq α → Stream'.Seq α) →
          [Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass op] → β → Stream'.Seq α

(General) non-primitive corecursor for Seq α that allows using a friendly operation in the tail of the corecursive definition.

theorem Tactic.ComputeAsymptotics.Seq.gcorec_nil E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {F : β → Option (α × γ × β)} {op : γ → Stream'.Seq α → Stream'.Seq α}
  [inst : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass op] {b : β},
  F b = none → Tactic.ComputeAsymptotics.Seq.gcorec F op b = Stream'.Seq.nil

theorem Tactic.ComputeAsymptotics.Seq.gcorec_some E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {F : β → Option (α × γ × β)} {op : γ → Stream'.Seq α → Stream'.Seq α}
  [inst : Tactic.ComputeAsymptotics.Seq.FriendlyOperationClass op] {b : β} {a : α} {c : γ} {b' : β},
  F b = some (a, c, b') →
    Tactic.ComputeAsymptotics.Seq.gcorec F op b =
      Stream'.Seq.cons a (op c (Tactic.ComputeAsymptotics.Seq.gcorec F op b'))

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.cons E1 #

∀ {α : Type u_1} (hd : α), Tactic.ComputeAsymptotics.Seq.FriendlyOperation (Stream'.Seq.cons hd)

The operation cons hd · is friendly.

theorem Tactic.ComputeAsymptotics.Seq.dist_const_tail_cons_tail_le E1 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α} {hd : α} {x y : Stream'.Seq α},
  dist (op (Stream'.Seq.cons hd x)) (op (Stream'.Seq.cons hd y)) ≤
      dist (Stream'.Seq.cons hd x) (Stream'.Seq.cons hd y) →
    dist (op (Stream'.Seq.cons hd x)).tail (op (Stream'.Seq.cons hd y)).tail ≤ dist x y

If two sequences have the same head and applying op reduces their distance, then it also reduces the distance of their tails.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.cons_tail E1 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α} {hd : α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperation fun s ↦ (op (Stream'.Seq.cons hd s)).tail

The operation (op (.cons hd ·)).tail is friendly if op is friendly.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.op_cons_head_eq E0 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op →
    ∀ {a : α} {s t : Stream'.Seq α}, (op (Stream'.Seq.cons a s)).head = (op (Stream'.Seq.cons a t)).head

The first element of op (a :: s) depends only on a.

def Tactic.ComputeAsymptotics.Seq.FriendlyOperation.unfold E1 #

{α : Type u_1} →
  {op : Stream'.Seq α → Stream'.Seq α} →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperation op →
      Option α → Option (α × Subtype Tactic.ComputeAsymptotics.Seq.FriendlyOperation)

Decomposes a friendly operation by the head of the input sequence. Returns none if the output is nil, or some (out_hd, op') where out_hd is the head of the output and op' is a friendly operation mapping the tail of the input to the tail of the output. See destruct_apply_eq_unfold for the correctness statement.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.destruct_apply_eq_unfold E1 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α} (h : Tactic.ComputeAsymptotics.Seq.FriendlyOperation op)
  {s : Stream'.Seq α},
  (op s).destruct =
    Option.map
      (fun x ↦
        match x with
        | (hd, op') => (hd, ↑op' s.tail))
      (h.unfold s.head)

unfold correctly decomposes a friendly operation: the head of op s depends only on the head of s (and is given by unfold), while the tail of op s is obtained by applying the friendly operation returned by unfold to the tail of s. This gives a coinductive characterization of FriendlyOperation. For the coinduction principle, see FriendlyOperation.coind.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.op_head_eq E0 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α},
  Tactic.ComputeAsymptotics.Seq.FriendlyOperation op →
    ∀ {s t : Stream'.Seq α}, s.head = t.head → (op s).head = (op t).head

If op is friendly, then op s and op t have the same head if s and t have the same head.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.of_dist_le_pow E1 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α},
  (∀ (s t : Stream'.Seq α) (n : ℕ), dist s t ≤ 2⁻¹ ^ n → dist (op s) (op t) ≤ 2⁻¹ ^ n) →
    Tactic.ComputeAsymptotics.Seq.FriendlyOperation op

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.coind E3 #

∀ {α : Type u_1} (motive : (Stream'.Seq α → Stream'.Seq α) → Prop) {op : Stream'.Seq α → Stream'.Seq α},
  motive op →
    (∀ (op : Stream'.Seq α → Stream'.Seq α),
        motive op →
          ∃ T,
            ∀ (s : Stream'.Seq α),
              (op s).destruct =
                Option.map
                  (fun x ↦
                    match x with
                    | (hd, op') => (hd, ↑op' s.tail))
                  (T s.head)) →
      Tactic.ComputeAsymptotics.Seq.FriendlyOperation op

Coinduction principle for proving that an operation is friendly.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.coind_comp_friend_left E3 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α} (motive : (Stream'.Seq α → Stream'.Seq α) → Prop),
  motive op →
    (∀ (op : Stream'.Seq α → Stream'.Seq α),
        motive op →
          ∃ T,
            ∀ (s : Stream'.Seq α),
              (op s).destruct =
                Option.map
                  (fun x ↦
                    match x with
                    | (hd, opf, op') => (hd, ↑opf (↑op' s.tail)))
                  (T s.head)) →
      Tactic.ComputeAsymptotics.Seq.FriendlyOperation op

A generalisation of FriendlyOperation.coind which allows using opf ∘ op' in the tail of op s where opf is friendly and op' is a function satisfying motive.

theorem Tactic.ComputeAsymptotics.Seq.FriendlyOperation.coind_comp_friend_right E3 #

∀ {α : Type u_1} {op : Stream'.Seq α → Stream'.Seq α} (motive : (Stream'.Seq α → Stream'.Seq α) → Prop),
  motive op →
    (∀ (op : Stream'.Seq α → Stream'.Seq α),
        motive op →
          ∃ T,
            ∀ (s : Stream'.Seq α),
              (op s).destruct =
                Option.map
                  (fun x ↦
                    match x with
                    | (hd, opf, op') => (hd, ↑op' (↑opf s.tail)))
                  (T s.head)) →
      Tactic.ComputeAsymptotics.Seq.FriendlyOperation op

A generalisation of FriendlyOperation.coind that allows using op' ∘ opf in the tail of op s where opf is friendly and op' is a function satisfying motive.