The Proofs and Programs Club @ RHUL

Everyone starts here

Level 1 Tables, and Your First Proof

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.
What to learn first, and where
Learn toRead
prove your first equations, one step at a timeNatural Number Game (Tutorial World)
write values and functions, with their types: if you like programsFPIL 1.1 Evaluating Expressions · FPIL 1.3 Functions and Definitions
… or the same, if you like proofsTPIL 2.1 Simple Type Theory · TPIL 2.4 Definitions
read a proposition as a type, and prove an equation by computingTPIL 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

  1. Install VS Code, from code.visualstudio.com.
  2. In VS Code, open the Extensions panel, search for Lean 4, and install it.
  3. 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

  1. Click the ∀, then New Project… › Create Standalone Project…, and give it a name, such as Club.
  2. Open the project. Inside its folder, create a file Club.lean: it will grow, level by level.
  3. 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

Leanhello.lean
#eval 1 + 1                  -- 2
#eval "Hello, " ++ "club!"   -- "Hello, club!"
#check 1 + 1                 -- 1 + 1 : Nat

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:

\[\rel{Member} = \{(\text{Ada}, \text{Maths}),\ (\text{Alan}, \text{CS}),\ (\text{Grace}, \text{CS})\} \subseteq \mathit{Name} \times \mathit{Dept}, \qquad |\rel{Member}| = 3.\]

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

\(\rel{Member}\)
\(\att{name}\)\(\att{dept}\)
AdaMaths
AlanCS
GraceCS
\(\rel{Attended}\)
\(\att{name}\)\(\att{week}\)
Ada1
Alan1
Alan2
Grace3
\(\rel{Session}\)
\(\att{week}\)\(\att{topic}\)
1Lists
2Proofs
3Types
4Sets

Here \(\rel{Member} \subseteq \mathit{Name} \times \mathit{Dept}\), \(\rel{Attended} \subseteq \mathit{Name} \times \N\) and \(\rel{Session} \subseteq \N \times \mathit{Topic}\).

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:

Leantables.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   -- 4

A first proof, by computing

Leanproof.lean
example : attended.length = 4 := rfl

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

Leansession.lean
def session : List (Nat × String) :=
  sorry   -- the four sessions: (1, "Lists"), (2, "Proofs"), ...

example : session.length = 4 := rfl
example : member.length = 3 := rfl

Replace 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.

Lean
#check ("Ada", 1)
#check member
#check attended.length
#check attended.length = 4
Answer

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

Lean
def twice : List (String × Nat) := attended ++ attended

example : twice.length = 8 := rfl

Prove 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

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

  1. Level 2F Selection
  2. Level 2T Selection and union

Stuck, or curious? Ask us on Discord, or come along any Thursday.