Skip to content
Rainbow docs menu

Reference · The Applied Higher-Order Functions catalogue

rainbow.algorithms.proofs.ahof.fisher-yates

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

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?

DefinitionSource, line 59

fisher-yates/valid-swap? :
  (x₁ x₂ : ℚ≥0)
  → data/Bool

ℚ≥0 = nnrat/NonnegativeRational

fisher-yates/in-range-swap-valid

TheoremWorked exampleNo axiomsSource, line 70

fisher-yates/in-range-swap-valid :
  → ce-is/elite? 2 5 = data/true

= = equality/Eq

fisher-yates/valid-swap-within-range

TheoremWorked exampleNo axiomsSource, line 79

fisher-yates/valid-swap-within-range :
  → 2 ≤ 5

≤ = nnrat/at-most

fisher-yates/out-of-range-swap-invalid

TheoremWorked exampleNo axiomsSource, line 85

fisher-yates/out-of-range-swap-invalid :
  → ce-is/elite? 7 5 = data/false

= = equality/Eq