Chapter 2 · Logic
Not, false and if-and-only-if
Step 1 of 22 · Concept
False has no proof
In this lesson you prove "not" and "if and only if", and meet a proof that rests on an axiom.
Two propositions are special. True always holds, and trivial proves it:
example : True := by trivial
False has no proof at all. A hypothesis h : False means the hypotheses contradict each other: they can never
all hold.
Step 2 of 22 · Quiz
Which can be proved from nothing?
A theorem has no hypotheses at all. Which of these can it prove?
Step 3 of 22 · Concept
¬p means p → False
¬p means not p. Type ¬ as \not, or write Not p in ASCII.
¬p is p → False: from a proof of p you could prove False. So intro hp starts its proof and leaves ⊢ False.
It binds tightest (lesson 6): ¬p ∧ ¬q is (¬p) ∧ (¬q).
Step 4 of 22 · Quiz
Predict the goal after intro
The goal panel shows this. What does it show after intro hp? The line p : Prop stays, so the options leave it
out.
p : Prop
⊢ ¬p
Step 5 of 22 · Concept
A proof of p and of ¬p give False
A proof of ¬p is a proof of p → False. Give it a proof of p, and it gives False, as hpq hp proved
q in lesson 5:
| you have | it proves |
|---|---|
hn : ¬p | p → False |
hn hp, with hp : p | False |
a ≠ b (lesson 1) is ¬(a = b), an arrow too: intro h on ⊢ 2 + 2 ≠ 5 leaves h : 2 + 2 = 5 and ⊢ False.
Step 6 of 22 · Concept
From False, any goal
Hypotheses that give False can never all hold. From them, any goal may be closed:
| tactic | what it does |
|---|---|
exfalso | turns any goal into ⊢ False |
contradiction | closes the goal when two hypotheses clash, or one is h : False |
hp : p and hn : ¬p clash. After exfalso, a proof of False, such as hn hp, closes the goal.
Step 7 of 22 · Demo
See a double negation proved
¬¬p is ¬p → False. Press Run, then put the cursor on exact hn hp: before it, the goal is False, with
hp : p and hn : ¬p.
The second proof closes the same goal with contradiction, and clash turns its goal into False with exfalso.
The last line proves True.
Step 8 of 22 · Try it
Try it: anything from a clash
The goal q has nothing to do with p, so Lean refuses exact hp:
Main.lean:2:2: error: Type mismatch
hp
has type
p
but is expected to have type
q
Press Run and read the refusal. Then replace exact hp with exfalso, and close the goal False with hn hp. Any
proof Lean accepts passes.
After exfalso, the goal is False. hn turns hp into a proof of it.
Step 9 of 22 · Quiz
Which hypotheses clash?
With which hypotheses does contradiction close any goal?
Step 10 of 22 · Exercise
Your turn: no q, so no p
Prove tollens: if p gives q, and q fails, then p fails too.
The goal ¬p is p → False, so exact hnq does not fit it:
error: Type mismatch
hnq
has type
¬q
but is expected to have type
¬p
Start with intro, as for any →.
After intro hp, the goal is False. Which hypothesis gives False, and what does it need?
hpq hp is a proof of q.
Step 11 of 22 · Concept
p ↔ q is two implications
p ↔ q means p if and only if q: each one gives the other. Type ↔ as \iff, or <-> in ASCII. It binds
loosest, looser even than →: p → q ↔ r is (p → q) ↔ r.
| you write | you get |
|---|---|
constructor on ⊢ p ↔ q | two goals: case mp ⊢ p → q, case mpr ⊢ q → p |
h.mp, from h : p ↔ q | a proof of p → q |
h.mpr, from h : p ↔ q | a proof of q → p |
Step 12 of 22 · Quiz
Predict the first goal
The goal panel shows this. After constructor, what is the first goal? The line p q : Prop stays, so the options
leave it out.
p q : Prop
⊢ p ∧ q ↔ q ∧ p
Step 13 of 22 · Demo
See both directions
Press Run, then put the cursor on each exact and read the goal panel.
In each direction a second constructor makes two goals with bullets of their own. A · inside a · focuses on
a goal of that goal.
Step 14 of 22 · Quiz
Which one proves p?
You have h : p ↔ q and hq : q. Which term proves p?
Step 15 of 22 · Exercise
Your turn: turn it around
Prove iff_swap: if p ↔ q, then q ↔ p.
exact h does not fit: h proves p ↔ q, and the goal is q ↔ p.
error: Type mismatch
h
has type
p ↔ q
but is expected to have type
q ↔ p
constructor leaves case mp with ⊢ q → p, then case mpr with ⊢ p → q. h holds both directions.
· exact h.mpr
Step 16 of 22 · Exercise
Your turn: not this one, so that one
Prove or_not: if p or q holds, and p does not, then q does.
In case inl, exact hn does not fit: hn proves ¬p, and the goal is q.
error: Type mismatch
hn
has type
¬p
but is expected to have type
q
In case inl you have hp : p and hn : ¬p. They clash, and a clash closes any goal.
| inr hq => exact hq
Step 17 of 22 · Concept
Every proposition is true or false
by_cases h : p splits a proof on whether p holds. It leaves two goals: case pos with h : p, and case neg
with h : ¬p.
That every proposition is true or false is classical logic. For a proposition you know nothing about, Lean cannot prove it from its other rules: it rests on an axiom.
Step 18 of 22 · Demo
See which axioms it rests on
Press Run. p_or_not splits on p and proves each case, and #print axioms names the axioms it rests on.
The check accepts these three standard axioms, and refuses sorryAx.
Step 19 of 22 · Quiz
Does the check accept it?
#print axioms p_or_not printed this. Does the check accept p_or_not?
'p_or_not' depends on axioms: [propext, Classical.choice, Quot.sound]
theorem p_or_not (p : Prop) : p ∨ ¬p := by
by_cases h : p
· exact Or.inl h
· exact Or.inr h
#print axioms p_or_notStep 20 of 22 · Check yourself
Check yourself: not either one
No shape this time.
Prove not_either: "not (p or q)" holds exactly when "not p, and not q" holds.
Two directions, each an implication. In the first, the goal ¬p ∧ ¬q splits into two goals, and each ¬ starts with intro.
Step 21 of 22 · Challenge · optional
Challenge: proof by contradiction
Optional.
Prove contra by contradiction: assume ¬q and reach False. A library fact does the first move:
Classical.byContradiction says that a proposition holds if its negation gives False. Any proof Lean accepts
passes.
apply Classical.byContradiction works backwards from the goal q, as apply does: it leaves ⊢ ¬q → False.
That goal is an implication: intro hnq gives hnq : ¬q and leaves ⊢ False.
h hnq is a proof of ¬p.
Step 22 of 22 · Recap
Recap
trivialprovesTrue;Falsehas no proof.¬pisp → False:intro hpstarts it;hn hpprovesFalse.exfalsoturns a goal intoFalse;contradictioncloses a goal when hypotheses clash.p ↔ qis two implications:constructorgivesmpandmpr; useh.mpandh.mpr.by_cases h : psplits onp; for apyou know nothing about, it rests onClassical.choice.
Next: lesson 8 proves facts about every number, and about some number.
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 nn_intro (p : Prop) (hp : p) : ¬¬p := by
intro hn
exact hn hp
theorem nn_intro' (p : Prop) (hp : p) : ¬¬p := by
intro hn
contradiction
theorem clash (p : Prop) (hp : p) (hn : ¬p) : 1 = 2 := by
exfalso
exact hn hp
example : True := by trivial
theorem anything (p q : Prop) (hp : p) (hn : ¬p) : q := by
exact hp
theorem tollens (p q : Prop) (hpq : p → q) (hnq : ¬q) : ¬p := by
sorry
theorem and_comm' (p q : Prop) : p ∧ q ↔ q ∧ p := by
constructor
· intro h
constructor
· exact h.2
· exact h.1
· intro h
constructor
· exact h.2
· exact h.1
theorem iff_swap (p q : Prop) (h : p ↔ q) : q ↔ p := by
sorry
theorem or_not (p q : Prop) (h : p ∨ q) (hn : ¬p) : q := by
sorry
theorem p_or_not (p : Prop) : p ∨ ¬p := by
by_cases h : p
· exact Or.inl h
· exact Or.inr h
#print axioms p_or_not
theorem not_either (p q : Prop) : ¬(p ∨ q) ↔ ¬p ∧ ¬q := by
sorry
theorem contra (p q : Prop) (h : ¬q → ¬p) (hp : p) : q := by
sorry