open import 1Lab.Prelude

open import Data.Fin.Base
open import Data.Dec
open import Data.Sum

open import Meta.Invariant

import Data.Nat.Order as Nat
import Data.Nat.Base as Nat

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


## 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 , f) fzero    β z
.snd .is-iso.inv (z , f) (fsuc x) β f x

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

_ = 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) β₯
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
Fin-exhaustible
: β {n β} {A : Fin n β Type β}
β β¦ β {x} β Dec (A x) β¦ β Dec (β x β A x)
Fin-exhaustible {n} β¦ d β¦ = Fin-Monoidal n Ξ» _ β d

Fin-omniscient
: β {n β} {A : Fin n β Type β}
β β¦ β {x} β Dec (A x) β¦ β Dec (Ξ£ (Fin n) A)
Fin-omniscient {n} β¦ d β¦ = Fin-Alternative n Ξ» _ β d


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