Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Corollary 1 (of Theorem 2) — the recurrence in terms of steps

Proved
MasekPaterson.FourRussians.step_recurrence

by mikedeng1 · Sep 26, 2026 · Mathlib 0df444a (Lean v4.33.1)

dynamic-programmingedit-distancep2o-batch-p100ap2o-gran-per-chapterp2o-plan-paperp2o-v1

Let γ\gammaγ be a nonnegative, normalized cost function and A,BA, BA,B strings with edit matrix δi,j\delta_{i,j}δi,j​. For all i,ji, ji,j with 1≤i≤∣A∣1 \le i \le |A|1≤i≤∣A∣ and 1≤j≤∣B∣1 \le j \le |B|1≤j≤∣B∣, the vertical step δi,j−δi−1,j\delta_{i,j} - \delta_{i-1,j}δi,j​−δi−1,j​ and the horizontal step δi,j−δi,j−1\delta_{i,j} - \delta_{i,j-1}δi,j​−δi,j−1​ satisfy

δi,j−δi−1,j=min⁡{RAi,Bj−(δi−1,j−δi−1,j−1), DAi, IBj+(δi,j−1−δi−1,j−1)−(δi−1,j−δi−1,j−1)},\delta_{i,j} - \delta_{i-1,j} = \min\{R_{A_i,B_j} - (\delta_{i-1,j} - \delta_{i-1,j-1}),\ D_{A_i},\ I_{B_j} + (\delta_{i,j-1} - \delta_{i-1,j-1}) - (\delta_{i-1,j} - \delta_{i-1,j-1})\},δi,j​−δi−1,j​=min{RAi​,Bj​​−(δi−1,j​−δi−1,j−1​), DAi​​, IBj​​+(δi,j−1​−δi−1,j−1​)−(δi−1,j​−δi−1,j−1​)}, δi,j−δi,j−1=min⁡{RAi,Bj−(δi,j−1−δi−1,j−1), DAi+(δi−1,j−δi−1,j−1)−(δi,j−1−δi−1,j−1), IBj}.\delta_{i,j} - \delta_{i,j-1} = \min\{R_{A_i,B_j} - (\delta_{i,j-1} - \delta_{i-1,j-1}),\ D_{A_i} + (\delta_{i-1,j} - \delta_{i-1,j-1}) - (\delta_{i,j-1} - \delta_{i-1,j-1}),\ I_{B_j}\}.δi,j​−δi,j−1​=min{RAi​,Bj​​−(δi,j−1​−δi−1,j−1​), DAi​​+(δi−1,j​−δi−1,j−1​)−(δi,j−1​−δi−1,j−1​), IBj​​}.

The steps leaving a cell are determined by the steps entering it and the two characters Ai,BjA_i, B_jAi​,Bj​, without reference to the absolute values of δ\deltaδ. Algorithm Y is exactly this recurrence.

Formalization Note The corollary prints no range for i,ji, ji,j; it inherits Theorem 2's range 1≤i≤∣A∣1 \le i \le |A|1≤i≤∣A∣, 1≤j≤∣B∣1 \le j \le |B|1≤j≤∣B∣ and its hypotheses (nonnegative, normalized γ\gammaγ).

Preamble
import Mathlib
import Definitions.Def_MasekPaterson_Shared_editDist
open MasekPaterson.Shared
Formal statement
namespace MasekPaterson.FourRussians

/-- Corollary 1 (of Theorem 2): the Wagner–Fischer recurrence in terms of steps. For
`1 ≤ i ≤ |A|`, `1 ≤ j ≤ |B|` and a nonnegative normalized cost function,
`δ_{i,j} − δ_{i−1,j} = min{R_{A_i,B_j} − (δ_{i−1,j} − δ_{i−1,j−1}), D_{A_i},
  I_{B_j} + (δ_{i,j−1} − δ_{i−1,j−1}) − (δ_{i−1,j} − δ_{i−1,j−1})}` and
`δ_{i,j} − δ_{i,j−1} = min{R_{A_i,B_j} − (δ_{i,j−1} − δ_{i−1,j−1}),
  D_{A_i} + (δ_{i−1,j} − δ_{i−1,j−1}) − (δ_{i,j−1} − δ_{i−1,j−1}), I_{B_j}}`. -/
theorem step_recurrence {α : Type*} (γ : EditOp α → ℝ) (hγ : ∀ o, 0 ≤ γ o)
    (hN : IsNormalized γ) (A B : List α) (i j : ℕ)
    (hi : 1 ≤ i) (hiA : i ≤ A.length) (hj : 1 ≤ j) (hjB : j ≤ B.length) :
    dmat γ A B i j - dmat γ A B (i - 1) j =
      min (min (replCost γ A[i - 1] B[j - 1]
                  - (dmat γ A B (i - 1) j - dmat γ A B (i - 1) (j - 1)))
               (delCost γ A[i - 1]))
          (insCost γ B[j - 1] + (dmat γ A B i (j - 1) - dmat γ A B (i - 1) (j - 1))
                  - (dmat γ A B (i - 1) j - dmat γ A B (i - 1) (j - 1))) ∧
    dmat γ A B i j - dmat γ A B i (j - 1) =
      min (min (replCost γ A[i - 1] B[j - 1]
                  - (dmat γ A B i (j - 1) - dmat γ A B (i - 1) (j - 1)))
               (delCost γ A[i - 1] + (dmat γ A B (i - 1) j - dmat γ A B (i - 1) (j - 1))
                  - (dmat γ A B i (j - 1) - dmat γ A B (i - 1) (j - 1))))
          (insCost γ B[j - 1]) := by sorry

end MasekPaterson.FourRussians
Source
Masek, Paterson, A Faster Algorithm Computing String Edit Distances, J. Comput. System Sci. 20, 1980, p. 21, Corollary 1 (of Theorem 2)
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Let α\alphaα be any type and γ\gammaγ a real-valued cost function on edit operations over α\alphaα (a type defined in the imported shared file). There are two assumptions:

  • γ(o)≥0\gamma(o)\ge0γ(o)≥0 for all ooo;
  • γ\gammaγ satisfies the imported, unshown predicate IsNormalized.

Let A,BA,BA,B be finite lists over α\alphaα with 1-based entries. Write δi,j\delta_{i,j}δi,j​ for the imported dmat γ A B i j, which the docstring calls the edit matrix. Write rep\mathrm{rep}rep, del\mathrm{del}del and ins\mathrm{ins}ins for the imported replacement, deletion and insertion costs.

Fix natural numbers i,ji,ji,j with 1≤i≤∣A∣1\le i\le|A|1≤i≤∣A∣ and 1≤j≤∣B∣1\le j\le|B|1≤j≤∣B∣, and set

p=δi−1,j−δi−1,j−1,q=δi,j−1−δi−1,j−1.p=\delta_{i-1,j}-\delta_{i-1,j-1},\qquad q=\delta_{i,j-1}-\delta_{i-1,j-1}.p=δi−1,j​−δi−1,j−1​,q=δi,j−1​−δi−1,j−1​.

The statement asserts that both of the following equalities hold:

δi,j−δi−1,j=min⁡{rep(Ai,Bj)−p, del(Ai), ins(Bj)+q−p},\delta_{i,j}-\delta_{i-1,j}=\min\bigl\{\mathrm{rep}(A_i,B_j)-p,\ \mathrm{del}(A_i),\ \mathrm{ins}(B_j)+q-p\bigr\},δi,j​−δi−1,j​=min{rep(Ai​,Bj​)−p, del(Ai​), ins(Bj​)+q−p}, δi,j−δi,j−1=min⁡{rep(Ai,Bj)−q, del(Ai)+p−q, ins(Bj)}.\delta_{i,j}-\delta_{i,j-1}=\min\bigl\{\mathrm{rep}(A_i,B_j)-q,\ \mathrm{del}(A_i)+p-q,\ \mathrm{ins}(B_j)\bigr\}.δi,j​−δi,j−1​=min{rep(Ai​,Bj​)−q, del(Ai​)+p−q, ins(Bj​)}.

All subtractions are subtractions of real numbers. The index subtractions i−1i-1i−1 and j−1j-1j−1 are exact because i,j≥1i,j\ge1i,j≥1.

In the degenerate cases:

  • If AAA or BBB is empty (including whenever α\alphaα is empty), the hypotheses on i,ji,ji,j cannot be met and the statement is vacuous.
  • It would also be vacuous if IsNormalized were incompatible with nonnegativity. This is not determinable from the code shown.
Human review
  • Endorsed by Shuze Chen · Sep 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by mikedeng1 · Sep 27, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me