Pairing binary strings #
This file defines the low-level self-delimiting pairing codec used by machine inputs throughout Complexitylib. It deliberately has no dependency on the machine or complexity-class layers, so parsers and encoders can reuse it without introducing an import cycle.
Encode a pair of binary strings as a single binary string.
Each bit of x is doubled (false ↦ [false, false], true ↦ [true, true]),
followed by the separator [false, true], followed by y verbatim.
This encoding is injective and computable in linear time.
Instances For
Partial inverse to pair. It scans doubled bits until the first
separator [false, true], returning the decoded left component together
with the remaining suffix. Invalid doubled prefixes return none.
Equations
- Complexity.unpair? [] = none
- Complexity.unpair? (false :: true :: y) = some ([], y)
- Complexity.unpair? (false :: false :: z) = Option.map (fun (xy : List Bool × List Bool) => (false :: xy.fst, xy.snd)) (Complexity.unpair? z)
- Complexity.unpair? (true :: true :: z) = Option.map (fun (xy : List Bool × List Bool) => (true :: xy.fst, xy.snd)) (Complexity.unpair? z)
- Complexity.unpair? x✝ = none