Papers
Topics
Authors
Recent
Search
2000 character limit reached

Guiding LLM-based Loop Invariant Synthesis via Feedback on Local Reasoning Errors

Published 18 May 2026 in cs.PL | (2605.17914v1)

Abstract: We propose a novel framework that provides constructive feedback to an LLM in the "guess-and-check" paradigm by formally verifying its own thinking process and detecting local reasoning errors. We apply this framework to the loop invariant synthesis problem. We prompt the model to produce a step-by-step natural language proof justifying its thinking process for the failed verification condition of its generated loop invariants. Then, we use an LLM to translate the reasoning steps into first-order logic implications, which can be checked automatically. An invalid implication pinpoints the exact logical flaw in the LLM's thinking process, which we then use to construct targeted feedback for refinement. We have implemented our approach in a tool called LORIS and evaluated it on a main benchmark suite of 460 C programs and an additional benchmark suite of 50 C programs each of which involves non-linear properties. On the main benchmark suite, LORIS solved 445 of the programs, and achieved an overall success rate of $93.1\%$. LORIS also demonstrates robustness on the challenging non-linear benchmark suite.

Summary

  • The paper introduces LORIS, a framework that leverages precise, step-specific feedback to correct local reasoning errors in LLM-generated loop invariants.
  • It integrates natural language proof extraction, autoformalization into first-order logic, and SMT verification to iteratively refine candidate invariants.
  • Empirical results demonstrate LORIS outperforms existing methods by solving 93.1% of main-suite cases and achieving robust invariant refinement.

Guiding LLMs in Loop Invariant Synthesis via Feedback on Local Reasoning Errors

Introduction and Motivation

Loop invariant synthesis is a foundational problem in program verification, serving as a cornerstone for proving properties over programs with complex control flow. The canonical "guess-and-check" paradigm remains the dominant strategy: a candidate invariant is heuristically generated ("guess") and subsequently verified for inductiveness and sufficiency ("check"). The recent performance of LLMs in code generation and program reasoning suggests their utility in the "guess" phase, but their solutions are typically undermined by local logical errors, hallucinations, and incomplete reasoning. Crucially, current verification systems offer only binary or coarse-grained counterexample-based feedback; such signals are frequently insufficient to guide an LLM toward correcting the precise defect in its output.

The paper "Guiding LLM-based Loop Invariant Synthesis via Feedback on Local Reasoning Errors" (2605.17914) introduces a framework—instantiated as LORIS—that systematically addresses this gap. By extracting a stepwise natural language proof from the LLM for each failed verification condition (VC), translating these steps to first-order logic, and automatically verifying them, the system provides precise, targeted feedback to guide the LLM's refinement. This enables more robust convergence to correct, inductive invariants, outperforming established LLM-invariant synthesis methods on diverse benchmarks.

Core Framework

The proposed methodology augments the classical "guess-and-check" loop with a feedback-refinement loop grounded in the verification of the LLM's own reasoning artifacts:

  • Initial Guess: The system prompts a Synthesizer LLM to propose candidate invariants for a given program and assertion, generally using zero-shot or few-shot prompting strategies.
  • Verification: Formal verification (e.g., via Frama-C) checks establishment, preservation, and post-condition implications for the generated invariants. If all VCs are satisfied, synthesis terminates.
  • Natural Language Proof Extraction: Upon failure, the LLM is prompted to articulate a step-by-step natural language proof aimed at justifying the problematic VC. This proof is structured for downstream parsing and formalization.

(Figure 1)

Figure 1: The structure of the natural language proof.

  • Autoformalization: A secondary LLM translates each step of the natural proof into first-order logic implications expressed in C-style logic, locally linked to the corresponding proof step.
  • Semantic Checking: Each implication is validated by an SMT solver. Invalid implications pinpoint local logical errors or unjustified leaps in reasoning within the LLM’s proposal.
  • Targeted Feedback: A feedback prompt identifies the specific step and logical flaw, providing this information to the Synthesizer LLM for invariant refinement.
  • Iterative Refinement: The loop iterates until a correct inductive invariant is synthesized or resource limits are reached.

This process isolates local logical errors, which are endemic in LLM-generated reasoning, while avoiding the typical brittleness and lack of faithfulness associated with direct code or proof artifact synthesis.

Methodological Strengths and Technical Contributions

Precision in Feedback and Error Localization

Whereas previous LLM-based invariant synthesis approaches (e.g., counterexample-driven BMC-as-feedback frameworks such as LaM4Inv and Clause2Inv) provide only coarse-grained or conjunct-level feedback, LORIS delivers granular, step-specific guidance derived from formally verified implications. The local checking paradigm is especially effective for subtle errors (e.g., off-by-one mistakes, invalid boundary inferences) that are generally missed when only examining failed VCs holistically. The integration of a lightweight autoformalization pipeline circumvents the brittleness of script-level formalization in systems like Coq and Isabelle, focusing instead on mapping proof steps to checkable logical implications.

Empirical Performance

On a main benchmark suite of 460 C programs and an additional 50-program non-linear suite, LORIS achieved:

  • 445/460 solved (93.1%) with GPT-4.1 on the main suite, compared to 414 (LaM4Inv) and 409 (Clause2Inv).
  • 47/50 solved on the challenging non-linear benchmarks (again outperforming the baselines).

This demonstrates both the efficacy of local feedback for getting beyond the limitations of atomic-clause or BMC-augmented approaches and the method's robustness to model variability.

Generalization and Integration Potential

While the focus is on single loops and numerical domains, the core framework is agnostic to program complexity and is extensible to nested or multiple loops by prompting for multi-level invariant generation and appropriately chaining the feedback mechanism. Potential for application to programs with memory manipulation (e.g., through adaptation to separation logic and suitable solvers) is noted, referencing emerging work in memory-aware invariant generation.

Key Numerical Results and Ablation Insights

  • Feedback Efficiency: On GPT-4.1, 41.2% of successful runs (over all benchmarks) required at least one round of feedback-driven refinement, rising to over 65% for smaller models, indicating the critical role of the feedback mechanism for both model stability and capability.
  • Refinement Trajectory: On average, 70–80% of feedback rounds yield invariants closer (by Jaccard similarity of atomic clauses) to the reference, confirming constructiveness and non-randomness of the corrections.
  • Baseline Comparisons: LORIS achieves not just increased unique problems solved, but also higher robustness—many "directly solvable" problems by other methods only succeed stochastically, while LORIS’s feedback loop increases the per-instance consistency.

Implications and Future Directions

Practical Impact

LORIS sets a new state-of-the-art for data-driven loop invariant synthesis leveraging LLMs, especially for complex or non-linear invariants that defy recovery by template-based, clause-wise, or counterexample-guided learning. Its application is most compelling in settings where minimal task-specific engineering (prompts, templates, datasets) is desirable and where correctness must be statically guaranteed.

Theoretical Consequences

The paper underscores the significance of local reasoning formalization and checking in harnessing the potential of LLMs for formal method tasks. It aligns with a broader movement toward hybrid neuro-symbolic architectures where LLMs serve as heuristics-driven generators and symbolic reasoners act as correctness filters and feedback providers. The results also suggest that improvements in LLM capacity can further amplify the gains from such feedback-guided correction.

Future Prospects

  • Deep Integration with Symbolic Methods: While symbolic learners can assemble partially correct conjuncts, LORIS demonstrates complementary synergy: symbolic methods help with efficiency, but only feedback on thinking steps enables full discovery of missing invariants.
  • Generalization to Heap and Richer Domains: There is tangible future scope in integrating with richer logical theories (e.g., separation logic, quantified invariants for arrays and graphs) and in scaling to real-world codebases—especially as LLMs' program understanding continues to improve.
  • Broader "Guess-and-Check" Problem Classes: The approach may generalize to inductive property mining, invariant learning for concurrent or distributed systems, and automated theorem proving, where stepwise reasoning feedback could help LLMs bridge the gap between human-like creativity and formal rigor.

Conclusion

The framework proposed in "Guiding LLM-based Loop Invariant Synthesis via Feedback on Local Reasoning Errors" demonstrates that the principal bottleneck in LLM-driven program verification—persistent local errors and lack of actionable feedback—can be effectively addressed by formalizing and checking the model’s own chain-of-thought. The approach delivers strong empirical improvements and offers a scalable, domain-generalizable paradigm for integrating formal verification and LLM reasoning. The future of LLM-assisted formal synthesis will likely revolve around not just learning to guess, but learning to understand and act upon their own errors through principled, formal feedback loops.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Collections

Sign up for free to add this paper to one or more collections.

Tweets

Sign up for free to view the 1 tweet with 2 likes about this paper.