Skip to content

Setting up Lean 4

12 October 2024

Lean moves quickly, so the first thing to install isn't Lean itself but elan, its toolchain manager. Each project has a lean-toolchain file at its root naming a release, and elan fetches exactly that one, the way rustup does for Rust. Two projects on two different versions of Lean just work.

With elan in place, Lake, the build tool that ships with every toolchain, starts a project:

lake new hello
cd hello
lake build
lake exe hello

That's a small package: a Main.lean, a Lake configuration, and the lean-toolchain pinning the release. Mine ended up as the smallest program there is:

def main : IO Unit :=
  IO.println "Hello, Lean!"

The editor matters more than usual. The VS Code extension (search for Lean 4) shows the infoview beside the code: the goal and the hypotheses wherever the cursor is, updated as you type. Most of what follows was learned by watching it.

Tip

Commit lean-toolchain with the project. Upgrading Lean is then one line in one file, and lake update brings the dependencies along.