A Practical Guide to Formal Methods, Model Checking and Building Correct Software
Preface
Part I: Foundations
Chapter 1: The Verification Imperative
- 1.1 The Special Difficulty of Distributed Systems
- 1.2 What Formal Verification Is and Is Not
- 1.3 The Cost of Incorrect Distributed Systems
- 1.4 Why TLA+?
- 1.5 The Role of Claude Code
- 1.6 What You Will Learn
Chapter 2: A Brief History of Formal Methods and Their Industrial Renaissance
- 2.1 From Mathematics to Software
- 2.2 The Industrial Adoption
- 2.3 The AWS Case Study in Detail
- 2.4 The Microsoft Azure Experience
- 2.5 Lessons for the Individual Engineer
Chapter 3: Mathematical Foundations
- 3.1 Set Theory
- 3.2 Propositional and Predicate Logic
- 3.3 State Machines
- 3.4 Concurrent State Machines
- 3.5 Invariants
- 3.6 Summary
Chapter 4: Safety and Liveness
- 4.1 The Fundamental Dichotomy
- 4.2 Safety Properties
- 4.3 Liveness Properties
- 4.4 Combining Safety and Liveness
- 4.5 The Role of Fairness
- 4.6 Practical Guidance
Chapter 5: Introduction to Temporal Logic
- 5.1 The Need for a Logic of Time
- 5.2 The Always Operator
- 5.3 The Eventually Operator
- 5.4 The Next-State Relation
- 5.5 Until
- 5.6 The Leads-To Operator
- 5.7 Temporal Logic in Practice
Part II: Mastering TLA+
Chapter 6: TLA+ Syntax and Semantics
- 6.1 The Structure of a TLA+ Module
- 6.2 Variables and State
- 6.3 Operators and Expressions
- 6.4 Actions and Stuttering
- 6.5 The Specification
- 6.6 The Standard Modules
- 6.7 Common Syntax Patterns
- 6.8 The Semantics of TLA+
- 6.9 Summary
Chapter 7: Writing Your First TLA+ Specification
- 7.1 A Simple Counter
- 7.2 Adding an Invariant
- 7.3 Introducing a Bug
- 7.4 A Concurrent Example
- 7.5 Running TLC
- 7.6 Interpreting TLC Output
- 7.7 Summary
Chapter 8: PlusCal: TLA+ for Programmers
- 8.1 Why PlusCal?
- 8.2 The Basic Structure of a PlusCal Algorithm
- 8.3 Variables and Assignments
- 8.4 Control Flow
- 8.5 Processes and Concurrency
- 8.6 Procedures and Macros
- 8.7 Fairness
- 8.8 The Translation
- 8.9 When to Use PlusCal vs. Raw TLA+
- 8.10 Summary
Chapter 9: Structuring Complex Specifications
- 9.1 The Challenge of Scale
- 9.2 Modules and EXTENDS
- 9.3 Constants and Model Values
- 9.4 The INSTANCE Operator
- 9.5 Parameterized Operators
- 9.6 Hierarchical Specifications
- 9.7 Abstraction and Refinement
- 9.8 Naming Conventions and Style
- 9.9 Summary
Chapter 10: Nondeterminism, Concurrency and State
- 10.1 Nondeterminism in TLA+
- 10.2 Modeling Concurrency
- 10.3 Atomicity and Granularity
- 10.4 Shared State and Data Races
- 10.5 Communication and Message Passing
- 10.6 The State Space Explosion Problem
- 10.7 Summary
Part III: Model Checking with TLC
Chapter 11: The TLC Model Checker
- 11.1 What TLC Is
- 11.2 How TLC Works
- 11.3 The Configuration File
- 11.4 Running TLC
- 11.5 TLC Output
- 11.6 Performance Considerations
- 11.7 Summary
Chapter 12: TLC Configuration and Execution
- 12.1 The Configuration File in Detail
- 12.2 Constant Assignment
- 12.3 Initial State and Next-State Relation
- 12.4 Invariants
- 12.5 Properties
- 12.6 Deadlock Checking
- 12.7 Symmetry Reduction
- 12.8 Views
- 12.9 Constraints
- 12.10 Running TLC with Different Configurations
- 12.11 Summary
Chapter 13: Interpreting Results
- 13.1 Understanding TLC Reports
- 13.2 Reading Counterexamples
- 13.3 Common Types of Errors
- 13.4 Debugging Strategies
- 13.5 Validating Counterexamples
- 13.6 Summary
Chapter 14: Scaling Model Checking
- 14.1 The State Space Explosion Problem
- 14.2 Symmetry Reduction
- 14.3 State-Space Pruning
- 14.4 Abstraction
- 14.5 Optimization Techniques
- 14.6 Incremental Verification
- 14.7 When Model Checking Is Not Enough
- 14.8 Summary
Part IV: Claude Code as Engineering Partner
Chapter 15: Setting Up Claude Code for Formal Verification
- 15.1 What Claude Code Is
- 15.2 Installation
- 15.3 Verifying the Installation
- 15.4 Setting Up a TLA+ Project
- 15.5 Installing TLA+ Tools
- 15.6 Configuring Claude Code for TLA+
- 15.7 Your First Claude Code Session
- 15.8 Best Practices for Using Claude Code
- 15.9 Summary
Chapter 16: CLAUDE.md and Project Configuration
- 16.1 The Role of CLAUDE.md
- 16.2 Structure of a CLAUDE.md File
- 16.3 Writing Effective Instructions
- 16.4 Managing Context
- 16.5 Iterating on CLAUDE.md
- 16.6 Summary
Chapter 17: The Spec-to-Verification Workflow
- 17.1 The Workflow Overview
- 17.2 Step 1: Describing the System
- 17.3 Step 2: Generating the Specification
- 17.4 Step 3: Configuring TLC
- 17.5 Step 4: Running TLC
- 17.6 Step 5: Interpreting Counterexamples
- 17.7 Step 6: Fixing the Specification
- 17.8 Step 7: Iterating
- 17.9 Automating the Workflow
- 17.10 Summary
Chapter 18: Human-in-the-Loop Verification
- 18.1 The Necessity of Human Judgment
- 18.2 The Risks of AI-Assisted Verification
- 18.3 Managing Hallucination
- 18.4 Managing Overconfidence
- 18.5 The Human’s Role in the Loop
- 18.6 Building a Culture of Verification
- 18.7 The Ethics of AI-Assisted Verification
- 18.8 Summary
Part V: Case Studies
Chapter 19: Case Study: A Replicated Key-Value Store
- 19.1 The Problem
- 19.2 System Boundaries and Assumptions
- 19.3 Deriving the Specification
- 19.4 The Complete Specification
- 19.5 TLC Configuration
- 19.6 Running TLC
- 19.7 Introducing a Bug
- 19.8 Analyzing the Counterexample
- 19.9 Fixing the Bug
- 19.10 Lessons Learned
Chapter 20: Case Study: Leader Election and Consensus Protocols
- 20.1 The Leader Election Problem
- 20.2 Specifying a Simple Election Protocol
- 20.3 The TLA+ Specification
- 20.4 Modeling Network Failures
- 20.5 Verifying Safety and Liveness
- 20.6 Lessons from Consensus
- 20.7 Lessons Learned
Chapter 21: Case Study: Distributed Locks and Resource Allocation
- 21.1 The Distributed Lock Problem
- 21.2 Specifying a Centralized Lock Manager
- 21.3 The TLA+ Specification
- 21.4 Verifying Mutual Exclusion
- 21.5 Verifying Progress
- 21.6 Performance Implications of Lock Design
- 21.7 Lessons Learned
Chapter 22: Case Study: Transaction Processing and Fault Tolerance
- 22.1 Transactional Guarantees in a Distributed Setting
- 22.2 Specifying Two-Phase Commit
- 22.3 The TLA+ Specification
- 22.4 Modeling Coordinator and Participant Failures
- 22.5 Verifying Atomicity and Consistency
- 22.6 Message Delivery Semantics
- 22.7 Lessons Learned
Part VI: Integration, Comparison and Beyond
Chapter 23: From Specification to Implementation: Refinement and Correspondence
- 23.1 The Gap Between Model and Code
- 23.2 Refinement Mappings
- 23.3 Implementation Assumptions
- 23.4 Testing as a Complement to Verification
- 23.5 Tracing a Model Through a Production Implementation
- 23.6 Summary
Chapter 24: Formal Verification in the Engineering Workflow and CI/CD
- 24.1 When to Use Formal Verification: A Decision Framework
- 24.2 Integrating TLC into CI/CD Pipelines
- 24.3 Specification as Living Documentation
- 24.4 Team Adoption: Training, Culture and Process
- 24.5 Measuring the Value of Verification
- 24.6 Summary
Chapter 25: The Verification Landscape: TLA+ Versus Alloy, SPIN, Theorem Provers and Testing
- 25.1 Alloy: Relational Logic and Bounded Model Checking
- 25.2 SPIN and Promela: Process-Oriented Verification
- 25.3 Temporal Logic Model Checkers: NuSMV and Beyond
- 25.4 Theorem Provers: Isabelle, Coq, Lean and the TLA+ Proof System
- 25.5 Property-Based Testing and Concurrency Testing
- 25.6 Choosing the Right Tool
- 25.7 Summary
