1. Your first checked file
Elan selects and installs toolchains. Lean checks files and executes programs. Lake resolves package dependencies, invokes the selected compiler, and builds packages.
Install and select a toolchain
Follow the official installation guide for your operating system. Install VS Code and the official Lean 4 extension. The extension provides the InfoView: a panel that displays types, messages, and the current proof state as you move the cursor.
Shell commands in this book use a Unix-like shell. On Windows, WSL provides the same command and path conventions.
For this book, create a directory containing a lean-toolchain file with this single line:
leanprover/lean4:v4.35.0
When lean is provided by Elan, running it in this directory selects that version. Confirm the compiler actually in use:
lean --version
lake --version
The first command should report version 4.35.0. If Nix supplies Lean directly, the Nix package must match the version in the file. In the deva repository, nix develop supplies the checked version.
Create First.lean, open the directory in VS Code, and enter:
#check Nat
#check Nat → Nat
#eval 19 + 23
def twice (n : Nat) : Nat := n + n
#eval twice 21
example : twice 3 = 6 := by rfl
#check asks for a type. #eval runs an expression and reports a value. Both evaluations above produce 42.
A def gives a name to an expression. An example checks a term against the stated type without giving the declaration a reusable name.
The final line is a proof. rfl succeeds because both sides reduce to the same value. The reduction establishes this particular equality.
Run the file outside the editor as well:
lean First.lean
A successful check exits with status 0. Evaluation messages appear in the output. Use the command-line check to reproduce the editor’s result.
Read the environment
Put the cursor after by in the example. The InfoView displays the goal after ⊢, showing the type of the term you still need to construct. After rfl, there should be no remaining goals.
Lean’s editor can enter Unicode through backslash abbreviations: \to for →, \alpha for α, \forall for ∀, and \vdash for ⊢. The syntax fun x => x uses ASCII, so you can write many first examples without special input. Learn the notation as it appears in the examples.
Exercises
- Define
triple : Nat → Natand evaluate it at14. - Prove
triple 4 = 12usingrfl. - Change the expected result to
13. Read the error and explain why Lean rejects it. Restore the correct statement afterwards.
You are ready to continue when you can distinguish a type-checking command, an evaluation command, a definition, and a theorem.