open import 1Lab.Prelude

open import Algebra.Group.Homotopy.BAut

open import Data.Fin.Properties
open import Data.Fin.Closure
open import Data.Fin.Base
open import Data.Nat
open import Data.Sum

open import Meta.Bind

module Data.Fin.Finite where

Finite typesπŸ”—

This module pieces together a couple of pre-existing constructions: In terms of the standard finite sets (which are defined for natural numbers nn) and deloopings of automorphism groups, we construct the type of finite types. By univalence, the space of finite types classifies maps with finite fibres.

But what does it mean for a type to be finite? A naΓ―ve first approach is to define β€œXX is finite” to mean β€œXX is equipped with n:Nn : {\mathbb{N}} and f:[n]≃Xf : [n] \simeq X” but this turns out to be too strong: This doesn’t just equip the type XX with a cardinality, but also with a choice of total order. Additionally, defined like this, the type of finite types is a set!

naΓ―ve-fin-is-set : is-set (Ξ£[ X ∈ Type ] Ξ£[ n ∈ Nat ] Fin n ≃ X)
naΓ―ve-fin-is-set = is-hlevel≃ 2 Ξ£-swapβ‚‚ $
  Σ-is-hlevel 2 (hlevel 2) λ x → is-prop→is-hlevel-suc {n = 1} $
    is-contr→is-prop $ Equiv-is-contr (Fin x)

That’s because, as the proof above shows, it’s equivalent to the type of natural numbers: The type βˆ‘X:Typeβˆ‘n:NΒ [n]≃X \sum_{X : {{\mathrm{Type}}}} \sum_{n : {\mathbb{N}}}\ [n] \simeq X is equivalent to the type βˆ‘n:Nβˆ‘X:Type[n]≃X, \sum_{n : {\mathbb{N}}} \sum_{X : {{\mathrm{Type}}}} [n] \simeq X\text{,} and univalence says (rather directly) that the sum of [n]≃X[n] \simeq X as XX ranges over a universe is contractible, so we’re left with the type of natural numbers.

This simply won’t do: we want the type of finite sets to be equivalent to the (core of the) category of finite sets, where the automorphism group of nn has n!n! elements, not exactly one element. What we do is appeal to a basic intuition: A groupoid is the sum over its connected components, and we have representatives for every connected component (given by the standard finite sets):

Fin-type : Type (lsuc lzero)
Fin-type = Σ[ n ∈ Nat ] BAut (Fin n)

Fin-type-is-groupoid : is-hlevel Fin-type 3
Fin-type-is-groupoid = Ξ£-is-hlevel 3 (hlevel 3) Ξ» _ β†’
  BAut-is-hlevel (Fin _) 2 (hlevel 2)

Informed by this, we now express the correct definition of β€œbeing finite”, namely, being merely equivalent to some standard finite set. Rather than using Ξ£ types for this, we can set up typeclass machinery for automatically deriving boring instances of finiteness, i.e.Β those that follow directly from the closure properties.

record Finite {β„“} (T : Type β„“) : Type β„“ where
  constructor fin
    {cardinality} : Nat
    enumeration   : βˆ₯ T ≃ Fin cardinality βˆ₯

open Finite ⦃ ... ⦄ public
  Finite-Fin : βˆ€ {n} β†’ Finite (Fin n)
  Finite-Γ— : ⦃ Finite A ⦄ β†’ ⦃ Finite B ⦄ β†’ Finite (A Γ— B)
  Finite-⊎ : ⦃ Finite A ⦄ β†’ ⦃ Finite B ⦄ β†’ Finite (A ⊎ B)

    : {P : A β†’ Type β„“} β†’ ⦃ Finite A ⦄ β†’ ⦃ βˆ€ x β†’ Finite (P x) ⦄ β†’ Finite (Ξ£ A P)
    : {P : A β†’ Type β„“} β†’ ⦃ Finite A ⦄ β†’ ⦃ βˆ€ x β†’ Finite (P x) ⦄ β†’ Finite (βˆ€ x β†’ P x)