Documentation

CslibFrontier.Algorithms.Lean.AhoCorasick.KMP

Aho–Corasick with One Keyword: the Knuth–Morris–Pratt Special Case #

For a single keyword p, the pattern matching machine of [AhoCorasick1975] is the Knuth–Morris–Pratt automaton ([KnuthMorrisPratt1977]): the failure state of the prefix of length q is the prefix of length borderLen p q (the KMP prefix function), and the Aho–Corasick search reports exactly the KMP match shifts, each shifted to its end position.

Main results #

References #

theorem Cslib.Algorithms.Lean.AhoCorasick.failure_singleton_take {α : Type u_1} [DecidableEq α] {p : List α} {q : ℕ} (hq : 0 < q) (hqp : q ≤ p.length) :

For one keyword, the failure function is the KMP prefix function: the failure state of the prefix of length q is its longest proper border.

For one nonempty keyword, its occurrences ending at s + p.length are its KMP matches at shift s.

theorem Cslib.Algorithms.Lean.AhoCorasick.search_singleton {α : Type u_1} [DecidableEq α] {p : List α} (hp : p ≠ []) (t : List α) :
search [p] t = List.map (fun (s : ℕ) => (s + p.length, p)) (KMP.kmpMatchesList p t)

For one nonempty keyword, the Aho–Corasick search reports exactly the KMP matches, in the same order, each as its end position.