Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.kmp
AHoF Knuth-Morris-Pratt (KMP) string matching: find all occurrences of a pattern in a text in O(n + m) time -- never re-examining a text character -- by precomputing a FAILURE FUNCTION that says, on a mismatch after matching q characters, how far the pattern can shift without missing a match (reusing the longest proper prefix that is also a suffix). A full occurrence is reported exactly when the running matched-length reaches the pattern length: matched? matched-length pattern-length = decide (pattern-length <= matched-length) over exact-rational (integer) lengths. Proved native and axiom-free, as worked instances: matched length 4 against pattern length 4 reports an occurrence; the worked certificate 4 <= 4 (native nnrat/at-most); and a partial match of 2 of 4 is not yet an occurrence. KMP's correctness and its O(n + m) bound are classical and not proved here; the failure function and the scan over the text are executable. Requires nnrat/at-most, audit/decide-nnrat-at-most, nnrat/decide-at-most-true-sound (all in the D63 checkpoint). Scope (measured by the kernel): this module has 3 worked instances (statements about specific values). None of its theorems is a guarantee about the algorithm for all inputs; `lein verify rainbow.algorithms.proofs.ahof.kmp` prints this classification.
kmp/matched?
kmp/matched? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
kmp/full-match-reports
kmp/full-match-reports : → bo/is-safe 4 4 = data/true
= = equality/Eq
kmp/occurrence-matched-whole-pattern
kmp/occurrence-matched-whole-pattern : → 4 ≤ 4
≤ = nnrat/at-most
kmp/partial-match-not-occurrence
kmp/partial-match-not-occurrence : → bo/is-safe 2 4 = data/false
= = equality/Eq