Documentation

CslibFrontier.Algorithms.Lean.KMP.Search

Knuth–Morris–Pratt Failure Table Construction and Exact String Matching #

This module implements the Knuth–Morris–Pratt ([KnuthMorrisPratt1977], Sections 1–2; [CLRS2009], Section 32.4) failure-table construction (computePrefix) and exact string matcher (kmpSearchWithPrefix, kmpMatches, kmpMatchesList) as pure functions over arbitrary alphabets [DecidableEq α], proving soundness, completeness, and strict output ordering.

Linear O(|p|) and O(|p| + |t|) machine-level running-time bounds on word arrays are proved in CslibFrontier.Algorithms.WordRAM.KMP.

Source Conventions #

Main definitions #

Main results #

References #

Prefix-table and exact-match specifications #

pi is a valid partial KMP failure table for p if pi.size ≤ p.size and each entry pi[j] equals borderLen p.toList (j + 1).

Structural fallback bound pi[j] ≤ j for every j < pi.size.

def Cslib.Algorithms.Lean.KMP.IsMatchAt {α : Type u_1} (p t : Array α) (s : ℕ) :

Pattern p occurs in text t at shift s (0-indexed).

theorem Cslib.Algorithms.Lean.KMP.toList_isSuffix_take_iff_isMatchAt {α : Type u_1} {p t : Array α} {i : ℕ} (hit : i < t.size) :
p.toList <:+ List.take (i + 1) t.toList ↔ p.size ≤ i + 1 ∧ IsMatchAt p t (i + 1 - p.size)

For i < t.size, p.toList is a suffix of t.toList.take (i + 1) iff p.size ≤ i + 1 and p matches t at shift i + 1 - p.size.

KMP automaton transition (advance) #

@[irreducible]
def Cslib.Algorithms.Lean.KMP.advance {α : Type u_1} [DecidableEq α] (p : Array α) (pi : Array ℕ) (k : ℕ) (c : α) :

Single-character transition of the KMP automaton on pattern p with failure table pi.

theorem Cslib.Algorithms.Lean.KMP.advance_eq_nextLen {α : Type u_1} [DecidableEq α] {p : Array α} {pi : Array ℕ} {k : ℕ} (c : α) (htab : IsPrefixTableFor p pi) (hkpi : k ≤ pi.size) (hkp : k < p.size) :
advance p pi k c = nextLen p.toList k c

Under a valid partial prefix table IsPrefixTableFor p pi covering k ≤ pi.size with k < p.size, advance computes nextLen p.toList k c.

Failure-table construction (computePrefix) #

Construct the KMP failure table of p ([KnuthMorrisPratt1977]; [CLRS2009], Section 32.4).

KMP exact string matching (kmpSearchWithPrefix, kmpMatches, kmpMatchesList) #

Exact KMP string matching using a precomputed failure table pi ([KnuthMorrisPratt1977]; [CLRS2009], Section 32.4). Returns all 0-indexed match shifts in strictly increasing order; when p.size = 0, returns List.range (t.size + 1).

Pure KMP exact string matcher returning all 0-indexed match shifts of p in t in strictly increasing order ([KnuthMorrisPratt1977], Sections 1–2; [CLRS2009], Section 32.4).

Pure KMP exact string matcher on List α.

theorem Cslib.Algorithms.Lean.KMP.advance_eq_maxPrefixSuffixUpTo_take_succ {α : Type u_1} [DecidableEq α] {p : Array α} {pi : Array ℕ} {t : Array α} {i k : ℕ} (hp : 0 < p.size) (hp_pi : pi.size = p.size) (htab : IsPrefixTableFor p pi) (hilt : i < t.size) (hk : k = maxPrefixSuffixUpTo p.toList (List.take i t.toList) (p.size - 1)) :

Reading t[i] through advance updates the longest matched prefix of p at the end of t.toList.take i (bounded by p.size - 1) to the longest matched prefix at t.toList.take (i + 1) (bounded by p.size).

theorem Cslib.Algorithms.Lean.KMP.kmpSearchWithPrefix_spec {α : Type u_1} [DecidableEq α] {p : Array α} {pi : Array ℕ} (t : Array α) (hsize : pi.size = p.size) (htab : IsPrefixTableFor p pi) :
(∀ (s : ℕ), s ∈ kmpSearchWithPrefix p pi t ↔ IsMatchAt p t s) ∧ List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) (kmpSearchWithPrefix p pi t)
theorem Cslib.Algorithms.Lean.KMP.mem_kmpMatches_iff {α : Type u_1} [DecidableEq α] (p t : Array α) (s : ℕ) :

Unconditional soundness and completeness of kmpMatches ([KnuthMorrisPratt1977], Sections 1–2; [CLRS2009], Section 32.4): s ∈ kmpMatches p t iff p occurs in t at shift s.

theorem Cslib.Algorithms.Lean.KMP.pairwise_lt_kmpMatches {α : Type u_1} [DecidableEq α] (p t : Array α) :
List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) (kmpMatches p t)

kmpMatches p t lists all match shifts in strictly increasing order.

kmpMatches p t contains no duplicate shifts.

Soundness and completeness of kmpMatchesList on List α.

theorem Cslib.Algorithms.Lean.KMP.pairwise_lt_kmpMatchesList {α : Type u_1} [DecidableEq α] (p t : List α) :
List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) (kmpMatchesList p t)

Explicit filter characterization of kmpMatchesList, useful for kernel decide evaluation in downstream module clients without unfolding the KMP search loop.