Canonical variable-width binary natural-number codes #
This module packages Nat.bits as a canonical binary code for natural
numbers. Bits are ordered least-significant first, and redundant high zeroes
are rejected. In particular, zero has the unique empty code.
Unlike a prefix code, this representation relies on an enclosing framing
layer to determine the field boundary. Complexitylib.Encoding.Pairing
provides that framing for variable-length machine inputs.
Encode a natural by its minimal little-endian binary expansion.
Equations
- Complexity.BinaryNatCode.encode value = value.bits
Instances For
Decode a natural only when the supplied bits are its minimal expansion.
This rejects every redundant high-zero representation, making encode and
decode? an exact codec rather than merely a value interpretation.
Equations
- Complexity.BinaryNatCode.decode? bits = if (Nat.fromBitsLE bits).bits = bits then some (Nat.fromBitsLE bits) else none
Instances For
The canonical binary natural-number encoding is injective.