Logging Queries Made by a Computation #
QueryImpl.withLogging records every query/response pair ⟨t, u⟩ to a
WriterT (QueryLog spec) writer layer. It is a response-dependent trace,
defined as a specialisation of QueryImpl.withTraceAppend (see
Tracing.lean): the log is appended after the underlying handler returns,
so a handler failure leaves no log entry for the failed query.
We use the Append-flavoured withTraceAppend (rather than the Monoid
flavoured withTrace) because QueryLog spec = List _ only carries an
[EmptyCollection, Append, LawfulAppend] structure, not a Monoid. This
matches the pre-existing Monad (WriterT (QueryLog spec) m) instance the
rest of WriterTBridge / mvcgen infrastructure already targets.
Push an outer oracle interpretation through the base monad of a
WriterT-valued query implementation.
Instances For
Running a WriterT handler and then interpreting its base oracle
computations is the same as first mapping the handler's base through the
outer interpreter.
Given that so implements the oracles in spec using the monad m,
withLogging so gives the same implementation in the extension WriterT (QueryLog spec) m,
by appending a single-entry log [⟨t, u⟩] after the handler returns response u.
This is the response-dependent specialisation of QueryImpl.withTraceAppend with the
trace function fun t u => [⟨t, u⟩] (a single-element list, the free-monoid
generator of QueryLog spec = List ((t : spec.Domain) × spec.Range t)).
Instances For
Logging preserves failure probability: for any base monad m with MonadLiftT m SPMF,
wrapping an oracle implementation with withLogging does not change the probability of failure.
When m = OracleComp spec, both sides are 0 (trivially true). When m can genuinely fail
(e.g. OptionT (OracleComp spec)), this is a non-trivial faithfulness property.
Run an implementation and append each queried input to a StateT list.
This is the state-transformer analogue of withLogging when only the query
inputs are needed: responses are returned exactly as in the base
implementation, while the state records the input sequence in order.
Defined as the response-independent preInsert instrumentation that appends
the queried input t to the state list before delegating to so.
Instances For
A WriterT query log can be replayed as a StateT input log.
For computations over a sum spec + loggedSpec, this theorem compares two
implementations:
- left queries in
specare forwarded unchanged; - right queries in
loggedSpecare either handled withwithLogging, producing aQueryLog loggedSpec, or withappendInputLog, appending just the queried inputs to a state list.
Mapping the WriterT result to (output, initialInputs ++ loggedInputs) yields
exactly the same base-monad computation as running the StateT
implementation from initialInputs.
Simulation oracle for tracking the quries in a QueryLog, without modifying the actual
behavior of the oracle. Requires decidable equality of the indexing set to determine
which list to update when queries come in.
Instances For
Specialization of QueryImpl.probFailure_run_simulateQ_withLogging to loggingOracle.
Specialization of QueryImpl.NeverFail_run_simulateQ_withLogging_iff to loggingOracle.
A total query bound controls the length of every loggingOracle trace in support:
if oa makes at most n queries, then every support point of
(simulateQ loggingOracle oa).run has log length at most n.
Add a query log to a computation using a logging oracle.
Instances For
withQueryLog distributes over bind: the combined log is the
concatenation of the prefix's log and the continuation's log.
withQueryLog of pure x produces (x, []) — no oracle queries,
empty log.
withQueryLog of a single query t produces (u, [⟨t, u⟩]) where
u is the oracle response: one query, one log entry.
For any computation oa and predicate p, the probability of p holding on the output
equals the probability of p ∘ Prod.fst holding on the output of oa.withQueryLog.
Self-log fixed point. The two log layers produced by
oa.withQueryLog.withQueryLog agree on every support point: simulating the
logging oracle over oa.withQueryLog records exactly the queries that the
inner withQueryLog already recorded, since withQueryLog does not add new
queries to the underlying OracleComp.