Structured Hoare logic: while-loop rule with a variant and an explicit step bound
ProvedCentralStrQ.STriple.loophoare-logicverified-programmingword-ram
Let () be assertions, read as "at most more rounds remain". Consider the loop . Suppose:
- (round) for every , a round that starts in , after the condition has been read and found true, ends within steps in some with ;
- (exit) the condition is false in .
Then for every , from the loop reaches its exit within
steps. It exits in a state that satisfies some with , after the condition has been read and found false.
Exact iteration counts are a special case. The variant form is needed for loops whose number of rounds depends on the data, such as binary search.
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
/-- **Loop rule with a variant.** `Inv k` means "at most `k` more rounds". If a round started in
`Inv (k + 1)` with the condition true ends in some `Inv k′` with `k′ ≤ k` within `tb` steps, and
the condition is false in `Inv 0`, then from `Inv n` the loop exits within `n·(tb + 2) + 2`
steps, in some `Inv k` with the condition false. -/
theorem STriple.loop {w : Width} {c : Comparison} {x y : Operand} {body : Stmt}
(Inv : ℕ → Memory w → ℕ → Prop) {tb : ℕ}
(hbody : ∀ n, STriple body
(fun m r => ∃ r₀, Inv (n + 1) m r₀ ∧ r = readReach x y m r₀ ∧ condEval c x y m = true)
(fun m r => ∃ k ≤ n, Inv k m r) tb)
(hzero : ∀ m r, Inv 0 m r → condEval c x y m = false) (n : ℕ) :
STriple (.loop c x y body) (Inv n)
(fun m r => ∃ k ≤ n, ∃ r₀, Inv k m r₀ ∧ r = readReach x y m r₀ ∧
condEval c x y m = false)
(n * (tb + 2) + 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.