Assembling structured programs #
A structured program (Code) assembles into a flat list of instructions (Code.assemble) whose
conditionals and loops are tests and jumps to absolute labels. A machine supplies a FlatMachine:
its instructions, among them the data instructions, an unconditional jump and a jump on each
test, and its step relation, with the four facts about these that assembly relies on. Every
structured execution is then a run of the assembled program with exactly the same number of
instructions (Exec.runs), so bounds proved with the rules of Program.Structured are bounds on
the flat machine.
Main definitions #
FlatMachine: the flat instructions and step relation of a machine with an instruction set.Code.assemble: assembly at a given label.
Main results #
Code.length_assemble: the assembled program hasCode.sizeinstructions.Exec.runs: a structured execution is a run of the assembled program inside any program.
References #
- MIT–ULB CompGeom Group, Undecidability of tiling with a tromino; assembly and
its correctness generalize
Cslib.WordRAM.Code.assembleandCslib.WordRAM.Exec.runs, which are adapted from its accompanying Lean formalization.
The flat instructions of a machine with instruction set I, and how it steps. A state is a
label and the data. The four step facts say that the instructions named here behave as data
instructions, jumps and tests; the machine may have other instructions, such as a halt.
- Instr : Type i
Flat instructions.
A data instruction.
Go to a label.
Go to a label if a test holds.
One step of a program, from a label and data to a label and data.
- step_op {p : List self.Instr} {pc : ℕ} {o : I.Op} {x y : I.Data} : p[pc]? = some (self.op o) → I.eval o x = some y → self.Step p (pc, x) (pc + 1, y)
A data instruction continues at the next label with the data it produces.
- step_goto {p : List self.Instr} {pc label : ℕ} {x : I.Data} : p[pc]? = some (self.goto label) → self.Step p (pc, x) (label, x)
A jump continues at its label.
- step_jumpIf_pos {p : List self.Instr} {pc label : ℕ} {t : I.Test} {x : I.Data} : p[pc]? = some (self.jumpIf t label) → I.Holds t x → self.Step p (pc, x) (label, x)
A test that holds continues at its label.
- step_jumpIf_neg {p : List self.Instr} {pc label : ℕ} {t : I.Test} {x : I.Data} : p[pc]? = some (self.jumpIf t label) → ¬I.Holds t x → self.Step p (pc, x) (pc + 1, x)
A test that fails continues at the next label.
Assemble a structured program to be placed at label pc.
Assembling c at any label gives c.size instructions.
Assembly is correct. A structured execution is a run of the assembled program placed after any prefix and before any suffix, from its first instruction to the label after it, with exactly the same number of instructions.