Hoare-style reasoning for the CentralStrQ word RAM: exact step counts, triples with budgets, code blocks at an offset
DefinitionCentralStrQ_Hoarehoare-logicverified-programmingword-ram
Infrastructure definitions for verifying programs of the published deterministic word RAM CentralStrQ_Machine (Hagerup's multiplication instruction set plus unsigned division and modulo, unit cost, ticks counts executed instructions, maxAddr is one more than the largest touched address).
- Exact step counts. is the state reached from after exactly successful steps of , and is undefined (
none) if the machine halts or gets stuck earlier. - Hoare triples with step budgets. (
Triple P A B t) holds if from every state satisfying , at most successful steps of reach a state satisfying . This is total correctness together with a running-time bound. - Code blocks at an offset. (
Instr.relocate o i) shifts the target of a jump or branch by and leaves other instructions unchanged.CodeAt P o Bsays that for every , instruction of is . (State.shift o s) is with its program counter shifted by . - Instruction equations. The result of
exec1for each instruction and ofopReadfor each operand, all by definition (rfl), plus the two basic facts aboutMemory.write.
The companion theorems prove sequencing, consequence, loops, single instructions, linking (Triple.relocate), and the bridge to the machine's specification format RunsWithin. They also reduce the online data-structure protocol ServesOnline of the Dynamic LZ77 mission to a representation invariant (DynLZ77.Main.servesOnline_of_invariant).
Definition code
import Mathlib
import Definitions.Def_CentralStrQ_Machine
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Hoare-style reasoning for the `CentralStrQ` word RAM: definitions
Infrastructure on top of the published deterministic word RAM `Def_CentralStrQ_Machine`
(Hagerup's multiplication instruction set plus `udiv`/`umod`, unit cost, `ticks` = executed
instructions, `maxAddr` = one more than the largest touched address).
* `stepN P k s = some t`: exactly `k` successful steps of `P` lead from `s` to `t`
(`none` if the machine halts or gets stuck earlier).
* `Triple P A B t`: from every state satisfying `A`, at most `t` successful steps reach a state
satisfying `B` (a Hoare triple with a step budget, total correctness).
* `Instr.relocate o`, `CodeAt P o B`, `State.shift o`: a code block `B` placed at offset `o` of a
program `P`, with jump and branch targets shifted by `o`, and the correspondingly shifted
machine state.
* The equations of `exec1` for each instruction and of `opRead` for each operand, all by `rfl`.
-/
variable {w : Width}
/-! ## Instruction equations -/
@[simp] theorem exec1_copy (dst : Address) (src : Operand) (s : State w) :
exec1 (.copy dst src) s = some { s with
mem := s.mem.write (dst.resolve s.mem) (opRead s.mem src), pc := s.pc + 1,
ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf ((Instr.copy dst src).touched s.mem)) } := rfl
@[simp] theorem exec1_binary (op : BinOp) (dst : Address) (x y : Operand) (s : State w) :
exec1 (.binary op dst x y) s = some { s with
mem := s.mem.write (dst.resolve s.mem) (op.eval (opRead s.mem x) (opRead s.mem y)),
pc := s.pc + 1, ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf ((Instr.binary op dst x y).touched s.mem)) } := rfl
@[simp] theorem exec1_bitNot (dst : Address) (src : Operand) (s : State w) :
exec1 (.bitNot dst src) s = some { s with
mem := s.mem.write (dst.resolve s.mem) (~~~(opRead s.mem src)), pc := s.pc + 1,
ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf ((Instr.bitNot dst src).touched s.mem)) } := rfl
@[simp] theorem exec1_jump (target : ℕ) (s : State w) :
exec1 (.jump target) s = some { s with
pc := target, ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf ((Instr.jump target).touched s.mem)) } := rfl
@[simp] theorem exec1_branch (cmp : Comparison) (x y : Operand) (target : ℕ) (s : State w) :
exec1 (.branch cmp x y target) s = some { s with
pc := if cmp.test (opRead s.mem x) (opRead s.mem y) then target else s.pc + 1,
ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf ((Instr.branch cmp x y target).touched s.mem)) } := rfl
@[simp] theorem exec1_halt (s : State w) :
exec1 .halt s = some { s with
pc := s.pc + 1, halted := true, ticks := s.ticks + 1,
maxAddr := max s.maxAddr (reachOf (Instr.halt.touched s.mem)) } := rfl
@[simp] theorem exec1_call (a : ℕ) (s : State w) : exec1 (.call a) s = none := rfl
@[simp] theorem opRead_immediate (mem : Memory w) (v : ℕ) :
opRead mem (.immediate v) = BitVec.ofNat w.bits v := rfl
@[simp] theorem opRead_direct (mem : Memory w) (a : ℕ) :
opRead mem (.memory (.direct a)) = mem a := rfl
@[simp] theorem opRead_indirect (mem : Memory w) (a : ℕ) :
opRead mem (.memory (.indirect a)) = mem (mem a).toNat := rfl
@[simp] theorem memory_write_same (mem : Memory w) (a : ℕ) (v : Word w) :
mem.write a v a = v := by
simp [Memory.write]
theorem memory_write_ne (mem : Memory w) {a b : ℕ} (v : Word w) (h : b ≠ a) :
mem.write a v b = mem b := by
simp [Memory.write, h]
/-! ## Exact step counts -/
/-- `stepN P k s = some t`: exactly `k` successful steps of `P` lead from `s` to `t`. -/
def stepN (P : Program) : ℕ → State w → Option (State w)
| 0, s => some s
| k + 1, s => (step P s).bind (stepN P k)
@[simp] theorem stepN_zero (P : Program) (s : State w) : stepN P 0 s = some s := rfl
theorem stepN_succ (P : Program) (k : ℕ) (s : State w) :
stepN P (k + 1) s = (step P s).bind (stepN P k) := rfl
/-! ## Triples -/
/-- From every state satisfying `A`, at most `t` successful steps of `P` reach a state
satisfying `B`. -/
def Triple (P : Program) (A B : State w → Prop) (t : ℕ) : Prop :=
∀ s, A s → ∃ k ≤ t, ∃ s', stepN P k s = some s' ∧ B s'
/-! ## Code blocks at an offset -/
/-- Shift jump and branch targets by `o`. -/
def Instr.relocate (o : ℕ) : Instr → Instr
| .jump target => .jump (o + target)
| .branch cmp x y target => .branch cmp x y (o + target)
| i => i
/-- The block `B`, relocated by `o`, occupies positions `o, …, o + |B| − 1` of `P`. -/
def CodeAt (P : Program) (o : ℕ) (B : Program) : Prop :=
∀ j (hj : j < B.length), P[o + j]? = some (B[j].relocate o)
/-- The state with its program counter shifted by `o`. -/
def State.shift (o : ℕ) (s : State w) : State w := { s with pc := o + s.pc }
end CentralStrQ
Source
Infrastructure for the published deterministic word RAM Def_CentralStrQ_Machine (Kempa, Kociumaka, A Complete Characterization of Central String Queries in Compact Space, STOC 2027 submission, Section 3.2, p. 13; T. Hagerup, Sorting and searching on the word RAM, STACS 1998, https://doi.org/10.1007/BFb0028575). Hoare-style rules after C. A. R. Hoare, An axiomatic basis for computer programming, Commun. ACM 12(10), 1969, https://doi.org/10.1145/363235.363259.