Chapter 4 · Your own data
One symbol, many types
Step 1 of 22 · Concept
Int has negative numbers
You will make + work on your own type, prove facts about it, and write functions for any type.
Nat has no negative numbers (lesson 3). The type Int has them. (3 : Int) is 3 as an Int: the type is
named as in lesson 17's (⟨3, 4⟩ : Point).
| line | value |
|---|---|
(3 : Int) - 5 | -2 |
(3 : Nat) - 5 | 0 |
This course keeps to Nat, so subtraction stops at 0 in its statements.
Step 2 of 22 · Quiz
Predict the Int line
Line 2 prints 0. Type the missing line.
#eval (3 : Int) - 5
#eval (3 : Nat) - 5
Step 3 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 4 of 22 · Concept
+ works on many types through a class
+ adds Nats and Ints, two different types. It works through a class.
A class names an operation without saying how it works: the class Add names +. An instance gives the
operation for one type.
The library has an instance Add Nat and an instance Add Int. For a + b, Lean finds the instance for the type
of a and b.
Step 5 of 22 · Quiz
Why does + work on both?
2 + 3 and (2 : Int) + 3 both work. Why?
Step 6 of 22 · Concept
Give a type of your own an instance
An instance gives the class's operation for your type:
instance : Add Point where
add p q := { x := p.x + q.x, y := p.y + q.y }
add is the operation the class names; here it adds the x fields and the y fields. From then on, p + q
works on points: Lean finds this instance and uses its add.
Step 7 of 22 · Demo
Add two points
#eval prints the sum with the class Repr: deriving Repr (lesson 14) wrote an instance of it for Point.
Press Run. Each field of the sum is the sum of the fields.
Step 8 of 22 · Try it
Try it: fix the instance
Press Run: the sum prints { x := 11, y := 2 }. Make add add the y fields too, so that Run prints
{ x := 11, y := 22 }.
The y field keeps p.y. Make it p.y + q.y, as x is.
Step 9 of 22 · Exercise
Your turn: an instance for pairs
- Write the
Addinstance forPair, as the comment says. - Prove
pair_sum.
Keep the colon: instance : Add Pair where. Without it, Lean says:
error: unexpected token 'where'; expected ':'
add p q builds a new Pair, as the Point instance built a Point: name its fields a and b.
The sum computes, field by field, to ⟨4, 6⟩: rfl sees it.
Step 10 of 22 · Concept
One definition for every type
twice applies a function two times. It works for any type α:
def twice {α : Type} (f : α → α) (x : α) : α := f (f x)
Type α as \a; it is a name like any other, so T would work too. {α : Type} makes α an implicit argument: you never write it, and Lean fills it in from the
other arguments. In twice (fun n => n + 1) 5, α is Nat.
List α is a list of any one type. Lesson 15's List Nat is one of them.
Step 11 of 22 · Quiz
What is α here?
In this call, what does Lean fill in for α?
twice (fun s => s ++ "!") "Hi"
Step 12 of 22 · Demo
See it run
Press Run. The same twice works on a number and on a text.
Step 13 of 22 · Concept
[Add α] asks for an instance
A function for every type cannot use + on its own: not every type has it. [Add α] asks for an Add instance:
def double' {α : Type} [Add α] (x : α) : α := x + x
double' works for every type that has an Add instance, and only for those. Nat and Int have one. String
has none: text is joined with ++.
Step 14 of 22 · Demo
See where it stops
Press Run. Lines 2 and 3 print. Lean refuses line 4:
error(lean.synthInstanceFailed): failed to synthesize instance of type class
Add String
Step 15 of 22 · Exercise
Your turn: add up a list of any type
- Write
totalby cases on the list, as lesson 15'ssum:[]giveszero. - Prove
total_sum: on numbers, with0forzero,totalis lesson 15'ssum.
Keep the list out of the brackets. The signature ends in List α → α, so the lines after it match the list, as in sum. A named (xs : List α) would need a match, which this course does not teach.
The call on the tail needs zero too: total zero xs, as lesson 13's addTo m n kept m. Without it, Lean looks for an instance Add (List α).
total_sum is an induction on xs, as in lesson 16: unfold total and sum, and use ih.
Step 16 of 22 · Concept
x == y is a Bool, x = y is a Prop
Two ways to compare look alike, and differ:
| you write | it is | |
|---|---|---|
x == y | a Bool | computed: true or false |
x = y | a Prop | a proposition, which may need a proof |
#eval 2 == 3 computes false. 2 = 3 is a proposition, like every theorem's statement: #check shows it is a
Prop.
Step 17 of 22 · Demo
See the difference
Press Run. Line 1 prints a computed Bool; line 2 prints a proposition and its type.
Step 18 of 22 · Concept
Decidable: a proposition Lean can compute
The class Decidable says how to compute a Prop to true or false. decide works only through such an instance,
and so does if a = b then ... else ... in a definition (lesson 19).
2 = 3 has one: two given numbers can be compared. deriving DecidableEq (lesson 14) wrote one for Color.
A proposition about every number, with no bound, has none, so decide refuses it. A bounded one has one:
decide proves ∀ n, n < 3 → n + n < 6, checking 0, 1 and 2.
Step 19 of 22 · Demo
See an instance missing
Press Run. Line 1 prints. Lean refuses line 2:
error: failed to synthesize
Decidable (∀ (n : Nat), n + 0 = n)
Step 20 of 22 · Check yourself
Check yourself: facts about your instance
No shape this time.
- Write the
Addinstance forPoint, as the comment says. - Prove
add_zero_rightandadd_x.
Keep p first in each sum: rfl sees p.x + 0 compute to p.x, but not 0 + p.x (lesson 4).
Step 21 of 22 · Challenge · optional
Challenge: zero on the left
Optional.
Prove zero_add_left: adding ⟨0, 0⟩ on the left also gives the point back.
rfl refuses it, and simp makes no progress. The point that + builds has a sum 0 + ... in each field, and 0 + n does not compute (lesson 4).
simp cannot see through + on points. Lesson 10's show can restate the goal with that point written out, as a (⟨..., ...⟩ : Point): work out its two fields from the instance.
After the show, each field is a sum of numbers, and simp knows that 0 + n is n.
Step 22 of 22 · Recap
Recap
Inthas negative numbers:(3 : Int) - 5is-2.- A class names an operation; an instance gives it for one type:
instance : Add Point where. {α : Type}makes a definition work for every type;[Add α]asks for an instance.x == yis aBool,x = yaProp;Decidableis whydecidecan compute aProp.
Next: lesson 19 writes what a function should do as theorems, and proves it.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 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 : Int) - 5
#eval (3 : Nat) - 5
structure Point where
x : Nat
y : Nat
deriving Repr
instance : Add Point where
add p q := { x := p.x + q.x, y := p.y + q.y }
#eval ({ x := 1, y := 2 } : Point) + { x := 10, y := 20 }
structure Point where
x : Nat
y : Nat
deriving Repr
instance : Add Point where
add p q := { x := p.x + q.x, y := p.y }
#eval ({ x := 1, y := 2 } : Point) + { x := 10, y := 20 }
structure Pair where
a : Nat
b : Nat
deriving Repr
-- add the a fields, add the b fields
instance : Add Pair where
add p q := sorry
theorem pair_sum : (⟨1, 2⟩ + ⟨3, 4⟩ : Pair) = ⟨4, 6⟩ := by
sorry
def twice {α : Type} (f : α → α) (x : α) : α := f (f x)
#eval twice (fun n => n + 1) 5
#eval twice (fun s => s ++ "!") "Hi"
def double' {α : Type} [Add α] (x : α) : α := x + x
#eval double' 21
#eval double' (-4 : Int)
#eval double' "ab"
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
-- the elements added with +, starting from zero,
-- for every type α with an Add instance
def total {α : Type} [Add α] (zero : α) : List α → α
:= sorry
theorem total_sum (xs : List Nat) : total 0 xs = sum xs := by
sorry
#eval 2 == 3
#check 2 = 3
#eval if 2 = 3 then "same" else "different"
example : ∀ n : Nat, n + 0 = n := by decide
structure Point where
x : Nat
y : Nat
deriving Repr
-- add the x fields, add the y fields
instance : Add Point where
add p q := sorry
theorem add_zero_right (p : Point) : p + ⟨0, 0⟩ = p := by
sorry
theorem add_x (p q : Point) : (p + q).x = p.x + q.x := by
sorry
structure Point where
x : Nat
y : Nat
deriving Repr
instance : Add Point where
add p q := { x := p.x + q.x, y := p.y + q.y }
theorem zero_add_left (p : Point) :
(⟨0, 0⟩ : Point) + p = p := by
sorry