Documentation

CslibFrontier.Algorithms.WordRAM.AhoCorasick.BFSCorrect

Correctness of the Pure Breadth-First Aho–Corasick Automaton #

Proves that PBFS.build (PTrie.build P) σ satisfies AutomatonInv: fail, deterministic next-move table delta, and linked output chains (outHead, outNext) agree with the reference Aho–Corasick failure/output specification from Cslib.Algorithms.Lean.AhoCorasick.

Main definitions #

Main results #

Outer breadth-first loop invariant linking queue monotonicity, expanded nodes, delta, and TablesInv.

structure Cslib.Algorithms.WordRAM.AhoCorasick.InnerLoopInv (P : List (List ℕ)) (σ : ℕ) (T : PTrie) (qHead₀ r fr a : ℕ) (B : PBFS) :

Inner loop invariant while scanning characters 0..a-1 of dequeued state r.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.innerStep_spec {P : List (List ℕ)} {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) (hchar : ∀ u < T.numStates, ∀ a < σ, Lean.AhoCorasick.IsNode P (T.strOf u ++ [a]) ↔ 0 < T.trie u a) (hterm : ∀ u < T.numStates, T.isTerm u = if T.strOf u ≠ [] ∧ T.strOf u ∈ P then 1 else 0) {qHead₀ r fr a : ℕ} (ha : a < σ) {B : PBFS} (hinv : InnerLoopInv P σ T qHead₀ r fr a B) :
InnerLoopInv P σ T qHead₀ r fr (a + 1) (PBFS.innerStep T r fr B a)
theorem Cslib.Algorithms.WordRAM.AhoCorasick.innerLoopInv_of_bfsInv {P : List (List ℕ)} {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) {B : PBFS} (hinv : BFSInv P σ T B) (hlt : B.qHead < B.qTail) :
InnerLoopInv P σ T B.qHead (B.queue B.qHead) (B.fail (B.queue B.qHead)) 0 { delta := B.delta, fail := B.fail, outHead := B.outHead, outNext := B.outNext, queue := B.queue, qHead := B.qHead + 1, qTail := B.qTail }
theorem Cslib.Algorithms.WordRAM.AhoCorasick.outerStep_spec {P : List (List ℕ)} {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) (hchar : ∀ u < T.numStates, ∀ a < σ, Lean.AhoCorasick.IsNode P (T.strOf u ++ [a]) ↔ 0 < T.trie u a) (hterm : ∀ u < T.numStates, T.isTerm u = if T.strOf u ≠ [] ∧ T.strOf u ∈ P then 1 else 0) {B : PBFS} (hinv : BFSInv P σ T B) (hlt : B.qHead < B.qTail) :
have r := B.queue B.qHead; have fr := B.fail r; have B₂ := PBFS.innerLoop T r fr σ 0 { delta := B.delta, fail := B.fail, outHead := B.outHead, outNext := B.outNext, queue := B.queue, qHead := B.qHead + 1, qTail := B.qTail }; BFSInv P σ T B₂ ∧ B₂.qHead = B.qHead + 1

Post-condition invariant of the completed breadth-first Aho–Corasick automaton PBFS.build.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.bfsInv_rootLoop {P : List (List ℕ)} {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) (hchar : ∀ u < T.numStates, ∀ a < σ, Lean.AhoCorasick.IsNode P (T.strOf u ++ [a]) ↔ 0 < T.trie u a) (hterm : ∀ u < T.numStates, T.isTerm u = if T.strOf u ≠ [] ∧ T.strOf u ∈ P then 1 else 0) :
have B₀ := PBFS.rootLoop T σ 0 PBFS.init; BFSInv P σ T B₀ ∧ B₀.qHead = 0
theorem Cslib.Algorithms.WordRAM.AhoCorasick.build_bfs_spec (P : List (List ℕ)) (σ : ℕ) (hvalid : ∀ p ∈ P, ∀ a ∈ p, a < σ) :