Prove every constructor case

Split an arbitrary value into cases and prove a property in every branch.

12 minBeginnercasesone goal per constructorbranch proofsdecide
0/13 completed in this course

Pattern matching computes a function. The cases tactic uses the same constructor split inside a proof. For an arbitrary m : Mode, cases m with opens one goal for off, one for warm, and one for bright.

After the split, each branch contains a concrete constructor. brightness and advance can now compute, turning the symbolic theorem into three closed arithmetic facts.

decide proves each closed fact by evaluation. The proof is longer than a one-line calculation because the theorem covers every possible Mode, and each case is visible and checked separately.

This pattern scales to real programs: define behaviour by constructors, then prove a property by following the same cases. If a new constructor is added later, both the function and the proof become incomplete until it is handled.

Worked example

example (m : Mode) : brightness m  2 := by
  cases m with
  | off => decide
  | warm => decide
  | bright => decide

The proof covers every mode separately. Your theorem follows two transitions in each branch before checking the arithmetic.

Takeaway

cases turns a theorem about an inductive value into one goal per constructor. Each branch can then use the computation rules for that constructor.

Your exercise

Prove that the brightness values of the next two modes always add to a positive number.

Suggested steps
  1. Split m into all 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 here?
Question 2 of 3Why can decide finish each branch?
Question 3 of 3What happens if a fourth constructor is later added to Mode?