module Functions.Dependent where

open import Data.Nat using (; zero; suc; _+_; z≤n; s≤s; _<_)
open import Data.Fin using (Fin; zero; suc)
open import Data.Vec using (Vec; []; _∷_)
open import Data.Product using (_×_; _,_)

Dependently typed functions

Dependently typed function:

f : (x : A) → B, where B may depend on x

Example


fromℕ : (n : )  Fin (suc n)
fromℕ zero = zero  -- Note: different zeros
fromℕ (suc n) = suc (fromℕ n)

Notes

Exercises

Define a substraction with restricted domain:


_-_ : (n : )  Fin (suc n)  

Define toℕ:


toℕ :  {n}  Fin n  

Define fromℕ≤ such that toℕ (fromℕ≤ e) is m if e : m < n:


fromℕ≤ :  {m n}  m < n  Fin n

Define inject+ such that toℕ (inject+ n i) is the same as toℕ i:


inject+ :  {m} n  Fin m  Fin (m + n)

Define four such that toℕ four is 4:


four : Fin 66

Define raise such that toℕ (raise n i) is the same as n + toℕ i:


raise :  {m} n  Fin m  Fin (n + m)

Exercises

Define head and tail.


head :  {n}{A : Set}  Vec A (suc n)  A


tail :  {n}{A : Set}  Vec A (suc n)  Vec A n

Define concatenation for vectors.


_++_ :  {n m}{A : Set}  Vec A n  Vec A m  Vec A (n + m)

Define maps. (Note that two cases will be enough!)


maps :  {n}{A B : Set}  Vec (A  B) n  Vec A n  Vec B n

Define replicate.


replicate :  {n}{A : Set}  A  Vec A n

Define map with the help of maps and replicate.


map :  {n}{A B : Set}  (A  B)  Vec A n  Vec B n

Define zip with the help of map and maps.


zip :  {n}{A B : Set}  Vec A n  Vec B n  Vec (A × B) n

Define safe element indexing.


_!_ :  {n}{A : Set}  Vec A n  Fin n  A