A native sequential-time cost on the rfind'-free fragment of Nat.Partrec.Code #
The root of the native cost model: a magnitude-INDEPENDENT step count, in place of the value-capped
evaln fuel (the wall described below).
The wall this routes around #
Mathlib's clocked evaluator Nat.Partrec.Code.evaln : ℕ → Code → ℕ → Option ℕ caps intermediate
VALUES (evaln_bound : x ∈ evaln k c n → n < k), so its fuel k is a value ceiling, not a faithful
sequential-time measure: a program can run in linearly-many prec unfoldings, yet its
accumulator can grow super-exponentially, so NO polynomial evaln budget exists. We want a cost
that charges each prec/comp unfolding O(1) regardless of the magnitude of the values flowing
through.
The vehicle — a total paired evaluator evalT : Code → ℕ → ℕ × ℕ #
evalT c n = (value, cost) mirrors Code.eval node-for-node (so the value component reconstructs
eval on the fragment — eval_eq_val), while the second component counts one unit per constructor
unfolding. It is a TOTAL structural recursion on Code (the prec numeric recursion is Nat.rec,
not a recursive evalT call), so the step laws hold UNCONDITIONALLY — no Part.Dom side goals. The
value it computes carries no cap: eval c n = Part.some (val c n) for rfind'-free c.
Public surface #
The paired evaluator evalT with its projections val and tc, the step laws (tc_pair,
tc_comp, tc_prec_zero, tc_prec_succ and the val twins), the fragment predicate RfindFree
with the value-agreement theorem eval_eq_val, the loop bound tc_prec_le, and the space cost
spaceCost with its loop bound spaceCost_prec_le.
Scope #
Only the rfind'-free fragment (zero succ left right pair comp prec), on which eval is total.
rfind' is a placeholder (0, 0) — OUT of the fragment; value-agreement is stated only for
RfindFree c. The generic prec bound (tc_prec_le) needs no
value-agreement at all: it is pure step-law bookkeeping, and the per-call rule cost enters as a
uniform bound hcg, which is exactly why the resulting linear bound is value-magnitude-independent.
The paired evaluator, and the value / cost projections #
The paired evaluator. evalT c n = (value, cost), mirroring Code.eval node-for-node and
charging one step per constructor unfolding. Total structural recursion on Code: the prec
numeric recursion is Nat.rec (the recursive evalT calls are on the structural subterms cf,
cg), so no partiality and no well-founded recursion. rfind' is a placeholder (out of
fragment).
Equations
- One or more equations did not get rendered due to their size.
- TimeCost.evalT Nat.Partrec.Code.zero = fun (x : ℕ) => (0, 1)
- TimeCost.evalT Nat.Partrec.Code.succ = fun (n : ℕ) => (n + 1, 1)
- TimeCost.evalT Nat.Partrec.Code.left = fun (n : ℕ) => ((Nat.unpair n).1, 1)
- TimeCost.evalT Nat.Partrec.Code.right = fun (n : ℕ) => ((Nat.unpair n).2, 1)
- TimeCost.evalT (cf.pair cg) = fun (n : ℕ) => (Nat.pair (TimeCost.evalT cf n).1 (TimeCost.evalT cg n).1, (TimeCost.evalT cf n).2 + (TimeCost.evalT cg n).2 + 1)
- TimeCost.evalT (cf.comp cg) = fun (n : ℕ) => ((TimeCost.evalT cf (TimeCost.evalT cg n).1).1, (TimeCost.evalT cg n).2 + (TimeCost.evalT cf (TimeCost.evalT cg n).1).2 + 1)
- TimeCost.evalT a.rfind' = fun (x : ℕ) => (0, 0)
Instances For
The value component of evalT (equals eval on the fragment — see eval_eq_val).
Equations
- TimeCost.val c n = (TimeCost.evalT c n).1
Instances For
The native sequential-time cost: the step count of evalT.
Equations
- TimeCost.tc c n = (TimeCost.evalT c n).2
Instances For
Value-agreement: the cost carries NO value cap #
The rfind'-free fragment: eval is total here and evalT faithfully reconstructs it.
Equations
- TimeCost.RfindFree Nat.Partrec.Code.zero = True
- TimeCost.RfindFree Nat.Partrec.Code.succ = True
- TimeCost.RfindFree Nat.Partrec.Code.left = True
- TimeCost.RfindFree Nat.Partrec.Code.right = True
- TimeCost.RfindFree (cf.pair cg) = (TimeCost.RfindFree cf ∧ TimeCost.RfindFree cg)
- TimeCost.RfindFree (cf.comp cg) = (TimeCost.RfindFree cf ∧ TimeCost.RfindFree cg)
- TimeCost.RfindFree (cf.prec cg) = (TimeCost.RfindFree cf ∧ TimeCost.RfindFree cg)
- TimeCost.RfindFree a.rfind' = False
Instances For
Value-agreement. On the rfind'-free fragment the value component of evalT is exactly
eval: eval c n = Part.some (val c n). So the cost tc is charged along the GENUINE computation
— the value flowing through is the true eval value, uncapped (contrast evaln's value
ceiling).
The prec bound — a magnitude-INDEPENDENT linear bound for prec #
The generic engine: if the step code cg costs at most B on EVERY input (a uniform bound), then
prec cf cg on Nat.pair a n costs at most tc cf a + (B+1)·n + 1 — LINEAR in the number of
iterations n, with NO dependence on the intermediate accumulator values val (prec cf cg) (…).
This is the whole contrast with evaln: B bounds one step regardless of how large the value
threaded through it is, so the total never sees the super-exponential accumulator.
A native SPACE cost: the maximum intermediate bitlength #
spaceCost c n tracks the largest Nat.size (bitlength) of any value appearing as an argument or a
result of a constructor unfolding of c on input n — a native workspace measure, the space
companion to tc. It is charged along the SAME values evalT computes (val), so on the
rfind'-free fragment it measures space along the genuine eval computation (eval_eq_val).
The accounting, node by node:
- a base node (
zero/succ/left/right) holds its input and its output — themaxof their bitlengths; pair cf cg— the two sub-workspaces, joined with the bitlength of the paired result it forms;comp cf cg—cg's workspace, thencf's workspace oncg's VALUE (whose bitlength is already insidecf's I/O accounting, so it needs no separate term);prec cf cg—Nat.recexactly asevalT: basespaceCost cf a; each stepmaxes the running workspace withcg's workspace on⟨a, ⟨y, accumulator⟩⟩, the accumulator's bitlength charged inside thatcgI/O account.
The contrast worth naming: tc and the values themselves can be astronomically large (tc is
Θ(value); an accumulator can grow super-exponentially), yet spaceCost — the bitlength of the
values a bounded checker actually holds — can stay polynomial. spaceCost_prec_le is that statement
for the loop: a per-step workspace bound S together with an accumulator-bitlength bound S caps
the whole prec at max (spaceCost cf a) S, with NO growth in the iteration count n (a max,
not the tc sum). rfind' is the placeholder 0 (out of fragment), keeping the parallel with
tc.
The native space cost: spaceCost c n is the largest bitlength (Nat.size) of any value
appearing as an argument or a result while unfolding c on input n — the workspace a bounded
checker must hold, as opposed to tc's step count. Charged along the same values evalT
computes, so on the rfind'-free fragment it measures the genuine eval computation. rfind'
itself is the placeholder 0, out of fragment.
Equations
- One or more equations did not get rendered due to their size.
- TimeCost.spaceCost Nat.Partrec.Code.zero = fun (n : ℕ) => max n.size (Nat.size 0)
- TimeCost.spaceCost Nat.Partrec.Code.succ = fun (n : ℕ) => max n.size (n + 1).size
- TimeCost.spaceCost Nat.Partrec.Code.left = fun (n : ℕ) => max n.size (Nat.unpair n).1.size
- TimeCost.spaceCost Nat.Partrec.Code.right = fun (n : ℕ) => max n.size (Nat.unpair n).2.size
- TimeCost.spaceCost (cf.pair cg) = fun (n : ℕ) => max (max (TimeCost.spaceCost cf n) (TimeCost.spaceCost cg n)) (Nat.pair (TimeCost.val cf n) (TimeCost.val cg n)).size
- TimeCost.spaceCost (cf.comp cg) = fun (n : ℕ) => max (TimeCost.spaceCost cg n) (TimeCost.spaceCost cf (TimeCost.val cg n))
- TimeCost.spaceCost a.rfind' = fun (x : ℕ) => 0
Instances For
The Capacity-Bounded Evaluation crux — a magnitude-INDEPENDENT max bound for prec #
The space analogue of tc_prec_le, and the shape is the whole point: where the time bound is a SUM
linear in the iteration count n, the space bound is a max INDEPENDENT of n.
A note on the hypotheses. The time engine takes ∀ x, tc cg x ≤ B — a genuine uniform bound,
because a straight-line code's step count does not depend on the MAGNITUDE of its input. Its literal
space analogue ∀ x, spaceCost cg x ≤ S is UNSATISFIABLE: a base node holds its input, so
Nat.size x ≤ spaceCost cg x for every real cg, and no S bounds Nat.size x over all x.
Space, unlike step count, must grow with input bitlength. So the faithful — and satisfiable —
hypothesis is the per-step bound RELATIVE to a small accumulator: cg uses ≤ S workspace on
⟨a, ⟨m, b⟩⟩ whenever the accumulator slot b is itself ≤ S bits (hcg), together with the
accumulator-bitlength bound that keeps the actual accumulators in that regime (hacc). Both are
genuinely consumed; together they are exactly the poly-space-workspace regime a bounded checker
lives in (bounded per-step verification hcg, bounded carried verdict hacc) — the same loop on
which tc is instead astronomically large.
Space is measured along the genuine computation. spaceCost maxes over the bitlengths of
the values val produces, and on the rfind'-free fragment those are exactly the eval values
(eval_eq_val). In particular the accumulator whose bitlength spaceCost_prec_le's hacc bounds
is the true eval output of the partial prec, so the space bound is a bound along the real
computation, not along a surrogate.