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 #
- Indexing: arrays are
0-indexed, sopi[j] = borderLen p.toList (j + 1)(0 ≤ j < p.size) stores[CLRS2009]'s1-indexed prefix table $\pi[1..m]$ atj = q - 1. Reported match shiftssare0-based (t.extract s (s + p.size) = p), matching[CLRS2009]'s shift $s \in \{0, \dots, n - m\}$. - Alphabet and empty pattern: all operations are universe-polymorphic over any
[DecidableEq α]inType*(rather than a fixed finite alphabet $\Sigma$) and handle empty patternsp.size = 0unconditionally by returningList.range (t.size + 1)(whereas[CLRS2009]assumes $m \ge 1$).
Main definitions #
Cslib.Algorithms.Lean.KMP.IsPrefixTableFor:pi.size ≤ p.sizeandpi[j] = borderLen p.toList (j + 1)forj < pi.size.Cslib.Algorithms.Lean.KMP.IsDecreasingTable: fallback boundpi[j] ≤ jforj < pi.size.Cslib.Algorithms.Lean.KMP.IsMatchAt: exact occurrences + p.size ≤ t.sizeandt.extract s (s + p.size) = p.Cslib.Algorithms.Lean.KMP.advance: single-character KMP automaton transition.Cslib.Algorithms.Lean.KMP.computePrefix: failure-table construction onArray α.Cslib.Algorithms.Lean.KMP.kmpSearchWithPrefix,kmpMatches: exact string matching onArray αreturning all match shifts in strictly increasing order.Cslib.Algorithms.Lean.KMP.kmpMatchesList: list wrapper returning all shiftss ≤ t.lengthwherep <+: t.drop s.
Main results #
Cslib.Algorithms.Lean.KMP.advance_eq_nextLenandadvance_eq_maxPrefixSuffixUpTo_take_succ: under a valid failure table,advanceagrees withnextLenand updates the matched prefix length ont.take (i + 1).Cslib.Algorithms.Lean.KMP.computePrefix_eq_ofFn,computePrefix_size, andgetElem_computePrefix:computePrefix phas sizep.sizeand entryqequal toborderLen p.toList (q + 1).Cslib.Algorithms.Lean.KMP.mem_kmpMatches_iff:s ∈ kmpMatches p t ↔ IsMatchAt p t s(unconditionally for allpandt, including empty patterns and overlapping matches).Cslib.Algorithms.Lean.KMP.pairwise_lt_kmpMatchesandnodup_kmpMatches: match shifts are strictly increasing and duplicate-free.Cslib.Algorithms.Lean.KMP.mem_kmpMatchesList_iffandkmpMatchesList_eq_filter_range:s ∈ kmpMatchesList p t ↔ s ≤ t.length ∧ p <+: t.drop s, giving a kernel-decidable closed form(List.range (t.length + 1)).filter (fun s => decide (p <+: t.drop s)).
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.
Single-character transition of the KMP automaton on pattern p with failure table pi.
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 α.
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).
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.
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 α.
Explicit filter characterization of kmpMatchesList, useful for kernel decide evaluation in
downstream module clients without unfolding the KMP search loop.