Stage 10 · Step 47 of 50
Swap a pair and swap back.
01The idea
Imagine a card with a name on the front and a score on the back. Swap the two fields twice.
Even if the fields store different kinds of data, both end up where they started.
02In maths
For every pair p, swapping its two entries twice gives p.
Picture it
Swipe sideways to see the whole picture.
cases p opens the card so Lean can see both fields. One .swap trades their places and even flips the type to β × α. A second swap trades them back, so rfl can check by computing.03In Lean
This code comes directly from the file checked by Lean.
example (p : α × β) : p.swap.swap = p := by
cases p
rfl
α × β is a pair type. The first entry has type α, and the second has type β. A single .swap returns a pair of type β × α.
With the two components exposed by cases p, Lean can compute where each goes. After two swaps the fields match the original pair.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
α : Type u_1 β : Type u_2 p : α × β ⊢ p.swap.swap = p
After line 1: example (p : α × β) : p.swap.swap = p := by
α : Type u_1 β : Type u_2 p : α × β ⊢ p.swap.swap = p
After line 2: cases p
case mk α : Type u_1 β : Type u_2 fst✝ : α snd✝ : β ⊢ (fst✝, snd✝).swap.swap = (fst✝, snd✝)
After line 3: rfl
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
example : (1, 2).swap = (1, 2) := by
rfl
Tactic `rfl` failed: The left-hand side (1, 2).swap is not definitionally equal to the right-hand side (1, 2) ⊢ (1, 2).swap = (1, 2)
One swap reverses the entries of this pair, and the entries are different.
The fix: Compare a single swap with the reversed pair, or swap twice to recover the original.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Swap a natural number paired with a Boolean.
Show a hint
The output has a Boolean first and a natural number second.
One more hint
Lean can compute the fields of an explicitly constructed pair.
- A little stretch
Show that swapping three times agrees with swapping once.
Show a hint
Break p into its two fields.
One more hint
After exposing the pair, both sides compute to the same ordered pair.
example (x : Nat) (y : Bool) : (x, y).swap = (y, x) := by
sorry
example (p : α × β) : (p.swap.swap.swap) = p.swap := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
example (x : Nat) (y : Bool) : (x, y).swap = (y, x) := by
rfl
example (p : α × β) : (p.swap.swap.swap) = p.swap := by
cases p
rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.