Chapter 2 · Logic
And, or
Step 1 of 22 · Concept
p ∧ q needs a proof of each
In this lesson you prove "and" and "or", and use them as hypotheses. Some proofs have two goals at once.
p ∧ q means p and q: it holds when both hold. Type ∧ as \and, or /\ in ASCII.
On the goal p ∧ q, constructor leaves two goals: case left with ⊢ p, then case right with ⊢ q.
Step 2 of 22 · Quiz
Predict the goals after constructor
The goal panel shows this. What does it show after constructor? The hypotheses stay, so the options leave them
out.
p q : Prop
hp : p
hq : q
⊢ p ∧ q
Step 3 of 22 · Concept
· focuses on one goal
After constructor, Lean works on the first goal. A bullet · focuses on it: the tactics after the · prove
that goal alone. Type · as \., or a plain . in ASCII.
theorem both (p q : Prop) (hp : p) (hq : q) : p ∧ q := by
constructor
· exact hp
· exact hq
Each · sits at the proof's indentation, one per goal, in order. A bullet's proof may take several lines, each lined
up after the ·. Lean checks that every bullet closes its goal.
Step 4 of 22 · Demo
See both goals closed
Press Run. Put the cursor on each exact: inside a bullet the goal panel shows one goal. Line 5 prints the term
the tactics built.
Step 5 of 22 · Concept
⟨hp, hq⟩ builds it at once
The demo See both goals closed printed the term its tactics built:
fun p q hp hq => ⟨hp, hq⟩
⟨hp, hq⟩ is a proof of p ∧ q made from its two parts, in order. You may write it yourself: exact ⟨hp, hq⟩ closes
the goal p ∧ q in one tactic.
Type ⟨ as \< and ⟩ as \>. They have no ASCII form.
Step 6 of 22 · Concept
Using p ∧ q: h.1, h.2, obtain
A hypothesis h : p ∧ q holds two proofs. Take them out in either of two ways:
from h : p ∧ q | you get |
|---|---|
h.1 and h.2 | a proof of p and a proof of q |
obtain ⟨hp, hq⟩ := h | hp : p and hq : q in the hypotheses, in place of h |
Lean's messages write h.1 as h.left and h.2 as h.right.
obtain ⟨hp, hq⟩ takes an ∧ apart, not an ∨. On h : p ∨ q it fails:
error: Tactic `rcases` failed: `h✝ : p` is not an inductive datatype
h✝ is a name Lean made up (lesson 13).
The part Or shows how to use an ∨.
Step 7 of 22 · Quiz
Predict the goal after obtain
The goal panel shows this. What does it show after obtain ⟨hp, hq⟩ := h? The line p q : Prop stays, so the
options leave it out.
p q : Prop
h : p ∧ q
⊢ q ∧ p
Step 8 of 22 · Demo
See obtain run
Press Run. Put the cursor on obtain and read the goal panel, before and after: h becomes hp and hq.
Then ⟨hp, hq⟩ puts the two parts back together.
Step 9 of 22 · Try it
Try it: swap the two
The starter gives the two parts of h in the wrong order, so Lean refuses it. The message begins:
Main.lean:2:9: error: Application type mismatch: The argument
h.left
has type
p
but is expected to have type
q
Press Run and read the refusal. Then swap the two parts so that the proof fits q ∧ p.
The first part of ⟨ , ⟩ proves the left side of the goal, q. Which of h.1 and h.2 proves q?
Step 10 of 22 · Exercise
Your turn: pick and pack
Prove pick: take a proof of p out of h, then build r ∧ p.
exact ⟨hr, h⟩ does not fit: h proves p ∧ q, not p. Lean's message begins:
error: Application type mismatch: The argument
h
has type
p ∧ q
but is expected to have type
p
obtain gives you hp : p. Then the goal r ∧ p needs its two proofs, r first.
obtain ⟨hp, hq⟩ := h
Step 11 of 22 · Concept
p ∨ q needs a proof of one
p ∨ q means p or q: it holds when at least one side holds. Type ∨ as \or, or \/ in ASCII.
To prove p ∨ q, choose the side you can prove:
| tactic | the goal p ∨ q becomes | the same as a term |
|---|---|---|
left | ⊢ p | Or.inl hp, with hp : p |
right | ⊢ q | Or.inr hq, with hq : q |
So exact Or.inr hq closes the goal p ∨ q at once.
Step 12 of 22 · Quiz
Which side?
The goal panel shows this. Which tactics close the goal?
p q : Prop
hq : q
⊢ p ∨ q
Step 13 of 22 · Concept
Using p ∨ q: one goal per side
h : p ∨ q says one side holds, but not which. So split h before you choose a side, and prove the goal once
for each side:
cases h with
| inl hp => exact Or.inr hp
| inr hq => exact Or.inl hq
Each | line sits under cases. inl and inr are Lean's names for the sides, as in Or.inl and Or.inr;
hp and hq are yours. | left hp => is refused:
error: Invalid alternative name `left`: Expected `inl`
Step 14 of 22 · Trace
Watch both cases
or_swap turns p ∨ q around. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem or_swap (p q : Prop) : p ∨ q → q ∨ p := by
intro h
cases h with
| inl hp => exact Or.inr hp
| inr hq => exact Or.inl hq- Line 2 is next. The goal is
p ∨ q → q ∨ p. intro haddedh : p ∨ q. The goal isq ∨ p. Line 3 is next.casessplith, one case per side: firstcase inl, withhp : p. Line 4 is next.exact Or.inr hpclosedcase inl. Left iscase inr, withhq : q. Line 5 is next.exact Or.inl hqclosedcase inr: No goals, the proof is complete.
Step 15 of 22 · Demo
See it run
This or_swap chooses its sides with right and left. A case's proof may take several lines after =>, indented
further.
Press Run. Put the cursor on each tactic in a case, such as right in case inl, and read the goal panel.
Step 16 of 22 · Exercise
Your turn: either way
Prove either: whichever side of h holds, you can reach r.
left chooses a side of the goal, and the goal r has no sides. Lean says:
error: Tactic `left` failed: target is not an inductive datatype
The ∨ to use is in the hypothesis h.
cases h with gives one goal per side. With hp : p, the call hpr hp proves r (lesson 5).
| inl hp => exact hpr hp
Step 17 of 22 · Concept
∧ binds tighter than ∨
When ∧ and ∨ meet without brackets, ∧ groups first. From tightest to loosest:
| symbol | read |
|---|---|
¬ | not (typed in lesson 7) |
∧ | and |
∨ | or |
→ | if, then |
↔ | if and only if (typed in lesson 7) |
So p ∨ p ∧ q is p ∨ (p ∧ q), and p ∧ q ∨ p ∧ r is (p ∧ q) ∨ (p ∧ r). The goal panel leaves out brackets
that change nothing. Write them when in doubt.
Step 18 of 22 · Demo
See how Lean reads it
Press Run: Lean accepts exact h, so h and the goal are the same proposition. Put the cursor on exact h: the
goal panel shows h : p ∨ p ∧ q, without the brackets.
Step 19 of 22 · Exercise
Your turn: and inside or
Prove mix. Split h with cases, then find a proof of p in each case. Any proof Lean accepts passes.
After cases, h is gone: each case has only the name you gave it. exact h.1 in case inr gives:
error(lean.unknownIdentifier): Unknown identifier `h`
Read p ∨ p ∧ q as p ∨ (p ∧ q). In case inr you have a proof of p ∧ q, and its .1 proves p.
| inl hp => exact hp
Step 20 of 22 · Check yourself
Check yourself: and over or
No shape this time.
Prove distrib: from p together with q or r, get p with q, or p with r.
Take the hypothesis apart in two moves: first its ∧, then its ∨. In each case, choose a side of the goal.
Step 21 of 22 · Challenge · optional
Challenge: three cases
Optional.
Prove three. A new tactic helps: rcases h with hp | hq | hr splits h : p ∨ q ∨ r into three goals at once,
one for each proposition, with its proof named.
∨ groups from the right, as → does: p ∨ q ∨ r is p ∨ (q ∨ r), and the goal is r ∨ (q ∨ p).
After intro h and rcases, prove the three goals in turn, one bullet each. To reach p inside r ∨ (q ∨ p), choose right twice.
· left
exact hr
Step 22 of 22 · Recap
Recap
constructorsplitsp ∧ qinto two goals; a bullet·focuses on one.⟨hp, hq⟩buildsp ∧ q;h.1,h.2orobtaintake it apart.leftorrightchooses a side ofp ∨ q;cases h withusesh : p ∨ q, one case per side.- Split a hypothesis before you choose a side of the goal.
∧binds tighter than∨:p ∨ p ∧ qisp ∨ (p ∧ q).
Next: lesson 7 proves "not" and "if and only if".
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
theorem both (p q : Prop) (hp : p) (hq : q) : p ∧ q := by
constructor
· exact hp
· exact hq
#print both
theorem both_again (p q : Prop) (h : p ∧ q) : p ∧ q := by
obtain ⟨hp, hq⟩ := h
exact ⟨hp, hq⟩
theorem and_swap (p q : Prop) (h : p ∧ q) : q ∧ p := by
exact ⟨h.1, h.2⟩
theorem pick (p q r : Prop) (h : p ∧ q) (hr : r) : r ∧ p := by
sorry
theorem or_swap (p q : Prop) : p ∨ q → q ∨ p := by
intro h
cases h with
| inl hp =>
right
exact hp
| inr hq =>
left
exact hq
theorem either (p q r : Prop) (hpr : p → r) (hqr : q → r)
(h : p ∨ q) : r := by
sorry
example (p q : Prop) (h : p ∨ (p ∧ q)) : p ∨ p ∧ q := by
exact h
theorem mix (p q : Prop) (h : p ∨ p ∧ q) : p := by
sorry
theorem distrib (p q r : Prop) :
p ∧ (q ∨ r) → p ∧ q ∨ p ∧ r := by
sorry
theorem three (p q r : Prop) : p ∨ q ∨ r → r ∨ q ∨ p := by
sorry