Documentation

CslibFrontier.Probability.RankDecoder

Decoding integer ranks in a binary probability tree #

A prefix has a natural-number mass, equal to the sum of its two children's masses. A rank strictly below the initial mass selects a leaf by comparing it with the left child's mass and subtracting that mass when taking the right branch. Each leaf has exactly its mass many initial ranks. In particular, a tree of total mass 2 ^ bits is sampled exactly by bits fair bits.

The decoder is an executable deterministic function. The distribution theorems describe its pushforward of an existing uniform source; they do not assert a machine resource bound for the supplied mass function. Zero-mass nodes need no special case and are never reached from a valid rank. Depth zero and a zero-bit seed are included.

Main definitions #

Main results #

Decode remaining bits after pre, querying only the left child's mass at each step. The return value is the new suffix. The function is total; its probability guarantees apply to ranks strictly below the initial mass.

@[simp]
theorem Cslib.BitString.RankDecoder.length_decode (mass : List Bool → ℕ) (remaining : ℕ) (pre : List Bool) (rank : ℕ) :
(decode mass remaining pre rank).length = remaining

Decoding always produces exactly the requested number of bits, even for an invalid rank.

theorem Cslib.BitString.RankDecoder.mass_decode_pos {mass : List Bool → ℕ} {depth : ℕ} (hadd : ∀ (pre : List Bool), pre.length < depth → mass pre = mass (pre ++ [false]) + mass (pre ++ [true])) (remaining : ℕ) (pre : List Bool) (hdepth : pre.length + remaining ≤ depth) (rank : ℕ) (hrank : rank < mass pre) :
0 < mass (pre ++ decode mass remaining pre rank)

A valid rank reaches a positive-mass leaf; zero-mass branches are not sampled.

theorem Cslib.BitString.RankDecoder.card_filter_decode {mass : List Bool → ℕ} {depth : ℕ} (hadd : ∀ (pre : List Bool), pre.length < depth → mass pre = mass (pre ++ [false]) + mass (pre ++ [true])) (remaining : ℕ) (pre : List Bool) (hdepth : pre.length + remaining ≤ depth) (word : List Bool) :
{rank ∈ Finset.range (mass pre) | decode mass remaining pre rank = word}.card = if word.length = remaining then mass (pre ++ word) else 0

The number of valid initial ranks producing a word is its leaf mass. Words of a different length have no preimages. This cardinality statement also permits total mass zero.

theorem Cslib.BitString.RankDecoder.uniformFin_decode_apply {mass : List Bool → ℕ} {depth : ℕ} (hadd : ∀ (pre : List Bool), pre.length < depth → mass pre = mass (pre ++ [false]) + mass (pre ++ [true])) (remaining : ℕ) (pre : List Bool) (hdepth : pre.length + remaining ≤ depth) {size : ℕ} [NeZero size] (hroot : mass pre = size) (word : List Bool) :
(PMF.map (fun (rank : Fin size) => decode mass remaining pre ↑rank) (Probability.PMF.uniformOfFintype (Fin size))) word = if word.length = remaining then ↑(mass (pre ++ word)) / ↑size else 0

Decoding a uniform finite rank gives each leaf its integer mass divided by the total mass.

theorem Cslib.BitString.RankDecoder.uniformList_decode_apply {mass : List Bool → ℕ} {depth : ℕ} (hadd : ∀ (pre : List Bool), pre.length < depth → mass pre = mass (pre ++ [false]) + mass (pre ++ [true])) (bits : ℕ) (hroot : mass [] = 2 ^ bits) (word : List Bool) :
(PMF.map (fun (seed : List Bool) => decode mass depth [] (binaryValue seed)) (uniformList bits)) word = if word.length = depth then ↑(mass word) / ↑(2 ^ bits) else 0

A tree with root mass 2 ^ bits is sampled exactly by bits fair bits followed by the integer decoder. The statement includes zero bits and depth zero.