Documentation

Examples.Ram.BitLength

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.

One program, independent of both the input value and its word width.

Equations
Instances For
    theorem Ram.Examples.BitLength.body_total {w H depth : ℕ} (s : Source.State w) :
    Source.TotalContract [] H depth body (fun (t : Source.State w) => t = s) fun (t : Source.State w) => t = bodyResult s

    The actual assignments implement one division and one answer increment, without selecting an instruction budget.

    theorem Ram.Examples.BitLength.body_contract {w H depth : ℕ} (s : Source.State w) :
    Source.Contract 2 [] H depth body (fun (t : Source.State w) => t = s) (fun (t : Source.State w) => t = bodyResult s) fun (x : Source.State w) => 8

    These two actual assignments compile to eight primitive instructions.

    Equations
    Instances For
      Equations
      Instances For
        Equations
        Instances For
          def Ram.Examples.BitLength.Post {w : ℕ} (input : ℕ) (s : Source.State w) :
          Equations
          Instances For
            theorem Ram.Examples.BitLength.answer_le_width {w input : ℕ} (hinput : input < 2 ^ w) :
            Nat.clog 2 (input + 1) ≤ w

            The result bound uses mathlib's logarithm/power adjunction directly.

            theorem Ram.Examples.BitLength.answer_fits {w input : ℕ} (hinput : input < 2 ^ w) :
            Nat.clog 2 (input + 1) < 2 ^ w

            The mathematical state consists of the remaining value and accumulated answer.

            Equations
            Instances For

              The final answer, expressed directly with mathlib's logarithm.

              Equations
              Instances For
                theorem Ram.Examples.BitLength.modelResult_step (state : ℕ × ℕ) (positive : 0 < state.1) :

                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.

                  theorem Ram.Examples.BitLength.bodyResult_rep {w : ℕ} (hw : 2 ≤ w) {state : ℕ × ℕ} (positive : 0 < state.1) (fits : modelResult state < 2 ^ w) (s : Source.State w) (represented : ModelRep state s) :

                  The word adapter proves the real body implements the mathematical step. Its only arithmetic-specific obligations are faithful division and no wrap.

                  theorem Ram.Examples.BitLength.bodyResult_preserves {w input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) (s : Source.State w) (hs : Invariant input s) (hz : s.eval condition ≠ 0) :

                  One positive iteration preserves the answer equation. A positive remaining logarithm guarantees room for the actual increment before it is executed.

                  theorem Ram.Examples.BitLength.loop_refines {w H depth : ℕ} (hw : 2 ≤ w) :
                  Source.Refines [] H depth loop (fun (state : ℕ × ℕ) (s : Source.State w) => ModelRep state s ∧ modelResult state < 2 ^ w) (fun (answer : ℕ) (s : Source.State w) => BitVec.toNat (s.regs 1) = answer ∧ s.input = [] ∧ s.outputRev = []) modelResult

                  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.

                  theorem Ram.Examples.BitLength.loop_total {w H depth input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) :

                  Recover the original invariant-based functional API from the model refinement.

                  theorem Ram.Examples.BitLength.loop_timeBound {w H depth input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) :
                  Source.TimeBound 2 [] H depth loop (Invariant input) fun (x : Source.State w) => 11 * Nat.clog 2 (input + 1) + 2

                  The cost proof is independent of the loop's final-answer specification. The shared logarithmic rule accounts for every guard, body and back-edge.

                  theorem Ram.Examples.BitLength.loop_contract {w H depth input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) :
                  Source.Contract 2 [] H depth loop (Invariant input) (AnswerReady input) fun (x : Source.State w) => 11 * Nat.clog 2 (input + 1) + 2

                  The original exact public budget combines separate model correctness and machine-time proofs for the same loop.

                  theorem Ram.Examples.BitLength.main_contract {w H depth input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) :
                  Source.Contract 2 [] H depth main (InputReady input) (Post input) fun (x : Source.State w) => 11 * Nat.clog 2 (input + 1) + 7

                  The I/O adapters and initialization are included in the source contract; no host-side initialization or uncharged output conversion is assumed.

                  theorem Ram.Examples.BitLength.machine_correct {w input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) (hcodefit : 18 < 2 ^ w) :
                  ∃ (finish : State w), TerminatesWithin machine (11 * Nat.clog 2 (input + 1) + 9) (State.initial [0, BitVec.ofNat w input]) finish ∧ finish.output = [BitVec.ofNat w (Nat.clog 2 (input + 1))] ∧ finish.input = []

                  Full actual-machine logarithmic time, including the header read and halt. The empty source heap is represented by the counted zero boundary header.

                  theorem Ram.Examples.BitLength.machine_correct_decoded {w input : ℕ} (hw : 2 ≤ w) (hinput : input < 2 ^ w) (hcodefit : 18 < 2 ^ w) :
                  ∃ (finish : State w), TerminatesWithin machine (11 * Nat.clog 2 (input + 1) + 9) (State.initial [0, BitVec.ofNat w input]) finish ∧ List.map BitVec.toNat finish.output = [Nat.clog 2 (input + 1)] ∧ finish.input = []

                  Decoding the emitted word returns the actual natural ceiling logarithm, not merely a modular encoding of an out-of-range answer.