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.
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.
Discrete and conditional expectation principles are classical background; see [MU17], Sections 2.1 and 2.3. 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 and 2.3.
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).