The closure library, part III: unary arithmetic and tree size #
Programs on unary numerals (Data.ofNat) built from the length loop of Cost.Loops:
addition (addProg), the bit string of a power of two (expBitsProg), and the size of a
tree in unary (sizeProg, a stack-driven traversal proved with Eval.loop_of_invariant).
These are the ingredients of the threshold function r e = 2 ^ q(esize e) of the recursive
compression argument (planning/compression-track.md, K3 step 4); multiplication and powers
follow in Cost.Numeric.
Sum of the sizes of the elements of a list.
Equations
Instances For
The measure of the tree-size loop: the node sum of the stack component.
Equations
- (st.cons a).stackMeasure = st.nodeSum
- MIPRE.Cost.Data.nil.stackMeasure = 0
Instances For
Addition of unary numerals #
addProg on cons (ofNat a) (ofNat b) computes ofNat (a + b): it is the length
loop with the second numeral as accumulator.
Instances For
Powers of two, as bit strings #
expBitsProg on the unary numeral ofNat j computes encode (2 ^ j), the bit string
false^j ++ [true].
Equations
- One or more equations did not get rendered due to their size.
Instances For
Size of a tree, in unary #
Body of sizeProg: the state is cons stack count with stack a list of pending
subtrees and count a unary numeral. Pop a subtree: if it is nil, count it; if it is
cons a b, count it and push a and b. Stop with count when the stack is empty.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The invariant of the tree-size loop for a tree of size N: the state is
cons stack count with count + Σ size (stack) = N.
Equations
- MIPRE.Cost.Prog.sizeInv N s = ∃ (st : List MIPRE.Cost.Data) (k : ℕ), s = (MIPRE.Cost.Data.list st).cons (MIPRE.Cost.Data.ofNat k) ∧ k + MIPRE.Cost.Data.sumSize st = N