Documentation

CslibFrontier.Algorithms.Lean.EditDistance.Basic

Edit distance: the Wagner–Fischer algorithm #

The edit distance between two words under general letter costs, computed by the dynamic program of Wagner and Fischer (see also CLRS, Problem 15-5 and Kleinberg and Tardos, Section 6.6).

A cost function C : Costs α charges C.del a to delete a letter a, C.ins b to insert a letter b, and C.sub a b to replace a by b (keeping a letter is the replacement sub a a, of cost C.sub a a, usually 0). An edit script is a list of such operations; read left to right, it consumes its source word and produces its target word. A script is the same thing as an alignment of the two words, or a trace in the terminology of Wagner and Fischer: every letter of the source is deleted or replaced exactly once, and every letter of the target is inserted or is the result of exactly one replacement, in order.

Cslib.Algorithms.Lean.EditDistance.editDist C x y is the Wagner–Fischer table entry for x and y, and Cslib.Algorithms.Lean.EditDistance.isLeast_editDist proves that it is the least cost of a script from x to y. Cslib.Algorithms.Lean.EditDistance.optScript reads an optimal script off the table.

Scope #

Costs are natural numbers and arbitrary: no triangle inequality or symmetry is assumed. The theorem is about scripts (alignments, traces). Wagner and Fischer also consider sequences of edit operations applied one after another at arbitrary positions, and show that under their cost conditions the least cost of such a sequence equals the least cost of a trace. CslibFrontier.Algorithms.Lean.EditDistance.Unit proves this equivalence for unit costs and derives the Levenshtein metric laws. The weighted sequence equivalence remains open.

Mathlib used to have Levenshtein.Cost and levenshtein (Mathlib/Data/List/EditDistance), with the same three cost fields over separate source and target alphabets and the same recurrence, but no characterization as a least cost over scripts. That API was removed from Mathlib in leanprover-community/mathlib4#29196 and is absent from the pinned Mathlib, so nothing here bridges to it.

Main definitions #

Main results #

Word-RAM running-time bounds for Wagner–Fischer edit distance are proved in CslibFrontier.Algorithms.WordRAM.EditDistance.

References #

Letter costs for edit distance: del a deletes a, ins b inserts b, and sub a b replaces a by b.

  • del : α → ℕ

    The cost of deleting a letter.

  • ins : α → ℕ

    The cost of inserting a letter.

  • sub : α → α → ℕ

    The cost of replacing the first letter by the second.

Unit costs: every deletion, insertion and replacement by a different letter costs 1, and keeping a letter is free. The resulting distance is the Levenshtein distance.

Insertion and deletion costs: 1 per insertion or deletion, and 2 for replacing a letter by a different one, the cost of a deletion followed by an insertion; keeping a letter is free. Replacements are not forbidden, and optScript Costs.indel may return them, but splitting each into a deletion and an insertion keeps the cost, so the distance is the insertion–deletion distance (Cslib.Algorithms.Lean.EditDistance.isLeast_editDist_indel).

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.Costs.unit_sub {α : Type u_1} [DecidableEq α] (a b : α) :
unit.sub a b = if a = b then 0 else 1
@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.Costs.indel_sub {α : Type u_1} [DecidableEq α] (a b : α) :
indel.sub a b = if a = b then 0 else 2

An edit operation: delete a letter, insert a letter, or replace a letter by a letter.

  • del {α : Type u_2} (a : α) : EditOp α

    Delete the letter.

  • ins {α : Type u_2} (b : α) : EditOp α

    Insert the letter.

  • sub {α : Type u_2} (a b : α) : EditOp α

    Replace the first letter by the second.

Instances For
    def Cslib.Algorithms.Lean.EditDistance.instDecidableEqEditOp.decEq {α✝ : Type u_2} [DecidableEq α✝] (x✝ x✝¹ : EditOp α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    @[instance_reducible]

    The letters an operation consumes from the source word.

    The letters an operation produces in the target word.

    The cost of an operation.

    @[reducible, inline]

    An edit script: a list of edit operations, read left to right.

    The source word of a script: the letters it consumes, in order.

    The target word of a script: the letters it produces, in order.

    The cost of a script: the sum of the costs of its operations.

    @[simp]
    @[simp]
    @[simp]
    theorem Cslib.Algorithms.Lean.EditDistance.EditScript.cost_cons {α : Type u_1} (C : Costs α) (o : EditOp α) (s : EditScript α) :
    cost C (o :: s) = EditOp.cost C o + cost C s

    The Wagner–Fischer table, over pairs of suffixes.

    def Cslib.Algorithms.Lean.EditDistance.editDist {α : Type u_1} (C : Costs α) (x y : List α) :

    The edit distance from x to y under the costs C: the Wagner–Fischer table entry.

    theorem Cslib.Algorithms.Lean.EditDistance.editDist_cons_nil {α : Type u_1} (C : Costs α) (a : α) (x : List α) :
    editDist C (a :: x) [] = C.del a + editDist C x []
    theorem Cslib.Algorithms.Lean.EditDistance.editDist_nil_cons {α : Type u_1} (C : Costs α) (b : α) (y : List α) :
    editDist C [] (b :: y) = C.ins b + editDist C [] y
    theorem Cslib.Algorithms.Lean.EditDistance.editDist_cons_cons {α : Type u_1} (C : Costs α) (a b : α) (x y : List α) :
    editDist C (a :: x) (b :: y) = min (C.sub a b + editDist C x y) (min (C.del a + editDist C x (b :: y)) (C.ins b + editDist C (a :: x) y))

    The Wagner–Fischer recurrence.

    Deleting every letter of x.

    Inserting every letter of y.

    theorem Cslib.Algorithms.Lean.EditDistance.editDist_cons_left_le {α : Type u_1} (C : Costs α) (a : α) (x y : List α) :
    editDist C (a :: x) y ≤ C.del a + editDist C x y
    theorem Cslib.Algorithms.Lean.EditDistance.editDist_cons_right_le {α : Type u_1} (C : Costs α) (b : α) (x y : List α) :
    editDist C x (b :: y) ≤ C.ins b + editDist C x y
    theorem Cslib.Algorithms.Lean.EditDistance.editDist_cons_cons_le {α : Type u_1} (C : Costs α) (a b : α) (x y : List α) :
    editDist C (a :: x) (b :: y) ≤ C.sub a b + editDist C x y

    Deleting every letter of x and inserting every letter of y.

    Under unit costs, the edit distance is at most |x| + |y|.

    Every script from x to y costs at least editDist C x y.

    def Cslib.Algorithms.Lean.EditDistance.optScriptStep {α : Type u_1} (C : Costs α) (a b : α) (dx ux lx : ℕ) (d u l : EditScript α) :

    The traceback for a nonempty source and target: given the optimal scripts d, u, l for (x, y), (x, b :: y) and (a :: x, y), extend the cheapest of them. Ties prefer a replacement, then a deletion.

    An optimal script from x to y, read off the Wagner–Fischer table. This is a specification for proofs and decide: each choice consults the entries editDist C x' y' directly instead of a stored table, so it is not the linear-time traceback, which is not formalized.

    def Cslib.Algorithms.Lean.EditDistance.optScript.optScriptAux {α : Type u_1} (C : Costs α) (a : α) (x : List α) (f : List α → EditScript α) :
    List α → EditScript α

    The scripts from a :: x, given the scripts from x.

    theorem Cslib.Algorithms.Lean.EditDistance.optScript_cons_cons {α : Type u_1} (C : Costs α) (a b : α) (x y : List α) :
    optScript C (a :: x) (b :: y) = optScriptStep C a b (editDist C x y) (editDist C x (b :: y)) (editDist C (a :: x) y) (optScript C x y) (optScript C x (b :: y)) (optScript C (a :: x) y)
    theorem Cslib.Algorithms.Lean.EditDistance.optScript_spec {α : Type u_1} (C : Costs α) (x y : List α) :
    (optScript C x y).src = x ∧ (optScript C x y).tgt = y ∧ EditScript.cost C (optScript C x y) = editDist C x y

    optScript C x y is a script from x to y of cost editDist C x y.

    @[simp]
    theorem Cslib.Algorithms.Lean.EditDistance.src_optScript {α : Type u_1} (C : Costs α) (x y : List α) :
    (optScript C x y).src = x
    @[simp]
    theorem Cslib.Algorithms.Lean.EditDistance.tgt_optScript {α : Type u_1} (C : Costs α) (x y : List α) :
    (optScript C x y).tgt = y
    theorem Cslib.Algorithms.Lean.EditDistance.isLeast_editDist {α : Type u_1} (C : Costs α) (x y : List α) :
    IsLeast {c : ℕ | ∃ (s : EditScript α), s.src = x ∧ s.tgt = y ∧ EditScript.cost C s = c} (editDist C x y)

    Wagner–Fischer. The table entry editDist C x y is the least cost of an edit script (an alignment, or trace) from x to y.

    theorem Cslib.Algorithms.Lean.EditDistance.editDist_append_le {α : Type u_1} (C : Costs α) (x₁ x₂ y₁ y₂ : List α) :
    editDist C (x₁ ++ x₂) (y₁ ++ y₂) ≤ editDist C x₁ y₁ + editDist C x₂ y₂

    Concatenating two optimal scripts bounds the cost of the whole alignment.

    theorem Cslib.Algorithms.Lean.EditDistance.EditScript.exists_split_src {α : Type u_1} (C : Costs α) (s : EditScript α) {u v : List α} (hsrc : s.src = u ++ v) :
    ∃ (s₁ : EditScript α) (s₂ : EditScript α), s₁ ++ s₂ = s ∧ s₁.src = u ∧ s₂.src = v ∧ s₁.tgt ++ s₂.tgt = s.tgt ∧ cost C s₁ + cost C s₂ = cost C s

    Splitting an edit script at a chosen source boundary s.src = u ++ v produces two scripts whose sources are u and v, whose targets concatenate to s.tgt, and whose costs sum to s.cost C.