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 #
- States: as in
[AhoCorasick1975], Section 4, a state is represented by its string (IsNode P). The goto function is the prefix testIsNode P (u ++ [a]), and the start state loops on every symbol that does not begin a keyword. - Output: reported pairs
(i, y)give the1-indexed end positioniof the keywordy, as printed by Algorithm 1. A keyword listed several times inPis reported once per end position, and the empty keyword is never reported. - Tables: a
Machine αbundles a failure table and an output table, both functions on strings.Machine.IsValid P Msays they agree withfailure Pandoutput Pon every state. - Guarded failure loop:
stepstops at the start state if a failure transition does not shorten the state. This never happens with a valid table and guarantees termination for any failure table.
Main definitions #
Cslib.Algorithms.Lean.AhoCorasick.Machine: failure and output tables.Cslib.Algorithms.Lean.AhoCorasick.Machine.IsValid: the tables agree withfailureandoutputon every state.Cslib.Algorithms.Lean.AhoCorasick.step: one operating cycle of Algorithm 1.Cslib.Algorithms.Lean.AhoCorasick.scan: Algorithm 1.Cslib.Algorithms.Lean.AhoCorasick.build: Algorithm 3.Cslib.Algorithms.Lean.AhoCorasick.search: build the machine, then scan.
Main results #
Cslib.Algorithms.Lean.AhoCorasick.scan_eq_occurrences: with valid tables, the scan reports exactlyoccurrences P t([AhoCorasick1975], Theorem 1 for Algorithm 1).Cslib.Algorithms.Lean.AhoCorasick.isValid_build: Algorithm 3 computes valid tables ([AhoCorasick1975], Theorem 1 for Algorithm 3).Cslib.Algorithms.Lean.AhoCorasick.search_eqandmem_search_iff.
References #
Machines and validity #
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 #
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.
Algorithm 1: the scan #
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).
Correctness of Algorithm 3 ([AhoCorasick1975], Lemmas 1 and 2, Theorem 1): the constructed
failure and output functions are those of P on every state.
Correctness of the Aho–Corasick algorithm ([AhoCorasick1975], Theorem 1).