A "learn-you-a-language"-style tour of Lean 4 as a programming language, with batteries-included examples for everyday tasks: basic types and pattern matching, the standard collections, file I/O, processes, JSON, the network, and a sprinkling of metaprogramming at the end.
If you came here looking for theorem-proving tutorials, see Mathematics in Lean or Theorem Proving in Lean 4 — those cover the proof side of the language in depth. This tutorial complements them: same language, different audience.
Every chapter is a runnable Jupyter notebook (open in JupyterLite
or with the local xlean kernel). Most cells are followed by their
evaluated output as a ```output block so you can read the
chapter without firing up a kernel.
- Ch00 — Setup — elan, lake,
xleankernel - Ch01 — Values and functions —
def,fun, types,#eval/#check - Ch02 — Pattern matching and inductive types —
match,if let,Option, custom enums - Ch03 — Structures and type classes — records,
Add/ToStringinstances
- Ch04 — Lists —
[],::,map/filter/foldl,zipWith,range - Ch05 — Arrays —
#[...], indexing,push/pop, persistent semantics,Id.run do - Ch06 — Strings — UTF-8,
s!"..."interpolation, iterators, padding,toUTF8 - Ch07 —
HashMap/HashSet/TreeMap— keyed containers, counters, dedup - Ch08 —
Option/Except— error handling without exceptions,donotation,<|>
- Ch09 — The
IOmonad anddonotation —pure/←/let mut/IO.Ref, clock + random,try/catch - Ch10 — File I/O — whole-file + streaming + binary + directories,
System.FilePath,/operator - Ch11 — Processes and pipes —
IO.Process.run/output/spawn, stdin pipes, pipe-two-children, kill / wait - Ch12 — Sockets and networking —
Std.NetTCP/UDP, HTTP viacurl+Lean.Data.Json - Ch13 — Tasks, refs, mutexes —
IO.asTask/wait,Std.Mutex,Std.Channel, race + cooperative cancel
- Ch14 — JSON —
Json.mkObj,compress/pretty, parsing,deriving ToJson, FromJson,.jsonlstreaming - Ch15 — Macros —
notation,macro,elab, hygiene, anti-quotation, when not to write one - Ch16 — Type-level programming — type application, value↔type, GADTs / type families, side-by-side with Haskell
- Ch17 — Coming from Haskell — Haskell ↔ Lean 4 syntax/idiom table, mutation (
IORef/MVar/STRef→IO.Ref/let mut), and how to find an API without Hackage (editor,#check,exact?, Loogle / Moogle, grep the source) - Ch18 — Why Lean 4 runs like C, not GHC — compile-to-C pipeline, reference counting vs. tracing GC, FBIP, C/C++ interop with destructors, the GC × FFI mismatch (why 100 K live native handles hurt a copying GC but not refcounting), and an honest "pick Lean vs. pick GHC" table
The chapters live as plain Markdown under docs/tutorial/md/. The
xlean-convert CLI converts each chapter into
the artefact you need:
# Re-evaluate the cells and bake outputs back into the .md
LEAN_PATH=$(pwd)/.lake/build/lib/lean \
xlean-convert --eval docs/tutorial/md/Ch01_Values.md \
-o docs/tutorial/md/Ch01_Values.md
# Build the static HTML site
xlean-convert --site docs/tutorial/md \
-o _site \
--title "Learn Lean 4"That's it — same source, three target formats. See
docs/Convert.md for the full pipeline.