Learn2Lean

Verified Pen-and-Paper Arithmetic

The arithmetic you learned in school, written as programs and proved correct in Lean 4.

Formal verification has a reputation as specialist work, something for researchers and senior engineers. But the question underneath it is plain enough: does a program actually do what it is meant to? School arithmetic is a good place to start asking, because everyone already knows what the right answer looks like.

The stakes are not academic. A single wrong division once cost Intel around $475 million in recalled chips, and the same kind of proof you'll write here is what now guards processors, compilers, aircraft software and cryptography. The wager of this book is that none of it is specialist magic: if you can follow long division, you can read โ€” and write โ€” a machine-checked guarantee that a program is correct.

Every chapter takes one of these algorithms and works through it the same way: describe the method, implement it as a Lean 4 program, run it a step at a time, then prove it correct for every possible input. You don't need to know Lean to begin. The writing keeps almost every line within reach of someone seeing it for the first time.

Start reading โ†’
How to use this book. Most of it runs in your browser โ€” the explanations, the step-by-step visualisations, and the multiple-choice checks. The exception is the "Write some Lean" worksheet that closes each chapter: it sends your answer to Lean to compile, so it needs the checker server running locally (see web/checker). Or clone the repo and do those exercises in your own editor instead.
Part I ยท Foundations
Part II ยท The four operations
Part III ยท Number tricks
Epilogue

Source and all Lean proofs: github.com/UnOrdinary19/Lean_formalization.