Kraft–McMillan inequality
ProvedSourceCoding.kraft_mcmillan_inequalityFor a uniquely decodable -ary code , .
import Mathlib
namespace SourceCoding
/-- **Kraft–McMillan inequality** (McMillan, 1956). If `S` is a finite uniquely decodable
`D`-ary code (`D = Fintype.card α`), then `∑_{w ∈ S} D^{-|w|} ≤ 1`. -/
theorem kraft_mcmillan_inequality
{α : Type} [Fintype α] [Nonempty α] (S : Finset (List α))
(h : InformationTheory.UniquelyDecodable (S : Set (List α))) :
∑ w ∈ S, (1 / (Fintype.card α : ℝ)) ^ w.length ≤ 1 := by
sorry
end SourceCoding
Read-back
What the Lean code literally says, in plain math · claude-sonnet-5
The definition InformationTheory.UniquelyDecodable applied to a set S of lists over a type α asserts the following universally quantified statement: for all pairs of finite lists of lists L₁ and L₂ (i.e., L₁ and L₂ are each finite sequences whose entries are themselves lists over α — "codewords"), if every entry w of L₁ belongs to S and every entry w of L₂ belongs to S, and if concatenating (flattening) all the lists in L₁ in order yields the same single list as concatenating (flattening) all the lists in L₂ in order, then L₁ and L₂ are equal as lists (same length, same entries in the same order). Note L₁ and L₂ range over arbitrary finite sequences of codewords drawn from S — including the empty sequence, and allowing repeated codewords and any ordering — with no bound on their length; equality of L₁ and L₂ is equality of sequences of codewords, which is strictly stronger than merely producing the same flattened list.
The theorem kraft_mcmillan_inequality fixes a type α (with a Fintype α instance, hence finite, and a Nonempty α instance, hence Fintype.card α ≥ 1) and a finite set S : Finset (List α), i.e., a finite collection of finite lists ("codewords") over the alphabet α. Its hypothesis h is exactly the statement InformationTheory.UniquelyDecodable (S : Set (List α)) as unfolded above, applied to S coerced to a Set (List α). Its conclusion is that the sum, taken over all codewords w in the finite set S, of — where denotes Fintype.card α, the number of elements of α, and is the length of the list w — is at most ; formally, , where the sum, base, and exponent are all real numbers (the exponentiation is real-number exponentiation of a real base by a natural-number exponent, w.length : ℕ). Note that since α is nonempty, Fintype.card α ≥ 1, so 1 / (Fintype.card α : ℝ) is well-defined and lies in (0, 1] (no division-by-zero degeneracy); if S is empty the sum is the empty sum 0 ≤ 1, and if S contains the empty list [] (length 0), that term contributes (1/|α|)^0 = 1 to the sum.
Confirmed by the mission captain (proposal self-audit).