2020-06-30 12:51:09 +03:00
|
|
|
data Vect : Nat -> Type -> Type where
|
|
|
|
Nil : Vect Z a
|
|
|
|
(::) : a -> Vect k a -> Vect (S k) a
|
|
|
|
|
|
|
|
(++) : Vect n a -> Vect m a -> Vect (n + m) a
|
|
|
|
(++) Nil ys = ys
|
|
|
|
(++) (x :: xs) ys = x :: xs ++ ys
|
|
|
|
|
|
|
|
data Fin : Nat -> Type where
|
|
|
|
FZ : Fin (S k)
|
|
|
|
FS : Fin k -> Fin (S k)
|
|
|
|
|
|
|
|
index : Fin n -> Vect n a -> a
|
|
|
|
index FZ (x :: xs) = x
|
|
|
|
index (FS k) (x :: xs) = index k xs
|
|
|
|
|
|
|
|
filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
|
|
|
|
filter p Nil = (_ ** [])
|
|
|
|
filter p (x :: xs)
|
|
|
|
= case filter p xs of
|
|
|
|
(_ ** xs') => if p x then (_ ** x :: xs')
|
|
|
|
else (_ ** xs')
|
|
|
|
|
|
|
|
cons : t -> (x : Nat ** Vect x t) -> (x : Nat ** Vect x t)
|
|
|
|
cons val xs
|
2021-12-16 21:23:18 +03:00
|
|
|
= { fst := S (fst xs),
|
|
|
|
snd := (val :: snd xs) } xs
|
2020-06-30 12:51:09 +03:00
|
|
|
|
|
|
|
cons' : t -> (x : Nat ** Vect x t) -> (x : Nat ** Vect x t)
|
|
|
|
cons' val
|
2021-12-16 21:23:18 +03:00
|
|
|
= { fst $= S,
|
|
|
|
snd $= (val ::) }
|
2020-06-30 12:51:09 +03:00
|
|
|
|
|
|
|
Show a => Show (Vect n a) where
|
|
|
|
show xs = "[" ++ show' xs ++ "]" where
|
|
|
|
show' : forall n . Vect n a -> String
|
|
|
|
show' Nil = ""
|
|
|
|
show' (x :: Nil) = show x
|
|
|
|
show' (x :: xs) = show x ++ ", " ++ show' xs
|