Functions, inductive types, structures

Lean and its type theory — chapter 2

In Lean, types can depend on values.

Every term has a type

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.

Four steps for every type

step

question

function

inductive type

structure

formation

how is the type written?

α → β

ℕ, inductive T

Point, structure S

construction

how is a term built?

fun x => e

0, n + 1

⟨1, 2⟩, Point.mk 1 2

use

how is a term used?

f a

match n with …

p.x, p.y

computation

what if a term just built is used?

(fun x => e) a ⟶ e[x := a]

a match on 0 takes the 0 branch

⟨1, 2⟩.x ⟶ 1

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.

Commands for terms

def four : ℕ := 2 + 2 -- names a term theorem four_eq : four = 4 := rfl -- names a proof example : four = 4 := rfl -- checks, no name #check four -- the type
SlidesTypes.four : ℕ
#eval four -- the value
4
#print four -- the definition
def SlidesTypes.four : ℕ := 2 + 2

Mathlib also accepts lemma for theorem.

#print shows the proof term

theorem swap_of_and (P Q : Prop) (h : P ∧ Q) : Q ∧ P := by constructor · exact h.2 · exact h.1 #print 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.

The universe hierarchy

universe

its terms, for example

lives in

Prop = Sort 0

2 = 2, ∀ n : ℕ, n < n + 1

Type

Type = Type 0 = Sort 1

ℕ, ℝ, Bool, ℕ → ℝ, Prop

Type 1

Type 1 = Sort 2

Type, ℕ → Type

Type 2

Type u = Sort (u + 1)

…

Type (u + 1)

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.

Function types and Π-types

Π-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 #eval (fun n : ℕ => n + 1) 4 -- 5
5

Defining functions

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 #check add 3 -- add 3 : ℕ → ℕ
add 3 : ℕ → ℕ

Functions are curried: ℕ → ℕ → ℕ is ℕ → (ℕ → ℕ).

Anonymous functions

#check (fun x : ℕ ↦ x + 1) -- ℕ → ℕ
fun x => x + 1 : ℕ → ℕ
#eval (fun x : ℕ ↦ x + 1) 5 -- 6
6
#check (fun x y : ℕ ↦ x * y) -- ℕ → ℕ → ℕ
fun x y => x * y : ℕ → ℕ → ℕ
#eval (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

Dependent functions

example : ∀ n : ℕ, n < n + 1 := fun n => Nat.lt_succ_self n -- ZMod n is ℤ/nℤ def sevenMod : (n : ℕ) → ZMod n := fun _ => 7 #check sevenMod 5 -- sevenMod 5 : ZMod 5
sevenMod 5 : ZMod 5
#eval 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).

Polymorphic functions

def myId (α : Type) (a : α) : α := a #check myId ℕ 42 -- ℕ
myId ℕ 42 : ℕ
def myId' {α : Type} (a : α) : α := a #check myId' 42 -- ℕ, inferred
myId' 42 : ℕ
#check @myId' -- {α : Type} → α → α
@myId' : {α : Type} → α → α
def id {α : Sort u} (a : α) : α := a   -- Lean's Prelude

Braces: implicit argument; @ shows it. Sort u: every universe, Prop included.

Proofs as arguments

def safeDiv (a b : ℤ) (_ : b ≠ 0) : ℤ := a / b #eval safeDiv 10 3 (by decide) -- 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 types

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 (ι)

More inductive types

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.

Disjunction is an inductive type

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.

Structures

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 steps

example : (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.

Building values

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.

Operations

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.

Default values

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

Extending a structure

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.

Data with a property

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 structures

In 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 decide⟩ 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.

When are two values equal?

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 := by cases a; cases b; cases h; rfl

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.

Structures and inductive types

constructors

data

proposition

none

Empty

False

one, no arguments

Unit

True

two, no arguments

Bool

—

one, two arguments

α × β

P ∧ Q

two, one argument each

α ⊕ β

P ∨ Q

one, value and proof

{x : α // P x}

∃ x, P x

recursive

ℕ, List α

n ≤ m

A structure is an inductive type with one constructor, plus field names, projections, defaults and extends.

Summary

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