Module 3: Propositions as Types·Lesson 8 of 16·4 min read

And, Or, Implies: Building Proofs Like Data in Lean

The problem: real statements have connectives

2 + 2 = 4 is a fine start, but real statements are built from pieces. "x is positive and x is even." "The list is empty or it has a first element." "If the input is sorted, then the output is sorted." To prove anything interesting, you need to handle and, or and if then.

The good news from the last lesson is that proofs are terms. So proving an "and" or an "or" is just building a value with the right constructor, the same way you built a Point or a Shape. This lesson shows how.

And: a pair of proofs

To prove p ∧ q, read as "p and q", you need a proof of p and a proof of q. In Lean, And is a structure with two fields, and its constructor is And.intro:

lean
variable (p q : Prop)

example (hp : p) (hq : q) : p ∧ q := And.intro hp hq

example (hp : p) (hq : q) : p ∧ q := ⟨hp, hq⟩

example (h : p ∧ q) : p := h.left

example (h : p ∧ q) : q ∧ p := ⟨h.right, h.left⟩

example : 1 = 1 ∧ 2 + 2 = 4 := ⟨rfl, rfl⟩

This file compiles with no output, which means every proof was accepted. The line variable (p q : Prop) declares two arbitrary propositions that the examples can use, so you do not repeat them each time.

Walk through the examples. And.intro hp hq builds a proof of p ∧ q from the two parts. The angle brackets ⟨hp, hq⟩ are the anonymous constructor: Lean knows the expected type is an And, so it uses the right constructor for you. In VS Code and the playground you type them as \< and \>, and the ∧ symbol as \and.

Going the other way, h.left and h.right pull the two proofs back out of h : p ∧ q, just like p.x on a Point. Combining both directions gives the swap ⟨h.right, h.left⟩, a proof of q ∧ p. The last example proves a concrete "and" by giving rfl for each side.

Or: one proof, tagged with a side

To prove p ∨ q, read as "p or q", you only need one side, but you must say which. Or is an inductive type with two constructors, Or.inl for the left side and Or.inr for the right:

lean
variable (p q : Prop)

example (hp : p) : p ∨ q := Or.inl hp

example (hq : q) : p ∨ q := Or.inr hq

example : 1 = 1 ∨ 1 = 2 := Or.inl rfl

example (h : p ∨ q) : q ∨ p :=
  match h with
  | Or.inl hp => Or.inr hp
  | Or.inr hq => Or.inl hq

This also compiles with no output. Or.inl hp says "I am proving the left side, and here is the proof". 1 = 1 ∨ 1 = 2 is proved by choosing the left side, which is true.

Using an "or" is the interesting part. If someone hands you h : p ∨ q, you do not know which side holds. So you handle both, with match, exactly like matching on a Shape. In each branch you get the proof for that side, and here you put it on the opposite side to prove q ∨ p. Type ∨ as \or.

Choosing the wrong side fails, because you cannot supply a proof of something false:

lean
example : 1 = 1 ∨ 1 = 2 := Or.inr rfl

Lean says:

lean
-- error: Application type mismatch: The argument
--   rfl
-- has type
--   ?m.16 = ?m.16
-- but is expected to have type
--   1 = 2

rfl can only prove that something equals itself, and Lean found no way to make 1 = 2 fit that shape.

Implies: a function from proofs to proofs

Here is the idea that makes everything click. "p implies q", written p → q, is the same arrow you used for function types. A proof of p → q is a function that turns any proof of p into a proof of q:

lean
variable (p q r : Prop)

example (hp : p) : q → p := fun _ => hp

example (hpq : p → q) (hqr : q → r) : p → r :=
  fun hp => hqr (hpq hp)

theorem and_swap : p ∧ q → q ∧ p :=
  fun h => ⟨h.right, h.left⟩

#check and_swap

Lean prints:

lean
-- and_swap (p q : Prop) : p ∧ q → q ∧ p

The first example proves q → p given a proof of p: it is a function that ignores its input, written fun _ => hp, and returns hp.

The second example chains two implications. If hpq turns proofs of p into proofs of q, and hqr turns proofs of q into proofs of r, then fun hp => hqr (hpq hp) turns proofs of p into proofs of r. That is ordinary function composition. Using an implication is just calling a function.

The and_swap theorem packages the swap from earlier as a named result. Notice in the #check output that p and q became arguments automatically, because the theorem used variables declared with variable.

Animation: And is a pair of proofs, Or is one proof tagged with its side, and implies is a function from proofs to proofs.

Term mode proofs

Every proof in this lesson was written as a term: you built the proof directly with constructors, functions and match. This is called term mode. It is the most direct view of what a proof really is, and reading term proofs helps you understand what Lean is checking.

For longer proofs, writing the whole term by hand gets tiring. Lean also has tactic mode, where you build the same term step by step with commands, and Lean shows you what is left to prove after each step. The next part of the course covers tactics, and you will find they produce exactly the kind of terms you wrote here.

Try this

Paste into https://live.lean-lang.org:

lean
variable (p q r : Prop)

example (h : p ∧ q) : q := h.right

example (hq : q) : p ∨ q := Or.inr hq

example (hpq : p → q) (hp : p) : q := hpq hp

Then prove example (hp : p) (hq : q) (hr : r) : p ∧ (q ∧ r) using nested angle brackets, and example (hp : p) : p ∨ (q ∧ r) using Or.inl.

Sources

Try it yourselfMedium

Move proofs between and, or and implies

Prove each statement. You can use term mode or tactics. For the or statement, look at which side you have and build the other order.

example (p q r : Prop) (h : p ∧ (q ∧ r)) : (p ∧ q) ∧ r := by
  sorry

example (p q : Prop) (h : p ∨ q) : q ∨ p := by
  sorry

example (p q r : Prop) (hpq : p → q) (hqr : q → r) : p → r := by
  sorry
Open in the Lean web editor

The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.

The worked solution unlocks after your first attempt.

Saved in this browser. Pass the quiz while signed in to count it for the certificate.

Check what you learned

Answer all 3 questions. You need at least 2 right to pass.

1. Which term proves p ∧ q from hp : p and hq : q?

2. How do you use a hypothesis h : p ∨ q?

3. What is a proof of p → q?