Documentation

CodeLayer.TimeCost

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
Instances For

    The value component of evalT (equals eval on the fragment — see eval_eq_val).

    Equations
    Instances For

      The native sequential-time cost: the step count of evalT.

      Equations
      Instances For

        Step laws — all UNCONDITIONAL (no Part.Dom side goals) #

        theorem TimeCost.tc_pair (f g : Nat.Partrec.Code) (n : ℕ) :
        tc (f.pair g) n = tc f n + tc g n + 1
        theorem TimeCost.tc_comp (f g : Nat.Partrec.Code) (n : ℕ) :
        tc (f.comp g) n = tc g n + tc f (val g n) + 1

        The comp step law: g runs on n, then f runs on g's VALUE.

        theorem TimeCost.val_pair (f g : Nat.Partrec.Code) (n : ℕ) :
        val (f.pair g) n = Nat.pair (val f n) (val g n)
        theorem TimeCost.val_comp (f g : Nat.Partrec.Code) (n : ℕ) :
        val (f.comp g) n = val f (val g n)
        theorem TimeCost.tc_prec_zero (cf cg : Nat.Partrec.Code) (a : ℕ) :
        tc (cf.prec cg) (Nat.pair a 0) = tc cf a + 1
        theorem TimeCost.val_prec_zero (cf cg : Nat.Partrec.Code) (a : ℕ) :
        val (cf.prec cg) (Nat.pair a 0) = val cf a
        theorem TimeCost.tc_prec_succ (cf cg : Nat.Partrec.Code) (a m : ℕ) :
        tc (cf.prec cg) (Nat.pair a (m + 1)) = tc (cf.prec cg) (Nat.pair a m) + tc cg (Nat.pair a (Nat.pair m (val (cf.prec cg) (Nat.pair a m)))) + 1
        theorem TimeCost.val_prec_succ (cf cg : Nat.Partrec.Code) (a m : ℕ) :
        val (cf.prec cg) (Nat.pair a (m + 1)) = val cg (Nat.pair a (Nat.pair m (val (cf.prec cg) (Nat.pair a m))))

        Value-agreement: the cost carries NO value cap #

        theorem TimeCost.eval_eq_val {c : Nat.Partrec.Code} :
        RfindFree c → ∀ (n : ℕ), c.eval n = Part.some (val c n)

        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.

        theorem TimeCost.tc_prec_le {cf cg : Nat.Partrec.Code} {B : ℕ} (hcg : ∀ (x : ℕ), tc cg x ≤ B) (a n : ℕ) :
        tc (cf.prec cg) (Nat.pair a n) ≤ tc cf a + (B + 1) * n + 1

        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:

        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
        Instances For

          Space step laws — all UNCONDITIONAL (same Nat.rec structure ⇒ no Part.Dom side goals) #

          theorem TimeCost.spaceCost_pair (f g : Nat.Partrec.Code) (n : ℕ) :
          spaceCost (f.pair g) n = max (max (spaceCost f n) (spaceCost g n)) (Nat.pair (val f n) (val g n)).size
          theorem TimeCost.spaceCost_comp (f g : Nat.Partrec.Code) (n : ℕ) :
          spaceCost (f.comp g) n = max (spaceCost g n) (spaceCost f (val g n))

          The comp space law: g's workspace, then f's workspace on g's VALUE (the intermediate val g n bitlength is already inside f's I/O account, so no separate term).

          theorem TimeCost.spaceCost_prec_succ (cf cg : Nat.Partrec.Code) (a m : ℕ) :
          spaceCost (cf.prec cg) (Nat.pair a (m + 1)) = max (spaceCost (cf.prec cg) (Nat.pair a m)) (spaceCost cg (Nat.pair a (Nat.pair m (val (cf.prec cg) (Nat.pair a m)))))

          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.

          theorem TimeCost.spaceCost_prec_le {cf cg : Nat.Partrec.Code} {S a n : ℕ} (hcg : ∀ (m b : ℕ), m ≤ n → b.size ≤ S → spaceCost cg (Nat.pair a (Nat.pair m b)) ≤ S) (hacc : ∀ m ≤ n, (val (cf.prec cg) (Nat.pair a m)).size ≤ S) :
          spaceCost (cf.prec cg) (Nat.pair a n) ≤ max (spaceCost cf a) S
          theorem TimeCost.eval_prec_acc {cf cg : Nat.Partrec.Code} (hc : RfindFree (cf.prec cg)) (a m : ℕ) :
          (cf.prec cg).eval (Nat.pair a m) = Part.some (val (cf.prec cg) (Nat.pair a m))

          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.