Structured programs over an instruction set #
Flat programs are lists of instructions with numeric jump targets, which are inconvenient to
write and to reason about. A structured program (Code) is built from data instructions by
sequencing, conditionals and loops on tests. This file defines structured programs and their
cost-exact semantics once, for every machine: a machine supplies an InstructionSet, that is its
data, its data instructions and tests, and their meaning.
The big-step semantics Exec counts exactly the instructions of the flat program a structured
program assembles to (Code.assemble and Exec.runs in Program.Assemble). The costs assume a
machine whose test instruction jumps when the test holds:
- a conditional costs its test, plus a jump when the second alternative runs;
Code.loop t body(whiletholds) costs a test and a jump per test;Code.loopUntil t body(whiletfails) costs a test and a jump per iteration and one test to leave.
ExecWithin bounds the count instead, and its rules compose like Hoare logic, with time bounds
added along sequences. Code.eval is a structurally recursive interpreter with a budget of loop
iterations, so simp and decide evaluate it, and Exec.of_eval turns its result into an
execution.
Main definitions #
InstructionSet: data, data instructions and tests of a machine, with their meaning.Code,Code.size,Code.seqList: structured programs, the length of the flat program they assemble to, and the sequence of a list of programs.Exec,ExecWithin: executions with an exact instruction count, and within a bound.Code.eval: an interpreter with a budget of loop iterations.
Main results #
Exec.unique: executions are deterministic.Exec.invariant,Exec.invariant_ops: a property that every data instruction, or every one occurring in the program, preserves is preserved by executions.ExecWithin.loop_invariant,ExecWithin.loop_potential,ExecWithin.loopUntil_invariant,ExecWithin.loopUntil_potential: loop rules.Exec.of_eval: the interpreter only finds executions.
References #
- MIT–ULB CompGeom Group, Undecidability of tiling with a tromino; the
structured programs and their cost-exact semantics generalize
Cslib.WordRAM.Code, which is adapted from its accompanying Lean formalization.Code.skipandCode.loopUntilare new.
The data of a machine, its data instructions and its tests, with their meaning. Control flow
is not part of an instruction set: structured programs (Code) supply it.
Structured programs over data instructions Op and tests Test.
- skip {Op : Type v} {Test : Type w} : Code Op Test
Do nothing; no instruction.
- op {Op : Type v} {Test : Type w} (o : Op) : Code Op Test
A data instruction.
- seq {Op : Type v} {Test : Type w} (c₁ c₂ : Code Op Test) : Code Op Test
Run
c₁, thenc₂. - branch {Op : Type v} {Test : Type w} (t : Test) (pos neg : Code Op Test) : Code Op Test
Run
posiftholds, andnegotherwise. - loop {Op : Type v} {Test : Type w} (t : Test) (body : Code Op Test) : Code Op Test
Run
bodywhiletholds. - loopUntil {Op : Type v} {Test : Type w} (t : Test) (body : Code Op Test) : Code Op Test
Run
bodyuntiltholds.
Executions #
Exec I c x y n: the structured program c leads from data x to data y executing exactly
n instructions of the flat program it assembles to.
- skip {I : InstructionSet} {x : I.Data} : Exec I Code.skip x x 0
skipis no instruction. - op {I : InstructionSet} {o : I.Op} {x y : I.Data} : I.eval o x = some y → Exec I (Code.op o) x y 1
A data instruction is one instruction.
- seq {I : InstructionSet} {c₁ c₂ : Code I.Op I.Test} {x y z : I.Data} {m n : ℕ} : Exec I c₁ x y m → Exec I c₂ y z n → Exec I (c₁.seq c₂) x z (m + n)
Sequencing adds the instruction counts.
- branch_pos {I : InstructionSet} {t : I.Test} {pos neg : Code I.Op I.Test} {x y : I.Data} {n : ℕ} : I.Holds t x → Exec I pos x y n → Exec I (Code.branch t pos neg) x y (n + 1)
The first alternative of a conditional costs the test.
- branch_neg {I : InstructionSet} {t : I.Test} {pos neg : Code I.Op I.Test} {x y : I.Data} {n : ℕ} : ¬I.Holds t x → Exec I neg x y n → Exec I (Code.branch t pos neg) x y (n + 2)
The second alternative of a conditional costs the test and a jump.
- loop_exit {I : InstructionSet} {t : I.Test} {body : Code I.Op I.Test} {x : I.Data} : ¬I.Holds t x → Exec I (Code.loop t body) x x 2
Leaving a
loopcosts the test and a jump. - loop_iter {I : InstructionSet} {t : I.Test} {body : Code I.Op I.Test} {x y z : I.Data} {m n : ℕ} : I.Holds t x → Exec I body x y m → Exec I (Code.loop t body) y z n → Exec I (Code.loop t body) x z (m + n + 2)
An iteration of a
loopcosts its body, the test and a jump back. - loopUntil_exit {I : InstructionSet} {t : I.Test} {body : Code I.Op I.Test} {x : I.Data} : I.Holds t x → Exec I (Code.loopUntil t body) x x 1
Leaving a
loopUntilcosts the test. - loopUntil_iter {I : InstructionSet} {t : I.Test} {body : Code I.Op I.Test} {x y z : I.Data} {m n : ℕ} : ¬I.Holds t x → Exec I body x y m → Exec I (Code.loopUntil t body) y z n → Exec I (Code.loopUntil t body) x z (m + n + 2)
An iteration of a
loopUntilcosts its body, the test and a jump back.
ExecWithin I c x y T: the structured program c leads from x to y within T
instructions.
A property that every data instruction preserves is preserved by executions.
A property that every data instruction occurring in the program preserves is preserved by its executions.
An execution is within its instruction count.
Time bounds #
An execution whose instruction count is within the budget.
Monotonicity in the instruction budget.
A property that every data instruction preserves is preserved by executions.
skip executes within any bound.
A data instruction executes within one instruction.
Sequencing adds the instruction bounds.
The first alternative of a conditional adds one instruction to the bound.
The second alternative of a conditional adds two instructions to the bound.
A conditional, with one bound for both alternatives.
Leaving a loop takes two instructions.
An iteration of a loop followed by the rest of it.
Leaving a loopUntil takes one instruction.
An iteration of a loopUntil followed by the rest of it.
Loop rule, invariant and potential. If the body leads every data x satisfying the
invariant P and the test to data y satisfying P, within T instructions with
T + 2 + Φ y ≤ Φ x, then from data satisfying P the loop ends within Φ x + 2 instructions,
in data satisfying P where the test fails. The two extra instructions of an iteration are the
test and the jump back; the time of the body may differ between iterations, as long as the
potential Φ pays for it.
Loop rule, invariant and measure. If the body leads every data satisfying the invariant
P and the test, within T instructions, to data satisfying P with a smaller measure μ,
then from data satisfying P the loop ends within μ x * (T + 2) + 2 instructions, in data
satisfying P where the test fails.
Until rule, invariant and potential. If the body leads every data x satisfying the
invariant P where the test fails to data y satisfying P, within T instructions with
T + 2 + Φ y ≤ Φ x, then from data satisfying P the loop ends within Φ x + 1 instructions,
in data satisfying P and the test.
Until rule, invariant and measure. If the body leads every data satisfying the invariant
P where the test fails, within T instructions, to data satisfying P with a smaller measure
μ, then from data satisfying P the loop ends within μ x * (T + 2) + 1 instructions, in data
satisfying P and the test.
Interpreter #
Run a structured program, allowing each loop at most fuel iterations per entry:
some (y, n) if it leads to y executing n instructions, and none if it faults or runs out
of iterations. The definition is structurally recursive, so simp and decide evaluate it.
An interpreter result within the budget is an execution within it.