The search workspace: the native SPACE cost extended to all of Nat.Partrec.Code #
CodeLayer/TimeCost.lean builds a total paired evaluator whose second component is a
native workspace
measure spaceCost, at the price of leaving rfind' a placeholder 0: the space account, like the
step account, is stated only on the RfindFree fragment. ReflectiveSubsystems/SearchCost.lean
closed that gap for the step count. This file closes it for the workspace, so that BOTH native cost
measures are total over Nat.Partrec.Code.
What rfind' costs in space #
On input Nat.pair a m the code rfind' cf probes cf at Nat.pair a (i + m) for i = 0, 1, …
and halts at the least i whose result is 0, returning i + m. In space the honest account is
the largest workspace any of those probes needed, joined with the bit-length of the answer the
search forms — the same "form-the-result" convention pair uses. This is the whole point of the
file: where the step account sums over probes, the space account MAXES over them. The counter is
charged only inside the probe calls, exactly as the bounded recursion charges its own counter only
inside its step calls.
Why the model must go partial here #
spaceCost is total because every clause is a structural recursion on Code — the prec numeric
recursion is Nat.rec, not a recursive call. A rfind' search has no such bound, so the extended
evaluator evalW : Code → ℕ →. ℕ × ℕ is genuinely PARTIAL in its input and its laws carry domain
hypotheses. evalW_dom pins the domain to exactly (eval c n).Dom, no smaller and no larger.
One model, not two #
evalW returns a PAIR — the value and the workspace — and its value component is proved to agree
with Code.eval directly (eval_eq_valW), not through a projection against the step-count model.
Two further theorems make this an extension rather than a rival account:
evalW_coherent— on theRfindFreefragmentevalW c n = Part.some (val c n, spaceCost c n), so every result proved againstspaceCost(in particular the magnitude-independent bounded- recursion boundspaceCost_prec_le) is a statement about this model;eval_eq_valW— the value component isCode.evalon ALL ofCode, so the workspace is charged along the genuine computation for every code,rfind'included.
The payoff #
The rfind' space law comes in two forms: the exact identity spaceCostP_rfind', and the bound
spaceCostP_rfind'_le — if every probe fits in S, the whole search fits in
max S (Nat.size (n + m)). The probe count is absent. Where the step account pays
(n + 1)·(B + 1) for n + 1 probes, the search's workspace does not grow with the number of probes
at all; only holding the answer costs bits. As for the step account, the halting witness n stays
a hypothesis rather than a constant: an unbounded search admits no bound uniform in its input.
The probe-workspace accumulator #
The largest workspace among the probes 0, 1, …, n of a partial per-probe workspace g.
Defined exactly when every one of those probes is (probeMax_dom), which is why the search
workspace inherits eval's domain rather than a smaller one. The contrast with the step account's
probeSum is the operation: max, not +.
Equations
- ReflectiveSubsystems.probeMax g 0 = g 0
- ReflectiveSubsystems.probeMax g n.succ = (ReflectiveSubsystems.probeMax g n).bind fun (s : ℕ) => Part.map (max s) (g (n + 1))
Instances For
The extended paired evaluator #
The extended workspace evaluator. evalW c n = Part.some (value, workspace) when c halts
on n, mirroring Code.eval node for node and accounting the workspace exactly as
TimeCost.spaceCost does — a base node holds its input and its output, pair joins its two
sub-workspaces with the bit-length of the result it forms, comp runs cf on cg's value,
bounded recursion maxes the running workspace with the step's — now carried in the Part monad
so that rfind' can be charged honestly: the largest probe workspace, joined with the bit-length of
the answer the search forms.
Equations
- One or more equations did not get rendered due to their size.
- ReflectiveSubsystems.evalW Nat.Partrec.Code.zero = fun (n : ℕ) => Part.some (0, max n.size (Nat.size 0))
- ReflectiveSubsystems.evalW Nat.Partrec.Code.succ = fun (n : ℕ) => Part.some (n + 1, max n.size (n + 1).size)
- ReflectiveSubsystems.evalW Nat.Partrec.Code.left = fun (n : ℕ) => Part.some ((Nat.unpair n).1, max n.size (Nat.unpair n).1.size)
- ReflectiveSubsystems.evalW Nat.Partrec.Code.right = fun (n : ℕ) => Part.some ((Nat.unpair n).2, max n.size (Nat.unpair n).2.size)
Instances For
The value component of the workspace evaluator (equals Code.eval — see eval_eq_valW).
Equations
Instances For
The extended native workspace cost: the largest intermediate bit-length evalW sees, 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 #
The rfind' clause: locate the halting witness, then charge the largest probe workspace,
joined with the bit-length of the answer formed.
Coherence with the total model on the rfind'-free fragment #
Coherence. On the RfindFree fragment the extended evaluator returns exactly the total
model's answer, value and workspace together. So the magnitude-independent bounded-recursion bound
spaceCost_prec_le — and everything built on it — is a statement about this one workspace model,
not about a separate one that happens to share a name.
The workspace half of coherence: on the fragment the search workspace is spaceCost.
Value-agreement on ALL of Code — the model is total #
Value-agreement, unrestricted. The value component of the workspace evaluator is Code.eval
on every code, rfind' included. This is what TimeCost.eval_eq_val could state only on the
RfindFree fragment: the workspace spaceCostP is charged along the genuine computation for all of
Nat.Partrec.Code.
The domain is exactly eval's. The extended workspace is defined precisely when the
computation it prices halts — the account neither restricts the domain (as a fuel bound would) nor
extends it (as a placeholder value would).
The rfind' space laws #
The rfind' space law: the probe-maximum identity. Given the halting witness n — the
least offset at which cf returns 0 — the search on Nat.pair a m occupies the largest workspace
any of its probes needed, joined with the bit-length of the answer n + m it forms.
The per-probe workspace bound — and the probe count is ABSENT. If every probe the search
makes fits in S, the whole search fits in max S (Nat.size (n + m)), no matter how many probes it
made. Contrast the step account, where n + 1 probes cost (n + 1) * (B + 1): rerunning a probe
reuses the same workspace, so only holding the answer adds to the bill.
As for the step account, the witness n is a hypothesis rather than a constant — an unbounded
search has no bound uniform in its input, so no ∀ n-free version of this statement is true.