Documentation

CslibFrontier.Algorithms.WordRAM.AhoCorasick.TrieModel

Pure Integer-State Trie Model for Word-RAM Aho–Corasick #

Pure Lean specification of the integer-indexed array trie (PTrie) built by inserting all keywords P (Algorithm 2 of [AhoCorasick1975]). Depends only on AhoCorasick.Basic and AhoCorasick.Input (no SymbolicExec, Allocation, or PreservesMask imports).

Main definitions #

Main results #

Pure integer-state trie model #

Pure integer-state trie model built by PTrie.build.

  • numStates : ℕ

    Number of allocated trie states (≥ 1, with root 0).

  • strOf : ℕ → List ℕ

    Ghost mapping assigning each state ID to the keyword prefix it represents.

  • trie : ℕ → ℕ → ℕ

    Flat goto transition table (0 denotes an absent child).

  • depth : ℕ → ℕ

    Depth |(strOf u)| of each trie state u.

  • isTerm : ℕ → ℕ

    Nonzero indicator marking terminal states whose string belongs to P.

Initial single-state trie containing only the root 0.

Traverse or allocate the child of state u on symbol a, returning the updated trie and child state ID.

Insert a character suffix into T starting from state u, returning the updated trie and final state ID.

Insert a single keyword p from root 0 and mark its nonempty terminal state.

Build the integer-state trie for keyword list P starting from PTrie.init.

Structural tree invariant relating a PTrie's tables to its ghost string map strOf.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.stepChar_spec {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) {u a : ℕ} (hu : u < T.numStates) (ha : a < σ) :
have res := T.stepChar u a; TreeInv σ res.1 ∧ T.numStates ≤ res.1.numStates ∧ res.1.numStates ≤ T.numStates + 1 ∧ 0 < res.2 ∧ res.2 < res.1.numStates ∧ res.1.strOf res.2 = T.strOf u ++ [a] ∧ ∀ v < T.numStates, res.1.strOf v = T.strOf v ∧ res.1.isTerm v = T.isTerm v
theorem Cslib.Algorithms.WordRAM.AhoCorasick.insertChars_spec {σ : ℕ} (xs : List ℕ) (T : PTrie) (u : ℕ) :
TreeInv σ T → u < T.numStates → (∀ a ∈ xs, a < σ) → have res := T.insertChars u xs; TreeInv σ res.1 ∧ T.numStates ≤ res.1.numStates ∧ res.1.numStates ≤ T.numStates + xs.length ∧ res.2 < res.1.numStates ∧ res.1.strOf res.2 = T.strOf u ++ xs ∧ (xs ≠ [] → 0 < res.2) ∧ (∀ v < T.numStates, res.1.strOf v = T.strOf v ∧ res.1.isTerm v = T.isTerm v) ∧ (∀ (w : ℕ), T.numStates ≤ w → w < res.1.numStates → res.1.isTerm w = 0 ∧ ∃ (q : List ℕ), q ≠ [] ∧ q <+: xs ∧ res.1.strOf w = T.strOf u ++ q) ∧ ∀ (q : List ℕ), q <+: xs → ∃ v < res.1.numStates, res.1.strOf v = T.strOf u ++ q
theorem Cslib.Algorithms.WordRAM.AhoCorasick.trie_char_pos_iff (P : List (List ℕ)) (σ : ℕ) (hvalid : ∀ p ∈ P, ∀ a ∈ p, a < σ) (u : ℕ) :
theorem Cslib.Algorithms.WordRAM.AhoCorasick.depth_le_self {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) (w : ℕ) :
w < T.numStates → T.depth w ≤ w