Skip to content
Rainbow docs menu

Reference · The Applied Higher-Order Functions catalogue

rainbow.algorithms.proofs.ahof.kmp

The Applied Higher-Order Functions cataloguesrc/rainbow/algorithms/proofs/ahof/kmp.clj1 definitions, 3 theorems

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?

DefinitionSource, line 59

kmp/matched? :
  (x₁ x₂ : ℚ≥0)
  → data/Bool

ℚ≥0 = nnrat/NonnegativeRational

kmp/full-match-reports

TheoremWorked exampleNo axiomsSource, line 70

kmp/full-match-reports :
  → bo/is-safe 4 4 = data/true

= = equality/Eq

kmp/occurrence-matched-whole-pattern

TheoremWorked exampleNo axiomsSource, line 78

kmp/occurrence-matched-whole-pattern :
  → 4 ≤ 4

≤ = nnrat/at-most

kmp/partial-match-not-occurrence

TheoremWorked exampleNo axiomsSource, line 84

kmp/partial-match-not-occurrence :
  → bo/is-safe 2 4 = data/false

= = equality/Eq