Prove every case

Split a value of your own type into cases; close impossible ones with contradiction.

16 minBeginnercasesdecidecontradictionone goal per constructor
0/8 completed in this course

A claim about an unknown m : Mode cannot be computed, because Lean does not know which mode m is. But there are only three possibilities. cases m with splits the proof into one goal for each constructor:

example (m : Mode) : advance (advance (advance m)) = m := by
  cases m with
  | off => rfl
  | warm => rfl
  | bright => rfl

In each branch m is replaced by one concrete constructor, so everything computes and rfl closes it. For a claim that is not an equation, such as brightness m ≤ 2, use decide: it works out a claim with no unknowns left and accepts it if it is true.

Sometimes a branch is impossible. If you know h : brightness m = 2, then in the off branch h becomes 0 = 2, which can never hold. contradiction closes a goal whose hypotheses contradict themselves, such as an equation between two different numbers or constructors.

This is how proofs about programs often go: the function was defined case by case, so the proof follows the same cases.

Worked example

example (m : Mode) : brightness m ≤ 2 := by
  cases m with
  | off => decide
  | warm => decide
  | bright => decide
Step through the proof1 / 1

At the start of the proof on line 1. Everything above ⊢ may be used; the line below it is what must be proved.

m:Mode
⊢brightness m ≤ 2

Each branch is a concrete comparison, such as 1 ≤ 2 for warm, which decide settles.

Takeaway

cases gives one goal per constructor. In each one things compute, decide settles concrete claims, and contradiction closes impossible branches.

Practise on Problems
Exercise 1 of 3

Prove that the brightness of the next two modes always adds up to a positive number.

Suggested steps
  1. Split m into its three constructors
  2. Prove the off branch
  3. Prove the warm branch
  4. Prove the bright branch

These marks only recognize text. Another correct proof may use different steps; Lean checks whether it works.

Exercise.lean
1inductive Mode where
2 | off
3 | warm
4 | bright
5
6def advance : Mode → Mode
7 | .off => .warm
8 | .warm => .bright
9 | .bright => .off
10
11def brightness : Mode → Nat
12 | .off => 0
13 | .warm => 1
14 | .bright => 2
15theorem exercise (m : Mode) : 0 < brightness (advance m) + brightness (advance (advance m)) := by
given
Loading editor…

Where you start. Everything above ⊢ may be assumed; the line below it is what you must prove.

m:Mode
⊢0 < brightness (advance m) + brightness (advance (advance m))
ReadyLn 1, Col 1Lean 4
Draft saved in this browser
Stuck?
Recall and apply

Knowledge check

Answer without looking back, then check your reasoning.

0of 3
correct
Question 1 of 3How many goals does cases m create for m : Mode?
Question 2 of 3Why can decide finish each branch?
Question 3 of 3In a branch, h : 0 = 2. What closes the goal?