arXiv:2611.07882cs.SENov 2026

Manuscript Offprint

arXiv:2611.07882

Compositional Loop Invariant Inference for Multiphase Programs via Structured Neurosymbolic Reasoning

Mingxiu Wang, Jiawei Wang, Shuangxiang Kan, Angela Huo, Yuekang Li, Xi Zheng, Xiao Cheng

Mingxiu Wang, Jiawei Wang, Shuangxiang Kan, Angela Huo, Yuekang Li, Xi Zheng, and Xiao Cheng*

Abstract—Loop invariants are essential for deductive program verification, yet automatically inferring them for multiphase programs, whose loops contain conditional branches that induce multiple execution phases with distinct behaviors, remains an open problem. Existing LLM-based methods either reason about loops as a whole or use explicit branch information to generate atomic assertions jointly across branches, without maintaining complete candidate sub-invariants for independent generation and targeted repair. We present CLOVER, a structured neurosymbolic reasoning framework for multiphase invariant inference built on two interlinked mechanisms. First, compositional inference: CLOVER statically decomposes a loop into symbolic loop branch sequences, conjunctions of the loop guard and branch conditions that characterize distinct execution scenarios, and guides the LLM to generate candidate sub-invariants for each one independently, so that each LLM query focuses on a single execution scenario rather than the entire loop. Second, counterexample-guided localized refinement: when SMT verification rejects a composed disjunctive invariant, CLOVER traces the counterexample back through the branch structure to identify which specific loop branch sequence caused the failure and repairs only that sub-invariant, preserving all correct components. Both mechanisms are unified by a shared symbolic structure derived from the loop's branch semantics, which guides what the LLM generates, how candidates are composed, and where refinement is directed. We evaluate CLOVER on 316 linear arithmetic, 50 nonlinear arithmetic, and 54 multiphase benchmarks. CLOVER solves 35 of 54 multiphase benchmarks, outperforming the best LLM-based baseline (29) by 20.7% and the best traditional method (22) by 59.1%. On linear/nonlinear benchmarks, CLOVER remains competitive, solving 355 of 366 programs overall.

Index Terms—Loop invariant inference, program verification, multiphase programs, large language models, neurosymbolic reasoning.

1 INTRODUCTION

Loop invariants [1], [2] are central to deductive program verification [3], enabling formal reasoning about loop correctness via Hoare logic [4]. Large language models (LLMs) have recently emerged as a promising approach to automatic invariant inference, overcoming the scalability limitations of traditional symbolic methods [5], [6], [7], [8], [9], [10] and the generalization difficulties of earlier learning-based methods [11], [12], [13], [14]. Recent LLM-based approaches [15], [16], [17], [18] follow a guess-and-check paradigm: the LLM guesses candidate invariants or clauses, an SMT solver checks them, and counterexample feedback guides the next round of guessing. These methods show good performance on standard linear/non-linear benchmarks.

However, real-world loops often contain conditional branches that cause different iterations to follow different execution paths, each updating variables in its own way. Such programs are called multiphase programs [19] and arise naturally whenever loop variables cross domain boundaries

that activate different branches (e.g., a loop that increments a counter ii and executes one branch while i<ni < n but switches to another once i≥ni \geq n, so that the invariant must capture both phases). Generating correct invariants for multiphase programs is a recognized challenge [20], [21]: the correct invariant is inherently a disjunction of phase-specific invariants, one per execution phase, whose complexity grows combinatorially with the number of branches. Despite longstanding interest, multiphase programs expose a critical blind spot in current LLM-based loop invariant inference techniques.

Many LLM-based methods struggle on multiphase loops because they are blind to the structure of the guess space: in a multiphase loop, the program's branch structure induces a natural decomposition of the guess space into separate dimensions, one per execution phase, and the correct invariant is a disjunction of phase-specific clauses, one drawn from each dimension. These methods ignore this structure and collapse the multi-dimensional guess space into a flat, unstructured one, prompting the LLM with the entire program and producing candidates without distinguishing which dimension each belongs to. The resulting clauses are then combined without branch awareness: LaM4Inv [15] combines clauses exclusively via conjunction, unable to represent disjunctive invariants, while Clause2Inv [16] enumerates both conjunctions and disjunctions, causing the candidate pool to grow exponentially with the number of phases. When verification fails, the counterexample feedback is exploited only coarsely at program level, unable to localize

which phase dimension caused the failure. Even approaches that improve feedback quality, such as LORIS [18], which detects local reasoning errors in the LLM's proof steps, still operate on the loop as a whole and cannot attribute failures to specific phases.

PAL2Inv [22] is a recent path-sensitive exception. It applies abstract interpretation to obtain initial invariants, prompts an LLM to generate atomic assertions for every enumerated loop path, and uses counterexample-guided clause combination to assemble disjunctions of path-level conjunctions. This design makes path information explicit, but its generation step still asks the LLM to reason about all paths in one request, and its search state consists of path-indexed pools of atomic clauses rather than multiple alternative, complete sub-invariants for each path. Refinement therefore enriches and recombines clause pools instead of maintaining competing global compositions in which one selected phase component can be replaced while all other choices remain fixed. Thus, path sensitivity alone does not eliminate the simultaneous-reasoning and candidate-composition burdens of multiphase invariant inference.

These failures point to two core challenges. First, the LLM must reason about all phases simultaneously, yet the joint reasoning complexity overwhelms its capacity and the resulting candidate space grows combinatorially. Second, when verification fails, whole-loop feedback is too coarse to identify which phase is responsible, wasting repair effort on phases that are already correct. Our key insight is that the branch structure of a multiphase loop enables two interlinked mechanisms that address both challenges. First, compositional inference: the distinct branch-condition combinations decompose the invariant synthesis task so that the LLM generates sub-invariants for each combination independently, reducing per-step reasoning complexity from the full loop to a single execution scenario. This decomposition also naturally admits multiple candidates per combination, providing candidate diversity that prevents premature commitment. Second, counterexample-guided localized refinement: because these combinations partition the state space, any counterexample can be uniquely mapped to the responsible combination, so the framework repairs only the failed phase's sub-invariant while preserving all other phases.

We realize this architecture in CLOVER, a framework that tightly integrates neural generation and symbolic verification through a shared symbolic structure derived from the program's branch semantics. CLOVER statically analyzes the loop's control flow to extract loop branch sequences, distinct combinations of branch conditions that partition the loop's execution space. This symbolic structure guides LLM reasoning at every stage: it determines what the LLM generates (one sub-invariant per loop branch sequence), how candidates are composed (root-to-leaf disjunctions), and where refinement is directed (the specific loop branch sequence that caused the failure). Through this structure, the framework realizes both contributions. For compositional inference, each loop branch sequence becomes a level in a search tree, and the LLM generates multiple candidate sub-invariants for each level independently, maintaining candidate diversity across levels. For localized refinement, when the solver rejects a composed invariant, CLOVER traces the counterexample back through the loop branch

sequence partition to identify the responsible loop branch sequence and restricts repair to that loop branch sequence alone, preserving all other sub-invariants.

We implement CLOVER and evaluate its performance on three benchmark datasets: 316 linear arithmetic benchmarks, 50 nonlinear arithmetic benchmarks, and 54 multiphase benchmarks. The first two categories are widely used in prior work and serve as standard evaluation sets for state-of-the-art invariant synthesis tools. The multiphase benchmarks are drawn from the CHC-COMP repository [23], which provides realistic, publicly available verification tasks for inductive invariant inference and safety checking, ensuring fair and representative evaluation [24]. We compare CLOVER against state-of-the-art LLM-based tools (LaM4Inv [15], Clause2Inv [16], LORIS [18], and PAL2Inv [22]), representative learning-based approaches (Code2Inv [14], CLN2INV [25], and LiPuS [26]), and traditional methods, including the multiphase invariant synthesizer ImplCheck [19], the CHC solver Eldarica [6], and the software model checkers CPAchecker [27] and UAutomizer [28], demonstrating its effectiveness across both standard and challenging benchmark settings.

Our contributions are summarized as follows:

  • We identify limitations in current LLM-based loop invariant inference methods [15], [16], [18], [22] when applied to multiphase programs: most use whole-loop generation and composition, while even path-sensitive methods do not retain complete local alternatives that support counterexample-guided replacement of one selected loop branch sequence's sub-invariant.

  • We propose CLOVER, a framework built on two interlinked mechanisms: compositional inference, which decomposes invariant synthesis along loop branch sequences so that the LLM reasons about one execution scenario at a time, and counterexample-guided localized refinement, which traces SMT counterexamples through the loop branch sequence partition to identify and repair only the failed sub-invariant while preserving all correct components.

  • We evaluate CLOVER on 316 linear arithmetic, 50 nonlinear arithmetic, and 54 multiphase benchmarks, demonstrating competitive loop invariant synthesis performance on linear/non-linear benchmarks and substantial loop invariant synthesis improvements on multiphase programs over both LLM-based methods, learning-based baselines and traditional verification engines.

References (66)