2. Logic in Lean - Part 2

Your mission today is to wrap up the remaining bits of logic and move on to doing some “actual math”. Remember to always save your work. You might find the Glossary of tactics page and the Pretty symbols page useful.

Before we move on to new stuff, let’s understand what we did yesterday.

2.1. Behind the scenes

A note on brackets: It is not uncommon to compose half a dozen functions in Lean. The brackets get really messy and unwieldy. As such, Lean will often drop the brackets by following these conventions.

  • The function P → Q → R → S stands for P → (Q → (R → S)).

  • The expression a + b + c + d stands for ((a + b) + c) + d.

An easy way to remember this is that arrows are bracketed on the right and binary operators on the left.

2.1.1. Proof irrelevance

It might feel a bit weird to say that a proposition has proofs as its inhabitants. Proofs can get huge and it seems unnecessary to have to remember not just the statement but also its proof. This is something we don’t normally do in math. To hide this complication, in type theory there is an axiom, called proof irrelevance, which says that if P : Prop and hp1 hp2 : P then hp1 = hp2. Taking our analogy with sets further, you can think of a proposition as a set which is either empty or contains a single element (false or true). In fact, in some forms of type theory (e.g. homotopy type theory) this is taken as the definition of propositions. This is of course not true for general types. For example, 0 : ℕ ≠ 1 : ℕ.

2.1.2. Proofs as functions

Every time you successfully construct a proof of a theorem, say

theorem tautology (P : Prop) : P → P := by
  intro hp
  exact hp

Lean constructs a proof term tautology : ∀ P : Prop, P → P (you can see this by typing #check tautology).

In type theory, the for all quantifier, ∀, is a generalized function, called a dependent function. For all practical purposes, we can think of tautology as having the type (P : Prop) → (P → P). Note that this is not a function in the classical sense of the word because the codomain (P → P) depends on the input variable P. If Q : Prop, then tautology Q is a term of type Q → Q.

Consider a theorem with multiple hypotheses, say

theorem hello_world (hp : P) (hq : Q) (hr : R) : S

Once we provide a proof of it, Lean will create a proof term hello_world : (hp : P) → (hq : Q) → (hr : R) → S. So if we have terms hp' : P, hq' : Q, hr' : R then hello_world hp' hq' hr' (note the convenient lack of brackets) will be a term of type S.

Once constructed, any term can be used in a later proof. For example,

example (P Q : Prop) : (P → Q) → (P → Q) := by
  exact tautology (P → Q)

This is how Lean simulates mathematics. Every time you prove a theorem using tactics a proof term gets created. Because of proof irrelevance, Lean forgets the exact content of the proof and only remembers its type. All the proof terms can then be used in later proofs. All of this falls under the giant umbrella of the Curry–Howard correspondence.

We’ll now continue our study of the remaining logical operators: and (∧), or (∨), if and only if (↔), for all (∀), there exists (∃).

2.2. And / Or

The operators and (∧) and or (∨) are very easy to use in Lean. Given a term hpq : P ∧ Q, there are tactics that let you create terms hp : P and hq : Q, and vice versa. Similarly for P ∨ Q, with a subtle change (see below).

Note that when multiple goals are open, you are trying to solve the topmost goal.

obtain

obtain is a general tactic that breaks a complicated term into simpler ones.

If hpq is a term of type P ∧ Q, then obtain ⟨hp, hq⟩ := hpq breaks it into hp : P and hq : Q.

If fg is a term of type P ↔ Q, then obtain ⟨f, g⟩ := fg breaks it into f : P → Q and g : Q → P.

If hpq is a term of type P ∨ Q, then obtain hp | hq := hpq creates two goals and adds the hypothesis hp : P in one and hq : Q in the other.

constructor

constructor is a general tactic that breaks a complicated goal into simpler ones.

If the target of the current goal is P ∧ Q, then constructor breaks up the goal into two goals with targets P and Q.

If the target of the current goal is P ↔ Q, then constructor breaks up the goal into two goals with targets P → Q and Q → P.

left

If the target of the current goal is P ∨ Q, then left changes the target to P.

right

If the target of the current goal is P ∨ Q, then right changes the target to Q.

-- --------------------------------------------------------------------------
--
-- ``obtain``
--
--   ``obtain`` is a general tactic that breaks up complicated terms.
--   If ``hpq`` is a term of type ``P ∧ Q`` or ``P ↔ Q``, then use
--   ``obtain ⟨hp, hq⟩ := hpq``.
--   If ``hpq`` is a term of type ``P ∨ Q``, then use
--   ``obtain hp | hq := hpq``.
--
-- ``constructor``
--
--   If the target of the current goal is ``P ∧ Q`` or ``P ↔ Q``, then use
--   ``constructor``.
--
-- ``left``/``right``
--
--   If the target of the current goal is ``P ∨ Q``, then use
--   either ``left`` or ``right`` (choose wisely).
--
-- ``exfalso``
--
--   Changes the target of the current goal to ``False``.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------

example (P Q : Prop) : P ∧ Q → Q ∧ P := by
  sorry

example (P Q : Prop) : P ∨ Q → Q ∨ P := by
  sorry

example (P Q R : Prop) : P ∧ False ↔ False := by
  sorry

theorem principle_of_explosion (P Q : Prop) : P ∧ ¬ P → Q := by
  sorry

2.3. Quantifiers

As mentioned in the introduction, the for all quantifier, ∀, is a generalization of a function. As such the tactics for dealing with ∀ are the same as those for →.

have

If hp is a term of type ∀ x : X, P x and y is a term of type X then have hpy := hp y creates a hypothesis hpy : P y.

intro

If the target of the current goal is ∀ x : X, P x, then intro x creates a hypothesis x : X and changes the target to P x.

The there exists quantifier, ∃, in type theory is very intuitive. If you want to prove a statement ∃ x : X, P x then you need to provide a witness. If you have a term hp : ∃ x : X, P x then from this you can extract a witness.

obtain

If hp is a term of type ∃ x : X, P x, then obtain ⟨x, key⟩ := hp breaks it into x : X and key : P x.

use

If the target of the current goal is ∃ x : X, P x and y is a term of type X, then use y changes the target to P y and tries to close the goal.

Note that divisibility, a ∣ b, is itself an existential statement (∃ k, b = a * k), so both obtain and use work on it as well.

Finally, we know enough Lean tactics to start doing some fun stuff.

2.3.1. Barber paradox

Let’s disprove the “barber paradox” due to Bertrand Russell. The claim is that in a certain town there is a (male) barber that shaves all the men who do not shave themselves. (Why is this a paradox?) Prove that this is a contradiction. Here are some hints if you get stuck.

-- --------------------------------------------------------------------------
--
-- ``by_cases``
--
--   If ``P`` is a proposition, then ``by_cases hp : P`` creates two goals,
--     the first with a hypothesis ``hp : P`` and
--     second with a hypothesis ``hp : ¬ P``.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------

-- men is a type.
-- x : men means x is a man in the town
-- shaves x y is inhabited if x shaves y

variable (men : Type) (barber : men)
variable (shaves : men → men → Prop)

example : ¬ (∀ x : men, shaves barber x ↔ ¬ shaves x x) := by
  sorry

2.3.2. Mathcampers singing paradox

Assume that the main lounge is non-empty. At a fixed moment in time, there is someone in the lounge such that, if they are singing, then everyone in the lounge is singing. (See hints).

-- --------------------------------------------------------------------------
--
-- ``by_cases``
--
--   If ``P`` is a proposition, then ``by_cases hp : P`` creates two goals,
--     the first with a hypothesis ``hp : P`` and
--     second with a hypothesis ``hp : ¬ P``.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------

-- camper is a type.
-- If x : camper then x is a camper in the main lounge.
-- singing x is inhabited if x is singing

theorem math_campers_singing_paradox
    (camper : Type)
    (singing : camper → Prop)
    (alice : camper) -- making sure that there is at least one camper in the lounge
    : ∃ x : camper, (singing x → (∀ y : camper, singing y)) := by
  sorry

2.3.3. Relationship conundrum

A relation r on a type X is a map r : X → X → Prop. We say that x is related to y if r x y is inhabited.

  • r is reflexive if ∀ x : X, x is related to itself.

  • r is symmetric if ∀ x y : X, x is related to y implies y is related to x.

  • r is transitive if ∀ x y z : X, x is related to y and y is related to z implies x is related to z.

  • r is connected if for all x : X there is a y : X such that x is related to y.

Show that if a relation is symmetric, transitive, and connected, then it is also reflexive.

import Mathlib.Tactic

variable (X : Type)

theorem reflexive_of_symmetric_transitive_and_connected
    (r : X → X → Prop)
    (h_symm : ∀ x y : X, r x y → r y x)
    (h_trans : ∀ x y z : X, r x y → r y z → r x z)
    (h_connected : ∀ x, ∃ y, r x y) :
    (∀ x : X, r x x) := by
  sorry

2.4. Proving “trivial” statements

In mathlib, divisibility for natural numbers is defined as the following proposition.

a ∣ b := (∃ k : ℕ, b = a * k)

For example, 2 | 4 will be a proposition ∃ k : ℕ, 4 = 2 * k. Very important. The statement 2 | 4 is not saying that “2 divides 4 is true”. It is simply a proposition that requires a proof.

Similarly, the mathlib library also contains a definition of prime. In mathlib, Nat.Prime p is defined as Irreducible p, a general notion of primality that makes sense in many number systems, not just ℕ. Fortunately, mathlib also proves that this is equivalent to the definition you are used to (see Nat.prime_def):

theorem Nat.prime_def {p : ℕ} :
  Nat.Prime p ↔
    2 ≤ p                                     -- p is at least 2
    ∧                                         -- and
    ∀ (m : ℕ), m ∣ p → m = 1 ∨ m = p          -- if m divides p, then m = 1 or m = p.

Same as with divisibility, for every natural number n, Nat.Prime n is a proposition. So that Nat.Prime 101 requires a proof. It is possible to go down the rabbit hole and prove it using just the axioms of natural numbers. However, this might come at the cost of your sanity. Fortunately, there are tactics in Lean for proving trivial proofs such as these.

norm_num

norm_num is Lean’s calculator. If the target has a proof that involves only numbers and arithmetic operations, then norm_num will close this goal.

If hp : P is an assumption then norm_num at hp tries to use simplify hp using basic arithmetic operations.

ring

ring is Lean’s symbolic manipulator. If the target has a proof that involves only algebraic operations, then ring will close the goal.

If hp : P is an assumption then ring at hp tries to use simplify hp using basic algebraic operations.

linarith

linarith is Lean’s inequality solver. It uses the hypotheses in context to prove goals that follow from them by adding up linear (in)equalities. It cannot handle products of variables such as x * y.

omega

omega is Lean’s solver for linear arithmetic over ℕ and ℤ. It uses all the hypotheses in context and either closes the goal or fails. Unlike linarith it understands integers, so it knows things like x < 1 → x = 0 and can handle ℕ subtraction, / and % by numerals, and ≠. It cannot handle products of variables such as x * y.

simp

simp is a very complex tactic that tries to use theorems from the mathlib library to close the goal. You should only ever use simp to close a goal because its behavior changes as more theorems get added to the library.

import Mathlib.Tactic

-- --------------------------------------------------------------------------
--
-- ``norm_num``
--
--   Useful for arithmetic.
--
-- ``ring``
--
--   Useful for basic algebra.
--
-- ``linarith``
--
--   Useful for inequalities.
--
-- ``omega``
--
--   Useful for linear arithmetic over ``ℕ`` and ``ℤ``.
--
-- ``simp``
--
--   Complex simplifier. Use only to close goals.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------

example : 1 > 0 := by
  sorry

example (m a b : ℕ) : m ^ 2 + (a + b) * m + a * b = (m + a) * (m + b) := by
  sorry

example : 101 ∣ 2020 := by
  sorry


#check Nat.prime_def
example : Nat.Prime 101 := by
  sorry

-- you will need the definition
-- a ∣ b := (∃ k : ℕ, b = a * k)
example (m a b : ℕ) : m + a ∣ m ^ 2 + (a + b) * m + a * b := by
  sorry

-- ``Nat.prime_def`` is a proof of ``Nat.Prime p ↔ (2 ≤ p ∧ ...)``.
-- Its first component ``Nat.prime_def.1`` is the ``→`` direction,
-- so try ``have h := Nat.prime_def.1 hp`` to get started
example (p : ℕ) (hp : Nat.Prime p) : ¬ (p = 1) := by
  sorry

-- if none of the simplifiers work, try doing ``contrapose!``
-- sometimes the simplifiers need a little help
example (n : ℕ) : 0 < n ↔ n ≠ 0 := by
  sorry

2.5. Solutions

Worked solutions to all of today’s exercises, in the order they appear above, are in Day2.lean.