Structured while-programs for the CentralStrQ word RAM, with step- and space-aware Hoare triples
DefinitionCentralStrQ_Structuredhoare-logicverified-programmingword-ram
A small structured programming language on top of the deterministic word RAM CentralStrQ. Data-structure routines can be written in it as while/if programs and verified with Hoare rules that carry step budgets.
- Statements
Stmt: the straight-line instructionscopy,binary,bitNot; sequencingseq a b; the conditionalite cmp x y thn els(runthnif the unsigned comparisoncmp x yholds, otherwiseels); and the looploop cmp x y body(while cmp x y do body). - Compilation.
Stmt.compile s olists the instructions of placed at offset , with absolute jump targets, andStmt.size sis their number.Stmt.program sis the code of at offset followed byhalt.- A conditional compiles to
branch → then, the else-part,jump → end, then the then-part. - A loop compiles to
branch → body,jump → end, the body, thenjump → start.
- A conditional compiles to
- Code placement.
CodeIn P o l: the instructions occupy positions of . - Statement triples.
STriple s A B t: wherever the code of sits in a program , from every state at its first instruction that is running and whose memory and space usage satisfy , at most steps reach its end in a running state with .- Assertions see the space usage (
maxAddr, one more than the largest address touched). Space bounds are therefore proved with the same rules as correctness and running time.
- Assertions see the space usage (
- Conditions.
condEval cmp x y mis the value of the condition on memory .readReach x y m ris the space usage after reading the operands : a branch reads its operands, and the cells it touches count towardmaxAddr.
- Routines.
fillStmt ptr cnt val: while , write into cell , increment and decrement .bsearchStmt lo hi mid ptr baseC keyC: binary search for the first entry of a sorted array starting at address . Each round sets , then if the entry at is , else .BSearchHypbundles the hypotheses of the binary search. The four registers are distinct and differ from the input cells and the array cells. There is no overflow: . The array is sorted. bounds every address used.
Definition code
import Mathlib
import Definitions.Def_CentralStrQ_Machine
import Definitions.Def_CentralStrQ_Hoare
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Structured programs for the `CentralStrQ` word RAM: definitions
A small structured language on top of the published deterministic word RAM
`Def_CentralStrQ_Machine` (Kempa–Kociumaka, STOC 2027 submission, §3.2, p. 13; Hagerup's word
RAM) and the triples of `Def_CentralStrQ_Hoare`, so that data-structure routines can be written as
`while`/`if` programs and verified with Hoare rules that carry step budgets (C. A. R. Hoare,
*An axiomatic basis for computer programming*, CACM 12(10), 1969; R. W. Floyd, *Assigning
meanings to programs*, 1967).
* `Stmt`: straight-line instructions (`copy`, `binary`, `bitNot`), `seq`, `ite cmp x y thn els`
(run `thn` if `cmp x y` holds, else `els`) and `loop cmp x y body` (`while cmp x y do body`).
* `Stmt.compile s o`: the instructions of `s` placed at offset `o` (absolute jump targets);
`Stmt.size s` its length; `Stmt.program s`: the code of `s` at offset `0` followed by `halt`.
* `CodeIn P o l`: the instructions `l` occupy positions `o, …, o + |l| − 1` of `P`.
* `STriple s A B t`: wherever the code of `s` sits in a program, from every state at its start
(running, memory `m`, space usage `r` with `A m r`) at most `t` steps reach its end with `B`.
Assertions see the memory and the space usage (`maxAddr`), so space bounds are proved with
the same rules as functional correctness and running time.
* `condEval`, `readReach`: the value of a condition, and the space usage after reading its
operands (a branch reads its operands, which counts toward `maxAddr`).
* Routines: `fillStmt` (write one word into consecutive cells) and `bsearchStmt` (binary search
for the first entry `≥ key` of a sorted array), with the hypothesis bundle `BSearchHyp`.
-/
/-- Structured statements over the straight-line instructions of `CentralStrQ`. -/
inductive Stmt where
| skip
| copy (dst : Address) (src : Operand)
| binary (op : BinOp) (dst : Address) (x y : Operand)
| bitNot (dst : Address) (src : Operand)
| seq (a b : Stmt)
| ite (cmp : Comparison) (x y : Operand) (thn els : Stmt)
| loop (cmp : Comparison) (x y : Operand) (body : Stmt)
/-- Number of instructions of the compiled statement. -/
def Stmt.size : Stmt → ℕ
| .skip => 0
| .copy .. => 1
| .binary .. => 1
| .bitNot .. => 1
| .seq a b => a.size + b.size
| .ite _ _ _ a b => b.size + a.size + 2
| .loop _ _ _ b => b.size + 3
/-- Compilation to instructions placed at offset `o` (absolute jump targets).
`ite`: `branch → then`, else-part, `jump → end`, then-part.
`loop`: `branch → body`, `jump → end`, body, `jump → start`. -/
def Stmt.compile : Stmt → ℕ → List Instr
| .skip, _ => []
| .copy d s, _ => [.copy d s]
| .binary op d x y, _ => [.binary op d x y]
| .bitNot d s, _ => [.bitNot d s]
| .seq a b, o => a.compile o ++ b.compile (o + a.size)
| .ite c x y a b, o =>
.branch c x y (o + b.size + 2) ::
(b.compile (o + 1) ++ .jump (o + b.size + 2 + a.size) :: a.compile (o + b.size + 2))
| .loop c x y b, o =>
.branch c x y (o + 2) :: .jump (o + b.size + 3) :: (b.compile (o + 2) ++ [.jump o])
/-- The program of a statement: its code at offset `0`, then `halt`. -/
def Stmt.program (s : Stmt) : Program := s.compile 0 ++ [.halt]
/-- The instructions `l` occupy positions `o, …, o + |l| − 1` of `P`. -/
def CodeIn (P : Program) (o : ℕ) (l : List Instr) : Prop :=
∀ j, j < l.length → P[o + j]? = l[j]?
variable {w : Width}
/-- The machine is running at program counter `o` and `A` holds of its memory and space usage. -/
abbrev atPc (o : ℕ) (A : Memory w → ℕ → Prop) (st : State w) : Prop :=
st.pc = o ∧ st.halted = false ∧ A st.mem st.maxAddr
/-- **Statement triple.** Wherever the code of `s` is placed, from every state at its start whose
memory and space usage satisfy `A`, at most `t` steps reach its end with `B`. -/
def STriple (s : Stmt) (A B : Memory w → ℕ → Prop) (t : ℕ) : Prop :=
∀ (P : Program) (o : ℕ), CodeIn P o (s.compile o) →
Triple P (atPc o A) (atPc (o + s.size) B) t
/-- The value of the condition `cmp x y` on memory `m`. -/
def condEval (cmp : Comparison) (x y : Operand) (m : Memory w) : Bool :=
cmp.test (opRead m x) (opRead m y)
/-- The space usage after reading the operands `x` and `y`. -/
def readReach (x y : Operand) (m : Memory w) (r : ℕ) : ℕ :=
max r (reachOf (opTouched m x ++ opTouched m y))
@[simp] theorem condEval_lt (x y : Operand) (m : Memory w) :
condEval .lt x y m = decide ((opRead m x).toNat < (opRead m y).toNat) := rfl
@[simp] theorem condEval_le (x y : Operand) (m : Memory w) :
condEval .le x y m = decide ((opRead m x).toNat ≤ (opRead m y).toNat) := rfl
/-! ## Routines -/
/-- The loop body of `fill`: `[[ptr]] := [val]; [ptr] += 1; [cnt] -= 1`. -/
def fillBody (ptr cnt val : ℕ) : Stmt :=
.seq (.copy (.indirect ptr) (.memory (.direct val)))
(.seq (.binary .add (.direct ptr) (.memory (.direct ptr)) (.immediate 1))
(.binary .sub (.direct cnt) (.memory (.direct cnt)) (.immediate 1)))
/-- `fill`: while `0 < [cnt]`, run `fillBody`. Writes `[val]` into `[cnt]` consecutive cells
starting at address `[ptr]`. -/
def fillStmt (ptr cnt val : ℕ) : Stmt :=
.loop .lt (.immediate 0) (.memory (.direct cnt)) (fillBody ptr cnt val)
/-- Loop body of binary search: `[mid] := ([lo] + [hi]) / 2`, `[ptr] := [mid] + [baseC]`,
then `[lo] := [mid] + 1` if `[[ptr]] < [keyC]`, else `[hi] := [mid]`. -/
def bsearchBody (lo hi mid ptr baseC keyC : ℕ) : Stmt :=
.seq (.binary .add (.direct mid) (.memory (.direct lo)) (.memory (.direct hi)))
(.seq (.binary .shr (.direct mid) (.memory (.direct mid)) (.immediate 1))
(.seq (.binary .add (.direct ptr) (.memory (.direct mid)) (.memory (.direct baseC)))
(.ite .lt (.memory (.indirect ptr)) (.memory (.direct keyC))
(.binary .add (.direct lo) (.memory (.direct mid)) (.immediate 1))
(.copy (.direct hi) (.memory (.direct mid))))))
/-- Binary search (lower bound): while `[lo] < [hi]`, run `bsearchBody`. -/
def bsearchStmt (lo hi mid ptr baseC keyC : ℕ) : Stmt :=
.loop .lt (.memory (.direct lo)) (.memory (.direct hi)) (bsearchBody lo hi mid ptr baseC keyC)
section bsearch
variable (lo hi mid ptr baseC keyC base n key R : ℕ) (m₀ : Memory w)
/-- Hypotheses of binary search: distinct registers, array cells `base + j` (`j < n`) and the
input cells `baseC`, `keyC` untouched by the writes, no overflow, sorted array. -/
structure BSearchHyp : Prop where
regs : lo ≠ hi ∧ lo ≠ mid ∧ lo ≠ ptr ∧ hi ≠ mid ∧ hi ≠ ptr ∧ mid ≠ ptr
inputs : baseC ≠ lo ∧ baseC ≠ hi ∧ baseC ≠ mid ∧ baseC ≠ ptr ∧
keyC ≠ lo ∧ keyC ≠ hi ∧ keyC ≠ mid ∧ keyC ≠ ptr
fit : 2 * n + base + 1 < 2 ^ w.bits
baseV : (m₀ baseC).toNat = base
keyV : (m₀ keyC).toNat = key
sorted : ∀ i j, i ≤ j → j < n → (m₀ (base + i)).toNat ≤ (m₀ (base + j)).toNat
bound : base + n ≤ R ∧ lo + 1 ≤ R ∧ hi + 1 ≤ R ∧ mid + 1 ≤ R ∧ ptr + 1 ≤ R ∧
baseC + 1 ≤ R ∧ keyC + 1 ≤ R
arr : ∀ j < n, base + j ≠ lo ∧ base + j ≠ hi ∧ base + j ≠ mid ∧ base + j ≠ ptr
end bsearch
end CentralStrQ
Source
Structured-programming layer (Hoare 1969; R. W. Floyd, Assigning meanings to programs, 1967) for the published Hoare-triple infrastructure Def_CentralStrQ_Hoare. 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.