InformationTheory.Shannon.ShannonCode.KraftReverse
Kraft inequality converse: existence of prefix codes #
Converse direction of Cover–Thomas (McMillan converse): if a length function
l : α → ℕ satisfies the Kraft inequality ∑_a D^{−l(a)} ≤ 1, then there exists
a prefix-free code c : α → List (Fin D) with (c a).length = l a for all a.
Main statements #
exists_prefix_code_of_kraft— Kraft-feasible lengths (D ≥ 2,0 < l a) yield an injective prefix-free code with the prescribed lengths.
Implementation notes #
The proof uses a greedy (Shannon–Fano) construction: sort α by l in ascending order
(sortedByLen), compute a cumulative slot offset
slotStart k := ∑_{j < k} D^(L − l (as[j])) at a common depth L = sup l,
and define c(a) := toBaseDLen D (l a) (slotStart (idx a) / D^(L − l a)).
Here toBaseDLen D L n is the MSB-first length-L base-D digit expansion of n.
Prefix-freeness follows from a gap argument on slotStart.
Definitions #
InformationTheory.Shannon.ShannonCodeKraftReverse.toBaseDLen
sourceMSB-first base-D digit expansion of length L of n.
For n < D^L, this is the canonical length-L digit list (most-significant first).
Equations
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.IsPrefixFree
sourcePredicate asserting that c is prefix-free: no codeword is a proper prefix of another.
Equations
- InformationTheory.Shannon.ShannonCodeKraftReverse.IsPrefixFree c = ∀ (a b : α), a ≠ b → ¬c a <+: c b
Instances For
Used by
Properties of toBaseDLen #
InformationTheory.Shannon.ShannonCodeKraftReverse.toBaseDLen_length
sourceLength of toBaseDLen D L n is L.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.toBaseDLen_take
sourceThe top-L₁ digits of toBaseDLen D L₂ n equal toBaseDLen D L₁ (n / D^(L₂ − L₁))
when L₁ ≤ L₂ (MSB-first prefix property).
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.toBaseDLen_eq_of_isPrefix
sourceIf toBaseDLen D L₁ n₁ is a prefix of toBaseDLen D L₂ n₂ and L₁ ≤ L₂, then
toBaseDLen D L₁ n₁ = toBaseDLen D L₁ (n₂ / D ^ (L₂ − L₁)).
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.toBaseDLen_injOn_lt
sourceStrict variant: if n₁, n₂ < D^L, toBaseDLen D L n₁ = toBaseDLen D L n₂ ⟹ n₁ = n₂.
Used by
Greedy construction #
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedByLen
sourceElements of α sorted by l in ascending order.
Equations
- InformationTheory.Shannon.ShannonCodeKraftReverse.sortedByLen l = Finset.univ.toList.mergeSort fun (a b : α) => decide (l a ≤ l b)
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedByLen_length
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.mem_sortedByLen
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedByLen_pairwise_le
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart
sourceCumulative slot offset: slotStart D l L k = ∑_{j < k} D^(L − l (sortedByLen l)[j]).
Saturates to the full sum for k ≥ |α|.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_mono
sourceslotStart is monotone in k.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_succ
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_of_ge
sourceslotStart stabilizes when k ≥ (sortedByLen l).length.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_card_eq_sum
sourceslotStart at Fintype.card α equals the full sum ∑ a : α, D ^ (L − l a).
Used by
Slot upper bound from the Kraft condition #
InformationTheory.Shannon.ShannonCodeKraftReverse.kraft_sum_nat_le_of_real
sourceThe Kraft condition implies ∑_a D^(L − l(a)) ≤ D^L as a natural number inequality.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_le_pow
sourceslotStart D l L k ≤ D ^ L for all k.
Used by
Code construction and main theorem #
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedIndex
sourcePosition of a in sortedByLen l.
Equations
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedIndex_lt
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.getElem_sortedByLen_sortedIndex
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.sortedIndex_injective
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.shannonFanoCode
sourceMain code construction: the Shannon–Fano codeword for a.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.shannonFanoCode_length
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_gap
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.slotStart_lt_pow_of_lt
sourceslotStart D l L j < D ^ L for j < (sortedByLen l).length.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.not_isPrefix_of_sortedIndex_lt
sourceWhen sortedIndex l x < sortedIndex l y and l x ≤ l y, the code of x is not
a prefix of the code of y.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.shannonFanoCode_prefixFree
sourceshannonFanoCode is prefix-free. (The hypothesis hl : 0 < l a is redundant given hk
but is kept for API uniformity.)
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.shannonFanoCode_injective
sourceshannonFanoCode is injective.
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.commonDepth
sourceCommon depth: sup_{a} l a, the maximum codeword length.
Instances For
Used by
InformationTheory.Shannon.ShannonCodeKraftReverse.le_commonDepth
sourceUsed by
InformationTheory.Shannon.ShannonCodeKraftReverse.exists_prefix_code_of_kraft
sourceKraft converse: Kraft-feasible positive lengths yield a prefix-free code.