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 #
Programs are
Stmts, written withStmt.block [i₁, …, iₖ]for straight-line pieces,Stmt.seq,Stmt.ifLtN/ifLtRandStmt.whileLtN/whileLtRfor control. Register allocation is documented next to the definition (which registers are inputs, outputs, scratch).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.Goals are always of the form
∃ c' k, Exec P W s c c' k ∧ Q c' k("spec goals") withcan explicit configuration and no metavariables. The lemmasspec_seq,spec_ifLtR_true/false,spec_ifLtN_true/false,spec_skip,spec_instr,spec_useandspec_impdecompose a spec goal along the structure ofs, threading the cost intoQ; whenshas been consumed the goal isQ c_final k_finalwith 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);omegaandconfig_simpnormalise it). The postcondition is proved conjunct by conjunct:refine ⟨by omega, by config_simp, fun r hr => by simp [hr], rfl, rfl⟩—rflcloses the untouched-field conjuncts (c'.nmem = c.nmem),config_simpthe register readings.Straight-line blocks are discharged by
exec_block. On a spec goal forStmt.block [i₁, …, iₖ]withhc : WordsBounded W cin context, it chainsspec_stepwith the per-instruction rules ofWordRAM.Machine.Exec, flattening the configuration after each step (so its size stays linear ink). The remaining goals are the side conditions (x ≤ WforconstN/inputSize/addN,_ ≠ 0for the divisor ofdivR), 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 withspec_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 onlywith the projection andupdlemmas, never failing) reduces a configuration's fields; fullsimpdoes too. The hypothesis names‹WordsBounded _ _›are found byassumption, so the word bound of the current configuration must be a hypothesis: afterspec_instr … fun hw₁ => ?_orspec_use …; intro c₁ k₁ hk hw₁ hpostit is.Conditionals:
by_cases h : (test in terms of the original registers), thenrefine spec_ifLtR_true (by config_simp; exact h) ?_or_false(config_simpreduces 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.
Loops use
Exec.whileLtN_bounded(body costB, measureμ) orExec.whileLtN_potential; the invariant includesWordsBounded 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.Reading off facts:
config_simporsimp; the frame (∀ r, r ∉ written → c'.nreg r = c.nreg r) is proved byintro r hr; simp [h₁, h₂]withhᵢ : r ≠ ithe register inequalities (they letMachine.upd_otherfire);Machine.upd_applyturns a symbolicupd f i v jinto anif.Top level:
Machine.runsWithin_of_execturns an execution frominitConfigintoRunsWithin (compile s 0 ++ [.halt]) P W (k + 1) out.
Contents #
spec_nil,spec_step: the chaining lemmas used byexec_block.spec_seq,spec_skip,spec_instr,spec_ifLtN_true/false,spec_ifLtR_true/false,spec_use,spec_imp,spec_seq_of: structural decomposition of spec goals.exec_block: the tactic (built fromexec_block_step,config_flatten_at,spec_flatten).config_simp: the projection/updsimp set (alsoconfig_simp at h).exec_block_example: a worked example (also a regression test for the tactic).
Spec goals and their decomposition #
The empty block: the postcondition holds now, at cost 0.
One instruction of a block: run it (rule h₁, giving the explicit next configuration
c₁), then continue with the rest at cost k + 1.
A single instruction statement.
skip.
Sequencing: run s, then t from where s stopped; costs add.
ifLtN, test true: cost k + 1.
ifLtN, test false: cost k + 2.
ifLtR, test true: cost k + 1.
ifLtR, test false: cost k + 2.
Plug in a spec lemma for a sub-statement: its cost bound and postcondition are all that the continuation may use.
Weaken the postcondition.
Compose two spec lemmas of the standard shape (pattern 2 of the module docstring).
Simp support #
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
- WordRAM.Programs.flattenSpec = Lean.ParserDescr.node `WordRAM.Programs.flattenSpec 1024 (Lean.ParserDescr.nonReservedSymbol "spec_flatten" false)
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
- WordRAM.Programs.executeBlockStep = Lean.ParserDescr.node `WordRAM.Programs.executeBlockStep 1024 (Lean.ParserDescr.nonReservedSymbol "exec_block_step" false)
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
- WordRAM.Programs.executeBlock = Lean.ParserDescr.node `WordRAM.Programs.executeBlock 1024 (Lean.ParserDescr.nonReservedSymbol "exec_block" false)
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.
Worked example of the recommended pattern (and a regression test of exec_block).