Chapter 4 · Your own data
Structures
Step 1 of 22 · Concept
A structure groups named values
In this lesson you group values into one, prove facts about them, and put a proof inside data.
structure Point where
x : Nat
y : Nat
deriving Repr
A structure is a type whose values hold named fields. Every Point has a number x and a number y.
{ x := 1, y := 2 } is a point, each field named.
Step 2 of 22 · Concept
⟨1, 2⟩ builds a point when the type is known
⟨1, 2⟩ gives the fields in their order, x then y, as lesson 6's ⟨hp, hq⟩ gave two proofs. Type ⟨ as \<
and ⟩ as \>; they have no ASCII form.
Lean must know which type ⟨1, 2⟩ builds. After #eval alone it does not, and refuses:
error: Invalid `⟨...⟩` notation: The expected type of this term could not be determined
(⟨3, 4⟩ : Point) names the type. Where a definition says : Point, the type is known already.
Step 3 of 22 · Concept
p.x reads a field
p.x is the field x of the point p, and p.y its field y:
| term | its value |
|---|---|
(⟨3, 4⟩ : Point).x | 3 |
(⟨3, 4⟩ : Point).y | 4 |
Reading a field computes, so rfl sees it: (⟨3, 4⟩ : Point).x = 3 holds by rfl.
Step 4 of 22 · Quiz
Predict a field
Line 6 prints { x := 1, y := 2 }. Type the missing line.
structure Point where
x : Nat
y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
#eval p
#eval (⟨3, 4⟩ : Point).x
Step 5 of 22 · Demo
See it run
Press Run and compare with your answer. A point prints with its fields named.
Step 6 of 22 · Concept
with makes a new value with one field changed
{ p with x := 5 } is a new point: the fields of p, but x is 5.
def p : Point := { x := 1, y := 2 }
def q : Point := { p with x := 5 }
q is { x := 5, y := 2 }, and p is still { x := 1, y := 2 }. Nothing in Lean is assigned: a def names its
value for good (lesson 3), so with cannot change p. It builds another point.
Step 7 of 22 · Try it
Try it: change the other field
Press Run: q prints { x := 5, y := 2 }. Make q change y to 9 instead, so that Run prints
{ x := 1, y := 9 }.
After with, name the field to change and its new value: y := 9.
Step 8 of 22 · Quiz
What is p.x after with?
What is p.x in this file?
structure Point where
x : Nat
y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
def q : Point := { p with x := 5 }
Step 9 of 22 · Concept
A function in the structure's name
A function named after the structure can be called after a dot:
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
p.swap calls Point.swap with p, as xs.length called List.length in lesson 15. The new point's x is
p.y, and its y is p.x.
Step 10 of 22 · Demo
See swap
Press Run. p.swap is a new point with the two fields exchanged.
Step 11 of 22 · Concept
p.swap.swap = p by rfl
Swapping twice gives the point back, for every p, and rfl proves it:
theorem swap_swap (p : Point) : p.swap.swap = p := by
rfl
Unfolding swap twice gives { x := p.x, y := p.y }, the point built from the fields of p. Lean counts a
structure equal to the point built from its own fields. So both sides are the same.
Step 12 of 22 · Demo
See it proved
Press Run: Lean accepts swap_swap and prints nothing.
Step 13 of 22 · Concept
cases p splits a point into its fields
A structure has one constructor, mk: Point.mk 1 2 is ⟨1, 2⟩. So cases p with | mk a b leaves one goal,
case mk, where p is { x := a, y := b }.
In swap_sum, simp [Point.swap] then leaves:
case mk
a b : Nat
⊢ b + a = a + b
The goal names the fields, a and b. Here simp [Point.swap] and omega would manage without cases.
cases is the way to see a point's fields in the goal panel.
Step 14 of 22 · Demo
See cases at work
Press Run: Lean accepts swap_sum. Put the cursor on simp and read the goal before and after it.
Step 15 of 22 · Exercise
Your turn: move a point
- Define
shift p d, the pointdsteps further right, withwith, as the comment says. Any definition that gives this point passes. - Prove
shift_xandshift_zero.
Mind the order in the sum. With d + p.x, rfl refuses both facts: 3 + p.x and 0 + p.x do not compute (lesson 4), while p.x + 3 and p.x + 0 do.
With the sum in that order, both facts compute once shift is unfolded.
def shift (p : Point) (d : Nat) :
Point := { p with x := ... }
Step 16 of 22 · Exercise
Your turn: adding points either way
Point.add adds two points field by field. Prove add_comm': the order does not matter.
rfl refuses: p.x + q.x and q.x + p.x do not compute to the same term. Nat.add_comm (lesson 9) turns a sum around.
To see the fields in the goal panel, you can split both points: cases p with | mk a b, then, inside it, the same for q. simp also works without it.
Give simp the definition to unfold, Point.add, and the library fact that turns a sum around.
Step 17 of 22 · Concept
A field can be a proof
A field's type can be a proposition. Then the field holds a proof:
structure Pos where
n : Nat
pos : 0 < n
To make a Pos, give a number and a proof that it is positive: ⟨3, by decide⟩. A proof is a term (lesson 2), so
it can sit in a field. Every Pos is positive, because no value can be built without that proof.
Step 18 of 22 · Quiz
Can a Pos be zero?
What does Lean say about this line?
def zero : Pos := ⟨0, by decide⟩
Step 19 of 22 · Demo
See a Pos printed
Press Run. A proof prints as _: what matters about it is that it exists.
Step 20 of 22 · Check yourself
Check yourself: rectangles
No shape this time.
- Define
Rect.areaandRect.turnas the comments say. - Show the area of a 3 by 4 rectangle with
#eval: Run prints12. - Prove
turn_areaandturn_turn.
turn_turn follows swap_swap. For turn_area, omega cannot turn a product around: give simp the library fact that does (lesson 9).
Step 21 of 22 · Challenge · optional
Challenge: a point on the diagonal
Optional.
A Diag holds a proof that its two fields are equal. Prove diag_sum.
omega alone fails: to it, d.x and d.y are two unknown numbers. The fact that they are equal is in d itself, in its field same.
d.same reads the proof field, as d.x reads a number. It is a proof of d.x = d.y.
Put it among the hypotheses with lesson 10's have h : d.x = d.y := .... Then the arithmetic is omega's.
Step 22 of 22 · Recap
Recap
- A structure has named fields:
{ x := 1, y := 2 }, or⟨1, 2⟩where the type is known. { p with x := 5 }builds a new value;pkeeps its fields.Point.swapis called asp.swap;cases p with | mk a bshows the fields.- A field can be a proof,
pos : 0 < n: every value keeps the promise.
Next: lesson 18 makes + work on points.
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.
structure Point where
x : Nat
y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
#eval p
#eval (⟨3, 4⟩ : Point).x
structure Point where
x : Nat
y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
def q : Point := { p with x := 5 }
#eval q
structure Point where
x : Nat
y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
def p : Point := { x := 1, y := 2 }
#eval p.swap
structure Point where
x : Nat
y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
theorem swap_swap (p : Point) : p.swap.swap = p := by
rfl
structure Point where
x : Nat
y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
theorem swap_sum (p : Point) :
p.swap.x + p.swap.y = p.x + p.y := by
cases p with
| mk a b =>
simp [Point.swap]
omega
structure Point where
x : Nat
y : Nat
deriving Repr
-- the point d steps further right: x + d
def shift (p : Point) (d : Nat) : Point := sorry
theorem shift_x (p : Point) : (shift p 3).x = p.x + 3 := by
sorry
theorem shift_zero (p : Point) : shift p 0 = p := by
sorry
structure Point where
x : Nat
y : Nat
deriving Repr
def Point.add (p q : Point) : Point :=
{ x := p.x + q.x, y := p.y + q.y }
theorem add_comm' (p q : Point) : p.add q = q.add p := by
sorry
structure Pos where
n : Nat
pos : 0 < n
deriving Repr
def three : Pos := ⟨3, by decide⟩
#eval three
structure Rect where
w : Nat
h : Nat
deriving Repr
-- w times h
def Rect.area (r : Rect) : Nat := sorry
-- w and h swapped
def Rect.turn (r : Rect) : Rect := sorry
-- show the area of a 3 by 4 rectangle with #eval
theorem turn_area (r : Rect) : r.turn.area = r.area := by
sorry
theorem turn_turn (r : Rect) : r.turn.turn = r := by
sorry
structure Diag where
x : Nat
y : Nat
same : x = y
def two : Diag := ⟨2, 2, by rfl⟩
theorem diag_sum (d : Diag) : d.x + d.y = 2 * d.x := by
sorry