Kraft's inequality, sufficiency direction, for positive codeword lengths
ProvedSourceCoding.kraft_sufficiency_of_pos_lengthsLet be a finite nonempty code alphabet with letters, let be a finite set of source symbols, and let prescribe a positive codeword length for each symbol. If the lengths satisfy Kraft's inequality
then there is an injective assignment of codewords with for every whose set of codewords 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 is essential: a codeword of length 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 (this is why the earlier platform statement SourceCoding.kraft_inequality_sufficiency was disproved). No lower bound on is assumed: for the Kraft bound forces at most one symbol, and a single nonempty codeword is uniquely decodable.
import Mathlib
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