Vec.agda 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. module Data.Vec where
  2. open import Prelude
  3. open import Data.Nat
  4. open import Data.Fin hiding (_==_; _<_)
  5. open import Logic.Structure.Applicative
  6. open import Logic.Identity
  7. open import Logic.Base
  8. infixl 90 _#_
  9. infixr 50 _::_
  10. infixl 45 _!_ _[!]_
  11. data Vec (A : Set) : Nat -> Set where
  12. [] : Vec A zero
  13. _::_ : {n : Nat} -> A -> Vec A n -> Vec A (suc n)
  14. -- Indexing
  15. _!_ : {n : Nat}{A : Set} -> Vec A n -> Fin n -> A
  16. [] ! ()
  17. x :: xs ! fzero = x
  18. x :: xs ! fsuc i = xs ! i
  19. -- Insertion
  20. insert : {n : Nat}{A : Set} -> Fin (suc n) -> A -> Vec A n -> Vec A (suc n)
  21. insert fzero y xs = y :: xs
  22. insert (fsuc i) y (x :: xs) = x :: insert i y xs
  23. insert (fsuc ()) y []
  24. -- Index view
  25. data IndexView {A : Set} : {n : Nat}(i : Fin n) -> Vec A n -> Set where
  26. ixV : {n : Nat}{i : Fin (suc n)}(x : A)(xs : Vec A n) ->
  27. IndexView i (insert i x xs)
  28. _[!]_ : {A : Set}{n : Nat}(xs : Vec A n)(i : Fin n) -> IndexView i xs
  29. [] [!] ()
  30. x :: xs [!] fzero = ixV x xs
  31. x :: xs [!] fsuc i = aux xs i (xs [!] i)
  32. where
  33. aux : {n : Nat}(xs : Vec _ n)(i : Fin n) ->
  34. IndexView i xs -> IndexView (fsuc i) (x :: xs)
  35. aux .(insert i y xs) i (ixV y xs) = ixV y (x :: xs)
  36. -- Build a vector from an indexing function (inverse of _!_)
  37. build : {n : Nat}{A : Set} -> (Fin n -> A) -> Vec A n
  38. build {zero } f = []
  39. build {suc _} f = f fzero :: build (f ∘ fsuc)
  40. -- Constant vectors
  41. vec : {n : Nat}{A : Set} -> A -> Vec A n
  42. vec {zero } _ = []
  43. vec {suc m} x = x :: vec x
  44. -- Vector application
  45. _#_ : {n : Nat}{A B : Set} -> Vec (A -> B) n -> Vec A n -> Vec B n
  46. [] # [] = []
  47. (f :: fs) # (x :: xs) = f x :: fs # xs
  48. -- Vectors of length n form an applicative structure
  49. ApplicativeVec : {n : Nat} -> Applicative (\A -> Vec A n)
  50. ApplicativeVec {n} = applicative (vec {n}) (_#_ {n})
  51. -- Map
  52. map : {n : Nat}{A B : Set} -> (A -> B) -> Vec A n -> Vec B n
  53. map f xs = vec f # xs
  54. -- Zip
  55. zip : {n : Nat}{A B C : Set} -> (A -> B -> C) -> Vec A n -> Vec B n -> Vec C n
  56. zip f xs ys = vec f # xs # ys
  57. module Elem where
  58. infix 40 _∈_ _∉_
  59. data _∈_ {A : Set}(x : A) : {n : Nat}(xs : Vec A n) -> Set where
  60. hd : {n : Nat} {xs : Vec A n} -> x ∈ x :: xs
  61. tl : {n : Nat}{y : A}{xs : Vec A n} -> x ∈ xs -> x ∈ y :: xs
  62. data _∉_ {A : Set}(x : A) : {n : Nat}(xs : Vec A n) -> Set where
  63. nl : x ∉ []
  64. cns : {n : Nat}{y : A}{xs : Vec A n} -> x ≢ y -> x ∉ xs -> x ∉ y :: xs
  65. ∉=¬∈ : {A : Set}{x : A}{n : Nat}{xs : Vec A n} -> x ∉ xs -> ¬ (x ∈ xs)
  66. ∉=¬∈ nl ()
  67. ∉=¬∈ {A} (cns x≠x _) hd = elim-False (x≠x refl)
  68. ∉=¬∈ {A} (cns _ ne) (tl e) = ∉=¬∈ ne e
  69. ∈=¬∉ : {A : Set}{x : A}{n : Nat}{xs : Vec A n} -> x ∈ xs -> ¬ (x ∉ xs)
  70. ∈=¬∉ e ne = ∉=¬∈ ne e
  71. find : {A : Set}{n : Nat} -> ((x y : A) -> (x ≡ y) \/ (x ≢ y)) ->
  72. (x : A)(xs : Vec A n) -> x ∈ xs \/ x ∉ xs
  73. find _ _ [] = \/-IR nl
  74. find eq y (x :: xs) = aux x y (eq y x) (find eq y xs) where
  75. aux : forall x y -> (y ≡ x) \/ (y ≢ x) -> y ∈ xs \/ y ∉ xs -> y ∈ x :: xs \/ y ∉ x :: xs
  76. aux x .x (\/-IL refl) _ = \/-IL hd
  77. aux x y (\/-IR y≠x) (\/-IR y∉xs) = \/-IR (cns y≠x y∉xs)
  78. aux x y (\/-IR _) (\/-IL y∈xs) = \/-IL (tl y∈xs)
  79. delete : {A : Set}{n : Nat}(x : A)(xs : Vec A (suc n)) -> x ∈ xs -> Vec A n
  80. delete .x (x :: xs) hd = xs
  81. delete {A}{zero } _ ._ (tl ())
  82. delete {A}{suc _} y (x :: xs) (tl p) = x :: delete y xs p