Chapter 2 · Logic
For all, there exists
Step 1 of 22 · Concept
A property is a function to propositions
In this lesson you prove facts about every number, and about some number.
A property of numbers turns each number into a proposition. (P : Nat → Prop) names one, as (p : Prop)
named a proposition in lesson 5. P 3 is a call (lesson 3), and it is a proposition.
fun n => n < 5 is a property: given 3, it gives 3 < 5.
Step 2 of 22 · Concept
∀ is a function from numbers to proofs
∀ n : Nat, P n means for every number n, P n. Type ∀ as \forall, or forall in ASCII. The goal panel
writes it ∀ (n : Nat), P n.
A proof of it works like a function: give it a number, and it gives a proof about that number. To prove it, intro n
takes any number n and leaves ⊢ P n.
Step 3 of 22 · Quiz
Predict the goal after intro n
The goal panel shows this. What does it show after intro n?
⊢ ∀ (n : Nat), n + 0 = n
Step 4 of 22 · Demo
See it checked
Press Run. Put the cursor on intro n and read the goal panel, before and after. Then rfl closes
n + 0 = n, since n + 0 computes to n (lesson 4).
Step 5 of 22 · Concept
Use ∀ by giving it a number
h : ∀ n, P n proves P n for every number. Give it one, as in a call: h 3 is a proof of P 3.
| you write | you get |
|---|---|
exact h 3 | the goal P 3 closed |
specialize h 3 | h : P 3 in place of h |
In ∀ n, P n, Lean works out from P that n is a Nat. ∀ n, reaches to the end: ∀ n, P n → Q n is
∀ n, (P n → Q n).
A theorem's parameters are a ∀ too: lesson 5's #print first showed ∀ (p q : Prop), p → q → p.
Step 6 of 22 · Quiz
What does h 3 prove?
You have h : ∀ n, P n → Q n. What does h 3 prove?
Step 7 of 22 · Demo
See specialize run
Press Run. Put the cursor on specialize h 2 and read the goal panel, before and after: h becomes
h : P 2 → Q 2. Then h hp proves Q 2.
Step 8 of 22 · Try it
Try it: give h its number
h proves P n for every n, but the goal is P 3, so Lean refuses exact h:
Main.lean:2:2: error: Type mismatch
h
has type
∀ (n : Nat), P n
but is expected to have type
P 3
Press Run and read the refusal. Then give h the number the goal is about.
Write the number after h, as in a call. The result proves P 3.
Step 9 of 22 · Exercise
Your turn: two facts about 7
Prove at_seven: every P n gives Q n, and P holds for every number, so Q 7 holds.
exact h1 7 is refused: h1 7 still needs a proof of P 7.
error: Type mismatch
h1 7
has type
P 7 → Q 7
but is expected to have type
Q 7
h2 gives a proof of P n for any number you hand it. You may also specialize each hypothesis first.
h2 7 is a proof of P 7.
Step 10 of 22 · Concept
∃ needs a witness
∃ n, P n means there is a number n with P n. Type ∃ as \exists, or exists in ASCII. The goal panel
writes it without n's type.
To prove it, give a number that works, a witness: exists 3 leaves ⊢ P 3. Then exists tries to close that
goal on its own. It succeeds when the goal computes, such as 3 + 2 = 5.
Step 11 of 22 · Quiz
Which number?
The goal panel shows this. Which tactic proves it?
⊢ ∃ n, n + 2 = 5
Step 12 of 22 · Demo
See it run
Press Run: Lean accepts both. In has_three, exists 3 closed 3 + 2 = 5 on its own. has_three' is a
term: ⟨3, rfl⟩ gives the witness 3, then a proof about it, rfl.
Step 13 of 22 · Concept
Using ∃: a number you cannot choose
h : ∃ n, P n says some number has P, not which. obtain ⟨n, hn⟩ := h names that number n, which you
cannot choose, and its proof hn : P n.
exists also closes a goal that a hypothesis proves. Here exists n leaves nothing, since hn proves P n. A
following exact hn is one tactic too many:
error: No goals to be solved
Step 14 of 22 · Quiz
Predict the goal after obtain
The goal panel shows this. What does it show after obtain ⟨n, hn⟩ := h? The line P Q : Nat → Prop stays, so the
options leave it out.
P Q : Nat → Prop
h : ∃ n, P n ∧ Q n
⊢ ∃ n, Q n
Step 15 of 22 · Trace
Watch a witness passed on
some_q takes the number out of h and gives it to the goal. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem some_q (P Q : Nat → Prop)
(h : ∃ n, P n ∧ Q n) : ∃ n, Q n := by
obtain ⟨n, hn⟩ := h
exists n
exact hn.2- Line 3 is next.
hsays some number has bothPandQ. The goal is∃ n, Q n. obtainreplacedhwith the numbernandhn : P n ∧ Q n. Line 4 is next.exists ngave that number as the witness. No hypothesis isQ n, so the goalQ nstays. Line 5 is next.hn.2provesQ n, soexact hn.2closed the goal: No goals.
Step 16 of 22 · Demo
See the witness passed on
Press Run. Put the cursor on each tactic and read the goal panel, before and after.
Step 17 of 22 · Exercise
Your turn: pass the witness on
Prove pass_on: if some number has P, then some number has P or Q.
exists 0 picks a number nobody knows has P, and leaves a goal no hypothesis proves:
error: unsolved goals
P Q : Nat → Prop
h : ∃ n, P n
⊢ P 0 ∨ Q 0
Take the number out of h instead.
obtain gives you n and hn : P n. After exists n, the goal is P n ∨ Q n: choose a side (lesson 6).
obtain ⟨n, hn⟩ := h
Step 18 of 22 · Exercise
Your turn: every number has a next one
Prove next_one: for every number n, some number m is n + 1.
After intro n, exists n leaves a false goal:
error: unsolved goals
n : Nat
⊢ n = n + 1
After intro n, the goal is ∃ m, m = n + 1. Which m makes m = n + 1 true? A witness may be a sum.
With the right witness, the goal computes and exists closes it on its own: nothing follows that line.
Step 19 of 22 · Quiz
Does the order matter?
Which of these two propositions are true?
∀ n, ∃ m, m = n + 1
∃ m, ∀ n, m = n + 1
Step 20 of 22 · Check yourself
Check yourself: for all, then for all
No shape this time.
Prove all_imp: if every P n gives Q n, and P holds for every number, then Q holds for every number.
Three things to move into the hypotheses: two proofs, then a number. Then give that number to both proofs.
Step 21 of 22 · Challenge · optional
Challenge: two numbers at once
Optional.
Prove two_numbers. ∃ a b : Nat asks for two numbers, a and then b. The new form: exists takes several
witnesses at once, separated by commas.
∃ a b : Nat, ... is two ∃s, a first. exists 2 alone leaves ⊢ ∃ b, 2 + b = 5 ∧ 2 = 2.
Give the witnesses in the order of the ∃s, separated by a comma. Which b makes 2 + b = 5?
With both numbers given, the goal computes, and exists closes it on its own.
Step 22 of 22 · Recap
Recap
intro nproves∀ n, P n;h 3orspecialize h 3uses it.exists 3or⟨3, proof⟩proves∃ n, P n.obtain ⟨n, hn⟩ := husesh : ∃ n, P n: a number you cannot choose.existscloses what is left when it computes or a hypothesis proves it.- In
∀ n, ∃ m,mmay depend onn; in∃ m, ∀ n, it may not.
Next: lesson 9 rewrites equations with facts from the library.
Review
5 questions from this lesson, in a new order each time. Answer from memory: 3 of 5 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
theorem add_zero_all : ∀ n : Nat, n + 0 = n := by
intro n
rfl
theorem at_two (P Q : Nat → Prop) (h : ∀ n, P n → Q n)
(hp : P 2) : Q 2 := by
specialize h 2
exact h hp
theorem at_three (P : Nat → Prop) (h : ∀ n, P n) : P 3 := by
exact h
theorem at_seven (P Q : Nat → Prop) (h1 : ∀ n, P n → Q n)
(h2 : ∀ n, P n) : Q 7 := by
sorry
theorem has_three : ∃ n : Nat, n + 2 = 5 := by
exists 3
theorem has_three' : ∃ n : Nat, n + 2 = 5 := ⟨3, rfl⟩
theorem some_q (P Q : Nat → Prop)
(h : ∃ n, P n ∧ Q n) : ∃ n, Q n := by
obtain ⟨n, hn⟩ := h
exists n
exact hn.2
theorem pass_on (P Q : Nat → Prop) (h : ∃ n, P n) :
∃ n, P n ∨ Q n := by
sorry
theorem next_one : ∀ n : Nat, ∃ m : Nat, m = n + 1 := by
sorry
theorem all_imp (P Q : Nat → Prop) : (∀ n, P n → Q n) →
(∀ n, P n) → ∀ n, Q n := by
sorry
theorem two_numbers : ∃ a b : Nat, a + b = 5 ∧ a = 2 := by
sorry