Vec.agda 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. data Vec (n : Nat)(A : Set) : Set
  11. Vec' : Nat -> Set -> Set
  12. Vec' zero A = Nil
  13. Vec' (suc n) A = Cons A (Vec n A)
  14. data Vec n A 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)