FreeM: query/cost interpreters and lower-bound lemma #
This file adds query-complexity interpreters to FreeM F α, where the type constructor
F : Type u → Type v represents a query type mapping each query to its response type.
The key operations are:
FreeM.eval oracle p: evaluatepby answering each query usingoracleFreeM.countQueries oracle p: count queries along the oracle-determined pathFreeM.cost oracle weight p: weighted query cost in any additive monoid
The program p must be fixed independently of oracle. Arbitrary pure computation embedded
in p is uncharged, so countQueries and cost measure query complexity rather than total
runtime. However, pure code cannot inspect oracle responses: those enter only through
FreeM.lift.
This provides an alternative to the TimeM-based cost analysis in
Cslib.Algorithms.Lean.MergeSort.MergeSort: here query counting is structural (derived from
the FreeM tree) rather than annotation-based.
The combinatorial lower-bound lemma FreeM.exists_countQueries_ge_clog says: if n distinct
oracles produce n distinct evaluation results from a program whose every response type has
cardinality at most r, then some oracle makes at least ⌈log_r n⌉ queries. The proof uses
the adversarial/partition argument: at each query node, the oracles split by their answer,
and the largest fiber still produces distinct results in the corresponding subtree.
Setting up your own query type #
- Define an inductive
Q : Type u → Type vwhose constructors are the queries, indexed by their response types (seeLEQuery,ArithQuery). - Wrap each constructor with
FreeM.liftto obtain one-step programs (LEQuery.ask). - Write algorithms in
do-notation as values ofFreeM Q α. - Prove correctness by relating
FreeM.evalto a reference implementation, and bounds by equational reasoning with thecountQueries/costsimp lemmas; state them withCslib.Query.UpperBound/Cslib.Query.LowerBound.
TimeM.ret distributes across FreeM.liftM.
Interpreters #
All three interpreters (eval, cost, countQueries) are defined as liftM interpretations
into target monads, routing them through the universal property of the free monad rather
than direct pattern-match on FreeM's constructors:
evalinterprets intoId.costinterprets into aTimeMaccumulator monad (a value paired with a running cost in an arbitrary additive monoid).countQueriesiscostwith unit weight.
The pure and lift-then-bind simp lemmas (eval_pure, eval_liftBind, cost_pure,
cost_liftBind, countQueries_pure, countQueries_liftBind) all reduce by rfl, giving the
same proof ergonomics as direct pattern-match definitions while honouring the universal
property as the primary abstraction.
Evaluate a program by answering each query using oracle.
Defined as liftM to Id, the canonical interpreter into pure values.
Equations
- Cslib.FreeM.eval oracle p = (Cslib.FreeM.liftM (fun {ι : Type ?u.2} (i : F ι) => pure (oracle i)) p).run
Instances For
Weighted query cost in an additive monoid: each query has a cost given by weight,
accumulated along the oracle-determined path. Defined as liftM into TimeM.
Equations
- Cslib.FreeM.cost oracle weight p = (Cslib.FreeM.liftM (fun {ι : Type ?u.3} (op : F ι) => { ret := oracle op, time := weight op }) p).time
Instances For
Count the number of queries along the path determined by oracle.
This is deliberately a def with its own simp lemmas, rather than an abbreviation for
cost oracle (fun _ => 1), so that goals display countQueries.
Equations
- Cslib.FreeM.countQueries oracle p = Cslib.FreeM.cost (fun {ι : Type ?u.2} => oracle) (fun {ι : Type ?u.2} (x : F ι) => 1) p
Instances For
Simp lemmas for countQueries #
Combinatorial lower bound #
If n oracles produce n distinct evaluation results from a FreeM F α program
whose every response type has cardinality at most r (and hence is finite), then some
oracle makes at least ⌈log_r n⌉ queries.
This is the core combinatorial lemma for query complexity lower bounds. The proof uses
the adversarial/partition argument: at each query node, the n oracles split by their
answer; the largest group (size ≥ ⌈n/r⌉) still produces distinct results in the
corresponding subtree, and the induction proceeds there.