module Data.Wellfounded.W where
W-typesπ
The idea behind types is much simpler than they might appear at first brush, especially because their form is like that one of the βbig twoβ and However, the humble is much simpler: A value of is a tree with nodes labelled by and such that the branching factor of such a node is given by can be defined inductively:
data W {β β'} (A : Type β) (B : A β Type β') : Type (β β β') where
: (x : A) (f : B x β W A B) β W A B sup
: β {β β' β''} {A : Type β} {B : A β Type β'} {C : W A B β Type β''}
W-elim β ({a : A} {f : B a β W A B} β (β ba β C (f ba)) β C (sup a f))
β (w : W A B) β C w
{C = C} ps (sup a f) = ps (Ξ» ba β W-elim {C = C} ps (f ba)) W-elim
The constructor sup
stands for
supremum: it bunches up (takes the supremum of) a bunch
of subtrees, each subtree being given by a value
of the branching factor for that node. The natural question to ask,
then, is: βsupremum in what order?β. The order given by the βis
a subtree ofβ relation!
module _ {β β'} {A : Type β} {B : A β Type β'} where
_<_ : W A B β W A B β Type _
= β[ j β B i ] (f j β‘ x) x < sup i f
This order is actually well-founded: if we want to prove a property of we may as well assume itβs been proven for any (transitive) subtree of
: Wf _<_
W-well-founded (sup x f) = acc Ξ» y y<sup β
W-well-founded (Acc-is-prop _)
β₯-β₯-rec (Ξ» { (j , p) β subst (Acc _<_) p (W-well-founded (f j)) })
y<sup
Inductive types are initial algebrasπ
module inductionβinitial {β β'} (A : Type β) (B : A β Type β') where
We can use
types to illustrate the general fact that inductive types correspond to
initial algebras for certain endofunctors. Here, we are working
in the βambientβ
of types and functions, and we are interested in the polynomial
functor P
:
: Type (β β β') β Type (β β β')
P = Ξ£ A Ξ» a β B a β C
P C
: {C D : Type (β β β')} β (C β D) β P C β P D
Pβ (a , h) = a , f β h Pβ f
A algebra (or algebra) is simply a type with a function
: Type _
WAlg = Ξ£ (Type (β β β')) Ξ» C β P C β C WAlg
Algebras form a category, where an algebra homomorphism is a function that respects the algebra structure.
_Wβ_ : WAlg β WAlg β Type _
(C , c) Wβ (D , d) = Ξ£ (C β D) Ξ» f β f β c β‘ d β Pβ f
: β {A} β A Wβ A
idW .fst = id
idW .snd = refl
idW
_Wβ_ : β {A B C} β B Wβ C β A Wβ B β A Wβ C
(f Wβ g) .fst x = f .fst (g .fst x)
(f Wβ g) .snd = ext Ξ» a b β ap (f .fst) (happly (g .snd) (a , b)) β happly (f .snd) _
Now the inductive W
type defined
above gives us a canonical
: WAlg
W-algebra .fst = W A B
W-algebra .snd (a , f) = sup a f W-algebra
The claim is that this algebra is special in that it satisfies a universal property: it is initial in the aforementioned category of This means that, for any other there is exactly one homomorphism
: WAlg β Type _
is-initial-WAlg = (C : WAlg) β is-contr (I Wβ C) is-initial-WAlg I
Existence is easy: the algebra
gives us exactly the data we need to specify a function
W A B β C
by recursion, and the computation rules ensure
that this respects the algebra structure definitionally.
: is-initial-WAlg W-algebra
W-initial (C , c) .centre = W-elim (Ξ» {a} f β c (a , f)) , refl W-initial
For uniqueness, we proceed by induction, using the fact that
g
is a homomorphism.
(C , c) .paths (g , hom) = Ξ£-pathp unique coherent where
W-initial : W-elim (Ξ» {a} f β c (a , f)) β‘ g
unique = funext (W-elim Ξ» {a} {f} rec β ap (Ξ» x β c (a , x)) (funext rec)
unique (hom $β (a , f))) β sym
There is one subtlety: being an algebra homomorphism is not
a proposition in general, so we
must also show that unique
is in
fact an algebra 2-cell, i.e.Β that it makes the
following two identity proofs equal:
Luckily, this is completely straightforward.
: Square (Ξ» i β unique i β W-algebra .snd) refl hom (Ξ» i β c β Pβ (unique i))
coherent = transpose (flipβ (β-filler _ _)) coherent
Initial algebras are inductive typesπ
We will now show the other direction of the correspondence: given an initial we recover the type and its induction principle, albeit with a propositional computation rule.
open inductionβinitial using (WAlg ; is-initial-WAlg ; W-initial) public
module initialβinduction {β β'} (A : Type β) (B : A β Type β') (alg : WAlg A B) (init : is-initial-WAlg A B alg) where
open inductionβinitial A B using (_Wβ_ ; idW ; _Wβ_)
module _ where private
Itβs easy to invert the construction of W-algebra
to obtain a type W'
and a candidate βconstructorβ
sup'
, by projecting the
corresponding components from the given
: Type _
W' = alg .fst
W'
: (a : A) (b : B a β W') β W'
sup' = alg .snd (a , b) sup' a b
open Ξ£ alg renaming (fst to W' ; snd to sup')
We will now show that W'
satisfies the induction
principle of
To that end, suppose we have a predicate
the motive for induction, and a function β the method
β showing
given the data of the constructor
and the induction hypotheses
module
_ (P : W' β Type (β β β'))
(psup : β {a f} (f' : (b : B a) β P (f b)) β P (sup' (a , f)))
where
The first part of the construction is observing that is precisely what is needed to equip the total space of the motive with structure. Correspondingly, we call this a βtotal algebraβ of and write it
: WAlg A B
total-alg .fst = Ξ£[ x β W' ] P x
total-alg .snd (x , ff') = sup' (x , fst β ff') , psup (snd β ff') total-alg
By our assumed initiality of we then have a morphism
private module _ where private
: alg Wβ total-alg
elim-hom = init total-alg .centre elim-hom
To better understand this, we can write out the components of this
map. First, we have an underlying function
which sends an element
to a pair
with
and
Since
indexes a fibre of
we refer to it as the index
of the
result; the returned element
is the datum
.
: W' β W'
index = elim-hom .fst x .fst
index x
: β x β P (index x)
datum = elim-hom .fst x .snd datum x
open is-contr (init total-alg) renaming (centre to elim-hom)
module _ (x : W') where
open Ξ£ (elim-hom .fst x) renaming (fst to index ; snd to datum) public
We also have the equation expressing that elim-hom
is an algebra map, which says
that index
and datum
both commute with supremum, where
the second identification depends on the first.
beta: (a : A) (f : (x : B a) β W')
β (index (sup' (a , f)) , datum (sup' (a , f)))
(sup' (a , index β f) , psup (datum β f))
β‘ = happly (elim-hom .snd) (a , f) beta a f
The datum part of elim-hom
is
almost what we want, but itβs not quite a section of
To get the actual elimination principle, weβll have to get rid of the
index
in its type. The insight now
is that, much like a total
category admits a projection functor to the base, the total
algebra of
should admit a projection morphism to the base
The composition would then be an algebra morphism, inhabiting the contractible type which is also inhabited by the identity. But note that the function part of this composition is exactly so we obtain a homotopy
: β x β index x β‘ x
Ο = happly (ap fst htpy) module Ο where
Ο : alg Wβ alg
Οe .fst = index
Οe .snd i (a , z) = beta a z i .fst
Οe
= is-contrβis-prop (init alg) Οe idW htpy
We can then define the eliminator at by transporting along Since weβll want to characterise the value of using the second component of we can not directly define in terms of the composition Instead, we equip with a bespoke algebra structure, where the proof that comes from the first component of
: β x β P x
elim = subst P (Ο w) (datum w) elim w
Since the construction of works at the level of algebra morphisms, rather than their underlying functions, we obtain a coherence fitting in the diagram below.
To show that commutes with we can then perform a short calculation using the second component of the coherence and some stock facts about substitution.
: (a : A) (f : (x : B a) β W') β elim (sup' (a , f)) β‘ psup (elim β f)
Ξ² =
Ξ² a f let
: β a f β sym (ap fst (beta a f)) β Ο (sup' (a , f)) β‘ ap sup'' (funext Ξ» i β Ο (f i))
Ο = squareβcommutes (Ξ» i j β Ο.htpy j .snd (~ i) (a , z)) β β-idr _
Ο a z in
(Ο (sup'' f)) β datum (sup' (a , f)) β β‘β¨ ap! (from-pathpβ» (ap snd (beta a f))) β©
subst P (Ο (sup'' f)) (subst P (sym (ap fst (beta a f))) (psup (datum β f))) β‘β¨ sym (subst-β P _ _ _) β apβ (subst P) (Ο a f) refl β©
subst P (ap sup'' (funext Ξ» z β Ο (f z))) (psup (datum β f)) β‘β¨ nat index datum (funext Ο) a f β©
subst P (Ξ» z β subst P (Ο (f z)) (datum (f z))) β psup
where
: β {a} (f : B a β W') β W'
sup'' = sup' (_ , f)
sup'' f
: (ix : W' β W') (h : ix β‘ id) (dt : β x β P (ix x)) β _
nat-t =
nat-t ix h dt β a (f : B a β W')
β subst P (ap sup'' (funext Ξ» z β happly h (f z))) (psup (dt β f))
(Ξ» z β subst P (happly h (f z)) (dt (f z)))
β‘ psup
: β ix dt h β nat-t ix h dt
nat = J (Ξ» ix h β β dt β nat-t ix (sym h) dt)
nat ix dt h (Ξ» dt a f β Regularity.fast! refl)
(sym h) dt