Documentation

CslibFrontier.Computability.WordRAM.PointerMachine.Local

Local-address RAM simulation #

A finite RAM interval has address registers separate from scalar registers. Each cell holds a scalar and a reference. The only address arithmetic is successor and predecessor, with a distinguished null reference for a move outside the interval.

The pointer representation has one record per cell, with immutable predecessor and successor links. The representation is supplied at entry. Each RAM instruction translates to one pointer instruction, and structured executions preserve instruction counts.

Schönhage describes the real-time equivalence of storage modification machines and successor RAMs [Sch80], abstract. The compiler here goes from the typed local-address RAM to the pointer machine.

Main definitions #

Main results #

A local-address RAM with n cells, p address registers and d scalar registers.

  • addr : Fin p → Option (Fin n)

    Address registers.

  • scalars : Fin d → M.Value

    Scalar registers.

  • values : Fin n → M.Value

    Data stored at each RAM address.

  • refs : Fin n → Option (Fin n)

    References stored at each RAM address.

A local address move.

Move within the interval, returning null at its endpoints.

The operations of the local-address RAM.

def Cslib.WordRAM.Local.Op.eval {M : PointerMachine.Model} {n p d : ℕ} (x : Data M n p d) :
Op M p d → Data M n p d

Operations through null leave the state unchanged.

The local-address RAM can test reference equality and compare scalar data.

@[reducible]

Structured instruction semantics for the local-address RAM.

@[instance_reducible]
instance Cslib.WordRAM.Local.instDecidableHoldsInstructionSet {M : PointerMachine.Model} {n p d : ℕ} (t : Test M p d) (x : Data M n p d) :
@[reducible]

Record fields for the reference, successor and predecessor, and one scalar payload.

Encode an interval as a doubly linked chain of records in address order.

@[simp]
theorem Cslib.WordRAM.Local.encode_pc {M : PointerMachine.Model} {n p d : ℕ} (x : Data M n p d) :
(encode x).pc = 0

Encoded states start at label zero.

@[simp]

The linked representation uses exactly one record per source cell.

theorem Cslib.WordRAM.Local.encode_heap_getElem_data {M : PointerMachine.Model} {n p d : ℕ} (x : Data M n p d) (a : Fin n) (h : ↑a < (encode x).heap.length) :
(encode x).heap[↑a].data 0 = x.values a

Reading a payload from the encoded heap gives its source cell value.

theorem Cslib.WordRAM.Local.encode_valid {M : PointerMachine.Model} {n p d : ℕ} (x : Data M n p d) :

All references in the initial linked representation designate allocated records or null.

Compile local-address programs, preserving their control flow.

@[simp]

Compilation preserves the number of assembled instructions.

theorem Cslib.WordRAM.Local.exec_compile {M : PointerMachine.Model} {n p d : ℕ} {c : Program.Code (Op M p d) (Test M p d)} {x y : Data M n p d} {t : ℕ} (h : Program.Exec (instructionSet M n p d) c x y t) :

Every source structured run executes on the linked representation at the same cost.

@[simp]
theorem Cslib.WordRAM.Local.encode_data {M : PointerMachine.Model} {n p d : ℕ} (x : Data M n p d) :

The encoded scalar registers retain their source values.

theorem Cslib.WordRAM.Local.executes_compile {M : PointerMachine.Model} {n p d : ℕ} {c : Program.Code (Op M p d) (Test M p d)} {x y : Data M n p d} {t : ℕ} (h : Program.Exec (instructionSet M n p d) c x y t) :
PointerMachine.Executes (compile c).program (encode x) (have __src := encode y; { pc := Program.Code.size (compile c), ptr := __src.ptr, data := __src.data, heap := __src.heap }) (t + 1)

Assembling the simulation adds only the final halt to the source instruction count.

Probabilistic scalar primitives #

def Cslib.WordRAM.Local.evalRand {M : PointerMachine.Model} {n p d : ℕ} (kernel : M.Op → M.Value → M.Value → ProbComp M.Value) (o : Op M p d) (x : Data M n p d) :
Option (ProbComp (Data M n p d))

Interpret scalar operations using probabilistic kernels. Address operations and memory access keep their deterministic meanings. Each operation costs one instruction.

theorem Cslib.WordRAM.Local.execRand_compile {M : PointerMachine.Model} {n p d : ℕ} (kernel : M.Op → M.Value → M.Value → ProbComp M.Value) {c : Program.Code (Op M p d) (Test M p d)} {x : Data M n p d} {m : ProbComp (Data M n p d × ℕ)} (h : Program.ExecRand (evalRand kernel) c x m) :
Program.ExecRand (PointerMachine.Op.evalRand kernel) (compile c) (encode x) ((fun (z : Data M n p d × ℕ) => (encode z.1, z.2)) <$> m)

With matching scalar kernels, compilation preserves the entire distribution of results and instruction counts of each terminating structured execution.

theorem Cslib.WordRAM.Local.runRand_compile {M : PointerMachine.Model} {n p d : ℕ} (kernel : M.Op → M.Value → M.Value → ProbComp M.Value) {c : Program.Code (Op M p d) (Test M p d)} {x : Data M n p d} {m : ProbComp (Data M n p d × ℕ)} {T : ℕ} (h : Program.ExecRand (evalRand kernel) c x m) (hT : ∀ z ∈ OracleComp.support m, z.2 ≤ T) :
PointerMachine.runRand kernel (compile c).program T (encode x) = (fun (z : Data M n p d × ℕ) => have __src := encode z.1; { pc := Program.Code.size (compile c), ptr := __src.ptr, data := __src.data, heap := __src.heap }) <$> m

A budget covering every structured run reaches the compiled halt with the encoded output distribution.