Chapter 4 · Your own data
Lists and recursion
Step 1 of 22 · Concept
A list holds values of one type
In this lesson you write functions on lists, and prove facts about given lists.
A list holds values of one type, in order. List Nat is the type of lists of numbers:
| list | what it holds |
|---|---|
[1, 2, 3] | the numbers 1, 2 and 3, in that order |
[5] | one number |
[] | nothing: the empty list |
#eval [1, 2, 3] prints [1, 2, 3].
Step 2 of 22 · Concept
A list is built from [] and ::
x :: xs puts the number x in front of the list xs. So [1, 2, 3] is short for 1 :: 2 :: 3 :: [].
A list has two constructors, as lesson 14's types had: [], and ::. The second one holds a smaller list, the
tail; the number in front is the head.
Step 3 of 22 · Quiz
What is 0 :: [1, 2]?
Which list is 0 :: [1, 2]?
Step 4 of 22 · Concept
Lists come with functions
The library has functions on lists:
| you write | it gives | for [1, 2] and [3] |
|---|---|---|
xs ++ ys | xs, then ys | [1, 2] ++ [3] is [1, 2, 3] |
xs.length | how many values | [1, 2].length is 2 |
xs.reverse | the values in the other order | [1, 2].reverse is [2, 1] |
xs.length is a call after a dot: the library's List.length, given xs. xs.reverse is List.reverse, the
same way.
Step 5 of 22 · Quiz
Which fact does rfl prove?
Which of these does rfl prove?
Step 6 of 22 · Demo
See it run
Press Run. Each line prints the list or number it computes.
Step 7 of 22 · Try it
Try it: put one in front
front x xs should put x in front of xs, but it returns xs unchanged. Lean warns about the unused x, then
refuses the proof of len_front:
warning: Variable name `x` is not explicitly referenced.
error: Tactic `rfl` failed: The left-hand side
(front x xs).length
is not definitionally equal to the right-hand side
xs.length + 1
Press Run and read the messages. Then fix front with ::, so that rfl proves len_front. Leave the proof
as it is.
After :=, write x :: xs in place of xs. The length of x :: xs computes to xs.length + 1.
Step 8 of 22 · Concept
A function on lists by pattern matching
sum adds the numbers of a list. It has one line per constructor:
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
List Nat → Nat takes a list and gives a number; type → as \to or ->. The empty list sums to 0. A list
x :: xs sums to its head plus the sum of its tail.
sum xs calls sum on the tail, which is shorter. So the calls end, and Lean sees that, as in lesson 12.
Step 9 of 22 · Quiz
What is sum [5, 6]?
Type what this file prints.
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
#eval sum [5, 6]
Step 10 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 11 of 22 · Trace
Watch sum take a list apart
Lean computes sum [5, 6] one equation at a time. Press Next to apply the next one.
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
#eval sum [5, 6][5, 6]is5 :: [6], so line 3 fits, withxas5.sum [6]is next: line 3 again, withxas6andxsas[].sum []fits line 2, which gives0.- No
sumis left. Line 4 prints the sum of the numbers. - The sum is
11, the value#evalprints.
Step 12 of 22 · Concept
_ marks a value you do not use
len counts a list's numbers:
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
len never uses the head, so its line says _ in place of a name. Named x, the head would give lesson 4's
warning, as in the step Try it: put one in front. Each step adds 1 and calls len on the tail.
Step 13 of 22 · Demo
Count a list
Press Run. len [5, 6] adds 1 for each of the two numbers, and simp [len] proves the example by unfolding
len.
Step 14 of 22 · Exercise
Your turn: double every number
- Finish
doubleAll: every number of the list times 2. Its[]line is given. - Add
#eval doubleAll [1, 2, 3], so that Run prints[2, 4, 6].
++ joins two lists, so 2 * x ++ doubleAll xs is refused: 2 * x is a number. Lean says:
error(lean.synthInstanceFailed): failed to synthesize instance of type class
HAppend Nat (List Nat) (List Nat)
The result is a list: the doubled head in front of the doubled tail. :: puts a number in front of a list.
| x :: xs => 2 * x :: ...
Step 15 of 22 · Concept
A function can match two lists
app joins two lists, as ++ does. Each line matches both lists:
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
The first list is taken apart; the second, ys, is carried along unchanged. Joining [] to ys gives ys.
Joining x :: xs to ys puts x in front of app xs ys.
Step 16 of 22 · Try it
Try it: keep the head
Press Run: app [1, 2] [3] prints [3], with the warning Variable name `x` is not explicitly referenced.
Put x back in front, so that Run prints [1, 2, 3].
The second line drops the head. Write x :: before app xs ys.
Step 17 of 22 · Concept
A fact about a given list computes
A fact about a given list, such as len [5, 6] = 2, computes. So rfl or decide proves it: Lean unfolds len,
as it unfolded double in lesson 3.
simp can do it too, if you name the definition. simp [len] unfolds len while it simplifies:
theorem len_two : len [5, 6] = 2 := by
simp [len]
Plain simp does not know your len, and says `simp` made no progress (lesson 11). Lesson 16 needs
simp [len].
Step 18 of 22 · Exercise
Your turn: three facts about given lists
len, sum and app are given. Prove sum_three and app_one, about given lists, and len_app_two, which
holds for every list ys.
sum_three and app_one compute. len_app_two does not: ys is unknown, and rfl refuses it. Plain simp says `simp` made no progress: it does not unfold your own functions.
simp [len, app] unfolds both functions as far as they go: it leaves 1 + (1 + len ys) = 2 + len ys, which is arithmetic (lesson 11).
theorem sum_three : sum [1, 2, 3] = 6 := by
rfl
Step 19 of 22 · Quiz
What does simp [len] use?
simp [len] closes len [5, 6] = 2. Which facts does it use?
Step 20 of 22 · Check yourself
Check yourself: squares
No shape this time.
len and sum are given.
- Define
squares, as the comment says. - Show
squares [1, 2, 3]with#eval: Run prints[1, 4, 9]. - Prove
len_squaresandsum_squares, about a given list, andlen_squares_cons, about every list.
squares has doubleAll's shape. Two facts compute; for the one about every list, let simp unfold.
Step 21 of 22 · Challenge · optional
Challenge: the last number
Optional.
Define lastOr d xs: the last number of xs, or d when xs is []. Then prove last_six and last_none.
A list's last number is its head only when the tail is empty. A pattern can say exactly that: [x] matches a list of one number.
Lean tries the lines from the top and takes the first that fits. Put | _ :: xs => ... before | [x] => ..., and _ :: xs already matches [x]:
error(lean.redundantMatchAlt): Redundant alternative: Any expression matching
[x]
will match one of the preceding alternatives
Three lines: [] gives d, [x] gives x, and any longer list keeps looking in its tail.
Step 22 of 22 · Recap
Recap
- A list is
[]orx :: xs:[1, 2, 3]is1 :: 2 :: 3 :: []. - A function on lists has a line for
[]and one forx :: xs, and calls itself on the tail. _marks a value a line does not use.- A fact about a given list computes:
rfl,decide, orsimp [len].
Next: lesson 16 proves facts about every list, by induction.
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 [1, 2] ++ [3, 4]
#eval [1, 2, 3].length
#eval [1, 2, 3].reverse
def front (x : Nat) (xs : List Nat) : List Nat := xs
theorem len_front (x : Nat) (xs : List Nat) :
(front x xs).length = xs.length + 1 := by
rfl
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
#eval sum [5, 6]
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
#eval len [5, 6]
example : len [5, 6, 7] = 3 := by
simp [len]
-- doubleAll: every number of the list times 2
-- then show doubleAll [1, 2, 3] with #eval
def doubleAll : List Nat → List Nat
| [] => []
| x :: xs => sorry
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => app xs ys
#eval app [1, 2] [3]
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
theorem sum_three : sum [1, 2, 3] = 6 := by
sorry
theorem app_one : app [1] [2, 3] = [1, 2, 3] := by
sorry
theorem len_app_two (ys : List Nat) :
len (app [1, 2] ys) = 2 + len ys := by
sorry
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
-- every number of the list times itself
def squares : List Nat → List Nat := sorry
-- show squares [1, 2, 3] with #eval
theorem len_squares : len (squares [1, 2, 3]) = 3 := by
sorry
theorem sum_squares : sum (squares [1, 2, 3]) = 14 := by
sorry
theorem len_squares_cons (x : Nat) (xs : List Nat) :
len (squares (x :: xs)) = 1 + len (squares xs) := by
sorry
-- the last number of xs, or d when xs is []
def lastOr (d : Nat) : List Nat → Nat := sorry
theorem last_six : lastOr 0 [4, 5, 6] = 6 := by
sorry
theorem last_none : lastOr 7 [] = 7 := by
sorry