Documentation

CslibFrontier.Computability.WordRAM.Structured.FastEval

A fast compiled interpreter for structured word-RAM programs #

Cslib.WordRAM.Code.eval keeps the registers and memory as functions, and each write wraps the previous function in a Function.update. That shape suits proofs. In compiled code, though, a read walks back through every earlier write, so a run of n instructions takes Θ(n²) time.

Cslib.WordRAM.Code.evalFast is the same interpreter on Cslib.WordRAM.FastData, which keeps the registers in a Vector and the written memory cells in a Std.HashMap over the initial memory. Cslib.WordRAM.Code.evalFast_toData proves that it computes Code.eval, and the @[csimp] theorem Cslib.WordRAM.Code.eval_eq_evalImpl makes the compiler use it for Code.eval. This changes only compiled evaluation, such as #eval and #guard regressions over concrete runs. Code.eval, its lemmas and kernel reduction (decide, rfl) are unchanged.

Import this module where compiled code evaluates long runs. The replacement applies only to callers compiled with this module imported, not to callers compiled before. This module does not certify a runtime bound.

Main definitions #

Main results #

Machine data for compiled execution: the registers as a vector, and memory as the cells written so far over the initial memory base.

@[inline]
def Cslib.WordRAM.FastData.reg {r : ℕ} (x : FastData r) (i : Fin r) :

The contents of register i.

@[inline]

The contents of memory at address a. The initial memory is only consulted for cells that were never written.

The data that x represents.

Fast data for x, with nothing written yet.

@[inline]
def Cslib.WordRAM.FastData.set {r : ℕ} (x : FastData r) (i : Fin r) (v : ℕ) :

Set register i to v.

@[inline]
def Cslib.WordRAM.FastData.put {r : ℕ} (w : ℕ) (x : FastData r) (i : Fin r) (v : ℕ) :

Set register i to v if v fits a w-bit word, and fault otherwise.

@[inline]

Set the memory cell at address a to v.

@[simp]

Fast data made from x represents x.

theorem Cslib.WordRAM.FastData.toData_set {r : ℕ} (x : FastData r) (i : Fin r) (v : ℕ) :
(x.set i v).toData = x.toData.set i v

Setting a register of fast data sets it in the data represented.

theorem Cslib.WordRAM.FastData.toData_put {r : ℕ} (w : ℕ) (x : FastData r) (i : Fin r) (v : ℕ) :
Option.map toData (put w x i v) = Data.put w x.toData i v

FastData.put agrees with Data.put on the data represented.

theorem Cslib.WordRAM.FastData.toData_write {r : ℕ} (x : FastData r) (a v : ℕ) :
(x.write a v).toData = x.toData.write a v

Writing memory of fast data writes it in the data represented.

@[inline]

Whether a condition holds of fast data.

theorem Cslib.WordRAM.Cond.holdsFast_eq {r : ℕ} (c : Cond r) (x : FastData r) :

Cond.holdsFast decides Cond.Holds on the data represented.

def Cslib.WordRAM.Op.evalFast {B : Basis} {r : ℕ} (w : ℕ) :
Op B r → FastData r → Option (FastData r)

The effect of a data instruction on fast data, as Op.eval.

theorem Cslib.WordRAM.Op.evalFast_toData {B : Basis} {r : ℕ} (w : ℕ) (o : Op B r) (x : FastData r) :

Op.evalFast computes Op.eval on the data represented.

def Cslib.WordRAM.Code.evalLoopFast {r : ℕ} (c : Cond r) (body : FastData r → Option (FastData r × ℕ)) :

Code.evalLoop on fast data.

def Cslib.WordRAM.Code.evalFast {B : Basis} {r : ℕ} (w fuel : ℕ) :
Code B r → FastData r → Option (FastData r × ℕ)

Code.eval on fast data.

theorem Cslib.WordRAM.Code.evalLoopFast_toData {r : ℕ} (c : Cond r) {f : FastData r → Option (FastData r × ℕ)} {g : Data r → Option (Data r × ℕ)} (hfg : ∀ (x : FastData r), Option.map (Prod.map FastData.toData id) (f x) = g x.toData) (fuel : ℕ) (x : FastData r) :

The fast loop computes Code.evalLoop when its body does.

theorem Cslib.WordRAM.Code.evalFast_toData {B : Basis} {r : ℕ} (w fuel : ℕ) (c : Code B r) (x : FastData r) :

Code.evalFast computes Code.eval.

def Cslib.WordRAM.Code.evalImpl {B : Basis} {r : ℕ} (w fuel : ℕ) (c : Code B r) (x : Data r) :

Code.eval, computed with Code.evalFast.

@[csimp]

Compiled code evaluates Code.eval with the fast interpreter.

Fast compiled interpreter for flat Programs #

def Cslib.WordRAM.Program.runFast {B : Basis} {r : ℕ} (w fuel : ℕ) (p : Program B r) (input : List ℕ) :

Fast compiled interpreter for a flat Program B r from State.init input, returning (haltingOutput, steps) within fuel instructions.

def Cslib.WordRAM.Program.runFastList {B : Basis} {r : ℕ} (w fuel : ℕ) (p : Program B r) (input : List ℕ) :

Fast compiled list-output interpreter for a flat Program B r from State.init input, returning the length-prefixed array at the halting address and the exact instruction count.

theorem Cslib.WordRAM.Program.runFast_eq_run {B : Basis} {r : ℕ} (w fuel : ℕ) (p : Program B r) (input : List ℕ) :
runFast w fuel p input = run w p fuel (State.init input)

Program.runFast agrees identically with Cslib.WordRAM.run.

theorem Cslib.WordRAM.Program.runFastList_sound {B : Basis} {r w fuel : ℕ} {p : Program B r} {input l : List ℕ} {n : ℕ} (h : runFastList w fuel p input = some (l, n)) :
∃ (t : State r) (m : ℕ) (v : ℕ), Runs w p (State.init input) t m ∧ step w p t = Outcome.halt v ∧ List.map (fun (i : ℕ) => t.data.mem (v + 1 + i)) (List.range (t.data.mem v)) = l ∧ n = m + 1

A halting Program.runFastList result yields a corresponding halting execution of p from State.init input whose pre-halt memory holds l as a length-prefixed array at the returned address v.