Documentation

CslibFrontier.Languages.Probabilistic.Retry

Bounded retries and independent coordinates #

OracleComp.retry good program fuel runs program at most fuel times and returns the first result satisfying good, or none when every run fails. It is the bounded form of a Las Vegas loop: an oracle program is a finite tree of calls, so "repeat until success" is represented by its truncation after fuel runs, and the truncation is visible as the none outcome. Checking good is local computation and makes no call.

When one run fails with probability q (under answer distributions h, as in OracleComp.evalWith):

OracleComp.pi runs a family of programs one after the other and collects their results. Over an arbitrary interface its expected cost is the sum of the expected costs (OracleComp.expectedCost_pi), and the probability that some coordinate is bad is at most the sum of the probabilities (OracleComp.toOuterMeasure_evalWith_pi_exists_le, a union bound). These complement the support and law statements of CslibFrontier.Languages.Probabilistic.Vector.

OracleComp.retrySome specializes OracleComp.retry to ProbComp (Option α) attempts, returning the first some a (collapsed via Option.join) or none if all fuel attempts fail. It is the ProbComp counterpart of BlockRejectionSpec.runTrials in CslibFrontier.Probability.DDGTree.Rejection: while BlockRejectionSpec operates on fair-bit width-block decoders, retrySome operates on arbitrary ProbComp (Option α) programs, proving exact failure decay q ^ fuel (eval_retrySome_none), zero conditional bias against conditionedSome (filter_eval_retrySome_isSome), and statistical distance bounds against totalConditionedSome and differing retry caps (closeTo_retrySome_conditioned, closeTo_retrySome_totalConditionedSome, closeTo_retrySome_retrySome).

Main definitions #

Main results #

def Cslib.OracleComp.retry {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) :
ℕ → OracleComp spec (Option α)

Run program at most fuel times and return the first result satisfying good, or none when all fuel runs fail. Checking good makes no call.

@[simp]
theorem Cslib.OracleComp.retry_zero {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) :
retry good program 0 = pure none
theorem Cslib.OracleComp.retry_succ {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (fuel : ℕ) :
retry good program (fuel + 1) = do let a ← program if good a then pure (some a) else retry good program fuel
theorem Cslib.OracleComp.mem_supportWith_retry {ι : Type u} {spec : OracleSpec ι} {α : Type v} {good : α → Prop} [DecidablePred good] {program : OracleComp spec α} {possible : (q : ι) → Set (spec q)} {fuel : ℕ} {a : α} (ha : some a ∈ supportWith possible (retry good program fuel)) :
good a ∧ a ∈ supportWith possible program

A result returned by the loop satisfies good and is a possible result of one run.

theorem Cslib.OracleComp.evalWith_retry_none {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (fuel : ℕ) :
(evalWith h (retry good program fuel)) none = (evalWith h program).toOuterMeasure {a : α | ¬good a} ^ fuel

The loop fails exactly when all its runs fail, with probability q ^ fuel, where q is the probability that one run fails.

theorem Cslib.OracleComp.evalWith_retry_none_le {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) {q : ENNReal} (hfail : (evalWith h program).toOuterMeasure {a : α | ¬good a} ≤ q) (fuel : ℕ) :
(evalWith h (retry good program fuel)) none ≤ q ^ fuel

A run failing with probability at most q makes the loop fail with probability at most q ^ fuel.

theorem Cslib.OracleComp.toOuterMeasure_evalWith_retry_le {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (bad : Set α) {ε : ENNReal} (hbad : (evalWith h program).toOuterMeasure {a : α | good a ∧ a ∈ bad} ≤ ε) (fuel : ℕ) :
(evalWith h (retry good program fuel)).toOuterMeasure (some '' bad) ≤ ↑fuel * ε

Union bound for retries. If one run returns a good result in bad with probability at most ε, then the loop returns a result in bad with probability at most fuel ε.

theorem Cslib.OracleComp.evalWith_retry_some {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (fuel : ℕ) (a : α) :
(evalWith h (retry good program fuel)) (some a) = (if good a then (evalWith h program) a else 0) * ∑ i ∈ Finset.range fuel, (evalWith h program).toOuterMeasure {a : α | ¬good a} ^ i

The loop returns a with probability P(a) (1 + q + ⋯ + q ^ (fuel - 1)) when a is good, and never otherwise, where P(a) is the probability that one run returns a and q is the probability that one run fails.

theorem Cslib.OracleComp.expectedCost_retry_succ {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (cost : ι → ENNReal) (fuel : ℕ) :
expectedCost h cost (retry good program (fuel + 1)) = expectedCost h cost program + (evalWith h program).toOuterMeasure {a : α | ¬good a} * expectedCost h cost (retry good program fuel)

One more allowed run costs a run, plus the remaining loop when the run fails.

theorem Cslib.OracleComp.expectedCost_retry {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (cost : ι → ENNReal) (fuel : ℕ) :
expectedCost h cost (retry good program fuel) = expectedCost h cost program * ∑ i ∈ Finset.range fuel, (evalWith h program).toOuterMeasure {a : α | ¬good a} ^ i

The expected cost of the loop is a geometric sum: a run of expected cost c failing with probability q gives c (1 + q + ⋯ + q ^ (fuel - 1)).

theorem Cslib.OracleComp.expectedCost_retry_le_two_mul {ι : Type u} {spec : OracleSpec ι} {α : Type v} (good : α → Prop) [DecidablePred good] (program : OracleComp spec α) (h : (q : ι) → PMF (spec q)) (cost : ι → ENNReal) (hfail : (evalWith h program).toOuterMeasure {a : α | ¬good a} ≤ 2⁻¹) (fuel : ℕ) :
expectedCost h cost (retry good program fuel) ≤ 2 * expectedCost h cost program

Expected cost of a retry loop. If one run fails with probability at most 1 / 2, the loop costs at most twice a run in expectation, whatever the fuel.

theorem Cslib.OracleComp.evalWith_bind_elim_none_le {ι : Type u} {spec : OracleSpec ι} {α β : Type v} (h : (q : ι) → PMF (spec q)) (program : OracleComp spec (Option α)) (next : α → OracleComp spec (Option β)) {c : ENNReal} (hnext : ∀ (a : α), (evalWith h (next a)) none ≤ c) :
(evalWith h do let o ← program o.elim (pure none) next) none ≤ (evalWith h program) none + c

Union bound for sequenced partial programs. A partial program followed, on success, by a partial continuation fails with probability at most the failure probability of the first program plus a bound on the failure probability of every continuation.

theorem Cslib.OracleComp.expectedCost_bind_elim_le {ι : Type u} {spec : OracleSpec ι} {α β : Type v} (h : (q : ι) → PMF (spec q)) (cost : ι → ENNReal) (program : OracleComp spec (Option α)) (next : α → OracleComp spec (Option β)) {c : ENNReal} (hnext : ∀ (a : α), expectedCost h cost (next a) ≤ c) :
(expectedCost h cost do let o ← program o.elim (pure none) next) ≤ expectedCost h cost program + c

The expected cost of a partial program followed, on success, by a continuation is at most the expected cost of the first program plus a bound on the expected cost of every continuation.

theorem Cslib.OracleComp.expectedCost_pi {ι : Type u} {spec : OracleSpec ι} {m : ℕ} {β : Fin m → Type v} (h : (q : ι) → PMF (spec q)) (cost : ι → ENNReal) (programs : (i : Fin m) → OracleComp spec (β i)) :
expectedCost h cost (pi programs) = ∑ i : Fin m, expectedCost h cost (programs i)

The expected cost of running a family of programs is the sum of their expected costs.

theorem Cslib.OracleComp.toOuterMeasure_evalWith_pi_exists_le {ι : Type u} {spec : OracleSpec ι} {m : ℕ} {β : Fin m → Type v} (h : (q : ι) → PMF (spec q)) (programs : (i : Fin m) → OracleComp spec (β i)) (bad : (i : Fin m) → β i → Prop) :
(evalWith h (pi programs)).toOuterMeasure {r : (i : Fin m) → β i | ∃ (i : Fin m), bad i (r i)} ≤ ∑ i : Fin m, (evalWith h (programs i)).toOuterMeasure {a : β i | bad i a}

Union bound for a family of programs: the probability that some coordinate is bad is at most the sum over the coordinates of the probability that it is bad.

noncomputable def Cslib.OracleComp.retrySome {α : Type} (attempt : ProbComp (Option α)) (fuel : ℕ) :

Run up to fuel independent attempts of attempt : ProbComp (Option α) via OracleComp.retry (fun o => o.isSome = true), returning the first some a (collapsed via Option.join) or none if all attempts return none.

noncomputable def Cslib.OracleComp.conditionedSome {α : Type} (attempt : ProbComp (Option α)) (hgood : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) :
PMF (Option α)

Condition a single attempt attempt : ProbComp (Option α) on succeeding (o.isSome = true), yielding a zero-failure PMF (Option α) supported on some a.

noncomputable def Cslib.OracleComp.totalConditionedSome {α : Type} (attempt : ProbComp (Option α)) :
PMF (Option α)

Totalized conditioned single-attempt distribution returning conditionedSome attempt h when some successful outcome is supported and PMF.pure none otherwise.

theorem Cslib.OracleComp.totalConditionedSome_of_exists {α : Type} (attempt : ProbComp (Option α)) (hgood : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) :
totalConditionedSome attempt = conditionedSome attempt hgood

totalConditionedSome equals conditionedSome whenever a successful outcome is supported.

theorem Cslib.OracleComp.totalConditionedSome_of_not_exists {α : Type} (attempt : ProbComp (Option α)) (hnot : ¬∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) :

totalConditionedSome falls back to PMF.pure none when every attempt fails (none).

theorem Cslib.OracleComp.mem_support_eval_retrySome {α : Type} {attempt : ProbComp (Option α)} {fuel : ℕ} {a : α} (ha : some a ∈ (retrySome attempt fuel).eval.support) :
some a ∈ attempt.eval.support

Every outcome in the support of retrySome attempt fuel is either none or a supported some a of attempt.

theorem Cslib.OracleComp.eval_retrySome_none {α : Type} (attempt : ProbComp (Option α)) (fuel : ℕ) :
(retrySome attempt fuel).eval none = attempt.eval none ^ fuel

Exact all-attempts-fail probability of retrySome attempt fuel: (Pr[attempt = none]) ^ fuel.

theorem Cslib.OracleComp.eval_retrySome_some {α : Type} (attempt : ProbComp (Option α)) (fuel : ℕ) (a : α) :
(retrySome attempt fuel).eval (some a) = attempt.eval (some a) * ∑ i ∈ Finset.range fuel, attempt.eval none ^ i

Exact probability of returning some a under retrySome attempt fuel: Pr[attempt = some a] * ∑ i < fuel, (Pr[attempt = none]) ^ i.

theorem Cslib.OracleComp.eval_retrySome_some_eq_conditioned {α : Type} (attempt : ProbComp (Option α)) (hgood : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) (fuel : ℕ) (a : α) :
(retrySome attempt fuel).eval (some a) = (1 - attempt.eval none ^ fuel) * (conditionedSome attempt hgood) (some a)

Exact zero-bias factorization: for every a : α, (ProbComp.eval (retrySome attempt fuel)) (some a) equals (1 - Pr[attempt = none] ^ fuel) * conditionedSome attempt hgood (some a).

theorem Cslib.OracleComp.filter_eval_retrySome_isSome {α : Type} (attempt : ProbComp (Option α)) (hgood : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) {fuel : ℕ} (hretry : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ (retrySome attempt fuel).eval.support) :
(retrySome attempt fuel).eval.filter {o : Option α | o.isSome = true} hretry = conditionedSome attempt hgood

Zero conditional bias: whenever retrySome attempt fuel has positive acceptance probability, conditioning it on o.isSome = true produces the exact same distribution as conditionedSome attempt hgood.

theorem Cslib.OracleComp.closeTo_retrySome_conditioned {α : Type} (attempt : ProbComp (Option α)) (hgood : ∃ o ∈ {o : Option α | o.isSome = true}, o ∈ attempt.eval.support) (fuel : ℕ) {q : ENNReal} (hfail : attempt.eval none ≤ q) :
Probability.CloseTo (retrySome attempt fuel).eval (conditionedSome attempt hgood) (q ^ fuel)

Statistical closeness between fuel-attempt bounded retry retrySome attempt fuel and the rejection-free conditioned distribution conditionedSome attempt hgood: CloseTo ≤ q ^ fuel whenever Pr[attempt = none] ≤ q.

theorem Cslib.OracleComp.closeTo_retrySome_totalConditionedSome {α : Type} (attempt : ProbComp (Option α)) (fuel : ℕ) {q : ENNReal} (hfail : attempt.eval none ≤ q) :
Probability.CloseTo (retrySome attempt fuel).eval (totalConditionedSome attempt) (q ^ fuel)

Statistical closeness between retrySome attempt fuel and totalConditionedSome attempt: holds unconditionally without requiring attempt to have supported successful outcomes.

theorem Cslib.OracleComp.closeTo_retrySome_retrySome {α : Type} (attempt : ProbComp (Option α)) (fuel₁ fuel₂ : ℕ) {q : ENNReal} (hfail : attempt.eval none ≤ q) :
Probability.CloseTo (retrySome attempt fuel₁).eval (retrySome attempt fuel₂).eval (q ^ min fuel₁ fuel₂)

Statistical closeness between two different retry caps fuel₁ and fuel₂ on the same single-attempt sampler: CloseTo ≤ q ^ min fuel₁ fuel₂ whenever Pr[attempt = none] ≤ q.