Kraft's inequality (sufficiency)
DisprovedSourceCoding.kraft_inequality_sufficiencyGiven integer lengths with , a uniquely decodable code with exactly those lengths exists.
import Mathlib
namespace SourceCoding
/-- **Kraft's inequality, sufficiency direction** (Kraft, 1949). If `D = Fintype.card α ≥ 2`
and integer lengths `ℓ : ι → ℕ` satisfy the Kraft sum bound `∑ i, D^{-ℓ i} ≤ 1`, there is an
injective assignment `c : ι → List α` of distinct codewords with `(c i).length = ℓ i` for every
`i`, whose codeword set is uniquely decodable. -/
theorem kraft_inequality_sufficiency
{α : Type} [Fintype α] [Nonempty α] {ι : Type} [Fintype ι]
(ℓ : ι → ℕ) (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
Read-back
What the Lean code literally says, in plain math · claude-sonnet-5
The statement introduces a finite type (with a Fintype instance and a proof that is nonempty) and a second finite type (with its own Fintype instance), together with a function assigning a natural number to each element of . It takes as hypothesis hK the assertion that
where is the (finite) cardinality of and the sum ranges over all in the finite index type , with the summand for each computed as a real number. (If is empty, this sum is the empty sum, equal to , and the hypothesis holds automatically/vacuously regardless of .)
Given this hypothesis, the theorem's conclusion asserts the existence of a function (assigning to each a finite list, i.e. a "word," of elements of ) satisfying, jointly, the following three properties:
- Injectivity: is injective as a function, i.e. for all , if (as lists) then ; equivalently, distinct elements of are mapped to distinct lists.
- Length matching: for every , the length of the list equals exactly — i.e. for all , with no slack or inequality, an exact equality for every single index.
- Unique decodability of the range: writing for the image (as a set) of , the property holds, which unfolds exactly as follows: for all finite lists of words such that every word occurring in belongs to and every word occurring in belongs to , if the concatenation ("flatten") of equals the concatenation of (as lists of elements of , obtained by flattening each list of lists into one list), then as lists (i.e. as sequences — same length, same words in the same order, not merely as sets or multisets). This is a universally quantified statement over all pairs of finite sequences of words drawn from (repetitions of words from within or are allowed, and may have any length, including zero), asserting that the map "concatenate the sequence of words" is injective when restricted to sequences whose words all lie in .
The existential quantifier over has the widest scope: a single function is claimed to exist that simultaneously satisfies all three properties (injectivity, exact length-matching for every , and unique decodability of its range), and this is asserted for every choice of , , , and every proof of satisfying the stated hypothesis — i.e. is a hypothesis of the theorem, universally bound (implicitly, via the theorem's signature) before the existential claim about .
Nothing in the conclusion states or implies that the set has the "prefix" property (that no codeword is a prefix of another) or any other structural property of the codewords (such as being a prefix code, suffix code, or satisfying any combinatorial condition on how the words relate to one another as strings) beyond exactly these three stated properties: injectivity of , exact matching of lengths to , and unique decodability of the range as defined above. In particular, unique decodability here is stated purely in terms of the flatten/concatenation-injectivity condition on sequences of words drawn from , not in terms of any prefix-free or other syntactic condition on the words themselves.
Same edge case as the goal, same fix needed here: with iota a singleton and ell = 0 the Kraft sum is exactly 1, the conclusion forces a codeword of length 0, and the range then contains the empty list, contradicting unique decodability. The docstring even states a
D >= 2hypothesis the formal statement does not have. Addingforall i, 1 <= ell i(and the missing2 <= Fintype.card alphaif the docstring is to match) makes it the true Kraft statement. One further note, no separate flag: the Kraft McMillan milestone is byte identical to Mathlib's ownInformationTheory.kraft_mcmillan_inequality, which the description discloses; carrying it as an anchor is fine, but it will be closed by a one line wrapper, so consider marking it as such or importing the Mathlib name directly in downstream sketches.