Stage 07 · Step 34 of 50
Make your own type.
01The idea
A menu gives a fixed set of choices. An inductive type can name those choices directly.
Pattern matching describes what happens for each constructor.
02In maths
Each weekday has a next weekday. Sunday maps back to Monday.
Picture it
Weekday has exactly seven values, the seven constructors, and nothing else. So nextDay only needs one rule per constructor: seven arrows, one from each tile. Lean refuses the definition if any tile is missing its arrow. rfl just follows the .sun arrow and lands on .mon.03In Lean
This code comes directly from the file checked by Lean.
inductive Weekday where
| mon | tue | wed | thu | fri | sat | sun
deriving Repr, DecidableEq
def nextDay : Weekday → Weekday
| .mon => .tue
| .tue => .wed
| .wed => .thu
| .thu => .fri
| .fri => .sat
| .sat => .sun
| .sun => .mon
#eval nextDay .fri
example : nextDay .sun = .mon := by
rfl
Pattern matching defines behavior by inspecting which constructor built an input.
The leading dot in a constructor name lets Lean infer its type from context. The constructors remain distinct values.
The practice solution uses <;>, a tactic combinator. It runs the tactic on its right for every goal produced by the tactic on its left.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
⊢ nextDay Weekday.sun = Weekday.mon
After line 16: example : nextDay .sun = .mon := by
⊢ nextDay Weekday.sun = Weekday.mon
After line 17: rfl
no goals
05Lean says
Weekday.sat 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.
inductive TinyDay where
| first | second
def badNext : TinyDay → TinyDay
| .first => .second
Missing cases: TinyDay.second
The second constructor has no matching result.
The fix: Handle every constructor in the function definition.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Prove that the two light constructors are different.
Show a hint
The type derives a decision procedure for equality.
One more hint
decide can check this concrete inequality.
- A little stretch
Prove that flipping the supplied switch twice restores it.
Show a hint
Split into the two switch constructors.
One more hint
Both resulting equations compute directly.
inductive Light where
| red | green
deriving DecidableEq
example : Light.red ≠ Light.green := by
sorry
inductive Switch where
| off | on
def flipSwitch : Switch → Switch
| .off => .on
| .on => .off
example (s : Switch) : flipSwitch (flipSwitch s) = s := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
inductive Light where
| red | green
deriving DecidableEq
example : Light.red ≠ Light.green := by
decide
inductive Switch where
| off | on
def flipSwitch : Switch → Switch
| .off => .on
| .on => .off
example (s : Switch) : flipSwitch (flipSwitch s) = s := by
cases s <;> rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.