How short can a code for a data source be, if the code must still be uniquely decodable — if every string of concatenated codewords can be unambiguously split back into the original symbols? Shannon's 1948 source coding theorem answers this exactly: the entropy of the source is a hard lower bound on the average codeword length of any uniquely decodable code, and it is also achievable up to a one-symbol slack. Entropy is not just a measure of "average surprise" — it is the literal, tight answer to a combinatorial question about how densely symbols can be packed into strings without losing decodability. This is the theorem that gives Shannon's entropy its operational meaning, and it underlies every practical lossless compression scheme (Huffman coding, arithmetic coding, Lempel–Ziv) as the benchmark they approach.
Timeline.
A source has a finite alphabet of symbols , with at least two symbols, and probability distribution (, ). A code assigns to each symbol a codeword , a finite string over a -ary code alphabet (); the code is uniquely decodable if every finite sequence of codewords is determined by its concatenation. The entropy of in base is
The expected codeword length of under is .
(For a source with symbols — see Formalization scope for why the single-symbol case must be excluded.)
The result itself. This theorem is the reason entropy is called entropy in an information-theoretic sense at all: it converts a quantity defined by an abstract formula () into the exact answer to an operational question (minimum achievable expected code length), with a slack no worse than one symbol. It is the founding theorem of lossless source coding and the benchmark every practical compressor is measured against.
Formalizing it. Mathlib recently gained genuine information-theoretic coding content: InformationTheory.UniquelyDecodable and the necessity direction of the Kraft–McMillan inequality (McMillan's 1956 result: a uniquely decodable code's lengths satisfy ) are already proved, via a counting argument on concatenations of codewords. This mission builds directly on that foundation rather than duplicating it. What Mathlib does not have — and what this mission's milestones supply — is Kraft's original 1949 sufficiency direction (existence of a uniquely decodable code realizing any length assignment satisfying the Kraft sum bound), any notion of Shannon entropy for a general finite distribution, and the source coding theorem itself.
The lower bound () is the easier half: it follows from the Kraft–McMillan inequality (already in Mathlib) via Gibbs'/Jensen's inequality applied to the two probability-like sequences and (where is the Kraft sum) — a short, self-contained convexity argument.
The achievability half is the genuine construction. Given the ideal (generally non-integer) lengths , one rounds up to (Shannon–Fano–Elias lengths); a one-line estimate shows , so the Kraft sum of the rounded lengths is still , and the bound gives immediately once a code with exactly these lengths is shown to exist. Producing that code is Kraft's sufficiency direction, and it needs an explicit construction: order the lengths, and assign to symbol the first digits of the -ary expansion of the cumulative sum . Verifying this assignment is injective, has the prescribed lengths, and is uniquely decodable (indeed prefix-free) is a careful but standard combinatorial argument — the main open piece of this mission.
The source alphabet must have at least two symbols (), not merely be nonempty. A single-symbol source forces and entropy , so the achievability conjunct would demand a codeword of length — but a uniquely decodable code can never contain the empty codeword (InformationTheory.UniquelyDecodable.epsilon_not_mem, provable from the definition: the empty string decodes ambiguously as zero or two copies of itself), so no admissible code exists and the theorem would be false, not merely hard, at . The same defect breaks Kraft's sufficiency direction (Milestone 2) whenever any prescribed length is , independent of ; that milestone accordingly requires every length strictly positive. With and full support, every strictly, so the Shannon–Fano lengths are automatically all , and the achievability construction only ever needs Milestone 2 at positive lengths. The code alphabet is likewise an arbitrary finite type (matching Mathlib's own Fintype/Nonempty conventions for the Kraft–McMillan file), with required to keep Real.logb non-degenerate. The source distribution is required strictly positive () — the standard simplifying assumption (zero-probability symbols can always be dropped without loss). Unique decodability is stated exactly as Mathlib's InformationTheory.UniquelyDecodable, not re-derived from a "prefix code" definition, so the mission's results transport directly onto Mathlib's existing Kraft–McMillan file. A trivializing route to rule out: proving only the lower bound (citing Mathlib's inequality) while leaving the existential achievability half unaddressed would not be Shannon's theorem — the sandwich is the theorem's actual content, and the lower bound alone (already essentially free from Mathlib) is not a novel contribution on its own.
Reusable output: the Kraft sufficiency construction (Milestone 2) is directly reusable for any future formalization of Huffman coding optimality, arithmetic coding, or the general "Kraft-inequality-achieving code exists" fact used throughout coding theory. Contributions are welcome starting from Milestone 2 (the open construction) or Milestone 3 (the Gibbs'-inequality lower bound, which only needs Milestone 1, already available via Mathlib).
Mathlib.InformationTheory.Coding.UniquelyDecodable and Mathlib.InformationTheory.Coding.KraftMcMillan (2026).import Mathlib
import Definitions.Def_SourceCoding_entropy
namespace SourceCoding
/-- **Shannon's source coding theorem** (Shannon, 1948) — corrected, superseding
`SourceCoding.shannon_source_coding_theorem`, which omitted the hypothesis that the source has
at least two symbols: `Nonempty ι` alone permits `Fintype.card ι = 1`, forcing `p ≡ 1` and
entropy `0`, so the achievability conjunct would demand a codeword of length `0` — impossible,
since a uniquely decodable code can never contain the empty codeword
(`InformationTheory.UniquelyDecodable.epsilon_not_mem`). With `2 ≤ Fintype.card ι`, full
support forces every `p i < 1` strictly, so the Shannon–Fano lengths `⌈-log_D (p i)⌉` are all
at least `1` and the classical argument goes through. For a source with probability
distribution `p : ι → ℝ` (`p i > 0`, `∑ p i = 1`, at least two symbols) and a `D`-ary code
alphabet (`D = Fintype.card α ≥ 2`): every uniquely decodable code assigning distinct
codewords to the source symbols has expected length at least the source's entropy `H_D(p)`,
and there exists a uniquely decodable code whose expected length is within one symbol of this
bound, `< H_D(p) + 1`. -/
theorem shannon_source_coding_theorem_v2
{ι : Type} [Fintype ι] [Nonempty ι] (p : ι → ℝ) (hp_pos : ∀ i, 0 < p i)
(hp_sum : ∑ i, p i = 1) (hι : 2 ≤ Fintype.card ι)
{α : Type} [Fintype α] [Nonempty α] (hD : 2 ≤ Fintype.card α) :
(∀ c : ι → List α, Function.Injective c →
InformationTheory.UniquelyDecodable (Set.range c) →
entropy p (Fintype.card α) ≤ ∑ i, p i * (c i).length) ∧
(∃ c : ι → List α, Function.Injective c ∧
InformationTheory.UniquelyDecodable (Set.range c) ∧
∑ i, p i * (c i).length < entropy p (Fintype.card α) + 1) := by
sorry
end SourceCoding
For a source with at least 2 symbols, over uniquely decodable codes .
No open leaves. Every sub-goal is proved or awaiting decomposition.