Binary Kraft Converse, Prefix-Free Trie Compaction, and McMillan Reduction #
Mathlib's Mathlib.InformationTheory.Coding.Kraft and KraftMcMillan prove the forward $D$-ary
Kraft and Kraft–McMillan inequalities ∑ w ∈ S, (1 / D) ^ w.length ≤ 1 for finite prefix-free and
uniquely decodable codes. This module supplies the finite binary ($D = 2$, alphabet Bool)
constructive converse and tree reductions needed for Huffman optimality ([Kra49];
[McM56]; [CT06], Theorems 5.2.1 and 5.5.1, specialized from $D$-ary to binary
codes and from real probability weights to arbitrary ordered additive commutative monoids β):
- Prefix-free codebook compaction (
treeOfCodebook): any nonempty prefix-free binary codebook contracts (by eliminating unary internal nodes) to a full binaryCodeTree αwith the same leaf symbols and at most the same weighted external path length under nonnegative weights. - Constructive binary Kraft converse (
kraftAssign,exists_isPrefixFree_iff_kraft_le_one): sorting symbols in nondecreasing order of target lengthℓand assigning consecutive dyadic subintervals of[0, 2 ^ L)constructs an explicit injective prefix-free binary code achieving exact lengthsℓwhenever∑ a ∈ s, (1 / 2 : ℝ) ^ ℓ a ≤ 1, completing the if-and-only-if characterization. - McMillan tree reduction (
exists_codeTree_of_isUniquelyDecodable): combining Mathlib'sIsUniquelyDecodable.finsetSum_one_div_card_pow_length_le_onewith the binary Kraft converse andtreeOfCodebook, every finite injective uniquely decodable binary code on at least two symbols (2 ≤ syms.length) admits a full binaryCodeTree αwhose codebook is itselfInformationTheory.IsUniquelyDecodableand whose weighted path length is at most that of the original code over any ordered additive commutative monoidβ. (For a single symbol, the unique full binaryCodeTree.leaf ahascodewordSet = {[]}, which isIsPrefixFreein Mathlib but notIsUniquelyDecodable.)
Main definitions #
Cslib.Algorithms.Lean.Huffman.bitsOfNat,natOfBits: MSB-first fixed-width binary encoding and decoding onℕ.Cslib.Algorithms.Lean.Huffman.kraftAssign: constructive sorted dyadic codeword assignment.Cslib.Algorithms.Lean.Huffman.treeOfCodebook: compaction of a binary codebook into a full binaryCodeTree α.
Main results #
Cslib.Algorithms.Lean.Huffman.exists_isPrefixFree_of_kraft_nat: integer-scaled constructive binary Kraft converse.Cslib.Algorithms.Lean.Huffman.exists_isPrefixFree_iff_kraft_le_one: Binary Kraft Theorem (∃injective prefix-free binary code of lengthsℓ↔ ∑ a ∈ s, (1 / 2 : ℝ) ^ ℓ a ≤ 1).Cslib.Algorithms.Lean.Huffman.exists_codeTree_of_isPrefixFree: every nonempty finite injective prefix-free binary code is dominated in weighted path length by a fullCodeTree α.Cslib.Algorithms.Lean.Huffman.exists_codeTree_of_isUniquelyDecodable: every finite injective uniquely decodable binary code on at least two symbols (2 ≤ syms.length) is dominated in weighted path length by a fullCodeTree αwhose own codebook is uniquely decodable.
References #
- [Kra49] Leon G. Kraft, A Device for Quantizing, Grouping, and Coding Amplitude-Modulated Pulses, M.S. thesis, MIT, 1949.
- [McM56] Brockway McMillan, Two Inequalities Implied by Unique Decipherability, 1956.
- [CT06] Thomas M. Cover and Joy A. Thomas, Elements of Information Theory (2nd ed.), Section 5.2 and Section 5.5, 2006.
Fixed-width binary representation and dyadic prefix intervals #
Natural number represented by a most-significant-bit-first list of bits.
Constructive binary Kraft converse #
Integer-Scaled Binary Kraft Converse: if s : Finset α and ℓ : α → ℕ satisfy
∀ a ∈ s, ℓ a ≤ L and ∑ a ∈ s, 2 ^ (L - ℓ a) ≤ 2 ^ L, then there exists an injective prefix-free
binary code on s whose codewords have exact lengths ℓ.
Binary Kraft Theorem ([Kra49]; [CT06], Theorem 5.2.1 specialized to binary
alphabet Bool, $D = 2$, on finite codebooks s : Finset α): a finite symbol set s admits an
injective prefix-free binary code with codeword lengths ℓ if and only if
∑ a ∈ s, (1 / 2 : ℝ) ^ ℓ a ≤ 1.
Prefix-free codebook compaction to CodeTree and McMillan reduction #
On any nonempty prefix-incomparable codebook pairs, treeOfCodebook pairs succeeds and
produces a full binary CodeTree α with the same leaf multiset and at most the same weighted
path length under nonnegative weights.
Prefix-Free Code Tree Reduction: every nonempty finite injective prefix-free binary code on
syms admits a full binary CodeTree α with the same leaf symbols and at most the same weighted
path length under nonnegative weights.
McMillan Code Tree Reduction (derived from McMillan's forward inequality [McM56] /
[CT06], Theorem 5.5.1 in Mathlib together with the finite binary Kraft converse of
Theorem 5.2.1 and exists_codeTree_of_isPrefixFree): every finite injective uniquely decodable
binary ($D = 2$) code on at least two distinct symbols (2 ≤ syms.length) admits a full binary
CodeTree α with the same leaf symbols, a uniquely decodable codebook
(InformationTheory.IsUniquelyDecodable T.codewordSet), and at most the same weighted path length
under nonnegative weights over any ordered additive commutative monoid β.
The hypothesis 2 ≤ syms.length is necessary for the CodeTree representation: on a 1-symbol
alphabet, the only full binary tree CodeTree.leaf a assigns the empty codeword []
(codewordSet = {[]}), which satisfies InformationTheory.IsPrefixFree in Mathlib but violates
InformationTheory.IsUniquelyDecodable.epsilon_notMem, whereas every uniquely decodable 1-symbol
code has length at least 1.