module Data.Fin.Properties where

Finite sets - propertiesπŸ”—

OrderingπŸ”—

As noted in Data.Fin.Base, we’ve set up the ordering on Fin so that we can re-use all the proofs about the ordering on Nat.

However, there are still quite a few interesting things one can say about skip and squish. In particular, we can prove the simplicial identities, which characterize the interactions between these two functions.

These lemmas might seem somewhat arbitrary and complicated, which is true! However, they are enough to describe all the possible interactions of skip and squish, which in turn are the building blocks for every monotone function between Fin, so it’s not that surprising that they would be a bit of a mess!

skip-comm : βˆ€ {n} (i j : Fin (suc n)) β†’ i ≀ j
          β†’ βˆ€ x β†’ skip (weaken i) (skip j x) ≑ skip (fsuc j) (skip i x)
skip-comm fzero    j        le x        = refl
skip-comm (fsuc i) (fsuc j) le fzero    = refl
skip-comm (fsuc i) (fsuc j) (Nat.s≀s le) (fsuc x) = ap fsuc (skip-comm i j le x)

drop-comm : βˆ€ {n} (i j : Fin n) β†’ i ≀ j
          β†’ βˆ€ x β†’ squish j (squish (weaken i) x) ≑ squish i (squish (fsuc j) x)
drop-comm fzero    fzero    le fzero = refl
drop-comm fzero    fzero    le (fsuc x) = refl
drop-comm fzero    (fsuc j) le fzero = refl
drop-comm fzero    (fsuc j) le (fsuc x) = refl
drop-comm (fsuc i) (fsuc j) le fzero = refl
drop-comm (fsuc i) (fsuc j) (Nat.s≀s le) (fsuc x) = ap fsuc (drop-comm i j le x)

squish-skip-comm : βˆ€ {n} (i : Fin (suc n)) (j : Fin n) β†’ i < fsuc j
                 β†’ βˆ€ x β†’ squish (fsuc j) (skip (weaken i) x) ≑ skip i (squish j x)
squish-skip-comm fzero j (Nat.s≀s p) x = refl
squish-skip-comm (fsuc i) (fsuc j) (Nat.s≀s p) fzero = refl
squish-skip-comm (fsuc i) (fsuc j) (Nat.s≀s p) (fsuc x) =
  ap fsuc (squish-skip-comm i j p x)

squish-skip : βˆ€ {n} (i j : Fin n) β†’ i ≑ j
            β†’ βˆ€ x β†’ squish j (skip (weaken j) x) ≑ x
squish-skip fzero fzero p x = refl
squish-skip fzero (fsuc j) p x = absurd (fzero≠fsuc p)
squish-skip (fsuc i) fzero p x = refl
squish-skip (fsuc i) (fsuc j) p fzero = refl
squish-skip (fsuc i) (fsuc j) p (fsuc x) = ap fsuc (squish-skip i j (fsuc-inj p) x)

squish-skip-fsuc : βˆ€ {n} (i : Fin (suc n)) (j : Fin n) β†’ i ≑ fsuc j
                 β†’ βˆ€ x β†’ squish j (skip i x) ≑ x
squish-skip-fsuc fzero fzero p x = refl
squish-skip-fsuc fzero (fsuc j) p x = absurd (fzero≠fsuc p)
squish-skip-fsuc (fsuc i) fzero p fzero = refl
squish-skip-fsuc (fsuc fzero) fzero p (fsuc x) = refl
squish-skip-fsuc (fsuc (fsuc i)) fzero p (fsuc x) =
  absurd (fzero≠fsuc (fsuc-inj (sym p)))
squish-skip-fsuc (fsuc i) (fsuc j) p fzero = refl
squish-skip-fsuc (fsuc i) (fsuc j) p (fsuc x) =
  ap fsuc (squish-skip-fsuc i j (fsuc-inj p) x)

Fin-peel : βˆ€ {l k} β†’ Fin (suc l) ≃ Fin (suc k) β†’ Fin l ≃ Fin k
Fin-peel {l} {k} sl≃sk = (Isoβ†’Equiv (lβ†’k , (iso kβ†’l bβ†’aβ†’b aβ†’bβ†’a))) where
  sk≃sl : Fin (suc k) ≃ Fin (suc l)
  sk≃sl = sl≃sk e⁻¹
  module sl≃sk = Equiv sl≃sk
  module sk≃sl = Equiv sk≃sl

  l→k : Fin l → Fin k
  lβ†’k x with inspect (sl≃sk.to (fsuc x))
  ... | fsuc y , _ = y
  ... | fzero , p with inspect (sl≃sk.to fzero)
  ... | fsuc y , _ = y
  ... | fzero , q = absurd (fzeroβ‰ fsuc (sl≃sk.injectiveβ‚‚ q p))

  k→l : Fin k → Fin l
  kβ†’l x with inspect (sk≃sl.to (fsuc x))
  ... | fsuc x , _ = x
  ... | fzero , p with inspect (sk≃sl.to fzero)
  ... | fsuc y , _ = y
  ... | fzero , q = absurd (fzeroβ‰ fsuc (sk≃sl.injectiveβ‚‚ q p))

  absurd-path : βˆ€ {β„“} {A : Type β„“} {y : A} .{x : βŠ₯} β†’ absurd x ≑ y
  absurd-path {x = ()}

  aβ†’bβ†’a : βˆ€ a β†’ kβ†’l (lβ†’k a) ≑ a
  aβ†’bβ†’a a with inspect (sl≃sk.to (fsuc a))
  aβ†’bβ†’a a | fsuc x , p' with inspect (sk≃sl.to (fsuc x))
  a→b→a a | fsuc x , p' | fsuc y , q' = fsuc-inj (
    sym q' βˆ™ ap (sk≃sl.to) (sym p') βˆ™ sl≃sk.Ξ· _)
  a→b→a a | fsuc x , p' | fzero , q' = absurd contra where
    r = sl≃sk.injectiveβ‚‚ p' (sl≃sk.Ξ΅ (fsuc x))
    contra = fzeroβ‰ fsuc (sym (r βˆ™ q'))
  aβ†’bβ†’a a | fzero , p' with inspect (sl≃sk.to fzero)
  aβ†’bβ†’a a | fzero , p' | fsuc x , q' with inspect (sk≃sl.to (fsuc x))
  a→b→a a | fzero , p' | fsuc x , q' | fsuc y , r' = absurd do
    fzeroβ‰ fsuc (sym (sym r' βˆ™ ap sk≃sl.to (sym q') βˆ™ sl≃sk.Ξ· fzero))
  aβ†’bβ†’a a | fzero , p' | fsuc x , q' | fzero , r' with inspect (sk≃sl.to fzero)
  a→b→a a | fzero , p' | fsuc x , q' | fzero , r' | fsuc z , s = fsuc-inj $
    sym s βˆ™ ap sk≃sl.to (sym p') βˆ™ sl≃sk.Ξ· (fsuc a)
  a→b→a a | fzero , p' | fsuc x , q' | fzero , r' | fzero , s = absurd-path
  a→b→a a | fzero , p' | fzero , q' = absurd (fzero≠fsuc $
    sl≃sk.injectiveβ‚‚ q' p')

  bβ†’aβ†’b : βˆ€ b β†’ lβ†’k (kβ†’l b) ≑ b
  bβ†’aβ†’b b with inspect (sk≃sl.to (fsuc b))
  bβ†’aβ†’b b | fsuc x , p' with inspect (sl≃sk.to (fsuc x))
  b→a→b b | fsuc x , p' | fsuc y , q' = fsuc-inj $
    sym q' βˆ™ ap (sl≃sk.to) (sym p') βˆ™ sk≃sl.Ξ· _
  b→a→b b | fsuc x , p' | fzero , q' = absurd contra where
    r = sk≃sl.injectiveβ‚‚ p' (sk≃sl.Ξ΅ (fsuc x))
    contra = fzeroβ‰ fsuc (sym (r βˆ™ q'))
  bβ†’aβ†’b b | fzero , p' with inspect (sk≃sl.to fzero)
  bβ†’aβ†’b b | fzero , p' | fsuc x , q' with inspect (sl≃sk.to (fsuc x))
  b→a→b b | fzero , p' | fsuc x , q' | fsuc y , r'  = absurd (fzero≠fsuc $
    sym (sym r' βˆ™ ap (sl≃sk.to) (sym q') βˆ™ sk≃sl.Ξ· _))
  bβ†’aβ†’b b | fzero , p' | fsuc x , q' | fzero , r' with inspect (sl≃sk.to fzero)
  b→a→b a | fzero , p' | fsuc x , q' | fzero , r' | fsuc z , s = fsuc-inj $
    sym s βˆ™ ap (sl≃sk.to) (sym p') βˆ™ sk≃sl.Ξ· (fsuc a)
  b→a→b a | fzero , p' | fsuc x , q' | fzero , r' | fzero , s = absurd-path
  b→a→b b | fzero , p' | fzero , q' = absurd (fzero≠fsuc $
    sk≃sl.injectiveβ‚‚ q' p')

Fin-injective : βˆ€ {l k} β†’ Fin l ≃ Fin k β†’ l ≑ k
Fin-injective {zero} {zero} l≃k = refl
Fin-injective {zero} {suc k} l≃k with equivβ†’inverse (l≃k .snd) fzero
... | ()
Fin-injective {suc l} {zero} l≃k with l≃k .fst fzero
... | ()
Fin-injective {suc l} {suc k} sl≃sk = ap suc $ Fin-injective (Fin-peel sl≃sk)

to-from-β„•< : βˆ€ {n} (x : β„•< n) β†’ to-β„•< {n = n} (from-β„•< x) ≑ x
to-from-β„•< {n = suc n} x = Ξ£-prop-path! (to-from-β„• {n = suc n} x) where
  to-from-β„• : βˆ€ {n} x β†’ to-nat {n = n} (from-β„•< x) ≑ x .fst
  to-from-β„• {n = suc n} (zero , p) = refl
  to-from-β„• {n = suc n} (suc x , Nat.s≀s p) = ap suc (to-from-β„• {n = n} (x , p))

from-to-β„•< : βˆ€ {n} (x : Fin n) β†’ from-β„•< (to-β„•< x) ≑ x
from-to-β„•< fzero = refl
from-to-β„•< (fsuc x) = ap fsuc (from-to-β„•< x)

Fin≃ℕ< : βˆ€ {n} β†’ Fin n ≃ β„•< n
Fin≃ℕ< {n} = to-β„•< , is-isoβ†’is-equiv (iso from-β„•< (to-from-β„•< {n}) from-to-β„•<)

avoid-injective
  : βˆ€ {n} (i : Fin (suc n)) {j k : Fin (suc n)} {iβ‰ j : i β‰  j} {iβ‰ k : i β‰  k}
  β†’ avoid i j iβ‰ j ≑ avoid i k iβ‰ k β†’ j ≑ k
avoid-injective fzero {fzero} {k} {i≠j} p = absurd (i≠j refl)
avoid-injective fzero {fsuc j} {fzero} {i≠k = i≠k} p = absurd (i≠k refl)
avoid-injective {suc n} fzero {fsuc j} {fsuc k} p = ap fsuc p
avoid-injective {suc n} (fsuc i) {fzero} {fzero} p = refl
avoid-injective {suc n} (fsuc i) {fzero} {fsuc k} p = absurd (fzero≠fsuc p)
avoid-injective {suc n} (fsuc i) {fsuc j} {fzero} p = absurd (fzero≠fsuc (sym p))
avoid-injective {suc n} (fsuc i) {fsuc j} {fsuc k} p =
  ap fsuc (avoid-injective {n} i {j} {k} (fsuc-inj p))

skip-injective
  : βˆ€ {n} (i : Fin (suc n)) (j k : Fin n)
  β†’ skip i j ≑ skip i k β†’ j ≑ k
skip-injective fzero j k p = fsuc-inj p
skip-injective (fsuc i) fzero fzero p = refl
skip-injective (fsuc i) fzero (fsuc k) p = absurd (fzero≠fsuc p)
skip-injective (fsuc i) (fsuc j) fzero p = absurd (fzero≠fsuc (sym p))
skip-injective (fsuc i) (fsuc j) (fsuc k) p = ap fsuc (skip-injective i j k (fsuc-inj p))

skip-skips
  : βˆ€ {n} (i : Fin (suc n)) (j : Fin n)
  β†’ skip i j β‰  i
skip-skips fzero j p = fzero≠fsuc (sym p)
skip-skips (fsuc i) fzero p = fzero≠fsuc p
skip-skips (fsuc i) (fsuc j) p = skip-skips i j (fsuc-inj p)

avoid-skip
  : βˆ€ {n} (i : Fin (suc n)) (j : Fin n) {neq : i β‰  skip i j}
  β†’ avoid i (skip i j) neq ≑ j
avoid-skip fzero fzero = refl
avoid-skip fzero (fsuc j) = refl
avoid-skip (fsuc i) fzero = refl
avoid-skip (fsuc i) (fsuc j) = ap fsuc (avoid-skip i j)

skip-avoid
  : βˆ€ {n} (i : Fin (suc n)) (j : Fin (suc n)) {iβ‰ j : i β‰  j}
  β†’ skip i (avoid i j iβ‰ j) ≑ j
skip-avoid fzero fzero {i≠j} = absurd (i≠j refl)
skip-avoid {suc n} fzero (fsuc j) = refl
skip-avoid {suc n} (fsuc i) fzero = refl
skip-avoid {suc n} (fsuc i) (fsuc j) = ap fsuc (skip-avoid i j)

Iterated products and sumsπŸ”—

We can break down and over finite sets as iterated products and sums, respectively.

Fin-suc-Ξ 
  : βˆ€ {β„“} {n} {A : Fin (suc n) β†’ Type β„“}
  β†’ (βˆ€ x β†’ A x) ≃ (A fzero Γ— (βˆ€ x β†’ A (fsuc x)))
Fin-suc-Π = Iso→Equiv λ where
  .fst f β†’ f fzero , (Ξ» x β†’ f (fsuc x))

  .snd .is-iso.inv (z , s) β†’ fin-cons z s

  .snd .is-iso.rinv x β†’ refl

  .snd .is-iso.linv k i fzero    β†’ k fzero
  .snd .is-iso.linv k i (fsuc n) β†’ k (fsuc n)

Fin-suc-Ξ£
  : βˆ€ {β„“} {n} {A : Fin (suc n) β†’ Type β„“}
  β†’ Ξ£ (Fin (suc n)) A ≃ (A fzero ⊎ Ξ£ (Fin n) (A ∘ fsuc))
Fin-suc-Σ = Iso→Equiv λ where
  .fst (fzero , a) β†’ inl a
  .fst (fsuc x , a) β†’ inr (x , a)

  .snd .is-iso.inv (inl a) β†’ fzero , a
  .snd .is-iso.inv (inr (x , a)) β†’ fsuc x , a

  .snd .is-iso.rinv (inl _) β†’ refl
  .snd .is-iso.rinv (inr _) β†’ refl

  .snd .is-iso.linv (fzero , a) β†’ refl
  .snd .is-iso.linv (fsuc x , a) β†’ refl

Finite choiceπŸ”—

An important fact about the (standard) finite sets in constructive mathematics is that they always support choice, which we phrase below as a β€œsearch” operator: if is any Monoidal functor on types, then it commutes with products. Since over are iterated products, we have that commutes with

Fin-Monoidal
  : βˆ€ {β„“} n {A : Fin n β†’ Type β„“} {M}
      (let module M = Effect M)
  β†’ ⦃ Monoidal M ⦄
  β†’ (βˆ€ x β†’ M.β‚€ (A x)) β†’ M.β‚€ (βˆ€ x β†’ A x)
Fin-Monoidal zero _ = invmap (Ξ» _ ()) _ munit
Fin-Monoidal (suc n) k =
  Fin-suc-Ξ  e⁻¹ <≃> (k 0 <,> Fin-Monoidal n (k ∘ fsuc))

In particular, instantiating with the propositional truncation (which is an Idiom and hence Monoidal), we get a version of the axiom of choice for finite sets.

finite-choice
  : βˆ€ {β„“} n {A : Fin n β†’ Type β„“}
  β†’ (βˆ€ x β†’ βˆ₯ A x βˆ₯) β†’ βˆ₯ (βˆ€ x β†’ A x) βˆ₯
finite-choice n = Fin-Monoidal n

An immediate consequence is that surjections into a finite set (thus, between finite sets) merely split:

finite-surjection-split
  : βˆ€ {β„“} {n} {B : Type β„“}
  β†’ (f : B β†’ Fin n) β†’ is-surjective f
  β†’ βˆ₯ (βˆ€ x β†’ fibre f x) βˆ₯
finite-surjection-split f = finite-choice _

Dually, we have that any Alternative functor commutes with on finite sets, since those are iterated sums.

Fin-Alternative
  : βˆ€ {β„“} n {A : Fin n β†’ Type β„“} {M}
      (let module M = Effect M)
  β†’ ⦃ Alternative M ⦄
  β†’ (βˆ€ x β†’ M.β‚€ (A x)) β†’ M.β‚€ (Ξ£ (Fin n) A)
Fin-Alternative zero _ = invmap (Ξ» ()) (Ξ» ()) empty
Fin-Alternative (suc n) k =
  Fin-suc-Ξ£ e⁻¹ <≃> (k 0 <+> Fin-Alternative n (k ∘ fsuc))

As a consequence, instantiating with Dec, we get that finite sets are exhaustible and omniscient, which means that any family of decidable types indexed by a finite sets yields decidable and respectively.

instance
  Dec-Fin-βˆ€
    : βˆ€ {n β„“} {A : Fin n β†’ Type β„“}
    β†’ ⦃ βˆ€ {x} β†’ Dec (A x) ⦄ β†’ Dec (βˆ€ x β†’ A x)
  Dec-Fin-βˆ€ {n} ⦃ d ⦄ = Fin-Monoidal n (Ξ» _ β†’ d)

  Dec-Fin-Ξ£
    : βˆ€ {n β„“} {A : Fin n β†’ Type β„“}
    β†’ ⦃ βˆ€ {x} β†’ Dec (A x) ⦄ β†’ Dec (Ξ£ (Fin n) A)
  Dec-Fin-Ξ£ {n} ⦃ d ⦄ = Fin-Alternative n Ξ» _ β†’ d
Fin-omniscience
  : βˆ€ {n β„“} (P : Fin n β†’ Type β„“) ⦃ _ : βˆ€ {x} β†’ Dec (P x) ⦄
  β†’ (Ξ£[ j ∈ Fin n ] P j Γ— βˆ€ k β†’ P k β†’ j ≀ k) ⊎ (βˆ€ x β†’ Β¬ P x)
Fin-omniscience {zero} P = inr Ξ» ()
Fin-omniscience {suc n} P with holds? (P 0)
... | yes here = inl (0 , here , Ξ» _ _ β†’ 0≀x)
... | no ¬here with Fin-omniscience (P ∘ fsuc)
... | inl (ix , pix , least) = inl (fsuc ix , pix , fin-cons (Ξ» here β†’ absurd (Β¬here here)) Ξ» i pi β†’ Nat.s≀s (least i pi))
... | inr nowhere = inr (fin-cons Β¬here nowhere)

Injections and surjectionsπŸ”—

The standard finite sets are Dedekind-finite, which means that every injection is a bijection. We prove this by a straightforward but annoying induction on

Fin-injection→equiv
  : βˆ€ {n} (f : Fin n β†’ Fin n)
  β†’ injective f β†’ is-equiv f
Fin-injection→equiv {zero} f inj .is-eqv ()
Fin-injectionβ†’equiv {suc n} f inj .is-eqv i with f 0 ≑? i
... | yes p = contr (0 , p) Ξ» (j , p') β†’ Ξ£-prop-path! (inj (p βˆ™ sym p'))
... | no Β¬p = contr
  (fsuc (rec .centre .fst) , avoid-injective (f 0) (rec .centre .snd))
  Ξ» where
    (fzero , p) β†’ absurd (Β¬p p)
    (fsuc j , p) β†’ Ξ£-prop-path! (ap (fsuc ∘ fst)
      (rec .paths (j , apβ‚‚ (avoid (f 0)) p prop!)))
  where
    rec = Fin-injection→equiv {n}
      (Ξ» x β†’ avoid (f 0) (f (fsuc x)) (fzeroβ‰ fsuc ∘ inj))
      (Ξ» p β†’ fsuc-inj (inj (avoid-injective (f 0) p)))
      .is-eqv (avoid (f 0) i Β¬p)

Since every surjection between finite sets splits, any surjection has an injective right inverse, which is thus a bijection; by general properties of equivalences, this implies that is also a bijection.

Fin-surjection→equiv
  : βˆ€ {n} (f : Fin n β†’ Fin n)
  β†’ is-surjective f β†’ is-equiv f
Fin-surjection→equiv f surj = case finite-surjection-split f surj of λ split →
  left-inverseβ†’equiv (snd ∘ split)
    (Fin-injectionβ†’equiv (fst ∘ split)
      (right-inverseβ†’injective f (snd ∘ split)))

Vector operationsπŸ”—

avoid-insert
  : βˆ€ {n} {β„“} {A : Type β„“}
  β†’ (ρ : Fin n β†’ A)
  β†’ (i : Fin (suc n)) (a : A)
  β†’ (j : Fin (suc n))
  → (i≠j : i ≠ j)
  β†’ (ρ [ i ≔ a ]) j ≑ ρ (avoid i j iβ‰ j)
avoid-insert {n = n} ρ fzero a fzero iβ‰ j = absurd (iβ‰ j refl)
avoid-insert {n = suc n} ρ fzero a (fsuc j) iβ‰ j = refl
avoid-insert {n = suc n} ρ (fsuc i) a fzero iβ‰ j = refl
avoid-insert {n = suc n} ρ (fsuc i) a (fsuc j) iβ‰ j =
  avoid-insert (ρ ∘ fsuc) i a j (iβ‰ j ∘ ap fsuc)

insert-lookup
  : βˆ€ {n} {β„“} {A : Type β„“}
  β†’ (ρ : Fin n β†’ A)
  β†’ (i : Fin (suc n)) (a : A)
  β†’ (ρ [ i ≔ a ]) i ≑ a
insert-lookup {n = n} ρ fzero a = refl
insert-lookup {n = suc n} ρ (fsuc i) a = insert-lookup (ρ ∘ fsuc) i a

delete-insert
  : βˆ€ {n} {β„“} {A : Type β„“}
  β†’ (ρ : Fin n β†’ A)
  β†’ (i : Fin (suc n)) (a : A)
  β†’ βˆ€ j β†’ delete (ρ [ i ≔ a ]) i j ≑ ρ j
delete-insert ρ fzero a j = refl
delete-insert ρ (fsuc i) a fzero = refl
delete-insert ρ (fsuc i) a (fsuc j) = delete-insert (ρ ∘ fsuc) i a j

insert-delete
  : βˆ€ {n} {β„“} {A : Type β„“}
  β†’ (ρ : Fin (suc n) β†’ A)
  β†’ (i : Fin (suc n)) (a : A)
  β†’ ρ i ≑ a
  β†’ βˆ€ j β†’ ((delete ρ i) [ i ≔ a ]) j ≑ ρ j
insert-delete {n = n} ρ fzero a p fzero = sym p
insert-delete {n = n} ρ fzero a p (fsuc j) = refl
insert-delete {n = suc n} ρ (fsuc i) a p fzero = refl
insert-delete {n = suc n} ρ (fsuc i) a p (fsuc j) = insert-delete (ρ ∘ fsuc) i a p j