You can start with a single file. We’ll ask Lean two questions about 1 + 2 * 3: what kind of value is it, and what do you get when you calculate it? Once you’ve read both answers, use the same file to join some text and compare two ways of doing the same subtraction.
1. Preparing elan and VS Code InfoView
Install VS Code from the Lean installation guide. Open the Extensions tab on the left and install the official Lean 4 extension. Return to the guide, choose Open setup guide, and follow the steps in VS Code to install Lean.
Once setup finishes, choose Terminal → New Terminal in VS Code. A terminal is where you type commands and read the output. Enter the two commands below one at a time, pressing Enter after each line.
# Verify installation (prints Lean 4 and elan versions)
elan --version
lean --versionBoth commands should print a version number. elan chooses which version of Lean to run, and lean is the program that will read and check your file. If the terminal can’t find the commands, check that setup finished, then reopen VS Code and try them again.
rustup installs Rust compiler toolchains and lets you choose which version to use. Lean's elan was modeled on it. For this exercise, the only version manager you need is elan, which you just checked in the terminal.
Create an empty practice folder and open it in VS Code with File → Open Folder. Use the file list on the left to create and save a file named Scratch.lean.
2. Evaluating Expressions with #eval
Put these two lines in the Scratch.lean editor and save. Both lines use the same expression, but the first asks for its type and the second asks for its value.
#check (1 + 2 * 3)
#eval 1 + 2 * 3With the practice folder open, start a new terminal. This time, enter the following command in the terminal and press Enter.
lean Scratch.leanLean reads the file and prints these two answers.
Output:
1 + 2 * 3 : Nat
7The first answer comes from #check. The : Nat on the right means the expression on the left has type Nat. A type tells Lean what kind of value it is working with; Nat is the type of natural numbers, starting at zero.
The second answer, 7, comes from #eval. Multiplication comes first here, so Lean works out 2 * 3 before adding 1. Lean checks the expression’s type, compiles it into code it can run, then runs it and prints the value.1
main is the entry point Lean uses to start an executable program. Here, #eval runs the expression you give it while Lean processes the file, so you can try a calculation without writing a main. In Part 7, you'll write one that reads a name from the command line.
You can compare the same two answers inside VS Code. Put the cursor at the end of the #check line and then the #eval line. The Lean InfoView panel on the right displays the result for the line you’re on.23
Add these four lines to the bottom of the same Scratch.lean file and save it.
#eval "Hello, " ++ "Lean 4!"
#eval (10 : Nat) - 3
#eval (2 : Nat) - 5
#eval (2 : Int) - 5Run lean Scratch.lean again. The first two answers repeat, followed by these four new lines.
Output:
"Hello, Lean 4!"
7
0
-3The first new line, "Hello, Lean 4!", comes from joining the two pieces of text with ++. Text inside double quotes is a string. The second line is 7, the result of subtracting 3 from 10.
Compare the last two lines. Both subtract 5 from 2, but (2 : Nat) tells Lean to treat 2 as a natural number. Since natural numbers (Nat, ) have no negative values, Lean gives 0 when the subtraction would go below zero. That is truncated subtraction. With (2 : Int), Lean reads the 2 as an integer, which can be negative, and prints -3.4
Optional: Reading the Results in Mathematical Notation
You can go straight to Section 3 if you want to keep working in the file. Here’s how to write the same answers with mathematical symbols.
Write for checking the type of an expression and for evaluating it to a value. The first notation reads “expression has type ,” and the second reads “evaluating produces the value .” The two answers from our first exercise look like this in that notation.
We can summarize the difference between the two subtractions using mathematical notation. Here is the set of all natural numbers , and is the set of all integers including negative numbers ().
The notation reads “ and belong to the set of natural numbers (both are natural numbers),” and the dotted minus sign distinguishes natural-number truncated subtraction from ordinary integer subtraction (). Likewise, picks the larger of and . The subtraction on the right is over the integers, so when is negative, the result is .
For natural numbers with a < b, subtracting and then adding back the same amount no longer gives (a - b) + b = a. For example, (2 - 5) + 5 = 5: the first subtraction stopped at zero, so adding 5 takes you to 5 rather than back to 2. Use Int when a change needs to keep its minus sign, and Nat for counts that should stop at zero.5
3. Binding Names and Types with #check and def
Give 3 a name and you can refer to it later without writing the number again. In the first line, def binds 3 to maxRetry. Add the block to the same Scratch.lean file and run lean Scratch.lean again. The output below lists the five lines added by this block.
def maxRetry : Nat := 3
def serviceName : String := "makonea"
def tempDelta : Int := -4
#check maxRetry
#check serviceName
#check tempDelta
#check (true : Bool)
#eval maxRetry + 2Output:
maxRetry : Nat
serviceName : String
tempDelta : Int
true : Bool
5The first four lines report types from #check. maxRetry : Nat tells you that the named value is a natural number. The next two lines identify a string (serviceName : String) and an integer (tempDelta : Int). In true : Bool, Bool is the type with two possible values: true and false.6
The final 5 comes from #eval maxRetry + 2. Lean takes the 3 stored under maxRetry and adds 2.
In mathematical notation, the two pieces of information (where the colon : declares that the name on the left has the type on the right) and (where the colon-equals := defines the name on the left to equal the value on the right) live together in a single line of code. Because the value now has a name, later expressions can reuse maxRetry instead of repeating the literal 3.7
Optional: A First Equality Proof with by and rfl
So far, we've asked Lean for types and values. Now give it evidence that 1 + 1 = 2 by appending these two lines to the same file. The example command checks a proof without binding a name to it.
example : 1 + 1 = 2 := by
rflThe claim is on the left of :=, and its proof goes on the right. After by, write the steps that build the proof; Lean calls them tactics. The name rfl is short for reflexivity. It closes this equality because 1 + 1 computes to 2, the same term as on the right.
Run the file again and you'll get the earlier output. The new lines check a proof without printing a value. In VS Code, place the cursor before rfl to see the equality as a goal in InfoView, then move past it to see that no goals remain. Part 5 uses that panel to follow proofs with more than one step.
Optional: When Does Lean Write Build Files?
The lean Scratch.lean command we’ve used prints results without leaving a build file beside the source. To save what Lean processed in a form another Lean file can import, use lean -o Scratch.olean Scratch.lean. The name after -o tells Lean where to write the .olean file.
For a project with several files, use the lake build tool. A project can name its Lean version in lean-toolchain, which elan reads when choosing what to run. By default, lake build puts the project’s build files under .lake/build/. We’ll build a project in Part 7.
In Part 2, we’ll use def to name a function that takes an input and calculates a result.
Footnotes
- David Thrane Christiansen. Functional Programming in Lean. Evaluating Expressions. Shows how to evaluate expressions with
#evalwithout defining a function first. ↩ - Leonardo de Moura and Sebastian Ullrich. The Lean 4 Theorem Prover and Programming Language. CADE 2021, §2. Compares the type and value of the same expression using
#checkand#eval. ↩ - Lean Language Reference 4.36.0. Checking Types · Evaluating Terms. The first section covers elaborating a term and displaying its type; the second covers compiling and running a term to display its value. ↩
- Lean Language Reference 4.36.0. Natural Numbers. Explains the subtraction and division rules on natural numbers. ↩
- Lean Language Reference 4.36.0. Type Ascription · Natural Numbers: Arithmetic. Explains how type ascriptions determine the interpretation of numeric literals and how
Nat.subperforms truncated subtraction. ↩ - Lean Language Reference 4.36.0. Interacting with Lean. Explains the syntax for inspecting the type of an expression with
#check. ↩ - Lean Language Reference 4.36.0. Definitions.
defadds a named constant with a body to the environment. ↩