Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.dutch-flag
AHoF Dutch National Flag (Dijkstra's 3-way partition): partition an array around a pivot into three regions -- less than, equal to, and greater than -- in a single O(n) pass with O(1) extra space. It powers 3-way quicksort (fast on many duplicates) and the classic 0/1/2 sort. Each element is routed by comparing it to the pivot; the 'goes to the low region' test is: goes-low? element pivot = decide (element <= pivot-minus-one) i.e. element < pivot (via the predecessor of the pivot, so equal elements stay in the middle), over exact-rational (integer) keys. Proved native and axiom-free: the low-region test; that an element strictly below the pivot goes to the low region; the certificate that a low-region element is at most the pivot's predecessor (native nnrat/at-most), hence strictly less than the pivot; and that an element at the pivot value does not go low (it stays in the equal region). The three-pointer swap scan is executable; what is proved is the low-region routing 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.dutch-flag` prints this classification.
dutch-flag/goes-low?
dutch-flag/goes-low? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
dutch-flag/below-pivot-goes-low
dutch-flag/below-pivot-goes-low : → ce-is/elite? 2 4 = data/true
= = equality/Eq
dutch-flag/low-element-below-pivot
dutch-flag/low-element-below-pivot : → 2 ≤ 4
≤ = nnrat/at-most
dutch-flag/equal-element-stays-middle
dutch-flag/equal-element-stays-middle : → ce-is/elite? 5 4 = data/false
= = equality/Eq