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.â©ïž