Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Theorem 2 [7] — the Wagner–Fischer recurrence for δi,j\delta_{i,j}δi,j​

Proved
MasekPaterson.FourRussians.wagner_fischer_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=δ(γ,Ai,Bj)\delta_{i,j} = \delta(\gamma, A^i, B^j)δi,j​=δ(γ,Ai,Bj). 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∣,

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

Each internal entry of the edit matrix is thus determined by its three neighbours above and to the left; this is Wagner and Fischer's matrix-filling algorithm [7], and it is the basis of every step of the Masek–Paterson algorithm.

Formalization Note δ\deltaδ is the minimum over all edit sequences (Section 1.1), not the recurrence, so this is a genuine theorem. Normalization is needed: with Ra,c=10R_{a,c} = 10Ra,c​=10, Ra,b=Rb,c=1R_{a,b} = R_{b,c} = 1Ra,b​=Rb,c​=1 and all insertions and deletions costing 100100100, δ(γ,a,c)=2\delta(\gamma, a, c) = 2δ(γ,a,c)=2 while the right-hand side is 101010. Characters are 1-based as in the paper (AiA_iAi​ is A[i-1]).

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

/-- Theorem 2 [Wagner–Fischer]: for `1 ≤ i ≤ |A|`, `1 ≤ j ≤ |B|`,
`δ_{i,j} = min(δ_{i−1,j−1} + R_{A_i,B_j}, δ_{i−1,j} + D_{A_i}, δ_{i,j−1} + I_{B_j})`,
for a nonnegative normalized cost function. `A_i` (1-based) is `A[i-1]`. -/
theorem wagner_fischer_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 =
      min (min (dmat γ A B (i - 1) (j - 1) + replCost γ A[i - 1] B[j - 1])
               (dmat γ A B (i - 1) j + delCost γ A[i - 1]))
          (dmat γ A B i (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. 20, Theorem 2 [7]
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 predicate IsNormalized, whose content is not shown.

Let A,BA,BA,B be finite lists over α\alphaα with 1-based entries ArA_rAr​ and BrB_rBr​. Write δi,j\delta_{i,j}δi,j​ for the imported quantity dmat γ A B i j, which the docstring calls the edit matrix and whose definition is not reproduced here. Write rep(a,b)\mathrm{rep}(a,b)rep(a,b), del(a)\mathrm{del}(a)del(a) and ins(b)\mathrm{ins}(b)ins(b) for the imported replacement, deletion and insertion costs under γ\gammaγ.

For all 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∣, the statement asserts the exact equality

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

Because i,j≥1i,j\ge1i,j≥1, the indices i−1i-1i−1 and j−1j-1j−1 are ordinary predecessors; natural-number truncation at 000 never occurs. The index i−1i-1i−1 or j−1j-1j−1 may equal 000, so boundary entries of δ\deltaδ can appear on the right.

In the degenerate cases:

  • If AAA or BBB is empty (in particular if α\alphaα is empty), no (i,j)(i,j)(i,j) satisfies the hypotheses and the statement is vacuous.
  • If IsNormalized were unsatisfiable together with nonnegativity, it would be vacuous too. This cannot be judged 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