Stage 07 · Step 33 of 50

Keep related data together.

Core Lean12 minutesLean 4.36.0

01The idea

A coordinate card keeps an x-value beside a y-value. A structure packages named fields in the same way.

Lemmi · Your guide for this idea
Lemmi pointing

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

A structure is a card with named slotsLeft: a Point card with two named slots, x holding 2 and y holding 3, both of type Int. The call moveRight of the point 2, 3 with 4 steps builds a new Point card on the right. Its x slot is computed by x := p.x + steps, which is 2 + 4 = 6. Its y slot is not mentioned in the update, so it is copied unchanged: 3. Lean's #eval prints x := 6, y := 3. Bottom: because y is only copied, the goal (moveRight p 0).y = p.y holds for every point p, and rfl proves it.moveRight ⟨2, 3⟩ 4{ p with x := p.x + steps }Pointx: Int2y: Int3Pointx: Int6y: Int3the point ⟨2, 3⟩#eval shows{ x := 6, y := 3 }x := p.x + steps2 + 4 = 6y is not mentioned, so it is copied⊢ (moveRight p 0).y = p.yrfl: y never changes

Swipe sideways to see the whole picture.

A 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.

E33.leanTry it live (opens official Lean editor)
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.

After line 11
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

Captured from Lean 4.36.0
{ x := 6, y := 3 }
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Construct the supplied point record and verify its x-coordinate.

  2. A little stretch

    Prove that the supplied horizontal shift preserves y.

Exercises/E33.leanTry it live (opens official Lean editor)
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

08A quick check

1. What does a structure describe?

Read the answer and explanation

A collection of named fields. A structure constructor packages values for its declared fields.

2. Does moveRight change the y-field?

Read the answer and explanation

No. The record update supplies a new x-field and keeps the remaining field.

One more idea in your toolkit.

Take your time. Progress is yours to mark.