Documentation

CslibFrontier.Languages.Probabilistic.Clocked

Clocked oracle programs with unbounded search #

An OracleComp always halts, so it cannot express unbounded search. A clocked oracle program ClockedComp spec α is an OracleComp indexed by a fuel bound: run with fuel k, it either finishes with some a or gives up with none. It is the transformer stack ReaderT ℕ (OptionT (OracleComp spec)), so pure, bind, do notation and the monad laws come from the transformers. ClockedComp.query makes an oracle call and ClockedComp.rfind is unbounded search: with fuel k it tries the first k arguments, each with fuel k.

The partial semantics ClockedComp.eval o P answers queries with a partial handler o : (q : ι) → Part (spec q) and returns the result of the least fuel for which the run finishes; it diverges when an oracle answer on the way is undefined or when no fuel suffices. A run with fuel k is ClockedComp.attempt o P k, the ordinary interpretation OracleComp.interpret of the fuel-k program in Mathlib's Part monad.

The semantics is compositional for stable programs (ClockedComp.Stable): once a run with some fuel finishes, every run with more fuel finishes with the same result, and every run with less fuel is defined and either finishes with that result or runs out. Pure values, queries, bind, map and rfind preserve stability, and on stable programs eval sends them to Part.some, the handler, Part bind and map, and Nat.rfind.

Main definitions #

Main results #

@[reducible, inline]
abbrev Cslib.ClockedComp {ι : Type u} (spec : OracleSpec ι) :
Type → Type u

Oracle programs indexed by a fuel bound. Run with fuel k, a program is a terminating OracleComp that returns some a when it finishes and none when it runs out of fuel.

def Cslib.ClockedComp.query {ι : Type u} {spec : OracleSpec ι} (q : ι) :
ClockedComp spec (spec q)

Make one oracle call, whatever the fuel.

def Cslib.ClockedComp.rfindFrom {ι : Type u} {spec : OracleSpec ι} (p : ℕ → ClockedComp spec Bool) (k : ℕ) :
ℕ → ℕ → OptionT (OracleComp spec) ℕ

The trials i, i + 1, …, i + r - 1 of an unbounded search, each with fuel k.

def Cslib.ClockedComp.rfind {ι : Type u} {spec : OracleSpec ι} (p : ℕ → ClockedComp spec Bool) :

Unbounded search: the least n for which p n returns true, provided p m returns false for every m < n. With fuel k it tries p 0, …, p (k - 1), each with fuel k.

def Cslib.ClockedComp.attempt {ι : Type u} {spec : OracleSpec ι} {α : Type} (o : (q : ι) → Part (spec q)) (P : ClockedComp spec α) (k : ℕ) :

The run of P with fuel k, answering each query with the partial handler o.

def Cslib.ClockedComp.eval {ι : Type u} {spec : OracleSpec ι} {α : Type} (o : (q : ι) → Part (spec q)) (P : ClockedComp spec α) :
Part α

The partial result of P, answering each query with the partial handler o: the result of the run with the least fuel that finishes. It diverges if no run finishes, or if a run with less fuel diverges because the handler is undefined on one of its queries.

def Cslib.ClockedComp.Stable {ι : Type u} {spec : OracleSpec ι} {α : Type} (o : (q : ι) → Part (spec q)) (P : ClockedComp spec α) :

Runs of P against the handler o with different fuel agree: once the run with fuel k finishes with a, the run with any fuel j is defined, and finishes with a unless j < k and it runs out of fuel.

@[simp]
theorem Cslib.ClockedComp.attempt_pure {ι : Type u} {spec : OracleSpec ι} {α : Type} (o : (q : ι) → Part (spec q)) (a : α) (k : ℕ) :
theorem Cslib.ClockedComp.attempt_bind {ι : Type u} {spec : OracleSpec ι} {α β : Type} (o : (q : ι) → Part (spec q)) (P : ClockedComp spec α) (f : α → ClockedComp spec β) (k : ℕ) :
attempt o (P >>= f) k = (attempt o P k).bind fun (r : Option α) => match r with | some a => attempt o (f a) k | none => Part.some none
@[simp]
theorem Cslib.ClockedComp.attempt_query {ι : Type u} {spec : OracleSpec ι} (o : (q : ι) → Part (spec q)) (q : ι) (k : ℕ) :
attempt o (query q) k = Part.map some (o q)
theorem Cslib.ClockedComp.attempt_rfindFrom_succ {ι : Type u} {spec : OracleSpec ι} (o : (q : ι) → Part (spec q)) (p : ℕ → ClockedComp spec Bool) (k r i : ℕ) :
OracleComp.interpret o (rfindFrom p k (r + 1) i).run = (attempt o (p i) k).bind fun (x : Option Bool) => match x with | some true => Part.some (some i) | some false => OracleComp.interpret o (rfindFrom p k r (i + 1)).run | none => Part.some none
theorem Cslib.ClockedComp.attempt_rfind {ι : Type u} {spec : OracleSpec ι} (o : (q : ι) → Part (spec q)) (p : ℕ → ClockedComp spec Bool) (k : ℕ) :
theorem Cslib.ClockedComp.Stable.mono {ι : Type u} {spec : OracleSpec ι} {α : Type} {o : (q : ι) → Part (spec q)} {P : ClockedComp spec α} (hP : Stable o P) {k : ℕ} {a : α} (h : some a ∈ attempt o P k) {j : ℕ} (hj : k ≤ j) :

A finished run of a stable program stays finished with more fuel.

theorem Cslib.ClockedComp.mem_eval_iff {ι : Type u} {spec : OracleSpec ι} {α : Type} {o : (q : ι) → Part (spec q)} {P : ClockedComp spec α} (hP : Stable o P) {a : α} :
a ∈ eval o P ↔ ∃ (k : ℕ), some a ∈ attempt o P k

A stable program returns a exactly when one of its runs finishes with a.

theorem Cslib.ClockedComp.Stable.pure {ι : Type u} {spec : OracleSpec ι} {α : Type} {o : (q : ι) → Part (spec q)} (a : α) :
theorem Cslib.ClockedComp.Stable.bind {ι : Type u} {spec : OracleSpec ι} {α β : Type} {o : (q : ι) → Part (spec q)} {P : ClockedComp spec α} {f : α → ClockedComp spec β} (hP : Stable o P) (hf : ∀ (a : α), Stable o (f a)) :
Stable o (P >>= f)
theorem Cslib.ClockedComp.Stable.map {ι : Type u} {spec : OracleSpec ι} {α β : Type} {o : (q : ι) → Part (spec q)} {P : ClockedComp spec α} (hP : Stable o P) (f : α → β) :
Stable o (f <$> P)
theorem Cslib.ClockedComp.Stable.query {ι : Type u} {spec : OracleSpec ι} {o : (q : ι) → Part (spec q)} (q : ι) :
theorem Cslib.ClockedComp.Stable.rfind {ι : Type u} {spec : OracleSpec ι} {o : (q : ι) → Part (spec q)} {p : ℕ → ClockedComp spec Bool} (hp : ∀ (i : ℕ), Stable o (p i)) :
@[simp]
theorem Cslib.ClockedComp.eval_pure {ι : Type u} {spec : OracleSpec ι} {α : Type} (o : (q : ι) → Part (spec q)) (a : α) :
theorem Cslib.ClockedComp.eval_bind {ι : Type u} {spec : OracleSpec ι} {α β : Type} (o : (q : ι) → Part (spec q)) {P : ClockedComp spec α} {f : α → ClockedComp spec β} (hP : Stable o P) (hf : ∀ (a : α), Stable o (f a)) :
eval o (P >>= f) = (eval o P).bind fun (a : α) => eval o (f a)
theorem Cslib.ClockedComp.eval_map {ι : Type u} {spec : OracleSpec ι} {α β : Type} (o : (q : ι) → Part (spec q)) {P : ClockedComp spec α} (hP : Stable o P) (f : α → β) :
eval o (f <$> P) = Part.map f (eval o P)
@[simp]
theorem Cslib.ClockedComp.eval_query {ι : Type u} {spec : OracleSpec ι} (o : (q : ι) → Part (spec q)) (q : ι) :
eval o (query q) = o q
theorem Cslib.ClockedComp.eval_rfind {ι : Type u} {spec : OracleSpec ι} (o : (q : ι) → Part (spec q)) {p : ℕ → ClockedComp spec Bool} (hp : ∀ (i : ℕ), Stable o (p i)) :
eval o (rfind p) = Nat.rfind fun (i : ℕ) => eval o (p i)