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 #
evalP_eq_evalT— on theRfindFreefragment the extended evaluator returns precisely the total one's answer,Part.some (val c n, tc c n). Every result proved againsttctherefore transfers; there is one cost model, not two.eval_eq_valP— the value component agrees withCode.evalon ALL ofCode, not merely on the fragment. This is the sense in which the model is now total: the cost is charged along the genuine computation for every code,rfind'included.
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
- ReflectiveSubsystems.probeSum g 0 = g 0
- ReflectiveSubsystems.probeSum g n.succ = (ReflectiveSubsystems.probeSum g n).bind fun (s : ℕ) => Part.map (fun (x : ℕ) => s + x) (g (n + 1))
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
- One or more equations did not get rendered due to their size.
- ReflectiveSubsystems.evalP Nat.Partrec.Code.zero = fun (x : ℕ) => Part.some (0, 1)
- ReflectiveSubsystems.evalP Nat.Partrec.Code.succ = fun (n : ℕ) => Part.some (n + 1, 1)
- ReflectiveSubsystems.evalP Nat.Partrec.Code.left = fun (n : ℕ) => Part.some ((Nat.unpair n).1, 1)
- ReflectiveSubsystems.evalP Nat.Partrec.Code.right = fun (n : ℕ) => Part.some ((Nat.unpair n).2, 1)
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 #
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.
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.