Documentation

Examples.Ram.AmortizedClear

A real heap workload with amortized linear machine time #

The input is a command count followed by Boolean words. One means push a cell containing one; zero means clear, by visiting and zeroing every active cell. The final stack height is written to output. The source program is fixed and contains both loops: no command or heap operation is performed by the host.

The mathematical specification folds the commands over the stack height and describes every heap cell below the declared boundary. Potential is ten times the current height. It pays for expensive clear commands using credit retained by earlier pushes. Budgets count actual compiled word-RAM transitions.

Pure specification only; execution consumes the actual input words.

Equations
Instances For
    @[simp]
    theorem Ram.Examples.AmortizedClear.result_cons (b : Bool) (rest : List Bool) (top : ℕ) :
    result (b :: rest) top = result rest (advance b top)
    theorem Ram.Examples.AmortizedClear.result_le (commands : List Bool) (top : ℕ) :
    result commands top ≤ top + commands.length
    Equations
    Instances For
      @[simp]
      theorem Ram.Examples.AmortizedClear.encode_cons {w : ℕ} (b : Bool) (rest : List Bool) :
      encode (b :: rest) = (if b = true then 1 else 0) :: encode rest
      def Ram.Examples.AmortizedClear.Prefix {w : ℕ} (H top : ℕ) (mem : Word w → Word w) :

      Active cells contain one; all other allocated cells contain zero.

      Equations
      Instances For
        theorem Ram.Examples.AmortizedClear.Prefix.push {w H top : ℕ} {mem : Word w → Word w} (h : Prefix H top mem) (hH : H < 2 ^ w) (htop : top < H) :
        Prefix H (top + 1) fun (a : Word w) => if a = BitVec.ofNat w top then 1 else mem a
        theorem Ram.Examples.AmortizedClear.Prefix.pop {w H top : ℕ} {mem : Word w → Word w} (h : Prefix H top mem) (hH : H < 2 ^ w) (hpos : 0 < top) (htop : top ≤ H) :
        Prefix H (top - 1) fun (a : Word w) => if a = BitVec.ofNat w (top - 1) then 0 else mem a
        theorem Ram.Examples.AmortizedClear.push_contract {w H depth : ℕ} (s : Source.State w) (htop : BitVec.toNat (s.regs 1) < H) :
        Source.Contract 3 [] H depth push (fun (t : Source.State w) => t = s) (fun (t : Source.State w) => t = pushResult s) fun (x : Source.State w) => 7
        theorem Ram.Examples.AmortizedClear.pop_contract {w H depth : ℕ} (hw : 0 < w) (s : Source.State w) (hp : 0 < BitVec.toNat (s.regs 1)) (htop : BitVec.toNat (s.regs 1) ≤ H) :
        Source.Contract 3 [] H depth clearBody (fun (t : Source.State w) => t = s) (fun (t : Source.State w) => t = popResult s) fun (x : Source.State w) => 7
        Instances For
          structure Ram.Examples.AmortizedClear.ClearPost {w : ℕ} (H : ℕ) (entry s : Source.State w) :
          Instances For
            theorem Ram.Examples.AmortizedClear.clear_contract {w H depth : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (entry : Source.State w) (htop : BitVec.toNat (entry.regs 1) ≤ H) (hheap : Prefix H (BitVec.toNat (entry.regs 1)) entry.mem) :
            Source.Contract 3 [] H depth clear (fun (s : Source.State w) => s = entry) (ClearPost H entry) fun (x : Source.State w) => 10 * BitVec.toNat (entry.regs 1) + 2

            Clearing performs an actual store for each old cell, preserves the other live state, and costs at most ten transitions per cell plus its final guard.

            structure Ram.Examples.AmortizedClear.CommandPost {w : ℕ} (H : ℕ) (b : Bool) (entry s : Source.State w) :
            Instances For
              theorem Ram.Examples.AmortizedClear.pushResult_post {w H : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (s : Source.State w) (htop : BitVec.toNat (s.regs 1) < H) (hheap : Prefix H (BitVec.toNat (s.regs 1)) s.mem) :
              theorem Ram.Examples.AmortizedClear.command_contract {w H depth : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (b : Bool) (s : Source.State w) (hc : s.regs 2 = if b = true then 1 else 0) (htop : BitVec.toNat (s.regs 1) < H) (hheap : Prefix H (BitVec.toNat (s.regs 1)) s.mem) :
              Source.Contract 3 [] H depth command (fun (t : Source.State w) => t = s) (CommandPost H b s) fun (x : Source.State w) => if b = true then 10 else 10 * BitVec.toNat (s.regs 1) + 4

              The branch budgets include the compiled conditional and its true-branch skip jump; the potentially expensive clear is not charged a constant price.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                structure Ram.Examples.AmortizedClear.BodyPost {w : ℕ} (H : ℕ) (b : Bool) (rest : List Bool) (entry s : Source.State w) :
                Instances For
                  theorem Ram.Examples.AmortizedClear.body_contract {w H depth : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (b : Bool) (rest : List Bool) (s : Source.State w) (hin : s.input = encode (b :: rest)) (hcount : BitVec.toNat (s.regs 0) = rest.length + 1) (htop : BitVec.toNat (s.regs 1) < H) (hheap : Prefix H (BitVec.toNat (s.regs 1)) s.mem) :
                  Source.Contract 3 [] H depth body (fun (t : Source.State w) => t = s) (BodyPost H b rest s) fun (x : Source.State w) => if b = true then 15 else 10 * BitVec.toNat (s.regs 1) + 9

                  One command is read from the input stream, executed, and the actual remaining-count register is decremented. The expensive bound depends on the entry height, before it is converted into an amortized contract.

                  structure Ram.Examples.AmortizedClear.Snapshot {w : ℕ} (H expected : ℕ) (rest : List Bool) (s : Source.State w) :
                  Instances For
                    Equations
                    Instances For
                      theorem Ram.Examples.AmortizedClear.body_amortized {w H depth : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (b : Bool) (rest : List Bool) (s : Source.State w) (hin : s.input = encode (b :: rest)) (hcount : BitVec.toNat (s.regs 0) = rest.length + 1) (htop : BitVec.toNat (s.regs 1) < H) (hheap : Prefix H (BitVec.toNat (s.regs 1)) s.mem) :
                      Source.AmortizedContract 3 [] H depth body (fun (t : Source.State w) => t = s) (fun (x t : Source.State w) => BodyPost H b rest s t) potential fun (x : Source.State w) => 25

                      Expensive clear operations spend saved potential. The bound 25 is proved from the two real execution bounds, not used as an instruction price.

                      theorem Ram.Examples.AmortizedClear.loop_amortized {w H depth expected : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) :
                      Source.AmortizedContract 3 [] H depth loop (Invariant H expected) (fun (x t : Source.State w) => Invariant H expected t ∧ t.eval (Expr.var 0) = 0) potential fun (s : Source.State w) => 28 * BitVec.toNat (s.regs 0) + 2

                      The outer invariant records the unconsumed input and its abstract fold. The rule retains final potential, allowing this workload to be composed with another workload instead of silently throwing its saved credit away.

                      structure Ram.Examples.AmortizedClear.AnswerReady {w : ℕ} (H expected : ℕ) (s : Source.State w) :
                      Instances For
                        theorem Ram.Examples.AmortizedClear.loop_contract {w H depth expected : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) :
                        Source.Contract 3 [] H depth loop (Invariant H expected) (AnswerReady H expected) fun (s : Source.State w) => 28 * BitVec.toNat (s.regs 0) + 2 + 10 * BitVec.toNat (s.regs 1)

                        For an existing stack, initial credit is charged explicitly. In particular this bound is linear in command count plus the initial stack height.

                        structure Ram.Examples.AmortizedClear.InputReady {w : ℕ} (H : ℕ) (commands : List Bool) (s : Source.State w) :
                        Instances For
                          structure Ram.Examples.AmortizedClear.CountReady {w : ℕ} (H : ℕ) (commands : List Bool) (s : Source.State w) :
                          Instances For
                            structure Ram.Examples.AmortizedClear.LoopReady {w : ℕ} (H : ℕ) (commands : List Bool) (s : Source.State w) :
                            Instances For
                              structure Ram.Examples.AmortizedClear.Post {w : ℕ} (H : ℕ) (commands : List Bool) (s : Source.State w) :
                              Instances For
                                theorem Ram.Examples.AmortizedClear.main_contract {w H depth : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (commands : List Bool) (hcap : commands.length ≤ H) :
                                Source.Contract 3 [] H depth main (InputReady H commands) (Post H commands) fun (x : Source.State w) => 28 * commands.length + 7

                                Input, initialization and output are ordinary charged source statements. The initial heap is empty, so its potential is genuinely zero.

                                theorem Ram.Examples.AmortizedClear.machine_correct {w H : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (commands : List Bool) (hcap : commands.length ≤ H) (hcodefit : 35 < 2 ^ w) :
                                ∃ (finish : State w), TerminatesWithin machine (28 * commands.length + 9) (State.initial (BitVec.ofNat w H :: BitVec.ofNat w commands.length :: encode commands)) finish ∧ finish.output = [BitVec.ofNat w (result commands 0)] ∧ finish.input = [] ∧ Prefix H (result commands 0) finish.mem

                                Full machine result, consumed input and functional heap correctness, with an actual linear transition budget including boundary read and halt. Width and capacity constrain the problem input, not an uncharged host computation.

                                theorem Ram.Examples.AmortizedClear.machine_correct_decoded {w H : ℕ} (hw : 0 < w) (hH : H < 2 ^ w) (commands : List Bool) (hcap : commands.length ≤ H) (hcodefit : 35 < 2 ^ w) :
                                ∃ (finish : State w), TerminatesWithin machine (28 * commands.length + 9) (State.initial (BitVec.ofNat w H :: BitVec.ofNat w commands.length :: encode commands)) finish ∧ List.map BitVec.toNat finish.output = [result commands 0] ∧ finish.input = [] ∧ Prefix H (result commands 0) finish.mem

                                The observable answer decodes to the natural fold, not just its residue. The heap-capacity precondition also bounds every possible final stack height.