Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Kraft's inequality, sufficiency direction, for positive codeword lengths

Proved
SourceCoding.kraft_sufficiency_of_pos_lengths

by Zehao Jin · Sep 8, 2026 · Mathlib 0df444a (Lean v4.33.1)

coding-theoryinformation-theory

Let α\alphaα be a finite nonempty code alphabet with D=∣α∣D=|\alpha|D=∣α∣ letters, let ι\iotaι be a finite set of source symbols, and let ℓ:ι→N\ell:\iota\to\mathbb Nℓ:ι→N prescribe a positive codeword length ℓi≥1\ell_i\ge 1ℓi​≥1 for each symbol. If the lengths satisfy Kraft's inequality

∑i∈ιD−ℓi≤1,\sum_{i\in\iota} D^{-\ell_i}\le 1,i∈ι∑​D−ℓi​≤1,

then there is an injective assignment of codewords c:ι→α∗c:\iota\to\alpha^{*}c:ι→α∗ with ∣c(i)∣=ℓi|c(i)|=\ell_i∣c(i)∣=ℓi​ for every iii whose set of codewords {c(i)}\{c(i)\}{c(i)} is uniquely decodable, in the sense that distinct finite sequences of codewords have distinct concatenations.

This is the sufficiency direction of Kraft's theorem (Kraft 1949; Cover and Thomas, Elements of Information Theory, Theorem 5.2.1): a prefix code with the prescribed lengths exists, and prefix codes are uniquely decodable. The necessity direction, for uniquely decodable codes, is the Kraft–McMillan inequality available in Mathlib as InformationTheory.kraft_mcmillan_inequality. The positivity hypothesis ℓi≥1\ell_i\ge1ℓi​≥1 is essential: a codeword of length 000 is the empty word, which no uniquely decodable code can contain, and without the hypothesis the statement is false already for a one-symbol source with ℓ≡0\ell\equiv0ℓ≡0 (this is why the earlier platform statement SourceCoding.kraft_inequality_sufficiency was disproved). No lower bound on DDD is assumed: for D=1D=1D=1 the Kraft bound forces at most one symbol, and a single nonempty codeword is uniquely decodable.

Preamble
import Mathlib
Formal statement
namespace SourceCoding

/-- **Kraft's inequality, sufficiency direction** (Kraft 1949; Cover--Thomas, Theorem 5.2.1).
If `D = Fintype.card α` and the positive integer lengths `ℓ : ι → ℕ` satisfy the Kraft sum
bound `∑ i, D^{-ℓ i} ≤ 1`, there is an injective assignment `c : ι → List α` of codewords with
`(c i).length = ℓ i` for every `i` whose codeword set is uniquely decodable. -/
theorem kraft_sufficiency_of_pos_lengths
    {α : Type} [Fintype α] [Nonempty α] {ι : Type} [Fintype ι]
    (ℓ : ι → ℕ) (hpos : ∀ i, 1 ≤ ℓ i)
    (hK : ∑ i, (1 / (Fintype.card α : ℝ)) ^ ℓ i ≤ 1) :
    ∃ c : ι → List α, Function.Injective c ∧ (∀ i, (c i).length = ℓ i) ∧
      InformationTheory.UniquelyDecodable (Set.range c) := by sorry

end SourceCoding
Source
T. M. Cover and J. A. Thomas, Elements of Information Theory, 2nd ed., Wiley 2006, Theorem 5.2.1 (Kraft inequality), p. 107, converse (sufficiency) part; L. G. Kraft, A device for quantizing, grouping, and coding amplitude modulated pulses, MIT M.Sc. thesis, 1949.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me