Lean and its type theory — chapter 4
Each type former again in four steps: formation, construction, use, computation.
def aPair : (n : ℕ) × Fin (n + 1) :=
⟨3, Fin.last 3⟩
#eval3 aPair.1 -- 3
3example (p : ℕ × ℕ) : p = (p.1, p.2) := rfl
Formation: (x : α) × β x, also Σ x, β x; α × β if nothing depends
Construction: ⟨a, b⟩
Use: .1, .2 (also .fst, .snd)
Computation: ⟨a, b⟩.1 ⟶ a
In sets: the disjoint union \coprod_x \beta(x).
Fintype.card α is the number of elements; decide computes both sides.
-- Σ: 1 + 2 + 3 + 4
example : Fintype.card
((n : Fin 4) × Fin (n + 1)) = 10 := by⊢ Fintype.card ((n : Fin 4) × Fin (↑n + 1)) = 10 decideAll goals completed! 🐙
-- Π: 1 · 2 · 3 · 4
example : Fintype.card
((n : Fin 4) → Fin (n + 1)) = 24 := by⊢ Fintype.card ((n : Fin 4) → Fin (↑n + 1)) = 24 decideAll goals completed! 🐙
non-dependent | dependent | for propositions |
|---|---|---|
|
|
|
|
|
|
Like a Σ-type, but the second component is a proof: P x : Prop. In Lean's Prelude:
structure Subtype {α : Sort u} (p : α → Prop) where
val : α
property : p val
Formation: {x : α // P x}, i.e. Subtype P
Construction: ⟨a, h⟩ with h : P a
Use: .val, .property
Computation: ⟨a, h⟩.val ⟶ a
def BigNat : Type := {n : ℕ // n > 5}
def seven : BigNat := ⟨7, by⊢ 7 > 5 decideAll goals completed! 🐙⟩
#eval7 seven.val -- 7
7example (a b : BigNat) (h : a.val = b.val) : a = b :=
Subtype.ext h
Proof irrelevance: two elements are equal iff their values are (Subtype.ext).
Many Mathlib objects are subtypes, e.g. {n : ℕ // Nat.Prime n}.
Fin n carries the same data as {i : ℕ // i < n}, but is a type of its own.
A set s : Set α selects elements, which keep their type α.
The subtype {x : α // P x} is a new type.
A type family is a function that returns a type.
def BoolFamily : Bool → Type
| true => ℕ
| false => String
def fiber (f : ℕ → ℕ) (b : ℕ) : Type :=
{a : ℕ // f a = b}
def fiberExample : fiber (· * 2) 10 := ⟨5, rfl⟩
n ↦ (Fin n → α) is a type family: its value at n is the type of vectors of length n. ![a, b, c] is the function on Fin 3 with values a, b, c.
def zeros : (n : ℕ) → (Fin n → ℕ) := fun _ _ => 0
def myVec : Fin 3 → ℕ := ![10, 20, 30]
#eval30 myVec 2
30def myMatrix : Fin 2 → Fin 3 → ℕ :=
![![1, 2, 3], ![4, 5, 6]]
#eval6 myMatrix 1 2
6Index 5 of a 3-element vector does not exist. A dimension mismatch is a type error.
inductive Vec (α : Type) : ℕ → Type where
| nil : Vec α 0
| cons {n : ℕ} : α → Vec α n →
Vec α (n + 1)
def Vec.head {α : Type} {n : ℕ} :
Vec α (n + 1) → α
| .cons a _ => a
α is a parameter, fixed for all constructors. The ℕ is an index: each constructor chooses it.
head needs no case for nil: its index 0 is not n + 1.
inductive MyEven : ℕ → Prop where
| zero : MyEven 0
| add_two (n : ℕ) : MyEven n → MyEven (n + 2)
example : ¬ MyEven 1 := by⊢ ¬MyEven 1
intro hh:MyEven 1⊢ False
cases hAll goals completed! 🐙
The constructors are the only ways to prove MyEven n. cases h keeps the constructors whose index fits.
≤ on ℕ and = are inductive predicates of this kind.
A proposition P : Prop is a type; a proof is a term h : P. The connectives are the type formers, with values in Prop.
example (P : Prop) : (¬P) = (P → False) := rfl
example : True := True.intro
example (C : Prop) (h : False) : C := nomatch h
→, ∀: function types; ¬P is P → False
True: one constructor; False: none, so nomatch needs no branch
A proof of P → Q is a function from proofs of P to proofs of Q.
example (P Q : Prop) (hP : P) (hPQ : P → Q) :
Q :=
hPQ hP
example (P Q R : Prop) :
(P → Q) → (Q → R) → (P → R) :=
fun hPQ hQR hP => hQR (hPQ hP)
Modus ponens is application; chaining is composition. In tactic mode: intro hP, then exact ….
example (P Q : Prop) (h : P ∧ Q) : Q ∧ P :=
⟨h.2, h.1⟩
example (P Q R : Prop) (h : P ∨ Q)
(hPR : P → R) (hQR : Q → R) : R :=
match h with
| Or.inl hP => hPR hP
| Or.inr hQ => hQR hQ
And: a structure, like α × β. Or: an inductive type with Or.inl, Or.inr.
A match on P ∨ Q may only prove propositions: which side holds cannot be turned into data.
example : ∃ n : ℕ, n > 5 := ⟨7, by⊢ 7 > 5 decideAll goals completed! 🐙⟩
example (P Q : ℕ → Prop) (h : ∃ n, P n ∧ Q n) :
∃ n, Q n :=
match h with
| ⟨n, hn⟩ => ⟨n, hn.2⟩
Exists: one constructor, a witness and a proof. Like the subtype {n : ℕ // n > 5}, but in Prop: the witness can be used in proofs, not computed with.
∃ is the propositional truncation of Σ: it forgets which witness was given.
proposition | type former | a proof is |
|---|---|---|
| Π-type | a function |
|
structure, like |
|
| inductive, like a disjoint union |
|
| inductive, like a Σ-type |
|
| one, no constructor |
|
|
|
a function into |
Quot r: the type α seen up to a relation r. In Mathlib: ℝ (chapter 2.1), Multiset α. ℤ, ℚ, ZMod n are defined by normal forms instead.
Formation: Quot r
Construction: Quot.mk r a, the class of a
Use: Quot.lift f h, with h a proof that f respects r
Computation: Quot.lift f h (Quot.mk r a) ⟶ f a
Quot.lift is the mathematician's "check that this is well-defined".
Quot is a primitive
Quot has no definition: #print Quot says "Quotient primitive". The kernel adds four constants:
Quot : (α → α → Prop) → Sort u
Quot.mk : (r : α → α → Prop) → α → Quot r
Quot.lift : (f : α → β) →
(∀ a b, r a b → f a = f b) → Quot r → β
Quot.ind : (∀ a, P (Quot.mk r a)) → ∀ q, P q
and one computation rule: Quot.lift f h (Quot.mk r a) ⟶ f a.
Quot.sound : r a b → Quot.mk r a = Quot.mk r b
#print'Quot.sound' depends on axioms: [Quot.sound] axioms Quot.sound -- [Quot.sound]
Related elements have the same class. This does not follow from the four constants.
They all hold for Quot r := α, Quot.mk r a := a, Quot.lift f h := f: nothing glued.
So Lean assumes it as an axiom.
Then Quot r is α modulo the equivalence relation generated by r; r itself need not be one.
funext from Quot.soundtheorem myFunext {α β : Type*} {f g : α → β}
(h : ∀ x, f x = g x) : f = g := byα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g x⊢ f = g
let eqv (f g : α → β) := ∀ x, f x = g xα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g x⊢ f = g
have hq : Quot.mk eqv f = Quot.mk eqv g := Quot.sound hα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv g⊢ f = g
have hf : f = (fun x =>
Quot.lift (fun f => f x) (fun _ _ h => h x)
(Quot.mk eqv f)) := rflα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv ghf:f = fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)⊢ f = g
have hg : g = (fun x =>
Quot.lift (fun g => g x) (fun _ _ h => h x)
(Quot.mk eqv g)) := rflα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv ghf:f = fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)hg:g = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g)⊢ f = g
rw [hf,α:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv ghf:f = fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)hg:g = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g)⊢ (fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)) = g hg,α:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv ghf:f = fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)hg:g = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g)⊢ (fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)) = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g) hqα:Type u_1β:Type u_2f:α → βg:α → βh:∀ (x : α), f x = g xeqv:(α → β) → (α → β) → Prop := fun f g => ∀ (x : α), f x = g xhq:Quot.mk eqv f = Quot.mk eqv ghf:f = fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv f)hg:g = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g)⊢ (fun x => Quot.lift (fun f => f x) ⋯ (Quot.mk eqv g)) = fun x => Quot.lift (fun g => g x) ⋯ (Quot.mk eqv g)]All goals completed! 🐙
#print'SlidesTypeFormers.myFunext' depends on axioms: [Quot.sound] axioms myFunext -- [Quot.sound]
rfl proves hf
Lean computes the right side of hf until it is f.
Computation rule: Quot.lift (fun f => f x) _ (Quot.mk eqv f) ⟶ (fun f => f x) f ⟶ f x
η-rule: fun x => f x ≡ f
After rw [hq] both sides agree. The only axiom used is Quot.sound.
def SameParity (a b : ℤ) : Prop := a % 2 = b % 2
def Parity : Type := Quot SameParity
def Parity.mk (a : ℤ) : Parity := Quot.mk SameParity a
example : Parity.mk 7 = Parity.mk 1 :=
Quot.sound rfl
def Parity.rem : Parity → ℤ :=
Quot.lift (fun a => a % 2) (fun _ _ h => h)
example : Parity.rem (Parity.mk 17) = 1 := rfl
For the remainder, the proof of well-definedness is the relation itself.
Mathlib's ZMod 2 is not this quotient: it is Fin 2, so #eval and decide work.
type | formation | construction | use | computation |
|---|---|---|---|---|
function |
|
|
| β |
inductive |
| constructors | pattern matching | ι |
structure |
|
|
|
|
Σ-type |
|
|
|
|
subtype |
|
|
|
|
indexed family |
| constructors |
| ι |
quotient |
|
|
|
|
Chapter 1.5 states these steps as the rules of Lean's type theory.
1902 — Russell's set R = \{x : x \notin x\} breaks Frege's Grundgesetze.
1908 — Russell's repair: types. x ∈ x is not false, it is not a statement.
1932 — Church's lambda calculus. Kleene and Rosser derive a contradiction (1935).
1934 — Curry: the types of the combinators are the axioms of propositional logic.
1940 — Church's repair: the simply typed lambda calculus.
1967 — de Bruijn's Automath: types that depend on terms.
1969 — Howard: propositions are types, proofs are terms.
1971 — Martin-Löf's type theory has Type : Type. Girard derives a contradiction (1972); repair: a hierarchy of universes.
1985 — Coquand, Huet: the Calculus of Constructions. 1990 — Paulin-Mohring: inductive types. Lean's type theory is a variant.
Three times: a system allows self-application, a contradiction follows, types forbid it.
(x : α) × β x: dependent pairs, a disjoint union. Subtypes: the second component is a proof.
Type families are functions into a universe; with inductive, the constructors fix the index.
A proof of P → Q is a function; intro builds it, exact supplies a term.
The connectives are these type formers in Prop: ∧ a structure, ∨, ∃, False inductive types.
Quot r: a type up to a relation, with the axiom Quot.sound. ℤ/nℤ by hand.