Documentation

ComplexityTheory.Foundations.NatEncoding

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
    Instances For

      Decode exactly one natural number. Unlike decodePrefix?, this rejects every nonempty trailing suffix.

      Equations
      Instances For
        @[simp]

        An encoded natural occupies exactly one bit per unary unit plus its terminator. This equation is the accounting rule used by larger encodings.

        @[simp]

        Prefix decoding reverses encoding without consuming a caller-provided suffix. This is the compositional round-trip property needed by token decoders.

        @[simp]

        Exact decoding reverses canonical encoding. In plain language, every natural number survives an encode-decode round trip.

        theorem ComplexityTheory.NatPrefixCode.decode?_encode_append_eq_none (value : ) {suffix : BitString} (hsuffix : suffix []) :
        decode? (encode value ++ suffix) = none

        A nonempty suffix makes an otherwise canonical code invalid for exact decoding. This prevents two adjacent objects from being mistaken for one.

        @[simp]

        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.