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 #
Cslib.Algorithms.WordRAM.AhoCorasick.PTrie: integer-state trie record withnumStates,trie,depth,isTerm, and ghost string mapstrOf.Cslib.Algorithms.WordRAM.AhoCorasick.PTrie.build: insert a keyword list starting from root0.Cslib.Algorithms.WordRAM.AhoCorasick.TreeInv: tree invariant linking state numbers to strings.
Main results #
Cslib.Algorithms.WordRAM.AhoCorasick.treeInv_init,Cslib.Algorithms.WordRAM.AhoCorasick.stepChar_spec,Cslib.Algorithms.WordRAM.AhoCorasick.insertChars_spec,Cslib.Algorithms.WordRAM.AhoCorasick.build_trie_spec: correctness ofPTrie.build.Cslib.Algorithms.WordRAM.AhoCorasick.trie_char_pos_iff: transition positivity equivalence withIsNode.Cslib.Algorithms.WordRAM.AhoCorasick.depth_le_self: state depth is bounded by state index.
Pure integer-state trie model #
Pure integer-state trie model built by PTrie.build.
- numStates : ℕ
Number of allocated trie states (
≥ 1, with root0). Ghost mapping assigning each state ID to the keyword prefix it represents.
Flat goto transition table (
0denotes an absent child).Depth
|(strOf u)|of each trie stateu.Nonzero indicator marking terminal states whose string belongs to
P.
Initial single-state trie containing only the root 0.
Build the integer-state trie for keyword list P starting from PTrie.init.
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.build_trie_spec (P : List (List ℕ)) (σ : ℕ) (hvalid : ∀ p ∈ P, ∀ a ∈ p, a < σ) :
TreeInv σ (PTrie.build P) ∧ (PTrie.build P).numStates ≤ totalPatternLen P + 1 ∧ (∀ u < (PTrie.build P).numStates, Lean.AhoCorasick.IsNode P ((PTrie.build P).strOf u)) ∧ (∀ (w : List ℕ), Lean.AhoCorasick.IsNode P w → ∃ u < (PTrie.build P).numStates, (PTrie.build P).strOf u = w) ∧ ∀ u < (PTrie.build P).numStates,
(PTrie.build P).isTerm u = if (PTrie.build P).strOf u ≠ [] ∧ (PTrie.build P).strOf u ∈ P then 1 else 0
theorem Cslib.Algorithms.WordRAM.AhoCorasick.trie_char_pos_iff (P : List (List ℕ)) (σ : ℕ) (hvalid : ∀ p ∈ P, ∀ a ∈ p, a < σ) (u : ℕ) :
u < (PTrie.build P).numStates →
∀ a < σ, Lean.AhoCorasick.IsNode P ((PTrie.build P).strOf u ++ [a]) ↔ 0 < (PTrie.build P).trie u a