Randomized structured programs #
The structured control flow of Code can interpret machine operations as probabilistic
computations. ExecRand describes successful executions on every path and pairs each resulting
data state with its exact instruction count. The operation interpretation returns none when
an operation faults and some computation for an operation that always returns a data state.
Deterministic successful operations can return pure computations. Sampling costs one instruction
when the flat machine implements the sampling operation as a primitive instruction.
Main definitions #
ExecRand: randomized execution with exact per-path instruction counts.RandomizedFlatMachine: a flat machine's probabilistic step laws.
Main results #
ExecRand.runRand_assemble: equality of the clocked flat and structured computations.
The structured semantics and assembly proof generalize those in
CslibFrontier.Computability.WordRAM.Randomized.Structured.
Exact randomized execution of structured code, with every branch of the computation terminating successfully. Each primitive operation costs one flat instruction.
- skip {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {x : I.Data} : ExecRand evalRand Code.skip x (pure (x, 0))
Empty code costs zero instructions.
- op {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {o : I.Op} {x : I.Data} {m : ProbComp I.Data} (h : evalRand o x = some m) : ExecRand evalRand (Code.op o) x ((fun (y : I.Data) => (y, 1)) <$> m)
An operation returns a probabilistic data state and uses one instruction.
- seq {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c₁ c₂ : Code I.Op I.Test} {x : I.Data} {m₁ : ProbComp (I.Data × ℕ)} {m₂ : I.Data → ProbComp (I.Data × ℕ)} (h₁ : ExecRand evalRand c₁ x m₁) (h₂ : ∀ z ∈ OracleComp.support m₁, ExecRand evalRand c₂ z.1 (m₂ z.1)) : ExecRand evalRand (c₁.seq c₂) x do
let z₁ ← m₁
let z₂ ← m₂ z₁.1
pure (z₂.1, z₁.2 + z₂.2)
Sequencing composes the two computations and adds their instruction counts.
- branch_pos {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {pos neg : Code I.Op I.Test} {x : I.Data} {m : ProbComp (I.Data × ℕ)} (hc : I.Holds c x) (h : ExecRand evalRand pos x m) : ExecRand evalRand (Code.branch c pos neg) x ((fun (z : I.Data × ℕ) => (z.1, z.2 + 1)) <$> m)
Taking the positive branch adds
1instruction for the branch test. - branch_neg {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {pos neg : Code I.Op I.Test} {x : I.Data} {m : ProbComp (I.Data × ℕ)} (hc : ¬I.Holds c x) (h : ExecRand evalRand neg x m) : ExecRand evalRand (Code.branch c pos neg) x ((fun (z : I.Data × ℕ) => (z.1, z.2 + 2)) <$> m)
Taking the negative branch adds
2instructions (branch test and jump pastpos). - loop_exit {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {body : Code I.Op I.Test} {x : I.Data} (hc : ¬I.Holds c x) : ExecRand evalRand (Code.loop c body) x (pure (x, 2))
Exiting a loop when the condition fails takes
2instructions (branch test and exit jump). - loop_iter {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {body : Code I.Op I.Test} {x : I.Data} {mb : ProbComp (I.Data × ℕ)} {ml : I.Data → ProbComp (I.Data × ℕ)} (hc : I.Holds c x) (hb : ExecRand evalRand body x mb) (hl : ∀ z ∈ OracleComp.support mb, ExecRand evalRand (Code.loop c body) z.1 (ml z.1)) : ExecRand evalRand (Code.loop c body) x do
let z₁ ← mb
let z₂ ← ml z₁.1
pure (z₂.1, z₁.2 + z₂.2 + 2)
Entering a loop executes
body, jumps back to the header, and continues the loop, adding2instructions for the branch test and back-jump. - loopUntil_exit {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {body : Code I.Op I.Test} {x : I.Data} (hc : I.Holds c x) : ExecRand evalRand (Code.loopUntil c body) x (pure (x, 1))
Exiting a repeat-until loop takes its successful test.
- loopUntil_iter {I : InstructionSet} {evalRand : I.Op → I.Data → Option (ProbComp I.Data)} {c : I.Test} {body : Code I.Op I.Test} {x : I.Data} {mb : ProbComp (I.Data × ℕ)} {ml : I.Data → ProbComp (I.Data × ℕ)} (hc : ¬I.Holds c x) (hb : ExecRand evalRand body x mb) (hl : ∀ z ∈ OracleComp.support mb, ExecRand evalRand (Code.loopUntil c body) z.1 (ml z.1)) : ExecRand evalRand (Code.loopUntil c body) x do
let z₁ ← mb
let z₂ ← ml z₁.1
pure (z₂.1, z₁.2 + z₂.2 + 2)
A failed test, body execution and back-jump continue the repeat-until loop.
A randomized interpretation of a flat machine. The running configurations use the flat program counter and data. A machine may also have halted or faulted configurations.
- Config : Type u
Full configuration type, including any terminal configurations.
Configuration at the given instruction label and data state.
One probabilistic step.
- step_op {p : List F.Instr} {pc : ℕ} {o : I.Op} {x : I.Data} {m : ProbComp I.Data} : p[pc]? = some (F.op o) → evalRand o x = some m → self.step p (self.running pc x) = self.running (pc + 1) <$> m
A successful data operation falls through with its sampled result.
- step_goto {p : List F.Instr} {pc label : ℕ} {x : I.Data} : p[pc]? = some (F.goto label) → self.step p (self.running pc x) = pure (self.running label x)
An unconditional jump changes only the program counter.
- step_jumpIf_pos {p : List F.Instr} {pc label : ℕ} {t : I.Test} {x : I.Data} : p[pc]? = some (F.jumpIf t label) → I.Holds t x → self.step p (self.running pc x) = pure (self.running label x)
A satisfied test jumps to its target.
- step_jumpIf_neg {p : List F.Instr} {pc label : ℕ} {t : I.Test} {x : I.Data} : p[pc]? = some (F.jumpIf t label) → ¬I.Holds t x → self.step p (self.running pc x) = pure (self.running (pc + 1) x)
A failed test falls through.
Run the flat program for the given instruction budget.
A zero budget returns the initial configuration.
A positive budget takes one step and runs the remaining budget.
Running a contained compiled program agrees with performing the structured computation and then continuing after its block for the remaining budget on each path.