Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.huffman
AHoF Huffman Coding: build an optimal prefix-free binary code by a greedy bottom-up merge. Repeatedly take the two lowest-frequency nodes, merge them into an internal node whose frequency is their sum, and reinsert; the merge order determines the codeword lengths. The two core operations are: merge-frequency f1 f2 = f1 + f2 (internal node weight) merge-first? fa fb = decide (fa <= fb) (lower frequency first) over exact-rational symbol frequencies. Proved native and axiom-free: the merged frequency; a worked merge; the ordering decision that selects the lower-frequency node first; and the certificate for the worked pair (2 <= 5 as a native nnrat/at-most). Optimality of the Huffman tree (minimum expected code length) is classical and is not proved here. The priority queue and codeword assignment are executable; what is proved is the merge weight and greedy order at these instances. Requires nnrat/add, 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.huffman` prints this classification.
huffman/merge-frequency
huffman/merge-frequency : (x₁ x₂ : ℚ≥0) → ℚ≥0
ℚ≥0 = nnrat/NonnegativeRational
huffman/merge-first?
huffman/merge-first? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
huffman/merge-example
huffman/merge-example : → astar/f-value 2 3 = 5
= = equality/Eq
huffman/merges-lower-first
huffman/merges-lower-first : → ce-is/elite? 2 5 = data/true
= = equality/Eq
huffman/greedy-picks-minimum
huffman/greedy-picks-minimum : → 2 ≤ 5
≤ = nnrat/at-most