Documentation

LeanPool.PlanarSeparator.WordRAM.Machine.Structured

A structured language and a cost-exact compiler #

noncomputable def WordRAM.Instr.effect (P : Input) :

The effect of a straight-line instruction on the state; the program counter is left unchanged. none for a division by zero and for control instructions.

Equations
Instances For
    @[simp]
    theorem WordRAM.Instr.effect_inputSize {P : Input} {c : Config} (r : ℕ) :
    effect P (inputSize r) c = some { pc := c.pc, nreg := upd c.nreg r P.data.length, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_constN {P : Input} {c : Config} (r k : ℕ) :
    effect P (constN r k) c = some { pc := c.pc, nreg := upd c.nreg r k, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_copyN {P : Input} {c : Config} (r a : ℕ) :
    effect P (copyN r a) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_addN {P : Input} {c : Config} (r a b : ℕ) :
    effect P (addN r a b) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a + c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_subN {P : Input} {c : Config} (r a b : ℕ) :
    effect P (subN r a b) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a - c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_loadN {P : Input} {c : Config} (r a : ℕ) :
    effect P (loadN r a) c = some { pc := c.pc, nreg := upd c.nreg r (c.nmem (c.nreg a)), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_storeN {P : Input} {c : Config} (a r : ℕ) :
    effect P (storeN a r) c = some { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := upd c.nmem (c.nreg a) (c.nreg r), rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_constR {P : Input} {c : Config} (r : ℕ) (z : ℤ) :
    effect P (constR r z) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r ↑z, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_copyR {P : Input} {c : Config} (r a : ℕ) :
    effect P (copyR r a) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a), nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_addR {P : Input} {c : Config} (r a b : ℕ) :
    effect P (addR r a b) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a + c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_subR {P : Input} {c : Config} (r a b : ℕ) :
    effect P (subR r a b) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a - c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_mulR {P : Input} {c : Config} (r a b : ℕ) :
    effect P (mulR r a b) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a * c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output }
    theorem WordRAM.Instr.effect_divR {P : Input} {c : Config} (r a b : ℕ) (hb : c.rreg b ≠ 0) :
    effect P (divR r a b) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a / c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output }
    theorem WordRAM.Instr.effect_divR_zero {P : Input} {c : Config} (r a b : ℕ) (hb : c.rreg b = 0) :
    effect P (divR r a b) c = none
    @[simp]
    theorem WordRAM.Instr.effect_loadR {P : Input} {c : Config} (r a : ℕ) :
    effect P (loadR r a) c = some { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rmem (c.nreg a)), nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_storeR {P : Input} {c : Config} (a r : ℕ) :
    effect P (storeR a r) c = some { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := upd c.rmem (c.nreg a) (c.rreg r), output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_mulN {P : Input} {c : Config} (r a b : ℕ) :
    effect P (mulN r a b) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a * c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_divN {P : Input} {c : Config} (r a b : ℕ) :
    effect P (divN r a b) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a / c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_modN {P : Input} {c : Config} (r a b : ℕ) :
    effect P (modN r a b) c = some { pc := c.pc, nreg := upd c.nreg r (c.nreg a % c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_readIn {P : Input} {c : Config} (r a : ℕ) :
    effect P (readIn r a) c = some { pc := c.pc, nreg := upd c.nreg r (P.data.getD (c.nreg a) 0), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_rand {P : Input} {c : Config} (r a : ℕ) :
    effect P (rand r a) c = some { pc := c.pc, nreg := upd c.nreg r (P.tape (c.nreg a)), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }
    @[simp]
    theorem WordRAM.Instr.effect_outN {P : Input} {c : Config} (a : ℕ) :
    effect P (outN a) c = some { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output ++ [c.nreg a] }
    @[simp]
    @[simp]
    theorem WordRAM.Instr.effect_jump {P : Input} {c : Config} (l : ℕ) :
    effect P (jump l) c = none
    @[simp]
    theorem WordRAM.Instr.effect_brLtN {P : Input} {c : Config} (a b l : ℕ) :
    effect P (brLtN a b l) c = none
    @[simp]
    theorem WordRAM.Instr.effect_brLtR {P : Input} {c : Config} (a b l : ℕ) :
    effect P (brLtR a b l) c = none
    theorem WordRAM.Instr.effect_pc {P : Input} {c : Config} {ins : Instr} {c' : Config} (h : effect P ins c = some c') :
    c'.pc = c.pc

    The effect never changes the program counter.

    theorem WordRAM.Instr.effect_setPc {P : Input} {c : Config} (ins : Instr) (p : ℕ) :
    effect P ins { pc := p, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } = Option.map (fun (c' : Config) => { pc := p, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output }) (effect P ins c)

    The effect is insensitive to the program counter.

    theorem WordRAM.Instr.step_eq_effect {P : Input} {c : Config} {prog : Program} {ins : Instr} (h : prog[c.pc]? = some ins) (hs : ins.IsStraight) :
    step prog P c = Option.map (fun (c' : Config) => { pc := c.pc + 1, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output }) (effect P ins c)

    step on a straight-line instruction is effect followed by pc + 1.

    theorem WordRAM.Instr.step_of_effect {P : Input} {c : Config} {prog : Program} {ins : Instr} {c' : Config} (h : prog[c.pc]? = some ins) (hs : ins.IsStraight) (he : effect P ins c = some c') :
    step prog P c = some { pc := c.pc + 1, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output }

    step on a straight-line instruction whose effect is c'.

    The structured language #

    Structured statements over straight-line instructions.

    • skip : Stmt

      No code.

    • instr (i : Instr) : Stmt

      One straight-line instruction (Instr.IsStraight is required by the semantics).

    • seq (s t : Stmt) : Stmt

      Sequencing.

    • ifLtN (a b : ℕ) (thenS elseS : Stmt) : Stmt

      if nreg a < nreg b then thenS else elseS.

    • ifLtR (a b : ℕ) (thenS elseS : Stmt) : Stmt

      if rreg a < rreg b then thenS else elseS.

    • whileLtN (a b : ℕ) (body : Stmt) : Stmt

      while nreg a < nreg b do body.

    • whileLtR (a b : ℕ) (body : Stmt) : Stmt

      while rreg a < rreg b do body.

    Instances For

      Compile a statement to machine code placed at address base.

      Equations
      Instances For
        @[simp]
        theorem WordRAM.Machine.Stmt.codeSize_ifLtN (a b : ℕ) (t e : Stmt) :
        (ifLtN a b t e).codeSize = t.codeSize + e.codeSize + 2
        @[simp]
        theorem WordRAM.Machine.Stmt.codeSize_ifLtR (a b : ℕ) (t e : Stmt) :
        (ifLtR a b t e).codeSize = t.codeSize + e.codeSize + 2
        @[simp]
        theorem WordRAM.Machine.Stmt.codeSize_whileLtN (a b : ℕ) (body : Stmt) :
        (whileLtN a b body).codeSize = body.codeSize + 3
        @[simp]
        theorem WordRAM.Machine.Stmt.codeSize_whileLtR (a b : ℕ) (body : Stmt) :
        (whileLtR a b body).codeSize = body.codeSize + 3
        @[simp]

        The compiled code has exactly codeSize instructions.

        A block of straight-line instructions, as nested sequences ending in skip.

        Equations
        Instances For
          @[simp]
          theorem WordRAM.Machine.Stmt.block_cons (i : Instr) (l : List Instr) :
          block (i :: l) = (instr i).seq (block l)
          @[simp]
          theorem WordRAM.Machine.Stmt.compile_block (l : List Instr) (base : ℕ) :
          (block l).compile base = l

          A block compiles to exactly its list of instructions, at any base address.

          Big-step cost semantics #

          inductive WordRAM.Machine.Exec (P : Input) (W : ℕ) :
          Stmt → Config → Config → ℕ → Prop

          Exec P W s c c' k: from state c, the statement s runs to completion in state c' executing exactly k machine instructions of its compiled code. Every configuration at which an instruction runs, and the final configuration, is WordsBounded W; divR is never executed with a zero divisor (the effect must be some). The program counter is carried along unchanged.

          Instances For
            theorem WordRAM.Machine.Exec.wb_left {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) :

            The starting configuration of an execution is word-bounded.

            theorem WordRAM.Machine.Exec.wb_right {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) :

            The final configuration of an execution is word-bounded.

            theorem WordRAM.Machine.Exec.pc_eq {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) :
            c'.pc = c.pc

            An execution never changes the program counter.

            theorem WordRAM.Machine.Exec.setPc {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) (p : ℕ) :
            Exec P W s { pc := p, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } { pc := p, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output } k

            An execution is insensitive to the program counter.

            theorem WordRAM.Machine.Exec.cast {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k k' : ℕ} (h : Exec P W s c c' k) (hk : k = k') :
            Exec P W s c c' k'

            Replace the cost by an equal one.

            theorem WordRAM.Machine.Exec.cast_end {P : Input} {W : ℕ} {s : Stmt} {c c' c'' : Config} {k : ℕ} (h : Exec P W s c c' k) (hc : c' = c'') :
            Exec P W s c c'' k

            Replace the final configuration by an equal one.

            Per-instruction rules #

            Each rule gives the exact next state, cost 1, and the side conditions: the written natural value must be ≤ W (for inputSize, constN, addN, mulN, readIn, rand), and divR needs a nonzero divisor.

            theorem WordRAM.Machine.Exec.inputSize {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r : ℕ) (hn : P.data.length ≤ W) :
            Exec P W (Stmt.instr (Instr.inputSize r)) c { pc := c.pc, nreg := upd c.nreg r P.data.length, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.constN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r k : ℕ) (hk : k ≤ W) :
            Exec P W (Stmt.instr (Instr.constN r k)) c { pc := c.pc, nreg := upd c.nreg r k, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.copyN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) :
            Exec P W (Stmt.instr (Instr.copyN r a)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.addN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) (hab : c.nreg a + c.nreg b ≤ W) :
            Exec P W (Stmt.instr (Instr.addN r a b)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a + c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.subN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.subN r a b)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a - c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.loadN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) :
            Exec P W (Stmt.instr (Instr.loadN r a)) c { pc := c.pc, nreg := upd c.nreg r (c.nmem (c.nreg a)), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.storeN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (a r : ℕ) :
            Exec P W (Stmt.instr (Instr.storeN a r)) c { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := upd c.nmem (c.nreg a) (c.nreg r), rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.constR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r : ℕ) (z : ℤ) :
            Exec P W (Stmt.instr (Instr.constR r z)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r ↑z, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.copyR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) :
            Exec P W (Stmt.instr (Instr.copyR r a)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.addR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.addR r a b)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a + c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.subR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.subR r a b)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a - c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.mulR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.mulR r a b)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a * c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.divR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) (hb : c.rreg b ≠ 0) :
            Exec P W (Stmt.instr (Instr.divR r a b)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rreg a / c.rreg b), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.loadR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) :
            Exec P W (Stmt.instr (Instr.loadR r a)) c { pc := c.pc, nreg := c.nreg, rreg := upd c.rreg r (c.rmem (c.nreg a)), nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.storeR {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (a r : ℕ) :
            Exec P W (Stmt.instr (Instr.storeR a r)) c { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := upd c.rmem (c.nreg a) (c.rreg r), output := c.output } 1
            theorem WordRAM.Machine.Exec.mulN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) (hab : c.nreg a * c.nreg b ≤ W) :
            Exec P W (Stmt.instr (Instr.mulN r a b)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a * c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.divN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.divN r a b)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a / c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.modN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a b : ℕ) :
            Exec P W (Stmt.instr (Instr.modN r a b)) c { pc := c.pc, nreg := upd c.nreg r (c.nreg a % c.nreg b), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.readIn {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) (hv : P.data.getD (c.nreg a) 0 ≤ W) :
            Exec P W (Stmt.instr (Instr.readIn r a)) c { pc := c.pc, nreg := upd c.nreg r (P.data.getD (c.nreg a) 0), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.rand {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (r a : ℕ) (hv : P.tape (c.nreg a) ≤ W) :
            Exec P W (Stmt.instr (Instr.rand r a)) c { pc := c.pc, nreg := upd c.nreg r (P.tape (c.nreg a)), rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } 1
            theorem WordRAM.Machine.Exec.outN {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) (a : ℕ) :
            Exec P W (Stmt.instr (Instr.outN a)) c { pc := c.pc, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output ++ [c.nreg a] } 1

            Blocks #

            theorem WordRAM.Machine.Exec.block_nil {P : Input} {W : ℕ} {c : Config} (hw : WordsBounded W c) :
            Exec P W (Stmt.block []) c c 0
            theorem WordRAM.Machine.Exec.block_cons {P : Input} {W : ℕ} {i : Instr} {l : List Instr} {c c₁ c' : Config} {k : ℕ} (h₁ : Exec P W (Stmt.instr i) c c₁ 1) (h₂ : Exec P W (Stmt.block l) c₁ c' k) :
            Exec P W (Stmt.block (i :: l)) c c' (k + 1)

            Prepend one instruction to a block: cost k + 1.

            Loops with a potential #

            theorem WordRAM.Machine.Exec.loop_potential {P : Input} {W : ℕ} (test : Config → Prop) (body loop : Stmt) (hfalse : ∀ (c : Config), ¬test c → WordsBounded W c → Exec P W loop c c 2) (htrue : ∀ (c c₁ c' : Config) (k₁ k₂ : ℕ), test c → Exec P W body c c₁ k₁ → Exec P W loop c₁ c' k₂ → Exec P W loop c c' (k₁ + 2 + k₂)) (I : Config → Prop) (Φ : Config → ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → test c → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ Φ c' + k + 2 ≤ Φ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W loop c c' k ∧ I c' ∧ ¬test c' ∧ k ≤ Φ c + 2

            Generic loop rule. test is the loop test, loop the loop statement, with the two loop rules as hypotheses. Given an invariant I (implying the word bound) and a potential Φ such that from any I-state with the test true one iteration of the body executes with cost k, re-establishes I, and satisfies Φ c' + k + 2 ≤ Φ c, the whole loop executes from any I-state with total cost ≤ Φ c + 2 and ends in an I-state with the test false.

            theorem WordRAM.Machine.Exec.whileLtN_potential {P : Input} {W a b : ℕ} {body : Stmt} (I : Config → Prop) (Φ : Config → ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → c.nreg a < c.nreg b → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ Φ c' + k + 2 ≤ Φ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W (Stmt.whileLtN a b body) c c' k ∧ I c' ∧ ¬c'.nreg a < c'.nreg b ∧ k ≤ Φ c + 2

            Loop rule with a potential for whileLtN a b body: total cost ≤ Φ c + 2.

            theorem WordRAM.Machine.Exec.whileLtR_potential {P : Input} {W a b : ℕ} {body : Stmt} (I : Config → Prop) (Φ : Config → ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → c.rreg a < c.rreg b → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ Φ c' + k + 2 ≤ Φ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W (Stmt.whileLtR a b body) c c' k ∧ I c' ∧ ¬c'.rreg a < c'.rreg b ∧ k ≤ Φ c + 2

            Loop rule with a potential for whileLtR a b body: total cost ≤ Φ c + 2.

            theorem WordRAM.Machine.Exec.loop_bounded {P : Input} {W : ℕ} (test : Config → Prop) (body loop : Stmt) (hfalse : ∀ (c : Config), ¬test c → WordsBounded W c → Exec P W loop c c 2) (htrue : ∀ (c c₁ c' : Config) (k₁ k₂ : ℕ), test c → Exec P W body c c₁ k₁ → Exec P W loop c₁ c' k₂ → Exec P W loop c c' (k₁ + 2 + k₂)) (I : Config → Prop) (μ : Config → ℕ) (B : ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → test c → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ k ≤ B ∧ μ c' < μ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W loop c c' k ∧ I c' ∧ ¬test c' ∧ k ≤ (B + 2) * μ c + 2

            Loop rule with a bounded body cost B and a strictly decreasing measure μ: total cost ≤ (B + 2) * μ c + 2.

            theorem WordRAM.Machine.Exec.whileLtN_bounded {P : Input} {W a b : ℕ} {body : Stmt} (I : Config → Prop) (μ : Config → ℕ) (B : ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → c.nreg a < c.nreg b → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ k ≤ B ∧ μ c' < μ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W (Stmt.whileLtN a b body) c c' k ∧ I c' ∧ ¬c'.nreg a < c'.nreg b ∧ k ≤ (B + 2) * μ c + 2

            whileLtN with a bounded body cost B and a strictly decreasing measure μ.

            theorem WordRAM.Machine.Exec.whileLtR_bounded {P : Input} {W a b : ℕ} {body : Stmt} (I : Config → Prop) (μ : Config → ℕ) (B : ℕ) (hI : ∀ (c : Config), I c → WordsBounded W c) (hbody : ∀ (c : Config), I c → c.rreg a < c.rreg b → ∃ (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I c' ∧ k ≤ B ∧ μ c' < μ c) {c : Config} (hc : I c) :
            ∃ (c' : Config) (k : ℕ), Exec P W (Stmt.whileLtR a b body) c c' k ∧ I c' ∧ ¬c'.rreg a < c'.rreg b ∧ k ≤ (B + 2) * μ c + 2

            whileLtR with a bounded body cost B and a strictly decreasing measure μ.

            theorem WordRAM.Machine.Exec.whileLtN_ghost {P : Input} {W : ℕ} {α : Type} {a b : ℕ} {body : Stmt} (I : α → Config → Prop) (Φ : α → Config → ℕ) (hI : ∀ (x : α) (c : Config), I x c → WordsBounded W c) (hbody : ∀ (x : α) (c : Config), I x c → c.nreg a < c.nreg b → ∃ (x' : α) (c' : Config) (k : ℕ), Exec P W body c c' k ∧ I x' c' ∧ Φ x' c' + k + 2 ≤ Φ x c) {x : α} {c : Config} (hc : I x c) :
            ∃ (x' : α) (c' : Config) (k : ℕ), Exec P W (Stmt.whileLtN a b body) c c' k ∧ I x' c' ∧ ¬c'.nreg a < c'.nreg b ∧ k ≤ Φ x c + 2

            Loop rule with a ghost state: the invariant I a and the potential Φ a may depend on a value a : α that the configuration does not store (for example the set of vertices processed so far); each iteration may change it. Total cost ≤ Φ a c + 2.

            Simulation #

            theorem WordRAM.Machine.SubAt.cast {p q : Program} {b b' : ℕ} (h : SubAt p q b) (hb : b = b') :
            SubAt p q b'

            Placement casts.

            theorem WordRAM.Machine.setPc_congr {c : Config} {p p' : ℕ} (h : p = p') :
            { pc := p, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } = { pc := p', nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output }

            Two configurations differing only in an equal program counter are equal.

            theorem WordRAM.Machine.RunsTo.cast_end {prog : Program} {P : Input} {W k : ℕ} {c c' c'' : Config} (h : RunsTo prog P W k c c') (hc : c' = c'') :
            RunsTo prog P W k c c''

            Replace the final configuration of a bounded run by an equal one.

            theorem WordRAM.Machine.Exec.runsTo {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) {q : Program} {base : ℕ} (hq : SubAt (s.compile base) q base) :
            RunsTo q P W k { pc := base, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } { pc := base + s.codeSize, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output }

            Simulation. If compile s base sits at base inside q, an execution Exec P W s c c' k is realised by running q for exactly k steps from c with pc := base, ending at c' with pc := base + codeSize s, every visited configuration WordsBounded W.

            theorem WordRAM.Machine.Exec.runsTo_compile {P : Input} {W : ℕ} {s : Stmt} {c c' : Config} {k : ℕ} (h : Exec P W s c c' k) :
            RunsTo (s.compile 0) P W k { pc := 0, nreg := c.nreg, rreg := c.rreg, nmem := c.nmem, rmem := c.rmem, output := c.output } { pc := s.codeSize, nreg := c'.nreg, rreg := c'.rreg, nmem := c'.nmem, rmem := c'.rmem, output := c'.output }

            Simulation for the compiled program itself (placed at 0).

            theorem WordRAM.Machine.runsWithin_of_exec {P : Input} {W : ℕ} {s : Stmt} {c' : Config} {k : ℕ} (h : Exec P W s initConfig c' k) :

            Top level. An execution of s from the initial configuration gives a RunsWithin for the program compile s 0 ++ [halt] with k + 1 steps.

            theorem WordRAM.Machine.runsWithin_of_exec_output {P : Input} {W : ℕ} {s : Stmt} {c' : Config} {k : ℕ} {out : List ℕ} (h : Exec P W s initConfig c' k) (ho : c'.output = out) :
            RunsWithin (s.compile 0 ++ [Instr.halt]) P W (k + 1) out

            Top level, with the output named.