Documentation

CslibFrontier.Foundations.Data.BitString.List.PrefixFree

Self-Delimiting Binary Codes and Prefix-Free Concatenation Combinators #

This module provides deterministic concatenation combinators for InformationTheory.IsPrefixFree together with two basic binary prefix-free codes in Cslib.BitString:

Main definitions #

Main results #

References #

theorem InformationTheory.IsPrefixFree.bind_append {ι : Type u_1} {S : Set (List ι)} {T : List ι → Set (List ι)} (hS : IsPrefixFree S) (hT : ∀ u ∈ S, IsPrefixFree (T u)) :
IsPrefixFree {w : List ι | ∃ u ∈ S, ∃ v ∈ T u, w = u ++ v}

Dependent concatenation of a prefix-free prefix set S with fiberwise prefix-free suffix sets T u is prefix-free.

Self-delimiting and prefix-free combinators #

theorem Cslib.BitString.prefix_append_of_length_eq {α : Type u_1} {u₁ u₂ v₁ v₂ : List α} (hlen : u₁.length = u₂.length) (hp : u₁ ++ v₁ <+: u₂ ++ v₂) :
u₁ = u₂ ∧ v₁ <+: v₂

Equal-length prefixes of equal or prefix-related concatenations must be equal, leaving a prefix relation on the tails.

theorem Cslib.BitString.append_inj_of_isPrefixFree {ι : Type u_1} {S : Set (List ι)} (hS : InformationTheory.IsPrefixFree S) {u₁ u₂ v₁ v₂ : List ι} (hu₁ : u₁ ∈ S) (hu₂ : u₂ ∈ S) (heq : u₁ ++ v₁ = u₂ ++ v₂) :
u₁ = u₂ ∧ v₁ = v₂

Equal concatenations with prefixes drawn from a prefix-free set have equal prefix and suffix.

theorem Cslib.BitString.prefix_append_of_isPrefixFree {α : Type u_1} {S : Set (List α)} (hpf : InformationTheory.IsPrefixFree S) {u₁ u₂ v₁ v₂ : List α} (hu₁ : u₁ ∈ S) (hu₂ : u₂ ∈ S) (hp : u₁ ++ v₁ <+: u₂ ++ v₂) :
u₁ = u₂ ∧ v₁ <+: v₂

A prefix of concatenations whose heads are drawn from a prefix-free set forces equal heads and a prefix relation on the tails.

theorem Cslib.BitString.isPrefixFree_biUnion_append {ι : Type u_1} [DecidableEq ι] {S : Finset (List ι)} {T : List ι → Finset (List ι)} (hS : InformationTheory.IsPrefixFree ↑S) (hT : ∀ u ∈ S, InformationTheory.IsPrefixFree ↑(T u)) :
InformationTheory.IsPrefixFree ↑(S.biUnion fun (u : List ι) => Finset.image (fun (x : List ι) => u ++ x) (T u))

Finite Finset.biUnion form of IsPrefixFree.bind_append.

theorem Cslib.BitString.isPrefixFree_image_append {γ : Type u_1} {ι : Type u_2} {D : Set γ} {enc₁ enc₂ : γ → List ι} (h₁ : InformationTheory.IsPrefixFree (enc₁ '' D)) (h₂ : ∀ x ∈ D, InformationTheory.IsPrefixFree (enc₂ '' {y : γ | y ∈ D ∧ enc₁ y = enc₁ x})) :
InformationTheory.IsPrefixFree ((fun (x : γ) => enc₁ x ++ enc₂ x) '' D)

Sequential encoding x ↦ enc₁ x ++ enc₂ x on D is prefix-free whenever enc₁ '' D is prefix-free and enc₂ is prefix-free on each enc₁-fiber of D.

theorem Cslib.BitString.injOn_append {γ : Type u_1} {ι : Type u_2} {D : Set γ} {enc₁ enc₂ : γ → List ι} (h₁ : InformationTheory.IsPrefixFree (enc₁ '' D)) (hinj : Set.InjOn (fun (x : γ) => (enc₁ x, enc₂ x)) D) :
Set.InjOn (fun (x : γ) => enc₁ x ++ enc₂ x) D

Sequential encoding x ↦ enc₁ x ++ enc₂ x is injective on D whenever enc₁ '' D is prefix-free and the pair (enc₁, enc₂) is injective on D.

theorem Cslib.BitString.injOn_prod_append {α : Type u_1} {β : Type u_2} {ι : Type u_3} {D : Set (α × β)} {enc₁ : α → List ι} {enc₂ : α → β → List ι} (hinj₁ : Set.InjOn enc₁ (Prod.fst '' D)) (hpf₁ : InformationTheory.IsPrefixFree (enc₁ '' Prod.fst '' D)) (hinj₂ : ∀ (a : α), Set.InjOn (enc₂ a) {b : β | (a, b) ∈ D}) :
Set.InjOn (fun (p : α × β) => enc₁ p.1 ++ enc₂ p.1 p.2) D

Dependent pair encoding (a, b) ↦ enc₁ a ++ enc₂ a b is injective on D : Set (α × β) when enc₁ is injective and prefix-free on Prod.fst '' D and enc₂ a is injective on each slice.

theorem Cslib.BitString.isPrefixFree_prod_append {α : Type u_1} {β : Type u_2} {ι : Type u_3} {D : Set (α × β)} {enc₁ : α → List ι} {enc₂ : α → β → List ι} (hinj₁ : Set.InjOn enc₁ (Prod.fst '' D)) (hpf₁ : InformationTheory.IsPrefixFree (enc₁ '' Prod.fst '' D)) (hpf₂ : ∀ (a : α), InformationTheory.IsPrefixFree (enc₂ a '' {b : β | (a, b) ∈ D})) :
InformationTheory.IsPrefixFree ((fun (p : α × β) => enc₁ p.1 ++ enc₂ p.1 p.2) '' D)

Dependent pair encoding (a, b) ↦ enc₁ a ++ enc₂ a b has prefix-free image on D when enc₁ is injective and prefix-free on Prod.fst '' D and enc₂ a is prefix-free on each slice.

theorem Cslib.BitString.isPrefixFree_of_length_eq {ι : Type u_1} {S : Set (List ι)} {k : ℕ} (hlen : ∀ w ∈ S, w.length = k) :

Any set of words of a common fixed length k is prefix-free.

Fixed-width binary index code (finIdxBits) #

Fixed-width binary encoding of i : Fin K in ⌈log₂ K⌉ little-endian bits.

The codeword set of finIdxBits K has uniform length ⌈log₂ K⌉ and is prefix-free.

theorem Cslib.BitString.clog_two_lt_logb_add_one {K : ℕ} (hK : 1 ≤ K) :
↑(Nat.clog 2 K) < Real.logb 2 ↑K + 1

Ceiling slack bound: for 1 ≤ K, ⌈log₂ K⌉ < log₂ K + 1.

Elias gamma positive-integer code (eliasGamma) #

Little-endian-suffix variant of Peter Elias's 1975 gamma code [Eli75] for positive integers m ≥ 1: ⌊log₂ m⌋ leading false bits, a true delimiter bit, and the ⌊log₂ m⌋ low-order binary bits of m in least-significant-first order (lowBits). Unlike Elias's most-significant- first suffix convention (e.g., eliasGamma 6 = [false, false, true, false, true] vs. 00110), using lowBits composes directly with Nat.foldr_bit_testBit while keeping the exact codeword length 2 * ⌊log₂ m⌋ + 1 and prefix-freeness.

Computable left inverse for eliasGamma on positive integers: drops the unary prefix and true delimiter and folds the remaining low-order bits with leading bit 1.

@[simp]

decodeEliasGamma recovers every positive integer m ≥ 1 from eliasGamma m.

eliasGamma is injective on positive integers {m : ℕ | 1 ≤ m}.

Fixed-width encodings of finite structures #

noncomputable def Cslib.BitString.encodeFintype {α : Type u_1} [Fintype α] (x : α) :

Fixed-width binary encoding of a finite type α via finIdxBits.

noncomputable def Cslib.BitString.encodeMemFinset {β : Type u_1} (F : Finset β) (K : ℕ) (x : β) :

Fixed-width encoding of an element x ∈ F of a bounded finite set F (card F ≤ K).

@[simp]
theorem Cslib.BitString.length_encodeMemFinset {β : Type u_1} (F : Finset β) (K : ℕ) (x : β) :
theorem Cslib.BitString.encodeMemFinset_inj {β : Type u_1} {F : Finset β} {K : ℕ} {x₁ x₂ : β} (hx₁ : x₁ ∈ F) (hx₂ : x₂ ∈ F) (hcard : F.card ≤ K) (heq : encodeMemFinset F K x₁ = encodeMemFinset F K x₂) :
x₁ = x₂
noncomputable def Cslib.BitString.encodeRestr {α : Type u_1} {β : Type u_2} [Fintype β] (S : Finset α) (f : α → β) :

Fixed-width encoding of a function on a finite subset S : Finset α with codomain β.

@[simp]
theorem Cslib.BitString.length_encodeRestr {α : Type u_1} {β : Type u_2} [Fintype β] (S : Finset α) (f : α → β) :
theorem Cslib.BitString.length_encodeRestr_bool {α : Type u_1} (S : Finset α) (f : α → Bool) :
theorem Cslib.BitString.length_encodeRestr_fin_two_pow {α : Type u_1} (S : Finset α) (w : ℕ) (f : α → Fin (2 ^ w)) :
theorem Cslib.BitString.encodeRestr_inj {α : Type u_1} {β : Type u_2} [Fintype β] {S₁ S₂ : Finset α} {f₁ f₂ : α → β} (hS : S₁ = S₂) (heq : encodeRestr S₁ f₁ = encodeRestr S₂ f₂) (x : α) :
x ∈ S₁ → f₁ x = f₂ x
theorem Cslib.BitString.encodeRestr_subset_inj {α : Type u_1} {A₁ A₂ S₁ S₂ : Finset α} [DecidablePred fun (x : α) => x ∈ A₁] [DecidablePred fun (x : α) => x ∈ A₂] (hA₁ : A₁ ⊆ S₁) (hA₂ : A₂ ⊆ S₂) (hS : S₁ = S₂) (heq : (encodeRestr S₁ fun (x : α) => decide (x ∈ A₁)) = encodeRestr S₂ fun (x : α) => decide (x ∈ A₂)) :
A₁ = A₂

Restricted characteristic-code injectivity: equal encodeRestr encodings of the indicator functions of A₁ ⊆ S₁ and A₂ ⊆ S₂ over equal carrier sets S₁ = S₂ imply A₁ = A₂.