Verified word-RAM routine: fill writes one word into n consecutive cells in 5n + 2 steps
ProvedCentralStrQ.fill_specdata-structureshoare-logicverified-programmingword-ram
Let be three distinct cells.
Hypotheses.
- None of the target cells is one of these three cells.
- .
- The initial memory holds in cell and in cell .
Conclusion. The structured program fillStmt ptr cnt val, started on with space usage , reaches its end within steps in a memory such that:
- result: for every ;
- frame: every cell other than and outside is unchanged;
- space: the space usage is at most .
Preamble
import Mathlib import Definitions.Def_CentralStrQ_Machine import Definitions.Def_CentralStrQ_Hoare import Definitions.Def_CentralStrQ_Structured set_option autoImplicit false
Formal statement
namespace CentralStrQ
open WordRAM
/-- **`fill` writes `[val]` into `n` consecutive cells** starting at address `[ptr] = base` when
`[cnt] = n`, in at most `5n + 2` steps, changing no cell outside `[base, base + n)` other than
`ptr` and `cnt`, with space usage at most `max(r₀, base + n, ptr + 1, cnt + 1, val + 1)`. -/
theorem fill_spec {w : Width} (ptr cnt val base n r₀ : ℕ) (m₀ : Memory w)
(hpc : ptr ≠ cnt) (hpv : ptr ≠ val) (hcv : cnt ≠ val)
(hdisj : ∀ j < n, base + j ≠ ptr ∧ base + j ≠ cnt ∧ base + j ≠ val)
(hfit : base + n < 2 ^ w.bits)
(hptr : (m₀ ptr).toNat = base) (hcnt : (m₀ cnt).toNat = n) :
STriple (fillStmt ptr cnt val) (fun m r => m = m₀ ∧ r = r₀)
(fun m r => (∀ j < n, m (base + j) = m₀ val) ∧
(∀ a, a ≠ ptr → a ≠ cnt → (a < base ∨ base + n ≤ a) → m a = m₀ a) ∧
r ≤ max r₀ (max (base + n) (max (ptr + 1) (max (cnt + 1) (val + 1)))))
(n * 5 + 2) := by sorry
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.