We welcome your challenges!
Got a program you wish were proved correct? A theorem you would love to see Lean check?
Pitch it to us: it could be another challenge for the club!
Have a go at the challenges you find intriguing.
Tell us on Discord, or come along any Thursday (papc-rhul.github.io).
Our challenge: teach Lean to answer questions about tables
Here are two tables of our club: who is a member, in which department; and who came to which weekly session.
| \(\att{name}\) | \(\att{dept}\) |
|---|---|
| Ada | Maths |
| Alan | CS |
| Grace | CS |
| \(\att{name}\) | \(\att{week}\) |
|---|---|
| Ada | 1 |
| Alan | 1 |
| Alan | 2 |
| Grace | 3 |
Which CS members came in week 1? Alan.
- To answer it, we keep some rows, keep some columns, and match two tables. This is relational algebra, the maths behind every relational database.
- The challenge is to write it in Lean, and prove, by hand, that it always gives the right answer.
In the language you will learn, the question is \(\proj{\att{name}}(\sel{\att{dept} = \val{CS}}(\rel{Member}) \join \sel{\att{week} = 1}(\rel{Attended}))\). Do not worry if it means nothing to you yet: each symbol in it gets a level of its own, and Level 1 starts from what a table is.
Why prove it? Databases rewrite your questions
- To answer fast, a database rewrites your question into a quicker one, which must have the same answer.
- Each rewrite follows a law, such as "filter first, then combine". Some laws hold only under a side condition.
- Get one wrong, and the database silently answers wrongly. Testing tools have found hundreds of such bugs in mature databases (SQLancer; M. Rigger and Z. Su, ESEC/FSE 2020).
A law can be proved. In this challenge, you prove them.
Start at Level 1, then pick a track, or both
Everyone starts at Level 1. Then come two tracks, which you can take in either order, or side by side:
- Programs first follows Functional Programming in Lean (FPIL), chapter by chapter: you build the operators of relational algebra as Lean programs, and run them on the club's tables.
- Proofs first follows Theorem Proving in Lean 4 (TPIL), chapter by chapter: you prove the laws of relational algebra in Lean, for every table.
The tracks meet at Level 6, which needs both: there, you prove that your programs compute exactly what the laws are about.
What you will have built
- Your first proof: Lean checks a fact about the club's tables (Level 1).
- A query engine on lists: you program selection, projection, product, union, difference and joins, and run them on the club's tables (Levels 2F to 4F).
- Laws for every table: you prove laws 1 to 5 once, for all tables; law 5 needs a side condition (Levels 2T to 4T).
- A query language: queries become terms, and
evalanswers them (Level 5F). With induction, you prove facts about every list (Level 5T). - Programs proved correct: your programs compute the operators, so every law holds for them (Level 6).
- Laws for every query: each side condition becomes a precise hypothesis about attribute names (Level 7).
- A verified optimiser: it rewrites queries and never changes an answer (Level 8).
- A verified fast join: it computes the same table as the definition, much faster (Level 9).
How to start, whenever you like
- Play the Natural Number Game, in your browser: start with its Tutorial World.
- Open Level 1: it shows how to install Lean, what a table is, and how to write your first proof.
- Then pick a track, or both.
Stuck, or curious? Ask us on Discord, or come along any Thursday.
Your progress
Tick the milestones on each level page as you go: the map above shows how far you are. Your ticks are saved in this browser only.
References
- Laws at a glance
- The books are Functional Programming in Lean (FPIL) and Theorem Proving in Lean 4 (TPIL), and Mathematics in Lean (MIL) from Level 7 on.
- The theory of databases is in S. Abiteboul, R. Hull, V. Vianu. Foundations of Databases.