@@ -1,11 +1,599 @@
1module Data.Vect
1module Data.Vect
3import Language.Reflection
4import Language.Reflection
4−import public Data.VectType
6%access public
6%access public
7%default total
7%default total
11+||| Vectors: Generic lists with explicit length in the type
13+data Vect : Nat -> Type -> Type where
16+ ||| A non-empty vector of length `S k`, consisting of a head element and
17+ ||| the rest of the list, of length `k`.
18+ (::) : (x : a) -> (xs : Vect k a) -> Vect (S k) a
20+-- Hints for interactive editing
21+%name Vect xs,ys,zs,ws
23+--------------------------------------------------------------------------------
25+--------------------------------------------------------------------------------
27+||| Calculate the length of a `Vect`.
29+||| **Note**: this is only useful if you don't already statically know the length
30+||| and you want to avoid matching the implicit argument for erasure reasons.
31+||| @ n the length (provably equal to the return value)
33+length : (xs : Vect n a) -> Nat
35+length (x::xs) = 1 + length xs
37+||| Show that the length function on vectors in fact calculates the length
38+private lengthCorrect : (n : Nat) -> (xs : Vect n a) -> length xs = n
39+lengthCorrect Z [] = Refl
40+lengthCorrect (S n) (x :: xs) = rewrite lengthCorrect n xs in Refl
42+--------------------------------------------------------------------------------
43+-- Indexing into vectors
44+--------------------------------------------------------------------------------
46+||| All but the first element of the vector
47+tail : Vect (S n) a -> Vect n a
50+||| Only the first element of the vector
51+head : Vect (S n) a -> a
54+||| The last element of the vector
55+last : Vect (S n) a -> a
57+last (x::y::ys) = last $ y::ys
59+||| All but the last element of the vector
60+init : Vect (S n) a -> Vect n a
62+init (x::y::ys) = x :: init (y::ys)
64+||| Extract a particular element from a vector
65+index : Fin n -> Vect n a -> a
67+index (FS k) (x::xs) = index k xs
70+||| Insert an element at a particular index
71+insertAt : Fin (S n) -> a -> Vect n a -> Vect (S n) a
72+insertAt FZ y xs = y :: xs
73+insertAt (FS k) y (x::xs) = x :: insertAt k y xs
74+insertAt (FS k) y [] = absurd k
76+||| Construct a new vector consisting of all but the indicated element
77+deleteAt : Fin (S n) -> Vect (S n) a -> Vect n a
78+deleteAt FZ (x::xs) = xs
79+deleteAt {n = S m} (FS k) (x::xs) = x :: deleteAt k xs
80+deleteAt {n = Z} (FS k) (x::xs) = absurd k
81+deleteAt _ [] impossible
83+||| Replace an element at a particlar index with another
84+replaceAt : Fin n -> t -> Vect n t -> Vect n t
85+replaceAt FZ y (x::xs) = y :: xs
86+replaceAt (FS k) y (x::xs) = x :: replaceAt k y xs
88+||| Replace the element at a particular index with the result of applying a function to it
89+||| @ i the index to replace at
90+||| @ f the update function
91+||| @ xs the vector to replace in
92+updateAt : (i : Fin n) -> (f : t -> t) -> (xs : Vect n t) -> Vect n t
93+updateAt FZ f (x::xs) = f x :: xs
94+updateAt (FS k) f (x::xs) = x :: updateAt k f xs
96+--------------------------------------------------------------------------------
98+--------------------------------------------------------------------------------
100+||| Get the first n elements of a Vect
101+||| @ n the number of elements to take
102+take : (n : Nat) -> Vect (n + m) a -> Vect n a
104+take (S k) (x :: xs) = x :: take k xs
106+||| Remove the first n elements of a Vect
107+||| @ n the number of elements to remove
108+drop : (n : Nat) -> Vect (n + m) a -> Vect m a
110+drop (S k) (x :: xs) = drop k xs
112+||| Take the longest prefix of a Vect such that all elements satisfy some
113+||| Boolean predicate.
115+||| @ p the predicate
116+takeWhile : (p : a -> Bool) -> Vect n a -> (q ** Vect q a)
117+takeWhile p [] = (_ ** [])
118+takeWhile p (x::xs) =
119+ let (len ** ys) = takeWhile p xs
125+||| Remove the longest prefix of a Vect such that all removed elements satisfy some
126+||| Boolean predicate.
128+||| @ p the predicate
129+dropWhile : (p : a -> Bool) -> Vect n a -> (q ** Vect q a)
130+dropWhile p [] = (_ ** [])
131+dropWhile p (x::xs) =
137+--------------------------------------------------------------------------------
139+--------------------------------------------------------------------------------
141+||| Reverse the order of the elements of a vector
142+reverse : Vect n a -> Vect n a
143+reverse xs = go [] xs
144+ where go : Vect n a -> Vect m a -> Vect (n+m) a
145+ go {n} acc [] = rewrite plusZeroRightNeutral n in acc
146+ go {n} {m=S m} acc (x :: xs) = rewrite sym $ plusSuccRightSucc n m
149+||| Alternate an element between the other elements of a vector
150+||| @ sep the element to intersperse
151+||| @ xs the vector to separate with `sep`
152+intersperse : (sep : a) -> (xs : Vect n a) -> Vect (n + pred n) a
153+intersperse sep [] = []
154+intersperse sep (x::xs) = x :: intersperse' sep xs
156+ intersperse' : a -> Vect n a -> Vect (n + n) a
157+ intersperse' sep [] = []
158+ intersperse' {n=S n} sep (x::xs) = rewrite sym $ plusSuccRightSucc n n
159+ in sep :: x :: intersperse' sep xs
161+--------------------------------------------------------------------------------
162+-- Conversion from list (toList is provided by Foldable)
163+--------------------------------------------------------------------------------
166+fromList' : Vect n a -> (l : List a) -> Vect (length l + n) a
168+fromList' {n} ys (x::xs) =
169+ rewrite (plusSuccRightSucc (length xs) n) ==>
170+ Vect (plus (length xs) (S n)) a in
171+ fromList' (x::ys) xs
173+||| Convert a list to a vector.
175+||| The length of the list should be statically known.
176+fromList : (l : List a) -> Vect (length l) a
178+ rewrite (sym $ plusZeroRightNeutral (length l)) in
179+ reverse $ fromList' [] l
181+--------------------------------------------------------------------------------
182+-- Building (bigger) vectors
183+--------------------------------------------------------------------------------
185+||| Append two vectors
186+(++) : Vect m a -> Vect n a -> Vect (m + n) a
188+(++) (x::xs) ys = x :: xs ++ ys
190+||| Repeate some value n times
191+||| @ n the number of times to repeat it
192+||| @ x the value to repeat
193+replicate : (n : Nat) -> (x : a) -> Vect n a
195+replicate (S k) x = x :: replicate k x
197+--------------------------------------------------------------------------------
199+--------------------------------------------------------------------------------
201+||| Combine two equal-length vectors pairwise with some function
202+zipWith : (a -> b -> c) -> Vect n a -> Vect n b -> Vect n c
204+zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
206+||| Combine three equal-length vectors into a vector with some function
207+zipWith3 : (a -> b -> c -> d) -> Vect n a -> Vect n b -> Vect n c -> Vect n d
208+zipWith3 f [] [] [] = []
209+zipWith3 f (x::xs) (y::ys) (z::zs) = f x y z :: zipWith3 f xs ys zs
211+||| Combine two equal-length vectors pairwise
212+zip : Vect n a -> Vect n b -> Vect n (a, b)
213+zip = zipWith (\x,y => (x,y))
215+||| Combine three equal-length vectors elementwise into a vector of tuples
216+zip3 : Vect n a -> Vect n b -> Vect n c -> Vect n (a, b, c)
217+zip3 = zipWith3 (\x,y,z => (x,y,z))
219+||| Convert a vector of pairs to a pair of vectors
220+unzip : Vect n (a, b) -> (Vect n a, Vect n b)
222+unzip ((l, r)::xs) with (unzip xs)
223+ | (lefts, rights) = (l::lefts, r::rights)
225+||| Convert a vector of three-tuples to a triplet of vectors
226+unzip3 : Vect n (a, b, c) -> (Vect n a, Vect n b, Vect n c)
227+unzip3 [] = ([], [], [])
228+unzip3 ((l,c,r)::xs) with (unzip3 xs)
229+ | (lefts, centers, rights) = (l::lefts, c::centers, r::rights)
231+--------------------------------------------------------------------------------
233+--------------------------------------------------------------------------------
235+instance (Eq a) => Eq (Vect n a) where
237+ (==) (x::xs) (y::ys) =
244+--------------------------------------------------------------------------------
246+--------------------------------------------------------------------------------
248+instance Ord a => Ord (Vect n a) where
250+ compare (x::xs) (y::ys) =
257+--------------------------------------------------------------------------------
259+--------------------------------------------------------------------------------
261+instance Functor (Vect n) where
263+ map f (x::xs) = f x :: map f xs
266+||| Map a partial function across a vector, returning those elements for which
267+||| the function had a value.
269+||| The first projection of the resulting pair (ie the length) will always be
270+||| at most the length of the input vector. This is not, however, guaranteed
273+||| @ f the partial function (expressed by returning `Maybe`)
274+||| @ xs the vector to check for results
275+mapMaybe : (f : a -> Maybe b) -> (xs : Vect n a) -> (m : Nat ** Vect m b)
276+mapMaybe f [] = (_ ** [])
278+ let (len ** ys) = mapMaybe f xs
280+ Just y => (S len ** y :: ys)
281+ Nothing => ( len ** ys)
284+--------------------------------------------------------------------------------
286+--------------------------------------------------------------------------------
288+total foldrImpl : (t -> acc -> acc) -> acc -> (acc -> acc) -> Vect n t -> acc
289+foldrImpl f e go [] = go e
290+foldrImpl f e go (x::xs) = foldrImpl f e (go . (f x)) xs
292+instance Foldable (Vect n) where
293+ foldr f e xs = foldrImpl f e id xs
295+--------------------------------------------------------------------------------
297+--------------------------------------------------------------------------------
299+||| Flatten a vector of equal-length vectors
300+concat : Vect m (Vect n a) -> Vect (m * n) a
302+concat (v::vs) = v ++ concat vs
304+||| Foldr without seeding the accumulator
305+foldr1 : (t -> t -> t) -> Vect (S n) t -> t
306+foldr1 f (x::xs) = foldr f x xs
308+||| Foldl without seeding the accumulator
309+foldl1 : (t -> t -> t) -> Vect (S n) t -> t
310+foldl1 f (x::xs) = foldl f x xs
311+--------------------------------------------------------------------------------
313+--------------------------------------------------------------------------------
315+scanl : (b -> a -> b) -> b -> Vect n a -> Vect (S n) b
317+scanl f q (x::xs) = q :: scanl f (f q x) xs
319+--------------------------------------------------------------------------------
321+--------------------------------------------------------------------------------
323+||| Search for an item using a user-provided test
324+||| @ p the equality test
325+||| @ e the item to search for
326+||| @ xs the vector to search in
327+elemBy : (p : a -> a -> Bool) -> (e : a) -> (xs : Vect n a) -> Bool
328+elemBy p e [] = False
329+elemBy p e (x::xs) with (p e x)
331+ | False = elemBy p e xs
333+||| Use the default Boolean equality on elements to search for an item
334+||| @ x what to search for
335+||| @ xs where to search
336+elem : Eq a => (x : a) -> (xs : Vect n a) -> Bool
339+||| Find the association of some key with a user-provided comparison
340+||| @ p the comparison operator for keys (True if they match)
341+||| @ e the key to look for
342+lookupBy : (p : a -> a -> Bool) -> (e : a) -> (xs : Vect n (a, b)) -> Maybe b
343+lookupBy p e [] = Nothing
344+lookupBy p e ((l, r)::xs) with (p e l)
346+ | False = lookupBy p e xs
348+||| Find the assocation of some key using the default Boolean equality test
349+lookup : Eq a => a -> Vect n (a, b) -> Maybe b
350+lookup = lookupBy (==)
352+||| Check if any element of xs is found in elems by a user-provided comparison
353+||| @ p the comparison operator
354+||| @ elems the vector to search
355+||| @ xs what to search for
356+hasAnyBy : (p : a -> a -> Bool) -> (elems : Vect m a) -> (xs : Vect n a) -> Bool
357+hasAnyBy p elems [] = False
358+hasAnyBy p elems (x::xs) with (elemBy p x elems)
360+ | False = hasAnyBy p elems xs
362+||| Check if any element of xs is found in elems using the default Boolean equality test
363+hasAny : Eq a => Vect m a -> Vect n a -> Bool
364+hasAny = hasAnyBy (==)
366+--------------------------------------------------------------------------------
367+-- Searching with a predicate
368+--------------------------------------------------------------------------------
370+||| Find the first element of the vector that satisfies some test
371+||| @ p the test to satisfy
372+find : (p : a -> Bool) -> (xs : Vect n a) -> Maybe a
374+find p (x::xs) with (p x)
378+||| Find the index of the first element of the vector that satisfies some test
379+findIndex : (a -> Bool) -> Vect n a -> Maybe (Fin n)
380+findIndex p [] = Nothing
381+findIndex p (x :: xs) with (p x)
383+ | False = map FS (findIndex p xs)
385+||| Find the indices of all elements that satisfy some test
386+total findIndices : (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
387+findIndices = findIndices' 0
389+ total findIndices' : Nat -> (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
390+ findIndices' cnt p [] = (_ ** [])
391+ findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
398+elemIndexBy : (a -> a -> Bool) -> a -> Vect m a -> Maybe (Fin m)
399+elemIndexBy p e = findIndex $ p e
401+elemIndex : Eq a => a -> Vect m a -> Maybe (Fin m)
402+elemIndex = elemIndexBy (==)
404+total elemIndicesBy : (a -> a -> Bool) -> a -> Vect m a -> (p ** Vect p Nat)
405+elemIndicesBy p e = findIndices $ p e
407+total elemIndices : Eq a => a -> Vect m a -> (p ** Vect p Nat)
408+elemIndices = elemIndicesBy (==)
410+--------------------------------------------------------------------------------
412+--------------------------------------------------------------------------------
414+||| Find all elements of a vector that satisfy some test
415+total filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
416+filter p [] = ( _ ** [] )
417+filter p (x::xs) with (filter p xs)
424+||| Make the elements of some vector unique by some test
425+nubBy : (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
428+ nubBy' : Vect m a -> (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
429+ nubBy' acc p [] = (_ ** [])
430+ nubBy' acc p (x::xs) with (elemBy p x acc)
431+ | True = nubBy' acc p xs
432+ | False with (nubBy' (x::acc) p xs)
433+ | (_ ** tail) = (_ ** x::tail)
435+||| Make the elements of some vector unique by the default Boolean equality
436+nub : Eq a => Vect n a -> (p ** Vect p a)
439+deleteBy : (a -> a -> Bool) -> a -> Vect n a -> (p ** Vect p a)
440+deleteBy _ _ [] = (_ ** [])
441+deleteBy eq x (y::ys) =
442+ let (len ** zs) = deleteBy eq x ys
443+ in if x `eq` y then (_ ** ys) else (S len ** y ::zs)
445+delete : (Eq a) => a -> Vect n a -> (p ** Vect p a)
446+delete = deleteBy (==)
448+--------------------------------------------------------------------------------
449+-- Splitting and breaking lists
450+--------------------------------------------------------------------------------
452+||| A tuple where the first element is a Vect of the n first elements and
453+||| the second element is a Vect of the remaining elements of the original Vect
454+||| It is equivalent to (take n xs, drop n xs)
455+||| @ n the index to split at
456+||| @ xs the Vect to split in two
457+splitAt : (n : Nat) -> (xs : Vect (n + m) a) -> (Vect n a, Vect m a)
458+splitAt n xs = (take n xs, drop n xs)
460+partition : (a -> Bool) -> Vect n a -> ((p ** Vect p a), (q ** Vect q a))
461+partition p [] = ((_ ** []), (_ ** []))
462+partition p (x::xs) =
463+ let ((leftLen ** lefts), (rightLen ** rights)) = partition p xs in
465+ ((S leftLen ** x::lefts), (rightLen ** rights))
467+ ((leftLen ** lefts), (S rightLen ** x::rights))
469+--------------------------------------------------------------------------------
471+--------------------------------------------------------------------------------
473+isPrefixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
474+isPrefixOfBy p [] right = True
475+isPrefixOfBy p left [] = False
476+isPrefixOfBy p (x::xs) (y::ys) with (p x y)
477+ | True = isPrefixOfBy p xs ys
480+isPrefixOf : Eq a => Vect m a -> Vect n a -> Bool
481+isPrefixOf = isPrefixOfBy (==)
483+isSuffixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
484+isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
486+isSuffixOf : Eq a => Vect m a -> Vect n a -> Bool
487+isSuffixOf = isSuffixOfBy (==)
489+--------------------------------------------------------------------------------
491+--------------------------------------------------------------------------------
493+total maybeToVect : Maybe a -> (p ** Vect p a)
494+maybeToVect Nothing = (_ ** [])
495+maybeToVect (Just j) = (_ ** [j])
497+total vectToMaybe : Vect n a -> Maybe a
498+vectToMaybe [] = Nothing
499+vectToMaybe (x::xs) = Just x
501+--------------------------------------------------------------------------------
503+--------------------------------------------------------------------------------
505+catMaybes : Vect n (Maybe a) -> (p ** Vect p a)
506+catMaybes [] = (_ ** [])
507+catMaybes (Nothing::xs) = catMaybes xs
508+catMaybes ((Just j)::xs) with (catMaybes xs)
509+ | (_ ** tail) = (_ ** j::tail)
511+diag : Vect n (Vect n a) -> Vect n a
513+diag ((x::xs)::xss) = x :: diag (map tail xss)
515+range : Vect n (Fin n)
517+range {n=S _} = FZ :: map FS range
519+||| Transpose a Vect of Vects, turning rows into columns and vice versa.
521+||| As the types ensure rectangularity, this is an involution, unlike `Prelude.List.transpose`.
522+transpose : Vect m (Vect n a) -> Vect n (Vect m a)
523+transpose [] = replicate _ []
524+transpose (x :: xs) = zipWith (::) x (transpose xs)
526+--------------------------------------------------------------------------------
527+-- Applicative/Monad/Traversable
528+--------------------------------------------------------------------------------
530+instance Applicative (Vect k) where
533+ fs <*> vs = zipWith apply fs vs
535+||| This monad is different from the List monad, (>>=)
536+||| uses the diagonal.
537+instance Monad (Vect n) where
538+ m >>= f = diag (map f m)
540+instance Traversable (Vect n) where
541+ traverse f [] = pure Vect.Nil
542+ traverse f (x::xs) = [| Vect.(::) (f x) (traverse f xs) |]
544+--------------------------------------------------------------------------------
546+--------------------------------------------------------------------------------
548+instance Show a => Show (Vect n a) where
549+ show = show . toList
551+--------------------------------------------------------------------------------
553+--------------------------------------------------------------------------------
555+vectConsCong : (x : a) -> (xs : Vect n a) -> (ys : Vect m a) -> (xs = ys) -> (x :: xs = x :: ys)
556+vectConsCong x xs xs Refl = Refl
558+vectNilRightNeutral : (xs : Vect n a) -> xs ++ [] = xs
559+vectNilRightNeutral [] = Refl
560+vectNilRightNeutral (x :: xs) =
561+ vectConsCong _ _ _ (vectNilRightNeutral xs)
563+vectAppendAssociative : (x : Vect xLen a) -> (y : Vect yLen a) -> (z : Vect zLen a) -> x ++ (y ++ z) = (x ++ y) ++ z
564+vectAppendAssociative [] y z = Refl
565+vectAppendAssociative (x :: xs) ys zs =
566+ vectConsCong _ _ _ (vectAppendAssociative xs ys zs)
568+--------------------------------------------------------------------------------
570+--------------------------------------------------------------------------------
573+vectInjective1 : {xs, ys : Vect n a} -> {x, y : a} -> x :: xs = y :: ys -> x = y
574+vectInjective1 {x=x} {y=x} {xs=xs} {ys=xs} Refl = Refl
577+vectInjective2 : {xs, ys : Vect n a} -> {x, y : a} -> x :: xs = y :: ys -> xs = ys
578+vectInjective2 {x=x} {y=x} {xs=xs} {ys=xs} Refl = Refl
580+instance DecEq a => DecEq (Vect n a) where
581+ decEq [] [] = Yes Refl
582+ decEq (x :: xs) (y :: ys) with (decEq x y)
583+ decEq (x :: xs) (x :: ys) | Yes Refl with (decEq xs ys)
584+ decEq (x :: xs) (x :: xs) | Yes Refl | Yes Refl = Yes Refl
585+ decEq (x :: xs) (x :: ys) | Yes Refl | No neq = No (neq . vectInjective2)
586+ decEq (x :: xs) (y :: ys) | No neq = No (neq . vectInjective1)
588+{- The following definition is elaborated in a wrong case-tree. Examination pending.
589+instance DecEq a => DecEq (Vect n a) where
590+ decEq [] [] = Yes Refl
591+ decEq (x :: xs) (y :: ys) with (decEq x y, decEq xs ys)
592+ decEq (x :: xs) (x :: xs) | (Yes Refl, Yes Refl) = Yes Refl
593+ decEq (x :: xs) (y :: ys) | (_, No nEqTl) = No (\p => nEqTl (vectInjective2 p))
594+ decEq (x :: xs) (y :: ys) | (No nEqHd, _) = No (\p => nEqHd (vectInjective1 p))
9--------------------------------------------------------------------------------
597--------------------------------------------------------------------------------
11--------------------------------------------------------------------------------
599--------------------------------------------------------------------------------