Documentation

CslibFrontier.Computability.PointerMachine.Blocks

Composing pointer-machine instruction blocks #

Relocation shifts internal branch targets and replaces halt by a jump to a continuation. Executes.inBlock turns a proved halting run into a run of the same instruction count inside a larger concrete program. The old halt is charged as the continuation jump. Registers and heap are preserved exactly by this translation; only program counters change.

def Cslib.PointerMachine.Instr.relocate {M : Model} {L : Layout} (base next : ℕ) :
Instr M L → Instr M L

Shift branch labels by base and turn halt into a jump to next. Other instructions are unchanged.

@[simp]
theorem Cslib.PointerMachine.Instr.writesData_relocate {M : Model} {L : Layout} (i : Instr M L) (base next : ℕ) (field : Fin L.dataFields) :
(relocate base next i).WritesData field ↔ i.WritesData field

Relocating control flow does not change which scalar fields an instruction can write.

theorem Cslib.PointerMachine.Executes.inBlock {M : Model} {L : Layout} {code : List (Instr M L)} {x y : State M L} {time : ℕ} (exec : Executes code x y time) (pre post : List (Instr M L)) (next : ℕ) :
Runs (pre ++ List.map (Instr.relocate pre.length next) code ++ post) { pc := pre.length + x.pc, ptr := x.ptr, data := x.data, heap := x.heap } { pc := next, ptr := y.ptr, data := y.data, heap := y.heap } time

A halting block runs inside any prefix and suffix, with the same instruction count. Each former halt jumps to the absolute continuation label next. The resulting state is the original result with only its program counter replaced by that label.

def Cslib.PointerMachine.branch {M : Model} {L : Layout} (test : M.Test) (r s : Fin L.dataRegs) (yes no : List (Instr M L)) :
List (Instr M L)

Choose one halting block by a scalar test. Blocks retain their internal control flow; their halts jump to the final halt of this program.

theorem Cslib.PointerMachine.Executes.branch {M : Model} {L : Layout} (test : M.Test) (r s : Fin L.dataRegs) (yes no : List (Instr M L)) {x y : State M L} {time : ℕ} (pc : x.pc = 0) (exec : Executes (if M.evalTest test (x.data r) (x.data s) = true then yes else no) x y time) :
Executes (PointerMachine.branch test r s yes no) x { pc := 1 + no.length + yes.length, ptr := y.ptr, data := y.data, heap := y.heap } (time + 2)

Executing a conditional block costs the selected block's time plus two instructions: the initial test and the final halt. The selected block's old halt is a charged jump.

def Cslib.PointerMachine.sequence {M : Model} {L : Layout} (first second : List (Instr M L)) :
List (Instr M L)

Run two halting blocks consecutively. The first halt jumps to the second block, and the second halt jumps to the single final halt.

theorem Cslib.PointerMachine.Executes.sequence {M : Model} {L : Layout} {first second : List (Instr M L)} {x y z : State M L} {time₁ time₂ : ℕ} (h₁ : Executes first x y time₁) (h₂ : Executes second { pc := 0, ptr := y.ptr, data := y.data, heap := y.heap } z time₂) :
Executes (PointerMachine.sequence first second) x { pc := first.length + second.length, ptr := z.ptr, data := z.data, heap := z.heap } (time₁ + time₂ + 1)

Sequential block composition preserves the second result and charges the two block costs plus one final halt. The second block enters at its own label zero; the first keeps its original entry label.

@[simp]
theorem Cslib.PointerMachine.sequence_no_data_writes {M : Model} {L : Layout} (first second : List (Instr M L)) (field : Fin L.dataFields) :
(∀ i ∈ sequence first second, ¬i.WritesData field) ↔ (∀ i ∈ first, ¬i.WritesData field) ∧ ∀ i ∈ second, ¬i.WritesData field

Sequential composition introduces no scalar writes or allocation of its own.

@[simp]
theorem Cslib.PointerMachine.branch_no_data_writes {M : Model} {L : Layout} (test : M.Test) (r s : Fin L.dataRegs) (yes no : List (Instr M L)) (field : Fin L.dataFields) :
(∀ i ∈ branch test r s yes no, ¬i.WritesData field) ↔ (∀ i ∈ yes, ¬i.WritesData field) ∧ ∀ i ∈ no, ¬i.WritesData field

Branch selection introduces no scalar writes or allocation of its own.