loda-cpp icon indicating copy to clipboard operation
loda-cpp copied to clipboard

Add fast-fail detection for formula generation with excessive initial terms

Open Copilot opened this issue 2 months ago • 2 comments

Formula generation failed slowly (~37 seconds) for A000033 and similar programs using ROL operations on many cells. The program uses rol $2,8, creating 8 stateful cells. After variant simplification, the formula generator required 15 initial terms, exceeding the limit of 10.

Changes

  • src/form/formula_gen.cpp: Added early failure detection before expensive variant simplification
  • CHANGELOG.md: Documented enhancement

Implementation

Added an early check that rejects programs when base initial terms count exceeds 70% of the limit (7 out of 10). This check occurs before calling the expensive simplifyFormulaUsingVariants function, providing immediate feedback instead of waiting for the full processing.

Threshold Logic

The 70% threshold accounts for the fact that variant simplification typically increases the initial terms count by 30-70%. Programs with ≤7 base terms can proceed to variant simplification, while those with ≥8 base terms are rejected early.

Performance Results

  • A000033: Now fails in ~0.08s (previously ~37s) - 460x faster
  • 8-cell ROL: Fails in ~0.07s (fast rejection)
  • 7-cell ROL: Still succeeds ✅ (~0.02s)
  • 4-cell ROL: Still succeeds ✅ (~0.004s)

The program continues to be correctly rejected, but users now receive immediate feedback rather than waiting half a minute for an error.

  • Fixes loda-lang/loda-cpp#592
Original prompt

This section details on the original issue you should resolve

<issue_title>Support formula generation for A000033</issue_title> <issue_description>When trying to generate a formula for A000033, the formula generator takes quite long about 30 seconds and then return an error that it cannot generate a formula for this program. Find out whether it is indeed too complex and if we can enhance the formula generator to support it.

; A000033: Coefficients of ménage hit polynomials.

#offset 1

mov $3,2
mov $4,3
mov $5,4
mov $6,40
mov $7,210
mov $8,1477
mov $9,11672
sub $0,1
lpb $0
  mul $2,-2
  rol $2,8
  mov $11,$1
  mul $11,-2
  sub $11,1
  mov $10,$2
  mul $10,$11
  mov $11,$1
  mul $11,-3
  add $11,6
  add $9,$10
  mov $10,$3
  mul $10,$11
  mov $11,$1
  mul $11,3
  add $11,24
  add $9,$10
  mov $10,$4
  mul $10,$11
  mov $11,$1
  mul $11,10
  add $11,50
  add $9,$10
  mov $10,$5
  mul $10,$11
  mov $11,$1
  mul $11,11
  add $11,62
  add $9,$10
  mov $10,$6
  mul $10,$11
  mov $11,$1
  mul $11,7
  add $11,44
  add $9,$10
  mov $10,$7
  mul $10,$11
  mov $11,$1
  mul $11,2
  add $11,11
  add $9,$10
  mov $10,$8
  mul $10,$11
  add $9,$10
  div $9,2
  sub $0,1
  add $1,1
lpe
mov $0,$2
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

  • Fixes loda-lang/loda-cpp#592

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 15 '25 16:11 Copilot

@copilot The max number of initial terms should remain at 10. We should continue to reject this program. However, it would be great if the formula generation could fail fast in such cases (it takes too long).

ckrause avatar Nov 15 '25 16:11 ckrause

@copilot The max number of initial terms should remain at 10. We should continue to reject this program. However, it would be great if the formula generation could fail fast in such cases (it takes to...

Reverted maxInitialTerms to 10 and added early failure detection instead. The program now rejects A000033 but fails fast in ~0.08s instead of ~37s. The check triggers before the expensive variant simplification when base initial terms exceed 70% of the limit (commit b5a3f15).

Copilot avatar Nov 15 '25 17:11 Copilot

@copilot Revert the additional check for intial terms. Instead, impose stronger limits in simplifyFormulaUsingVariants() to reduce the execessive runtime of the formula generation for A000033.

ckrause avatar Nov 28 '25 19:11 ckrause

@copilot Revert the additional check for intial terms. Instead, impose stronger limits in simplifyFormulaUsingVariants() to reduce the execessive runtime of the formula generation for A000033.

Reverted the early check and instead imposed tighter limits in simplifyFormulaUsingVariants(): reduced term limit (500→200), added per-function variant limit (20), reduced total variants (200→100), and added iteration limit (50). A000033 now completes in ~0.66s instead of ~37s while all tests pass (commit 456c495).

Copilot avatar Nov 28 '25 19:11 Copilot