Stage 07 · Step 34 of 50

Make your own type.

Core Lean12 minutesLean 4.36.0

01The idea

A menu gives a fixed set of choices. An inductive type can name those choices directly.

Lemmi · Your guide for this idea
Lemmi pointing

Pattern matching describes what happens for each constructor.

02In maths

Each weekday has a next weekday. Sunday maps back to Monday.

Picture it

The Weekday type: seven constructors in a ringSeven tiles arranged in a ring: .mon, .tue, .wed, .thu, .fri, .sat, .sun. These seven constructors are the only values of type Weekday. An arrow goes from each day to the next one, one arrow per case of nextDay. The arrow from .sun back to .mon is highlighted, with the label nextDay .sun = .mon, proved by rfl. In the middle: 7 constructors, 7 cases, one arrow each..mon.tue.wed.thu.fri.sat.sunWeekday7 constructors,7 cases of nextDaynextDay .sun = .moncoral arrow: the loop back, by rfl
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.

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

After line 16
⊢ 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

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

    Prove that the two light constructors are different.

  2. A little stretch

    Prove that flipping the supplied switch twice restores it.

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

08A quick check

1. Must this nextDay definition cover Sunday?

Read the answer and explanation

Yes. Lean checks that the pattern matching definition handles every constructor.

2. Why can Lean print the weekday value?

Read the answer and explanation

The type derives Repr. Repr provides a representation used by evaluation output.

One more idea in your toolkit.

Take your time. Progress is yours to mark.