Chapter 3 · Numbers and induction

Recursion on numbers

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

Step 1 of 22 · Concept

Nat is built from zero and succ

In this lesson you define functions by recursion, and see why 0 + n = n is not rfl. Lean defines Nat so:

inductive Nat where
  | zero : Nat
  | succ (n : Nat) : Nat

A number is Nat.zero, or Nat.succ n: one more than a number n. So 3 is Nat.succ (Nat.succ (Nat.succ Nat.zero)). Goals and definitions write Nat.succ n as n + 1; a #check of a fact may still print n.succ.

#eval Nat.succ (Nat.succ Nat.zero)
#check Nat.succ
example : Nat.succ 4 = 5 := rfl
example (n : Nat) : Nat.succ n = n + 1 := rfl
def pred : Nat → Nat
  | 0 => 0
  | n + 1 => n
#eval pred 5
#eval pred 0
def sumTo : Nat → Nat
  | 0 => 0
  | n + 1 => (n + 1) + sumTo n
#eval sumTo 4
#eval sumTo 10
-- by recursion: double 0 is 0,
-- double (n + 1) is double n + 2
def double : Nat → Nat := sorry
-- then show double 5 with #eval
def loop (n : Nat) : Nat := loop (n + 1)
def half : Nat → Nat
  | 0 => 0
def sumSq : Nat → Nat
  | 0 => 0
  | n + 1 => (n + 1) + sumSq n
#eval sumSq 3
example (n m : Nat) : n + (m + 1) = n + m + 1 := rfl
example (n : Nat) : n + 0 = n := rfl
def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2
example (k : Nat) : double (k + 1) = double k + 2 := by
  rw [double]
def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2
theorem dbl_step (k m : Nat) (h : double k = m) :
    double (k + 1) = m + 2 := by
  sorry
theorem dbl_five : double 5 = 10 := by
  sorry
-- by recursion: triple 0 is 0,
-- triple (n + 1) is triple n + 3
def triple : Nat → Nat := sorry
-- show triple 4 with #eval
theorem triple_step (k m : Nat) (h : triple k = m) :
    triple (k + 1) = m + 3 := by
  sorry
-- fib 0 is 0, fib 1 is 1,
-- fib (n + 2) is fib n + fib (n + 1)
def fib : Nat → Nat := sorry
theorem fib_ten : fib 10 = 55 := by
  sorry
Terminal
Step 1 of 10See it run

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)