Chapter 4 · Your own data
Your own types
Step 1 of 22 · Concept
You can make a type of your own
In this lesson you define a type, compute with it, and prove a fact for every one of its values.
inductive Color where
| red | green | blue
inductive makes a new type, Color. Each | gives one of its constructors: the ways to build a value.
Color.red is a value of type Color, and so are Color.green and Color.blue.
Step 2 of 22 · Quiz
How many colours are there?
How many values does the type Color have?
inductive Color where
| red | green | blue
Step 3 of 22 · Concept
deriving Repr gives a type its printed form
#eval Color.red prints Color.red: a constructor prints with its type's name in front. #eval makes such a
printed form on the fly when the type has none.
deriving Repr under the type writes that printed form once, as part of the type:
inductive Color where
| red | green | blue
deriving Repr
Every type these lessons #eval says deriving Repr; lesson 18 says what it makes. Where Lean knows the type, .red is
short for Color.red.
Step 4 of 22 · Demo
See a colour printed
Press Run. Line 4 prints the constructor, with its type's name.
Step 5 of 22 · Concept
A function by cases on the constructors
next gives the colour after a colour. It has one line per constructor:
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .red
Color → Color is a function from colours to colours; type → as \to or ->. Each line says what one
constructor gives, as lesson 12's | 0 => ... and | n + 1 => ... did for numbers. The type is known here, so
.green means Color.green.
Step 6 of 22 · Quiz
Predict the second colour
Line 8 prints Color.green. Type the missing line.
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .red
#eval next Color.red
#eval next (next Color.red)
Step 7 of 22 · Demo
See it run
DecidableEq after Repr is for later in the lesson, the step Different constructors are never equal.
Press Run and compare with your answer.
Step 8 of 22 · Try it
Try it: go round
Press Run: next Color.blue prints Color.blue. Make blue go round to red, so that Run prints Color.red.
Change what the line | .blue => ... gives: .red.
Step 9 of 22 · Concept
cases c leaves one goal per constructor
To prove a fact for every colour c, prove it for each constructor. cases c does that. It replaces c by each
constructor in turn: one goal each, case red, case green and case blue.
Each goal is about a given colour, so it computes, and rfl closes it. In lesson 6, cases split a hypothesis.
Here it splits a value of your own type.
Step 10 of 22 · Trace
Watch cases split the goal
next is the step A function by cases on the constructors. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem next3 (c : Color) : next (next (next c)) = c := by
cases c
· rfl
· rfl
· rfl- Line 2 is next. The goal is about any colour
c. cases cleft three goals, one per constructor.case redis in focus. Line 3 is next.· rflclosedcase red: threenexts from red come back to red. Line 4 is next.case greenis closed too. Onlycase blueis left. Line 5 is next.- The last
rflclosedcase blue. No goals.
Step 11 of 22 · Concept
<;> runs a tactic on every goal
The three · rfl lines do the same thing. <;> says it once:
theorem next3 (c : Color) : next (next (next c)) = c := by
cases c <;> rfl
t <;> s runs t, then runs s on every goal that t left. Here rfl runs on case red, case green and
case blue, and closes all three.
Step 12 of 22 · Demo
One line for three goals
Press Run: Lean accepts next3 and prints nothing. Put the cursor on cases: after it, the goal panel shows
three goals. Then put it on <;>: after the whole line, No goals.
Step 13 of 22 · Exercise
Your turn: the colour before
- Define
prev, the colour before:prev Color.greenisColor.red. - Prove
prev_next: going forward, then back, gives the colour you started with.
prev needs a line for every constructor. Leave one out, and Lean refuses the definition with Missing cases:, naming the constructor (lesson 12).
Write prev as next is written, one line per colour, each going back one. Then one cases line proves prev_next.
def prev : Color → Color
| .red => .blue
Step 14 of 22 · Concept
A constructor can carry values
A constructor can hold values, its fields:
inductive Shape where
| circle (r : Nat)
| rect (w h : Nat)
Shape.circle 2 is a value: a circle with r equal to 2. A function's line names the fields, and uses them:
def area : Shape → Nat
| .circle r => 3 * r * r
| .rect w h => w * h
Step 15 of 22 · Demo
See shapes computed
Press Run. A value prints with its fields; area names them in each line.
Step 16 of 22 · Concept
Different constructors are never equal
Color.red ≠ Color.blue: two different constructors are never equal. Type ≠ as \ne; in ASCII,
Not (Color.red = Color.blue) (lesson 7).
deriving DecidableEq lets decide compare two values of the type, so decide proves it. Without it, decide
refuses:
error: failed to synthesize
Decidable (Color.red ≠ Color.blue)
With it, decide also refuses the false claim Color.red = Color.blue, as in lesson 4:
error: Tactic `decide` proved that the proposition
Color.red = Color.blue
is false
Step 17 of 22 · Concept
Bool and Nat are inductive types too
The types you know are built the same way:
| type | its constructors |
|---|---|
Bool | false and true |
Nat | Nat.zero and Nat.succ n (lesson 12) |
Color | red, green and blue |
So cases works on them as on Color. cases b on a b : Bool leaves case false and case true.
Nat.succ holds a field, the smaller number, as circle holds r.
Step 18 of 22 · Quiz
Which one does decide prove?
Color has deriving Repr, DecidableEq, and next is the step A function by cases on the constructors. Which
claim does decide prove?
Step 19 of 22 · Demo
See decide compare
Press Run: Lean accepts red_blue and prints nothing.
Step 20 of 22 · Check yourself
Check yourself: a traffic light
No shape this time.
- Define
after, the light that comes next, as the comment says. - Prove
after_three: three steps come back to the start. - Prove
red_ne_green.
One line per constructor for after; one goal per constructor for after_three.
Step 21 of 22 · Challenge · optional
Challenge: is it a circle?
Optional.
Define isCircle: true for a circle, false for a rectangle, whatever its fields. Then prove circle_is and
rect_not.
A line names each field of its constructor, but isCircle uses none of them. Named r, the field gives lesson 4's warning: Variable name `r` is not explicitly referenced..
_ in place of a name marks a field you do not use: | .circle _ => .... A rect has two fields, so two _.
With isCircle defined by its two lines, each fact computes for any r, w and h: rfl sees it.
Step 22 of 22 · Recap
Recap
inductivemakes a type; each|is a constructor, and a value is one of them.- A function by cases has one line per constructor:
| .red => .green. cases cleaves one goal per constructor;cases c <;> rflcloses them all.- A constructor can hold fields,
| circle (r : Nat);deriving Repr, DecidableEqprints and compares values.
Next: lesson 15 builds a type with a constructor that holds a smaller value of its own type: lists.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
inductive Color where
| red | green | blue
deriving Repr
#eval Color.red
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .red
#eval next Color.red
#eval next (next Color.red)
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .blue
#eval next Color.blue
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .red
theorem next3 (c : Color) : next (next (next c)) = c := by
cases c <;> rfl
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
def next : Color → Color
| .red => .green
| .green => .blue
| .blue => .red
-- the colour before: prev green is red
def prev : Color → Color := sorry
theorem prev_next (c : Color) : prev (next c) = c := by
sorry
inductive Shape where
| circle (r : Nat)
| rect (w h : Nat)
deriving Repr
def area : Shape → Nat
| .circle r => 3 * r * r
| .rect w h => w * h
#eval Shape.circle 2
#eval area (Shape.rect 3 4)
inductive Color where
| red | green | blue
deriving Repr, DecidableEq
theorem red_blue : Color.red ≠ Color.blue := by decide
inductive Light where
| red | amber | green
deriving Repr, DecidableEq
-- red to green, green to amber, amber to red
def after : Light → Light := sorry
theorem after_three (l : Light) :
after (after (after l)) = l := by
sorry
theorem red_ne_green : Light.red ≠ Light.green := by
sorry
inductive Shape where
| circle (r : Nat)
| rect (w h : Nat)
-- true for a circle, false for a rectangle
def isCircle : Shape → Bool := sorry
theorem circle_is (r : Nat) :
isCircle (Shape.circle r) = true := by
sorry
theorem rect_not (w h : Nat) :
isCircle (Shape.rect w h) = false := by
sorry