Register facts across calls #
A proof about a word-RAM program that calls subprograms carries the registers it still needs across
each call: the specification of a call gives its result state y and a frame, a statement
∀ i, i ≠ r → … → y.regs i = x.regs i of the registers the call leaves alone, and the proof needs
y.regs a = v for every register a the next call reads. Written by hand, each such fact names the
frames it passes, proves each of their premises at a, and names the hypothesis that gives the
value: (cf _ (by decide) (by decide)).trans ((bf _ (by decide)).trans ha).
regs_frame proves these facts from the hypotheses, without naming them, including across renamed
register banks (Data.comap, Data.extend) and framed calls (Data.havoc). wram_exec * and
wram_simp * use the same context scanner to supply register, frame, and memory-equality rewrites
automatically during symbolic execution.
Main definitions #
regs_frame: prove an equation between register reads and values, a conjunction orProp-valued structure of such equations, or a frame, by following each ready.regs aback through the frames, bank views, andData.havoclayers among the hypotheses to a hypothesis that states the value.wram_exec *,wram_simp *: symbolic execution and read simplification automatically resolving register equations, frames, bank views, andy.mem = x.memchains from the local context.
A register frame: a proof of a statement ∀ xs, s'.regs i = s.regs i with i among xs.
- proof : Lean.Expr
The proof.
- type : Lean.Expr
Its statement.
- name : Lean.MessageData
How error messages name the frame.
The facts about registers and whole-memory / cell equalities among the hypotheses.
The frames, by their later state
s'.The values:
(s, a) ↦ (v, h)forh : s.regs a = v(orf a = vforf : Fin r → ℕ).Memory equalities:
s' ↦ (s, h)forh : s'.mem = s.mem.Memory cell values:
(s, a) ↦ (v, h)forh : s.mem a = v.
The facts about registers among the hypotheses.
The reads that could not be followed further, each with the frames of its state and the premise of each that could not be proved.
Reduce a single register read d.regs j across Data.extend:
(y.extend f x).regs (f i) = y.regs iusingproveBankInjective? f(y.extend f x).regs j = x.regs jwhen a hypothesis givesj ∉ Set.range for∀ k, f k ≠ jFin.castAdd/Fin.castLE/Fin.natAddnumeric and symbolic embeddings- closed finite banks
f : Fin n → Fin swith literalj.
Rewrite the reads of registers in e along the frames.
Split the goal into the components of conjunctions and Prop-valued structures.
Prove the equation g along the frames F, or return what is left of it.
Attempt to close g with regs_frame (including intros and structure splitting).
Restores the state and returns false on failure.
Rewrite the leaf register and memory reads in e using F (register equations, frames,
Exec/ExecWithin.regs_eq, Data.extend/Data.comap/Data.havoc, s'.mem = s.mem, and
s.mem a = v).
wram_exec symbolically executes structured word-RAM code (Cslib.WordRAM.Code) on a goal
∃ y, ExecWithin w c x y T ∧ P y, ExecWithin w c x y T or Cslib.WordRAM.WP w c x Q. It runs
the data instructions and conditionals of c in order, with the data in closed form, until the
end of the program, where t ≤ T ∧ P y (respectively t ≤ T ∧ y' = y, or Q y t) remains for
the data y and instruction count t reached; a leading bound t ≤ T is closed by omega when
it can. It stops at a loop, a random sample or a program given by name, leaving WP w c' y Q':
continue with refine WP.of_execWithin h ?_ from a specification h of c', then wram_exec
again. Name a program among the hints to execute it instead.
On the exact-count goals ∃ y, Exec w c x y T ∧ P y, Exec w c x y T and
Cslib.WordRAM.WP.Exact w c x Q it works the same way with the rules of WP.Exact: the count
t = T is closed by omega when it can, and refine WP.Exact.of_exec h ?_ continues from an
exact specification h at a stop.
wram_exec [h₁, h₂]adds simp lemmas for the values read, typically the contents of the registers ofx, such asx.regs zeroP = 0, and the memory cells it reads. A definition of a program among them, as inwram_exec [program, h₁], is unfolded where execution reaches it; programs that are not listed stay closed. The hint opens the program in this call only, whileunfold programrewrites the whole goal: a proof in which a laterwram_execorwram_callmeets the same program again keeps theunfold.wram_exec (disch := tac)sets the discharger, by defaultfitson a word-size bound andomegaon anything else. It proves side conditions (Fits w v,b ≤ afor an exact subtraction, a nonzero divisor), decides conditions of branches, and decides whether two memory addresses are equal. Register names, and side conditions without free variables, are decided by evaluation, and a side condition that is a hypothesis is proved by it, beforetacruns. A lemma such ashfit : ∀ v, v ≤ b → Fits w vamong the given lemmas proves fitting conditions withtac.
A conditional neither simp nor the discharger decides splits into both alternatives, tagged
pos and neg, with the hypothesis hc on the compared values; wram_exec -split stops at it
instead, leaving WP w (.branch …) y Q', for instance to join the alternatives with WP.cut.
Side conditions that remain are left as goals after the main ones, tagged by the premise they come
from (hfit, hle, hb).
Extensions:
wram_exec *executes structured word-RAM code just likewram_exec, and additionally resolves register reads (s.regs i), frames (∀ i, ... → y.regs i = x.regs i,Exec/ExecWithin.regs_eq), bank views (Data.extend/Data.comap),Data.havoc, memory equalities (y.mem = x.mem), and pointwise memory facts (x.mem a = v) from the local context automatically, so callers need not list[h0, h1, h2, ...]by hand.
wram_simp [h₁, h₂] simplifies reads of registers and memory after Data.set and
Data.write with the simp set of wram_exec, typically in the postcondition wram_exec leaves.
wram_simp (disch := tac) sets the discharger, by default fits on a word-size bound and omega
on anything else, and wram_simp at h works on
hypotheses.
Extensions:
wram_simp *simplifies reads of registers and memory likewram_simp, also resolving local register facts, frames,Data.extend/Data.comap/Data.havoc, ands'.mem = s.memchains automatically.
regs_frame proves a fact about the registers after a sequence of calls from the frames of the
calls: an equation such as z.regs a = v, a conjunction or Prop-valued structure of such
equations, such as Parameters L H z, or the frame ∀ i, i ≠ r → i ≠ r' → z.regs i = x.regs i of
the sequence.
It follows each read z.regs a in the goal back to a hypothesis that states the value:
- through a frame among the hypotheses,
yf : ∀ i, i ≠ r → … → z.regs i = y.regs i, whose premises hold ata: by evaluation when they have no free variables (a predicate introduced byletis unfolded first), otherwise by a hypothesis; - through an execution
he : ExecWithin w c y z T(orExec, or anyJwith a lemmaJ.regs_eq), when the codecdoes not writea, decided in the kernel whencis closed and structurally whenccontains parameters; - through
Data.set,Data.write,Data.extend,Data.comap,Data.havoc, and the definition of a state introduced bylet; - to the value given by
h : x.regs a = v, or by a component of a conjunction or a field of a hypothesishx : Parameters L H x.
When several frames of a state hold at a, the tactic follows the latest hypothesis among them and
does not return to the others. A value stated only at an intermediate state that this frame skips
is therefore not found, and the tactic fails with the error described below.
No hypothesis is named, and a value the goal leaves open is filled in, so that the facts a
specification asks for can be given as exec_callee y (by regs_frame) (by regs_frame). When a
read cannot be followed to the stated value, the error shows the equation that is left and, for
each frame of the state it stopped at, the premise that does not hold at the register.