Chapter 4 · Your own data

Induction on lists

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

Step 1 of 21 · Concept

Induction works on lists too

In this lesson you prove facts about every list, such as app xs [] = xs for any xs.

A list is [] or x :: xs (lesson 15). So two proofs cover every list, as in lesson 13:

  • the fact for [];
  • the fact for x :: xs, assuming it for the shorter xs.

[] plays the part of 0, and x :: xs the part of k + 1.

def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
theorem app_nil (xs : List Nat) : app xs [] = xs := by
  induction xs with
  | nil => rfl
  | cons x xs ih => simp [app, ih]
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
def doubleAll : List Nat → List Nat
  | [] => []
  | x :: xs => 2 * x :: doubleAll xs
theorem len_doubleAll (xs : List Nat) :
    len (doubleAll xs) = len xs := by
  induction xs with
  | nil => rfl
  | cons x xs ih => simp [doubleAll, len]
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
    len (app xs ys) = len xs + len ys := by
  induction xs with
  | nil => simp [len, app]
  | cons x xs ih =>
    simp [len, app, ih]
    omega
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
theorem sum_app (xs ys : List Nat) :
    sum (app xs ys) = sum xs + sum ys := by
  sorry
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
def doubleAll : List Nat → List Nat
  | [] => []
  | x :: xs => 2 * x :: doubleAll xs
theorem sum_double (xs : List Nat) :
    sum (doubleAll xs) = 2 * sum xs := by
  sorry
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
    len (app xs ys) = len xs + len ys := by
  induction xs with
  | nil => simp [len, app]
  | cons x xs ih =>
    simp [len, app, ih]
    omega
theorem len_twice (xs : List Nat) :
    len (app xs xs) = 2 * len xs := by
  simp [len_app]
  omega
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
    len (app xs ys) = len xs + len ys := by
  induction xs with
  | nil => simp [len, app]
  | cons x xs ih =>
    simp [len, app, ih]
    omega
theorem len_three (xs ys zs : List Nat) :
    len (app (app xs ys) zs) = len xs + len ys + len zs := by
  sorry
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
theorem app_assoc (xs ys zs : List Nat) :
    app (app xs ys) zs = app xs (app ys zs) := by
  sorry
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
theorem len_eq_length (xs : List Nat) : len xs = xs.length := by
  sorry
Terminal
Step 1 of 9A first proof: app xs [] = xs

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)