Documentation

Complexitylib.Encoding.Delimit

Self-delimiting blocks #

To concatenate binary strings into a single binary string, each piece must announce its own end. This file defines the library's single framing operation and its parsers:

This file deliberately has no dependency on the machine or complexity-class layers, so the machine-input pairing codec (Complexitylib.Encoding.Pairing) can build on it without import cycles.

Frame a binary string as a self-delimiting block: each payload bit is doubled (false ↦ [false, false], true ↦ [true, true]) and the block is terminated by the separator [false, true], which no run of doubled bits can produce.

Equations
Instances For
    @[simp]
    theorem Complexity.delimit_cons (b : Bool) (l : List Bool) :
    delimit (b :: l) = b :: b :: delimit l
    @[simp]

    Parse one self-delimiting block off the front of the input. It scans doubled bits until the first separator [false, true], returning the decoded payload together with the remaining suffix. Invalid doubled prefixes return none.

    Equations
    Instances For
      @[simp]

      unpair? reads back the framing written by delimit: parsing one block off the front of any input recovers the payload and the remaining suffix.

      Soundness of the parser: a successful parse decomposes the input as the parsed payload's framing followed by the leftover suffix.

      Strip the framing of a single self-delimiting block, returning its payload. On delimit P this returns P. Unlike unpair?, this is total: it ignores any data trailing the first block and maps malformed input to [].

      Equations
      Instances For

        Keep the leading self-delimiting block of a bitstring, dropping everything after it. On a pair encoding delimit x ++ w this returns delimit x.

        Equations
        Instances For

          Does the bitstring begin with a well-formed self-delimiting block?

          Equations
          Instances For

            Tag a bitstring with a leading true if it begins with a well-formed self-delimiting block, and return the empty bitstring otherwise. On pair encodings this computes encode ∘ decode.

            Equations
            Instances For

              Parse a sequence of self-delimiting blocks, using fuel to bound the number of blocks.

              This is the auxiliary, fuel-carrying implementation of undelimitBlocks; since every block is nonempty, input.length is always enough fuel.

              Equations
              Instances For

                Parse a sequence of self-delimiting blocks off the front of the input.

                Since every block is nonempty, input.length bounds the number of blocks, so it always suffices as fuel for undelimitBlocksAux.

                Equations
                Instances For