Documentation

ReflectiveSubsystems.SearchCost

The search cost: the native cost model extended to all of Nat.Partrec.Code #

CodeLayer/TimeCost.lean builds a TOTAL paired evaluator evalT : Code → ℕ → ℕ × ℕ whose step laws are unconditional, at the price of leaving rfind' a placeholder: its value-agreement theorem eval_eq_val is stated only on the RfindFree fragment. This file supplies the missing case — the cost of an unbounded search — and so closes the cost model over the whole of Nat.Partrec.Code.

What rfind' costs #

On input Nat.pair a m the code rfind' cf probes cf at Nat.pair a (i + m) for i = 0, 1, 2, … and halts at the least i whose result is 0, returning i + m. The honest sequential cost is what the search actually spends: the probes it makes. So the account is the sum of the probe costs ∑ i ≤ n, tc cf (Nat.pair a (i + m)) over the halting witness n, plus one unit per probe.

Why the model must go partial here #

evalT is total because every one of its clauses is a structural recursion on Code — the prec numeric recursion is Nat.rec, not a recursive call. A rfind' search has no such bound: it runs until it finds a witness, and on an input where no witness exists it does not halt. So the extended evaluator evalP : Code → ℕ →. ℕ × ℕ is genuinely PARTIAL in its input, and its laws carry domain hypotheses where TimeCost's carry none. That is the mathematics of unbounded search, not a defect of the accounting: evalP_dom pins the domain to exactly (eval c n).Dom, no smaller and no larger.

The two theorems that make this an extension rather than a second model #

The rfind' step law comes in two forms: the exact probe-sum identity tcP_rfind', and the uniform bound tcP_rfind'_le — if every probe costs at most B, a search with witness n costs at most (n + 1) * (B + 1), linear in the number of probes. That is the rfind' analogue of tc_prec_le, and it is domain-restricted by construction: the witness n appears in the bound, because no bound uniform in the input exists for an unbounded search.

The probe-cost accumulator #

The total cost of the probes 0, 1, …, n of a partial per-probe cost g. Defined exactly when every one of those probes is (probeSum_dom), which is why the search cost inherits eval's domain rather than a smaller one.

Equations
Instances For

    The extended paired evaluator #

    The extended paired evaluator. evalP c n = Part.some (value, cost) when c halts on n, mirroring Code.eval node for node and charging one step per constructor unfolding — the same accounting as TimeCost.evalT, now carried in the Part monad so that rfind' can be charged honestly. The rfind' clause locates the halting witness with Nat.rfind and charges the probes the search actually made, one unit of overhead apiece.

    Equations
    Instances For

      The value component of the extended evaluator (equals Code.eval — see eval_eq_valP).

      Equations
      Instances For

        The extended native sequential-time cost: the step count of evalP, charging every probe an unbounded search makes. Total over Code; partial in the input exactly where Code.eval is.

        Equations
        Instances For

          Step laws for the extended evaluator #

          Coherence with the total model on the rfind'-free fragment #

          Coherence. On the RfindFree fragment the extended evaluator returns exactly the total evaluator's answer. So TimeCost's unconditional step laws and both magnitude-independent bounds (tc_prec_le, spaceCost_prec_le) are statements about this one cost model, not about a separate one that happens to share a name.

          Value-agreement on ALL of Code — the model is total #

          Value-agreement, unrestricted. The value component of the extended evaluator is Code.eval on every code, rfind' included. This is what TimeCost.eval_eq_val could only state on the RfindFree fragment: the cost tcP is charged along the genuine computation for all of Nat.Partrec.Code.

          The domain is exactly eval's. The extended cost is defined precisely when the computation it prices halts — the search cost neither restricts the domain (as a fuel bound would) nor extends it (as a placeholder value would).

          The rfind' step law #

          theorem ReflectiveSubsystems.tcP_rfind' (cf : Nat.Partrec.Code) (a m n : ℕ) (hn : n ∈ Nat.rfind fun (i : ℕ) => (fun (p : ℕ × ℕ) => decide (p.1 = 0)) <$> evalP cf (Nat.pair a (i + m))) :
          tcP cf.rfind' (Nat.pair a m) = Part.map (fun (x : ℕ) => x + (n + 1)) (probeSum (fun (i : ℕ) => tcP cf (Nat.pair a (i + m))) n)

          The rfind' step law: the probe-sum identity. Given the halting witness n — the least offset at which cf returns 0 — the search on Nat.pair a m costs the probes it made, plus one unit of overhead per probe.

          theorem ReflectiveSubsystems.tcP_rfind'_le {cf : Nat.Partrec.Code} {B : ℕ} (a m n : ℕ) (hn : n ∈ Nat.rfind fun (i : ℕ) => (fun (p : ℕ × ℕ) => decide (p.1 = 0)) <$> evalP cf (Nat.pair a (i + m))) (hB : ∀ i ≤ n, ∀ s ∈ tcP cf (Nat.pair a (i + m)), s ≤ B) (s : ℕ) :
          s ∈ tcP cf.rfind' (Nat.pair a m) → s ≤ (n + 1) * (B + 1)

          The uniform probe bound — the rfind' analogue of tc_prec_le. If every probe the search makes costs at most B, the whole search costs at most (n + 1) * (B + 1): linear in the number of probes, with no dependence on the magnitude of the values probed.

          The witness n is a hypothesis, not a constant, and that is the honest form: unlike prec, whose iteration count is read off its own input, an unbounded search has no bound uniform in the input, so no ∀ n-free version of this statement is true.