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:
Cslib.BitString.finIdxBits: fixed-width⌈log₂ K⌉-bit little-endian encoding ofFin K.Cslib.BitString.eliasGammaandCslib.BitString.decodeEliasGamma: a little-endian-suffix variant of Peter Elias's 1975 gamma code [Eli75] for positive integersm ≥ 1. It emits⌊log₂ m⌋leadingfalsebits, atruedelimiter bit, and the⌊log₂ m⌋low-order binary bits ofmleast-significant bit first (viaCslib.BitString.lowBits, whereas Elias [Eli75] orders the suffix bits most-significant bit first, e.g.,eliasGamma 6 = [false, false, true, false, true]vs.00110), preserving the exact codeword length2 * ⌊log₂ m⌋ + 1, unique decodability on{m | 1 ≤ m}, and prefix-freeness.
Main definitions #
Cslib.BitString.finIdxBits: fixed-width⌈log₂ K⌉-bit binary encoding ofFin K.Cslib.BitString.eliasGamma,Cslib.BitString.decodeEliasGamma: little-endian-suffix variant of the Elias gamma code [Eli75] of length2 * ⌊log₂ m⌋ + 1and its left inverse onm ≥ 1.Cslib.BitString.encodeFintype,Cslib.BitString.encodeMemFinset,Cslib.BitString.encodeRestr: canonical binary encodings for finite types, membership, and restricted maps.
Main results #
InformationTheory.IsPrefixFree.bind_append,Cslib.BitString.prefix_append_of_length_eq,Cslib.BitString.prefix_append_of_isPrefixFree,Cslib.BitString.isPrefixFree_biUnion_append,Cslib.BitString.isPrefixFree_image_append,Cslib.BitString.isPrefixFree_prod_append,Cslib.BitString.injOn_append,Cslib.BitString.injOn_prod_append: sequential dependent concatenation of prefix-free codes remains prefix-free and uniquely decomposable; equal-length or prefix-free heads preserve prefix relations on tails.Cslib.BitString.finIdxBits_injective,Cslib.BitString.isPrefixFree_range_finIdxBits,Cslib.BitString.clog_two_lt_logb_add_one: fixed-width index code properties and ceiling slack(Nat.clog 2 K : ℝ) < Real.logb 2 K + 1.Cslib.BitString.encodeFintype,Cslib.BitString.encodeMemFinset,Cslib.BitString.encodeRestr: canonical binary encodings, exact lengths, and injectivity for finite types, subsets, and restricted maps (encodeFintype_injective,encodeMemFinset_inj,encodeRestr_inj,encodeRestr_subset_inj).Cslib.BitString.decodeEliasGamma_eliasGamma,Cslib.BitString.injOn_eliasGamma,Cslib.BitString.isPrefixFree_range_eliasGamma,Cslib.BitString.isPrefixFree_eliasGamma: codeword length, left-invertibility, injectivity on{m | 1 ≤ m}, and prefix-freeness ofeliasGamma.
References #
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 #
Equal concatenations with prefixes drawn from a prefix-free set have equal prefix and suffix.
A prefix of concatenations whose heads are drawn from a prefix-free set forces equal heads and a prefix relation on the tails.
Finite Finset.biUnion form of IsPrefixFree.bind_append.
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.
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.
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.
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.
Fixed-width binary index code (finIdxBits) #
finIdxBits K is injective on Fin K.
The codeword set of finIdxBits K has uniform length ⌈log₂ K⌉ and is prefix-free.
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.
decodeEliasGamma recovers every positive integer m ≥ 1 from eliasGamma m.
eliasGamma is injective on positive integers {m : ℕ | 1 ≤ m}.
The full range of eliasGamma is prefix-free.
eliasGamma '' {m : ℕ | 1 ≤ m} is prefix-free.
Fixed-width encodings of finite structures #
Fixed-width binary encoding of a finite type α via finIdxBits.
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₂.