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!
: ∀ {n} (i j : Fin (suc n)) → i ≤ j
skip-comm → ∀ x → skip (weaken i) (skip j x) ≡ skip (fsuc j) (skip i x)
with fin-view i | fin-view j | le | fin-view x
skip-comm i j le x ... | zero | zero | _ | _ = refl
... | zero | suc _ | _ | _ = refl
... | suc i | suc j | le | zero = refl
... | suc i | suc j | s≤s le | (suc x) = ap fsuc (skip-comm i j le x)
: ∀ {n} (i j : Fin n) → i ≤ j
drop-comm → ∀ x → squish j (squish (weaken i) x) ≡ squish i (squish (fsuc j) x)
with fin-view i | fin-view j | le | fin-view x
drop-comm i j le x ... | zero | zero | le | zero = refl
... | zero | zero | le | suc x = refl
... | zero | suc j | le | zero = refl
... | zero | suc j | le | suc x = refl
... | suc i | suc j | le | zero = refl
... | suc i | suc j | s≤s le | suc x = ap fsuc (drop-comm i j le x)
: ∀ {n} (i : Fin (suc n)) (j : Fin n) → i < fsuc j
squish-skip-comm → ∀ x → squish (fsuc j) (skip (weaken i) x) ≡ skip i (squish j x)
with fin-view i | fin-view j | le | fin-view x
squish-skip-comm i j le x ... | zero | zero | s≤s p | zero = refl
... | zero | zero | s≤s p | suc _ = refl
... | zero | suc _ | s≤s p | zero = refl
... | zero | suc _ | s≤s p | suc _ = refl
... | suc i | (suc j) | (Nat.s≤s p) | zero = refl
... | suc i | (suc j) | (Nat.s≤s p) | (suc x) =
(squish-skip-comm i j p x)
ap fsuc
: ∀ {n} (i j : Fin n) → i ≡ j
squish-skip → ∀ x → squish j (skip (weaken j) x) ≡ x
with fin-view i | fin-view j | fin-view x
squish-skip i j p x ... | zero | zero | x = refl
... | zero | (suc j) | x = absurd (fzero≠fsuc p)
... | (suc i) | zero | x = refl
... | (suc i) | (suc j) | zero = refl
... | (suc i) | (suc j) | (suc x) =
(squish-skip i j (fsuc-inj p) x)
ap fsuc
: ∀ {n} (i : Fin (suc n)) (j : Fin n) → i ≡ fsuc j
squish-skip-fsuc → ∀ x → squish j (skip i x) ≡ x
with fin-view i | fin-view j | fin-view x
squish-skip-fsuc i j p x ... | zero | zero | x = refl
... | zero | suc j | x = absurd (fzero≠fsuc p)
... | suc i | suc j | zero = refl
... | suc i | suc j | suc x = ap fsuc (squish-skip-fsuc i j (fsuc-inj p) x)
... | suc i | zero | x with fin-view i | x
... | zero | zero = refl
... | zero | suc x = refl
... | suc i | zero = refl
... | suc i | suc x = absurd (Nat.zero≠suc λ i → Nat.pred (p (~ i) .lower))
: ∀ {n} → Fin (suc n) ≃ Maybe (Fin n)
Fin-suc = Iso→Equiv (to , iso from ir il) where
Fin-suc to : ∀ {n} → Fin (suc n) → Maybe (Fin n)
to i with fin-view i
... | suc i = just i
... | zero = nothing
: ∀ {n} → Maybe (Fin n) → Fin (suc n)
from (just x) = fsuc x
from = fzero
from nothing
: is-right-inverse from to
ir = refl
ir nothing (just x) = refl
ir
: is-left-inverse from to
il with fin-view i
il i ... | suc i = refl
... | zero = refl
: ∀ {l k} → Fin (suc l) ≃ Fin (suc k) → Fin l ≃ Fin k
Fin-peel {l} {k} sl≃sk = Maybe-injective (Equiv.inverse Fin-suc ∙e sl≃sk ∙e Fin-suc)
Fin-peel
: ∀ {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)
Fin-injective
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
{j} {k} {i≠j} {i≠k} p with fin-view i | fin-view j | fin-view k
avoid-injective i ... | zero | zero | _ = absurd (i≠j refl)
... | zero | suc j | zero = absurd (i≠k refl)
... | zero | suc j | suc k = ap fsuc p
... | suc i | zero | zero = refl
{suc n} _ p | suc i | zero | suc k = absurd (fzero≠fsuc p)
avoid-injective {suc n} _ p | suc i | suc j | zero = absurd (fsuc≠fzero p)
avoid-injective {suc n} _ p | suc i | suc j | suc k = ap fsuc (avoid-injective {n} i {j} {k} (fsuc-inj p))
avoid-injective
skip-injective: ∀ {n} (i : Fin (suc n)) (j k : Fin n)
→ skip i j ≡ skip i k → j ≡ k
with fin-view i | fin-view j | fin-view k
skip-injective i j k p ... | zero | j | k = fsuc-inj p
... | suc i | zero | zero = refl
... | suc i | zero | suc k = absurd (fzero≠fsuc p)
... | suc i | suc j | zero = absurd (fsuc≠fzero p)
... | suc i | suc j | suc k = ap fsuc (skip-injective i j k (fsuc-inj p))
skip-skips: ∀ {n} (i : Fin (suc n)) (j : Fin n)
→ skip i j ≠ i
with fin-view i | fin-view j
skip-skips i j p ... | zero | j = fsuc≠fzero p
... | suc i | zero = fzero≠fsuc p
... | suc i | suc j = 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
with fin-view i | fin-view j
avoid-skip i j ... | zero | zero = refl
... | zero | suc j = refl
... | suc i | zero = refl
... | suc i | suc 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
{i≠j} with fin-view i | fin-view j
skip-avoid i j ... | zero | zero = absurd (i≠j refl)
{suc n} _ _ | zero | suc j = refl
skip-avoid {suc n} _ _ | suc i | zero = refl
skip-avoid {suc n} _ _ | suc i | suc j = ap fsuc (skip-avoid i j) skip-avoid
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)))
= Iso→Equiv λ where
Fin-suc-Π .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 (fin zero ⦃ forget auto ⦄)
.snd .is-iso.linv k i (fin (suc n) ⦃ b ⦄) → k (fin (suc n) ⦃ b ⦄)
Fin-suc-Σ: ∀ {ℓ} {n} {A : Fin (suc n) → Type ℓ}
→ Σ (Fin (suc n)) A ≃ (A fzero ⊎ Σ (Fin n) (A ∘ fsuc))
{A = A} = Iso→Equiv (to , iso from ir il) where
Fin-suc-Σ to : ∫ₚ A → A fzero ⊎ ∫ₚ (A ∘ fsuc)
to (i , a) with fin-view i
... | zero = inl a
... | suc x = inr (x , a)
: A fzero ⊎ ∫ₚ (A ∘ fsuc) → ∫ₚ A
from (inl x) = fzero , x
from (inr (x , a)) = fsuc x , a
from
: is-right-inverse from to
ir (inl x) = refl
ir (inr x) = refl
ir
: is-left-inverse from to
il (i , a) with fin-view i
il ... | zero = refl
... | suc _ = 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)
_ = invmap (λ _ ()) _ munit
Fin-Monoidal zero (suc n) k =
Fin-Monoidal (k 0 <,> Fin-Monoidal n (k ∘ fsuc)) Fin-suc-Π e⁻¹ <≃>
_ = Idiom
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) ∥
= Fin-Monoidal n finite-choice 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-choice _ finite-surjection-split f
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)
_ = invmap (λ ()) (λ ()) empty
Fin-Alternative zero (suc n) k =
Fin-Alternative (k 0 <+> Fin-Alternative n (k ∘ fsuc)) Fin-suc-Σ e⁻¹ <≃>
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)
{n} ⦃ d ⦄ = Fin-Monoidal n (λ _ → d)
Dec-Fin-∀
Dec-Fin-Σ: ∀ {n ℓ} {A : Fin n → Type ℓ}
→ ⦃ ∀ {x} → Dec (A x) ⦄ → Dec (Σ (Fin n) A)
{n} ⦃ d ⦄ = Fin-Alternative n λ _ → d Dec-Fin-Σ
Fin-omniscience: ∀ {n ℓ} (P : Fin n → Type ℓ) ⦃ _ : ∀ {x} → Dec (P x) ⦄
→ (Σ[ j ∈ Fin n ] P j × ∀ k → P k → j ≤ k) ⊎ (∀ x → ¬ P x)
{zero} P = inr λ ()
Fin-omniscience {suc n} P with holds? (P 0)
Fin-omniscience ... | 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)
Fin-omniscience-neg: ∀ {n ℓ} (P : Fin n → Type ℓ) ⦃ _ : ∀ {x} → Dec (P x) ⦄
→ (∀ x → P x) ⊎ (Σ[ j ∈ Fin n ] ¬ P j × ∀ k → ¬ P k → j ≤ k)
with Fin-omniscience (¬_ ∘ P)
Fin-omniscience-neg P ... | inr p = inl λ i → dec→dne (p i)
... | inl (j , ¬pj , least) = inr (j , ¬pj , least)
Fin-find: ∀ {n ℓ} {P : Fin n → Type ℓ} ⦃ _ : ∀ {x} → Dec (P x) ⦄
→ ¬ (∀ x → P x)
→ Σ[ x ∈ Fin n ] ¬ P x × ∀ y → ¬ P y → x ≤ y
{P = P} ¬p with Fin-omniscience-neg P
Fin-find ... | inl p = absurd (¬p p)
... | inr p = p
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
{zero} f inj .is-eqv ()
Fin-injection→equiv {suc n} f inj .is-eqv i with f 0 ≡? i
Fin-injection→equiv ... | yes p = contr (0 , p) λ (j , p') → Σ-prop-path! (inj (p ∙ sym p'))
... | no ¬p = contr fib cen where
= Fin-injection→equiv {n}
rec (λ x → avoid (f 0) (f (fsuc x)) (Nat.zero≠suc ∘ ap lower ∘ inj))
(λ p → fsuc-inj (inj (avoid-injective (f 0) p)))
.is-eqv (avoid (f 0) i ¬p)
: fibre f i
fib = fsuc (rec .centre .fst) , avoid-injective (f 0) (rec .centre .snd)
fib
: ∀ x → fib ≡ x
cen (i , p) with fin-view i
cen ... | zero = absurd (¬p p)
... | suc j = Σ-prop-path! (ap (fsuc ∘ fst)
(rec .paths (j , ap₂ (avoid (f 0)) p prop!)))
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
= case finite-surjection-split f surj of λ split →
Fin-surjection→equiv f surj (snd ∘ split)
left-inverse→equiv (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)
with fin-view i | fin-view j
avoid-insert ρ i a j i≠j ... | zero | zero = absurd (i≠j refl)
... | zero | suc j = refl
{suc n} ρ _ a _ _ | suc i | zero = refl
avoid-insert {suc n} ρ _ a _ i≠j | suc i | suc j =
avoid-insert (ρ ∘ fsuc) i a j (i≠j ∘ ap fsuc)
avoid-insert
insert-lookup: ∀ {n} {ℓ} {A : Type ℓ}
→ (ρ : Fin n → A)
→ (i : Fin (suc n)) (a : A)
→ (ρ [ i ≔ a ]) i ≡ a
{n = n} ρ i a with fin-view i
insert-lookup ... | zero = refl
{n = suc n} ρ _ a | suc i = insert-lookup (ρ ∘ fsuc) i a
insert-lookup
delete-insert: ∀ {n} {ℓ} {A : Type ℓ}
→ (ρ : Fin n → A)
→ (i : Fin (suc n)) (a : A)
→ ∀ j → delete (ρ [ i ≔ a ]) i j ≡ ρ j
with fin-view i | fin-view j
delete-insert ρ i a j ... | zero | j = refl
... | suc i | zero = refl
... | suc i | (suc 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
with fin-view i | fin-view j
insert-delete ρ i a p j ... | zero | zero = sym p
... | zero | suc j = refl
{suc n} ρ _ a p _ | suc i | zero = refl
insert-delete {suc n} ρ _ a p _ | suc i | suc j = insert-delete (ρ ∘ fsuc) i a p j
insert-delete
: Nat → Type
ℕ< = Σ[ k ∈ Nat ] k Nat.< n
ℕ< n
: ∀ {n} → ℕ< n → Fin n
from-ℕ< (i , p) = fin i ⦃ forget p ⦄
from-ℕ<
: ∀ {n} → Fin n → ℕ< n
to-ℕ< (fin i ⦃ forget p ⦄) = i , recover p to-ℕ<