Abstract: Traditional software testing often fails to capture complex business logic and suffers from scalability issues. Formal verification provides a rigorous alternative but is historically labor-intensive. This paper introduces a framework that automates formal verification for backend systems by translating Scala code into Lean 4 using Large Language Models (LLMs). Our results show that the system can verify over 50% of API requirements at a fraction of the cost of manual testing, while maintaining high accuracy in bug detection.
Introduction
Software reliability is a cornerstone of modern engineering, yet traditional testing methodologies—such as unit and integration testing—often fall short. These methods typically suffer from test locality, focusing on isolated functions rather than holistic system states, and frequently fail to validate complex "business logic"—the underlying rules governing an application's behavior. While formal verification offers a mathematically rigorous alternative by proving that code adheres to its specification, its adoption has been hindered by the high cost of manual translation into formal logic and the scarcity of expertise in tools like Coq or Lean.
This paper introduces a novel framework designed to bridge this gap. By combining the mathematical rigor of functional programming with the reasoning capabilities of Large Language Models (LLMs), the framework automates the translation of backend code into formal proofs, significantly reducing the barrier to entry for formal verification.
Methodology
The proposed pipeline leverages a multi-stage process to transform standard backend implementations into verified systems:
- Functional Foundation: The framework utilizes Scala, taking advantage of its strong type system and "pure" functions. By employing Monads and Algebraic Effects, database operations and API calls are represented as pure data transformations, which map cleanly to formal logic.
- Formalization via LLMs: Using models such as
qwen-max, the system translates Scala backend code into Lean 4, a functional programming language and interactive theorem prover. - Theorem Generation: The pipeline parses natural language documentation to automatically generate formal theorem statements in Lean that define expected API behavior.
- Automated Proof Search: A reasoning-heavy LLM, DeepSeek-R1, serves as the automated prover. This stage incorporates Compiler-Guided Refinement, where the model iterates on proofs based on Lean compiler feedback, and Adaptive Few-Shot Learning, which retrieves successful proof patterns from similar APIs.
- Bug Detection via Negation: To distinguish between AI limitations and actual code defects, the system attempts to prove the negation of a theorem. A successful proof of negation provides a concrete counterexample, confirming a bug.
Experimental Evaluation
The framework was evaluated on four realistic Scala-based backend projects:
- UserAuth: Authentication and registration services.
- BankAccount: Financial operations including deposits and withdrawals.
- Email: Authenticated group messaging services.
- TaxiApp: A complex system involving ride tracking and payment processing.
The evaluation suite comprised over 150 API theorems and 100 table property theorems. To assess bug detection capabilities, 11 manual errors were injected into the codebase.
Key Results and Insights
The experimental results highlight the viability of LLM-powered formal verification:
- Verification Performance: The system successfully proved over 50% of all API specifications and table properties. Notably, the "proving ratio" remained stable as project complexity increased, suggesting strong scalability.
- Accuracy and Bug Detection: The initial translation and theorem generation phases achieved over 95% accuracy. The framework detected 72.7% (8 out of 11) of the injected bugs, primarily through counterexample generation during the proof search.
- Economic Viability: The cost of verifying an API was approximately $2.19 in LLM tokens. This represents a substantial cost reduction compared to the manual effort required by specialized test engineers.
- Compositionality: The use of functional programming allows complex APIs to be decomposed into smaller, verifiable units, preventing the state-space explosion typically associated with formal methods.
Limitations and Future Work
Despite its success, the framework faces several challenges. Approximately 40-50% of theorems remained unproven, often due to complex logic or external dependencies not yet modeled in Lean. Furthermore, the current implementation is optimized for functional Scala; adapting it to imperative languages like Java or Python, which feature heavy global state, remains a significant hurdle. Future research will focus on fine-tuning models specifically for Lean proof data and improving the formalization of external components like third-party APIs and database constraints.
Summary updated