The closure library, part IV: binary numbers #
Programs on binary numbers presented as bit strings (LSB first, as in Nat.bits), used by
the bit-query program of the recursive compression argument: the zero test
(isZeroProg) and decrement (decProg). Decrement flips the leading falses to true
and the first true to false; it may leave a non-canonical representation (trailing
falses), which is why the specifications are in terms of the value bitsVal of a bit
string rather than Nat.bits.
Costs: every list walk is quadratic in this model (the loop state is rebuilt each
iteration, copying the tail), so bounds have the form (iterations + 1) · (S + c) with S
dominating the sizes involved.
Bit strings as data #
The value of a bit string (LSB first), canonical or not.
Equations
- MIPRE.Cost.bitsVal l = List.foldr Nat.bit 0 l
Instances For
Zero test #
Body of isZeroProg: walk the bits; stop with nil (zero) at the end, with
cons nil nil (nonzero) at the first true bit.
Equations
- One or more equations did not get rendered due to their size.
Instances For
isZeroProg on a bit string computes encode (l.any id): nil iff the value is zero.
Instances For
Decrement #
Body of the first phase of decProg: on state cons bits acc, a leading false bit
is turned into a true pushed on acc; at the first true bit the loop stops with the pair
cons (false :: acc) rest, which the second phase (revOntoProg) reverses onto rest.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The first phase of decProg on false^j ++ true :: rest with accumulator true^i. The
slack S must cover the growth of the accumulator: each converted bit adds two nodes.
The next level index #
n ↦ 2 n + 1 on binary numerals: prepend a true bit. This is the "next level"
map of the recursive compression argument (the paper's n + 1 would need a carry
propagation; 2 n + 1 is one node).
Equations
- One or more equations did not get rendered due to their size.