Documentation

LeanPool.PlanarSeparator.WordRAM.Programs.Tactics

Verification support for straight-line blocks #

This file fixes the style in which programs for the RAM of WordRAM/Spec.lean are written and verified. Everything downstream (MinVertex, EdgeHit, and the larger programs to come) follows the same pattern.

The pattern #

  1. Programs are Stmts, written with Stmt.block [i₁, …, iₖ] for straight-line pieces, Stmt.seq, Stmt.ifLtN/ifLtR and Stmt.whileLtN/whileLtR for control. Register allocation is documented next to the definition (which registers are inputs, outputs, scratch).

  2. Every building block gets a spec lemma of the shape

    theorem foo_exec (P : Input) (W : ℕ) (c : Config) (hc : WordsBounded W c) (side…) :
        ∃ c' k, Exec P W fooStmt c c' k ∧ k ≤ K ∧ ⟨postcondition on c, c'⟩ ∧ ⟨frame⟩
    

    with an explicit numeric cost K, a postcondition on the registers written, and a frame: which registers/memory/output are untouched.

  3. Goals are always of the form ∃ c' k, Exec P W s c c' k ∧ Q c' k ("spec goals") with c an explicit configuration and no metavariables. The lemmas spec_seq, spec_ifLtR_true/false, spec_ifLtN_true/false, spec_skip, spec_instr, spec_use and spec_imp decompose a spec goal along the structure of s, threading the cost into Q; when s has been consumed the goal is Q c_final k_final with the final configuration written out as { c with nreg := upd (upd c.nreg …) …, rreg := … } and the cost as a sum of literals (0 + 1 + 1 + (1 + 2); omega and config_simp normalise it). The postcondition is proved conjunct by conjunct: refine ⟨by omega, by config_simp, fun r hr => by simp [hr], rfl, rfl⟩ — rfl closes the untouched-field conjuncts (c'.nmem = c.nmem), config_simp the register readings.

  4. Straight-line blocks are discharged by exec_block. On a spec goal for Stmt.block [i₁, …, iₖ] with hc : WordsBounded W c in context, it chains spec_step with the per-instruction rules of WordRAM.Machine.Exec, flattening the configuration after each step (so its size stays linear in k). The remaining goals are the side conditions (x ≤ W for constN/inputSize/addN, _ ≠ 0 for the divisor of divR), in program order, followed by the postcondition goal (Q c_final k, or the spec goal for the rest of the program when the block was sequenced with spec_seq). The postcondition itself is never simplified by the tactic. Typical use:

    refine spec_seq ?_
    -- Goal: ∃ c₁ k₁, Exec (block …) c c₁ k₁ ∧ ∃ c' k₂, Exec t c₁ c' k₂ ∧ Q c' (k₁ + k₂)
    exec_block
    · omega                        -- side condition `5 ≤ W`
    · config_simp; omega           -- side condition `{…}.nreg 0 + {…}.nreg 1 ≤ W`
    -- main goal: ∃ c' k₂, Exec t { c with … } c' k₂ ∧ Q c' (0 + 1 + 1 + 1 + k₂)
    

    config_simp (simp only with the projection and upd lemmas, never failing) reduces a configuration's fields; full simp does too. The hypothesis names ‹WordsBounded _ _› are found by assumption, so the word bound of the current configuration must be a hypothesis: after spec_instr … fun hw₁ => ?_ or spec_use …; intro c₁ k₁ hk hw₁ hpost it is.

  5. Conditionals: by_cases h : (test in terms of the original registers), then refine spec_ifLtR_true (by config_simp; exact h) ?_ or _false (config_simp reduces the test on the current configuration to the original registers).

5'. Using a spec lemma of a sub-program inside a spec goal: refine spec_use (foo_exec _ ‹_›) ?_; intro c₁ k₁ hk hw₁ hpost — the facts in hpost mention fields of the current (explicit) configuration; config_simp at hpost reduces them to the original registers.

  1. Loops use Exec.whileLtN_bounded (body cost B, measure μ) or Exec.whileLtN_potential; the invariant includes WordsBounded W, the loop-variable facts, the mathematical invariant and the frame. The body is proved as a spec lemma (pattern 2) and plugged into the invariant proof.

  2. Reading off facts: config_simp or simp; the frame (∀ r, r ∉ written → c'.nreg r = c.nreg r) is proved by intro r hr; simp [h₁, h₂] with hᵢ : r ≠ i the register inequalities (they let Machine.upd_other fire); Machine.upd_apply turns a symbolic upd f i v j into an if.

  3. Top level: Machine.runsWithin_of_exec turns an execution from initConfig into RunsWithin (compile s 0 ++ [.halt]) P W (k + 1) out.

Contents #

Spec goals and their decomposition #

theorem WordRAM.Programs.spec_nil {P : Input} {W : ℕ} {c : Config} {Q : Config → ℕ → Prop} (hw : WordsBounded W c) (hQ : Q c 0) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.block []) c c' k ∧ Q c' k

The empty block: the postcondition holds now, at cost 0.

theorem WordRAM.Programs.spec_step {P : Input} {W : ℕ} {i : Instr} {l : List Instr} {c c₁ : Config} {Q : Config → ℕ → Prop} (h₁ : Machine.Exec P W (Machine.Stmt.instr i) c c₁ 1) (h₂ : WordsBounded W c₁ → ∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.block l) c₁ c' k ∧ Q c' (k + 1)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.block (i :: l)) c c' k ∧ Q c' k

One instruction of a block: run it (rule h₁, giving the explicit next configuration c₁), then continue with the rest at cost k + 1.

theorem WordRAM.Programs.spec_instr {P : Input} {W : ℕ} {i : Instr} {c c₁ : Config} {Q : Config → ℕ → Prop} (h₁ : Machine.Exec P W (Machine.Stmt.instr i) c c₁ 1) (hQ : WordsBounded W c₁ → Q c₁ 1) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.instr i) c c' k ∧ Q c' k

A single instruction statement.

theorem WordRAM.Programs.spec_skip {P : Input} {W : ℕ} {c : Config} {Q : Config → ℕ → Prop} (hw : WordsBounded W c) (hQ : Q c 0) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W Machine.Stmt.skip c c' k ∧ Q c' k

skip.

theorem WordRAM.Programs.spec_seq {P : Input} {W : ℕ} {s t : Machine.Stmt} {c : Config} {Q : Config → ℕ → Prop} (h : ∃ (c₁ : Config) (k₁ : ℕ), Machine.Exec P W s c c₁ k₁ ∧ ∃ (c' : Config) (k₂ : ℕ), Machine.Exec P W t c₁ c' k₂ ∧ Q c' (k₁ + k₂)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (s.seq t) c c' k ∧ Q c' k

Sequencing: run s, then t from where s stopped; costs add.

theorem WordRAM.Programs.spec_ifLtN_true {P : Input} {W a b : ℕ} {t e : Machine.Stmt} {c : Config} {Q : Config → ℕ → Prop} (ht : c.nreg a < c.nreg b) (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W t c c' k ∧ Q c' (k + 1)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.ifLtN a b t e) c c' k ∧ Q c' k

ifLtN, test true: cost k + 1.

theorem WordRAM.Programs.spec_ifLtN_false {P : Input} {W a b : ℕ} {t e : Machine.Stmt} {c : Config} {Q : Config → ℕ → Prop} (ht : ¬c.nreg a < c.nreg b) (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W e c c' k ∧ Q c' (k + 2)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.ifLtN a b t e) c c' k ∧ Q c' k

ifLtN, test false: cost k + 2.

theorem WordRAM.Programs.spec_ifLtR_true {P : Input} {W a b : ℕ} {t e : Machine.Stmt} {c : Config} {Q : Config → ℕ → Prop} (ht : c.rreg a < c.rreg b) (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W t c c' k ∧ Q c' (k + 1)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.ifLtR a b t e) c c' k ∧ Q c' k

ifLtR, test true: cost k + 1.

theorem WordRAM.Programs.spec_ifLtR_false {P : Input} {W a b : ℕ} {t e : Machine.Stmt} {c : Config} {Q : Config → ℕ → Prop} (ht : ¬c.rreg a < c.rreg b) (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W e c c' k ∧ Q c' (k + 2)) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.ifLtR a b t e) c c' k ∧ Q c' k

ifLtR, test false: cost k + 2.

theorem WordRAM.Programs.spec_use {P : Input} {W : ℕ} {s : Machine.Stmt} {c : Config} {K : ℕ} {post : Config → Prop} {Q : Config → ℕ → Prop} (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W s c c' k ∧ k ≤ K ∧ post c') (hQ : ∀ (c' : Config), ∀ k ≤ K, WordsBounded W c' → post c' → Q c' k) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W s c c' k ∧ Q c' k

Plug in a spec lemma for a sub-statement: its cost bound and postcondition are all that the continuation may use.

theorem WordRAM.Programs.spec_imp {P : Input} {W : ℕ} {s : Machine.Stmt} {c : Config} {Q Q' : Config → ℕ → Prop} (h : ∃ (c' : Config) (k : ℕ), Machine.Exec P W s c c' k ∧ Q c' k) (hQ : ∀ (c' : Config) (k : ℕ), Q c' k → Q' c' k) :
∃ (c' : Config) (k : ℕ), Machine.Exec P W s c c' k ∧ Q' c' k

Weaken the postcondition.

theorem WordRAM.Programs.spec_seq_of {P : Input} {W : ℕ} {s t : Machine.Stmt} {c : Config} {K₁ K₂ : ℕ} {Q₁ : Config → Prop} {Q₂ : Config → Config → Prop} (h₁ : ∃ (c₁ : Config) (k₁ : ℕ), Machine.Exec P W s c c₁ k₁ ∧ k₁ ≤ K₁ ∧ Q₁ c₁) (h₂ : ∀ (c₁ : Config), WordsBounded W c₁ → Q₁ c₁ → ∃ (c₂ : Config) (k₂ : ℕ), Machine.Exec P W t c₁ c₂ k₂ ∧ k₂ ≤ K₂ ∧ Q₂ c₁ c₂) :
∃ (c₂ : Config) (k : ℕ), Machine.Exec P W (s.seq t) c c₂ k ∧ k ≤ K₁ + K₂ ∧ ∃ (c₁ : Config), Q₁ c₁ ∧ Q₂ c₁ c₂

Compose two spec lemmas of the standard shape (pattern 2 of the module docstring).

Simp support #

@[simp]
theorem WordRAM.Programs.Config.nreg_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.nreg = nreg

Reading a field of an explicitly built configuration.

@[simp]
theorem WordRAM.Programs.Config.rreg_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.rreg = rreg
@[simp]
theorem WordRAM.Programs.Config.nmem_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.nmem = nmem
@[simp]
theorem WordRAM.Programs.Config.rmem_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.rmem = rmem
@[simp]
theorem WordRAM.Programs.Config.output_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.output = output
@[simp]
theorem WordRAM.Programs.Config.pc_mk (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
{ pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output }.pc = pc
theorem WordRAM.Programs.wordsBounded_mk {W : ℕ} (pc : ℕ) (nreg : ℕ → ℕ) (rreg : ℕ → ℝ) (nmem : ℕ → ℕ) (rmem : ℕ → ℝ) (output : List ℕ) :
WordsBounded W { pc := pc, nreg := nreg, rreg := rreg, nmem := nmem, rmem := rmem, output := output } ↔ (∀ (r : ℕ), nreg r ≤ W) ∧ ∀ (a : ℕ), nmem a ≤ W

The word bound of an explicitly built configuration.

simp only with the configuration-projection and upd lemmas. Reduces ({ c with nreg := upd c.nreg 0 5, … } : Config).nreg 0 to 5, and .nreg 1 to c.nreg 1 (the inequality 1 ≠ 0 between literals is decided by simp's arithmetic simprocs). An optional at clause selects the hypothesis to simplify. The tactic also normalises costs such as 0 + 1 + 1 to 2.

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

    Flatten a nested configuration in a hypothesis to a single { pc := …, nreg := upd (upd …) …, … } (without this the term after k instructions would have printed size exponential in k).

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

      On a spec goal ∃ c' k, Exec P W s c c' k ∧ Q c' k, flatten the starting configuration c only (the postcondition Q is left untouched).

      Equations
      Instances For

        The block tactic #

        exec_block discharges a spec goal ∃ c' k, Exec P W (block [i₁, …, iₖ]) c c' k ∧ Q c' k. It needs a hypothesis WordsBounded W c for the starting configuration in the local context (found by assumption); the word bound of every later configuration is supplied by spec_step. Each step tries the per-instruction rules in turn, then flattens the new configuration (in the goal and in the new word-bound hypothesis); rules with a side condition (constN, inputSize, addN, divR) leave it as a goal, moved behind the main goal by rotate_left so the chain continues. After the block is exhausted the goals are the side conditions in program order, then the postcondition Q c_final k_final (with c_final flat and k_final a sum of literals).

        One step of exec_block: try every instruction rule on the head of the block.

        Equations
        Instances For

          Symbolic execution of a straight-line block on a spec goal; see the module docstring. Leaves the side conditions (in program order) and then the postcondition goal.

          Equations
          Instances For

            Worked example #

            The block nreg 0 := n; nreg 1 := 1; nreg 3 := input[nreg 1]; nreg 2 := nreg 0 + nreg 1, executed from any word-bounded c whose input words are at most W, with n + 1 ≤ W.

            theorem WordRAM.Programs.exec_block_example (P : Input) (W : ℕ) (hW : P.data.length + 1 ≤ W) (hP : ∀ (i : ℕ), P.data.getD i 0 ≤ W) (c : Config) (hc : WordsBounded W c) :
            ∃ (c' : Config) (k : ℕ), Machine.Exec P W (Machine.Stmt.block [Instr.inputSize 0, Instr.constN 1 1, Instr.readIn 3 1, Instr.addN 2 0 1]) c c' k ∧ k = 4 ∧ c'.nreg 0 = P.data.length ∧ c'.nreg 1 = 1 ∧ c'.nreg 2 = P.data.length + 1 ∧ c'.nreg 3 = P.data.getD 1 0 ∧ (∀ (r : ℕ), 4 ≤ r → c'.nreg r = c.nreg r) ∧ c'.rreg = c.rreg ∧ c'.nmem = c.nmem ∧ c'.rmem = c.rmem ∧ c'.output = c.output

            Worked example of the recommended pattern (and a regression test of exec_block).