Newton for one-counter loops, and accelerated unrolling for slowly converging loops
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reading the existing lower-bound strategies in prodigy/analysis/lowerbounds/, especially the earlier newton.py and onecounter.py implementation mentioned in the issue, then inspect how strategies are registered and how PGF truncation represents bounded supports. The issue is complete when the one-counter Newton strategy and the truncation-based acceleration option produce sound lower bounds and residual mass, with the stated upper bounds and moments, and are benchmarked against plain unrolling on the named examples.
Written by the indexing model from the issue text.
Description
Use Newton iteration as an option to speed up unrolling.
Observation
The forward semantics of a while loop is linear in the distribution, so Newton on the loop's fixed-point equation over distributions is just the exact solve of (I − T) G = G_0 (what the symbolic strategies do). But for structured loops the infinite linear system collapses to a finite polynomial (non-linear) equation, and there Newton's quadratic convergence beats Kleene:
- One-counter loops
while (x > c) { body }where every iteration changes the counter by at least −1 (skip-free downwards) with a step distribution that does not depend on the counter (homogeneous), plus reward variables. By homogeneity and skip-freeness, the first passage fromc + 1 + kdown tocis the sum ofk + 1independent one-level passages, so the generating functionU(Y)of the rewards until exit is the least solution ofu = f(u, Y)withf(u, Y) = Σ P(k, r) Y^r u^(k+1)(non-negative coefficients). Started inx = c + mthe output isU(Y)^m, so the output distribution isG(U(Y), Y). - Newton
N(v) = v + (1 − f_u(v))^(−1) (f(v) − v)fromv = 0converges toUfrom below; by convexityv ≤ N(v) ≤ Ucoefficient-wise for every0 ≤ v ≤ U, so rounding down after each step is sound (truncated power series in the queried reward, ball arithmetic with FLINTarb_series, keep lower endpoints). Upper bounds by Park induction on the scalar equation atY = 1(anywwithf(w, 1) ≤ wboundsU(1)); moments fromU'(1) = f_Y / (1 − f_u). - An earlier version of
prodigy/analysis/lowerboundshad exactly this as a mode (newton.pywithonecounter.py: detection of one-counter loops, scalar and power-series Newton, Kleene as a switch for comparison); it was not merged with the other lower-bound modes. The maintainer has the code; it can be the starting point. - Checking applicability is syntactic: guard
x > c, counter decreases by at most 1 per iteration on every path, probabilities independent of the counter (detect_one_counter). Cost per step: Newton needsf,f_uand one series division, about 2–3× a Kleene step on the same truncation, but converges quadratically (and with rate 1/2 per step in the critical casef_u(U) = 1, where Kleene converges only like 1/k). So when a loop is one-counter, Newton is the better choice; otherwise fall back to the options below.
Options (for loops that are not one-counter)
- Exact solve on a truncation: restrict the loop to a finite set of states S (states leaving S count as unaccounted mass), solve the finite linear system exactly (rational arithmetic; this is the Newton step of the truncated system). The result is the occupation measure of the truncated loop, a sound lower bound with residual mass = 1 − mass found; grow S adaptively.
- Iteration doubling: compute
T^(2^k)by repeated squaring on a finite truncation, so k steps give 2^k Kleene iterations. - Compare cost: one exact solve on |S| states is O(|S|^3) (sparse: less) versus Kleene's O(nnz) per iteration times the number of iterations; worthwhile when the iteration count needed is ≫ |S|.
Questions
- Which state truncations fit Prodigy's PGF representation (bounded supports per variable, as the sandwich's numerical exploration does in
lowerbounds/)? - How to report the residual soundly (as for unrolling iterates).
Done when
- A loop strategy
newtonfor one-counter loops (lower bounds with residual mass, Park upper bounds, moments), registered like the other strategies, with Kleene as comparison on the same truncation. - A strategy option (e.g.
unroll:accelerate=solve) for loops whose relevant state space can be truncated, with sound lower bounds and residual mass, on all three engines; a benchmark against plain unrolling on the slowly converging examples inpgfexamples/inference/loopy/hard_distributions. - Related: #224 (unrolling performance), #188 (recursion, where Newton proper applies).
🤖 Generated with Claude Code
- Dominant language
- Python
- Stars
- 6
- Forks
- 2
- Avg merge
- 1h 52m
- Merged PRs (30d)
- 187
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from LKlinke/Prodigy
-
area: engines bug priority: medium
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
area: gui bug priority: medium
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
area: gui bug priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
area: engines bug priority: medium
Maintainers usually reply within 1 day
-
area: maintenance priority: low
Maintainers usually reply within 1 day
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day