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))

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 extension system (for example, the propositional truncation monad), then commutes with finite products:

finite-choice
  : βˆ€ {β„“} n {A : Fin n β†’ Type β„“} {M}
      (let module M = Effect M)
  β†’ ⦃ Idiom M ⦄
  β†’ (βˆ€ x β†’ M.β‚€ (A x)) β†’ M.β‚€ (βˆ€ x β†’ A x)
finite-choice zero _        = pure Ξ» ()
finite-choice (suc n) {A} k = ⦇ elim (k fzero) (finite-choice n (k ∘ fsuc)) ⦈
  where
    elim : A fzero β†’ (βˆ€ x β†’ A (fsuc x)) β†’ βˆ€ x β†’ A x
    elim azero asuc fzero = azero
    elim azero asuc (fsuc x) = asuc x

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 _

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