Start here · Next: Level 2F or Level 2T
At a glance
- You build
- the club's tables in Lean.
- You prove
- your first facts about them, by computing.
| Learn to | Read |
|---|---|
| prove your first equations, one step at a time | Natural Number Game (Tutorial World) |
| write values and functions, with their types: if you like programs | FPIL 1.1 Evaluating Expressions · FPIL 1.3 Functions and Definitions |
| … or the same, if you like proofs | TPIL 2.1 Simple Type Theory · TPIL 2.4 Definitions |
| read a proposition as a type, and prove an equation by computing | TPIL 3.1 Propositions as Types · TPIL 4.2 Equality |
Milestones
Your ticks stay in this browser.
0 of 5 done
Before you startYour own Lean project
Install Lean in three steps
- Install VS Code, from code.visualstudio.com.
- In VS Code, open the Extensions panel, search for Lean 4, and install it.
- Open a new file (File › New Text File). Click the ∀ at the top right, then Documentation… › Show Setup Guide, and follow the guide: it installs Lean for you.
If you prefer to install by hand, see lean-lang.org/install.
Create your own project, once
- Click the ∀, then New Project… › Create Standalone Project…, and give it a name, such as
Club. - Open the project. Inside its folder, create a file
Club.lean: it will grow, level by level. - From Level 7 on, you need Mathlib, Lean's library of mathematics: make a second project with New Project… › Create Project Using Mathlib…. It is a large download: use a good connection.
From a terminal, lake new Club does the same as step 1 (manual installation).
Lean answers as you type
#eval 1 + 1 -- 2
#eval "Hello, " ++ "club!" -- "Hello, club!"
#check 1 + 1 -- 1 + 1 : Nat- Put the cursor on a line: the Infoview, on the right, shows Lean's answer.
- A red underline is an error: hover over it to read it. A yellow one is a warning, such as a
sorrystill to fill in. #evalcomputes a value;#checkshows its type.- The first time Lean opens a file, it can take a few seconds to answer.
Theory, on paperWhat a table is, and how a list stands for one
A table is a finite set of rows
Take two sets, \(D_1\) and \(D_2\): the values allowed in each column.
Definition 1.1 (Row, table)
A row over \(D_1\) and \(D_2\) is a pair \((d_1, d_2)\) with \(d_1 \in D_1\) and \(d_2 \in D_2\). A table over \(D_1\) and \(D_2\) is a finite set of rows: \(R \subseteq D_1 \times D_2\).
The size of \(R\), written \(|R|\), is its number of rows. For example, with \(\mathit{Name}\) and \(\mathit{Dept}\) sets of strings:
For now, every table has two columns. Level 5F allows any number, and gives the columns names. This is the model E. F. Codd proposed in 1970, and every relational database still uses it (A Relational Model of Data for Large Shared Data Banks, CACM 13(6)).
Our club has three tables
| \(\att{name}\) | \(\att{dept}\) |
|---|---|
| Ada | Maths |
| Alan | CS |
| Grace | CS |
| \(\att{name}\) | \(\att{week}\) |
|---|---|
| Ada | 1 |
| Alan | 1 |
| Alan | 2 |
| Grace | 3 |
| \(\att{week}\) | \(\att{topic}\) |
|---|---|
| 1 | Lists |
| 2 | Proofs |
| 3 | Types |
| 4 | Sets |
Here \(\rel{Member} \subseteq \mathit{Name} \times \mathit{Dept}\), \(\rel{Attended} \subseteq \mathit{Name} \times \N\) and \(\rel{Session} \subseteq \N \times \mathit{Topic}\).
- The headers (\(\att{name}\), \(\att{week}\), …) are only there to help us read: for now, a column is known by its position.
- Each table is a set: its rows have no order, and no row appears twice. So the same table can be drawn with its rows in any order.
A list represents a table
A computer stores rows one after another, in a list. So we say exactly which table a list stands for.
Definition 1.2 (Representation)
A list \([t_1, \ldots, t_k]\) represents the table \(\{t_1, \ldots, t_k\}\).
Different lists can represent the same table: \([a, b]\), \([b, a]\) and \([a, b, a]\) all represent \(\{a, b\}\).
Fact 1.3
If a list of length \(k\) represents \(R\), then \(|R| \leq k\), with \(|R| = k\) exactly when no row repeats in the list.
Proof. The rows of \(R\) are \(t_1, \ldots, t_k\), where a row that appears several times in the list is counted once in \(R\). So \(R\) has at most \(k\) rows, and exactly \(k\) when \(t_1, \ldots, t_k\) are all different. ∎
Lean computes with lists; we reason about tables. Definition 1.2 is the bridge between the two, and Level 6, where the two tracks meet, proves that the programs you write respect it.
Questions
Try each one before you open its answer.
Question 1.A Write a table as a set
Write \(\rel{Session}\) as a set of pairs. What is \(|\rel{Session}|\)?
Answer
\(\rel{Session} = \{(1, \text{Lists}), (2, \text{Proofs}), (3, \text{Types}), (4, \text{Sets})\}\), and \(|\rel{Session}| = 4\).
Question 1.B A list with a repeat
Which table does the list \([(\text{Ada}, 1), (\text{Alan}, 1), (\text{Ada}, 1)]\) represent? Compare its size with the length of the list.
Answer
\(\{(\text{Ada}, 1), (\text{Alan}, 1)\}\), of size 2. The list has length 3: the row \((\text{Ada}, 1)\) repeats, so \(|R| < k\), as Fact 1.3 says.
Question 1.C Many lists, one table
Give three different lists that represent \(\rel{Member}\). How many lists of length 3 represent it?
Answer
For example \([(\text{Ada}, \text{Maths}), (\text{Alan}, \text{CS}), (\text{Grace}, \text{CS})]\), the same in reverse, and \([(\text{Ada}, \text{Maths}), (\text{Ada}, \text{Maths}), (\text{Alan}, \text{CS}), (\text{Grace}, \text{CS})]\). A list of length 3 that represents \(\rel{Member}\) must hold its 3 rows once each (Fact 1.3), in some order: there are \(3 \cdot 2 \cdot 1 = 6\) of them.
Question 1.D The empty table
Is \(\emptyset\) a table? Which lists represent it?
Answer
Yes: it is a finite set of rows, with no rows. Only the empty list \([\,]\) represents it, since any row in a list would be a row of its table.
Lean, on your computerThe club's tables, and your first proof
In Lean, a table is a list of pairs
Type this into Club.lean:
def member : List (String × String) :=
[("Ada", "Maths"), ("Alan", "CS"), ("Grace", "CS")]
def attended : List (String × Nat) :=
[("Ada", 1), ("Alan", 1), ("Alan", 2), ("Grace", 3)]
#eval member -- prints the table
#eval attended.length -- 4String × Natis Lean's \(\mathit{Name} \times \N\): the type of pairs.defnames a value and states its type;#evalcomputes it.- No row repeats in these lists, so they represent our tables exactly (Fact 1.3).
- Read more in TPIL 2.1 Simple Type Theory and TPIL 2.4 Definitions, or in FPIL 1.2 Types if you prefer programs.
A first proof, by computing
example : attended.length = 4 := rflexample : P := proofstates the propositionP, and proves it. HerePisattended.length = 4.rflproves that a thing equals itself. Lean accepts it here because both sides compute to the same value: 4.- Try: state
attended.length = 5instead, and read Lean's error. A false statement has no proof.
Read TPIL 3.1 Propositions as Types, then TPIL 4.2 Equality, or the FPIL Interlude: Propositions, Proofs, and Indexing.
Exercises
Exercise 1.1 In Lean Add the Session table
def session : List (Nat × String) :=
sorry -- the four sessions: (1, "Lists"), (2, "Proofs"), ...
example : session.length = 4 := rfl
example : member.length = 3 := rflReplace sorry with the four sessions of the \(\rel{Session}\) table, and #eval session.
Hint
The same shape as attended, with the two columns the other way round: a number, then a string.
Exercise 1.2 In Lean Prove its size
Prove both examples above. Then write and prove one more fact of your own about the tables.
Hint
While session is still sorry, Lean cannot compute its length, so rfl fails. Fill in Exercise 1.1 first.
Some facts to try
member.length + attended.length = 7, or (member ++ member).length = 6: ++ appends two lists.
Exercise 1.3 In Lean A statement with no proof
State member.length = 4. Why can you not prove it?
Answer
member.length computes to 3, and 3 is not 4: rfl fails, and so does every other proof, since the statement is false.
Exercise 1.4 In Lean Predict the type
Before you run each line, write down the type you expect. Then check.
#check ("Ada", 1)
#check member
#check attended.length
#check attended.length = 4Answer
String × Nat, List (String × String), Nat, and Prop: attended.length = 4 is a proposition, and rfl is a proof of it.
Exercise 1.5 In Lean A list with a repeated row
def twice : List (String × Nat) := attended ++ attended
example : twice.length = 8 := rflProve the example. Then, on paper: which table does twice represent, and what is its size?
Answer
twice represents \(\rel{Attended}\) itself, of size 4: every row appears twice in the list, and a table counts each row once (Fact 1.3).
Choose your track, or take both
- Programs first, from Level 2F: build the operators of relational algebra as Lean programs, and run them. It follows Functional Programming in Lean (FPIL), chapter by chapter.
- Proofs first, from Level 2T: prove the laws of relational algebra in Lean, for every table. It follows Theorem Proving in Lean 4 (TPIL), chapter by chapter.
Not sure which?
If you enjoy writing programs, start with 2F. If you enjoy puzzles and logic, start with 2T. Many people alternate: one level of each, in turn. The tracks meet at Level 6, which needs both.
Next
Stuck, or curious? Ask us on Discord, or come along any Thursday.