Documentation

CslibFrontier.Computability.PointerMachine.Assemble

Assembling structured pointer-machine programs #

The pointer machine is a Program.FlatMachine for its instruction set (flatMachine), so a structured program (Code) assembles into a flat list of instructions (Code.assemble) whose conditionals and loops are tests and jumps to absolute labels. Every structured execution is a run of the assembled program with exactly the same number of instructions (Exec.runs), so bounds proved with the rules of Structured are bounds on the machine of Defs.

The assembled program followed by halt (Code.program) is an ordinary flat program, so it composes with hand-written ones through Executes.inBlock, sequence and branch of Blocks.

Main definitions #

Main results #

theorem Cslib.PointerMachine.step_op {M : Model} {L : Layout} {p : List (Instr M L)} {x : State M L} {pc : ℕ} {o : Op M L} (h : p[pc]? = some o.instr) :
step p { pc := pc, ptr := x.ptr, data := x.data, heap := x.heap } = some (have __src := o.effect x; { pc := pc + 1, ptr := __src.ptr, data := __src.data, heap := __src.heap })

A data instruction steps to the next label with its effect.

theorem Cslib.PointerMachine.step_goto {M : Model} {L : Layout} {p : List (Instr M L)} {x : State M L} {pc label : ℕ} (h : p[pc]? = some (Instr.goto label)) :
step p { pc := pc, ptr := x.ptr, data := x.data, heap := x.heap } = some { pc := label, ptr := x.ptr, data := x.data, heap := x.heap }

A goto steps to its label.

theorem Cslib.PointerMachine.step_test_pos {M : Model} {L : Layout} {p : List (Instr M L)} {x : State M L} {pc : ℕ} {t : Test M L} {label : ℕ} (h : p[pc]? = some (Test.instr label t)) (ht : Test.Holds x t) :
step p { pc := pc, ptr := x.ptr, data := x.data, heap := x.heap } = some { pc := label, ptr := x.ptr, data := x.data, heap := x.heap }

A test that holds steps to its label.

theorem Cslib.PointerMachine.step_test_neg {M : Model} {L : Layout} {p : List (Instr M L)} {x : State M L} {pc : ℕ} {t : Test M L} {label : ℕ} (h : p[pc]? = some (Test.instr label t)) (ht : ¬Test.Holds x t) :
step p { pc := pc, ptr := x.ptr, data := x.data, heap := x.heap } = some { pc := pc + 1, ptr := x.ptr, data := x.data, heap := x.heap }

A test that fails steps to the next label.

@[reducible]

The pointer machine as a flat machine for its instruction set. A state of the flat machine is a label and a state whose own program counter is ignored.

@[reducible, inline]
abbrev Cslib.PointerMachine.Code.assemble {M : Model} {L : Layout} (c : Code M L) (pc : ℕ) :
List (Instr M L)

Assemble a structured program to be placed at label pc.

@[simp]

Assembling c at any label gives c.size instructions.

def Cslib.PointerMachine.Code.program {M : Model} {L : Layout} (c : Code M L) :
List (Instr M L)

The complete program: the assembled structured program followed by halt.

theorem Cslib.PointerMachine.Exec.runs {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {n : ℕ} (h : Exec c x y n) (pre post : List (Instr M L)) :
Runs (pre ++ c.assemble pre.length ++ post) { pc := pre.length, ptr := x.ptr, data := x.data, heap := x.heap } { pc := pre.length + Program.Code.size c, ptr := y.ptr, data := y.data, heap := y.heap } n

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.

theorem Cslib.PointerMachine.Exec.executes {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {n : ℕ} (h : Exec c x y n) (pc : x.pc = 0) :
Executes c.program x { pc := Program.Code.size c, ptr := y.ptr, data := y.data, heap := y.heap } (n + 1)

A structured execution is a halting run of the complete program, with one more instruction for the halt.

theorem Cslib.PointerMachine.ExecWithin.executes {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (h : ExecWithin c x y T) (pc : x.pc = 0) :
∃ time ≤ T + 1, Executes c.program x { pc := Program.Code.size c, ptr := y.ptr, data := y.data, heap := y.heap } time

A structured execution within T instructions is a halting run of the complete program within T + 1.