module Data.Fin.Closure where
Closure of finite setsπ
In this module, we prove that the finite sets are closed under βtypal arithmeticβ: The initial and terminal objects are finite (they have 1 and 0 elements respectively), products of finite sets are finite, coproducts of finite sets are finite, and functions between finite sets are finite. Moreover, these operations all correspond to arithmetic operations on the natural number indices: etc.
Zero, one, successorsπ
The finite set is an initial object, and the finite set is a terminal object:
Finite-zero-is-initial : Fin 0 β β₯ Finite-zero-is-initial .fst () Finite-zero-is-initial .snd .is-eqv () Finite-one-is-contr : is-contr (Fin 1) Finite-one-is-contr .centre = fzero Finite-one-is-contr .paths fzero = refl
The successor operation on indices corresponds to taking coproducts with the unit set.
Finite-successor : Fin (suc n) β (β€ β Fin n) Finite-successor {n} = IsoβEquiv (f , iso g f-g g-f) where f : Fin (suc n) β β€ β Fin n f fzero = inl tt f (fsuc x) = inr x g : β€ β Fin n β Fin (suc n) g (inr x) = fsuc x g (inl _) = fzero f-g : is-right-inverse g f f-g (inr _) = refl f-g (inl _) = refl g-f : is-left-inverse g f g-f fzero = refl g-f (fsuc x) = refl
Additionπ
For binary coproducts, we prove the correspondence with addition in steps, to make the proof clearer:
Finite-coproduct : (Fin n β Fin m) β Fin (n + m) Finite-coproduct {zero} {m} = (Fin 0 β Fin m) ββ¨ β-apl Finite-zero-is-initial β©β (β₯ β Fin m) ββ¨ β-zerol β©β Fin m ββ Finite-coproduct {suc n} {m} = (Fin (suc n) β Fin m) ββ¨ β-apl Finite-successor β©β ((β€ β Fin n) β Fin m) ββ¨ β-assoc β©β (β€ β (Fin n β Fin m)) ββ¨ β-apr (Finite-coproduct {n} {m}) β©β (β€ β Fin (n + m)) ββ¨ Finite-successor eβ»ΒΉ β©β Fin (suc (n + m)) ββ
Sumsπ
We also have a correspondence between βcoproductsβ and βadditionβ in
the iterated case: If you have a family of finite types (represented by
a map to their cardinalities), the dependent sum of that family
is equivalent to the iterated binary sum of the cardinalities:
sum : β n β (Fin n β Nat) β Nat sum zero f = zero sum (suc n) f = f fzero + sum n (f β fsuc) Finite-sum : (B : Fin n β Nat) β Ξ£ (Fin _) (Fin β B) β Fin (sum n B) Finite-sum {zero} B .fst () Finite-sum {zero} B .snd .is-eqv () Finite-sum {suc n} B = Ξ£ (Fin (suc n)) (Fin β B) ββ¨ Fin-suc-Ξ£ β©β Fin (B 0) β Ξ£ (Fin n) (Fin β B β fsuc) ββ¨ β-apr (Finite-sum (B β fsuc)) β©β Fin (B 0) β Fin (sum n (B β fsuc)) ββ¨ Finite-coproduct β©β Fin (sum (suc n) B) ββ
Multiplicationπ
Recall (from middle school) that the product is the same thing as summing together copies of the number Correspondingly, we can use the theorem above for general sums to establish the case of binary products:
Finite-multiply : (Fin n Γ Fin m) β Fin (n * m) Finite-multiply {n} {m} = (Fin n Γ Fin m) ββ¨ Finite-sum (Ξ» _ β m) β©β Fin (sum n (Ξ» _ β m)) ββ¨ cast (sumβ‘* n m) , cast-is-equiv _ β©β Fin (n * m) ββ where sumβ‘* : β n m β sum n (Ξ» _ β m) β‘ n * m sumβ‘* zero m = refl sumβ‘* (suc n) m = ap (m +_) (sumβ‘* n m)
Productsπ
Similarly to the case for sums, the cardinality of a dependent
product of finite sets is the product of the
cardinalities:
product : β n β (Fin n β Nat) β Nat product zero f = 1 product (suc n) f = f fzero * product n (f β fsuc) Finite-product : (B : Fin n β Nat) β (β x β Fin (B x)) β Fin (product n B) Finite-product {zero} B .fst _ = fzero Finite-product {zero} B .snd = is-isoβis-equiv Ξ» where .is-iso.inv _ () .is-iso.rinv fzero β refl .is-iso.linv _ β funext Ξ» () Finite-product {suc n} B = (β x β Fin (B x)) ββ¨ Fin-suc-Ξ β©β Fin (B fzero) Γ (β x β Fin (B (fsuc x))) ββ¨ Ξ£-ap-snd (Ξ» _ β Finite-product (B β fsuc)) β©β Fin (B fzero) Γ Fin (product n (B β fsuc)) ββ¨ Finite-multiply β©β Fin (B fzero * product n (B β fsuc)) ββ
Permutationsπ
We show that the set of permutations
is finite with cardinality
(the factorial of
We start by showing that a permutation of is determined by what happens to and by the remaining permutation of
Fin-permutations-suc : β n β (Fin (suc n) β Fin (suc n)) β (Fin (suc n) Γ (Fin n β Fin n)) Fin-permutations-suc n = to , is-isoβis-equiv is where to : (Fin (suc n) β Fin (suc n)) β Fin (suc n) Γ (Fin n β Fin n) to e .fst = e # 0 to e .snd .fst i = avoid (e # 0) (e # (fsuc i)) Ξ» p β fzeroβ fsuc (Equiv.injective e p) to e .snd .snd = Fin-injectionβequiv _ Ξ» p β fsuc-inj (Equiv.injective e (avoid-injective (e # 0) p)) is : is-iso to is .is-iso.inv (n , e) .fst fzero = n is .is-iso.inv (n , e) .fst (fsuc x) = skip n (e # x) is .is-iso.inv (n , e) .snd = Fin-injectionβequiv _ Ξ» where {fzero} {fzero} p β refl {fzero} {fsuc y} p β absurd (skip-skips n _ (sym p)) {fsuc x} {fzero} p β absurd (skip-skips n _ p) {fsuc x} {fsuc y} p β ap fsuc (Equiv.injective e (skip-injective n _ _ p)) is .is-iso.rinv (n , e) = Ξ£-pathp refl (ext Ξ» i β avoid-skip n (e # i)) is .is-iso.linv e = ext Ξ» where fzero β refl (fsuc i) β skip-avoid (e # 0) (e # (fsuc i))
We can now show that by induction.
Fin-permutations : β n β (Fin n β Fin n) β Fin (factorial n) Fin-permutations zero = is-contrββ (contr idβ Ξ» _ β ext Ξ» ()) Finite-one-is-contr Fin-permutations (suc n) = Fin (suc n) β Fin (suc n) ββ¨ Fin-permutations-suc n β©β Fin (suc n) Γ (Fin n β Fin n) ββ¨ Ξ£-ap-snd (Ξ» _ β Fin-permutations n) β©β Fin (suc n) Γ Fin (factorial n) ββ¨ Finite-multiply β©β Fin (factorial (suc n)) ββ
Decidable subsetsπ
Given a decidable predicate on we can compute such that is equivalent to the subset of on which the predicate holds: a decidable proposition is finite (it has either or elements), so we can reuse our proof that finite sums of finite types are finite.
DecβFin : β {β} {A : Type β} β is-prop A β Dec A β Ξ£ Nat Ξ» n β Fin n β A DecβFin ap (no Β¬a) .fst = 0 DecβFin ap (no Β¬a) .snd = is-emptyββ (Finite-zero-is-initial .fst) Β¬a DecβFin ap (yes a) .fst = 1 DecβFin ap (yes a) .snd = is-contrββ Finite-one-is-contr (is-propββis-contr ap a) Finite-subset : β {n} (P : Fin n β Type β) β β¦ β {x} β H-Level (P x) 1 β¦ β β¦ dec : β {x} β Dec (P x) β¦ β Ξ£ Nat Ξ» s β Fin s β Ξ£ (Fin n) P Finite-subset {n = n} P β¦ dec = dec β¦ = sum n ns , Finite-sum ns eβ»ΒΉ βe Ξ£-ap-snd es where ns : Fin n β Nat ns i = DecβFin (hlevel 1) dec .fst es : (i : Fin n) β Fin (ns i) β P i es i = DecβFin (hlevel 1) dec .snd
Decidable quotientsπ
As a first step towards coequalisers, we show that the quotient of a finite set by a decidable congruence is finite.
Finite-quotient : β {n β} (R : Congruence (Fin n) β) (open Congruence R) β β¦ _ : β {x y} β Dec (x βΌ y) β¦ β Ξ£ Nat Ξ» q β Fin q β Fin n / _βΌ_
This amounts to counting the number of equivalence classes of We proceed by induction on the base case is trivial.
Finite-quotient {zero} R .fst = 0 Finite-quotient {zero} R .snd .fst () Finite-quotient {zero} R .snd .snd .is-eqv = elim! Ξ» ()
For the induction step, we restrict along the successor map to get a congruence on whose quotient is finite.
Finite-quotient {suc n} {β} R = go where module R = Congruence R Rβ : Congruence (Fin n) β Rβ = Congruence-pullback fsuc R module Rβ = Congruence Rβ n/Rβ : Ξ£ Nat Ξ» q β Fin q β Fin n / Rβ._βΌ_ n/Rβ = Finite-quotient {n} Rβ
In order to compute the size of the quotient we decide whether is related by to any using the omniscience of finite sets.
go : β¦ Dec (Ξ£ (Fin n) (Ξ» i β fzero R.βΌ fsuc i)) β¦ β Ξ£ Nat (Ξ» q β Fin q β Fin (suc n) / R._βΌ_)
If so, lives in the same equivalence class as and the size of the quotient remains unchanged.
go β¦ yes (i , r) β¦ .fst = n/Rβ .fst go β¦ yes (i , r) β¦ .snd = n/Rβ .snd βe IsoβEquiv is where is : Iso (Fin n / Rβ._βΌ_) (Fin (suc n) / R._βΌ_) is .fst = Coeq-rec (Ξ» x β inc (fsuc x)) Ξ» (x , y , s) β quot s is .snd .inv = Coeq-rec (Ξ» where fzero β inc i (fsuc x) β inc x) (Ξ» where (fzero , fzero , s) β refl (fzero , fsuc y , s) β quot (R.symαΆ r R.βαΆ s) (fsuc x , fzero , s) β quot (s R.βαΆ r) (fsuc x , fsuc y , s) β quot s) is .snd .rinv = elim! Ξ» where fzero β quot (R.symαΆ r) (fsuc x) β refl is .snd .linv = elim! Ξ» _ β refl
Otherwise, creates a new equivalence class for itself.
go β¦ no Β¬r β¦ .fst = suc (n/Rβ .fst) go β¦ no Β¬r β¦ .snd = Finite-successor βe β-apr (n/Rβ .snd) βe IsoβEquiv is where to : Fin (suc n) β β€ β (Fin n / Rβ._βΌ_) to fzero = inl _ to (fsuc x) = inr (inc x) is : Iso (β€ β (Fin n / Rβ._βΌ_)) (Fin (suc n) / R._βΌ_) is .fst (inl tt) = inc 0 is .fst (inr x) = Coeq-rec (Ξ» x β inc (fsuc x)) (Ξ» (x , y , s) β quot s) x is .snd .inv = Coeq-rec to Ξ» where (fzero , fzero , s) β refl (fzero , fsuc y , s) β absurd (Β¬r (y , s)) (fsuc x , fzero , s) β absurd (Β¬r (x , R.symαΆ s)) (fsuc x , fsuc y , s) β ap inr (quot s) is .snd .rinv = elim! go' where go' : β x β is .fst (to x) β‘ inc x go' fzero = refl go' (fsuc _) = refl is .snd .linv (inl tt) = refl is .snd .linv (inr x) = elim x where elim : β x β is .snd .inv (is .fst (inr x)) β‘ inr x elim = elim! Ξ» _ β refl
Coequalisersπ
Given two functions we can compute such that is equivalent to the coequaliser of and We start by expressing the coequaliser as the quotient of by the congruence generated by the relation so that we can apply the result above. Since this relation is clearly decidable by the omniscience of all that remains is to show that the closure of a decidable relation on a finite set is decidable.
instance Closure-Fin-Dec : β {n β} {R : Fin n β Fin n β Type β} β β¦ β {x y} β Dec (R x y) β¦ β β {x y} β Dec (Closure R x y)
This amounts to writing a (verified!) pathfinding algorithm: given we need to decide whether there is a path between and in the undirected graph whose edges are given by
We proceed by induction on the base case is trivial, so we are left with the inductive case The simplest1 way forward is to find a decidable congruence that is equivalent to the closure
We start by defining the restriction of along the successor map written as well as the symmetric closure of written
Closure-Fin-Dec {suc n} {R = R} {x} {y} = R*-dec where open Congruence module R = Congruence (Closure-congruence R) Rβ : Fin n β Fin n β Type _ Rβ x y = R (fsuc x) (fsuc y) module Rβ = Congruence (Closure-congruence Rβ) RββR : β {x y} β Closure Rβ x y β Closure R (fsuc x) (fsuc y) RββR = Closure-rec-congruence Rβ (Congruence-pullback fsuc (Closure-congruence R)) inc RΛ’ : Fin (suc n) β Fin (suc n) β Type _ RΛ’ x y = R x y β R y x RΛ’βR : β {x y} β RΛ’ x y β Closure R x y RΛ’βR = [ inc , R.symαΆ β inc ]
We build by cases. is trivial, since the closure is reflexive.
D : Fin (suc n) β Fin (suc n) β Type _ D fzero fzero = Lift _ β€
For we use the omniscience of to search for an such that and Here we rely on the closure of being decidable by the induction hypothesis. The case is symmetric.
D fzero (fsuc y) = Ξ£[ x β Fin n ] RΛ’ 0 (fsuc x) Γ Closure Rβ x y D (fsuc x) fzero = Ξ£[ y β Fin n ] Closure Rβ x y Γ RΛ’ (fsuc y) 0
Finally, in order to decide whether and are related by we have two possibilities: either there is a path from to in which we can find using the induction hypothesis, or there are are paths from to and from to in which we can find using the previous two cases.
D (fsuc x) (fsuc y) = Closure Rβ x y β D (fsuc x) 0 Γ D 0 (fsuc y)
Proving that (the propositional
truncation of)
is a decidable congruence is tedious but not difficult.
D-cong : Congruence (Fin (suc n)) _
instance D-Dec : β {x y} β Dec (D x y)
D-refl : β x β D x x D-refl fzero = _ D-refl (fsuc x) = inl Rβ.reflαΆ D-trans : β x y z β D x y β D y z β D x z D-trans fzero fzero z _ d = d D-trans fzero (fsuc y) fzero _ _ = _ D-trans fzero (fsuc y) (fsuc z) (y' , ry , cy) (inl c) = y' , ry , cy Rβ.βαΆ c D-trans fzero (fsuc y) (fsuc z) _ (inr (_ , dz)) = dz D-trans (fsuc x) fzero fzero d _ = d D-trans (fsuc x) fzero (fsuc z) dx dy = inr (dx , dy) D-trans (fsuc x) (fsuc y) fzero (inl c) (y' , cy , ry) = y' , c Rβ.βαΆ cy , ry D-trans (fsuc x) (fsuc y) fzero (inr (dx , _)) _ = dx D-trans (fsuc x) (fsuc y) (fsuc z) (inl c) (inl d) = inl (c Rβ.βαΆ d) D-trans (fsuc x) (fsuc y) (fsuc z) (inl c) (inr ((y' , cy , ry) , dz)) = inr ((y' , c Rβ.βαΆ cy , ry) , dz) D-trans (fsuc x) (fsuc y) (fsuc z) (inr (dx , (y' , ry , cy))) (inl c) = inr (dx , y' , ry , cy Rβ.βαΆ c) D-trans (fsuc x) (fsuc y) (fsuc z) (inr (dx , dy)) (inr (dy' , dz)) = inr (dx , dz) D-sym : β x y β D x y β D y x D-sym fzero fzero _ = _ D-sym fzero (fsuc y) (y' , r , c) = y' , Rβ.symαΆ c , β-comm .fst r D-sym (fsuc x) fzero (x' , c , r) = x' , β-comm .fst r , Rβ.symαΆ c D-sym (fsuc x) (fsuc y) (inl r) = inl (Rβ.symαΆ r) D-sym (fsuc x) (fsuc y) (inr (dx , dy)) = inr (D-sym fzero (fsuc y) dy , D-sym (fsuc x) fzero dx) D-cong ._βΌ_ x y = β₯ D x y β₯ D-cong .has-is-prop _ _ = hlevel 1 D-cong .reflαΆ {x} = inc (D-refl x) D-cong ._βαΆ_ {x} {y} {z} = β₯-β₯-mapβ (D-trans x y z) D-cong .symαΆ {x} {y} = map (D-sym x y) {-# INCOHERENT D-Dec #-} D-Dec {fzero} {fzero} = auto D-Dec {fzero} {fsuc y} = auto D-Dec {fsuc x} {fzero} = auto D-Dec {fsuc x} {fsuc y} = auto
To complete the proof, we show that is indeed equivalent to it suffices to show that lies between and
RβD : β {x y} β R x y β D x y RβD {fzero} {fzero} _ = _ RβD {fzero} {fsuc y} r = y , inl r , Rβ.reflαΆ RβD {fsuc x} {fzero} r = x , Rβ.reflαΆ , inl r RβD {fsuc x} {fsuc y} r = inl (inc r) DβR* : β {x y} β D x y β Closure R x y DβR* {fzero} {fzero} _ = R.reflαΆ DβR* {fzero} {fsuc y} (y' , r , c) = RΛ’βR r R.βαΆ RββR c DβR* {fsuc x} {fzero} (x' , c , r) = RββR c R.βαΆ RΛ’βR r DβR* {fsuc x} {fsuc y} (inl r) = RββR r DβR* {fsuc x} {fsuc y} (inr (dx , dy)) = DβR* dx R.βαΆ DβR* {fzero} dy R*βD : β {x y} β Closure R x y β β₯ D x y β₯ R*βD = Closure-rec-congruence R D-cong (inc β RβD) R*-dec : Dec (Closure R x y) R*-dec = invmap (rec! DβR*) R*βD (holds? β₯ D x y β₯)
We can finally assemble the pieces together: given the coequaliser of and is equivalent to the quotient of by the decidable relation induced by and and hence by its congruence closure But we know that quotients of finite sets by decidable congruences are finite, and we just proved that the closure of a decidable relation on a finite set is decidable, so weβre done.
Finite-coequaliser : β {n m} (f g : Fin m β Fin n) β Ξ£ Nat Ξ» q β Fin q β Coeq f g Finite-coequaliser {n} f g = n/R .fst , n/R .snd βe Closure-quotient R eβ»ΒΉ βe Coeqβquotient f g eβ»ΒΉ where R = spanβR f g n/R : Ξ£ Nat Ξ» q β Fin q β Fin n / Closure R n/R = Finite-quotient (Closure-congruence R)
In terms of ease of implementation; the complexity of the resulting algorithm is catastrophic.β©οΈ