Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.reservoir-sampling
AHoF Reservoir Sampling (Algorithm R): draw a uniform random sample of fixed size k from a STREAM of unknown, possibly huge length in a SINGLE PASS and O(k) memory -- used for log sampling, streaming analytics, and dataset subsampling. The reservoir holds the first k items; for each later item i (1-indexed) a random slot j in [1, i] is drawn, and the item REPLACES reservoir slot j exactly when the slot falls within the reservoir: replaces? slot k = decide (slot <= k) which happens with probability k/i, keeping every seen item equally likely. Proved native and axiom-free: the replacement test; that a slot within the reservoir triggers a replacement; the certificate that a replacing slot really is within the reservoir (native nnrat/at-most) -- so uniformity is preserved; and that a slot beyond the reservoir discards the item. The RNG and the reservoir array are executable; what is proved is the replacement 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.reservoir-sampling` prints this classification.
reservoir-sampling/replaces?
reservoir-sampling/replaces? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
reservoir-sampling/slot-in-reservoir-replaces
reservoir-sampling/slot-in-reservoir-replaces : → lis/extends? 2 3 = data/true
= = equality/Eq
reservoir-sampling/replacement-within-reservoir
reservoir-sampling/replacement-within-reservoir : → 2 ≤ 3
≤ = nnrat/at-most
reservoir-sampling/slot-beyond-reservoir-discards
reservoir-sampling/slot-beyond-reservoir-discards : → lis/extends? 5 3 = data/false
= = equality/Eq