Documentation

Examples.Ram.TriangularLoop

A nested countdown loop with a proved quadratic machine budget #

The fixed program reads n, executes inner loops of lengths n, n-1, ..., 1, and outputs their total. Mathematical finite sums occur only in specifications and proofs. The executed accumulator is incremented by actual RAM operations. The inner loop uses the linear rule; the outer loop uses the finite-sum rule because its body budget depends on the current counter.

Equations
Instances For
    theorem Ram.Examples.TriangularLoop.tri_pred {n : ℕ} (hn : 0 < n) :
    tri n = tri (n - 1) + n
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Ram.Examples.TriangularLoop.inner_body_contract {w : ℕ} (s : Source.State w) :
        Source.Contract 3 [] 0 0 innerBody (fun (t : Source.State w) => t = s) (fun (t : Source.State w) => t = innerNext s) fun (x : Source.State w) => 8
        theorem Ram.Examples.TriangularLoop.inner_next_preserves {w : ℕ} (hw : 0 < w) (entry s : Source.State w) (hfit : BitVec.toNat (entry.regs 2) + BitVec.toNat (entry.regs 1) < 2 ^ w) (hs : InnerInv entry s) (hz : s.eval innerCondition ≠ 0) :
        theorem Ram.Examples.TriangularLoop.inner_contract {w : ℕ} (hw : 0 < w) (entry : Source.State w) (hfit : BitVec.toNat (entry.regs 2) + BitVec.toNat (entry.regs 1) < 2 ^ w) :
        Source.Contract 3 [] 0 0 innerLoop (fun (s : Source.State w) => s = entry) (InnerPost entry) fun (x : Source.State w) => 11 * BitVec.toNat (entry.regs 1) + 2

        The inner loop adds its entry count to the accumulator without wrapping, and preserves the outer counter and I/O.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Ram.Examples.TriangularLoop.outer_body_contract {w : ℕ} (n : ℕ) (hw : 0 < w) (htri : tri n < 2 ^ w) (s : Source.State w) (hs : OuterInv n s) (hz : s.eval outerCondition ≠ 0) :
          Source.Contract 3 [] 0 0 outerBody (fun (t : Source.State w) => t = s) (fun (t : Source.State w) => OuterInv n t ∧ BitVec.toNat (t.regs 0) < BitVec.toNat (s.regs 0)) fun (x : Source.State w) => 11 * BitVec.toNat (s.regs 0) + 8

          A complete outer iteration has a nonconstant body budget: the current outer counter is copied to the inner counter and determines the inner work.

          theorem Ram.Examples.TriangularLoop.outer_contract {w : ℕ} (n : ℕ) (hw : 0 < w) (htri : tri n < 2 ^ w) :
          Source.Contract 3 [] 0 0 outerLoop (OuterInv n) (fun (t : Source.State w) => OuterInv n t ∧ t.eval outerCondition = 0) fun (s : Source.State w) => 11 * tri (BitVec.toNat (s.regs 0)) + 11 * BitVec.toNat (s.regs 0) + 2
          theorem Ram.Examples.TriangularLoop.main_contract {w : ℕ} (n : ℕ) (hw : 0 < w) (hn : n < 2 ^ w) (htri : tri n < 2 ^ w) :
          Source.Contract 3 [] 0 0 main (InputReady n) (Post n) fun (x : Source.State w) => 11 * tri n + 11 * n + 7
          theorem Ram.Examples.TriangularLoop.machine_correct {w : ℕ} (n : ℕ) (hw : 0 < w) (hn : n < 2 ^ w) (htri : tri n < 2 ^ w) (hcodefit : List.length machine < 2 ^ w) :
          ∃ (finish : State w), TerminatesWithin machine (11 * tri n + 11 * n + 9) (State.initial [0, BitVec.ofNat w n]) finish ∧ finish.output = [BitVec.ofNat w (tri n)] ∧ finish.input = []

          The complete checked program outputs the mathematical triangular number. The explicit affine-in-the-sum bound includes its input, output, prologue, and halt.

          theorem Ram.Examples.TriangularLoop.machine_quadratic {w : ℕ} (n : ℕ) (hw : 0 < w) (hn : n < 2 ^ w) (htri : tri n < 2 ^ w) (hcodefit : List.length machine < 2 ^ w) :
          ∃ (finish : State w), TerminatesWithin machine (11 * (n * n) + 11 * n + 9) (State.initial [0, BitVec.ofNat w n]) finish ∧ finish.output = [BitVec.ofNat w (tri n)] ∧ finish.input = []

          A polynomial upper bound follows from mathlib's finite-sum comparison; no closed form with natural-number division is needed for the algorithm proof.

          The published task takes the numeric value n as its size, not its binary encoding length. Its fixed minimum word width and representable mathematical input/output domain do not refer to any submission or instruction-list size. The input contains only the zero heap header and n, never a precomputed sum.

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

            One fixed executable solves every legal input under a concrete quadratic transition budget, retaining its mathematical result and termination.

            Equations
            Instances For
              theorem Ram.Examples.TriangularLoop.budget_isBigO :
              (fun (n : ℕ) => ↑(11 * (n * n) + 11 * n + 9)) =O[Filter.atTop] fun (n : ℕ) => ↑((n + 1) ^ 2)

              Analyze the proved natural machine bound with the main library's mathlib bridge. The coefficient is uniform over every input and legal word width.

              A genuine mathlib-backed O((n+1)^2) certificate for this same machine on the unbounded published domain. This is a numeric-size word-RAM claim, not a bit-complexity or bit-linear claim.

              Equations
              Instances For