Skip to content
Rainbow docs menu

Reference · The Applied Higher-Order Functions catalogue

rainbow.algorithms.proofs.ahof.two-pointers

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

AHoF Two Pointers: solve many array/string problems in a single O(n) pass by maintaining two indices that move toward each other (or in tandem) instead of nesting loops. The canonical use is finding a pair in a SORTED array that sums to a target: with a left and right pointer, if the current sum is too large move the right pointer left (to decrease), if too small move the left pointer right. The shrink decision is: shrink-right? current-sum target = decide (target <= current-sum) (the sum is at least the target, so shrink from the large end), over exact-rational values. Proved native and axiom-free: the pointer-move decision; that a sum at least the target shrinks from the right; the certificate that when shrinking, the current sum really is at least the target (native nnrat/at-most) -- so no smaller-sum pair is skipped; and that a sum below the target grows from the left. The pointer bookkeeping is executable; what is proved is the move-which-pointer decision. 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.two-pointers` prints this classification.

two-pointers/shrink-right?

DefinitionSource, line 60

two-pointers/shrink-right? :
  (x₁ x₂ : ℚ≥0)
  → data/Bool

ℚ≥0 = nnrat/NonnegativeRational

two-pointers/large-sum-shrinks-right

TheoremWorked exampleNo axiomsSource, line 71

two-pointers/large-sum-shrinks-right :
  → bo/is-safe 7 5 = data/true

= = equality/Eq

two-pointers/shrink-sum-at-least-target

TheoremWorked exampleNo axiomsSource, line 80

two-pointers/shrink-sum-at-least-target :
  → 5 ≤ 7

≤ = nnrat/at-most

two-pointers/small-sum-grows-left

TheoremWorked exampleNo axiomsSource, line 86

two-pointers/small-sum-grows-left :
  → bo/is-safe 3 5 = data/false

= = equality/Eq