1. Logic in Lean - Part 1¶
Today’s mission, should you choose to accept it, is to understand the philosophy of type theory (in Lean). Don’t try to memorize anything, that will happen automatically. Instead, try to r̶e̶a̶l̶i̶z̶e̶ ̶t̶h̶a̶t̶ ̶t̶h̶e̶r̶e̶ ̶i̶s̶ ̶n̶o̶ ̶s̶p̶o̶o̶n̶ do as many exercises as you can. Practice is the only way to learn a new programming language. And always save your work. The easiest way to do this is by bookmarking the Lean window in your web browser.
Lean is built on top of a logic system called type theory, which is an alternative to set theory.
In type theory, instead of elements we have terms and every term has a type.
When translated to math, terms can be either mathematical objects, functions, propositions, or proofs.
The notation x : X stands for “x is a term of type X” or “x is an inhabitant of X”.
For the most part, you can think of a type as a set and terms as elements of the set.
1.1. Propositions as types¶
In set theory, a proposition is any statement that has the potential of being true or false, like 2 + 2 = 4, 2 + 2 = 5, “Fermat’s last theorem”, or “Riemann hypothesis”.
In type theory, there is a special type called Prop whose inhabitants are propositions.
Furthermore, each proposition P is itself a type and the inhabitants of P are its proofs!
P : Prop -- P is a proposition
hp : P -- hp is a proof of P
As such, in type theory “producing a proof of P” is the same as “producing a term of type P”
and so a proposition P is true if there exists a term hp of type P.
Notation. Throughout these notes, P, Q, R, ... will denote propositions.
1.1.1. Implication¶
In set theory, the proposition P ⇒ Q (“P implies Q”) is true if either both P and Q are true or if P is false.
In type theory, a proof of an implication P ⇒ Q is just a function f : P → Q.
Given a function f : P → Q, every proof hp : P produces a proof f(hp) : Q.
If P is false then P is empty, and there exists an empty function from an empty type to any type.
Hence, in type theory we use → to denote implication.
1.1.2. Negation¶
In type theory, there is a special proposition False : Prop which has no proof (hence is empty).
The negation of a proposition ¬ P is the implication P → False.
Such a function exists if and only if P itself is empty (empty function), hence P → False is inhabited if and only if P is empty which justifies using it as the definition of ¬ P.
- To summarize:
Proving a proposition
Pis equivalent to producing an inhabitanthp : P.Proving an implication
P → Qis equivalent to producing a functionf : P → Q.The negation,
¬ P, is defined as the implicationP → False.
1.1.3. Propositions in Lean¶
In Lean, a proposition and its proof are written using the following syntax.
import Mathlib.Tactic
theorem fermats_last_theorem
(n : ℕ)
(n_gt_2 : n > 2) :
¬ (∃ x y z : ℕ, (x ^ n + y ^ n = z ^ n) ∧ (x ≠ 0) ∧ (y ≠ 0) ∧ (z ≠ 0)) := by
sorry
Let us parse the above statement. (Unlike the hypotheses and target, which Lean happily lets you
spread across as many lines as you like, the proof itself is whitespace-sensitive: once you’re
inside a by block, each tactic goes on its own line, and how far a line is indented determines
which proof (or sub-proof) it belongs to. We’ll see why this matters once our proofs have more
than one tactic in them.)
import Mathlib.Tacticloads the mathlib4 library. It’s what gives us notation likeℕand every tactic we’ll use, so most Lean files start with someimportline — you can safely ignore it for now.fermats_last_theoremis the name of the theorem.(n : ℕ)and(n_gt_2 : n > 2)are the two hypotheses. The former saysnis a natural number and the latter says thatn_gt_2is a proof ofn > 2.:is the delimiter between hypotheses and targets¬ (∃ x y z : ℕ, (x^n + y^n = z^n) ∧ (x ≠ 0) ∧ (y ≠ 0) ∧ (z ≠ 0))is the target of the theorem.:= by ...contains the proof. When you start your proof, Lean opens up a goal window for you to keep track of hypotheses and targets. Your goal is to produce a term that has the type of the target.-- example of Lean goal window n : ℕ -- hypothesis 1 n_gt_2 : n > 2 -- hypothesis 2 ⊢ ¬∃ x y z : ℕ, x ^ n + y ^ n = z ^ n ∧ x ≠ 0 ∧ y ≠ 0 ∧ z ≠ 0 -- target
The commands you write after
byare called tactics, one per line.sorryis an example of a tactic.
Even though they are not explicitly displayed, all the theorems in the Lean library are also hypotheses that you can use to close the goal.
1.2. Implications in Lean¶
We’ll start learning tactics by proving implications in Lean. In the following sections, there are tables describing what a tactic does. Solve the following exercises to see the tactics in action.
The first two tactics we’ll learn are exact and intro.
|
If
Mathematically, this saying “this is exactly what we were required to prove”. |
|
If the target of the current goal is a function Mathematically, this is saying that in order to define a function from |
-- --------------------------------------------------------------------------
--
-- ``exact``
--
-- If ``P`` is the target of the current goal and
-- ``hp`` is a term of type ``P``, then
-- ``exact hp`` will close the goal.
--
--
-- ``intro``
--
-- If the target of the current goal is a function ``P → Q``, then
-- ``intro hp`` will produce a hypothesis
-- ``hp : P`` and change the target to ``Q``.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------
theorem tautology (P : Prop) (hp : P) : P := by
sorry
theorem tautology' (P : Prop) : P → P := by
sorry
example (P Q : Prop) : (P → (Q → P)) := by
sorry
-- Can you find two different ways of proving the following?
example (P Q : Prop) : ((Q → P) → (Q → P)) := by
sorry
The next two tactics are have and apply.
|
If |
|
If the target of the current goal is Mathematically, this is equivalent to saying “because |
Often these two tactics can be used interchangeably.
Think of have as reasoning forward and apply as reasoning backward.
When writing a big proof, you often want a healthy combination of the two that makes the proof readable.
-- --------------------------------------------------------------------------
--
-- ``have``
--
-- If ``f`` is a term of type ``P → Q`` and
-- ``hp`` is a term of type ``P``, then
-- ``have hq := f hp`` creates the hypothesis ``hq : Q`` .
--
--
-- ``apply``
--
-- If the target of the current goal is ``Q`` and
-- ``f`` is a term of type ``P → Q``, then
-- ``apply f`` changes target to ``P``.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------
example (P Q R : Prop) (hp : P) (f : P → Q) (g : Q → R) : R := by
sorry
example (P Q R S T U : Prop)
(hpq : P → Q)
(hqr : Q → R)
(hqt : Q → T)
(hst : S → T)
(htu : T → U) :
P → U := by
sorry
For the following exercises, recall that ¬ P is defined as P → False,
¬ (¬ P) is (P → False) → False, and so on.
Here are some hints if you get stuck.
-- --------------------------------------------------------------------------
--
-- Recall that
-- ``¬ P`` is ``P → False``,
-- ``¬ (¬ P)`` is ``(P → False) → False``, and so on.
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------
theorem self_imp_not_not_self (P : Prop) : P → ¬ (¬ P) := by
sorry
theorem contrapositive (P Q : Prop) : (P → Q) → (¬ Q → ¬ P) := by
sorry
example (P : Prop) : ¬ (¬ (¬ P)) → ¬ P := by
sorry
1.3. Proof by contradiction¶
You can prove exactly one of the converses of the above three using just exact, intro, have, and apply.
Can you find which one?
-- --------------------------------------------------------------------------
--
-- You can prove exactly one of the following three using just
-- ``exact``, ``intro``, ``have``, and ``apply``.
--
-- Can you find which one?
--
-- --------------------------------------------------------------------------
theorem not_not_self_imp_self (P : Prop) : ¬ ¬ P → P := by
sorry
theorem contrapositive_converse (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := by
sorry
example (P : Prop) : ¬ P → ¬ ¬ ¬ P := by
sorry
This is because it is not true that ¬ ¬ P = P by definition, after all,
¬ ¬ P is (P → False) → False which is drastically different from P.
There is an extra axiom called the law of excluded middle which says that
either P is inhabited or ¬ P is inhabited (and there is no middle option)
and so P ↔ ¬ ¬ P.
This is the axiom that allows for proofs by contradiction.
Lean provides us the following tactics to use it.
|
Changes the target of the current goal to The name derives from “ex falso, quodlibet” which translates to “from contradiction, anything”. You should use this tactic when there are contradictory hypotheses present. |
|
If Mathematically, this is saying either |
|
If the target of the current goal is Mathematically, this is proof by contradiction. |
|
For example, if the target of the current goal is You can also push negations across a hypothesis |
|
If the target of the current goal is If the target of the current goal is Mathematically, this is replacing the target by its contrapositive. |
Even though the list is long, these tactics are almost all obvious.
The only two slightly unusual tactics are exfalso and by_cases.
You’ll see by_cases in action later.
For the following exercises, you only require exfalso, push Not, and contrapose!.
-- --------------------------------------------------------------------------
--
-- ``exfalso``
--
-- Changes the target of the current goal to ``False``.
--
-- ``push Not``
--
-- ``push Not`` simplifies negations in the target.
-- You can push negations across a hypothesis ``hp : P`` using
-- ``push Not at hp``.
--
--
-- ``contrapose!``
--
-- If the target of the current goal is ``P → Q``,
-- then ``contrapose!`` changes the target to ``¬ Q → ¬ P``.
--
-- If the target of the current goal is ``Q`` and
-- one of the hypotheses is ``hp : P``, then
-- ``contrapose! hp`` changes the target to ``¬ P`` and
-- changes the hypothesis to ``hp : ¬ Q``.
--
--
-- Delete the ``sorry`` below and replace them with a legitimate proof.
--
-- --------------------------------------------------------------------------
theorem not_not_self_imp_self (P : Prop) : ¬ ¬ P → P := by
sorry
theorem contrapositive_converse (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := by
sorry
example (P : Prop) : ¬ P → ¬ ¬ ¬ P := by
sorry
theorem principle_of_explosion (P Q : Prop) : P → (¬ P → Q) := by
sorry
1.4. Solutions¶
Worked solutions to all of today’s exercises, in the order they appear above, are in Day1.lean.
1.5. Final Remarks¶
You might be wondering, if type theory is so cool why have I not heard of it before?
Many programming languages highly depend on type theory (that’s where the term datatype comes from).
Once you define a term x : ℕ, a computer can immediately check that all the manipulations you do with x
are valid manipulations of natural numbers (so you don’t accidentally divide by 0 [1] , for example).
Unfortunately, this also means that the term 1 : ℕ is different from the term 1 : ℤ.
In Lean, if you do (1 : ℕ) - 2 you get 0 : ℕ but if you do (1 : ℤ) - 2 you get -1 : ℤ,
that’s because natural numbers and subtraction are not buddies.
Another issue is that (1 : ℕ) = (1 : ℤ) is not a valid statement in type theory.
This is not the end of the world though.
Lean allows you to coerce 1 : ℕ to 1 : ℤ if you want subtraction to work properly,
or 1 : ℕ to 1 : ℚ if you want division to work properly.
This, and a few other such things, is what drives most mathematicians away from type theory. But these things are only difficult when you’re first learning them. With practice, type theory becomes second nature, the same as set theory.
footnotes