Documentation

CodeLayer.PolyTime

A PolyTime predicate on the native cost tc, with the closure toolkit #

Provenance: the native cost model. Builds on CodeLayer/TimeCost.lean (evalT, val, tc, RfindFree, the step laws, tc_prec_le). Downstream infra for bounding a concrete algorithm's tc (the greedy 1-DL consistency solver, Learning/DecisionListSolver.lean, and the conjunction solver, Learning/ConjunctionSolver.lean), including the domain-relative PolyBoundedOn/PolyTimeOn pair both consume for their well-formed-input bounds.

What the module covers #

The public surface is the predicates PolyBounded (a function polynomial in the input bit-length), PolyTime (an rfind'-free code with polynomial step count and output bit-length) and their domain-relative forms PolyBoundedOn and PolyTimeOn, the PolyBounded algebra (add, mul, pow, mono, comp_le, comp_size_le), and the PolyTime closure results polyTime_pair, polyTime_comp, polyTime_prec, polyTime_loop with a worked example. It also holds the blocker unpair_snd_not_polyBounded, with its consequence not_polyBounded_id, and the constant codes constCode.

HONEST FRAMING — unit cost, measured against input BIT-LENGTH #

tc is a UNIT-cost model: each AST primitive costs 1 regardless of operand magnitude. So "polynomial tc" here means poly-many AST steps as a function of the INPUT BIT-LENGTH Nat.size n — NOT bit-complexity (a single AST step may move an arbitrarily large ℕ). Every definition and lemma is stated in those terms; nothing here claims a bit-cost bound.

The output-size clause is on Nat.size ∘ f, not f #

PolyTime f bundles a time bound AND an output-size bound; the output bound is on the bit-length Nat.size (f n), poly in Nat.size n. This is forced: the alternative "PolyBounded f" (bound the VALUE f n) is false already for succ — f n = n + 1 is exponential in Nat.size n. Bounding Nat.size (f n) is the honest, composable choice (and it is what a downstream comp/prec cost proof actually threads — see polyTime_comp, polyTime_prec).

val of the base codes (for the base PolyTime witnesses) #

The Nat.size fact the output-size bounds need beyond CodeLayer.Code.SizeArith #

PolyBounded — the poly-in-input-bit-length function algebra #

A function is poly-bounded if it is O((Nat.size n + 1)^k) in the input bit-length.

Equations
Instances For
    theorem TimeCost.PolyBounded.exists_mul {t : ℕ → ℕ} (h : PolyBounded t) :
    ∃ (C : ℕ) (k : ℕ), ∀ (n : ℕ), t n ≤ C * (n.size + 1) ^ k

    Normal form: the additive + C is absorbed into the multiplicative term (as (size n+1)^k ≥ 1), giving the cleaner t n ≤ C·(size n+1)^k shape that add/mul/pow compose through.

    theorem TimeCost.PolyBounded.of_mul {t : ℕ → ℕ} {C k : ℕ} (h : ∀ (n : ℕ), t n ≤ C * (n.size + 1) ^ k) :

    Rebuild PolyBounded from the multiplicative normal form.

    theorem TimeCost.PolyBounded.const (a : ℕ) :
    PolyBounded fun (x : ℕ) => a
    theorem TimeCost.PolyBounded.mono {s t : ℕ → ℕ} (hst : ∀ (n : ℕ), s n ≤ t n) (ht : PolyBounded t) :

    ≤-monotone domination: a function dominated by a poly-bounded one is poly-bounded.

    theorem TimeCost.PolyBounded.comp_le {t u : ℕ → ℕ} (ht : PolyBounded t) (hu : ∀ (n : ℕ), u n ≤ n) :
    PolyBounded fun (n : ℕ) => t (u n)

    Composition on the inside with a size-non-increasing map (u n ≤ n).

    theorem TimeCost.PolyBounded.comp_size_le {t u : ℕ → ℕ} (ht : PolyBounded t) (a b : ℕ) (hu : ∀ (n : ℕ), (u n).size ≤ a * n.size + b) :
    PolyBounded fun (n : ℕ) => t (u n)

    Composition on the inside with a map whose output bit-length is linear in its input's: Nat.size (u n) ≤ a · Nat.size n + b keeps a poly-bounded function poly-bounded.

    theorem TimeCost.PolyBounded.add {s t : ℕ → ℕ} (hs : PolyBounded s) (ht : PolyBounded t) :
    PolyBounded fun (n : ℕ) => s n + t n
    theorem TimeCost.PolyBounded.mul {s t : ℕ → ℕ} (hs : PolyBounded s) (ht : PolyBounded t) :
    PolyBounded fun (n : ℕ) => s n * t n
    theorem TimeCost.PolyBounded.pow {t : ℕ → ℕ} (h : PolyBounded t) (j : ℕ) :
    PolyBounded fun (n : ℕ) => t n ^ j

    PolyTime — the bundled predicate #

    def TimeCost.PolyTime (f : ℕ → ℕ) :

    f is poly-time (native cost): some rfind'-free code computes it, its native step count is poly in the input bit-length, AND its output bit-length is poly in the input bit-length. The output-size clause is REQUIRED for polyTime_comp/polyTime_prec to close (the inner call's cost is poly in the inner output's bit-length). See the module note on why it is on Nat.size ∘ f.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      PolyBoundedOn/PolyTimeOn — the domain-relative variants #

      PolyBounded/PolyTime state their bound over ALL of ℕ; a caller whose bound only holds on a well-formed domain (a raw slot of a packed input is exponential off it — see PolyTime's own unpair_snd_not_polyBounded blocker below) needs the relativized notion instead. Consumed by Learning/DecisionListSolver.lean (PurityWF, FindLitWF, SolveWF) and Learning/ConjunctionSolver.lean (ConjWF), which import this file already for PolyTime/PolyBounded.

      def TimeCost.PolyBoundedOn (D : ℕ → Prop) (t : ℕ → ℕ) :

      Poly-time on a domain — the honest "poly cost + output size on well-formed inputs" notion.

      Equations
      Instances For
        def TimeCost.PolyTimeOn (D : ℕ → Prop) (f : ℕ → ℕ) :

        f is computed on D by an rfind-free code whose step count AND output size are both PolyBoundedOn D. Charging the output size too is what keeps the notion honest: a code can be cheap to run and still emit something exponentially wide.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          A polynomially bounded function is polynomially bounded on every domain.

          theorem TimeCost.PolyTime.polyTimeOn {f : ℕ → ℕ} (D : ℕ → Prop) (h : PolyTime f) :

          A PolyTime function is PolyTimeOn every domain, with the same witnessing code.

          theorem TimeCost.PolyTime.congr {f g : ℕ → ℕ} (h : ∀ (n : ℕ), f n = g n) (hf : PolyTime f) :

          PolyTime is invariant under pointwise equality of functions.

          Base closure — zero, succ, left, right are PolyTime #

          theorem TimeCost.polyTime_zero :
          PolyTime fun (x : ℕ) => 0
          theorem TimeCost.polyTime_succ :
          PolyTime fun (n : ℕ) => n + 1

          pair closure #

          theorem TimeCost.polyTime_pair {f g : ℕ → ℕ} (hf : PolyTime f) (hg : PolyTime g) :
          PolyTime fun (n : ℕ) => Nat.pair (f n) (g n)

          comp closure — where the inner output-size clause earns its keep #

          theorem TimeCost.polyTime_comp {f g : ℕ → ℕ} (hf : PolyTime f) (hg : PolyTime g) :

          prec closure — bounded recursion (the loop cost lemma) #

          The exact per-iteration cost refinement of tc_prec_le: only the ACTUAL iteration inputs need the per-step bound B (not all of ℕ), so a per-step cost that grows with the accumulator is fine as long as it stays ≤ B on the states the loop visits.

          theorem TimeCost.tc_prec_le' {cf cg : Nat.Partrec.Code} {a B : ℕ} (n : ℕ) :
          (∀ i < n, tc cg (Nat.pair a (Nat.pair i (val (cf.prec cg) (Nat.pair a i)))) ≤ B) → tc (cf.prec cg) (Nat.pair a n) ≤ tc cf a + (B + 1) * n + 1
          theorem TimeCost.polyTime_prec {cf cg : Nat.Partrec.Code} (hcf : RfindFree cf) (hcg : RfindFree cg) (hbase : PolyBounded (tc cf)) (hiter : PolyBounded fun (N : ℕ) => (Nat.unpair N).2) (Bstep : ℕ → ℕ) (hBstep : PolyBounded Bstep) (hstep : ∀ (N i : ℕ), i < (Nat.unpair N).2 → tc cg (Nat.pair (Nat.unpair N).1 (Nat.pair i (val (cf.prec cg) (Nat.pair (Nat.unpair N).1 i)))) ≤ Bstep N) (hacc : PolyBounded fun (N : ℕ) => (val (cf.prec cg) N).size) :
          PolyTime fun (N : ℕ) => val (cf.prec cg) N

          Bounded-recursion closure. prec cf cg (as a function of its packed input N = ⟨a, m⟩) is PolyTime provided the explicit premises hold:

          • hbase — the base code cf's native cost is poly in its argument's bit-length;
          • hiter — the iteration count m = N.unpair.2 is poly in the input bit-length Nat.size N (a genuine restriction: m can be exponential in Nat.size N for an unconstrained packing, so the caller's loop must arrange a poly step count — see the module notes);
          • Bstep/hBstep/hstep — a per-step cost bound Bstep N (poly in Nat.size N) that holds UNIFORMLY over the iterations i < m of input N; this is where the accumulator matters — the step input threads val (prec cf cg) (⟨a, i⟩), so Bstep can only be poly because that accumulator is;
          • hacc — the running accumulator stays poly-bounded in bit-length across all inputs (this IS the output-size clause of PolyTime).

          Total: tc ≤ tc cf a + (Bstep N + 1)·m + 1, poly by the PolyBounded algebra (via tc_prec_le').

          Part 1 — the blocker: polyTime_prec's hiter premise is undischargeable for a genuine loop #

          polyTime_prec concludes PolyTime for the raw prec cf cg over ALL packed inputs N, but its premise hiter : PolyBounded (fun N => N.unpair.2) is FALSE: a raw second-projection slot is exponential in the input bit-length Nat.size N (the family N = ⟨0, 2^j⟩ has size N ≤ 2j+4 yet N.unpair.2 = 2^j). So as stated the lemma is essentially inapplicable — the raw prec over all inputs genuinely is not PolyTime. polyTime_prec stays as a low-level building block; the usable loop closure is polyTime_loop (Part 2), where the iteration COUNT is a poly-bit function of the actual input, not a raw slot.

          theorem TimeCost.exists_two_pow_gt_poly (C k : ℕ) :
          ∃ (j : ℕ), C * (2 * j + 5) ^ k + C < 2 ^ j

          Exponential beats every fixed polynomial: for all C k, some j has C·(2j+5)^k + C < 2^j. The elementary fact behind the blocker (a raw unpair slot is exponential in the input bit-length). Proved by the =o-of-itself route (mirrors ParityCounterexample.dSQ_not_polyBounded).

          The blocker. A raw second-projection slot is NOT poly-bounded in the input bit-length, so the hiter premise of polyTime_prec cannot be discharged for a genuine loop. Witness family: N = Nat.pair 0 (2^j) has Nat.size N + 1 ≤ 2j+5 (pairing is linear in the parts' bit-lengths, size_pair_le) yet N.unpair.2 = 2^j, and 2^j outgrows every polynomial in 2j+5 (exists_two_pow_gt_poly).

          The identity is not poly-bounded: a value is exponential in its own bit-length, which unpair_snd_not_polyBounded makes precise (the second projection is dominated by the identity).

          Part 2 — the repair: polyTime_loop, the usable bounded-recursion closure #

          The realistic pattern: the loop is comp (prec cf cg) shaper, where the shaper computes the packed input ⟨astart n, count n⟩ from the ACTUAL input n, and the iteration count count n is a genuine poly-bit VALUE function of n (premise hCval : PolyBounded count). This replaces the undischargeable hiter (Part 1): the number of iterations is count n, poly in Nat.size n because hCval bounds the VALUE count n — not a raw slot of the packed input.

          theorem TimeCost.polyTime_id :
          PolyTime fun (n : ℕ) => n

          The identity n ↦ n is PolyTime (code pair left right, reconstructing n from its projections via Nat.pair_unpair; native cost a constant 3). A convenient astart/count building block.

          The c-fold successor tower succ ∘ ⋯ ∘ succ ∘ zero computing the constant c.

          Equations
          Instances For

            constCode c is Mathlib's unary constant tower Code.const c.

            theorem TimeCost.tc_constCode_le (c n : ℕ) :
            tc (constCode c) n ≤ 2 * c + 1
            theorem TimeCost.polyTime_const (c : ℕ) :
            PolyTime fun (x : ℕ) => c

            Every constant function n ↦ c is PolyTime (native cost 2c+1, output bit-length size c).

            theorem TimeCost.polyTime_loop {cf cg : Nat.Partrec.Code} (hcf : RfindFree cf) (hcg : RfindFree cg) {astart count : ℕ → ℕ} (hA : PolyTime astart) (hC : PolyTime count) (hCval : PolyBounded count) (hbase : PolyBounded (tc cf)) (Bstep : ℕ → ℕ) (hBstep : PolyBounded Bstep) (hstep : ∀ (n i : ℕ), i < count n → tc cg (Nat.pair (astart n) (Nat.pair i (val (cf.prec cg) (Nat.pair (astart n) i)))) ≤ Bstep n) (hacc : PolyBounded fun (n : ℕ) => (val (cf.prec cg) (Nat.pair (astart n) (count n))).size) :
            PolyTime fun (n : ℕ) => val (cf.prec cg) (Nat.pair (astart n) (count n))

            Bounded-recursion closure, usable form. The loop n ↦ val (prec cf cg) ⟨astart n, count n⟩ is PolyTime under premises a caller can actually discharge:

            • hA/hC — the input-shaper stages astart, count are PolyTime (so pair-ing them is a poly-time shaper feeding prec);
            • hCval — the iteration COUNT count n is poly-bounded as a VALUE in Nat.size n. This is THE repair over polyTime_prec: the number of loop iterations is count n, so it is hCval (not the size clause of hC, which only bounds Nat.size (count n)) that makes the loop's linear step-count term poly. hC's output clause does NOT suffice — size (count n) poly allows count n itself to be exponential, i.e. exponentially many iterations;
            • hbase — the base code cf's native cost is poly in its argument's bit-length;
            • Bstep/hBstep/hstep — a per-step cost bound Bstep n (poly in Nat.size n) holding UNIFORMLY over the iterations i < count n (only the VISITED states, via tc_prec_le' — the step cost may be unbounded over all of ℕ);
            • hacc — the loop's output bit-length is poly (this IS the PolyTime output-size clause).

            Realized as comp (prec cf cg) (pair ca cc); cost tc (pair ca cc) n + tc cf (astart n) + (Bstep n + 1)·count n + O(1), poly by the PolyBounded algebra (via tc_comp, tc_prec_le').

            Part 3 — a worked example, fully discharged, exercising polyTime_loop #

            wloop := prec (pair left right) wcg: a 3-iteration loop that CARRIES its seed a unchanged (wloop_const), so wloop ⟨n, 3⟩ = n. Two features make it a genuine test rather than a triviality:

            Note on the count. count = fun _ => 3 is a CONSTANT: a prec-free-computable, poly-bit, scaling count does not exist (any prec-free code produces either a constant or an n-magnitude value), so a count that both scales with n and stays poly-bit — the honest C2 template count = input length — must itself be loop-computed. Here the count is constant while the accumulator scales; the two scaling dimensions cannot both be prec-free.

            Worked-example step code: outputs the accumulator (comp left (pair (comp right right) …)) while its cost runs a nested i-fold loop, making the per-step cost grow with the iteration index i.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              Worked example. The 3-iteration loop above is PolyTime via polyTime_loop; every premise (hA, hC, hCval, hbase, Bstep/hBstep/hstep, hacc) is discharged concretely. The step uses tc_prec_le' essentially — wcg_tc_le grows with i, so no uniform tc_prec_le bound exists — and hacc is the scaling PolyBounded Nat.size.