Aho–Corasick Goto-Graph States, Failure and Output Functions #
This module develops the combinatorics of the Aho–Corasick pattern matching machine
([AhoCorasick1975]) for a list P : List (List α) of keywords over an alphabet with decidable
equality. As in [AhoCorasick1975], Section 4, a state is represented by its string: the states
are the prefixes of keywords together with the start state [] (IsNode P); the goto function
sends u on a to u ++ [a] when that is a state, and the start state loops on every other
symbol.
The failure function, the output function and the state reached after reading a text are given
by their specifications ([AhoCorasick1975], Lemmas 1–3): failure P u is the longest proper
suffix of u that is a state, output P w lists the keywords that are nonempty suffixes of w,
and the state after reading w is longestNodeSuffix P w. The recurrences proved here are the
ones used by Algorithms 1 and 3 in CslibFrontier.Algorithms.Lean.AhoCorasick.Search.
Main definitions #
Cslib.Algorithms.Lean.AhoCorasick.IsNode:u = []oruis a prefix of a keyword ofP.Cslib.Algorithms.Lean.AhoCorasick.longestNodeSuffix: the longest suffix ofwthat is a state.Cslib.Algorithms.Lean.AhoCorasick.failure: the failure function,longestNodeSuffix P u.tail.Cslib.Algorithms.Lean.AhoCorasick.nextMove: one operating cycle (failure transitions followed by one goto transition).Cslib.Algorithms.Lean.AhoCorasick.output: the keywords ofPthat are nonempty suffixes ofw, longest first, without repetitions.Cslib.Algorithms.Lean.AhoCorasick.IsOccurrence: keywordy ∈ Pends at the1-indexed positionioft.Cslib.Algorithms.Lean.AhoCorasick.occurrences: all occurrences, by increasing end position.
Main results #
Cslib.Algorithms.Lean.AhoCorasick.eq_failure_iff:[AhoCorasick1975], Lemma 1.Cslib.Algorithms.Lean.AhoCorasick.mem_output_iffandoutput_eq_of_ne_nil:[AhoCorasick1975], Lemma 2, and the output recurrence of Algorithm 3.Cslib.Algorithms.Lean.AhoCorasick.longestNodeSuffix_append_singleton_eq_nextMoveandfoldl_nextMove:[AhoCorasick1975], Lemma 3.Cslib.Algorithms.Lean.AhoCorasick.failure_append_singleton: the failure recurrence of Algorithm 3.Cslib.Algorithms.Lean.AhoCorasick.mem_occurrences_iff,nodup_occurrences,pairwise_occurrences,occurrences_append_singleton,length_occurrences_leandoccurrences_prefix_append.
References #
Goto-graph nodes #
Longest node suffix #
The longest suffix of w that is a state of the goto graph of P.
Failure function #
The failure function: the longest proper suffix of u that is a goto-graph state
([AhoCorasick1975], Lemma 1).
[AhoCorasick1975], Lemma 1: for a nonempty u, failure P u is the longest proper suffix of
u that is a goto-graph state.
The next-move function #
The composite transition of one operating cycle: follow failure links from u until the goto
function is defined on a, then take that goto transition (the next-move function δ of
[AhoCorasick1975], Section 6).
[AhoCorasick1975], Lemma 3 (one cycle): the longest node suffix of w ++ [a] is reached from
the longest node suffix of w by one operating cycle.
The recurrence used by Algorithm 3 of [AhoCorasick1975]: for nonempty u, the failure state
of u ++ [a] is reached from the failure state of u by one operating cycle on a.
Output function #
The keywords of P that are nonempty suffixes of w, longest first, without repetitions.
Keywords that are suffixes of w are the keywords that are suffixes of its longest node
suffix.
[AhoCorasick1975], Lemma 2 and the last statement of Algorithm 3: the output of a nonempty
state u is u itself (when u is a keyword) followed by the output of its failure state.
Occurrences #
Appending a symbol to the text appends the occurrences ending at the new last position.
[AhoCorasick1975], Lemma 3, from an arbitrary text prefix: running the operating cycles on
w from the state of u reaches the state of u ++ w.
[AhoCorasick1975], Lemma 3: after the operating cycles on w from the start state, the
state is the longest suffix of w that is a goto-graph state.
Occurrences are listed by increasing end position and, at equal end positions, by decreasing keyword length.
There are at most t.length * P.length keyword occurrences in t.
Occurrences in a prefix p form a prefix of the occurrences in p ++ ts.