Introduction
Every chapter in this book covers one Lean 4 file except for the chapter “Making Stand Alone Programs With Lean 4” that is more complex. Most files have fewer than 100 lines. Each chapter prints the whole program, explains how it works, shows a real run, and suggests changes you can try.
The programs use only the Lean standard library. You need no packages, and you can run each file with one command:
1 $ lean --run Bayes.lean
The first eight programs are pure Lean: Bayesian inference, a triple store, BM25 document ranking, a text summarizer, a vector store, a Hopfield network, symbolic polynomial calculus, and automatic differentiation. The last six programs call other tools: Apple’s on-device model, the Lightpanda browser, Ollama, Google Gemini, SPARQL endpoints, and two web search APIs. Those six run curl or another command-line program and read the reply with Lean’s JSON library.
Installing Lean on macOS
Install elan, the Lean toolchain manager, with Homebrew:
1 brew install elan-init
You can use the official install script in place of Homebrew:
1 curl -sSf https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh | sh
Then install the stable toolchain and make it the default:
1 elan toolchain install leanprover/lean4:stable
2 elan default leanprover/lean4:stable
Check that both tools run:
1 lean --version
2 lake --version
I tested every program in this book with Lean 4.35.0 on an Apple silicon Mac. The Lean standard library still changes between releases. If a program fails to compile on a newer release, install the version I used:
1 elan toolchain install leanprover/lean4:v4.35.0
2 elan default leanprover/lean4:v4.35.0
For editing, I suggest VS Code with the Lean 4 extension. It shows types, errors, and proof goals as you type.
Running the examples
The source files are in the source_code directory of this book’s repository. Change to that directory and pass a file to lean --run:
1 $ cd source_code
2 $ lean --run Bayes.lean
3 Prior: has-disease: 0.0100, healthy: 0.9900
4 After 1st (+): has-disease: 0.1610, healthy: 0.8390 -> MAP: healthy
5 After 2nd (+): has-disease: 0.7848, healthy: 0.2152 -> MAP: has-disease
lean --run checks the types in the file, checks any proofs, and then runs the main function. To check a file without running it, leave out --run. A correct file prints nothing.
What each chapter needs
| Chapter | Needs beyond Lean | Lean feature shown | Output in this book |
|---|---|---|---|
| Bayes | Nothing | type classes, implicit parameters | Real run, complete |
| Triple store | Nothing |
Option in do, Coe
|
Real run, complete |
| BM25 | Nothing | folds, Nat and Float conversions |
Real run, complete |
| Summarizer | Nothing |
Std.HashMap, nested folds |
Real run, complete |
| Vector store | Nothing |
abbrev, pipelines, Float
|
Real run, complete |
| Hopfield | Nothing |
Fin, Array.ofFn, native_decide
|
Real run, complete |
| Symbolic math | Nothing |
Rat, OfNat, recursion on Nat
|
Real run, complete |
| Autograd | Nothing |
StateM, inductive types |
Real run, complete |
| Apple Foundation Models | macOS, Apple Intelligence, the fm tool |
IO.Process, IO errors |
Real run |
| Lightpanda | The lightpanda program |
IO.Process, pure string functions |
Real run, cut short |
| Ollama | A local Ollama server and one model | JSON, Except, IO.ofExcept
|
Real run |
| Gemini | GOOGLE_API_KEY |
Except path, let with fallback |
Real run |
| SPARQL | Internet access |
mapM, JSON arrays |
Real run |
| Web search (Exa, Brave) | Two API keys | structures, ToString, chained Except calls |
Real run, cut short |
Chapter 20 lists the errors I hit while writing these programs. If a file does not compile, start there, then check two environment facts: lean --version matches 4.35.0, and the external binary the chapter names is on your PATH. The six network chapters need accounts or keys, and some charge for use. #eval lines run when an editor checks a file, so keep paid calls behind main.
lean --run interprets the file after it checks it, so a program starts slowly. Chapter 18 shows the compiled path with Lake, which matters when you run a program many times. Chapter 19 builds stand-alone programs that run with no Lean installed and shows how to call C code.
The six network chapters all need curl, which macOS includes.