Chapter 3 · Numbers and induction

Induction

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

Step 1 of 19 · Concept

Some facts need one more idea

In this lesson you prove facts about every number, such as 0 + n = n and double n = 2 * n.

rfl cannot prove 0 + n = n: + recurses on its second number, and n is unknown (lesson 12). Checking 0, 1, 2 and on never ends.

omega proves this one, but it knows nothing about your own functions. It treats double n as an unknown number, b:

def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2
example (n : Nat) : double n = 2 * n := by
  omega
Main.lean:5:2: error: omega could not prove the goal:
a possible counterexample may satisfy the constraints
  b ≥ 0
  a ≥ 0
  2*a - b ≥ 1
where
 a := ↑n
 b := ↑(double n)
theorem my_zero_add (n : Nat) : 0 + n = n := by
  induction n with
  | zero => rfl
  | succ k ih =>
    show 0 + k + 1 = k + 1
    rw [ih]
def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2
theorem double_eq (n : Nat) : double n = 2 * n := by
  induction n with
  | zero => rfl
  | succ k ih =>
    rw [double]
    rw [ih]
    omega
def triple : Nat → Nat
  | 0 => 0
  | n + 1 => triple n + 3
theorem triple_eq (n : Nat) : triple n = 3 * n := by
  induction n with
  | zero => sorry
  | succ k ih => sorry
def addTo (m : Nat) : Nat → Nat
  | 0 => m
  | n + 1 => addTo m n + 1
theorem addTo_eq (m n : Nat) : addTo m n = m + n := by
  sorry
def bump : Nat → Nat
  | 0 => 1
  | n + 1 => bump n + 1
theorem bump_eq (n : Nat) : bump n = n + 1 := by
  sorry
def odd : Nat → Nat
  | 0 => 1
  | n + 1 => odd n + 2
theorem odd_eq (n : Nat) : odd n = 2 * n + 1 := by
  sorry
def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2
theorem double_ge (n : Nat) : n ≤ double n := by
  sorry
Terminal
Step 1 of 7See 0 + n = n proved

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)