Lean and its type theory — chapter 2
In Lean, types can depend on values.
x : α means: x is a term of type α. def name : α := x gives x a name.
def answer : ℕ := 42
def square : ℕ → ℕ := fun n => n * n
def zeroNeOne : Prop := 0 ≠ 1
-- a proposition that depends on n
def ltSucc : Prop := ∀ n : ℕ, n < n + 1
-- a type that depends on n: vectors in ℝⁿ
def RVec (n : ℕ) : Type := Fin n → ℝ
def zeroVec (n : ℕ) : RVec n := fun _ => 0
Fin n: the numbers i < n; its definition is on the slide on Fin n and ∧
h : P with P : Prop is a proof; a : α with α : Type u is data
A type is itself a term, so it has a type as well.
step | question | function | inductive type | structure |
|---|---|---|---|---|
formation | how is the type written? |
|
|
|
construction | how is a term built? |
|
|
|
use | how is a term used? |
|
|
|
computation | what if a term just built is used? |
|
a match on |
|
More type formers in chapter 1.4: pairs and Σ-types (x : α) × β x, subtypes {x : α // P x}, indexed families, quotients Quot r.
Types and terms come together. Chapter 1.5 turns the four steps into rules.
def four : ℕ := 2 + 2 -- names a term
theorem four_eq : four = 4 := rfl -- names a proof
example : four = 4 := rfl -- checks, no name
#checkSlidesTypes.four : ℕ four -- the type
SlidesTypes.four : ℕ#eval4 four -- the value
4#printdef SlidesTypes.four : ℕ :=
2 + 2 four -- the definition
def SlidesTypes.four : ℕ :=
2 + 2
Mathlib also accepts lemma for theorem.
#print shows the proof termtheorem swap_of_and (P Q : Prop) (h : P ∧ Q) :
Q ∧ P := byP:PropQ:Proph:P ∧ Q⊢ Q ∧ P
constructorP:PropQ:Proph:P ∧ Q⊢ QP:PropQ:Proph:P ∧ Q⊢ P
·P:PropQ:Proph:P ∧ Q⊢ Q exact h.2All goals completed! 🐙
·P:PropQ:Proph:P ∧ Q⊢ P exact h.1All goals completed! 🐙
#printtheorem SlidesTypes.swap_of_and : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
fun P Q h => ⟨h.right, h.left⟩ swap_of_and
-- theorem swap_of_and : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
-- fun P Q h => ⟨h.right, h.left⟩
theorem SlidesTypes.swap_of_and : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
fun P Q h => ⟨h.right, h.left⟩Tactics build a term.
universe | its terms, for example | lives in |
|---|---|---|
|
|
|
|
|
|
|
|
|
| … |
|
One rule: Sort u : Sort (u + 1). So Prop : Type.
{α : Type*} in a signature: α lies in some universe Type u, and Lean introduces the variable u itself.
Π-type (x : α) → β x: the type of the value depends on x. α → β is the case where it does not.
Formation: (x : α) → β x, or α → β
Construction: fun x => e, also fun x ↦ e
Use: application f a, no parentheses
Computation: (fun x => e) a reduces to e[x := a] (β)
example : (ℕ → ℕ) = ((n : ℕ) → ℕ) := rfl
#eval5 (fun n : ℕ => n + 1) 4 -- 5
5
def name (arguments) : resultType := body is short for def name : (arguments) → resultType := fun arguments => body.
def double (n : ℕ) : ℕ := 2 * n
example : double = fun n => 2 * n := rfl
def add (a b : ℕ) : ℕ := a + b
#checkadd 3 : ℕ → ℕ add 3 -- add 3 : ℕ → ℕ
add 3 : ℕ → ℕ
Functions are curried: ℕ → ℕ → ℕ is ℕ → (ℕ → ℕ).
#checkfun x => x + 1 : ℕ → ℕ (fun x : ℕ ↦ x + 1) -- ℕ → ℕ
fun x => x + 1 : ℕ → ℕ#eval6 (fun x : ℕ ↦ x + 1) 5 -- 6
6#checkfun x y => x * y : ℕ → ℕ → ℕ (fun x y : ℕ ↦ x * y) -- ℕ → ℕ → ℕ
fun x y => x * y : ℕ → ℕ → ℕ#eval12 (fun x y : ℕ ↦ x * y) 3 4 -- 12
12
↦ is typed \mapsto; math x ↦ x^2 is fun x ↦ x ^ 2
(· + 10) is fun x ↦ x + 10
(· + ·) is fun x y ↦ x + y
example : ∀ n : ℕ, n < n + 1 :=
fun n => Nat.lt_succ_self n
-- ZMod n is ℤ/nℤ
def sevenMod : (n : ℕ) → ZMod n := fun _ => 7
#checksevenMod 5 : ZMod 5 sevenMod 5 -- sevenMod 5 : ZMod 5
sevenMod 5 : ZMod 5#eval2 sevenMod 5 -- 2
2
For data we write (x : α) → β x, for a proposition ∀ x : α, β x.
f : (x : α) → β x picks f x ∈ β x for every x: an element of \prod_x \beta(x).
def myId (α : Type) (a : α) : α := a
#checkmyId ℕ 42 : ℕ myId ℕ 42 -- ℕ
myId ℕ 42 : ℕdef myId' {α : Type} (a : α) : α := a
#checkmyId' 42 : ℕ myId' 42 -- ℕ, inferred
myId' 42 : ℕ#check@myId' : {α : Type} → α → α @myId' -- {α : Type} → α → α
@myId' : {α : Type} → α → αdef id {α : Sort u} (a : α) : α := a -- Lean's Prelude
Braces: implicit argument; @ shows it. Sort u: every universe, Prop included.
def safeDiv (a b : ℤ) (_ : b ≠ 0) : ℤ := a / b
#eval3 safeDiv 10 3 (by⊢ 3 ≠ 0 decideAll goals completed! 🐙) -- 3
3
The third argument is a proof of 3 ≠ 0; decide finds it by computing. Its type depends on b, so the type of safeDiv is a Π-type.
inductive MyNat where
| zero : MyNat
| succ : MyNat → MyNat
example : MyNat := .succ (.succ .zero)
Formation: MyNat : Type
Construction: the constructors MyNat.zero, MyNat.succ; .succ if the type is known
Use: pattern matching, translated into the recursor MyNat.rec
Computation: a match on a constructor takes its branch (ι)
inductive Colour where
| red | green | blue
inductive MyOption (α : Type) where
| none : MyOption α
| some : α → MyOption α
example : MyOption ℕ := .some 3
MyOption α is a type for every type α.
ℕ∞ is Option ℕdef MyENat : Type := Option ℕ
Mathlib's ℕ∞ is notation for ENat, which is WithTop ℕ, which is Option ℕ:
example : ℕ∞ = MyENat := rfl
example : (⊤ : ℕ∞) = Option.none := rfl
example : (3 : ℕ∞) = Option.some 3 := rfl
WithTop α is Option α with none read as a new largest element ⊤.
Measures take values in ℝ≥0∞ = WithTop ℝ≥0, built the same way.
inductive MyOr (a b : Prop) : Prop where
| inl : a → MyOr a b
| inr : b → MyOr a b
example (P Q : Prop) (hP : P) : MyOr P Q := .inl hP
Two constructors: a proof of P ∨ Q is a proof of P or one of Q, marked by the constructor. Lean's ∨ is Or, declared in this way.
∧ has one constructor with two fields: a structure, coming next.
structure Point where
x : ℕ
y : ℕ
is essentially the inductive type
inductive Point where | mk : ℕ → ℕ → Point
with names for the two arguments of the one constructor Point.mk.
structure: one constructor, named fields (points, complex numbers, groups). inductive: several constructors (ℕ, lists, trees, Option).
Point in four stepsexample : (Point.mk 3 4).x = 3 := rfl
example (p : Point) : p.x = Point.x p := rfl
Formation: Point : Type
Construction: Point.mk : ℕ → ℕ → Point, all fields supplied
Use: the projections Point.x : Point → ℕ and Point.y
Computation: (Point.mk 3 4).x reduces to 3; η: p ≡ Point.mk p.x p.y
Dot notation: p.x means Point.x p. Lean sees p : Point and looks for x in the namespace Point.
def p1 : Point := ⟨1, 2⟩
def p2 : Point := { x := 3, y := 4 }
def p3 : Point where
x := 0
y := 0
def p4 : Point := { p1 with y := 10 }
Anonymous constructor, record, where form: all call Point.mk.
Structures are immutable: with builds a new Point.
def Point.normSq (p : Point) : ℕ :=
p.x * p.x + p.y * p.y
example : p2.normSq = 25 := rfl
structure MyComplex where
re : ℤ
im : ℤ
def MyComplex.mul (a b : MyComplex) : MyComplex :=
{ re := a.re * b.re - a.im * b.im,
im := a.re * b.im + a.im * b.re }
def iu : MyComplex := { re := 0, im := 1 } -- i
example : (iu.mul iu).re = -1 := rfl
An operation is an ordinary function. Named Point.f, it is called as p.f, like a projection.
-- a, a + d, a + 2d, ...
structure ArithProg where
start : ℕ := 0
step : ℕ := 1
def evens : ArithProg := { step := 2 }
example : evens.start = 0 := rfl
A field with a default may be left out. With both defaults, {} is the progression 0, 1, 2, ….
structure Point3D extends Point where
z : ℕ
def q : Point3D := { x := 1, y := 2, z := 3 }
example : q.x = 1 := rfl
example : q.toPoint = ⟨1, 2⟩ := rfl
example : q.normSq = 5 := rfl
extends Point adds a field toPoint : Point. Dot notation looks in Point3D, then in Point: fields and functions of Point work on q.
Mathlib builds its algebra this way: a commutative ring extends a ring, which extends a semiring.
structure IntAddMap where
toFun : ℤ → ℤ
map_add : ∀ x y,
toFun (x + y) = toFun x + toFun y
def triple : IntAddMap where
toFun x := 3 * x
map_add x y := mul_add 3 x y
A field may be a proof. Linear maps, continuous functions, homomorphisms: all of this kind.
Fin n and ∧ are structuresIn Lean's Prelude:
structure Fin (n : ℕ) where val : ℕ isLt : val < n structure And (a b : Prop) : Prop where intro :: left : a right : b
example : Fin 3 := ⟨2, by⊢ 2 < 3 decideAll goals completed! 🐙⟩
example (P Q : Prop) (h : P ∧ Q) : Q ∧ P :=
⟨h.2, h.1⟩
intro :: names the constructor And.intro instead of And.mk; h.1, h.2 are h.left, h.right.
example (n : ℕ) (a b : Fin n)
(h : a.val = b.val) : a = b :=
Fin.ext h
example (n : ℕ) (a b : Fin n)
(h : a.val = b.val) : a = b := byn:ℕa:Fin nb:Fin nh:↑a = ↑b⊢ a = b
cases an:ℕb:Fin nval✝:ℕisLt✝:val✝ < nh:↑⟨val✝, isLt✝⟩ = ↑b⊢ ⟨val✝, isLt✝⟩ = b; cases bn:ℕval✝¹:ℕisLt✝¹:val✝ < nval✝:ℕisLt✝:val✝ < nh:↑⟨val✝¹, isLt✝¹⟩ = ↑⟨val✝, isLt✝⟩⊢ ⟨val✝¹, isLt✝¹⟩ = ⟨val✝, isLt✝⟩; cases hn:ℕval✝:ℕisLt✝¹:val✝ < nisLt✝:val✝ < n⊢ ⟨val✝, isLt✝¹⟩ = ⟨val✝, isLt✝⟩; rflAll goals completed! 🐙
By η, p = Point.mk p.x p.y is rfl; ℕ has no such rule. Two values with equal fields are equal, but not by rfl:
Fin.ext needs no axiom: after cases, two proofs of val < n remain, and Lean identifies proofs of the same proposition (chapter 1.5).
Mathlib calls "equal if the fields are equal" .ext: Fin.ext, Prod.ext, Subtype.ext.
constructors | data | proposition |
|---|---|---|
none |
|
|
one, no arguments |
|
|
two, no arguments |
| — |
one, two arguments |
|
|
two, one argument each |
|
|
one, value and proof |
|
|
recursive |
|
|
A structure is an inductive type with one constructor, plus field names, projections, defaults and extends.
Universes: Prop, Type 0 : Type 1 : ...; there is no Type : Type.
Functions: (x : α) → β x, built with fun, used by application, computed by β.
def, currying, fun x ↦ …, ·, implicit and universe-polymorphic arguments.
inductive: built with constructors, used by pattern matching (chapter 1.3), computed by ι. ∨ is one of them.
structure: an inductive type with one constructor, named fields, projections, dot notation p.f for Point.f p; a field may be a proof; extends inherits fields and functions; equality by .ext. Fin n and ∧ are structures.