The successor, predecessor and zero test in binary #
encNat, reexported from Encodings.Nat.Bits, encodes a natural number by its binary digits,
least significant first (Nat.bits).
Each of the three counter operations is computed by one streaming transducer that reads its input
once and uses no work tape:
- the successor's state is the carry: on each digit
bin carrycit emitsc ^^ band keeps the carryc && b, and at the end it emits a final1exactly when a carry remains; - the predecessor turns the borrowed
0s into1s and the first1into a0, which it holds back until a further digit shows that it is not the leading digit, and then copies the rest; - the zero test emits whether the input was empty.
Main results #
Turing.MultiTapeTM.computableInTimeAndSpace_succ,computableInTimeAndSpace_pred,computableInTimeAndSpace_isZero: the operations are computable within2 * (encNat n).length + 3steps and zero work space.Turing.MultiTapeTM.ComputableInDegree.natSucc,.natPred,.natIsZero: their linear forms.
Binary successor in linear time and zero work space. One streaming transducer computes
n + 1 from encNat n within 2 * (encNat n).length + 3 steps without work tapes.
Binary successor in linear time and work space, from computableInTimeAndSpace_succ.
Binary predecessor in linear time and zero work space. One streaming transducer computes
n - 1 (so 0 ↦ 0) from encNat n within 2 * (encNat n).length + 3 steps without work
tapes.
Binary predecessor in linear time and work space, from computableInTimeAndSpace_pred.
Binary zero test in linear time and zero work space. One streaming transducer decides
n = 0 from encNat n within 2 * (encNat n).length + 3 steps without work tapes.
Binary zero test in linear time and work space, from computableInTimeAndSpace_isZero.