A bit-length program with a logarithmic machine budget #
This fixed program reads one word, repeatedly divides it by two, increments
an answer register, and writes the answer. Its mathematical result is directly
mathlib's Nat.clog 2 (input + 1), including zero input. There is no separate
bit-length function or input-dependent unrolling in the source language.
The body's functional behavior and instruction bound are proved separately.
Loop correctness is transferred from an ordinary pair of naturals by
Refines.while_wellFounded; the result equation is a mathlib logarithm identity,
independent of machine states. A shared dividing-variant rule supplies the
separate time bound of that same execution. The word
adapter proves that division is faithful and the answer increment cannot wrap.
The complete bound includes input, initialization, every evaluated guard, each division and increment, all back edges, output, the header read and halt. It counts word-RAM transitions, not bit-operation costs of arithmetic.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
One program, independent of both the input value and its word width.
Equations
Instances For
Instances For
The actual assignments implement one division and one answer increment, without selecting an instruction budget.
These two actual assignments compile to eight primitive instructions.
Equations
Instances For
Equations
Instances For
The functional invariant is ordinary arithmetic, without registers or words.
The two live registers represent an ordinary pair; I/O is preserved.
Equations
Instances For
This is a statement about the concrete word division, not an assumed decrease of a mathematical ghost counter. Width at least two represents the divisor two faithfully.
The word adapter proves the real body implements the mathematical step. Its only arithmetic-specific obligations are faithful division and no wrap.
One positive iteration preserves the answer equation. A positive remaining logarithm guarantees room for the actual increment before it is executed.
Functional loop verification uses a natural-pair model and the ordinary strict order on its remaining value. It has no time budget or execution tree.
Recover the original invariant-based functional API from the model refinement.
The cost proof is independent of the loop's final-answer specification. The shared logarithmic rule accounts for every guard, body and back-edge.
The original exact public budget combines separate model correctness and machine-time proofs for the same loop.
The I/O adapters and initialization are included in the source contract; no host-side initialization or uncharged output conversion is assumed.