16 | ||| Empty vector
18 | ||| A non-empty vector of length `S len`, consisting of a head element and
19 | ||| the rest of the list, of length `len`.
22 | -- Hints for interactive editing
30 | ||| Show that the length function on vectors in fact calculates the length
31 | export
36 | export
40 | export
44 | export
48 | --------------------------------------------------------------------------------
49 | -- Indexing into vectors
50 | --------------------------------------------------------------------------------
52 | export
57 | ||| All but the first element of the vector
58 | |||
59 | ||| ```idris example
60 | ||| tail [1,2,3,4]
61 | ||| ```
66 | ||| Only the first element of the vector
67 | |||
68 | ||| ```idris example
69 | ||| head [1,2,3,4]
70 | ||| ```
75 | export
79 | ||| The last element of the vector
80 | |||
81 | ||| ```idris example
82 | ||| last [1,2,3,4]
83 | ||| ```
89 | ||| All but the last element of the vector
90 | |||
91 | ||| ```idris example
92 | ||| init [1,2,3,4]
93 | ||| ```
99 | ||| Extract the first `n` elements of a Vect.
108 | ||| Take precisely n elements from the stream.
109 | ||| @ n how many elements to take
110 | ||| @ xs the stream
116 | ||| Drop the first `n` elements of a Vect.
122 | ||| Drop up to the first `n` elements of a Vect.
129 | ||| Generate all of the Fin elements as a Vect whose length is the number of
130 | ||| elements.
131 | |||
132 | ||| Useful, for example, when one wants all the indices for specific Vect.
135 | -- implemented using `map`, so the definition is further down
137 | ||| Extract a particular element from a vector
138 | |||
139 | ||| ```idris example
140 | ||| index 1 [1,2,3,4]
141 | ||| ```
147 | ||| Insert an element at a particular index
148 | |||
149 | ||| ```idris example
150 | ||| insertAt 1 8 [1,2,3,4]
151 | ||| ```
157 | ||| Construct a new vector consisting of all but the indicated element
158 | |||
159 | ||| ```idris example
160 | ||| deleteAt 1 [1,2,3,4]
161 | ||| ```
168 | ||| Replace an element at a particular index with another
169 | |||
170 | ||| ```idris example
171 | ||| replaceAt 1 8 [1,2,3,4]
172 | ||| ```
178 | ||| Replace the element at a particular index with the result of applying a function to it
179 | ||| @ i the index to replace at
180 | ||| @ f the update function
181 | ||| @ xs the vector to replace in
182 | |||
183 | ||| ```idris example
184 | ||| updateAt 1 (+10) [1,2,3,4]
185 | ||| ```
191 | ||| Append two vectors
192 | |||
193 | ||| ```idris example
194 | ||| [1,2,3,4] ++ [5,6]
195 | ||| ```
201 | ||| Add an element at the end of the vector.
202 | ||| The main use case for it is to get the expected type signature
203 | ||| `Vect n a -> a -> Vect (S n) a` instead of
204 | ||| `Vect n a -> a -> Vect (n + 1) a` which you get by using `++ [x]`
205 | |||
206 | ||| Snoc gets its name by reversing `cons`, indicating we are
207 | ||| tacking on the element at the end rather than the beginning.
208 | ||| `append` would also be a suitable name.
209 | |||
210 | ||| @ xs The vector to be appended
211 | ||| @ v The value to append
217 | ||| Pop the last element from a vector. This is the opposite of `snoc`, in that
218 | ||| `(uncurry snoc) unsnoc xs` is `xs`. It is equivalent to `(init xs, last xs)`,
219 | ||| but traverses the vector once.
220 | |||
221 | ||| @ xs The vector to pop the element from.
227 | ||| Repeat some value some number of times.
228 | |||
229 | ||| @ len the number of times to repeat it
230 | ||| @ x the value to repeat
231 | |||
232 | ||| ```idris example
233 | ||| replicate 4 1
234 | ||| ```
240 | ||| Merge two ordered vectors
241 | |||
242 | ||| ```idris example
243 | ||| mergeBy compare (fromList [1,3,5]) (fromList [2,3,4,5,6])
244 | ||| ```
245 | export
246 | mergeBy : (elem -> elem -> Ordering) -> (xs : Vect n elem) -> (ys : Vect m elem) -> Vect (n + m) elem
255 | export
259 | -- Properties for functions in this section --
261 | export
262 | replaceAtSameIndex : (xs : Vect n a) -> (i : Fin n) -> (0 y : a) -> index i (replaceAt i y xs) = y
266 | export
267 | replaceAtDiffIndexPreserves : (xs : Vect n a) -> (i, j : Fin n) -> Not (i = j) -> (0 y : a) -> index i (replaceAt j y xs) = index i xs
271 | replaceAtDiffIndexPreserves (_::_) (FS z) (FS w) co y = replaceAtDiffIndexPreserves _ z w (\zw => co $ cong FS zw) y
273 | --------------------------------------------------------------------------------
274 | -- Transformations
275 | --------------------------------------------------------------------------------
277 | ||| Reverse the second vector, prepending the result to the first.
278 | |||
279 | ||| ```idris example
280 | ||| reverseOnto [0, 1] [10, 11, 12]
281 | ||| ```
288 | ||| Reverse the order of the elements of a vector
289 | |||
290 | ||| ```idris example
291 | ||| reverse [1,2,3,4]
292 | ||| ```
297 | ||| Alternate an element between the other elements of a vector
298 | ||| @ sep the element to intersperse
299 | ||| @ xs the vector to separate with `sep`
300 | |||
301 | ||| ```idris example
302 | ||| intersperse 0 [1,2,3,4]
303 | ||| ```
304 | export
308 | where
314 | --------------------------------------------------------------------------------
315 | -- Conversion from list (toList is provided by Foldable)
316 | --------------------------------------------------------------------------------
326 | ||| Convert a list to a vector.
327 | |||
328 | ||| The length of the list should be statically known.
329 | |||
330 | ||| ```idris example
331 | ||| fromList [1,2,3,4]
332 | ||| ```
345 | --------------------------------------------------------------------------------
346 | -- Equality
347 | --------------------------------------------------------------------------------
359 | --------------------------------------------------------------------------------
360 | -- Order
361 | --------------------------------------------------------------------------------
371 | --------------------------------------------------------------------------------
372 | -- Maps
373 | --------------------------------------------------------------------------------
380 | ||| Map a partial function across a vector, returning those elements for which
381 | ||| the function had a value.
382 | |||
383 | ||| The first projection of the resulting pair (ie the length) will always be
384 | ||| at most the length of the input vector. This is not, however, guaranteed
385 | ||| by the type.
386 | |||
387 | ||| @ f the partial function (expressed by returning `Maybe`)
388 | ||| @ xs the vector to check for results
389 | |||
390 | ||| ```idris example
391 | ||| mapMaybe ((find (=='a')) . unpack) (fromList ["abc","ade","bgh","xyz"])
392 | ||| ```
393 | export
402 | -- now that we have `map`, we can finish implementing `allFins`
406 | --------------------------------------------------------------------------------
407 | -- Folds
408 | --------------------------------------------------------------------------------
415 | export
416 | foldrImplGoLemma
440 | --------------------------------------------------------------------------------
441 | -- Special folds
442 | --------------------------------------------------------------------------------
444 | ||| Flatten a vector of equal-length vectors
445 | |||
446 | ||| ```idris example
447 | ||| concat [[1,2,3], [4,5,6]]
448 | ||| ```
454 | ||| Foldr without seeding the accumulator
455 | |||
456 | ||| ```idris example
457 | ||| foldr1 (-) (fromList [1,2,3])
458 | ||| ```
464 | ||| Foldl without seeding the accumulator
465 | |||
466 | ||| ```idris example
467 | ||| foldl1 (-) (fromList [1,2,3])
468 | ||| ```
473 | --------------------------------------------------------------------------------
474 | -- Scans
475 | --------------------------------------------------------------------------------
477 | ||| The scanr function is similar to foldr, but returns all the intermediate
478 | ||| accumulator states in the form of a vector. Note the intermediate accumulator
479 | ||| states appear in the result in reverse order - the first state appears last
480 | ||| in the result.
481 | |||
482 | ||| ```idris example
483 | ||| scanr (-) 0 (fromList [1,2,3])
484 | ||| ```
490 | ||| The scanr1 function is a variant of scanr that doesn't require an explicit
491 | ||| starting value.
492 | ||| It assumes the last element of the vector to be the starting value and then
493 | ||| starts the fold with the element preceding it.
494 | |||
495 | ||| ```idris example
496 | ||| scanr1 (-) (fromList [1,2,3])
497 | ||| ```
503 | ||| The scanl function is similar to foldl, but returns all the intermediate
504 | ||| accumulator states in the form of a vector.
505 | |||
506 | ||| ```idris example
507 | ||| scanl (-) 0 (fromList [1,2,3])
508 | ||| ```
514 | ||| The scanl1 function is a variant of scanl that doesn't require an explicit
515 | ||| starting value.
516 | ||| It assumes the first element of the vector to be the starting value and then
517 | ||| starts the fold with the element following it.
518 | |||
519 | ||| ```idris example
520 | ||| scanl1 (-) (fromList [1,2,3])
521 | ||| ```
527 | --------------------------------------------------------------------------------
528 | -- Membership tests
529 | --------------------------------------------------------------------------------
531 | ||| Find the association of some key with a user-provided comparison
532 | ||| @ p the comparison operator for keys (True if they match)
533 | ||| @ e the key to look for
534 | |||
535 | ||| ```idris example
536 | ||| lookupBy (==) 2 [(1, 'a'), (2, 'b'), (3, 'c')]
537 | ||| ```
543 | ||| Find the association of some key using the default Boolean equality test
544 | |||
545 | ||| ```idris example
546 | ||| lookup 3 [(1, 'a'), (2, 'b'), (3, 'c')]
547 | ||| ```
552 | ||| Check if any element of xs is found in elems by a user-provided comparison
553 | ||| @ p the comparison operator
554 | ||| @ elems the vector to search
555 | ||| @ xs what to search for
556 | |||
557 | ||| ```idris example
558 | ||| hasAnyBy (==) [2,5] [1,2,3,4]
559 | ||| ```
561 | hasAnyBy : (p : elem -> elem -> Bool) -> (elems : Vect m elem) -> (xs : Vect len elem) -> Bool
565 | ||| Check if any element of xs is found in elems using the default Boolean equality test
566 | |||
567 | ||| ```idris example
568 | ||| hasAny [2,5] [1,2,3,4]
569 | ||| ```
574 | --------------------------------------------------------------------------------
575 | -- Searching with a predicate
576 | --------------------------------------------------------------------------------
578 | ||| Find the first element of the vector that satisfies some test
579 | ||| @ p the test to satisfy
580 | |||
581 | ||| ```idris example
582 | ||| find (== 3) [1,2,3,4]
583 | ||| ```
589 | ||| Find the index of the first element of the vector that satisfies some test
590 | |||
591 | ||| ```idris example
592 | ||| findIndex (== 3) [1,2,3,4]
593 | ||| ```
599 | ||| Find the indices of all elements that satisfy some test
600 | |||
601 | ||| ```idris example
602 | ||| findIndices (< 3) [1,2,3,4]
603 | ||| ```
611 | ||| Find the index of the first element of the vector that satisfies some test
612 | |||
613 | ||| ```idris example
614 | ||| elemIndexBy (==) 3 [1,2,3,4]
615 | ||| ```
620 | ||| Find the index of the first element of the vector equal to the given one.
621 | |||
622 | ||| ```idris example
623 | ||| elemIndex 3 [1,2,3,4]
624 | ||| ```
629 | ||| Find the indices of all elements that satisfy some test
630 | |||
631 | ||| ```idris example
632 | ||| elemIndicesBy (<=) 3 [1,2,3,4]
633 | ||| ```
638 | ||| Find the indices of all elements equal to the given one
639 | |||
640 | ||| ```idris example
641 | ||| elemIndices 3 [1,2,3,4,3]
642 | ||| ```
647 | --------------------------------------------------------------------------------
648 | -- Filters
649 | --------------------------------------------------------------------------------
651 | ||| Find all elements of a vector that satisfy some test
652 | |||
653 | ||| ```idris example
654 | ||| filter (< 3) (fromList [1,2,3,4])
655 | ||| ```
663 | else
674 | ||| Make the elements of some vector unique by some test
675 | |||
676 | ||| ```idris example
677 | ||| nubBy (==) (fromList [1,2,2,3,4,4])
678 | ||| ```
683 | ||| Make the elements of some vector unique by the default Boolean equality
684 | |||
685 | ||| ```idris example
686 | ||| nub (fromList [1,2,2,3,4,4])
687 | ||| ```
692 | ||| Delete first element from list according to some test
693 | |||
694 | ||| ```idris example
695 | ||| deleteBy (<) 3 (fromList [1,2,2,3,4,4])
696 | ||| ```
705 | ||| Delete first element from list equal to the given one
706 | |||
707 | ||| ```idris example
708 | ||| delete 2 (fromList [1,2,2,3,4,4])
709 | ||| ```
715 | --------------------------------------------------------------------------------
716 | -- Splitting and breaking vects
717 | --------------------------------------------------------------------------------
719 | ||| A tuple where the first element is a `Vect` of the `n` first elements and
720 | ||| the second element is a `Vect` of the remaining elements of the original.
721 | ||| It is equivalent to `(take n xs, drop n xs)` (`splitAtTakeDrop`),
722 | ||| but is more efficient.
723 | |||
724 | ||| @ n the index to split at
725 | ||| @ xs the `Vect` to split in two
726 | |||
727 | ||| ```idris example
728 | ||| splitAt 2 (fromList [1,2,3,4])
729 | ||| ```
736 | ||| A tuple where the first element is a `Vect` of the `n` elements passing given test
737 | ||| and the second element is a `Vect` of the remaining elements of the original.
738 | |||
739 | ||| ```idris example
740 | ||| partition (== 2) (fromList [1,2,3,2,4])
741 | ||| ```
749 | else
752 | ||| Split a vector whose length is a multiple of two numbers, k times n, into k
753 | ||| sections of length n.
754 | |||
755 | ||| ```idris example
756 | ||| > kSplits 2 4 [1, 2, 3, 4, 5, 6, 7, 8]
757 | ||| [[1, 2, 3, 4], [5, 6, 7, 8]]
758 | ||| ```
765 | ||| Split a vector whose length is a multiple of two numbers, k times n, into n
766 | ||| sections of length k.
767 | |||
768 | ||| ```idris example
769 | ||| > nSplits 2 4 [1, 2, 3, 4, 5, 6, 7, 8]
770 | ||| [[1, 5], [2, 6], [3, 7], [4, 8]]
771 | ||| ```
774 | -- implemented via matrix transposition, so the definition is further down
776 | --------------------------------------------------------------------------------
777 | -- Predicates
778 | --------------------------------------------------------------------------------
780 | ||| Verify vector prefix
781 | |||
782 | ||| ```idris example
783 | ||| isPrefixOfBy (==) (fromList [1,2]) (fromList [1,2,3,4])
784 | ||| ```
791 | ||| Verify vector prefix
792 | |||
793 | ||| ```idris example
794 | ||| isPrefixOf (fromList [1,2]) (fromList [1,2,3,4])
795 | ||| ```
800 | ||| Verify vector suffix
801 | |||
802 | ||| ```idris example
803 | ||| isSuffixOfBy (==) (fromList [3,4]) (fromList [1,2,3,4])
804 | ||| ```
809 | ||| Verify vector suffix
810 | |||
811 | ||| ```idris example
812 | ||| isSuffixOf (fromList [3,4]) (fromList [1,2,3,4])
813 | ||| ```
818 | --------------------------------------------------------------------------------
819 | -- Conversions
820 | --------------------------------------------------------------------------------
822 | ||| Convert Maybe type into Vect
823 | |||
824 | ||| ```idris example
825 | ||| maybeToVect (Just 2)
826 | ||| ```
832 | ||| Convert first element of Vect (if exists) into Maybe.
833 | |||
834 | ||| ```idris example
835 | ||| vectToMaybe [2]
836 | ||| ```
842 | --------------------------------------------------------------------------------
843 | -- Misc
844 | --------------------------------------------------------------------------------
846 | ||| Filter out Nothings from Vect and unwrap the Justs
847 | |||
848 | ||| ```idris example
849 | ||| catMaybes [Just 1, Just 2, Nothing, Nothing, Just 5]
850 | ||| ```
859 | ||| Get diagonal elements
860 | |||
861 | ||| ```idris example
862 | ||| diag [[1,2,3], [4,5,6], [7,8,9]]
863 | ||| ```
893 | --------------------------------------------------------------------------------
894 | -- Zippable
895 | --------------------------------------------------------------------------------
915 | export
916 | zipWithIndexLinear : (0 f : _) -> (xs, ys : Vect n a) -> (i : Fin n) -> index i (zipWith f xs ys) = f (index i xs) (index i ys)
920 | export
921 | zipWith3IndexLinear : (0 f : _) -> (xs, ys, zs : Vect n a) -> (i : Fin n) -> index i (zipWith3 f xs ys zs) = f (index i xs) (index i ys) (index i zs)
925 | --------------------------------------------------------------------------------
926 | -- Permutation
927 | --------------------------------------------------------------------------------
929 | ||| Rearrange the elements of a vector according to some permutation of its
930 | ||| indices.
931 | ||| @ v the vector whose elements to rearrange
932 | ||| @ p the permutation to apply
933 | |||
934 | ||| ```idris example
935 | ||| > permute ['a', 'b', 'c', 'd'] [0, 3, 2, 1]
936 | ||| ['a', 'd' , 'c' ,'b']
937 | ||| ```
938 | export
942 | --------------------------------------------------------------------------------
943 | -- Matrix transposition
944 | --------------------------------------------------------------------------------
946 | ||| Transpose a `Vect` of `Vect`s, turning rows into columns and vice versa.
947 | |||
948 | ||| This is like zipping all the inner `Vect`s together and is equivalent to `traverse id` (`transposeTraverse`).
949 | |||
950 | ||| As the types ensure rectangularity, this is an involution, unlike `Prelude.List.transpose`.
951 | |||
952 | ||| ```idris example
953 | ||| transpose [[1,2], [3,4], [5,6], [7,8]]
954 | ||| ```
960 | -- nSplits from earlier on
963 | --------------------------------------------------------------------------------
964 | -- Applicative/Monad/Traversable
965 | --------------------------------------------------------------------------------
966 | -- These only work if the length is known at run time!
973 | -- ||| This monad is different from the List monad, (>>=)
974 | -- ||| uses the diagonal.
984 | --------------------------------------------------------------------------------
985 | -- Semigroup/Monoid
986 | --------------------------------------------------------------------------------
996 | --------------------------------------------------------------------------------
997 | -- Show
998 | --------------------------------------------------------------------------------
1000 | export
1004 | -- Some convenience functions for testing lengths
1006 | -- Needs to be Maybe rather than Dec, because if 'n' is unequal to m, we
1007 | -- only know we don't know how to make a Vect n a, not that one can't exist.
1008 | export
1015 | ||| If the given Vect is at least the required length, return a Vect with
1016 | ||| at least that length in its type, otherwise return Nothing
1017 | ||| @len the required length
1018 | ||| @xs the vector with the desired length
1019 | export