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).
The Nat.size fact the output-size bounds need beyond CodeLayer.Code.SizeArith #
PolyBounded — the poly-in-input-bit-length function algebra #
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.
≤-monotone domination: a function dominated by a poly-bounded one is poly-bounded.
Composition on the inside with a size-non-increasing map (u n ≤ 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.
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.
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.
A PolyTime function is PolyTimeOn every domain, with the same witnessing code.
pair closure #
comp closure — where the inner output-size clause earns its keep #
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.
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 codecf's native cost is poly in its argument's bit-length;hiter— the iteration countm = N.unpair.2is poly in the input bit-lengthNat.size N(a genuine restriction:mcan be exponential inNat.size Nfor 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 boundBstep N(poly inNat.size N) that holds UNIFORMLY over the iterationsi < mof inputN; this is where the accumulator matters — the step input threadsval (prec cf cg) (⟨a, i⟩), soBstepcan 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 ofPolyTime).
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.
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.
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
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 stagesastart,countarePolyTime(sopair-ing them is a poly-time shaper feedingprec);hCval— the iteration COUNTcount nis poly-bounded as a VALUE inNat.size n. This is THE repair overpolyTime_prec: the number of loop iterations iscount n, so it ishCval(not the size clause ofhC, which only boundsNat.size (count n)) that makes the loop's linear step-count term poly.hC's output clause does NOT suffice —size (count n)poly allowscount nitself to be exponential, i.e. exponentially many iterations;hbase— the base codecf's native cost is poly in its argument's bit-length;Bstep/hBstep/hstep— a per-step cost boundBstep n(poly inNat.size n) holding UNIFORMLY over the iterationsi < count n(only the VISITED states, viatc_prec_le'— the step cost may be unbounded over all ofℕ);hacc— the loop's output bit-length is poly (this IS thePolyTimeoutput-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:
- NON-CONSTANT STEP. The step
wcgoutputs the accumulator unchanged, but its native cost runs a nested inner loop over the iteration INDEXi(comp (prec zero zero) …), sotc wcgGROWS withi(wcg_tc_le). Hencetc wcgis unbounded over all ofℕ; onlytc_prec_le'(a bound over the visitedi < count n) applies — the uniformtc_prec_ledoes NOT. This is the point of the repair. - POLY-BIT ACCUMULATOR OF LARGE VALUE. With
astart = idthe accumulator is the seednitself, whose VALUE is exponential inNat.size nyet whose bit-length isNat.size n— sohaccis the genuinely-scalingPolyBounded Nat.size, and the native cost never sees the accumulator magnitude.
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.