Engineering Correctness Through AI, Lean Principles and Formal Verification
Introduction
Chapter 1: The Correctness Problem in Software Engineering
- The Scale and Cost of Software Defects
- Why Testing Cannot Prove Correctness
- The Spectrum of Software Assurance
- Historical Attempts and Shortcomings
- What “Reliable Software” Really Means
- The Promise and Peril of New Tools
Chapter 2: Mathematics, Logic and Software: The Theoretical Bridge
- Propositions, Predicates and Program Properties
- The Curry–Howard Correspondence: Programs as Proofs
- Propositions-as-Types and Dependent Types
- Hoare Logic and Operational Semantics
- Induction, Recursion and Termination
- From Informal Reasoning to Machine-Checked Proof
Chapter 3: Lean 4: Architecture and Foundations
- The Kernel and Trusted Computing Base
- The Type Theory Underlying Lean
- Terms, Types and Elaboration
- Tactics, Proof Scripts and Declarative Proofs
- Inductive Types, Structures and Type Classes
- Mathlib and the Ecosystem
- Lean as a Programming Language
Chapter 4: AI as a Development Partner: Claude’s Capabilities and Limits
- How Large Language Models Generate Code and Proofs
- Claude’s Strengths: Reasoning, Refactoring, Specification
- Failure Modes: Hallucination, Consistency, Depth
- Context Management and Repository-Level Understanding
- Agentic Workflows and Tool Use
- Measuring Reliability and Detecting Errors
Chapter 5: From Natural Language to Formal Specification
- The Ambiguity Problem in Requirements
- Structuring Requirements for Formalization
- Using Claude to Extract Formal Specifications
- Representing Specifications in Lean
- Specification Review and Refinement
- Case Example: Financial Calculation Specifications
Chapter 6: Type-Driven Design with Lean
- Designing Domain Models as Types
- Dependent Types for Refined Specifications
- Type Classes for Generic Correctness
- Eliminating Invalid States at the Type Level
- Refinement Types and Subtype Constraints
- Design Patterns for Type-Safe Systems
Chapter 7: Proof Construction as Engineering Practice
- Proof Strategy and Tactical Decomposition
- Claude-Assisted Proof Generation
- Reading and Debugging Proof Scripts
- Structuring Proofs for Maintenance
- Automation vs. Explicit Reasoning
- When to Use Axioms and When to Resist
Chapter 8: Implementation, Extraction and Integration
- Lean as a Programming Language
- Functional Implementation and Verification
- Code Generation and Extraction
- Interfacing with Other Languages
- Performance Considerations
- Verification-Preserving Compilation
Chapter 9: Requirements Engineering with AI and Formal Methods
- Eliciting Requirements with AI Assistance
- Decomposition and Dependency Analysis
- Conflict Detection and Resolution
- Prioritization and Verification Budgeting
- Traceability from Requirements to Proofs
- Managing Changing Requirements
Chapter 10: Domain Modeling and Algebraic Specifications
- Identifying Core Domain Concepts
- Algebraic Specifications and Equational Laws
- Invariants and Well-Formedness Conditions
- State-Based vs. Behavior-Based Specifications
- Using Claude for Domain Model Refinement
- Domain Models in Lean: Data and Theories
Chapter 11: Architectural Design and Verification Boundaries
- Architecture Styles Amenable to Verification
- Defining Verification Boundaries
- Interface Contracts and Abstraction Barriers
- Layered Verification Strategies
- Deciding What Not to Verify Formally
- Integration Patterns for Verified Components
Chapter 12: Algorithm Development and Correctness Proofs
- Precondition-Postcondition Specifications
- Loop Invariants and Recursive Specifications
- Termination Proofs and Well-Founded Relations
- Complexity Annotations and Verification
- Claude-Guided Algorithm Design and Proof
Chapter 13: Testing, Property Checking and Complementary Assurances
- Where Testing Still Matters
- Property-Based Testing with Lean
- Fuzzing and Symbolic Execution
- Combining Verification with Testing
- Using Claude for Test Generation
- Measuring Coverage and Assurance
Chapter 14: CI/CD, Automation and Continuous Verification
- Build Systems for Verified Projects
- Automated Proof Checking in CI
- Incremental Verification and Caching
- Regression Checking for Proofs
- Monitoring and Alerting
- Toolchains and Developer Experience
Chapter 15: Concurrency, State Machines and Protocol Verification
- Modeling Concurrent Systems
- Linearizability and Consistency Properties
- State Machines and Protocol Specifications
- Model Checking vs. Theorem Proving
- Verifying Distributed Algorithms
- Lean for Protocol Verification: Approaches and Limits
Chapter 16: Security Properties and Side-Channel Considerations
- Formalizing Security Properties
- Access Control and Authorization Specifications
- Cryptographic Protocol Verification
- Side Channels and Information Flow
- Security-Relevant Case Studies
- Limits of Formal Security Verification
Chapter 17: Mathematical Libraries and Reusable Specifications
- Navigating Mathlib for Software Engineers
- Common Patterns and Reusable Theorems
- Building Internal Specification Libraries
- Versioning and Dependency Management
- Contributing Back to the Ecosystem
- When to Roll Your Own vs. Use Existing Libraries
Chapter 18: Metaprogramming, Tactics and Developer Experience
- Custom Tactics and Proof Automation
- Metaprogramming in Lean
- Improving Developer Experience
- Claude and Metaprogramming
- Balancing Automation and Clarity
Chapter 19: Case Studies: End-to-End Verified Components
- Verified Financial Calculation Engine
- Verified Parser and Interpreter
- Verified Authorization System
- Verified Data Structure Library
- Architectural Integration Patterns
- Lessons Learned Across Case Studies
Chapter 20: The Limits of Formal Assurance
- Proving the Wrong Specification
- The Trusted Computing Base Problem
- Compiler Correctness and Extraction
- External Dependencies and FFI
- Hardware, OS and Environment Assumptions
- The Gap Between Verified and Correct-in-Practice
Chapter 21: Risk Analysis and Mitigation
- AI-Generated Content Risks
- Specification and Assumption Risks
- Proof and Verification Risks
- Dependency and Supply Chain Risks
- Organizational and Knowledge Risks
Chapter 22: Comparative Assessment with Alternative Approaches
- Against Traditional Software Engineering
- Against Test-Driven and Behavior-Driven Development
- Against Static Analysis and Linters
- Against Property-Based Testing
- Against Model Checking Tools
- Against Other Proof Assistants (Coq, Isabelle, Agda)
Chapter 23: Adoption Strategies and Organizational Maturity
- Assessing Readiness and Maturity
- Phased Adoption Strategies
- Greenfield vs. Brownfield Projects
- Team Training and Skill Development
- Cost-Benefit Considerations
- Governance and Quality Standards
Chapter 24: The Future: Where AI, Lean and Formal Methods Are Heading
- Trajectories in AI Reasoning and Coding
- Evolution of Lean and Formal Method Tools
- Advances in Automated Theorem Proving
- Economic and Accessibility Implications
- Integration with Broader Dev Practices
- Scenarios for the Next Five Years
Chapter 25: Conclusion: A Realistic Vision for Reliable Software
- The Core Argument Summarized
- What This Methodology Can Deliver
- What It Cannot and Should Not Promise
- The Engineer’s Role in the Age of AI and Verification
- A Practical Path Forward
- Final Assessment