Pairs, subtypes, families, quotients

Lean and its type theory — chapter 4

Each type former again in four steps: formation, construction, use, computation.

Pairs and Σ-types

def aPair : (n : ℕ) × Fin (n + 1) := ⟨3, Fin.last 3⟩ #eval aPair.1 -- 3
3
example (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).

Sum and product

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 decide -- Π: 1 · 2 · 3 · 4 example : Fintype.card ((n : Fin 4) → Fin (n + 1)) = 24 := by decide

non-dependent

dependent

for propositions

α → β

(x : α) → β x

∀ x, P x

α × β

(x : α) × β x

∃ x, P x

Subtypes

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

Elements of a subtype

def BigNat : Type := {n : ℕ // n > 5} def seven : BigNat := ⟨7, by decide⟩ #eval seven.val -- 7
7
example (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).

Subtypes and sets

  • 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.

Type families

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⟩

Vectors and matrices

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] #eval myVec 2
30
def myMatrix : Fin 2 → Fin 3 → ℕ := ![![1, 2, 3], ![4, 5, 6]] #eval myMatrix 1 2
6

Index 5 of a 3-element vector does not exist. A dimension mismatch is a type error.

Indexed families

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 predicates

inductive MyEven : ℕ → Prop where | zero : MyEven 0 | add_two (n : ℕ) : MyEven n → MyEven (n + 2) example : ¬ MyEven 1 := by intro h cases h

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.

Propositions as types

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

Proofs of implications

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 ….

Conjunction and disjunction

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.

The existential quantifier

example : ∃ n : ℕ, n > 5 := ⟨7, by decide⟩ 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.

The dictionary

proposition

type former

a proof is

P → Q, ∀ x, P x

Π-type

a function

P ∧ Q

structure, like α × β

⟨hP, hQ⟩

P ∨ Q

inductive, like a disjoint union

Or.inl hP, Or.inr hQ

∃ x, P x

inductive, like a Σ-type

⟨a, h⟩

True, False

one, no constructor

True.intro; none

¬P

P → False

a function into False

Quotient types

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.

One axiom

Quot.sound : r a b → Quot.mk r a = Quot.mk r b
#print 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.sound

theorem myFunext {α β : Type*} {f g : α → β} (h : ∀ x, f x = g x) : f = g := by let eqv (f g : α → β) := ∀ x, f x = g x have hq : Quot.mk eqv f = Quot.mk eqv g := Quot.sound h have hf : f = (fun x => Quot.lift (fun f => f x) (fun _ _ h => h x) (Quot.mk eqv f)) := rfl have hg : g = (fun x => Quot.lift (fun g => g x) (fun _ _ h => h x) (Quot.mk eqv g)) := rfl rw [hf, hg, hq] #print axioms myFunext -- [Quot.sound]

Why 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.

The integers mod 2

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.

The four steps at a glance

type

formation

construction

use

computation

function

(x : α) → β x

fun x => e

f a

β

inductive

inductive T

constructors

pattern matching

ι

structure

structure S

⟨a, b⟩

s.x

(S.mk a b).x ⟶ a

Σ-type

(x : α) × β x

⟨a, b⟩

.1, .2

⟨a, b⟩.1 ⟶ a

subtype

{x : α // P x}

⟨a, h⟩

.val

⟨a, h⟩.val ⟶ a

indexed family

inductive T : I → Sort u

constructors

cases

ι

quotient

Quot r

Quot.mk r a

Quot.lift f h

… ⟶ f a

Chapter 1.5 states these steps as the rules of Lean's type theory.

Some history

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.

Summary

  • (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.