Skip to content

Latest commit

 

History

History
70 lines (56 loc) · 4.21 KB

README.md

File metadata and controls

70 lines (56 loc) · 4.21 KB

Learn Lean 4 (the practical bits)

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.

Chapters

Part I — Language basics

Part II — Data structures

  • 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, do notation, <|>

Part III — I/O and effects

  • Ch09 — The IO monad and do notation — 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.Net TCP/UDP, HTTP via curl + Lean.Data.Json
  • Ch13 — Tasks, refs, mutexes — IO.asTask / wait, Std.Mutex, Std.Channel, race + cooperative cancel

Part IV — Optional deeper dives

  • Ch14 — JSON — Json.mkObj, compress / pretty, parsing, deriving ToJson, FromJson, .jsonl streaming
  • 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

Part V — Coming from Haskell / choosing Lean

  • 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

How this is built

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.