Notation for structured word-RAM programs #
ram[ s₁; s₂; … ] writes a structured program (Cslib.WordRAM.Code) as a sequence of statements,
one per data instruction, in the style of word-RAM pseudocode. Registers are named by identifiers,
for instance local abbreviations of elements of Fin r. The statements are:
d ← #v: set registerdto the constantv;d ← s: copy registersinto registerd;d ← i + j, and likewise-,*,/,%,&&&,|||,^^^,<<<,>>>: a binary operation (Cslib.WordRAM.BinOp) on registersiandj;d ← *a,*a ← s: load from and store to the memory cell whose address is in registera;d ← width%: set registerdto the word size;d ← rand s: sample uniformly from[0, regs s]into registerd;if i = j then [ … ] else [ … ],if i < j then [ … ] else [ … ]: a conditional;while i = j do [ … ],while i < j do [ … ]: a loop;⟪c⟫: the structured programc, for instance a subroutine.
The notation unfolds to the constructors of Cslib.WordRAM.Code, so it adds nothing to the
semantics. The binary operations and width% are instructions of the standard basis
(Cslib.WordRAM.Op.bin, Cslib.WordRAM.Op.width); the other statements work over every basis.
It is scoped: open Cslib.WordRAM enables it, with its statement forms, their expansions and the
tactic ram_eval. Only the category wramStmt and the syntax abbreviation wramBlock are
global, since Lean cannot scope them; without the open, wramStmt has no rules, so they parse
nothing. The statement forms follow the word-RAM notation of Algolean. Its only
new tokens are ram[, width% and the internal ram_stmt% and ram_block%, so it reserves no
identifiers: ram and width remain ordinary names.
Main definitions #
ram[ s₁; s₂; … ]: scoped notation for structured word-RAM programs (Cslib.WordRAM.Code).
Tactics #
ram_eval,ram_eval n(scoped, like the notation): symbolic execution. On a goalCslib.WordRAM.ExecWithin w c x y TorCslib.WordRAM.Exec w c x y T, it runs the interpreterCslib.WordRAM.Code.evaloncandxwithsimp, allowing each loopniterations (default0, for loop-free code), and closes the goal when the result isywithin (exactly, forExec)Tinstructions; withya metavariable, it determinesy. Which constants and results fit a word is decided for numerals, byomegafrom theFitshypotheses in context (Cslib.WordRAM.reduceFits), so that a bound implied by a fact such asFits w (base + n)needs no statement of its own, or else by the given simp lemmas. Programs given by name are unfolded by passing their definitions among the simp lemmas, as inram_eval [myProgram, h]. When it fails, the error names the failing step: the goal has neither form, a given simp lemma does not elaborate,simpcannot evaluate the program, the interpreter returnsnone, a condition remains undecided (shown), oromegacannot prove the instruction bound (shown). Undertryandfirst, which discard the message, it fails without this replay, at the cost of the attempt alone.ram_evalandram_exact_evalboth expand to the internal tacticram_eval_core, which holds the shared steps; they differ only in their simp set,ram_eval_simpsorram_exact_eval_simps, whose lemmas for the instructions of the basis differ. The sets are filled, and so indexed, once (ram_eval_simp_set), so a call indexes only the simp lemmas it is given.
References #
A statement of a structured word-RAM program.
Instances For
A block of statements, separated by semicolons.
Instances For
A structured word-RAM program, written as a block of statements.
Instances For
Internal: a nested block of statements, as a structured program.
Instances For
Internal: a single statement, as a structured program.
Instances For
Load from the memory cell whose address is in a register.
Instances For
Store to the memory cell whose address is in a register.
Instances For
Sample uniformly from [0, regs s] into a register.
Instances For
A conditional on the equality of two registers.
Instances For
A conditional on the comparison of two registers.
Instances For
A loop while two registers are equal.
Instances For
A loop while one register is smaller than another.
Instances For
A structured program given as a term.
Instances For
Symbolically execute structured code on a goal ExecWithin w c x y T or Exec w c x y T,
allowing each loop the given number of iterations (default 0). Whether a written number fits a
word is decided by omega from the Fits hypotheses in context (Cslib.WordRAM.reduceFits),
or by the given simp lemmas.
On failure the error names the step that failed: the goal has neither form, a given simp lemma
does not elaborate, simp cannot evaluate the program (pass its definition, as in
ram_eval [myProgram]), the interpreter returns none (a fault, or too few loop iterations), a
branch or Fits condition remains (shown), or omega cannot prove the instruction bound (shown).
Under try or first, which discard the message, it fails without working out which step.
Instances For
Internal: the symbolic execution behind ram_eval and ram_exact_eval. The string names the
calling tactic in error messages and the number is the loop fuel; the identifier names the simp set
that evaluates the program, ram_eval_simps or ram_exact_eval_simps, and the list holds the
caller's simp lemmas.
Instances For
Internal: simp +decide only with the simp set set (ram_eval_simps or
ram_exact_eval_simps), the simplification procedures of ram_eval
(Cslib.WordRAM.RamEval.simprocs), and the given simp lemmas. It is
simp +decide only [l₁, …, lₙ, args] for the lemmas l₁, …, lₙ with which ram_eval_simp_set
filled set, but it does not index them again.
Instances For
Internal: fill the empty simp set set with the entries that simp only [names] builds from
the simp lemmas and definitions names, in their order, so that ram_eval_simp set [args] is
simp +decide only [names, args]. Simplification procedures belong in
Cslib.WordRAM.RamEval.simprocs.
Instances For
The simplification procedures of ram_eval and ram_exact_eval, in the order in which they
are added, with true for those that rewrite after visiting the subterms (post). They are added
by name on each call, since the builtin ones cannot be tagged into a simp set in a module.
Instances For
The simp context of ram_eval_simp set [args], given stx := simp +decide only [args]: the
context that mkSimpContext builds for simp +decide only [l₁, …, lₙ, args], where
ram_eval_simp_set filled set with l₁, …, lₙ, and simprocs come among the lᵢ. The theorems
of set are taken as indexed, and args are elaborated into the same tree, so simp searches one
tree, as it does for the literal list.
Instances For
Internal: fill the simp set set of ram_eval (ram_eval_simps) or ram_exact_eval
(ram_exact_eval_simps), given the simp lemmas basis that evaluate the instructions of the
basis. The simplification procedures among the lemmas are Cslib.WordRAM.RamEval.simprocs.