Chapter 2 · Logic
If, then
Step 1 of 21 · Concept
p and q can be any propositions
Until now every goal computed, like 2 + 2 = 4. In this lesson you prove facts about any propositions with
exact, intro and apply. You read the goal after each one.
A proposition is a statement: (p q : Prop) names any two. A hypothesis (hp : p) is a proof of p you
are given. The goal panel lists the hypotheses, then ⊢ and the goal.
Step 2 of 21 · Concept
exact closes a goal with a proof that fits
exact hp closes the goal with the proof hp, if it fits:
theorem same (p : Prop) (hp : p) : p := by
exact hp
Before exact hp, the goal panel shows:
p : Prop
hp : p
⊢ p
The goal is p, and hp is a proof of p, so it fits. After exact hp, the panel says No goals.
As in lesson 1, by ends the statement's line. Each tactic has a line of its own, at the same indentation.
Step 3 of 21 · Quiz
Which proof fits?
The goal panel shows this. Which tactic closes the goal?
p q : Prop
hp : p
hq : q
⊢ q
Step 4 of 21 · Demo
See it checked
Press Run: Lean accepts it and prints nothing. Then put the cursor on exact hp and read the goal panel, before
and after.
Step 5 of 21 · Concept
p → q turns a proof of p into a proof of q
p → q means if p, then q. Type → as \to or ->; the panel shows →.
Given hpq : p → q and hp : p, hpq hp proves q: a call, as in lesson 3.
p → q → r takes two proofs in turn: h hp hq proves r. → groups from the right: p → q → r is
p → (q → r), but (p → q) → r has one assumption, p → q.
Step 6 of 21 · Quiz
What does hpq hp prove?
You have hpq : p → q and hp : p. What does hpq hp prove?
Step 7 of 21 · Try it
Try it: give it both proofs
Press Run. Lean refuses exact h hp: it has type q → r, but the goal is r. Give h both of its proofs.
h takes a proof of p, then a proof of q. After h hp, add hq.
Step 8 of 21 · Concept
intro moves an assumption into the hypotheses
To prove if p, then q, assume p and prove q. On a goal p → q, intro hp does that: it moves p into the
hypotheses, named hp, and leaves q to prove.
| goal before | goal panel after intro hp |
|---|---|
⊢ p → q | hp : p, then ⊢ q |
For two assumptions, p → q → r, intro hp hq names both at once, in order.
Step 9 of 21 · Quiz
Predict the goal after intro
The goal panel shows this. What does it show after intro hp? The line p q : Prop stays, so the options leave it
out.
p q : Prop
⊢ p → q → p
Step 10 of 21 · Trace
Watch the goal change
first proves p → q → p. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem first (p q : Prop) :
p → q → p := by
intro hp
intro hq
exact hp- Line 3 is next. The goal is
p → q → p, andp q : Propnames the two propositions. intro hpmovedpinto the hypotheses ashp : p. The goal isq → p. Line 4 is next.intro hqaddedhq : q. The goal isp, andhpis a proof of it. Line 5 is next.exact hpclosed the goal: No goals, the proof is complete.
Step 11 of 21 · Concept
apply works backwards
apply works backwards from the goal. With hpq : p → q and the goal ⊢ q, apply hpq leaves the goal ⊢ p: to
prove q, it is enough to prove p.
| goal before | goal after apply hpq |
|---|---|
⊢ q | ⊢ p |
The hypotheses stay as they were. apply hpq fits only when what hpq ends in, here q, is the goal.
With h : p → q → r and the goal ⊢ r, apply h leaves two goals, ⊢ p and then ⊢ q, each headed case a.
Prove them one after the other; lesson 6 says more.
Step 12 of 21 · Quiz
Predict the goal after apply
The goal panel shows this. What does it show after apply hpq?
p q : Prop
hp : p
hpq : p → q
⊢ q
Step 13 of 21 · Demo
See it run
Press Run. Put the cursor on apply hpq and read the goal panel before and after it. Then do the same for
exact hp, and for intro hp hq, which names two assumptions at once.
Step 14 of 21 · Exercise
Your turn: two steps back
Work backwards from the goal, one hypothesis at a time.
Prove two_back. The goal is r: start with the hypothesis that ends in r.
apply hpq does not fit the goal ⊢ r, since hpq ends in q. Lean's message begins:
error: Tactic `apply` failed: could not unify the conclusion of `hpq`
q
with the goal
r
Which hypothesis ends in r?
apply hqr leaves ⊢ q. Then apply hpq leaves ⊢ p, and exact closes it.
apply hqr
apply hpq
Step 15 of 21 · Exercise
Your turn: swap the assumptions
Prove swap_args. Move the three assumptions into the hypotheses with intro, then close the goal with one
exact. Any proof Lean accepts passes.
intro gives the names in order: the first name goes to the first assumption, p → q → r. With intro hp hq h, hp names that, and exact h hp hq gives:
error: Function expected at
h
but this term has type
p
intro h hq hp leaves h : p → q → r, hq : q, hp : p and the goal ⊢ r.
h takes hp first, then hq: give it both, in that order, with one exact.
Step 16 of 21 · Concept
Two messages of a proof
unsolved goals means the proof ended with goals left. Lean lists them:
example (p : Prop) : p → p := by
intro hp
Main.lean:1:30: error: unsolved goals
p : Prop
hp : p
⊢ p
No goals to be solved means one tactic too many: the proof was already complete before that line.
example (p : Prop) (hp : p) : p := by
exact hp
exact hp
Main.lean:3:2: error: No goals to be solved
Step 17 of 21 · Quiz
What is missing?
Lean gives this message for this proof. Which line is missing at its end?
example (p q : Prop) : p → q → p := by
intro hp
intro hq
Main.lean:1:36: error: unsolved goals
p q : Prop
hp : p
hq : q
⊢ p
example (p q : Prop) : p → q → p := by
intro hp
intro hqMain.lean:1:36: error: unsolved goals
p q : Prop
hp : p
hq : q
⊢ pStep 18 of 21 · Concept
A proof of p → q is a function
#print first shows the term the tactics built:
theorem first : ∀ (p q : Prop), p → q → p :=
fun p q hp hq => hp
∀ (p q : Prop) reads "for any propositions p and q" (lesson 8); p q are the theorem's parameters. A proof of
p → q is a function: it takes a proof of p and gives a proof of q.
You may write one yourself, with lesson 3's fun:
theorem self (p : Prop) : p → p := fun hp => hp
Step 19 of 21 · Check yourself
Check yourself: from p to r
No shape this time.
Prove chain: if p gives q, and q gives r, then p gives r.
First move all three assumptions into the hypotheses. Then work backwards from the goal.
Step 20 of 21 · Challenge · optional
Challenge: the same proof as a function
Optional.
Prove chain' with no by and no tactics. Write one fun that takes the three proofs and gives a proof of r.
A fun takes one name per assumption, in order: fun hpq hqr hp => ....
After => comes a proof of r. hqr gives one from a proof of q, and hpq hp is a proof of q.
Put a call inside a call: the inner call goes in brackets, hqr (...).
Step 21 of 21 · Recap
Recap
exact hcloses a goal with a proof that fits it.intro hpmoves an assumption into the hypotheses;apply hpqworks backwards.hpq hpis a proof ofq;p → q → rtakes its two proofs in order.unsolved goalslists what is left;No goals to be solvedmeans one tactic too many.- A proof of
p → qis a function,fun hp => ....
Next: lesson 6 proves "and" and "or".
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 same (p : Prop) (hp : p) : p := by
exact hp
theorem give_both (p q r : Prop) (hp : p) (hq : q)
(h : p → q → r) : r := by
exact h hp
theorem backwards (p q : Prop) (hp : p) (hpq : p → q) : q := by
apply hpq
exact hp
theorem first' (p q : Prop) : p → q → p := by
intro hp hq
exact hp
theorem two_back (p q r : Prop) (hp : p) (hpq : p → q)
(hqr : q → r) : r := by
sorry
theorem swap_args (p q r : Prop) : (p → q → r) → q → p → r := by
sorry
theorem chain (p q r : Prop) : (p → q) → (q → r) → p → r := by
sorry
theorem chain' (p q r : Prop) : (p → q) → (q → r) → p → r :=
sorry