Documentation

CslibFrontier.Algorithms.Lean.AhoCorasick.Search

Aho–Corasick Multi-Pattern String Matching #

This module implements the pure reference pattern matching machine of [AhoCorasick1975] over arbitrary alphabets with decidable equality: the scan of a text by a machine with given failure and output tables (Algorithm 1, scan), the level-by-level construction of the failure and output tables (Algorithm 3, build), and their composition (search). It proves that scanning with valid tables reports exactly the keyword occurrences occurrences P t of CslibFrontier.Algorithms.Lean.AhoCorasick.Basic, and that build produces valid tables.

For bounded integer alphabets [0, σ), CslibFrontier.Algorithms.WordRAM.AhoCorasick constructs the explicit integer-state array trie, BFS failure links, next-move table, and output-link chains (Algorithms 2 and 3 and Section 6) and scans the text (Algorithm 1 with failure-depth potential) in O((‖P‖ + 1) · σ + |P| + |t| + occ) Word-RAM instructions.

Source conventions #

Main definitions #

Main results #

References #

Machines and validity #

Failure and output tables of a pattern matching machine whose states are represented by their strings.

M has the correct failure and output values at u.

M has the failure function and output function of P ([AhoCorasick1975], Lemmas 1 and 2) on every goto-graph state. This is stronger than the paper's (behavioural) validity, which only asks that Algorithm 1 report the right occurrences: it fixes the tables on every state.

One operating cycle #

@[irreducible]
def Cslib.Algorithms.Lean.AhoCorasick.step {α : Type u_1} [DecidableEq α] (P : List (List α)) (f : List α → List α) (u : List α) (a : α) :
List α

One operating cycle of Algorithm 1 of [AhoCorasick1975] with failure table f. A failure transition that does not shorten the state (which cannot happen for a valid table) stops at the start state.

theorem Cslib.Algorithms.Lean.AhoCorasick.step_eq_nextMove {α : Type u_1} [DecidableEq α] {P : List (List α)} {f : List α → List α} {u : List α} (a : α) (hf : ∀ (v : List α), IsNode P v → v.length ≤ u.length → f v = failure P v) (hu : IsNode P u) :
step P f u a = nextMove P u a

Algorithm 1: the scan #

def Cslib.Algorithms.Lean.AhoCorasick.scan {α : Type u_1} [DecidableEq α] (P : List (List α)) (M : Machine α) (t : List α) :

Algorithm 1 of [AhoCorasick1975]: run the pattern matching machine M on t, reporting each keyword occurrence as its end position and the keyword.

theorem Cslib.Algorithms.Lean.AhoCorasick.scan_eq_occurrences {α : Type u_1} [DecidableEq α] {P : List (List α)} {M : Machine α} (hM : Machine.IsValid P M) (t : List α) :
scan P M t = occurrences P t

Correctness of Algorithm 1 ([AhoCorasick1975], Theorem 1): with valid failure and output tables, the scan reports exactly the keyword occurrences.

Algorithm 3: the failure and output functions #

The pattern matching machine of P, built level by level ([AhoCorasick1975], Algorithm 3).

def Cslib.Algorithms.Lean.AhoCorasick.search {α : Type u_1} [DecidableEq α] (P : List (List α)) (t : List α) :

All keyword occurrences, computed by the Aho–Corasick algorithm.

Correctness of Algorithm 3 ([AhoCorasick1975], Lemmas 1 and 2, Theorem 1): the constructed failure and output functions are those of P on every state.

theorem Cslib.Algorithms.Lean.AhoCorasick.search_eq {α : Type u_1} [DecidableEq α] (P : List (List α)) (t : List α) :

Correctness of the Aho–Corasick algorithm ([AhoCorasick1975], Theorem 1).

theorem Cslib.Algorithms.Lean.AhoCorasick.mem_search_iff {α : Type u_1} [DecidableEq α] {P : List (List α)} {t : List α} {i : ℕ} {y : List α} :
(i, y) ∈ search P t ↔ IsOccurrence P t i y