Vec.agda 946 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. module Vec where
  2. import Nat
  3. import Fin
  4. open Nat hiding (_==_; _<_)
  5. open Fin
  6. data Nil : Set where
  7. vnil : Nil
  8. data Cons (A As : Set) : Set where
  9. vcons : A -> As -> Cons A As
  10. mutual
  11. Vec' : Nat -> Set -> Set
  12. Vec' zero A = Nil
  13. Vec' (suc n) A = Cons A (Vec n A)
  14. data Vec (n : Nat)(A : Set) : Set where
  15. vec : Vec' n A -> Vec n A
  16. ε : {A : Set} -> Vec zero A
  17. ε = vec vnil
  18. _∷_ : {A : Set}{n : Nat} -> A -> Vec n A -> Vec (suc n) A
  19. x ∷ xs = vec (vcons x xs)
  20. _!_ : {n : Nat}{A : Set} -> Vec n A -> Fin n -> A
  21. _!_ {zero} _ (fin ())
  22. _!_ {suc n} (vec (vcons x xs)) (fin fz) = x
  23. _!_ {suc n} (vec (vcons x xs)) (fin (fs i)) = xs ! i
  24. map : {n : Nat}{A B : Set} -> (A -> B) -> Vec n A -> Vec n B
  25. map {zero} f (vec vnil) = ε
  26. map {suc n} f (vec (vcons x xs)) = f x ∷ map f xs
  27. fzeroToN-1 : (n : Nat) -> Vec n (Fin n)
  28. fzeroToN-1 zero = ε
  29. fzeroToN-1 (suc n) = fzero ∷ map fsuc (fzeroToN-1 n)