Chapter 1 · Foundations
Terms, types and functions
Step 1 of 22 · Concept
Every value has a type
By the end of this lesson you define your own functions and prove facts about them.
Every value has a type. Three types to start with:
| type | its values |
|---|---|
Nat | the numbers 0, 1, 2 and up |
Bool | true and false: #eval 3 < 5 shows true |
String | text in quotes, such as "Lean" |
#check e shows a term with its type, as e : T. #check 42 shows 42 : Nat, and #check "Lean" shows
"Lean" : String.
Step 2 of 22 · Concept
Nat has no negative numbers
Nat holds 0, 1, 2 and so on, and no number less than 0:
#eval | prints | why |
|---|---|---|
4 - 10 | 0 | subtraction stops at 0 |
9 / 2 | 4 | / rounds down |
9 % 2 | 1 | % gives the remainder |
Nat has no largest number either. 2 ^ 100 is 2 to the power 100, and #eval prints all 31 of its digits.
Step 3 of 22 · Quiz
Predict the missing line
Lean prints one line for each #eval. Type the missing line.
#eval 3 - 5
#eval 7 / 2
#eval 7 % 2
#eval 2 ^ 100
Step 4 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 5 of 22 · Concept
Text joins with ++
++ joins two texts into one:
#eval | prints |
|---|---|
"Le" ++ "an" | "Lean" |
"Hi, " ++ "Ada" | "Hi, Ada" |
#eval shows text with its quotes, so you can see where it starts and ends. A space inside the quotes is part of the
text.
Step 6 of 22 · Concept
def gives a value a name
def name : T := value gives a value a name for good: after def answer : Nat := 42, answer means 42.
Nothing in Lean is assigned again.
A function also names its parameters, each with its type, then the type of its result:
def double (n : Nat) : Nat := n + n
double takes a Nat called n and gives n + n, also a Nat.
Step 7 of 22 · Try it
Try it: one greeting
Press Run: it prints "Hello". Then join ", Lean" to it, for example with ++, so that it prints
"Hello, Lean".
++ goes between two texts: the "Hello" you have, then ", Lean" in quotes of its own.
Step 8 of 22 · Concept
A call is the name, then the arguments
A call is the function's name, then its arguments, with no brackets: double 3 is 6. A function of two
numbers takes two, one after the other:
def add (a b : Nat) : Nat := a + b
add 3 4 is 7. A call binds tighter than +, and brackets decide what an argument is:
| expression | means | value |
|---|---|---|
double 5 + 1 | (double 5) + 1 | 11 |
double (5 + 1) | double 6 | 12 |
Step 9 of 22 · Quiz
Predict the call
Lean prints one line for each #eval. Type the missing line.
def double (n : Nat) : Nat := n + n
def add (a b : Nat) : Nat := a + b
#eval double 3 + 1
#eval double (3 + 1)
#eval add 10 4
Step 10 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 11 of 22 · Concept
A function's type has an arrow
#check (double), in brackets, shows the type of double itself:
double : Nat → Nat
Read Nat → Nat as "from Nat to Nat". Type → as \to, or as -> in plain text. Without the brackets,
#check double shows its header, double (n : Nat) : Nat.
Parameters of one type may share a bracket, as (a b : Nat) in add. Each parameter adds an arrow:
#check (add) shows add : Nat → Nat → Nat: two Nats in, one Nat out.
Step 12 of 22 · Concept
fun makes a function without a name
fun n => n + 1 is a function with no name: it takes n and gives n + 1. #check shows its type,
fun n => n + 1 : Nat → Nat.
def can give it a name. Then its type goes after the name:
def f : Nat → Nat := fun n => n + 1
f 5 is 6, as it would be for def f (n : Nat) : Nat := n + 1.
Step 13 of 22 · Quiz
What is its type?
What is the type of vol?
def vol (w h d : Nat) : Nat := w * h * d
Step 14 of 22 · Try it
Try it: a function without a name
Press Run: #eval f 5 prints 6. Change f so that f 5 is 50, for example in the body of the fun.
The body comes after =>. Which operation turns 5 into 50?
Step 15 of 22 · Exercise
Your turn: a function of two numbers
- Define
area (w h : Nat) : Nat: the widthwtimes the heighth. - Print
area 3 4with#eval. Run shows12.
#eval area(3, 4) gives two messages:
could not synthesize a `Repr` or `ToString` instance for type
Nat → Nat → Nat
unexpected token '('; expected command
Lean reads #eval area as the whole command and cannot show a function. Then it stops at the (. Write the arguments after the name, with spaces: area 3 4.
Copy the shape of double: def, the name, the parameters (w h : Nat), : Nat, :=, then the body.
def area (w h : Nat) : Nat :=
Step 16 of 22 · Concept
rfl unfolds your definitions
rfl proves facts about your own functions:
theorem double_three : double 3 = 6 := by rfl
To check it, Lean unfolds double 3: it replaces the call by the body of double, with n replaced by 3.
That gives 3 + 3, which computes to 6. Both sides are now 6, so rfl closes the goal.
#reduce double 3 shows where that work ends:
6
Step 17 of 22 · Trace
Watch Lean unfold double 3
Press Next to watch Lean check line 2, one moment at a time.
def double (n : Nat) : Nat := n + n
theorem double_three : double 3 = 6 := by rfl- The goal is
double 3 = 6. Line 1, the definition ofdouble, is next. - Line 1 turned
double 3into3 + 3: its body, withnreplaced by3. Next comes line 2's arithmetic. 3 + 3computed to6. Both sides are6, sorflcloses the goal.
Step 18 of 22 · Quiz
What if double gets text?
What happens when this file runs?
def double (n : Nat) : Nat := n + n
#eval double "two"
Step 19 of 22 · Exercise
Your turn: facts about your functions
double and area are given.
- Prove
area_three_four: replace itssorry. - Write and prove the theorem the last comment gives.
Write the second theorem in the shape of the first: theorem, the name and the statement from the comment, := by, then a tactic on the next line.
Both statements compute once Lean unfolds the definitions, and rfl unfolds them for you.
Unfold from the inside out: double (area 2 5) becomes double 10, then 10 + 10, which is 20.
Step 20 of 22 · Check yourself
Check yourself: triple
No shape this time.
- Define
triple (n : Nat) : Nat, three timesn. - Print
triple 7with#eval. Run shows21. - Write and prove the two theorems the comments give.
Define triple first: a theorem can use a name only after its declaration.
Step 21 of 22 · Challenge · optional
Challenge: the length of a greeting
Optional.
greet "Ada" is "Hello, Ada". Write and prove the theorem the comment gives: that text has 10 characters.
s.length is the number of characters in the text s: "Ada".length is 3. The function after the dot takes the value before it.
The brackets in (greet "Ada").length make the call first. Then .length counts the characters of "Hello, Ada".
Both sides compute to a number, so a tactic of lesson 1 closes the goal: rfl or decide.
Step 22 of 22 · Recap
Recap
- Every value has a type:
Nat,Bool,String;#checkshows it. defnames a value or a function; a call is the name, then its arguments.- A function's type has an arrow,
Nat → Nat;fun n => n + 1has no name. rflunfolds your definitions, then computes.
Next: lesson 4 reads every message Lean gives, and what to fix first.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
#eval 3 - 5
#eval 7 / 2
#eval 7 % 2
#eval 2 ^ 100
def greeting : String := "Hello"
#eval greeting
def double (n : Nat) : Nat := n + n
def add (a b : Nat) : Nat := a + b
#eval double 3 + 1
#eval double (3 + 1)
#eval add 10 4
def f : Nat → Nat := fun n => n + 1
#eval f 5
-- def area (w h : Nat) : Nat, w times h
-- then print area 3 4 with #eval
def double (n : Nat) : Nat := n + n
def area (w h : Nat) : Nat := w * h
theorem area_three_four : area 3 4 = 12 := by
sorry
-- double_area : double (area 2 5) = 20
-- def triple (n : Nat) : Nat, three times n
-- print triple 7 with #eval
-- triple_seven : triple 7 = 21
-- triple_five : triple (2 + 3) = 15
def greet (name : String) : String := "Hello, " ++ name
-- greet_length : (greet "Ada").length = 10