Documentation

Examples.Ram.ContractFill

An input/output array-fill program verified with contracts #

The fixed program reads a length, fills that many heap words with one, and outputs the length. The loop proof uses the public invariant/potential rule; it does not construct an execution derivation separately for each input.

The contracts use the optimized local-frame compiler and its measured execution judgment. The linear budget is derived from that pipeline's generated instructions. The final checked-compiler theorems give actual halted machine execution and matching output. The heap-preserving interface additionally transfers the filled-prefix assertion to the target machine's own memory, using the explicit address bound.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    One fixed program for every length and every admissible word width.

    Equations
    Instances For
      @[simp]
      theorem Ram.Examples.ContractFill.bodyResult_mem {w : ℕ} (s : Source.State w) (a : Word w) :
      (bodyResult s).mem a = if a = s.regs 1 then 1 else s.mem a

      The pointer marks the filled prefix; pointer plus remaining count is the original logical length. These are ghost assertions, not runtime operations.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        Equations
        Instances For
          theorem Ram.Examples.ContractFill.body_contract {w H depth : ℕ} (s : Source.State w) (haddr : BitVec.toNat (s.regs 1) < H) :
          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) => 11

          Three ordinary primitive contracts compose into the exact state transformer and an eleven-transition budget.

          theorem Ram.Examples.ContractFill.bodyResult_preserves {w H len : ℕ} (hw : 0 < w) (hlen : len ≤ H) (hfit : H < 2 ^ w) (s : Source.State w) (hs : Invariant len s) (hz : s.eval condition ≠ 0) :
          theorem Ram.Examples.ContractFill.loop_contract {w H len : ℕ} (hw : 0 < w) (hlen : len ≤ H) (hfit : H < 2 ^ w) :

          Invariant preservation and decreasing remaining budget establish both termination and a linear bound, through the public loop contract rule.

          theorem Ram.Examples.ContractFill.main_contract {w H len : ℕ} (hw : 0 < w) (hlen : len ≤ H) (hfit : H < 2 ^ w) :
          Source.Contract 2 [] H 0 main (InputReady len) (Post len) fun (x : Source.State w) => 14 * len + 7

          Full source application: input read, pointer initialization, fill loop, and output write, all included in the proved budget.

          theorem Ram.Examples.ContractFill.machine_correct {w H len : ℕ} (hw : 0 < w) (hlen : len ≤ H) (hfit : H < 2 ^ w) (hcodefit : List.length machine < 2 ^ w) :
          ∃ (sourceFinal : Source.State w) (targetFinal : State w), Post len sourceFinal ∧ TerminatesWithin machine (14 * len + 9) (State.initial [BitVec.ofNat w H, BitVec.ofNat w len]) targetFinal ∧ targetFinal.output = [BitVec.ofNat w len] ∧ targetFinal.input = []

          Checked compilation executes this same fixed application in at most 14 * len + 9 real RAM transitions, including its prologue and halt.

          theorem Ram.Examples.ContractFill.machine_heap_correct {w H len : ℕ} (hw : 0 < w) (hlen : len ≤ H) (hfit : H < 2 ^ w) (hcodefit : List.length machine < 2 ^ w) :
          ∃ (targetFinal : State w), TerminatesWithin machine (14 * len + 9) (State.initial [BitVec.ofNat w H, BitVec.ofNat w len]) targetFinal ∧ (∀ i < len, targetFinal.mem (BitVec.ofNat w i) = 1) ∧ targetFinal.output = [BitVec.ofNat w len] ∧ targetFinal.input = []

          The compiled machine itself fills the array, not merely its source model. Every observed address is proved to lie in the source heap represented by the compiler's heap correspondence. The runtime bound still counts the complete execution, including input, output, prologue, and halt.