Documentation

CslibFrontier.Computability.WordRAM.Realization.Random

Realizing memory clients with private coins #

A memory client with private coins is a ProbOracleComp Memory.Op.Result program: it calls the memory oracle and draws uniform coins. It runs against (Memory.spec w).toRandomized.withCoins, the memory oracle beside uniform sampling. Linking a client of a randomized specification through a memory refinement with its coins passed through (RandomizedRefinement.withCoins) produces exactly such a program. RealizesRand certifies one uniform native emitter for a family of them, as Realizes does for deterministic clients, with the same emitter, scratch bank, frontier and register frame. The certificate gives the exact law instead of a single run. At word size w, from native data whose memory abstraction is s, the emitted code's result words and memory state have the law of (Memory.spec w).toRandomized.withCoins.run (p d) s, read through the result encoding (ExecRandLaw). Every path runs within C * (time d s + 1) instructions and makes at most calls d s rand calls. Both are worst-case bounds that hold on every path, not expected ones.

Deterministic parts are certified by Realizes, including every certificate realize builds, and enter unchanged (Realizes.toRand). A coin is one rand instruction (RealizesRand.coin). Sequencing (RealizesRand.bind) needs the continuation's domain and budgets at every supported intermediate state; these obligations become part of the visible domain. Branching on the live words is RealizesRand.branch. A flat program from the native list-input convention halts within its bound on every path, respects the rand budget and has the law of the client (RealizesRand.exists_init_program). Runtime-bounded loops are in CslibFrontier.Computability.WordRAM.Realization.RandomLoop.

Main definitions #

Main results #

The randomized certificate is original infrastructure over the existing realization kernel, the structured randomized semantics ExecRandLaw and the randomized specifications of DataStructure.StateTransition.Randomized; no execution semantics or client language is added.

def Cslib.WordRAM.Realization.RealizesRand.Spec {δ α : Type} {k l : ℕ} (B : Basis) (p : δ → ProbOracleComp DataStructure.Memory.Op.Result α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (time calls : δ → DataStructure.Memory.State → ℕ) (n C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r) :

The existing pathwise execution, joint-law and register-frame contract for specified resource parameters and a specified emitter. Both existential and supplied witnesses use it.

def Cslib.WordRAM.Realization.RealizesRand {δ α : Type} {k l : ℕ} (B : Basis) (p : δ → ProbOracleComp DataStructure.Memory.Op.Result α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (time calls : δ → DataStructure.Memory.State → ℕ) :

Uniform native realization of a family of memory clients with private coins. The emitter, scratch size and exchange constant precede every runtime argument, word width and initial memory, and the register conventions are those of Realizes. From admissible entry data, the emitted code terminates on every path within C * (time d s + 1) instructions and calls d s rand calls, keeps every register outside the scratch bank and the frontier, and the result words together with the memory state have the law of the client's run, read through output.

structure Cslib.WordRAM.Realization.RealizesRand.Witness {δ α : Type} {k l : ℕ} (B : Basis) (p : δ → ProbOracleComp DataStructure.Memory.Op.Result α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (time calls : δ → DataStructure.Memory.State → ℕ) :

Retain specified randomized realization data with the same existing law contract. It changes neither the oracle model nor the observation or supported-path semantics.

  • scratchSize : ℕ

    Auxiliary registers, excluding results and the reserved extra slot.

  • factor : ℕ

    Exchange factor for the original visible time grade.

  • code {r : ℕ} (frontier : Fin r) : (Fin k → Fin r) → (Fin (l + self.scratchSize + 1) → Fin r) → Code B r

    One emitter, fixed before layout, width, memory and random outcomes.

  • code_spec : Spec B p input output safe time calls self.scratchSize self.factor fun {r : ℕ} => self.code

    The canonical original execution and law contract.

theorem Cslib.WordRAM.Realization.RealizesRand.Witness.toRealizesRand {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {time calls : δ → DataStructure.Memory.State → ℕ} (h : Witness B p input output safe time calls) :
RealizesRand B p input output safe time calls

Forget supplied resources and code while retaining the original randomized judgment.

def Cslib.WordRAM.Realization.Realizes.Witness.toRand {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Witness B p input output safe work) :
RealizesRand.Witness B (fun (d : δ) => OracleComp.liftRight (p d)) input output safe (fun (d : δ) (s : DataStructure.Memory.State) => memoryCost (p d) s + work d s) fun (x : δ) (x_1 : DataStructure.Memory.State) => 0

Deterministic parts. A deterministic certificate, for instance one built by realize, realizes the same client with coins available and unused: its law is the point mass at the deterministic run, its time is the memory charge plus local work, and it makes no rand call.

theorem Cslib.WordRAM.Realization.Realizes.toRand {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Realizes B p input output safe work) :
RealizesRand B (fun (d : δ) => OracleComp.liftRight (p d)) input output safe (fun (d : δ) (s : DataStructure.Memory.State) => memoryCost (p d) s + work d s) fun (x : δ) (x_1 : DataStructure.Memory.State) => 0

The existential randomized entry supplied by the deterministic witness construction.

theorem Cslib.WordRAM.Realization.RealizesRand.consequence {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {time calls : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} {time' calls' : δ → DataStructure.Memory.State → ℕ} (h : RealizesRand B p input output safe time calls) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → safe' w d s → safe w d s ∧ time d s ≤ time' d s ∧ calls d s ≤ calls' d s) :
RealizesRand B p input output safe' time' calls'

Strengthen the visible domain, and enlarge the time and rand budgets on it. The entry word and memory bounds may be used.

theorem Cslib.WordRAM.Realization.RealizesRand.congr {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {time calls : δ → DataStructure.Memory.State → ℕ} {p' : δ → ProbOracleComp DataStructure.Memory.Op.Result α} (h : RealizesRand B p input output safe time calls) (hp : ∀ (d : δ), p d = p' d) :
RealizesRand B p' input output safe time calls

Pointwise equal clients have the same certificates.

def Cslib.WordRAM.Realization.RealizesRand.coinWitness {B : Basis} {δ : Type} {k : ℕ} (input : δ ↪ Fin k → ℕ) (i : Fin k) :
Witness B (fun (d : δ) => Fin.val <$> ProbOracleComp.sample (ProbComp.unifFin (input d i))) input (Equiv.funUnique (Fin 1) ℕ).symm.toEmbedding (fun (x : ℕ) (x_1 : δ) (x_2 : DataStructure.Memory.State) => True) (fun (x : δ) (x_1 : DataStructure.Memory.State) => 1) fun (x : δ) (x_1 : DataStructure.Memory.State) => 1

A coin. Drawing uniformly from [0, n], for a live word n, is one rand instruction into the result slot.

theorem Cslib.WordRAM.Realization.RealizesRand.coin {B : Basis} {δ : Type} {k : ℕ} (input : δ ↪ Fin k → ℕ) (i : Fin k) :
RealizesRand B (fun (d : δ) => Fin.val <$> ProbOracleComp.sample (ProbComp.unifFin (input d i))) input (Equiv.funUnique (Fin 1) ℕ).symm.toEmbedding (fun (x : ℕ) (x_1 : δ) (x_2 : DataStructure.Memory.State) => True) (fun (x : δ) (x_1 : DataStructure.Memory.State) => 1) fun (x : δ) (x_1 : DataStructure.Memory.State) => 1

The existential coin realization supplied by coinWitness.

def Cslib.WordRAM.Realization.RealizesRand.Witness.bind {B : Basis} {δ α β : Type} {k l m : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {q : δ × α → ProbOracleComp DataStructure.Memory.Op.Result β} {answer : α ↪ Fin l → ℕ} {output : β ↪ Fin m → ℕ} {safeP : ℕ → δ → DataStructure.Memory.State → Prop} {safeQ : ℕ → δ × α → DataStructure.Memory.State → Prop} {timeP callsP : δ → DataStructure.Memory.State → ℕ} {timeQ callsQ : δ × α → DataStructure.Memory.State → ℕ} (hP : Witness B p input answer safeP timeP callsP) (hQ : Witness B q (appendWords input answer) output safeQ timeQ callsQ) (T R : δ → DataStructure.Memory.State → ℕ) :
Witness B (fun (d : δ) => do let a ← p d q (d, a)) input output (fun (w : ℕ) (d : δ) (s : DataStructure.Memory.State) => safeP w d s ∧ ∀ (a : α) (s' : (DataStructure.Memory.spec w).toRandomized.withCoins.State), (a, s') ∈ ((DataStructure.Memory.spec w).toRandomized.withCoins.run (p d) s).support → safeQ w (d, a) s' ∧ timeQ (d, a) s' ≤ T d s ∧ callsQ (d, a) s' ≤ R d s) (fun (d : δ) (s : DataStructure.Memory.State) => timeP d s + T d s + 1) fun (d : δ) (s : DataStructure.Memory.State) => callsP d s + R d s

Sequencing. One uniform continuation emitter consumes the original environment and the first computation's answer. Its domain and budgets must hold at every supported intermediate answer and state, within the uniform budgets T and R; these obligations join the visible domain. The laws compose by PMF.bind, the times and rand budgets add.

theorem Cslib.WordRAM.Realization.RealizesRand.bind {B : Basis} {δ α β : Type} {k l m : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {q : δ × α → ProbOracleComp DataStructure.Memory.Op.Result β} {answer : α ↪ Fin l → ℕ} {output : β ↪ Fin m → ℕ} {safeP : ℕ → δ → DataStructure.Memory.State → Prop} {safeQ : ℕ → δ × α → DataStructure.Memory.State → Prop} {timeP callsP : δ → DataStructure.Memory.State → ℕ} {timeQ callsQ : δ × α → DataStructure.Memory.State → ℕ} (hP : RealizesRand B p input answer safeP timeP callsP) (hQ : RealizesRand B q (appendWords input answer) output safeQ timeQ callsQ) (T R : δ → DataStructure.Memory.State → ℕ) :
RealizesRand B (fun (d : δ) => do let a ← p d q (d, a)) input output (fun (w : ℕ) (d : δ) (s : DataStructure.Memory.State) => safeP w d s ∧ ∀ (a : α) (s' : (DataStructure.Memory.spec w).toRandomized.withCoins.State), (a, s') ∈ ((DataStructure.Memory.spec w).toRandomized.withCoins.run (p d) s).support → safeQ w (d, a) s' ∧ timeQ (d, a) s' ≤ T d s ∧ callsQ (d, a) s' ≤ R d s) (fun (d : δ) (s : DataStructure.Memory.State) => timeP d s + T d s + 1) fun (d : δ) (s : DataStructure.Memory.State) => callsP d s + R d s

The existential sequential realization supplied by the shared-bank constructor.

theorem Cslib.WordRAM.Realization.RealizesRand.branch {B : Basis} {δ α : Type} {k l : ℕ} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {pos neg : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {safePos safeNeg : ℕ → δ → DataStructure.Memory.State → Prop} {timePos timeNeg callsPos callsNeg : δ → DataStructure.Memory.State → ℕ} (c : Cond k) (hp : RealizesRand B pos input output safePos timePos callsPos) (hn : RealizesRand B neg input output safeNeg timeNeg callsNeg) :
RealizesRand B (fun (d : δ) => if test c (input d) then pos d else neg d) input output (fun (w : ℕ) (d : δ) (s : DataStructure.Memory.State) => if test c (input d) then safePos w d s else safeNeg w d s) (fun (d : δ) (s : DataStructure.Memory.State) => if test c (input d) then timePos d s else timeNeg d s) fun (d : δ) (s : DataStructure.Memory.State) => if test c (input d) then callsPos d s else callsNeg d s

Branching on a test of the live words. Only the selected alternative's domain and budgets are required; the test and the jump are absorbed by the exchange constant.

theorem Cslib.WordRAM.Realization.RealizesRand.exists_init_program {δ α : Type} {k l : ℕ} {p : δ → ProbOracleComp DataStructure.Memory.Op.Result α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {time calls : δ → DataStructure.Memory.State → ℕ} (h : RealizesRand Basis.exact p input output safe time calls) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code Basis.exact r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (l + n + 1) → Fin r), Function.Injective src → Function.Injective tmp → (∀ (i : Fin k) (j : Fin (l + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (l + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (i : Fin l) (w : ℕ) (d : δ) (xs : List ℕ), (∀ (j : Fin k), xs.getD (↑j) 0 = input d j) → k ≤ xs.length → Fits w (xs.length + 1) → InputFits w xs → safe w d (DataStructure.Memory.State.init xs) → (DataStructure.Memory.spec w).toRandomized.withCoins.toSpec.DemonicValid (DataStructure.Memory.State.init xs) (p d) → ∀ (F : RandCfg r → ℕ × DataStructure.Memory.State), (∀ (v : ℕ) (y : Data r), F (RandCfg.halted v y) = (v, memoryState frontier y)) → RandHaltsWithin w (((inputPrefix frontier (tmp (Fin.last (l + n))) src).seq (emit frontier src tmp)).program (tmp (Fin.castAdd (n + 1) i))) (State.init xs) (C * (time d (DataStructure.Memory.State.init xs) + 1) + 1) ∧ RandCallsBounded w (((inputPrefix frontier (tmp (Fin.last (l + n))) src).seq (emit frontier src tmp)).program (tmp (Fin.castAdd (n + 1) i))) (State.init xs) (C * (time d (DataStructure.Memory.State.init xs) + 1) + 1) (calls d (DataStructure.Memory.State.init xs)) ∧ PMF.map F (evalRand w (((inputPrefix frontier (tmp (Fin.last (l + n))) src).seq (emit frontier src tmp)).program (tmp (Fin.castAdd (n + 1) i))) (C * (time d (DataStructure.Memory.State.init xs) + 1) + 1) (State.init xs)) = PMF.map (fun (x : α × (DataStructure.Memory.spec w).toRandomized.withCoins.State) => match x with | (a, s) => (output a i, s)) ((DataStructure.Memory.spec w).toRandomized.withCoins.run (p d) (DataStructure.Memory.State.init xs))

Flat programs. Each static coordinate of the result has one flat exact-basis program from the native list-input convention. On an input list whose first k words encode the environment, it halts on every path within its bound, makes at most the budgeted rand calls, and the halting output word together with the memory state has the law of the client's run from the whole input memory, read at that coordinate. The rest of the input list is arbitrary memory content, such as a graph the client reads. The argument installation is absorbed into the exchange constant; the bound includes the trailing halt.