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 i with fin-view i ... | zero = refl
The successor operation on indices corresponds to taking coproducts with the unit set.
Finite-successor : Fin (suc n) β (β€ β Fin n) Finite-successor {n} = Fin-suc βe Maybe-is-sum
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)) ββ¨ pathβequiv (ap Fin (sumβ‘* n m)) β©β 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.linv _ β funext Ξ» () .is-iso.rinv fzero β refl .is-iso.rinv (fin (suc i) β¦ forget p β¦) β absurd (Β¬sucβ€0 (β€-peel p)) 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 β zeroβ suc (ap lower (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) = record { fst = fun ; snd = Fin-injectionβequiv _ inj } module inv where fun : Fin (suc _) β Fin (suc _) fun i with fin-view i ... | zero = n ... | suc x = skip n (e # x) inj : injective fun inj {i} {j} p with fin-view i | fin-view j ... | zero | zero = refl ... | zero | suc y = absurd (skip-skips n _ (sym p)) ... | suc i | zero = absurd (skip-skips n _ p) ... | suc i | suc j = 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 p where p : β x β inv.fun (e # 0) (to e .snd) x β‘ e .fst x p x with fin-view x ... | zero = refl ... | suc 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 fn Ξ» (i , j , s) β resp i j s where fn : Fin (suc n) β Fin n / Rβ._βΌ_ fn j with fin-view j ... | zero = inc i ... | suc x = inc x resp : β i j β i R.βΌ j β fn i β‘ fn j resp i j s with fin-view i | fin-view j ... | zero | zero = refl ... | zero | suc y = quot (R.symαΆ r R.βαΆ s) ... | suc x | zero = quot (s R.βαΆ r) ... | suc x | suc y = quot s is .snd .rinv = elim! (Fin-cases (quot (R.symαΆ r)) (Ξ» _ β 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 i with fin-view i ... | zero = inl _ ... | suc x = inr (inc x) resp : β i j β i R.βΌ j β to i β‘ to j resp i j s with fin-view i | fin-view j ... | zero | zero = refl ... | zero | suc y = absurd (Β¬r (y , s)) ... | suc x | zero = absurd (Β¬r (x , R.symαΆ s)) ... | suc x | suc y = ap inr (quot s) 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 Ξ» (x , y , r) β resp x y r is .snd .rinv = elim! (Fin-cases refl (Ξ» _ β 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} {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' : {i j : Fin (suc n)} β Fin-view i β Fin-view j β Type β D' zero zero = 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' zero (suc y) = Ξ£[ x β Fin n ] RΛ’ 0 (fsuc x) Γ Closure Rβ x y D' (suc x) zero = Ξ£[ 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' (suc x) (suc y) = Closure Rβ x y β D' (suc x) zero Γ D' zero (suc 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 i with fin-view i ... | zero = _ ... | suc x = inl Rβ.reflαΆ D-trans : β x y z β D x y β D y z β D x z D-trans i j k p q with fin-view i | fin-view j | fin-view k | p | q ... | zero | zero | z | _ | d = d ... | zero | suc y | zero | _ | _ = _ ... | zero | suc y | suc z | y' , ry , cy | inl c = y' , ry , cy Rβ.βαΆ c ... | zero | suc y | suc z | _ | inr (_ , dz) = dz ... | suc x | zero | zero | d | _ = d ... | suc x | zero | suc z | dx | dy = inr (dx , dy) ... | suc x | suc y | zero | inl c | y' , cy , ry = y' , c Rβ.βαΆ cy , ry ... | suc x | suc y | zero | inr (dx , _) | _ = dx ... | suc x | suc y | suc z | inl c | inl d = inl (c Rβ.βαΆ d) ... | suc x | suc y | suc z | inl c | inr ((y' , cy , ry) , dz) = inr ((y' , c Rβ.βαΆ cy , ry) , dz) ... | suc x | suc y | suc z | inr (dx , (y' , ry , cy)) | inl c = inr (dx , y' , ry , cy Rβ.βαΆ c) ... | suc x | suc y | suc z | inr (dx , dy) | inr (dy' , dz) = inr (dx , dz) D-sym : β {i j} (x : Fin-view i) (y : Fin-view j) β D' x y β D' y x D-sym zero zero _ = _ D-sym zero (suc y) (y' , r , c) = y' , Rβ.symαΆ c , β-comm .fst r D-sym (suc x) zero (x' , c , r) = x' , β-comm .fst r , Rβ.symαΆ c D-sym (suc x) (suc y) (inl r) = inl (Rβ.symαΆ r) D-sym (suc x) (suc y) (inr (dx , dy)) = inr (D-sym zero (suc y) dy , D-sym (suc x) zero 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 (fin-view x) (fin-view y)) {-# INCOHERENT D-Dec #-} D-Dec {i} {j} with fin-view i | fin-view j ... | zero | zero = auto ... | zero | suc y = auto ... | suc x | zero = auto ... | suc x | suc 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 {i} {j} r with fin-view i | fin-view j ... | zero | zero = _ ... | zero | suc y = y , inl r , Rβ.reflαΆ ... | suc x | zero = x , Rβ.reflαΆ , inl r ... | suc x | suc y = inl (inc r) DβR* : β {x y i j} β D' {x} {y} i j β Closure R x y DβR* {i = zero} {j = zero} _ = R.reflαΆ DβR* {i = zero} {j = suc y} (y' , r , c) = RΛ’βR r R.βαΆ RββR c DβR* {i = suc x} {j = zero} (x' , c , r) = RββR c R.βαΆ RΛ’βR r DβR* {i = suc x} {j = suc y} (inl r) = RββR r DβR* {i = suc x} {j = suc y} (inr (dx , dy)) = DβR* {i = suc x} {j = zero} dx R.βαΆ DβR* {i = zero} {suc y} 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.β©οΈ