Documentation

ReflectiveSubsystems.SearchSpace

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:

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
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
    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 #

          theorem ReflectiveSubsystems.evalW_pair (cf cg : Nat.Partrec.Code) (n : ℕ) :
          evalW (cf.pair cg) n = (evalW cf n).bind fun (p : ℕ × ℕ) => Part.map (fun (q : ℕ × ℕ) => (Nat.pair p.1 q.1, max (max p.2 q.2) (Nat.pair p.1 q.1).size)) (evalW cg n)
          theorem ReflectiveSubsystems.evalW_comp (cf cg : Nat.Partrec.Code) (n : ℕ) :
          evalW (cf.comp cg) n = (evalW cg n).bind fun (q : ℕ × ℕ) => Part.map (fun (p : ℕ × ℕ) => (p.1, max q.2 p.2)) (evalW cf q.1)
          theorem ReflectiveSubsystems.evalW_prec_succ (cf cg : Nat.Partrec.Code) (a m : ℕ) :
          evalW (cf.prec cg) (Nat.pair a (m + 1)) = (evalW (cf.prec cg) (Nat.pair a m)).bind fun (p : ℕ × ℕ) => Part.map (fun (q : ℕ × ℕ) => (q.1, max p.2 q.2)) (evalW cg (Nat.pair a (Nat.pair m p.1)))
          theorem ReflectiveSubsystems.evalW_rfind' (cf : Nat.Partrec.Code) (a m : ℕ) :
          evalW cf.rfind' (Nat.pair a m) = (Nat.rfind fun (i : ℕ) => (fun (p : ℕ × ℕ) => decide (p.1 = 0)) <$> evalW cf (Nat.pair a (i + m))).bind fun (n : ℕ) => Part.map (fun (s : ℕ) => (n + m, max s (n + m).size)) (probeMax (fun (i : ℕ) => Part.map Prod.snd (evalW cf (Nat.pair a (i + m)))) n)

          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 #

          theorem ReflectiveSubsystems.spaceCostP_rfind' (cf : Nat.Partrec.Code) (a m n : ℕ) (hn : n ∈ Nat.rfind fun (i : ℕ) => (fun (p : ℕ × ℕ) => decide (p.1 = 0)) <$> evalW cf (Nat.pair a (i + m))) :
          spaceCostP cf.rfind' (Nat.pair a m) = Part.map (fun (x : ℕ) => max x (n + m).size) (probeMax (fun (i : ℕ) => spaceCostP cf (Nat.pair a (i + m))) n)

          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.

          theorem ReflectiveSubsystems.spaceCostP_rfind'_le {cf : Nat.Partrec.Code} {S : ℕ} (a m n : ℕ) (hn : n ∈ Nat.rfind fun (i : ℕ) => (fun (p : ℕ × ℕ) => decide (p.1 = 0)) <$> evalW cf (Nat.pair a (i + m))) (hS : ∀ i ≤ n, ∀ s ∈ spaceCostP cf (Nat.pair a (i + m)), s ≤ S) (s : ℕ) :
          s ∈ spaceCostP cf.rfind' (Nat.pair a m) → s ≤ max S (n + m).size

          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.