Documentation

CslibFrontier.Computability.WordRAM.ListOutput

List-valued outputs of word-RAM programs #

A word-RAM program returns one word with halt. A list is returned through memory: halt returns an address v, and the output is the length-prefixed array [mem (v + 1), …, mem (v + mem v)] in the memory just before the halting step (outputList). This mirrors the input convention of State.init, with the length at address 0 and the words at 1, …, n. In particular, returning address 0 without writing outputs the input itself (outputList_init). A successful list-valued execution requires the header and every output cell to have a word-sized address; the final occupied address is v + l.length.

Program.parkHalts replaces every halt i by a jump to a self-loop at label p.length + i. A halting run of p becomes a run of p.parkHalts that reaches the parked state, which has the pre-halt data and a control label recording the output register, and stays there (ExecutesList.runs_parkHalts). A clocked simulator that only produces continuing states can therefore read the output from its final state.

Main definitions #

Main results #

The output convention #

def Cslib.WordRAM.outputList (mem : ℕ → ℕ) (v : ℕ) :

The length-prefixed array at address v: [mem (v + 1), …, mem (v + mem v)].

@[simp]
theorem Cslib.WordRAM.length_outputList (mem : ℕ → ℕ) (v : ℕ) :
(outputList mem v).length = mem v
theorem Cslib.WordRAM.getElem_outputList (mem : ℕ → ℕ) (v i : ℕ) (hi : i < (outputList mem v).length) :
(outputList mem v)[i] = mem (v + 1 + i)
theorem Cslib.WordRAM.outputList_eq_iff {mem : ℕ → ℕ} {v : ℕ} {l : List ℕ} :
outputList mem v = l ↔ mem v = l.length ∧ Block.HoldsList (v + 1) l mem

The array at v is l exactly when v holds the length of l and the following cells hold its entries.

theorem Cslib.WordRAM.outputList_init {r : ℕ} (input : List ℕ) :
outputList (Data.init input).mem 0 = input

Returning address 0 of the initial memory outputs the input.

theorem Cslib.WordRAM.Block.holdsList_init {r : ℕ} (input : List ℕ) :
HoldsList 1 input (Data.init input).mem

In Data.init input, the slice starting at address 1 holds input.

def Cslib.WordRAM.ExecutesList {B : Basis} {r : ℕ} (w : ℕ) (p : Program B r) (s : State r) (l : List ℕ) (n : ℕ) :

ExecutesList w p s l n: running p at word size w from s halts after exactly n instructions, the final halt included, returning an address at which the memory just before halting holds l as a length-prefixed array. The final occupied address must fit a word, including the header address when l is empty.

theorem Cslib.WordRAM.ExecutesList.executes {B : Basis} {r w : ℕ} {p : Program B r} {s : State r} {l : List ℕ} {n : ℕ} (h : ExecutesList w p s l n) :
∃ (v : ℕ), Executes w p s v n

A list-valued run is in particular a halting run, returning the address of the list.

def Cslib.WordRAM.Program.ComputesListInTime {B : Basis} {r : ℕ} {α : Type u_1} (p : Program B r) (encode : ℕ → α → List ℕ) (time : α → ℕ → ℕ) (allowed : α → ℕ → Prop) (f : α → List ℕ) :

One program returns the list f a at every admitted fitting width within time a w instructions. The final halt is counted; the mathematical input encoding is a convention.

def Cslib.WordRAM.UniformComputableListInPolyTime {α : Type u_1} (B : Basis) (f : α → List ℕ) (encode : ℕ → α → List ℕ) (size : α → ℕ) :

A fixed program computes a list-valued function in polynomially many instructions in size a, at every fitting width above a positive logarithmic threshold. Every input must admit such a width.

theorem Cslib.WordRAM.Program.ComputesListInTime.mono {B : Basis} {r : ℕ} {α : Type u_1} {p : Program B r} {encode : ℕ → α → List ℕ} {time time' : α → ℕ → ℕ} {allowed allowed' : α → ℕ → Prop} {f : α → List ℕ} (h : p.ComputesListInTime encode time allowed f) (ht : ∀ (a : α) (w : ℕ), time a w ≤ time' a w) (ha : ∀ (a : α) (w : ℕ), allowed' a w → allowed a w) :
p.ComputesListInTime encode time' allowed' f

Increase a fixed program's time bound and restrict its admitted widths.

theorem Cslib.WordRAM.Exec.executesList {B : Basis} {r w : ℕ} {c : Code B r} {x y : Data r} {n : ℕ} (h : Exec w c x y n) (out : Fin r) (hfit : Fits w (y.regs out + (outputList y.mem (y.regs out)).length)) :
ExecutesList w (c.program out) { pc := 0, data := x } (outputList y.mem (y.regs out)) (n + 1)

A structured execution followed by halt out returns the array at the address in out, with one more instruction.

theorem Cslib.WordRAM.Code.computesListInTime_program {B : Basis} {r : ℕ} {α : Type u_1} {encode : ℕ → α → List ℕ} {time : α → ℕ → ℕ} {allowed : α → ℕ → Prop} {f : α → List ℕ} (c : Code B r) (out : Fin r) (h : ∀ (a : α) (w : ℕ), allowed a w → InputFits w (encode w a) → ∃ (y : Data r), ExecWithin w c (Data.init (encode w a)) y (time a w) ∧ outputList y.mem (y.regs out) = f a ∧ Fits w (y.regs out + (f a).length)) :
(c.program out).ComputesListInTime encode (fun (a : α) (w : ℕ) => time a w + 1) allowed f

A structured certificate leaving the address of f a in register out computes f, with one more instruction for the final halt.

Parking the halts #

def Cslib.WordRAM.Instr.parkHalt {B : Basis} {r : ℕ} (L : ℕ) :
Instr B r → Instr B r

Replace halt i by a jump to label L + i; every other instruction is unchanged.

Replace every halt i of p by a jump to a self-loop at label p.length + i. Registers and all non-halting steps are unchanged; the parked label records the output register.

theorem Cslib.WordRAM.Program.next_parkHalts {B : Basis} {r : ℕ} {p : Program B r} {w : ℕ} {s t : State r} (h : Next w p s t) :
Next w p.parkHalts s t

Every non-halting step of p is a step of p.parkHalts.

theorem Cslib.WordRAM.Program.runs_parkHalts {B : Basis} {r : ℕ} {p : Program B r} {w : ℕ} {s t : State r} {m : ℕ} (h : Runs w p s t m) :
Runs w p.parkHalts s t m

Every run of p is a run of p.parkHalts.

theorem Cslib.WordRAM.Program.next_parkHalts_of_halt {B : Basis} {r : ℕ} {p : Program B r} {w v : ℕ} {s : State r} (h : step w p s = Outcome.halt v) :
∃ (i : Fin r), s.data.regs i = v ∧ Next w p.parkHalts s { pc := List.length p + ↑i, data := s.data }

A halting step of p becomes a step of p.parkHalts to the parked label of the output register.

theorem Cslib.WordRAM.Program.next_parkHalts_park {B : Basis} {r : ℕ} {p : Program B r} {w : ℕ} (i : Fin r) (x : Data r) :
Next w p.parkHalts { pc := List.length p + ↑i, data := x } { pc := List.length p + ↑i, data := x }

A parked state steps to itself.

theorem Cslib.WordRAM.Program.runs_parkHalts_park {B : Basis} {r : ℕ} {p : Program B r} {w : ℕ} (i : Fin r) (x : Data r) (j : ℕ) :
Runs w p.parkHalts { pc := List.length p + ↑i, data := x } { pc := List.length p + ↑i, data := x } j
theorem Cslib.WordRAM.ExecutesList.runs_parkHalts {B : Basis} {r w : ℕ} {p : Program B r} {s : State r} {l : List ℕ} {n N : ℕ} (h : ExecutesList w p s l n) (hN : n ≤ N) :
∃ (i : Fin r) (x : Data r), Runs w p.parkHalts s { pc := List.length p + ↑i, data := x } N ∧ outputList x.mem (x.regs i) = l

A list-valued run, parked. If p returns l within n ≤ N instructions, then p.parkHalts runs for exactly N steps to a parked state: its label is p.length + i and its data holds l at the address in register i.