Stage 10 · Step 47 of 50

Swap a pair and swap back.

Core Lean8 minutesLean 4.36.0

01The idea

Imagine a card with a name on the front and a score on the back. Swap the two fields twice.

Typo · Your guide for this idea
Typo pointing

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

Swap the two fields, swap again, and each field is homeTop: three pair cards. The first, p, holds fst✝ on the left and snd✝ on the right and has type α × β. After .swap the card p.swap holds snd✝ on the left and fst✝ on the right and has type β × α. After a second .swap the card p.swap.swap holds fst✝ and snd✝ in their original places, type α × β again. Bottom: after cases p the goal is (fst✝, snd✝).swap.swap = (fst✝, snd✝), and rfl leaves no goals.pp.swapp.swap.swapfst✝snd✝α × β.swapsnd✝fst✝β × α.swapfst✝snd✝α × βAFTER cases p⊢ (fst✝, snd✝).swap.swap = (fst✝, snd✝)rflno goals

Swipe sideways to see the whole picture.

A pair is a card with two fields, possibly of different types. 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.

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

After line 1
α : 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

Captured from Lean 4.36.0
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

    Swap a natural number paired with a Boolean.

  2. A little stretch

    Show that swapping three times agrees with swapping once.

Exercises/E47.leanTry it live (opens official Lean editor)
example (x : Nat) (y : Bool) : (x, y).swap = (y, x) := by
  sorry

example (p : α × β) : (p.swap.swap.swap) = p.swap := by
  sorry

08A quick check

1. What is the type after one swap of α × β?

Read the answer and explanation

β × α. Each entry keeps its own type as the entries trade places.

2. What does cases p expose?

Read the answer and explanation

The two fields of one pair. A pair has one constructor, with a first and second component.

One more idea in your toolkit.

Take your time. Progress is yours to mark.