Chapter 3 · Numbers and induction

Rewriting

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

Step 1 of 22 · Concept

rw replaces one side of an equation by the other

In this lesson you use equations and library facts to change a goal.

With h : a = b, rw [h] replaces every a in the goal by b. Then it closes the goal if both sides are the same, or are given numbers that compute alike. rw [h1, h2] rewrites with two facts, in order.

That closing check never unfolds a definition, not even + on unknown numbers. a + 0 = a stays open:

example (a b : Nat) (h : b = a) : b + 0 = a := by
  rw [h]
Main.lean:1:47: error: unsolved goals
a b : Nat
h : b = a
⊢ a + 0 = a
example (x y : Nat) (h1 : x = 3) (h2 : y = 4) : x + y = 7 := by
  rw [h1]
  rw [h2]
example (x y : Nat) (h1 : x = 3) (h2 : y = 4) : x + y = 7 := by
  rw [h1, h2]
example (x y : Nat) (h1 : x = 3) (h2 : y = x + 4) : y = 7 := by
  rw [h1] at h2
  exact h2
theorem ten (x : Nat) (h : 5 = x) : x + x = 10 := by
  sorry
theorem b_four (a b : Nat) (h1 : a = 3) (h2 : b = a + 1) :
    b = 4 := by
  sorry
#check Nat.add_comm
#check Nat.mul_one
theorem turn_two (a b : Nat) : a + b + 0 = b + a := by
  rw [Nat.add_zero]
  sorry
example (n : Nat) : 0 + n = n := by
  exact?
theorem turn (a b c : Nat) : a + b + c = c + b + a := by
  sorry
theorem swap_mul (a b c : Nat) (h : c = a * b) :
    c * 1 = b * a := by
  sorry
theorem mid (a b c : Nat) : a + b + c = a + c + b := by
  sorry
Terminal
Step 1 of 9See rw work

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)