Loops, calls and a do-block driver for framed memory triples #
Memory.FramedTriple w s p c S Q says that the memory program p, run from s at word size w,
issues only admissible operations, costs at most c memory units, changes no allocated cell
outside S and never lowers the frontier (Memory.Modifies S), and ends in a result and state
satisfying Q. This module adds the rules that a program verified for a realize certificate
needs beyond the primitive ones of Cslib.DataStructure.Memory.TripleFrame:
FramedTriple.iteratechecks a bounded loopiterate body d n a₀from an invariant. Each step, started at a state the loop may reach, meets the body's realization safety predicate and a framed triple re-establishing the invariant; the loop then meets its safety predicateiterationSafeand a framed triple. The step and the final postcondition receive the frame facts of the loop so far, so invariants need not repeat them.FramedTriple.callandFramedTriple.call_eqcall a sub-program through its own triple, whose modifies-set lies in the caller's; the continuation receives the sub-program's finer frame.FramedTriple.seqcontinues at the concrete state and result that the sub-program reaches, as safety predicates that name intermediate states require.FramedTriple.ite_maxandFramedTriple.dite_maxcombine branch costs bymax.
The tactic framed_triple walks a do block with these rules. It synthesizes the cost and compares
it with the stated bound at the end; it applies a primitive rule for each read, write, allocation
and return and a conditional rule for each if, and it takes a sub-program call through a triple
for it in the local context: by FramedTriple.call_eq when that triple fixes the result and
final state, and by FramedTriple.seq otherwise. Side conditions are tried with region_omega,
which proves address, region and word-size bounds (a < s.frontier, a ∈ S, a ∉ S and
S ⊆ T for regions built from Set.Ico, unions, insertions and set-builder intervals, and
Fits w v) by linear arithmetic over the plain arithmetic hypotheses, unfolding the
@[wram_arith] layout definitions and local definitions. The goals it cannot close remain in
the order of the program: postconditions at each pure, and unsolved side conditions where they
arise.
Usage #
A loop copyWordsProg a := iterate copyStep a a.len 0, whose body reads a.src + i, writes it to
a.dst + i and returns i + 1, is specified and proved as follows.
theorem copyWordsProg_spec (a : CopyArgs) (s₀ : Memory.State)
(hsrc_f : a.src + a.len ≤ s₀.frontier) (hdst_f : a.dst + a.len ≤ s₀.frontier)
(hdisj : a.src + a.len ≤ a.dst) (hfit_src : Fits w (a.src + a.len))
(hfit_dst : Fits w (a.dst + a.len)) (hfit_vals : ∀ i < a.len, Fits w (s₀.mem (a.src + i))) :
iterationSafe copyStep CopyStepSafe w a a.len 0 s₀ ∧
Memory.FramedTriple w s₀ (copyWordsProg a) (2 * a.len) (Set.Ico a.dst (a.dst + a.len))
fun _ t => ∀ i < a.len, t.mem (a.dst + i) = s₀.mem (a.src + i) := by
refine Memory.FramedTriple.iterate
(fun k i t => i = k ∧ ∀ j < k, t.mem (a.dst + j) = s₀.mem (a.src + j))
⟨rfl, by simp⟩ (fun k hk i t ⟨hi, hdone⟩ ⟨hfr, hframe⟩ => ?_) fun _ _ h _ => h.2
subst i
have hsrc := hframe (a.src + k) (by omega) (by region_omega)
have hval := hsrc ▸ hfit_vals k hk
refine ⟨⟨by region_omega, by region_omega, by region_omega⟩, ?_⟩
unfold copyStep
framed_triple
refine ⟨rfl, Nat.forall_lt_succ_right.2 ⟨fun j hj => ?_, by simp [hsrc]⟩⟩
simp [hj.ne, hdone j hj]
The first argument of FramedTriple.iterate is the invariant, the second its initial case, the
third the step, which receives the invariant and the frame Memory.Modifies S s₀ t of the loop so
far and returns the body's safety predicate and its triple, and the last derives the
postcondition. framed_triple closes the step's address, region and word-size conditions and its
cost, and leaves the postcondition of the step.
Main results #
Cslib.DataStructure.Memory.FramedTriple.iterate: bounded iteration with the loop's safety predicate.Cslib.DataStructure.Memory.FramedTriple.iterate_sum: bounded iteration whose stepkcostscost k, for a total of∑ k ∈ Finset.range n, cost k.Cslib.DataStructure.Memory.FramedTriple.iterate_potential: bounded iteration amortized by a potentialΦof the step index, for a total ofΦ 0 + B * n.Cslib.DataStructure.Memory.FramedTriple.call,Cslib.DataStructure.Memory.FramedTriple.call_eq,Cslib.DataStructure.Memory.FramedTriple.seq,Cslib.DataStructure.Memory.FramedTriple.call_eq_tail: calling a sub-program through its triple.Cslib.DataStructure.Memory.FramedTriple.ite_max,Cslib.DataStructure.Memory.FramedTriple.dite_max: conditionals with branch costs combined bymax.Cslib.DataStructure.Memory.FramedTriple.drop_fresh,Cslib.DataStructure.Memory.FramedTriple.weaken: dropping the cells above the initial frontier from a frame, and postconditions that use the frame.Cslib.DataStructure.Memory.FramedTriple.of_readOnly: a read-only program specified in the older form (history, unchanged state, result, cost) as a framed triple with an empty frame.Cslib.DataStructure.Memory.FramedTriple.univ_of_realization: a program specified in the older form (history, cost) as a framed triple over all of memory that fixes its result and state.
Main definitions #
region_omega: closes address, region and word-size side conditions;region_pruneclears the hypotheses that are not plain arithmetic before it calls the arithmetic closers.framed_triple_step,framed_triple,framed_walk,framed_close: one structural step, the walk of adoblock, the walk that leaves every postcondition to the author, and the closer of the goals the walk leaves.
The rules and tactics are original infrastructure over the existing memory clients.
Call a sub-program through its own framed triple, whose modifies-set lies in the caller's. The continuation runs from every result and state the sub-program may reach, with its postcondition and its finer frame. Costs add.
Call a sub-program whose framed triple fixes its result r and final state t; the
continuation runs from them. framed_triple applies this rule with a triple from the local
context.
Call a sub-program in tail position through its own framed triple, which fixes its result r
and final state t; the postcondition is checked there. framed_triple applies this rule with a
triple from the local context.
Call a sub-program through its own framed triple and continue at the concrete result and state that the realization interpreter assigns it, as a realization safety predicate that names intermediate states requires.
A conditional whose branch costs combine by max.
A dependent conditional whose branch costs combine by max.
Cells at or above the initial frontier are not allocated there, so a frame may drop them. A sub-program that works in a block allocated by the caller is called with the block in the caller's frame, which this rule then removes.
Weaken a postcondition with the help of the triple's own frame, and enlarge the frame.
Bounded iteration. Each of the n steps, run from a state satisfying the invariant and
reached from s within the frame S, meets the body's realization safety predicate and a framed
triple of cost c re-establishing the invariant. The loop then meets its iteration safety
predicate and a framed triple of cost c * n whose postcondition follows from the final invariant
and the loop's frame. Unlike iterate_framedTriple_of_step, the invariant need not restate the
frame of the loop so far.
Bounded iteration with varying step costs. As FramedTriple.iterate, but step k meets a
framed triple of cost cost k, and the loop costs ∑ k ∈ Finset.range n, cost k.
Bounded iteration amortized by a potential of the step index. As
FramedTriple.iterate, but step k meets a framed triple of some cost c' with
c' + Φ (k + 1) ≤ Φ k + B, and the loop costs Φ 0 + B * n.
Allocate a dirty block of k cells at a frontier fr known by an equation, then continue
from the enlarged state with the block ⟨fr, k⟩. Stating the frontier as fr avoids rewriting a
frontier expression inside the program.
A framed triple whose postcondition keeps the frontier gives the older form of a memory
specification: an admissible history, the cost bound, the unchanged frontier, every changed cell
in S, and the postcondition, read through the realization interpreter.
A program specified in the older form (admissible history, unchanged frontier, every changed
cell in S) meets a framed triple at its own cost that fixes its result and final state, so that
FramedTriple.call_eq can call it.
A read-only program specified in the older form (admissible history, unchanged state, result
r, cost at most c) meets a framed triple with an empty frame that fixes its result and leaves
the state unchanged, so that FramedTriple.call_eq and framed_triple can call it.
A program specified in the older form (admissible history, cost at most c, result r, final
state t) meets a framed triple over all of memory that fixes its result and final state; no
frame or frontier fact is needed, since no program lowers the frontier.
Clear every hypothesis that is a proposition other than plain arithmetic (comparisons of
natural numbers or integers, word-size bounds Fits w v, and their quantifier-free combinations),
so that the arithmetic closers of region_omega see only the facts they can use.
Close an address, region or word-size side condition: a < s.frontier, a ∈ S, a ∉ S,
S ⊆ T for regions built from Set.Ico, ∪, insert, singletons and set-builder intervals, or
Fits w v. It keeps only the plain arithmetic hypotheses (region_prune), unfolds interval
membership, structure projections and local let definitions (so a region may be named by a
let), and closes the goal by omega, by fits, or by
wram_omega over the @[wram_arith] layout definitions and the local definitions in context.
A primitive step of framed_triple_step: a read, write, allocation or return, with its side
conditions tried by region_omega.
One structural step of framed_triple on the main goal. A read, write, allocation or return
that appears literally is taken by its primitive rule; a call of a sub-program goes through a
framed triple for it in the local context (FramedTriple.call_eq when the triple fixes the
result and final state, FramedTriple.seq otherwise, and FramedTriple.call_eq_tail for a call
in tail position); then conditionals; and only then a
primitive rule up to unfolding of definitions, which enters a sub-program that has no triple in
context. Side conditions are tried with region_omega and left open otherwise.
Close a goal left by framed_triple, all or nothing: by reducible rfl, by omega (after
dsimp only if needed, which reduces structure projections), by True.intro up to unfolding, by an
assumption, by rfl, or by splitting a conjunction and closing each part the same way. Unlike
trivial it never tries decide or contradiction, which are slow to fail on large invariants.
Walk the do block of a Memory.FramedTriple goal with framed_triple_step. The cost is
synthesized and compared with the stated bound at the end by omega; side conditions are tried
with region_omega; postconditions are tried with framed_close, and the remaining goals are left
to the author in the order of the program. A sub-program call is taken through a triple for it in
the local context; a call whose continuation needs the sub-program's postcondition at an arbitrary
result is composed with FramedTriple.call by hand.
framed_triple without its attempts on the postconditions: walk the do block with
framed_triple_step, compare the synthesized cost with the stated bound by omega, and leave every
postcondition to the author. Use it when the postconditions are large invariants, on which even
failing closers cost time.