The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Data.W.Constructions

40 declarations. Mathlib docs · Source

Examples of W-types

We take the view of W types as inductive types. Given α : Type and β : α → Type, the W type determined by this data, WType β, is the inductively with constructors from α and arities of each constructor a : α given by β a.

This file contains Nat and List as examples of W types.

Main results

  • WType.equivNat: the construction of the naturals as a W-type is equivalent to Nat
  • WType.equivList: the construction of lists on a type γ as a W-type is equivalent to List γ

theorem WType.toList.match_1.congr_eq_2 E1 #

∀ (γ : Type u_1) (motive : WType (WType.Listβ γ) → Sort u_2) (x : WType (WType.Listβ γ))
  (h_1 : (f : WType.Listβ γ WType.Listα.nil → WType (WType.Listβ γ)) → motive (WType.mk WType.Listα.nil f))
  (h_2 :
    (hd : γ) →
      (f : WType.Listβ γ (WType.Listα.cons hd) → WType (WType.Listβ γ)) → motive (WType.mk (WType.Listα.cons hd) f))
  (hd : γ) (f : WType.Listβ γ (WType.Listα.cons hd) → WType (WType.Listβ γ)),
  x = WType.mk (WType.Listα.cons hd) f →
    (match x with
      | WType.mk WType.Listα.nil f => h_1 f
      | WType.mk (WType.Listα.cons hd) f => h_2 hd f) ≍
      h_2 hd f

theorem WType.Listα.cons.inj E1 #

∀ {γ : Type u} {a a_1 : γ}, WType.Listα.cons a = WType.Listα.cons a_1 → a = a_1

theorem WType.ofList.match_1.congr_eq_2 E1 #

∀ (γ : Type u_1) (motive : List γ → Sort u_2) (x : List γ) (h_1 : Unit → motive [])
  (h_2 : (hd : γ) → (tl : List γ) → motive (hd :: tl)) (hd : γ) (tl : List γ),
  x = hd :: tl →
    (match x with
      | [] => h_1 ()
      | hd :: tl => h_2 hd tl) ≍
      h_2 hd tl

theorem WType.toNat.match_1.congr_eq_1 E1 #

∀ (motive : WType WType.Natβ → Sort u_1) (x : WType WType.Natβ)
  (h_1 : (f : WType.Natβ WType.Natα.zero → WType WType.Natβ) → motive (WType.mk WType.Natα.zero f))
  (h_2 : (f : WType.Natβ WType.Natα.succ → WType WType.Natβ) → motive (WType.mk WType.Natα.succ f))
  (f : WType.Natβ WType.Natα.zero → WType WType.Natβ),
  x = WType.mk WType.Natα.zero f →
    (match x with
      | WType.mk WType.Natα.zero f => h_1 f
      | WType.mk WType.Natα.succ f => h_2 f) ≍
      h_1 f

def WType.Listα.ctorIdx E1 #

{γ : Type u} → WType.Listα γ → ℕ

theorem WType.toList.match_1.congr_eq_1 E1 #

∀ (γ : Type u_1) (motive : WType (WType.Listβ γ) → Sort u_2) (x : WType (WType.Listβ γ))
  (h_1 : (f : WType.Listβ γ WType.Listα.nil → WType (WType.Listβ γ)) → motive (WType.mk WType.Listα.nil f))
  (h_2 :
    (hd : γ) →
      (f : WType.Listβ γ (WType.Listα.cons hd) → WType (WType.Listβ γ)) → motive (WType.mk (WType.Listα.cons hd) f))
  (f : WType.Listβ γ WType.Listα.nil → WType (WType.Listβ γ)),
  x = WType.mk WType.Listα.nil f →
    (match x with
      | WType.mk WType.Listα.nil f => h_1 f
      | WType.mk (WType.Listα.cons hd) f => h_2 hd f) ≍
      h_1 f

theorem WType.ofList.match_1.congr_eq_1 E1 #

∀ (γ : Type u_1) (motive : List γ → Sort u_2) (x : List γ) (h_1 : Unit → motive [])
  (h_2 : (hd : γ) → (tl : List γ) → motive (hd :: tl)),
  x = [] →
    (match x with
      | [] => h_1 ()
      | hd :: tl => h_2 hd tl) ≍
      h_1 ()

theorem WType.toNat.match_1.congr_eq_2 E1 #

∀ (motive : WType WType.Natβ → Sort u_1) (x : WType WType.Natβ)
  (h_1 : (f : WType.Natβ WType.Natα.zero → WType WType.Natβ) → motive (WType.mk WType.Natα.zero f))
  (h_2 : (f : WType.Natβ WType.Natα.succ → WType WType.Natβ) → motive (WType.mk WType.Natα.succ f))
  (f : WType.Natβ WType.Natα.succ → WType WType.Natβ),
  x = WType.mk WType.Natα.succ f →
    (match x with
      | WType.mk WType.Natα.zero f => h_1 f
      | WType.mk WType.Natα.succ f => h_2 f) ≍
      h_2 f

abbrev WType.Natα.ctorElimType E1 #

{motive : WType.Natα → Sort u} → ℕ → Sort (max 1 u)

abbrev WType.Listα.ctorElimType E1 #

{γ : Type u} → {motive : WType.Listα γ → Sort u_1} → ℕ → Sort (max 1 u_1 (imax (u + 1) u_1))

theorem WType.ofNat.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive Nat.zero) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
  x = n.succ →
    (match x with
      | Nat.zero => h_1 ()
      | n.succ => h_2 n) ≍
      h_2 n

theorem WType.ofNat.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive Nat.zero) (h_2 : (n : ℕ) → motive n.succ),
  x = Nat.zero →
    (match x with
      | Nat.zero => h_1 ()
      | n.succ => h_2 n) ≍
      h_1 ()

def WType.Natα.ctorIdx E1 #

WType.Natα → ℕ

theorem WType.Listα.cons.injEq E0 #

∀ {γ : Type u} (a a_1 : γ), (WType.Listα.cons a = WType.Listα.cons a_1) = (a = a_1)

inductive WType.Natα E1 #

Type

The constructors for the naturals

ctor WType.Natα.zero E0 #

WType.Natα

ctor WType.Natα.succ E0 #

WType.Natα

instance WType.instInhabitedNatα E0 #

Inhabited WType.Natα

def WType.Natβ E1 #

WType.Natα → Type

The arity of the constructors for the naturals, zero takes no arguments, succ takes one

instance WType.instInhabitedNatβSucc E0 #

Inhabited (WType.Natβ WType.Natα.succ)

def WType.ofNat E1 #

ℕ → WType WType.Natβ

The isomorphism from the naturals to its corresponding WType

def WType.toNat E1 #

WType WType.Natβ → ℕ

The isomorphism from the WType of the naturals to the naturals

theorem WType.leftInverse_nat E1 #

Function.LeftInverse WType.ofNat WType.toNat

theorem WType.rightInverse_nat E1 #

Function.RightInverse WType.ofNat WType.toNat

def WType.equivNat E3 #

WType WType.Natβ ≃ ℕ

The naturals are equivalent to their associated WType

def WType.NatαEquivPUnitSumPUnit E1 #

WType.Natα ≃ PUnit.{u + 1} ⊕ PUnit.{u_1 + 1}

WType.Natα is equivalent to PUnit ⊕ PUnit. This is useful when considering the associated polynomial endofunctor.

theorem WType.NatαEquivPUnitSumPUnit_apply E0 #

∀ (c : WType.Natα),
  WType.NatαEquivPUnitSumPUnit c =
    match c with
    | WType.Natα.zero => Sum.inl PUnit.unit
    | WType.Natα.succ => Sum.inr PUnit.unit

theorem WType.NatαEquivPUnitSumPUnit_symm_apply E0 #

∀ (b : PUnit.{u + 1} ⊕ PUnit.{u_1 + 1}),
  WType.NatαEquivPUnitSumPUnit.symm b =
    match b with
    | Sum.inl val => WType.Natα.zero
    | Sum.inr val => WType.Natα.succ

inductive WType.Listα E1 #

Type u → Type u

The constructors for lists. There is "one constructor cons x for each x : γ", since we view List γ as

| nil : List γ
| cons x₀ : List γ → List γ
| cons x₁ : List γ → List γ
|   ⋮      γ many times

ctor WType.Listα.nil E0 #

{γ : Type u} → WType.Listα γ

ctor WType.Listα.cons E0 #

{γ : Type u} → γ → WType.Listα γ

instance WType.instInhabitedListα E0 #

(γ : Type u) → Inhabited (WType.Listα γ)

def WType.Listβ E1 #

(γ : Type u) → WType.Listα γ → Type u

The arities of each constructor for lists, nil takes no arguments, cons hd takes one

instance WType.instInhabitedListβCons E0 #

(γ : Type u) → (hd : γ) → Inhabited (WType.Listβ γ (WType.Listα.cons hd))

def WType.ofList E1 #

(γ : Type u) → List γ → WType (WType.Listβ γ)

The isomorphism from lists to the WType construction of lists

def WType.toList E1 #

(γ : Type u) → WType (WType.Listβ γ) → List γ

The isomorphism from the WType construction of lists to lists

theorem WType.leftInverse_list E1 #

∀ (γ : Type u), Function.LeftInverse (WType.ofList γ) (WType.toList γ)

theorem WType.rightInverse_list E1 #

∀ (γ : Type u), Function.RightInverse (WType.ofList γ) (WType.toList γ)

def WType.equivList E3 #

(γ : Type u) → WType (WType.Listβ γ) ≃ List γ

Lists are equivalent to their associated WType

def WType.ListαEquivPUnitSum E1 #

(γ : Type u) → WType.Listα γ ≃ PUnit.{v + 1} ⊕ γ

WType.Listα is equivalent to γ with an extra point. This is useful when considering the associated polynomial endofunctor