Towards Large Language Model Aided Program Refinement

Yufan Cai, Zhe Hou, Xiaokun Luan, David Sanán, Yun Lin, Jun Sun, Jin Song DongarXiv 2024 · DOI

Abstract

The integration of Large Language Models (LLMs) into software engineering has significantly accelerated code production, yet it has introduced challenges regarding reliability and correctness. This paper presents LLM4PR, a framework that leverages LLMs as constraint solvers within a formal refinement calculus. By bridging the gap between creative code generation and mathematical rigor, LLM4PR enables the automated production of programs that are formally verified to satisfy their specifications. Our evaluation demonstrates that LLM4PR achieves high accuracy and maintains consistent performance across rigorous benchmarks, effectively eliminating the "robustness gap" common in traditional LLM-based code generation.

1. Introduction

While Large Language Models (LLMs) like GPT-4 and GitHub Copilot have revolutionized the coding landscape, they remain prone to "hallucinations" and subtle logic errors, particularly in edge cases. Conversely, formal program refinement—the process of transforming abstract mathematical specifications into executable code through correctness-preserving steps—guarantees bug-free software but remains labor-intensive and requires niche expertise.

This paper introduces LLM4PR, a framework designed to automate the refinement process. By treating the LLM as an intelligent agent within a structured refinement calculus, LLM4PR generates code that is not merely "likely correct" but mathematically verified against its formal specification.

2. The LLM4PR Framework

LLM4PR operates as a "mostly automated" loop between an LLM and formal verification tools. The system utilizes a refinement calculus where high-level specifications are iteratively decomposed into concrete implementations using predefined mathematical laws.

2.1 The Refinement Workflow

The process follows five distinct stages:

  1. Formal Specification ($L_{spec}$): The user defines the requirements using first-order logic and the Coq proof assistant language, establishing clear pre-conditions and post-conditions.
  2. Active Prompting: The framework constructs a prompt containing the current specification and a library of "Refinement Laws" (e.g., Assignment, Iteration, and Alternation).
  3. LLM Selection: A fine-tuned LLM selects the appropriate refinement law and generates a corresponding code snippet or sub-specification.
  4. Passive Verification: The system automatically generates verification scripts. It employs Automated Theorem Provers (ATPs) and the Coq proof assistant to ensure the LLM’s output strictly adheres to the mathematical constraints of the specification.
  5. Backtracking: If verification fails, the system provides the error or counterexample back to the LLM for correction or reverts to a previous refinement step to explore an alternative logic path.

3. Experimental Evaluation

To evaluate the efficacy of LLM4PR, the authors utilized three primary datasets:

  • HumanEval: A standard benchmark of 164 programming problems, 157 of which were converted into formal specifications.
  • EvalPlus: A rigorous extension of HumanEval featuring 80x more test cases to identify edge-case failures.
  • Refinement Library: A specialized dataset based on Morgan’s "Programming from Specifications" used to fine-tune the model on refinement logic.

3.1 Key Results

The quantitative results highlight a significant improvement in reliability over standard LLM approaches. LLM4PR successfully generated verified programs for 150 out of 157 problems.

Most notably, the framework addressed the "Robustness Gap." While GPT-4’s performance dropped from 145 on HumanEval to 128 on EvalPlus due to edge-case failures, LLM4PR’s performance remained perfectly flat at 150 on both benchmarks. Because the code is formally verified during generation, it is correct by definition, rendering additional test cases redundant for the purpose of identifying logic errors.

4. Discussion and Insights

4.1 LLMs as Constraint Solvers

A core insight of this work is the shift in how LLMs are prompted. Rather than asking the model to "write a function," LLM4PR asks it to "find an assignment that satisfies a specific logical predicate." This constrains the search space and prevents the model from taking creative but mathematically unsound leaps.

4.2 Limitations and Future Work

Despite its success, LLM4PR faces several challenges:

  • The Oracle Problem: The system currently requires humans to write the initial formal specification. Future work may involve "auto-formalization" to bridge natural language requirements and formal logic.
  • ATP Bottlenecks: The system's power is limited by the underlying Automated Theorem Provers. If an ATP cannot prove a valid property due to complexity, the system fails even if the generated code is correct.
  • Termination Proofs: Proving that loops will eventually terminate remains a difficult problem in formal logic. While LLM4PR uses flexible iteration laws, fully automated termination proofs for all cases remain an area for future research.

5. Conclusion

LLM4PR demonstrates that the combination of LLMs and formal refinement calculus can overcome the inherent unreliability of generative AI in software engineering. By forcing a step-by-step decomposition of complex problems and verifying each step against mathematical specifications, the framework provides a viable path toward the automated generation of high-assurance software.

Summary updated

BibTeX
@misc{cai2024towards,
  author = {Yufan Cai and Zhe Hou and Xiaokun Luan and David Sanán and Yun Lin and Jun Sun and Jin Song Dong},
  title = {{Towards Large Language Model Aided Program Refinement}},
  year = {2024},
  eprint = {2406.18616},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.18616},
}
Figure