Computations with Oracle Access #
A value oa : OracleComp spec α
represents a computation with return type α
,
with access to any of the oracles specified by spec : OracleSpec
.
Returning a value x
corresponds to the computation pure' α x
.
The computation queryBind' i t α ou
represents querying the oracle corresponding to index i
on input t
, getting a result u : spec.range i
, and then running ou u
.
We also allow for a failure'
operation for quitting out.
These operations induce Monad
and Alternative
instances on OracleComp spec
.
pure' α a
gives a monadic pure
operation, while a more general >>=
operator
is derived by induction on the first computation (see OracleComp.bind
).
This importantly allows us to define a LawfulMonad
instance on OracleComp spec
,
which isn't possible if a general bind operator is included in the main syntax.
We also define simple operations like coin : OracleComp coinSpec Bool
for flipping a fair coin,
and $[0..n] : ProbComp (Fin (n + 1))
for selecting from an inclusive range.
Note that the monadic structure on OracleComp
exists only for a fixed OracleSpec
,
so it isn't possible to combine computations where one has a superset of oracles of the other.
We later introduce a set of type coercions that mitigate this for most common cases,
such as calling a computation with spec
as part of a computation with spec ++ spec'
.
An OracleQuery
to one of the oracles in spec
, bundling an index and the input to
use for querying that oracle, implemented as a dependent pair.
Implemented as a functor with the oracle output type as the constructor result.
- query {ι : Type u} {spec : OracleSpec ι} (i : ι) (t : spec.domain i) : spec.OracleQuery (spec.range i)
Instances For
Equations
Instances For
Equations
- (OracleSpec.query i t).index = i
Instances For
Equations
- (OracleSpec.query i t).input = t
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
- One or more equations did not get rendered due to their size.
OracleComp spec α
represents computations with oracle access to oracles in spec
,
where the final return value has type α
.
The basic computation is just an OracleQuery
, augmented with pure
and bind
by FreeMonad
,
and failure
is also added after by the OptionT
transformer.
In practive computations in OracleComp spec α
have have one of three forms:
return x
to succeed with somex : α
as the result.do u ← query i t; oa u
whereoa
is a continutation to run with the query resultfailure
which terminates the computation early SeeOracleComp.inductionOn
for an explicit induction principle.
Equations
- OracleComp spec = OptionT (FreeMonad spec.OracleQuery)
Instances For
Equations
Equations
- OracleComp.instInhabited = { default := failure }
Lift a query by first lifting to the free moand and then to the option.
Equations
Instances For
Automatically coerce an OracleQuery spec α
to an OracleComp spec α
.
Equations
- OracleComp.instMonadLiftOracleQuery = { monadLift := fun {α : Type ?u.15} => OracleComp.lift }
Lift a function on oracle queries to one on oracle computations.
Equations
- One or more equations did not get rendered due to their size.
Alias of OracleComp.liftM_def
.
coin
is the computation representing a coin flip, given a coin flipping oracle.
Equations
Instances For
$[0..n]
is the computation choosing a random value in the given range, inclusively.
By making this range inclusive we avoid the case of choosing from the empty range.
Instances For
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Nicer induction rule for OracleComp
that uses monad notation.
Allows inductive definitions on compuations by considering the three cases:
return x
/pure x
for anyx
do let u ← query i t; oa u
(with inductive results foroa u
)failure
SeeoracleComp_emptySpec_equiv
for an example of using this in a proof. If the final result needs to be aType
and not aProp
, seeOracleComp.construct
.
Equations
- ⋯ = ⋯
Instances For
Equations
- ⋯ = ⋯
Instances For
NOTE: if inductionOn
could work with Sort u
instead of Prop
we wouldn't need this,
not clear to me why lean doesn't like unifying the Prop
and Type
cases.
If the output type of C
is a monad then OracleComp.mapM
is usually preferable.
Equations
- OracleComp.construct pure query_bind failure oa = FreeMonad.construct (fun (t : Option α) => Option.rec failure pure t) (fun {β : Type ?u.72} => query_bind) oa
Instances For
Version of construct
with automatic induction on the query
in when defining the
query_bind
case. Can be useful with spec.DecidableEq
and spec.FiniteRange
.
NOTE: may be better to just use this universally in all cases? avoids duplicating lemmas below.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Alias of OracleComp.construct_query
.
Returns true
for computations that don't query any oracles or fail, else false
.
Equations
- OracleComp.isPure (FreeMonad.pure x_1) = x_1.isSome
- x✝.isPure = false
Instances For
Returns true
for computations that fail else false
.
Equations
- OracleComp.isFailure (FreeMonad.pure x_1) = x_1.isNone
- x✝.isFailure = false
Instances For
Implement all queries in a computation using some other monad m
,
preserving the pure and bind operations, giving a computation in the new monad.
The function qm
specifies how to replace the queries in the computation,
and fail
is used whenever failure
is encountered.
If the final output type has an Alternative
instance then simulate
is usually preffered.
Equations
- OracleComp.mapM fail query_map oa = OracleComp.construct pure (fun {β : Type ?u.30} (q : spec.OracleQuery β) (x : β → OracleComp spec α) (r : β → m α) => query_map q >>= r) fail oa
Instances For
Given a computation oa : OracleComp spec α
, construct a value x : α
,
by assuming each query returns the default
value given by the Inhabited
instance.
Returns none
if the default path would lead to failure.
Equations
- oa.defaultResult = OracleComp.mapM (fun {α : Type ?u.27} => none) (fun {α : Type ?u.27} => some ∘ OracleSpec.OracleQuery.defaultOutput) oa
Instances For
Total number of queries in a computation across all possible execution paths.
Can be a helpful alternative to sizeOf
when proving recursive calls terminate.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Alias of the reverse direction of OracleComp.bind_eq_pure_iff
.
Alias of the reverse direction of OracleComp.pure_eq_bind_iff
.
If the oracle indexing-type ι
, output type α
, and domains of all oracles have DecidableEq
then OracleComp spec α
also has DecidableEq
.
Equations
- One or more equations did not get rendered due to their size.
- OracleComp.instDecidableEq (FreeMonad.pure (some x_2)) (FreeMonad.pure (some y)) = match y, h x_2 y with | .(x_2), isTrue ⋯ => isTrue ⋯ | y, isFalse h => isFalse ⋯
- OracleComp.instDecidableEq (FreeMonad.pure none) (FreeMonad.pure (some y)) = isFalse ⋯
- OracleComp.instDecidableEq (FreeMonad.pure (some x_2)) (FreeMonad.pure none) = isFalse ⋯
- OracleComp.instDecidableEq (FreeMonad.pure none) (FreeMonad.pure none) = isTrue ⋯
- OracleComp.instDecidableEq (FreeMonad.pure x_2) (FreeMonad.roll q r) = isFalse ⋯
- OracleComp.instDecidableEq (FreeMonad.roll q r) (FreeMonad.pure x_2) = isFalse ⋯