Documentation

CslibFrontier.Algorithms.WordRAM.AhoCorasick.BFSModel

Pure BFS Automaton Model and Root-Loop Specification #

Defines the pure breadth-first automaton state PBFS (delta, fail, outHead, outNext, FIFO queue), the linked output-chain walker followOut, the TablesInv queue-push lemma, and the depth-1 root-loop specification rootLoop_spec.

Main definitions #

Main results #

theorem Cslib.Algorithms.WordRAM.AhoCorasick.parent_unique {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) {u₁ c₁ u₂ c₂ : ℕ} (hu₁ : u₁ < T.numStates) (hc₁ : c₁ < σ) (hu₂ : u₂ < T.numStates) (hc₂ : c₂ < σ) (hpos : 0 < T.trie u₁ c₁) (heq : T.trie u₁ c₁ = T.trie u₂ c₂) :
u₁ = u₂ ∧ c₁ = c₂

Fuel-bounded walker collecting state depths along a linked outNext chain stopping at 0.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.followOut_zero_right (depth outNext : ℕ → ℕ) (k : ℕ) :
followOut depth outNext k 0 = []
theorem Cslib.Algorithms.WordRAM.AhoCorasick.followOut_pos (depth outNext : ℕ → ℕ) {k q : ℕ} (hq : 0 < q) :
followOut depth outNext (k + 1) q = depth q :: followOut depth outNext k (outNext q)
theorem Cslib.Algorithms.WordRAM.AhoCorasick.followOut_congr_on {depth outNext outNext' : ℕ → ℕ} {D : Finset ℕ} (hclosed : ∀ x ∈ D, 0 < x → outNext x ∈ D) (heq : ∀ x ∈ D, outNext' x = outNext x) (k q : ℕ) :
q ∈ D → followOut depth outNext' k q = followOut depth outNext k q
theorem Cslib.Algorithms.WordRAM.AhoCorasick.followOut_fuel_eq {depth outNext : ℕ → ℕ} {D : Finset ℕ} (hstep : ∀ x ∈ D, 0 < x → outNext x ∈ D ∧ (outNext x = 0 ∨ depth (outNext x) < depth x)) (k₁ k₂ q : ℕ) :
q ∈ D → q = 0 ∨ depth q < k₁ → q = 0 ∨ depth q < k₂ → followOut depth outNext k₁ q = followOut depth outNext k₂ q

Pure breadth-first construction state for failure links, delta, output chains, and queue.

  • delta : ℕ → ℕ → ℕ

    Deterministic next-move transition table delta u c.

  • fail : ℕ → ℕ

    Failure-link table fail u.

  • outHead : ℕ → ℕ

    Head of the linked output chain for state u (0 if no matches).

  • outNext : ℕ → ℕ

    Next-entry link of the output chain starting from u (0 at tail).

  • queue : ℕ → ℕ

    Explicit FIFO queue buffer storing discovered nonzero trie states.

  • qHead : ℕ

    Index of the next state to dequeue from queue.

  • qTail : ℕ

    Number of enqueued nonzero states (queue[0..qTail)).

Finset of all trie states discovered so far ({0} ∪ queue[0..qTail)).

Finset of trie states already expanded up to queue position h ({0} ∪ queue[0..h)).

Initial all-zero PBFS state with an empty FIFO queue.

Single root-level alphabet step processing symbol a from root 0.

Depth-1 BFS sweep iterating PBFS.rootStep across k symbols starting at a.

Inner BFS step processing symbol a out of dequeued state r with failure link fr.

Inner BFS loop iterating PBFS.innerStep across k symbols starting at a.

Fuel-bounded outer BFS queue expansion loop over alphabet size σ.

Full pure breadth-first automaton builder on trie T and alphabet size σ.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.card_disc {B : PBFS} (hq_pos : ∀ i < B.qTail, 0 < B.queue i) (hq_inj : ∀ i < B.qTail, ∀ j < B.qTail, B.queue i = B.queue j → i = j) :
B.disc.card = B.qTail + 1
theorem Cslib.Algorithms.WordRAM.AhoCorasick.mem_disc_lt {N : ℕ} {B : PBFS} (hN : 1 ≤ N) (hq_pos : ∀ i < B.qTail, 0 < B.queue i ∧ B.queue i < N) {x : ℕ} (hx : x ∈ B.disc) :
x < N
theorem Cslib.Algorithms.WordRAM.AhoCorasick.qTail_lt_numStates {N : ℕ} {B : PBFS} (hN : 1 ≤ N) (hq_pos : ∀ i < B.qTail, 0 < B.queue i ∧ B.queue i < N) (hq_inj : ∀ i < B.qTail, ∀ j < B.qTail, B.queue i = B.queue j → i = j) :
B.qTail < N
theorem Cslib.Algorithms.WordRAM.AhoCorasick.qTail_succ_lt_numStates {N s : ℕ} {B : PBFS} (hN : 1 ≤ N) (hq_pos : ∀ i < B.qTail, 0 < B.queue i ∧ B.queue i < N) (hq_inj : ∀ i < B.qTail, ∀ j < B.qTail, B.queue i = B.queue j → i = j) (hs_lt : s < N) (hs_not : s ∉ B.disc) :
B.qTail + 1 < N

Invariant capturing failure-link and linked output-chain correctness on all discovered states B.disc.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.tablesInv_push {P : List (List ℕ)} {σ : ℕ} {T : PTrie} (hT : TreeInv σ T) (hterm : ∀ u < T.numStates, T.isTerm u = if T.strOf u ≠ [] ∧ T.strOf u ∈ P then 1 else 0) {B B' : PBFS} (htab : TablesInv P T B) (hq_pos : ∀ i < B.qTail, 0 < B.queue i ∧ B.queue i < T.numStates) {s df : ℕ} (hs_pos : 0 < s) (hs_lt : s < T.numStates) (hs_not : s ∉ B.disc) (hdf_disc : df ∈ B.disc) (hdf_str : T.strOf df = Lean.AhoCorasick.failure P (T.strOf s)) (hdisc' : B'.disc = insert s B.disc) (hfail' : B'.fail = Function.update B.fail s df) (houtHead' : B'.outHead = Function.update B.outHead s (if 0 < T.isTerm s then s else B.outHead df)) (houtNext' : B'.outNext = Function.update B.outNext s (B.outHead df)) :
TablesInv P T B'
theorem Cslib.Algorithms.WordRAM.AhoCorasick.disc_push_queue (B : PBFS) (s : ℕ) :
{ delta := B.delta, fail := B.fail, outHead := B.outHead, outNext := B.outNext, queue := Function.update B.queue B.qTail s, qHead := B.qHead, qTail := B.qTail + 1 }.disc = insert s B.disc
structure Cslib.Algorithms.WordRAM.AhoCorasick.RootLoopInv (P : List (List ℕ)) (σ : ℕ) (T : PTrie) (a : ℕ) (B : PBFS) :

Loop invariant maintained after processing the first a symbols during PBFS.rootLoop.

theorem Cslib.Algorithms.WordRAM.AhoCorasick.rootStep_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) {a : ℕ} (ha : a < σ) {B : PBFS} (hinv : RootLoopInv P σ T a B) :
RootLoopInv P σ T (a + 1) (PBFS.rootStep T B a)
theorem Cslib.Algorithms.WordRAM.AhoCorasick.rootLoop_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) (k a : ℕ) (B : PBFS) :
a + k ≤ σ → RootLoopInv P σ T a B → RootLoopInv P σ T (a + k) (PBFS.rootLoop T k a B)