Documentation

CslibFrontier.Languages.Probabilistic.Oracle.Codec

Typed views of word oracles #

An oracle game is often stated twice: once for an adversary that sends named word requests and reads word replies, and once for an adversary over typed queries and replies. An OracleCodec records how typed queries travel on the word channels: encodeQuery writes a query as a request, decodeQuery reads the requests that carry a query, and encodeReply writes each reply.

OracleCodec.adapt runs a word program against a typed oracle: a request that carries a query is forwarded and its reply encoded, and any other request is answered [] without a call. Dually, OracleCodec.wordOracle is the word oracle that a typed oracle presents. run_adapt and runWith_adapt say that the two views agree: the typed run of c.adapt P is the word run of P against c.wordOracle. A word game therefore equals the typed game of the adapted adversary exactly when its oracle is c.wordOracle of the typed one, that is, when it answers requests carrying no query with [] and leaves its state unchanged on them, and answers the others through the typed oracle.

Main definitions #

Main results #

structure Cslib.OracleCodec (Op Q : Type) (spec : Q → Type) :

Typed queries q : Q with replies spec q, carried on word requests Op × List Bool with word replies. Each query has one request, and decodeQuery recognizes exactly these requests; replies are encoded injectively.

  • encodeQuery : Q → Op × List Bool

    The request carrying a query.

  • decodeQuery : Op × List Bool → Option Q

    The query carried by a request, if any.

  • decodeQuery_eq_some_iff (request : Op × List Bool) (q : Q) : self.decodeQuery request = some q ↔ self.encodeQuery q = request

    A request carries a query exactly when it is that query's request.

  • encodeReply (q : Q) : spec q ↪ List Bool

    The word reply to a query.

@[simp]
theorem Cslib.OracleCodec.decodeQuery_encodeQuery {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (q : Q) :

A query's request carries that query.

Distinct queries have distinct requests.

def Cslib.OracleCodec.wordOracle {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {m : Type → Type v} [Monad m] (oracle : (q : Q) → m (spec q)) (request : Op × List Bool) :

The word oracle presented by a typed oracle: a request carrying a query is answered through the oracle and its reply encoded; any other request is answered [] without a call.

def Cslib.OracleCodec.adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) :

Run a word program against a typed oracle. Private samples are kept, and each request is answered as by c.wordOracle, with the typed oracle as an external call.

@[simp]
theorem Cslib.OracleCodec.wordOracle_encodeQuery {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {m : Type → Type v} [Monad m] (oracle : (q : Q) → m (spec q)) (q : Q) :
c.wordOracle oracle (c.encodeQuery q) = ⇑(c.encodeReply q) <$> oracle q

The word oracle answers a query's request through the typed oracle.

theorem Cslib.OracleCodec.wordOracle_of_decodeQuery_eq_none {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {m : Type → Type v} [Monad m] (oracle : (q : Q) → m (spec q)) {request : Op × List Bool} (h : c.decodeQuery request = none) :
c.wordOracle oracle request = pure []

The word oracle answers a request carrying no query with [].

@[simp]
theorem Cslib.OracleCodec.adapt_pure {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (a : α) :
c.adapt (pure a) = pure a

Adapting keeps returned values.

@[simp]
theorem Cslib.OracleCodec.adapt_bind {Op Q α β : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) (next : α → ProbOracleComp (fun (x : Op × List Bool) => List Bool) β) :
c.adapt (program >>= next) = do let a ← c.adapt program c.adapt (next a)

Adapting preserves sequencing.

@[simp]
theorem Cslib.OracleCodec.adapt_map {Op Q α β : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (f : α → β) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) :
c.adapt (f <$> program) = f <$> c.adapt program

Adapting preserves deterministic postprocessing.

@[simp]
theorem Cslib.OracleCodec.adapt_sample {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (program : ProbComp α) :

Adapting keeps private samples.

@[simp]
theorem Cslib.OracleCodec.adapt_query {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (request : Op × List Bool) :

A word request becomes the word oracle's answer.

theorem Cslib.OracleCodec.interpret_wordOracle {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {m : Type → Type v} [Monad m] [LawfulMonad m] (random : (n : ℕ) → m (Fin (n + 1))) (oracle : (q : Q) → m (spec q)) (request : Op × List Bool) :

Interpreting the typed calls of the word oracle interprets the word oracle of the typed interpretation.

theorem Cslib.OracleCodec.interpret_adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {m : Type → Type v} [Monad m] [LawfulMonad m] (random : (n : ℕ) → m (Fin (n + 1))) (oracle : (q : Q) → m (spec q)) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) :

Typed and word runs agree: interpreting an adapted program with a typed oracle interprets the word program with the word oracle, under the same interpretation of private samples.

theorem Cslib.OracleCodec.run_adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) (oracle : (q : Q) → ProbComp (spec q)) :
(c.adapt program).run oracle = program.run (c.wordOracle oracle)

Running an adapted program against a typed oracle runs the word program against its word oracle.

theorem Cslib.OracleCodec.runWith_adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {State : Type} (oracle : (q : Q) → StateT State PMF (spec q)) (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) (s : State) :
ProbOracleComp.runWith oracle (c.adapt program) s = ProbOracleComp.runWith (c.wordOracle oracle) program s

Against a stateful typed oracle, the joint law of the result and the final state of an adapted program is that of the word program against the word oracle.

theorem Cslib.OracleCodec.isCostBounded_adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {wordCost : Op × List Bool → ℕ} {cost : Q → ℕ} (hcost : ∀ (q : Q), cost q ≤ wordCost (c.encodeQuery q)) {program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α} {n : ℕ} (h : OracleComp.IsCostBounded (Sum.elim (fun (x : ℕ) => 0) wordCost) program n) :
OracleComp.IsCostBounded (Sum.elim (fun (x : ℕ) => 0) cost) (c.adapt program) n

Adapting keeps a cost budget when each query costs at most its request.

theorem Cslib.OracleCodec.isQueryBounded_adapt {Op Q α : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α} {n : ℕ} (h : program.IsQueryBounded n) :
(c.adapt program).IsQueryBounded n

Adapting keeps a query budget.

Standard codecs #

def Cslib.OracleCodec.id (Op : Type) :
OracleCodec Op (Op × List Bool) fun (x : Op × List Bool) => List Bool

Word requests as their own queries, with word replies.

@[simp]
theorem Cslib.OracleCodec.adapt_id {Op α : Type} (program : ProbOracleComp (fun (x : Op × List Bool) => List Bool) α) :
(OracleCodec.id Op).adapt program = program

Adapting along the identity codec changes nothing.

n-bit strings on the single word channel: a request carries a query exactly when it has n bits.

def Cslib.OracleCodec.comap {Op Q : Type} {spec : Q → Type} (c : OracleCodec Op Q spec) {Q' : Type} {spec' : Q' → Type} (e : Q' ≃ Q) (reply : (q : Q') → spec' q ↪ spec (e q)) :
OracleCodec Op Q' spec'

Transport a codec along an equivalence of queries and embeddings of replies.

def Cslib.OracleCodec.withOp {Q : Type} (Op : Type) {R : Type} (c : OracleCodec Unit Q fun (x : Q) => R) :
OracleCodec Op (Op × Q) fun (x : Op × Q) => R

One copy of a single-channel codec on each channel Op: the query (op, q) is the request of q on channel op.

theorem Cslib.OracleCodec.wordOracle_withOp {Op Q R : Type} {m : Type → Type v} [Monad m] (c : OracleCodec Unit Q fun (x : Q) => R) (oracle : Op × Q → m R) (request : Op × List Bool) :
(withOp Op c).wordOracle oracle request = c.wordOracle (fun (q : Q) => oracle (request.1, q)) ((), request.2)

The word oracle of withOp reads the word on any channel with the single-channel codec.