Self-delimiting natural-number encoding #
A terminated-unary prefix code for natural numbers. It extends Mathlib's unary encoding with one false terminator, allowing a decoder to recover one number and leave the remaining bits untouched.
Encode value as value true bits followed by one false terminator. The
terminator makes the code self-delimiting inside a larger bitstring.
Equations
Instances For
Decode one terminated-unary prefix and return the unused suffix. A bitstring containing only true bits is malformed because it has no terminator.
Equations
- ComplexityTheory.NatPrefixCode.decodePrefix? [] = none
- ComplexityTheory.NatPrefixCode.decodePrefix? (false :: suffix) = some (0, suffix)
- ComplexityTheory.NatPrefixCode.decodePrefix? (true :: bits) = do let __x ← ComplexityTheory.NatPrefixCode.decodePrefix? bits match __x with | (value, suffix) => some (value + 1, suffix)
Instances For
Decode exactly one natural number. Unlike decodePrefix?, this rejects every
nonempty trailing suffix.
Equations
Instances For
An encoded natural occupies exactly one bit per unary unit plus its terminator. This equation is the accounting rule used by larger encodings.
Prefix decoding reverses encoding without consuming a caller-provided suffix. This is the compositional round-trip property needed by token decoders.
An unterminated run of true bits is rejected. The decoder therefore fails closed on truncated codes rather than inventing a value.
The terminated-unary functions form a Mathlib-compatible binary encoding.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Canonical terminated-unary encodings are unambiguous.