Stage 07 · Step 33 of 50
Keep related data together.
01The idea
A coordinate card keeps an x-value beside a y-value. A structure packages named fields in the same way.
Functions can read those fields or build an updated record.
02In maths
A point contains two integer coordinates. A horizontal move adds its step value to x and preserves y.
Picture it
Swipe sideways to see the whole picture.
Point is like a card with two named slots, x and y. moveRight builds a new card: it fills in a new x and copies every slot it does not mention. That is why (moveRight p 0).y = p.y holds for every point, and rfl can see it straight from the definition.03In Lean
This code comes directly from the file checked by Lean.
structure Point where
x : Int
y : Int
deriving Repr
def moveRight (p : Point) (steps : Int) : Point :=
{ p with x := p.x + steps }
#eval moveRight ⟨2, 3⟩ 4
example (p : Point) : (moveRight p 0).y = p.y := by
rfl
A structure defines a record type with named fields. This Point record has an integer x-field and an integer y-field.
Dot notation projects a field out of a record. The record update syntax keeps fields you do not replace.
The Int type contains the integers, including negative values. The example proves that a zero-step move preserves y for every point.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
p : Point ⊢ (moveRight p 0).y = p.y
After line 11: example (p : Point) : (moveRight p 0).y = p.y := by
p : Point ⊢ (moveRight p 0).y = p.y
After line 12: rfl
no goals
05Lean says
{ x := 6, y := 3 }
no goalsA goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
structure BadPoint where
x : Int
y : Int
def badOrigin : BadPoint := { x := 0 }
Fields missing: `y` Hint: Add missing fields: ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲y̲ ̲:̲=̲ ̲_̲
The record is missing its y-field.
The fix: Supply a value for every required field.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Construct the supplied point record and verify its x-coordinate.
Show a hint
Read the first field from the constructor.
One more hint
The field projection computes directly.
- A little stretch
Prove that the supplied horizontal shift preserves y.
Show a hint
The shift builds a record with the original y-coordinate.
One more hint
Unfolding that projection is definitional equality.
structure Spot where
x : Int
y : Int
example : (Spot.mk 2 3).x = 2 := by
sorry
structure Spot2 where
x : Int
y : Int
def shift (p : Spot2) : Spot2 := ⟨p.x + 1, p.y⟩
example (p : Spot2) : (shift p).y = p.y := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
structure Spot where
x : Int
y : Int
example : (Spot.mk 2 3).x = 2 := by
rfl
structure Spot2 where
x : Int
y : Int
def shift (p : Spot2) : Spot2 := ⟨p.x + 1, p.y⟩
example (p : Spot2) : (shift p).y = p.y := by
rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.