Explicit binary codes for finite alphabets #
An enumeration assigns index i to the palindromic codeword false, true^i, false. Concatenation encodes words, including the empty word. Decoding rejects
missing delimiters and indices outside the enumeration. Reversing an encoding
therefore reverses its letters without changing their codes.
Choose a code from a finite-alphabet instance.
Equations
- Complexity.FiniteAlphabetCode.ofFintype α = { size := Fintype.card α, index := Fintype.equivFin α }
Instances For
Concatenate the symbol codes of a word.
Equations
- c.encode w = List.flatMap c.symbol w
Instances For
Decode a suffix while waiting for an opening delimiter or counting an open codeword.
Equations
- c.decodeFrom none [] = some []
- c.decodeFrom (some val) [] = none
- c.decodeFrom none (false :: bs) = c.decodeFrom (some 0) bs
- c.decodeFrom none (true :: tail) = none
- c.decodeFrom (some i) (true :: bs) = if i + 1 < c.size then c.decodeFrom (some (i + 1)) bs else none
- c.decodeFrom (some i) (false :: bs) = if h : i < c.size then Option.map (fun (x : List α) => c.index.symm ⟨i, h⟩ :: x) (c.decodeFrom none bs) else none
Instances For
Decode an entire word, rejecting every malformed code.
Equations
- c.decode bits = c.decodeFrom none bits
Instances For
def
Complexity.FiniteAlphabetCode.map
{α β : Type}
(input : FiniteAlphabetCode α)
(output : FiniteAlphabetCode β)
(f : List α → List β)
(bits : List Bool)
:
Encode a function's result on valid inputs and return empty output on invalid codes.