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.PFunctor.Multivariate.W

42 declarations. Mathlib docs · Source

The W construction as a multivariate polynomial functor.

W types are well-founded tree-like structures. They are defined as the least fixpoint of a polynomial functor.

Main definitions

  • W_mk - constructor
  • W_dest - destructor
  • W_rec - recursor: basis for defining functions by structural recursion on P.W α
  • W_rec_eq - defining equation for W_rec
  • W_ind - induction principle for P.W α

Implementation notes

Three views of M-types:

  • wp: polynomial functor
  • W: data type inductively defined by a triple: shape of the root, data in the root and children of the root
  • W: least fixed point of a polynomial functor

Specifically, we define the polynomial functor wp as:

  • A := a tree-like structure without information in the nodes
  • B := given the tree-like structure t, B t is a valid path (specified inductively by W_path) from the root of t to any given node.

As a result wp α is made of a dataless tree and a function from its valid paths to values of α

References

  • Jeremy Avigad, Mario M. Carneiro and Simon Hudon. [Data Types as Quotients of Polynomial Functors][avigad-carneiro-hudon2019]

First, describe operations on W as a polynomial functor.

Now think of W as defined inductively by the data ⟨a, f', f⟩ where

  • a : P.A is the shape of the top node
  • f' : P.drop.B a ⟹ α is the contents of the top node
  • f : P.last.B a → P.last.W are the subtrees

Yet another view of the W type: as a fixed point for a multivariate polynomial functor. These are needed to use the W-construction to construct a fixed point of a qpf, since the qpf axioms are expressed in terms of map on P.

theorem MvPFunctor.WPath.brecOn.eq E1 #

∀ {n : ℕ} {P : MvPFunctor.{u} (n + 1)} {motive : (a : P.last.W) → (a_1 : Fin2 n) → P.WPath a a_1 → Sort u_1}
  {a : P.last.W} {a_1 : Fin2 n} (t : P.WPath a a_1)
  (F_1 : (a : P.last.W) → (a_2 : Fin2 n) → (t : P.WPath a a_2) → MvPFunctor.WPath.below t → motive a a_2 t),
  MvPFunctor.WPath.brecOn t F_1 = F_1 a a_1 t (MvPFunctor.WPath.brecOn.go t F_1).2

theorem MvPFunctor.WPath.child.injEq E0 #

∀ {n : ℕ} {P : MvPFunctor.{u} (n + 1)} (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (j : P.last.B a)
  (c : P.WPath (f j) i) (j_1 : P.last.B a) (c_1 : P.WPath (f j_1) i),
  (MvPFunctor.WPath.child a f i j c = MvPFunctor.WPath.child a f i j_1 c_1) = (j = j_1 ∧ c ≍ c_1)

theorem MvPFunctor.WPath.child.inj E1 #

∀ {n : ℕ} {P : MvPFunctor.{u} (n + 1)} {a : P.A} {f : P.last.B a → P.last.W} {i : Fin2 n} {j : P.last.B a}
  {c : P.WPath (f j) i} {j_1 : P.last.B a} {c_1 : P.WPath (f j_1) i},
  MvPFunctor.WPath.child a f i j c = MvPFunctor.WPath.child a f i j_1 c_1 → j = j_1 ∧ c ≍ c_1

theorem MvPFunctor.WPath.root.injEq E0 #

∀ {n : ℕ} {P : MvPFunctor.{u} (n + 1)} (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (c c_1 : P.drop.B a i),
  (MvPFunctor.WPath.root a f i c = MvPFunctor.WPath.root a f i c_1) = (c = c_1)

theorem MvPFunctor.WPath.root.inj E1 #

∀ {n : ℕ} {P : MvPFunctor.{u} (n + 1)} {a : P.A} {f : P.last.B a → P.last.W} {i : Fin2 n} {c c_1 : P.drop.B a i},
  MvPFunctor.WPath.root a f i c = MvPFunctor.WPath.root a f i c_1 → c = c_1

theorem MvPFunctor.wMk'.match_1.congr_eq_1 E1 #

∀ {n : ℕ} (P : MvPFunctor.{u_1} (n + 1)) {α : TypeVec.{u_1} n} (motive : ↑P (α ::: P.W α) → Sort u_2)
  (x : ↑P (α ::: P.W α)) (h_1 : (a : P.A) → (f : (P.B a).Arrow (α ::: P.W α)) → motive ⟨a, f⟩) (a : P.A)
  (f : (P.B a).Arrow (α ::: P.W α)),
  x = ⟨a, f⟩ →
    (match x with
      | ⟨a, f⟩ => h_1 a f) ≍
      h_1 a f

def MvPFunctor.WPath.ctorIdx E1 #

{n : ℕ} → {P : MvPFunctor.{u} (n + 1)} → {a : P.last.W} → {a_1 : Fin2 n} → P.WPath a a_1 → ℕ

abbrev MvPFunctor.WPath.ctorElimType E1 #

{n : ℕ} →
  {P : MvPFunctor.{u} (n + 1)} →
    {motive : (a : P.last.W) → (a_1 : Fin2 n) → P.WPath a a_1 → Sort u_1} →
      ℕ → Sort (max 1 (imax (u + 1) (u + 1) (u + 1) u_1) (imax (u + 1) (u + 1) (u + 1) (u + 1) u_1))

abbrev MvPFunctor.WPath.brecOn.go E1 #

{n : ℕ} →
  {P : MvPFunctor.{u} (n + 1)} →
    {motive : (a : P.last.W) → (a_1 : Fin2 n) → P.WPath a a_1 → Sort u_1} →
      {a : P.last.W} →
        {a_1 : Fin2 n} →
          (t : P.WPath a a_1) →
            ((a : P.last.W) → (a_2 : Fin2 n) → (t : P.WPath a a_2) → MvPFunctor.WPath.below t → motive a a_2 t) →
              motive a a_1 t ×' MvPFunctor.WPath.below t

inductive MvPFunctor.WPath E1 #

{n : ℕ} → (P : MvPFunctor.{u} (n + 1)) → P.last.W → Fin2 n → Type u

A path from the root of a tree to one of its node

ctor MvPFunctor.WPath.root E0 #

{n : ℕ} →
  {P : MvPFunctor.{u} (n + 1)} →
    (a : P.A) → (f : P.last.B a → P.last.W) → (i : Fin2 n) → P.drop.B a i → P.WPath (WType.mk a f) i

ctor MvPFunctor.WPath.child E0 #

{n : ℕ} →
  {P : MvPFunctor.{u} (n + 1)} →
    (a : P.A) →
      (f : P.last.B a → P.last.W) → (i : Fin2 n) → (j : P.last.B a) → P.WPath (f j) i → P.WPath (WType.mk a f) i

instance MvPFunctor.WPath.inhabited E0 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    (x : P.last.W) → {i : Fin2 n} → [I : Inhabited (P.drop.B x.head i)] → Inhabited (P.WPath x i)

def MvPFunctor.wPathCasesOn E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u_1} n} →
      {a : P.A} →
        {f : P.last.B a → P.last.W} →
          (P.drop.B a).Arrow α →
            ((j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α) → TypeVec.Arrow (P.WPath (WType.mk a f)) α

Specialized destructor on WPath

def MvPFunctor.wPathDestLeft E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u_1} n} →
      {a : P.A} → {f : P.last.B a → P.last.W} → TypeVec.Arrow (P.WPath (WType.mk a f)) α → (P.drop.B a).Arrow α

Specialized destructor on WPath

def MvPFunctor.wPathDestRight E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u_1} n} →
      {a : P.A} →
        {f : P.last.B a → P.last.W} →
          TypeVec.Arrow (P.WPath (WType.mk a f)) α → (j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α

Specialized destructor on WPath

theorem MvPFunctor.wPathDestLeft_wPathCasesOn E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u_1} n} {a : P.A} {f : P.last.B a → P.last.W}
  (g' : (P.drop.B a).Arrow α) (g : (j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α),
  P.wPathDestLeft (P.wPathCasesOn g' g) = g'

theorem MvPFunctor.wPathDestRight_wPathCasesOn E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u_1} n} {a : P.A} {f : P.last.B a → P.last.W}
  (g' : (P.drop.B a).Arrow α) (g : (j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α),
  P.wPathDestRight (P.wPathCasesOn g' g) = g

theorem MvPFunctor.wPathCasesOn_eta E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u_1} n} {a : P.A} {f : P.last.B a → P.last.W}
  (h : TypeVec.Arrow (P.WPath (WType.mk a f)) α), P.wPathCasesOn (P.wPathDestLeft h) (P.wPathDestRight h) = h

theorem MvPFunctor.comp_wPathCasesOn E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u_1} n} {β : TypeVec.{u_2} n} (h : α.Arrow β) {a : P.A}
  {f : P.last.B a → P.last.W} (g' : (P.drop.B a).Arrow α) (g : (j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α),
  TypeVec.comp h (P.wPathCasesOn g' g) = P.wPathCasesOn (TypeVec.comp h g') fun i ↦ TypeVec.comp h (g i)

def MvPFunctor.wp E1 #

{n : ℕ} → MvPFunctor.{u} (n + 1) → MvPFunctor.{u} n

Polynomial functor for the W-type of P. A is a data-less well-founded tree whereas, for a given a : A, B a is a valid path in tree a so that Wp.obj α is made of a tree and a function from its valid paths to the values it contains

def MvPFunctor.W E1 #

{n : ℕ} → MvPFunctor.{u} (n + 1) → TypeVec.{u} n → Type u

W-type of P

instance MvPFunctor.mvfunctorW E0 #

{n : ℕ} → (P : MvPFunctor.{u} (n + 1)) → MvFunctor P.W

def MvPFunctor.wpMk E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u} n} → (a : P.A) → (f : P.last.B a → P.last.W) → TypeVec.Arrow (P.WPath (WType.mk a f)) α → P.W α

Constructor for wp

def MvPFunctor.wpRec E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u_2} n} →
      {C : Sort u_1} →
        ((a : P.A) → (f : P.last.B a → P.last.W) → TypeVec.Arrow (P.WPath (WType.mk a f)) α → (P.last.B a → C) → C) →
          (x : P.last.W) → TypeVec.Arrow (P.WPath x) α → C

theorem MvPFunctor.wpRec_eq E0 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u_2} n} {C : Sort u_1}
  (g : (a : P.A) → (f : P.last.B a → P.last.W) → TypeVec.Arrow (P.WPath (WType.mk a f)) α → (P.last.B a → C) → C)
  (a : P.A) (f : P.last.B a → P.last.W) (f' : TypeVec.Arrow (P.WPath (WType.mk a f)) α),
  P.wpRec g (WType.mk a f) f' = g a f f' fun i ↦ P.wpRec g (f i) (P.wPathDestRight f' i)

def MvPFunctor.wpInd E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u_1} n} →
      {C : (x : P.last.W) → TypeVec.Arrow (P.WPath x) α → Sort v} →
        ((a : P.A) →
            (f : P.last.B a → P.last.W) →
              (f' : TypeVec.Arrow (P.WPath (WType.mk a f)) α) →
                ((i : P.last.B a) → C (f i) (P.wPathDestRight f' i)) → C (WType.mk a f) f') →
          (x : P.last.W) → (f' : TypeVec.Arrow (P.WPath x) α) → C x f'

Induction principle for an unfolded W

def MvPFunctor.wMk E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) → {α : TypeVec.{u} n} → (a : P.A) → (P.drop.B a).Arrow α → (P.last.B a → P.W α) → P.W α

Constructor for W

def MvPFunctor.wRec E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u} n} →
      {C : Sort u_1} → ((a : P.A) → (P.drop.B a).Arrow α → (P.last.B a → P.W α) → (P.last.B a → C) → C) → P.W α → C

Recursor for W

theorem MvPFunctor.wRec_eq E0 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u} n} {C : Sort u_1}
  (g : (a : P.A) → (P.drop.B a).Arrow α → (P.last.B a → P.W α) → (P.last.B a → C) → C) (a : P.A)
  (f' : (P.drop.B a).Arrow α) (f : P.last.B a → P.W α), P.wRec g (P.wMk a f' f) = g a f' f fun i ↦ P.wRec g (f i)

Defining equation for the recursor of W

def MvPFunctor.wInd E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u} n} →
      {C : P.W α → Sort v} →
        ((a : P.A) →
            (f' : (P.drop.B a).Arrow α) → (f : P.last.B a → P.W α) → ((i : P.last.B a) → C (f i)) → C (P.wMk a f' f)) →
          (x : P.W α) → C x

Induction principle for W

theorem MvPFunctor.wInd_wMk E0 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u} n} {C : P.W α → Sort v}
  (ih :
    (a : P.A) →
      (f' : (P.drop.B a).Arrow α) → (f : P.last.B a → P.W α) → ((i : P.last.B a) → C (f i)) → C (P.wMk a f' f))
  {a : P.drop.A} {f' : (P.drop.B a).Arrow α} {f : P.last.B a → P.W α},
  P.wInd ih (P.wMk a f' f) = ih a f' f fun i ↦ P.wInd ih (f i)

def MvPFunctor.wCases E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u} n} →
      {C : P.W α → Sort v} →
        ((a : P.A) → (f' : (P.drop.B a).Arrow α) → (f : P.last.B a → P.W α) → C (P.wMk a f' f)) → (x : P.W α) → C x

Cases lemma for W types

def MvPFunctor.wMap E1 #

{n : ℕ} → (P : MvPFunctor.{u} (n + 1)) → {α β : TypeVec.{u} n} → α.Arrow β → P.W α → P.W β

W-types are functorial

theorem MvPFunctor.wMk_eq E0 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u} n} (a : P.A) (f : P.last.B a → P.last.W)
  (g' : (P.drop.B a).Arrow α) (g : (j : P.last.B a) → TypeVec.Arrow (P.WPath (f j)) α),
  (P.wMk a g' fun i ↦ ⟨f i, g i⟩) = ⟨WType.mk a f, P.wPathCasesOn g' g⟩

theorem MvPFunctor.w_map_wMk E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α β : TypeVec.{u} n} (g : α.Arrow β) (a : P.A) (f' : (P.drop.B a).Arrow α)
  (f : P.last.B a → P.W α), MvFunctor.map g (P.wMk a f' f) = P.wMk a (TypeVec.comp g f') fun i ↦ MvFunctor.map g (f i)

abbrev MvPFunctor.objAppend1 E1 #

{n : ℕ} →
  (P : MvPFunctor.{u} (n + 1)) →
    {α : TypeVec.{u} n} → {β : Type u} → (a : P.A) → (P.drop.B a).Arrow α → (P.last.B a → β) → ↑P (α ::: β)

Constructor of a value of P.obj (α ::: β) from components. Useful to avoid complicated type annotation

theorem MvPFunctor.map_objAppend1 E0 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α γ : TypeVec.{u} n} (g : α.Arrow γ) (a : P.A) (f' : (P.drop.B a).Arrow α)
  (f : P.last.B a → P.W α),
  MvFunctor.map (g ::: P.wMap g) (P.objAppend1 a f' f) = P.objAppend1 a (TypeVec.comp g f') fun x ↦ P.wMap g (f x)

def MvPFunctor.wMk' E1 #

{n : ℕ} → (P : MvPFunctor.{u} (n + 1)) → {α : TypeVec.{u} n} → ↑P (α ::: P.W α) → P.W α

Constructor for the W-type of P

def MvPFunctor.wDest' E1 #

{n : ℕ} → (P : MvPFunctor.{u} (n + 1)) → {α : TypeVec.{u} n} → P.W α → ↑P (α ::: P.W α)

Destructor for the W-type of P

theorem MvPFunctor.wDest'_wMk E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u} n} (a : P.A) (f' : (P.drop.B a).Arrow α)
  (f : P.last.B a → P.W α), P.wDest' (P.wMk a f' f) = ⟨a, TypeVec.splitFun f' f⟩

theorem MvPFunctor.wDest'_wMk' E1 #

∀ {n : ℕ} (P : MvPFunctor.{u} (n + 1)) {α : TypeVec.{u} n} (x : ↑P (α ::: P.W α)), P.wDest' (P.wMk' x) = x