# 2020-01-22 `FinVec2`: Modified the types of some functions ``` module FinVec2 where open import Level using (Level) renaming ( _⊔_ to _⊍_ ) open import Data.Product as Product using ( _×_ ; _,_ ; Σ ; proj₁ ; proj₂ ) open import Data.Nat using ( ℕ ; zero ; suc ; _<_ ; z≤n ; s≤s ) open import Function using ( _∘′_ ; id ) open import Relation.Binary.PropositionalEquality using ( _≡_ ; _≗_ ; refl ; cong ; module ≡-Reasoning ) ``` Note that we import `_∘′_` instead of `_∘_`, since we do not need to compose functions of dependent function types in this module, and restricting composition to functions of non-dependent types makes type inference easier and frequently more helpful. A pretty syntax for dependent sum types, that is, types of dependently-typed pairs: ``` Σ-syntax : {ℓa ℓb : Level} (A : Set ℓa) (B : A → Set ℓb) → Set (ℓa ⊍ ℓb) Σ-syntax = Σ infix 2 Σ-syntax syntax Σ-syntax A (λ a → B) = Σ a ∶ A • B ``` In a `syntax` declaration, the right-hand side is the new syntax that is introduced, and the left-hand side is what it is expanded to. ## Prototypical Finite Types: `Fin` The type `Fin n` can be seen as an inductive implementation of the set of all natural numbers less than `n`. Informally: Fin 0 = {} Fin 1 = { 0 } Fin 2 = { 0 , 1 } ``` data Fin : ℕ → Set where zero : {n : ℕ} → Fin (suc n) suc : {n : ℕ} → Fin n → Fin (suc n) ``` suc (zero : Fin 1) : Fin 2 ``` toℕ : {n : ℕ} → Fin n → ℕ toℕ zero = 0 toℕ (suc i) = suc (toℕ i) ``` For each `n`, the function `toℕ {n}` is injective: ``` suc-injective : {m n : ℕ} → ℕ.suc m ≡ suc n → m ≡ n suc-injective = ? toℕ-injective : {n : ℕ} {i j : Fin n} → toℕ i ≡ toℕ j → i ≡ j toℕ-injective = ? ``` But since `toℕ {n}` is not surjective, we cannot define a full inverse: fromℕ : {n : ℕ} → (m : ℕ) → Fin n -- cannot be injective because of pigeon-hole toℕ-fromℕ : {n : ℕ} (m : ℕ) → toℕ (fromℕ m) ≡ m -- cannot be made to hold Since the range of the function `toℕ {n}` contains exactly the natural numbers that are smaller than `n`, the restriction to these makes it possible to define an inverse function for `toℕ {n}`: ``` fromℕ′ : {n : ℕ} → (Σ m ∶ ℕ • m < n) → Fin n fromℕ′ (zero , s≤s m