Expected running time for randomized word RAMs #
Expected instruction counts for flat (Program) and structured (Code) randomized word RAM
programs, valued in the extended nonnegative reals ℝ≥0∞ so expectations are total without
summability side conditions; nonsummable running-probability tails have value ⊤.
For a flat program p starting at state s, the expected running time is defined via the
discrete tail-sum formula over clocked horizons:
expectedTime w p s = ∑' t : ℕ, (evalRand w p t s).toOuterMeasure {cfg | ∃ u, cfg = .running u}.
Because .halted and .faulted are absorbing, each horizon t contributes the probability that
execution is still running before either halt or fault after t steps. Summing over t : ℕ yields
the exact 1-step Bellman equation expectedStepsFrom_running, connects directly to structured
ExecRand trees via
ExecRand.expectedTime_program, reduces to a finite sum under RandHaltsWithin
(RandHaltsWithin.expectedTime_eq_sum, expectedTime_le_of_randHaltsWithin), and recovers exact
deterministic step counts (Executes.expectedTime_eq).
For structured Code B r programs, expectedCodeTime measures the expected instruction count of
codeComp w c x when a finite, fault-free ExecRand tree exists, and is ⊤ otherwise. Thus
unbounded almost-surely terminating loops can receive a conservative infinite structured cost.
It satisfies exact sequential
additivity (expectedCodeTime_seq), branch laws (expectedCodeTime_branch_pos,
expectedCodeTime_branch_neg, expectedCodeTime_branch_le), and a potential/drift bound for
while loops (expectedCodeTime_loop_le_of_drift).
Main definitions #
Cslib.WordRAM.AlmostSurelyHalts: zero faults at all horizons and supremum halting probability equal to1.Cslib.WordRAM.expectedStepsFrom,Cslib.WordRAM.expectedTime:ℝ≥0∞-valued expected remaining instructions from aRandCfg ror initialState r.Cslib.WordRAM.expectedCodeTime:ℝ≥0∞-valued expected instruction count of a structuredCode B r.Cslib.WordRAM.ExecCodeDist: compositional structured execution certificate pairing an output distributionμ : PMF (Data r)with an expected step boundT : ℝ≥0∞, closed under unbounded almost-surely terminating geometricwhileloops (ExecCodeDist.loop_geometric).
Main results #
Cslib.WordRAM.expectedStepsFrom_running: 1-step Bellman equation forexpectedStepsFrom.Cslib.WordRAM.RandHaltsWithin.almostSurelyHalts,Cslib.WordRAM.AlmostSurelyHalts.of_expectedTime_ne_top: sufficient conditions for almost-sure halting.Cslib.WordRAM.RandHaltsWithin.expectedTime_eq_sum,Cslib.WordRAM.expectedTime_le_of_randHaltsWithin: finite-horizon reduction underRandHaltsWithin.Cslib.WordRAM.Executes.expectedTime_eq: deterministic halting runs haveexpectedTime = n.Cslib.WordRAM.ExecRand.expectedTime_program: exact bridge between compiledc.program outandexpectedCodeTime.Cslib.WordRAM.expectedCodeTime_seq,Cslib.WordRAM.expectedCodeTime_branch_le,Cslib.WordRAM.expectedCodeTime_loop_le_of_drift: compositional expected-time rules for structuredCode.Cslib.WordRAM.ExecCodeDist.loop_geometric,Cslib.WordRAM.expectedStepsFrom_loop_le_of_geometric,Cslib.WordRAM.ExecCodeDist.program_halts: expected-time and almost-sure halting rules for geometric Las Vegaswhileloops and structured programs.
Discrete and conditional expectation principles are classical background; see [MU17], Sections 2.1, 2.3, and 2.4. The particular native tail-sum/Bellman definitions, finite structured-execution-tree bridge and ranked-loop expected-cost formalization are original library constructions in this module.
References #
- M. Mitzenmacher and E. Upfal, Probability and Computing, Sections 2.1, 2.3, and 2.4.
Almost-sure halting and ℝ≥0∞-valued expected running time #
Set of active (.running) configurations.
Instances For
Set of cleanly halted (.halted) configurations.
Instances For
Halting probability is monotone in the horizon T because .halted is absorbing.
Any worst-case bounded execution halts almost surely.
1-step Bellman equation: an active state s incurs 1 immediate step plus the expected
remaining steps from stepRand w p (.running s).
Under RandFaultFree, every horizon partitions probability 1 between .running and
.halted.
A fault-free execution with finite expected running time halts almost surely.
Under RandHaltsWithin w p s T, the active-configuration tail sum vanishes from T onward and
reduces to a finite sum over Finset.range T.
A worst-case step bound RandHaltsWithin w p s T immediately bounds expectedTime by T.
Along any deterministic Runs of length m, expectedStepsFrom adds m to the expected
remaining steps at the destination state.
Structured expectedCodeTime and compilation bridge #
Along an assembled structured execution ExecRand w c x m inside p, the expected remaining
steps from ⟨pc, x⟩ equal the expected step count of m plus the expected continuation steps from
⟨pc + c.size, y⟩.
Exact bridge: compiling c with c.program out adds 1 (for the final halt out
instruction) to the expected step count of ExecRand w c x m.
Sequential additivity of expectedCodeTime: when c₁ executes via m₁, the expected time of
Code.seq c₁ c₂ equals the expected time of c₁ plus the m₁-expected time of c₂.
Branch convexity / uniform upper bound on expectedCodeTime (.branch cond pos neg).
Convex combination of branch expected times under a random input distribution m.
Potential/drift bound for Code.loop: if an invariant I holds, every iteration decreases a
well-founded ℕ ranking Φ, and the expected single-iteration cost (z.2 : ℝ≥0∞) + 2 plus next
potential Ψ z.1 is at most Ψ s, then the loop executes cleanly (∃ m, ExecRand ...) and its
expectedCodeTime is at most Ψ x + 2 (accounting for the final 2-step loop exit).
Distributional structured execution (ExecCodeDist) and geometric Las Vegas loops #
Non-faulting structured randomized executions transfer instruction-for-instruction along any
defined-evaluation basis simulation ψ : B.Simulates B'.
Finite-horizon approximation of expectedStepsFrom: expected number of active (.running)
steps within the first N steps starting from configuration cfg.
Instances For
1-step horizon recursion for expectedStepsWithin: at horizon N + 1, an active state s
incurs 1 step plus the N-step continuation expectation.
Along ExecRand w c x m, if no continuation from ⟨pc + c.size, y⟩ faults within T steps
for any supported outcome y, then running p from ⟨pc, x⟩ does not fault within T steps.
Along ExecRand w c x m, the expected supremum halting probability from ⟨pc + c.size, y⟩ is a
lower bound on the supremum halting probability from ⟨pc, x⟩.
Absorbing Markov chain for geometric Las Vegas loops #
Compositional distributional execution (ExecCodeDist) #
ExecCodeDist w c x μ T: at word size w, running structured program c from data x
terminates almost surely without faulting, realizes exit distribution μ : PMF (Data r) at the
fallthrough address pc + c.size of any containing flat program, and uses at most T : ℝ≥0∞
expected instructions.
Unlike ExecRand, which requires a finite-depth computation tree, ExecCodeDist includes
unbounded Las Vegas while loops whose each active iteration exits with probability bounded below
by p_exit > 0 (ExecCodeDist.loop_geometric).
- of_execRand {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} : ExecRand w c x m → ExecCodeDist w c x (Prod.fst <$> m).eval (m.expect fun (z : Data r × ℕ) => ↑z.2)
- mono {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {μ : PMF (Data r)} {T T' : ENNReal} : ExecCodeDist w c x μ T → T ≤ T' → ExecCodeDist w c x μ T'
- seq {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {μ₁ : PMF (Data r)} {μ₂ : Data r → PMF (Data r)} {T₁ : ENNReal} {T₂ : Data r → ENNReal} : ExecCodeDist w c₁ x μ₁ T₁ → (∀ y ∈ μ₁.support, ExecCodeDist w c₂ y (μ₂ y) (T₂ y)) → ExecCodeDist w (c₁.seq c₂) x (μ₁.bind μ₂) (T₁ + ∑' (y : Data r), μ₁ y * T₂ y)
- branch_pos {B : Basis} {r w : ℕ} {cond : Cond r} {pos neg : Code B r} {x : Data r} {μ : PMF (Data r)} {T : ENNReal} : cond.Holds x → ExecCodeDist w pos x μ T → ExecCodeDist w (Code.branch cond pos neg) x μ (T + 1)
- branch_neg {B : Basis} {r w : ℕ} {cond : Cond r} {pos neg : Code B r} {x : Data r} {μ : PMF (Data r)} {T : ENNReal} : ¬cond.Holds x → ExecCodeDist w neg x μ T → ExecCodeDist w (Code.branch cond pos neg) x μ (T + 2)
- loop_geometric {B : Basis} {r w : ℕ} {cond : Cond r} {body : Code B r} (I Q : Data r → Prop) {μ_body : Data r → PMF (Data r)} {T_body : Data r → ENNReal} {B_body p_exit : ENNReal} {x : Data r} : 0 < p_exit → p_exit ≠ ⊤ → (∀ (z : Data r), I z → cond.Holds z → ExecCodeDist w body z (μ_body z) (T_body z)) → (∀ (z : Data r), I z → cond.Holds z → T_body z ≤ B_body) → (∀ (z : Data r), I z → cond.Holds z → ∀ y ∈ (μ_body z).support, LoopStateInv cond I Q y) → (∀ (z : Data r), I z → cond.Holds z → p_exit ≤ (μ_body z).toOuterMeasure {y : Data r | ¬cond.Holds y}) → LoopStateInv cond I Q x → ExecCodeDist w (Code.loop cond body) x (loopExitPMF cond μ_body x) ((B_body + 2) / p_exit + 2)
Instances For
Every deterministic Exec w c x y n gives ExecCodeDist w c x (PMF.pure y) n.
Every deterministic ExecWithin w c x y n gives ExecCodeDist w c x (PMF.pure y) n.
Sequencing with a uniform continuation step bound T₂.
Sequencing after a deterministic first stage (PMF.pure y).
Every state in the support of μ preserves Data.Bounded w when x.Bounded w.
Any register i not written by c (c.Preserves i) is preserved across the support of μ.
Distributional executions transfer along any defined-evaluation basis simulation
ψ : B.Simulates B'.
Renaming a distributional execution along an injective register map f : Fin r → Fin r'
transforms the output distribution via Data.extend f x and preserves the expected step bound.
Compilation of ExecCodeDist to flat Program semantics #
Finite-horizon Bellman inequality for ExecCodeDist inside any containing flat program p.
Full-horizon Bellman inequality for ExecCodeDist inside any containing flat program p.
Continuation upper-bound form of expectedStepsFrom_assemble.
Along ExecCodeDist w c x μ T, if no continuation from ⟨pc + c.size, y⟩ faults within
T_hor steps for any supported y ∈ μ.support, then running p from ⟨pc, x⟩ does not fault
within T_hor steps.
If every continuation from ⟨pc + c.size, y⟩ (y ∈ μ.support) is fault-free at all horizons,
then running p from ⟨pc, x⟩ is fault-free at all horizons.
Along ExecCodeDist w c x μ T, the μ-weighted supremum halting probability from
⟨pc + c.size, y⟩ is a lower bound on the supremum halting probability from ⟨pc, x⟩.
Compiling ExecCodeDist w c x μ T with c.program out yields:
expectedTime w (c.program out) ⟨0, x⟩ ≤ T + 1,RandFaultFree w (c.program out) ⟨0, x⟩,AlmostSurelyHalts w (c.program out) ⟨0, x⟩wheneverT ≠ ⊤, and- for every
y : Data r,μ y ≤ ⨆ T_hor : ℕ, evalRand w (c.program out) T_hor ⟨0, x⟩ (.halted (y.regs out) y).
Geometric Las Vegas loop expected-step bound inside any flat program p containing
(Code.loop cond body).assemble pc.