Documentation

CslibFrontier.Computability.PointerMachine.Structured

Structured pointer-machine programs #

Flat programs are lists of Instr with numeric labels, which are inconvenient to write and to reason about. This file makes the pointer machine an instruction set (instructionSet) for the structured programs of Cslib.Program: its data instructions (Op) and its two tests (Test), acting on states. Code, Exec and ExecWithin are those of Cslib.Program at this instruction set, so the sequencing, conditional and loop rules there (Program.ExecWithin.seq, Program.ExecWithin.loopUntil_invariant, …) apply to them as they are, also by field notation. This file adds what is specific to the machine: the effect of a data instruction, and the program counter, validity and field frame of an execution.

Exec c x y n relates states and ignores the program counter: y.pc = x.pc (Exec.pc_eq). The count n is exactly that of the flat program c assembles to (Exec.runs in Assemble).

The machine jumps when a test holds, so Code.loopUntil t body (while t fails) costs one test and one jump per iteration; it is the natural loop for while r ≠ null.

Loop-free code is executed by the interpreter Code.eval, which simp evaluates on symbolic states (ExecWithin.of_eval).

Main definitions #

Main results #

Registers and records after an assignment #

@[simp]
theorem Cslib.PointerMachine.State.ptr_setPtr {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (p : Pointer) :

The pointer registers after a pointer assignment.

@[simp]
theorem Cslib.PointerMachine.State.data_setPtr {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (p : Pointer) :
(x.setPtr r p).data = x.data

A pointer assignment keeps the data registers.

@[simp]
theorem Cslib.PointerMachine.State.heap_setPtr {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (p : Pointer) :
(x.setPtr r p).heap = x.heap

A pointer assignment keeps the records.

@[simp]
theorem Cslib.PointerMachine.State.pc_setPtr {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (p : Pointer) :
(x.setPtr r p).pc = x.pc

A pointer assignment keeps the program counter.

@[simp]
theorem Cslib.PointerMachine.State.ptr_setData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (v : M.Value) :
(x.setData s v).ptr = x.ptr

A data assignment keeps the pointer registers.

@[simp]
theorem Cslib.PointerMachine.State.data_setData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (v : M.Value) :

The data registers after a data assignment.

@[simp]
theorem Cslib.PointerMachine.State.heap_setData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (v : M.Value) :
(x.setData s v).heap = x.heap

A data assignment keeps the records.

@[simp]
theorem Cslib.PointerMachine.State.pc_setData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (v : M.Value) :
(x.setData s v).pc = x.pc

A data assignment keeps the program counter.

@[simp]
theorem Cslib.PointerMachine.State.deref_setData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (v : M.Value) (r : Fin L.ptrRegs) :
(x.setData s v).deref r = x.deref r

A data assignment keeps the record a register specifies.

theorem Cslib.PointerMachine.State.deref_setPtr_some {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (a : ℕ) :
(x.setPtr r (some a)).deref r = x.heap[a]?

After assigning it a record's address, a register specifies that record.

After assigning it null, a register specifies no record.

theorem Cslib.PointerMachine.State.deref_setPtr_of_ne {M : Model} {L : Layout} (x : State M L) {r r' : Fin L.ptrRegs} (h : r' ≠ r) (p : Pointer) :
(x.setPtr r p).deref r' = x.deref r'

A pointer assignment keeps the record another register specifies.

inductive Cslib.PointerMachine.Op (M : Model) (L : Layout) :

The data instructions of a pointer machine: those of Instr other than jumps and halt.

def Cslib.PointerMachine.Op.instr {M : Model} {L : Layout} :
Op M L → Instr M L

The instruction of a data instruction.

def Cslib.PointerMachine.Op.effect {M : Model} {L : Layout} (o : Op M L) (x : State M L) :
State M L

The effect of a data instruction on registers and records.

The tests of a pointer machine.

def Cslib.PointerMachine.Test.Holds {M : Model} {L : Layout} (x : State M L) :
Test M L → Prop

Whether a test holds in a state.

Instances For
    @[instance_reducible]
    instance Cslib.PointerMachine.instDecidableHolds {M : Model} {L : Layout} (x : State M L) (t : Test M L) :
    def Cslib.PointerMachine.Test.instr {M : Model} {L : Layout} (label : ℕ) :
    Test M L → Instr M L

    The instruction that goes to label if the test holds.

    @[reducible]

    The pointer machine as an instruction set for structured programs: data instructions and tests on states. No data instruction faults.

    @[instance_reducible]
    @[reducible, inline]

    Structured pointer-machine programs.

    @[reducible, inline]
    abbrev Cslib.PointerMachine.Exec {M : Model} {L : Layout} (c : Code M L) (x y : State M L) (n : ℕ) :

    Exec c x y n: the structured program c leads from x to y executing exactly n instructions of the flat program it assembles to. The program counter is ignored.

    @[reducible, inline]
    abbrev Cslib.PointerMachine.ExecWithin {M : Model} {L : Layout} (c : Code M L) (x y : State M L) (T : ℕ) :

    ExecWithin c x y T: the structured program c leads from x to y within T instructions.

    @[reducible, inline]
    abbrev Cslib.PointerMachine.Code.eval {M : Model} {L : Layout} (c : Code M L) (fuel : ℕ) (x : State M L) :

    Run a structured program, allowing each loop at most fuel iterations per entry: the state reached and the instruction count, or none if a loop runs out of iterations.

    @[simp]
    theorem Cslib.PointerMachine.Op.pc_effect {M : Model} {L : Layout} (o : Op M L) (x : State M L) :
    (o.effect x).pc = x.pc

    A data instruction keeps the program counter.

    theorem Cslib.PointerMachine.Exec.op {M : Model} {L : Layout} {x : State M L} {o : Op M L} :

    A data instruction is one instruction.

    theorem Cslib.PointerMachine.Exec.pc_eq {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {n : ℕ} (h : Exec c x y n) :
    y.pc = x.pc

    Executions keep the program counter.

    theorem Cslib.PointerMachine.Exec.valid {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {n : ℕ} (h : Exec c x y n) (hx : x.Valid) :

    Executions preserve validity.

    theorem Cslib.PointerMachine.ExecWithin.op {M : Model} {L : Layout} {x : State M L} {o : Op M L} :

    A data instruction executes within one instruction.

    theorem Cslib.PointerMachine.ExecWithin.op_eq {M : Model} {L : Layout} {x y : State M L} {o : Op M L} (h : o.effect x = y) :

    A data instruction executes within one instruction, reaching a state given in advance.

    theorem Cslib.PointerMachine.ExecWithin.seqList_op {M : Model} {L : Layout} (os : List (Op M L)) (x : State M L) :
    ExecWithin (Program.Code.seqList (List.map Program.Code.op os)) x (List.foldl (fun (s : State M L) (o : Op M L) => o.effect s) x os) os.length

    A list of data instructions executes within its length, reaching the state that their effects give in order.

    theorem Cslib.PointerMachine.ExecWithin.data_frame {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (field : Fin L.dataFields) (safe : ∀ o ∈ Program.Code.ops c, ¬o.instr.WritesData field) (a : ℕ) :
    Option.map (fun (r : Record M L) => r.data field) y.heap[a]? = Option.map (fun (r : Record M L) => r.data field) x.heap[a]?

    When no data instruction of the program allocates or stores field, every execution preserves that field at every address, including the absence of a record.

    theorem Cslib.PointerMachine.ExecWithin.pc_eq {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (h : ExecWithin c x y T) :
    y.pc = x.pc

    Executions within a bound keep the program counter.

    theorem Cslib.PointerMachine.ExecWithin.valid {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (h : ExecWithin c x y T) (hx : x.Valid) :

    Executions within a bound preserve validity.