12%elim data Vect : Nat -> Type -> Type where
14 (::) : (x : a) -> (xs : Vect n a) -> Vect (S n) a
16-- Hints for interactive editing
19--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
23||| Calculate the length of a `Vect`.
25||| **Note**: this is only useful if you don't already statically know the length
26||| and you want to avoid matching the implicit argument for erasure reasons.
27||| @ n the length (provably equal to the return value)
29length : (xs : Vect n a) -> Nat
31length (x::xs) = 1 + length xs
33||| Show that the length function on vectors in fact calculates the length
34private lengthCorrect : (n : Nat) -> (xs : Vect n a) -> length xs = n
35lengthCorrect Z [] = Refl
36lengthCorrect (S n) (x :: xs) = rewrite lengthCorrect n xs in Refl
38--------------------------------------------------------------------------------
39-- Indexing into vectors
40--------------------------------------------------------------------------------
42||| All but the first element of the vector
43tail : Vect (S n) a -> Vect n a
46||| Only the first element of the vector
47head : Vect (S n) a -> a
50||| The last element of the vector
51last : Vect (S n) a -> a
53last (x::y::ys) = last $ y::ys
55||| All but the last element of the vector
56init : Vect (S n) a -> Vect n a
58init (x::y::ys) = x :: init (y::ys)
60||| Extract a particular element from a vector
61index : Fin n -> Vect n a -> a
63index (FS k) (x::xs) = index k xs
66||| Insert an element at a particular index
67insertAt : Fin (S n) -> a -> Vect n a -> Vect (S n) a
68insertAt FZ y xs = y :: xs
69insertAt (FS k) y (x::xs) = x :: insertAt k y xs
70insertAt (FS k) y [] = absurd k
72||| Construct a new vector consisting of all but the indicated element
73deleteAt : Fin (S n) -> Vect (S n) a -> Vect n a
74deleteAt FZ (x::xs) = xs
75deleteAt {n = S m} (FS k) (x::xs) = x :: deleteAt k xs
76deleteAt {n = Z} (FS k) (x::xs) = absurd k
77deleteAt _ [] impossible
79||| Replace an element at a particlar index with another
80replaceAt : Fin n -> t -> Vect n t -> Vect n t
81replaceAt FZ y (x::xs) = y :: xs
82replaceAt (FS k) y (x::xs) = x :: replaceAt k y xs
84||| Replace the element at a particular index with the result of applying a function to it
85||| @ i the index to replace at
86||| @ f the update function
87||| @ xs the vector to replace in
88updateAt : (i : Fin n) -> (f : t -> t) -> (xs : Vect n t) -> Vect n t
89updateAt FZ f (x::xs) = f x :: xs
90updateAt (FS k) f (x::xs) = x :: updateAt k f xs
92--------------------------------------------------------------------------------
94--------------------------------------------------------------------------------
96||| Get the first n elements of a Vect
97||| @ n the number of elements to take
98take : (n : Nat) -> Vect (n + m) a -> Vect n a
100take (S k) (x :: xs) = x :: take k xs
102||| Remove the first n elements of a Vect
103||| @ n the number of elements to remove
104drop : (n : Nat) -> Vect (n + m) a -> Vect m a
106drop (S k) (x :: xs) = drop k xs
108||| Take the longest prefix of a Vect such that all elements satisfy some
109||| Boolean predicate.
112takeWhile : (p : a -> Bool) -> Vect n a -> (q ** Vect q a)
113takeWhile p [] = (_ ** [])
115 let (len ** ys) = takeWhile p xs
121||| Remove the longest prefix of a Vect such that all removed elements satisfy some
122||| Boolean predicate.
125dropWhile : (p : a -> Bool) -> Vect n a -> (q ** Vect q a)
126dropWhile p [] = (_ ** [])
133--------------------------------------------------------------------------------
135--------------------------------------------------------------------------------
137||| Reverse the order of the elements of a vector
138reverse : Vect n a -> Vect n a
140 where go : Vect n a -> Vect m a -> Vect (n+m) a
141 go {n} acc [] = rewrite plusZeroRightNeutral n in acc
142 go {n} {m=S m} acc (x :: xs) = rewrite sym $ plusSuccRightSucc n m
145||| Alternate an element between the other elements of a vector
146||| @ sep the element to intersperse
147||| @ xs the vector to separate with `sep`
148intersperse : (sep : a) -> (xs : Vect n a) -> Vect (n + pred n) a
149intersperse sep [] = []
150intersperse sep (x::xs) = x :: intersperse' sep xs
152 intersperse' : a -> Vect n a -> Vect (n + n) a
153 intersperse' sep [] = []
154 intersperse' {n=S n} sep (x::xs) = rewrite sym $ plusSuccRightSucc n n
155 in sep :: x :: intersperse' sep xs
157--------------------------------------------------------------------------------
158-- Conversion from list (toList is provided by Foldable)
159--------------------------------------------------------------------------------
162fromList' : Vect n a -> (l : List a) -> Vect (length l + n) a
164fromList' {n} ys (x::xs) =
165 rewrite (plusSuccRightSucc (length xs) n) ==>
166 Vect (plus (length xs) (S n)) a in
169||| Convert a list to a vector.
171||| The length of the list should be statically known.
172fromList : (l : List a) -> Vect (length l) a
174 rewrite (sym $ plusZeroRightNeutral (length l)) in
175 reverse $ fromList' [] l
177--------------------------------------------------------------------------------
178-- Building (bigger) vectors
179--------------------------------------------------------------------------------
181||| Append two vectors
182(++) : Vect m a -> Vect n a -> Vect (m + n) a
184(++) (x::xs) ys = x :: xs ++ ys
186||| Repeate some value n times
187||| @ n the number of times to repeat it
188||| @ x the value to repeat
189replicate : (n : Nat) -> (x : a) -> Vect n a
191replicate (S k) x = x :: replicate k x
193--------------------------------------------------------------------------------
195--------------------------------------------------------------------------------
197||| Combine two equal-length vectors pairwise with some function
198zipWith : (a -> b -> c) -> Vect n a -> Vect n b -> Vect n c
200zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
202||| Combine three equal-length vectors into a vector with some function
203zipWith3 : (a -> b -> c -> d) -> Vect n a -> Vect n b -> Vect n c -> Vect n d
204zipWith3 f [] [] [] = []
205zipWith3 f (x::xs) (y::ys) (z::zs) = f x y z :: zipWith3 f xs ys zs
207||| Combine two equal-length vectors pairwise
208zip : Vect n a -> Vect n b -> Vect n (a, b)
209zip = zipWith (\x,y => (x,y))
211||| Combine three equal-length vectors elementwise into a vector of tuples
212zip3 : Vect n a -> Vect n b -> Vect n c -> Vect n (a, b, c)
213zip3 = zipWith3 (\x,y,z => (x,y,z))
215||| Convert a vector of pairs to a pair of vectors
216unzip : Vect n (a, b) -> (Vect n a, Vect n b)
218unzip ((l, r)::xs) with (unzip xs)
219 | (lefts, rights) = (l::lefts, r::rights)
221||| Convert a vector of three-tuples to a triplet of vectors
222unzip3 : Vect n (a, b, c) -> (Vect n a, Vect n b, Vect n c)
223unzip3 [] = ([], [], [])
224unzip3 ((l,c,r)::xs) with (unzip3 xs)
225 | (lefts, centers, rights) = (l::lefts, c::centers, r::rights)
227--------------------------------------------------------------------------------
229--------------------------------------------------------------------------------
231instance (Eq a) => Eq (Vect n a) where
233 (==) (x::xs) (y::ys) =
240--------------------------------------------------------------------------------
242--------------------------------------------------------------------------------
244instance Ord a => Ord (Vect n a) where
246 compare (x::xs) (y::ys) =
253--------------------------------------------------------------------------------
255--------------------------------------------------------------------------------
257instance Functor (Vect n) where
259 map f (x::xs) = f x :: map f xs
262||| Map a partial function across a vector, returning those elements for which
263||| the function had a value.
265||| The first projection of the resulting pair (ie the length) will always be
266||| at most the length of the input vector. This is not, however, guaranteed
269||| @ f the partial function (expressed by returning `Maybe`)
270||| @ xs the vector to check for results
271mapMaybe : (f : a -> Maybe b) -> (xs : Vect n a) -> (m : Nat ** Vect m b)
272mapMaybe f [] = (_ ** [])
274 let (len ** ys) = mapMaybe f xs
276 Just y => (S len ** y :: ys)
277 Nothing => ( len ** ys)
280--------------------------------------------------------------------------------
282--------------------------------------------------------------------------------
284total foldrImpl : (t -> acc -> acc) -> acc -> (acc -> acc) -> Vect n t -> acc
285foldrImpl f e go [] = go e
286foldrImpl f e go (x::xs) = foldrImpl f e (go . (f x)) xs
288instance Foldable (Vect n) where
289 foldr f e xs = foldrImpl f e id xs
291--------------------------------------------------------------------------------
293--------------------------------------------------------------------------------
295||| Flatten a vector of equal-length vectors
296concat : Vect m (Vect n a) -> Vect (m * n) a
298concat (v::vs) = v ++ concat vs
300||| Foldr without seeding the accumulator
301foldr1 : (t -> t -> t) -> Vect (S n) t -> t
302foldr1 f (x::xs) = foldr f x xs
304||| Foldl without seeding the accumulator
305foldl1 : (t -> t -> t) -> Vect (S n) t -> t
306foldl1 f (x::xs) = foldl f x xs
307--------------------------------------------------------------------------------
309--------------------------------------------------------------------------------
311scanl : (b -> a -> b) -> b -> Vect n a -> Vect (S n) b
313scanl f q (x::xs) = q :: scanl f (f q x) xs
315--------------------------------------------------------------------------------
317--------------------------------------------------------------------------------
319||| Search for an item using a user-provided test
320||| @ p the equality test
321||| @ e the item to search for
322||| @ xs the vector to search in
323elemBy : (p : a -> a -> Bool) -> (e : a) -> (xs : Vect n a) -> Bool
325elemBy p e (x::xs) with (p e x)
327 | False = elemBy p e xs
329||| Use the default Boolean equality on elements to search for an item
330||| @ x what to search for
331||| @ xs where to search
332elem : Eq a => (x : a) -> (xs : Vect n a) -> Bool
335||| Find the association of some key with a user-provided comparison
336||| @ p the comparison operator for keys (True if they match)
337||| @ e the key to look for
338lookupBy : (p : a -> a -> Bool) -> (e : a) -> (xs : Vect n (a, b)) -> Maybe b
339lookupBy p e [] = Nothing
340lookupBy p e ((l, r)::xs) with (p e l)
342 | False = lookupBy p e xs
344||| Find the assocation of some key using the default Boolean equality test
345lookup : Eq a => a -> Vect n (a, b) -> Maybe b
346lookup = lookupBy (==)
348||| Check if any element of xs is found in elems by a user-provided comparison
349||| @ p the comparison operator
350||| @ elems the vector to search
351||| @ xs what to search for
352hasAnyBy : (p : a -> a -> Bool) -> (elems : Vect m a) -> (xs : Vect n a) -> Bool
353hasAnyBy p elems [] = False
354hasAnyBy p elems (x::xs) with (elemBy p x elems)
356 | False = hasAnyBy p elems xs
358||| Check if any element of xs is found in elems using the default Boolean equality test
359hasAny : Eq a => Vect m a -> Vect n a -> Bool
360hasAny = hasAnyBy (==)
362--------------------------------------------------------------------------------
363-- Searching with a predicate
364--------------------------------------------------------------------------------
366||| Find the first element of the vector that satisfies some test
367||| @ p the test to satisfy
368find : (p : a -> Bool) -> (xs : Vect n a) -> Maybe a
370find p (x::xs) with (p x)
374||| Find the index of the first element of the vector that satisfies some test
375findIndex : (a -> Bool) -> Vect n a -> Maybe (Fin n)
376findIndex p [] = Nothing
377findIndex p (x :: xs) with (p x)
379 | False = map FS (findIndex p xs)
381||| Find the indices of all elements that satisfy some test
382total findIndices : (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
383findIndices = findIndices' 0
385 total findIndices' : Nat -> (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
386 findIndices' cnt p [] = (_ ** [])
387 findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
394elemIndexBy : (a -> a -> Bool) -> a -> Vect m a -> Maybe (Fin m)
395elemIndexBy p e = findIndex $ p e
397elemIndex : Eq a => a -> Vect m a -> Maybe (Fin m)
398elemIndex = elemIndexBy (==)
400total elemIndicesBy : (a -> a -> Bool) -> a -> Vect m a -> (p ** Vect p Nat)
401elemIndicesBy p e = findIndices $ p e
403total elemIndices : Eq a => a -> Vect m a -> (p ** Vect p Nat)
404elemIndices = elemIndicesBy (==)
406--------------------------------------------------------------------------------
408--------------------------------------------------------------------------------
410||| Find all elements of a vector that satisfy some test
411total filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
412filter p [] = ( _ ** [] )
413filter p (x::xs) with (filter p xs)
420||| Make the elements of some vector unique by some test
421nubBy : (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
424 nubBy' : Vect m a -> (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
425 nubBy' acc p [] = (_ ** [])
426 nubBy' acc p (x::xs) with (elemBy p x acc)
427 | True = nubBy' acc p xs
428 | False with (nubBy' (x::acc) p xs)
429 | (_ ** tail) = (_ ** x::tail)
431||| Make the elements of some vector unique by the default Boolean equality
432nub : Eq a => Vect n a -> (p ** Vect p a)
435deleteBy : (a -> a -> Bool) -> a -> Vect n a -> (p ** Vect p a)
436deleteBy _ _ [] = (_ ** [])
437deleteBy eq x (y::ys) =
438 let (len ** zs) = deleteBy eq x ys
439 in if x `eq` y then (_ ** ys) else (S len ** y ::zs)
441delete : (Eq a) => a -> Vect n a -> (p ** Vect p a)
442delete = deleteBy (==)
444--------------------------------------------------------------------------------
445-- Splitting and breaking lists
446--------------------------------------------------------------------------------
448||| A tuple where the first element is a Vect of the n first elements and
449||| the second element is a Vect of the remaining elements of the original Vect
450||| It is equivalent to (take n xs, drop n xs)
451||| @ n the index to split at
452||| @ xs the Vect to split in two
453splitAt : (n : Nat) -> (xs : Vect (n + m) a) -> (Vect n a, Vect m a)
454splitAt n xs = (take n xs, drop n xs)
456partition : (a -> Bool) -> Vect n a -> ((p ** Vect p a), (q ** Vect q a))
457partition p [] = ((_ ** []), (_ ** []))
459 let ((leftLen ** lefts), (rightLen ** rights)) = partition p xs in
461 ((S leftLen ** x::lefts), (rightLen ** rights))
463 ((leftLen ** lefts), (S rightLen ** x::rights))
465--------------------------------------------------------------------------------
467--------------------------------------------------------------------------------
469isPrefixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
470isPrefixOfBy p [] right = True
471isPrefixOfBy p left [] = False
472isPrefixOfBy p (x::xs) (y::ys) with (p x y)
473 | True = isPrefixOfBy p xs ys
476isPrefixOf : Eq a => Vect m a -> Vect n a -> Bool
477isPrefixOf = isPrefixOfBy (==)
479isSuffixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
480isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
482isSuffixOf : Eq a => Vect m a -> Vect n a -> Bool
483isSuffixOf = isSuffixOfBy (==)
485--------------------------------------------------------------------------------
487--------------------------------------------------------------------------------
489total maybeToVect : Maybe a -> (p ** Vect p a)
490maybeToVect Nothing = (_ ** [])
491maybeToVect (Just j) = (_ ** [j])
493total vectToMaybe : Vect n a -> Maybe a
494vectToMaybe [] = Nothing
495vectToMaybe (x::xs) = Just x
497--------------------------------------------------------------------------------
499--------------------------------------------------------------------------------
501catMaybes : Vect n (Maybe a) -> (p ** Vect p a)
502catMaybes [] = (_ ** [])
503catMaybes (Nothing::xs) = catMaybes xs
504catMaybes ((Just j)::xs) with (catMaybes xs)
505 | (_ ** tail) = (_ ** j::tail)
507diag : Vect n (Vect n a) -> Vect n a
509diag ((x::xs)::xss) = x :: diag (map tail xss)
511range : Vect n (Fin n)
513range {n=S _} = FZ :: map FS range
515||| Transpose a Vect of Vects, turning rows into columns and vice versa.
517||| As the types ensure rectangularity, this is an involution, unlike `Prelude.List.transpose`.
518transpose : Vect m (Vect n a) -> Vect n (Vect m a)
519transpose [] = replicate _ []
520transpose (x :: xs) = zipWith (::) x (transpose xs)
522--------------------------------------------------------------------------------
523-- Applicative/Monad/Traversable
524--------------------------------------------------------------------------------
526instance Applicative (Vect k) where
529 fs <*> vs = zipWith apply fs vs
531||| This monad is different from the List monad, (>>=)
532||| uses the diagonal.
533instance Monad (Vect n) where
534 m >>= f = diag (map f m)
536instance Traversable (Vect n) where
537 traverse f [] = pure Vect.Nil
538 traverse f (x::xs) = [| Vect.(::) (f x) (traverse f xs) |]
540--------------------------------------------------------------------------------
542--------------------------------------------------------------------------------
544instance Show a => Show (Vect n a) where
547--------------------------------------------------------------------------------
549--------------------------------------------------------------------------------
551vectConsCong : (x : a) -> (xs : Vect n a) -> (ys : Vect m a) -> (xs = ys) -> (x :: xs = x :: ys)
552vectConsCong x xs xs Refl = Refl
554vectNilRightNeutral : (xs : Vect n a) -> xs ++ [] = xs
555vectNilRightNeutral [] = Refl
556vectNilRightNeutral (x :: xs) =
557 vectConsCong _ _ _ (vectNilRightNeutral xs)
559vectAppendAssociative : (x : Vect xLen a) -> (y : Vect yLen a) -> (z : Vect zLen a) -> x ++ (y ++ z) = (x ++ y) ++ z
560vectAppendAssociative [] y z = Refl
561vectAppendAssociative (x :: xs) ys zs =
562 vectConsCong _ _ _ (vectAppendAssociative xs ys zs)
566--------------------------------------------------------------------------------
568--------------------------------------------------------------------------------
571vectInjective1 : {xs, ys : Vect n a} -> {x, y : a} -> x :: xs = y :: ys -> x = y
572vectInjective1 {x=x} {y=x} {xs=xs} {ys=xs} Refl = Refl
575vectInjective2 : {xs, ys : Vect n a} -> {x, y : a} -> x :: xs = y :: ys -> xs = ys
576vectInjective2 {x=x} {y=x} {xs=xs} {ys=xs} Refl = Refl
578instance DecEq a => DecEq (Vect n a) where
579 decEq [] [] = Yes Refl
580 decEq (x :: xs) (y :: ys) with (decEq x y)
581 decEq (x :: xs) (x :: ys) | Yes Refl with (decEq xs ys)
582 decEq (x :: xs) (x :: xs) | Yes Refl | Yes Refl = Yes Refl
583 decEq (x :: xs) (x :: ys) | Yes Refl | No neq = No (neq . vectInjective2)
584 decEq (x :: xs) (y :: ys) | No neq = No (neq . vectInjective1)
586{- The following definition is elaborated in a wrong case-tree. Examination pending.
587instance DecEq a => DecEq (Vect n a) where
588 decEq [] [] = Yes Refl
589 decEq (x :: xs) (y :: ys) with (decEq x y, decEq xs ys)
590 decEq (x :: xs) (x :: xs) | (Yes Refl, Yes Refl) = Yes Refl
591 decEq (x :: xs) (y :: ys) | (_, No nEqTl) = No (\p => nEqTl (vectInjective2 p))
592 decEq (x :: xs) (y :: ys) | (No nEqHd, _) = No (\p => nEqHd (vectInjective1 p))
595-- For the primitives, we have to cheat because we don't have access to their
596-- internal implementations.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list