Documentation

CslibFrontier.Algorithms.Lean.EditDistance.HammingEmbedding

Exact Isometric Embedding of Hamming Space into Edit-Distance Space #

This module constructs an explicit zero-distortion isometric embedding of any fixed-length Hamming cube (Σ^d, Δ) into the unit-cost Levenshtein edit-distance space (Σ^{d(d+1)}, ED), using the unit-cost metric axioms imported from CslibFrontier.Algorithms.Lean.EditDistance.Unit.

Given any alphabet α with a distinguished separator symbol sep : α, padding each symbol of a length-d word x with d trailing copies of sep produces a word of length d * (d + 1) whose active positions i * (d + 1) are spaced d + 1 apart. Any edit script of cost at most d shifts coordinates by at most d positions, preventing any non-separator symbol at active position i * (d + 1) from matching a different active position j * (d + 1). Every mismatching coordinate x_i ≠ y_i therefore incurs its own distinct unit-cost operation, yielding the exact identity ED(f(x), f(y)) = Δ(x, y).

Main definitions #

Main results #

Common-prefix mismatch count between two lists: counts positions i < min x.length y.length where x[i] ≠ y[i], ignoring any unmatched suffix (so e.g. listHammingDist [] [true] = 0). Agrees with standard Hamming distance on equal-length lists, which is the domain of the isometry theorems below.

@[simp]

Hamming distance with the empty list on the left is zero.

@[simp]

Hamming distance with the empty list on the right is zero.

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.listHammingDist_cons_cons {α : Type u_1} [DecidableEq α] (a b : α) (xs ys : List α) :
listHammingDist (a :: xs) (b :: ys) = (if a = b then 0 else 1) + listHammingDist xs ys

Single-step recurrence for listHammingDist on non-empty lists.

listHammingDist on List.ofFn agrees identically with Mathlib's hammingDist.

theorem Cslib.Algorithms.Lean.EditDistance.listHammingDist_eq_card_filter {α : Type u_1} [DecidableEq α] {d : ℕ} {x y : List α} (hx : x.length = d) (hy : y.length = d) :
listHammingDist x y = {i : Fin d | x[↑i] ≠ y[↑i]}.card

Equal-length lists have listHammingDist equal to the cardinality of mismatching indices.

Unit-cost edit distance is always at most listHammingDist on equal-length lists.

listHammingDist on equal-length lists vanishes iff the lists are equal.

def Cslib.Algorithms.Lean.EditDistance.padEditEmbed {α : Type u_1} (sep : α) (x : List α) :
List α

Separator-padded embedding of a length-d word x : List α into a word of length d * (d + 1), inserting d copies of sep after each character of x.

def Cslib.Algorithms.Lean.EditDistance.padEditEmbedFin {α : Type u_1} (sep : α) {d : ℕ} (x : Fin d → α) :
List α

Typed Fin d → α version of padEditEmbed.

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.length_padEditEmbed {α : Type u_1} (sep : α) (x : List α) :
(padEditEmbed sep x).length = x.length * (x.length + 1)

The padded word padEditEmbed sep x has length x.length * (x.length + 1).

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.length_padEditEmbedFin {α : Type u_1} (sep : α) {d : ℕ} (x : Fin d → α) :
(padEditEmbedFin sep x).length = d * (d + 1)

The padded word padEditEmbedFin sep x has length d * (d + 1).

theorem Cslib.Algorithms.Lean.EditDistance.getElem_padEditEmbed_mul {α : Type u_1} (sep : α) (x : List α) {i : ℕ} (hi : i < x.length) :
(padEditEmbed sep x)[i * (x.length + 1)] = x[i]

At active index i * (x.length + 1), padEditEmbed sep x recovers x[i].

theorem Cslib.Algorithms.Lean.EditDistance.getElem_padEditEmbed_of_mod_ne_zero {α : Type u_1} (sep : α) (x : List α) {p : ℕ} (hp : p < x.length * (x.length + 1)) (hm : p % (x.length + 1) ≠ 0) :
(padEditEmbed sep x)[p] = sep

Off the active multiples of x.length + 1, padEditEmbed sep x equals sep.

padEditEmbed sep is injective on all lists over α.

Exact isometry theorem on lists: padding each character of equal-length words x, y with x.length copies of any separator symbol sep makes their unit-cost Levenshtein edit distance identically equal to their Hamming distance.

Exact isometry theorem on Fin d → α: padEditEmbedFin sep embeds (Fin d → α, hammingDist) isometrically into (List α, editDist Costs.unit) with output length d * (d + 1).

The binary separator-padded isometric embedding padEditEmbed false : List Bool → List Bool is computable in polynomial time on a deterministic multi-tape Turing machine.