HoarePrompt: Structural Reasoning About Program Correctness in Natural Language
cs.SE, cs.AI
Submitted: 2025-03-25
Updated: 2026-09-08
Code: https://github.com/msvlab/HoarePrompt
License: http://creativecommons.org/licenses/by/4.0/
The gist: While software requirements are often expressed in natural language, verifying the correctness of a program against such requirements is a hard and underexplored problem.
Terminology
Abstract
While software requirements are often expressed in natural language, verifying the correctness of a program against such requirements is a hard and underexplored problem. Large language models (LLMs) are promising candidates for addressing this challenge, however our experience shows that they are ineffective in this task, often failing to detect even straightforward bugs. To address this gap, we introduce HoarePrompt, a novel approach that adapts fundamental ideas from program verification to natural language artifacts. Inspired from the strongest postcondition calculus, HoarePrompt employs a systematic, step-by-step process in which an LLM generates natural language descriptions of reachable program states at various code points. To manage loops, we propose few-shot-driven k-induction, an adaptation of the k-induction method widely used in model checking. Once program states are described, HoarePrompt leverages the LLM to assess whether the program, annotated with these state descriptions, conforms to the natural language requirements. For evaluating the quality of classifiers of program correctness with respect to natural language requirements, we constructed CoCoClaNeL, a challenging dataset of solutions to programming competition problems. Our experiments show that HoarePrompt improves the MCC by 61% compared to directly using Zero-shot-CoT prompts for correctness classification. Furthermore, HoarePrompt outperforms a classifier that assesses correctness via LLM-based test generation by an MCC increase of 106%. The inductive reasoning mechanism contributes a 26% boost to MCC, underscoring its effectiveness in managing loops.
Sources
- Ask Me Anything: A simple strategy for prompting language models
- Program Synthesis with Large Language Models
- Verified Code Transpilation with LLMs
- CodeT: Code Generation with Generated Tests
- Reasoning Runtime Behavior of a Program with LLM: How Far Are We?
- What is the Role of Small Models in the LLM Era: A Survey
- Verifying LLM-Generated Code in the Context of Software Verification with Ada/SPARK
- CoCoST: Automatic Complex Code Generation with Online Searching and Correctness Testing
- CodeCoT: Tackling Code Syntax Errors in CoT Reasoning for Code Generation
- AgentCoder: Multi-Agent-based Code Generation with Iterative Testing and Optimisation
- LiveCodeBench: Holistic and Contamination Free Evaluation of Large Language Models for Code
- Draft, Sketch, and Prove: Guiding Formal Theorem Provers with Informal Proofs
- Large Language Models as Test Case Generators: Performance Evaluation and Enhancement
- Towards General Loop Invariant Generation: A Benchmark of Programs with Memory Manipulation
- CodeMind: Evaluating Large Language Models for Code Reasoning
- GroupDebate: Enhancing the Efficiency of Multi-Agent Debate Using Group Discussion
- ClarifyGPT: Empowering LLM-based Code Generation with Intention Clarification
- Towards Automated Verification of LLM-Synthesized C Programs
- NExT: Teaching Large Language Models to Reason about Code Execution
- Training on the Benchmark Is Not All You Need
Related papers
- Falsification-Based Verification of LLM-Generated Optimization Models: Sound Test Batteries and Their Detection Limits
- GitSkills: A Dataset of Agent Skills on GitHub
- SABER: Benchmarking Operational Safety of LLM Coding Agents in Stateful Project Workspaces
- PackMonitor: Enabling Zero Package Hallucinations Through Decoding-Time Monitoring
- IntentCoding: Amplifying User Intent in Code Generation
- Incentives and Outcomes in Bug Bounties