← All solved theorems
Kraft–McMillan Inequality and Kraft Prefix-Free Code Existence
proved in Lean
The codeword lengths of any uniquely decodable code obey a budget: the sum of D to the power minus each length is at most 1. Conversely, any lengths within this budget are achieved by a prefix-free code, so prefix codes lose nothing.
Leon G. Kraft; Brockway McMillan (1949) · Ch. 5 · Data Compression & Source Coding · Cover & Thomas, Theorems 5.2.1 & 5.5.1; Mathlib.InformationTheory.Coding.KraftMcMillan
Statement
For any uniquely decodable code $S$ over a $D$-ary alphabet ($D = |\beta| \ge 1$), $$\sum_{w \in S} D^{-|w|} \le 1.$$ Conversely, for any finite family of codeword lengths $\ell_1, \dots, \ell_m \in \mathbb{N}$ satisfying $\sum_i D^{-\ell_i} \le 1$, there exists an injective prefix-free $D$-ary code with $|c(i)| = \ell_i$.
How the proof works
The necessity half re-exports Mathlib's IsUniquelyDecodable.finsetSum_one_div_card_pow_length_le_one. The converse (exists_prefixFree_code_of_kraft) is proved constructively by sorting lengths via Tuple.sort and assigning base-$D$ cumulative-sum codewords.
Lean declarations (2)
The key theorems in the Lean library. Each is compiled with Lean 4, uses only the standard axioms propext, Classical.choice and Quot.sound, and is re-checked by leanchecker. Click a name to see the full proof on GitHub.
theorem kraft_mcmillan_inequality {α : Type*} [Fintype α] [Nonempty α]
{S : Finset (List α)} (h : InformationTheory.IsUniquelyDecodable (S : Set (List α))) :
∑ w ∈ S, (1 / (Fintype.card α : ℝ)) ^ w.length ≤ 1 := …
theorem exists_prefixFree_code_of_kraft {ι β : Type*} [Fintype ι] [Fintype β]
[DecidableEq β] [Nonempty β] (l : ι → ℕ)
(hkraft : ∑ i : ι, (1 / (Fintype.card β : ℝ)) ^ (l i) ≤ 1) :
∃ c : ι → List β,
Function.Injective c ∧
InformationTheory.IsPrefixFree ((Finset.univ.image c : Finset (List β)) : Set (List β)) ∧
∀ i, (c i).length = l i := by …
Related open problems (3)
Open problems that build on, generalize, or are analogous to this theorem.