Documentation

CslibFrontier.Algorithms.WordRAM.KMP

Knuth–Morris–Pratt String Matching in Linear Time on the Word RAM #

This module implements the Knuth–Morris–Pratt ([KnuthMorrisPratt1977], Sections 1–2; [CLRS2009], Section 32.4) failure-table construction and exact pattern matcher as fixed Basis.standard word-RAM programs over word sequences, proving linear O(|p|) and O(|p| + |t|) instruction bounds:

Every index calculation, memory load and store, character comparison, fallback transition, match recording step, loop test, and final halt is charged by the machine semantics. Both the failure-table loop and the text-scanning loop use ExecWithin.loop_potential with matched-prefix potential 10 * k, charging each 10-instruction fallback step against the strict decrease pi[k - 1] ≤ k - 1.

Main definitions #

Main results #

References #

Input encoding and registers #

Pattern-matching instance on the word RAM: a pattern pat and a text txt over w-bit words.

Instances For

    Word-list layout [|pat|, |txt|] ++ pat ++ txt placed into State.init: mem[1] = |pat|, mem[2] = |txt|, pat occupies [3, 3 + |pat|), and txt occupies [3 + |pat|, 3 + |pat| + |txt|).

    @[reducible, inline]

    Address register out of the length-prefixed output array.

    Programs #

    Structured program computing (computePrefix p.toArray).toList as a length-prefixed array at out = |p| + 1 from Data.init p.

    Flat program computing (computePrefix p.toArray).toList via ExecutesList.

    Structured program computing kmpMatchesList I.pat I.txt as a length-prefixed array at out = 3 + 2 * |I.pat| + |I.txt| from Data.init I.encode.

    Flat program computing kmpMatchesList I.pat I.txt via ExecutesList.

    Amortized execution of advanceCode #

    Execution of buildPrefixNonempty and prefixCode #

    KMP failure-table construction on the word RAM. Starting from Data.init p at any word width w with Fits w (2 * p.length + 1), prefixCode executes within 31 * p.length + 9 instructions and writes (computePrefix p.toArray).toList as the length-prefixed array at y.regs Reg.out = p.length + 1, preserving the input block [1, 1 + p.length).

    theorem Cslib.Algorithms.WordRAM.KMP.prefixProgram_computesListInTime :
    prefixProgram.ComputesListInTime (fun (x : ℕ) (p : List ℕ) => p) (fun (p : List ℕ) (x : ℕ) => 31 * p.length + 10) (fun (p : List ℕ) (w : ℕ) => WordRAM.Fits w (2 * p.length + 1)) fun (p : List ℕ) => (Lean.KMP.computePrefix p.toArray).toList

    prefixProgram computes (computePrefix p.toArray).toList in 31 * |p| + 10 instructions on every fitting word array at every word width fitting 2 * |p| + 1.

    Execution of searchCode #

    End-to-end KMP pattern search on the word RAM. Starting from Data.init I.encode at any word width w with Fits w (2 * I.pat.length + 2 * I.txt.length + 4), searchCode executes within 31 * I.pat.length + 37 * I.txt.length + 22 instructions and writes kmpMatchesList I.pat I.txt as the length-prefixed array at y.regs Reg.out = 3 + 2 * I.pat.length + I.txt.length, preserving the pattern and text blocks at [3, 3 + I.pat.length) and [3 + I.pat.length, 3 + I.pat.length + I.txt.length).

    searchProgram computes kmpMatchesList I.pat I.txt in 31 * |I.pat| + 37 * |I.txt| + 23 instructions on every fitting SearchInput at every word width fitting 2 * |I.pat| + 2 * |I.txt| + 4.