Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.fisher-yates
AHoF Fisher-Yates Shuffle (Knuth shuffle): produce a UNIFORMLY RANDOM permutation of an array in O(n) time in place -- every one of the n! orderings is equally likely. Walking from the last index down to the first, position i is swapped with a random index j drawn from [0, i]. Correctness (uniformity) hinges on drawing j from exactly that range -- neither below zero nor above i -- so the swap index is valid exactly when it lies in [0, i]: valid-swap? j i = decide (j <= i) (with j >= 0 by construction), over exact-rational (integer) indices. Defined: the valid-swap test. Proved native and axiom-free, as worked instances: at i = 5 the index 2 is valid and the index 7 is not, and the worked certificate 2 <= 5 (native nnrat/at-most). Uniformity of the shuffle, and that the fixed suffix is never disturbed, are classical and not proved here. The RNG and the in-place swaps 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.fisher-yates` prints this classification.
fisher-yates/valid-swap?
fisher-yates/valid-swap? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
fisher-yates/in-range-swap-valid
fisher-yates/in-range-swap-valid : → ce-is/elite? 2 5 = data/true
= = equality/Eq
fisher-yates/valid-swap-within-range
fisher-yates/valid-swap-within-range : → 2 ≤ 5
≤ = nnrat/at-most
fisher-yates/out-of-range-swap-invalid
fisher-yates/out-of-range-swap-invalid : → ce-is/elite? 7 5 = data/false
= = equality/Eq