Documentation

CslibFrontier.Algorithms.Lean.AhoCorasick.Basic

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 #

Main results #

References #

Goto-graph nodes #

def Cslib.Algorithms.Lean.AhoCorasick.IsNode {α : Type u_1} (P : List (List α)) (u : List α) :

u is a state of the goto graph of P: the start state [] or a prefix of a keyword.

Instances For
    theorem Cslib.Algorithms.Lean.AhoCorasick.IsNode.of_prefix {α : Type u_1} {P : List (List α)} {u v : List α} (hv : IsNode P v) (h : u <+: v) :
    IsNode P u
    theorem Cslib.Algorithms.Lean.AhoCorasick.isNode_of_mem {α : Type u_1} {P : List (List α)} {p : List α} (hp : p ∈ P) :
    IsNode P p
    theorem Cslib.Algorithms.Lean.AhoCorasick.isNode_take {α : Type u_1} {P : List (List α)} {p : List α} (hp : p ∈ P) (n : ℕ) :

    Longest node suffix #

    The longest suffix of w that is a state of the goto graph of P.

    theorem Cslib.Algorithms.Lean.AhoCorasick.eq_longestNodeSuffix {α : Type u_1} [DecidableEq α] {P : List (List α)} {v w : List α} (hvw : v <:+ w) (hv : IsNode P v) (hmax : ∀ (x : List α), x <:+ w → IsNode P x → x.length ≤ v.length) :

    Failure function #

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

    The failure function: the longest proper suffix of u that is a goto-graph state ([AhoCorasick1975], Lemma 1).

    theorem Cslib.Algorithms.Lean.AhoCorasick.failure_cons {α : Type u_1} [DecidableEq α] (P : List (List α)) (b : α) (u : List α) :
    theorem Cslib.Algorithms.Lean.AhoCorasick.length_failure_lt {α : Type u_1} [DecidableEq α] (P : List (List α)) {u : List α} (hu : u ≠ []) :
    theorem Cslib.Algorithms.Lean.AhoCorasick.eq_failure_iff {α : Type u_1} [DecidableEq α] {P : List (List α)} {u v : List α} (hu : u ≠ []) :
    v = failure P u ↔ v <:+ u ∧ v.length < u.length ∧ IsNode P v ∧ ∀ (x : List α), x <:+ u → x.length < u.length → IsNode P x → x.length ≤ v.length

    [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 #

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

    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).

    theorem Cslib.Algorithms.Lean.AhoCorasick.nextMove_of_isNode {α : Type u_1} [DecidableEq α] {P : List (List α)} {u : List α} {a : α} (h : IsNode P (u ++ [a])) :
    nextMove P u a = u ++ [a]
    theorem Cslib.Algorithms.Lean.AhoCorasick.nextMove_of_not_isNode_of_eq_nil {α : Type u_1} [DecidableEq α] {P : List (List α)} {u : List α} {a : α} (h : ¬IsNode P (u ++ [a])) (hu : u = []) :
    nextMove P u a = []
    theorem Cslib.Algorithms.Lean.AhoCorasick.nextMove_of_not_isNode {α : Type u_1} [DecidableEq α] {P : List (List α)} {u : List α} {a : α} (h : ¬IsNode P (u ++ [a])) (hu : u ≠ []) :
    nextMove P u a = nextMove P (failure P u) a

    [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.

    theorem Cslib.Algorithms.Lean.AhoCorasick.failure_append_singleton {α : Type u_1} [DecidableEq α] (P : List (List α)) {u : List α} (hu : u ≠ []) (a : α) :
    failure P (u ++ [a]) = nextMove P (failure P u) a

    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 #

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

    The keywords of P that are nonempty suffixes of w, longest first, without repetitions.

    theorem Cslib.Algorithms.Lean.AhoCorasick.output_cons {α : Type u_1} [DecidableEq α] (P : List (List α)) (b : α) (w : List α) :
    output P (b :: w) = if b :: w ∈ P then (b :: w) :: output P w else output P w
    theorem Cslib.Algorithms.Lean.AhoCorasick.mem_output_iff {α : Type u_1} [DecidableEq α] {P : List (List α)} {w v : List α} :
    v ∈ output P w ↔ v ≠ [] ∧ v ∈ P ∧ v <:+ w
    theorem Cslib.Algorithms.Lean.AhoCorasick.pairwise_length_gt_output {α : Type u_1} [DecidableEq α] (P : List (List α)) (w : List α) :
    List.Pairwise (fun (x y : List α) => y.length < x.length) (output P w)

    Keywords that are suffixes of w are the keywords that are suffixes of its longest node suffix.

    theorem Cslib.Algorithms.Lean.AhoCorasick.output_eq_of_ne_nil {α : Type u_1} [DecidableEq α] (P : List (List α)) {u : List α} (hu : u ≠ []) :
    output P u = (if u ∈ P then [u] else []) ++ output P (failure P u)

    [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 #

    def Cslib.Algorithms.Lean.AhoCorasick.IsOccurrence {α : Type u_1} (P : List (List α)) (t : List α) (i : ℕ) (y : List α) :

    Keyword y ∈ P ends at position i of t (1-indexed end position, as in [AhoCorasick1975], Algorithm 1): y is nonempty and a suffix of t.take i, with i ≤ t.length.

    All occurrences of keywords of P in t, by increasing end position and, at equal end positions, by decreasing length.

    theorem Cslib.Algorithms.Lean.AhoCorasick.mem_occurrences_iff {α : Type u_1} [DecidableEq α] {P : List (List α)} {t : List α} {i : ℕ} {y : List α} :
    theorem Cslib.Algorithms.Lean.AhoCorasick.occurrences_append_singleton {α : Type u_1} [DecidableEq α] (P : List (List α)) (w : List α) (a : α) :
    occurrences P (w ++ [a]) = occurrences P w ++ List.map (fun (y : List α) => (w.length + 1, y)) (output P (w ++ [a]))

    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.

    theorem Cslib.Algorithms.Lean.AhoCorasick.pairwise_occurrences {α : Type u_1} [DecidableEq α] (P : List (List α)) (t : List α) :
    List.Pairwise (fun (x y : ℕ × List α) => x.1 < y.1 ∨ x.1 = y.1 ∧ y.2.length < x.2.length) (occurrences P t)

    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.