Stage 10 · Step 46 of 50

Flip a switch twice.

Core Lean6 minutesLean 4.36.0

01The idea

A light switch starts either on or off. Flip it twice and it returns to where it started.

Typo · Your guide for this idea
Typo pointing

Lean can inspect both possible starting positions.

02In maths

For every Boolean b, negating b twice returns b.

Picture it

Check both switch positions: two flips bring each one backLeft: the goal card with b : Bool and goal (!!b) = b. The tactic cases b splits it into two cases. Case true: a switch that is on is flipped by ! to off, then by ! again back to on, and rfl checks it. Case false: a switch that is off is flipped to on, then back to off, and rfl checks it. The combinator <;> runs rfl in both cases.STARTb : Bool⊢ (!!b) = bcases bcase truetrue!false!truerflcase falsefalse!true!falserfl<;> rfl runs rfl in both cases

Swipe sideways to see the whole picture.

A Bool is a switch with only two positions, true and false. cases b says “just try both”. In each case Lean can compute the two flips itself, so rfl sees that the switch is back where it started. <;> sends the same rfl to both cases at once.

03In Lean

This code comes directly from the file checked by Lean.

E46.leanTry it live (opens official Lean editor)
example (b : Bool) : (!(!b)) = b := by
  cases b <;> rfl

! flips a Boolean value. With an unknown input, a good first question is which values that input could have.

cases b gives one goal for each switch position. In each goal the two flips compute, so Lean can check the equality using rfl.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 1
b : Bool
⊢ (!!b) = b
After line 1: example (b : Bool) : (!(!b)) = b := by
b : Bool
⊢ (!!b) = b
After line 2: cases b <;> 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

    Calculate the result of flipping true once.

  2. A little stretch

    Show that three flips have the same effect as one flip.

Exercises/E46.leanTry it live (opens official Lean editor)
example : (!true) = false := by
  sorry

example (b : Bool) : (!(!(!b))) = (!b) := by
  sorry

08A quick check

1. How many starting states must the proof handle?

Read the answer and explanation

Two. A Bool is either false or true.

2. What does <;> rfl do here?

Read the answer and explanation

It applies rfl to every goal from cases. Both concrete Boolean goals can be closed by computation.

One more idea in your toolkit.

Take your time. Progress is yours to mark.