Stage 03 · Step 13 of 50
Unpack an and.
01The idea
A packed lunch contains both a drink and a sandwich. You can unpack the pair and use either part.
A proof of “and” also carries two component proofs.
02In maths
If P ∧ Q holds, then Q ∧ P holds.
Picture it
Swipe sideways to see the whole picture.
P ∧ Q is a packed pair: a proof of P and a proof of Q in one box. cases h opens the box and gives you the two parts as hP and hQ. The angle brackets ⟨hQ, hP⟩ pack them again in the other order, which is exactly a proof of Q ∧ P.03In Lean
This code comes directly from the file checked by Lean.
example (P Q : Prop) (h : P ∧ Q) : Q ∧ P := by
cases h with
| intro hP hQ =>
exact ⟨hQ, hP⟩
The tactic cases examines the constructor used to build a value. A conjunction has one constructor, with two components.
The symbols ⟨ and ⟩ form an anonymous constructor. Type them with \langle and \rangle.
For a conjunction proof, the fields left and right also retrieve its two component proofs.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
P Q : Prop h : P ∧ Q ⊢ Q ∧ P
After line 1: example (P Q : Prop) (h : P ∧ Q) : Q ∧ P := by
P Q : Prop h : P ∧ Q ⊢ Q ∧ P
After line 2: cases h with
P Q : Prop h : P ∧ Q ⊢ Q ∧ P
After line 3: | intro hP hQ =>
case intro P Q : Prop hP : P hQ : Q ⊢ Q ∧ P
After line 4: exact ⟨hQ, hP⟩
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
example (P Q : Prop) (h : P ∧ Q) : P := by
exact h
Type mismatch h has type P ∧ Q but is expected to have type P
A proof of a conjunction is not directly a proof term of its left component.
The fix: Extract the left component or split the conjunction into named parts.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Extract a proof of P from the supplied conjunction.
Show a hint
The left component proves P.
One more hint
The field named left retrieves it.
- A little stretch
Extract R from P and (Q and R).
Show a hint
First enter the right component.
One more hint
Its right component proves R.
example (P Q : Prop) (h : P ∧ Q) : P := by
sorry
example (P Q R : Prop) (h : P ∧ (Q ∧ R)) : R := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
example (P Q : Prop) (h : P ∧ Q) : P := by
exact h.left
example (P Q R : Prop) (h : P ∧ (Q ∧ R)) : R := by
exact h.right.right
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.