Chapter 4 · Your own data

Your own types

You are reading the whole lesson on one page. Back to one step at a time

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.

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
Terminal
Step 1 of 9See a colour printed

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)