2 | import public Control.Function
7 | import public Data.Zippable
13 | isNil : List a -> Bool
19 | isCons : List a -> Bool
26 | snoc : List a -> a -> List a
27 | snoc xs x = xs ++ [x]
35 | take : (n : Nat) -> (xs : List a) -> List a
36 | take (S k) (x :: xs) = x :: take k xs
45 | drop : (n : Nat) -> (xs : List a) -> List a
48 | drop (S n) (_::xs) = drop n xs
55 | data InBounds : (k : Nat) -> (xs : List a) -> Type where
57 | InFirst : InBounds Z (x :: xs)
59 | InLater : InBounds k xs -> InBounds (S k) (x :: xs)
62 | Uninhabited (InBounds k []) where
63 | uninhabited InFirst
impossible
64 | uninhabited (InLater
_) impossible
67 | Uninhabited (InBounds k xs) => Uninhabited (InBounds (S k) (x::xs)) where
68 | uninhabited (InLater y) = uninhabited y
72 | inBounds : (k : Nat) -> (xs : List a) -> Dec (InBounds k xs)
73 | inBounds _ [] = No uninhabited
74 | inBounds Z (_ :: _) = Yes InFirst
75 | inBounds (S k) (x :: xs) with (inBounds k xs)
76 | inBounds (S k) (x :: xs) | (Yes prf) = Yes (InLater prf)
77 | inBounds (S k) (x :: xs) | (No contra)
78 | = No $
\(InLater y) => contra y
84 | index : (n : Nat) -> (xs : List a) -> {auto 0 ok : InBounds n xs} -> a
85 | index Z (x :: xs) {ok = InFirst} = x
86 | index (S k) (_ :: xs) {ok = InLater _} = index k xs
89 | index' : (xs : List a) -> Fin (length xs) -> a
90 | index' (x::_) FZ = x
91 | index' (_::xs) (FS i) = index' xs i
98 | iterate : (f : a -> Maybe a) -> (x : a) -> List a
99 | iterate f x = x :: case f x of
101 | Just y => iterate f y
111 | unfoldr : (f : b -> Maybe (a, b)) -> b -> List a
112 | unfoldr f c = case f c of
114 | Just (a, n) => a :: unfoldr f n
123 | iterateN : (n : Nat) -> (f : a -> a) -> (x : a) -> List a
124 | iterateN Z _ _ = []
125 | iterateN (S k) f x = x :: iterateN k f (f x)
132 | takeWhile : (p : a -> Bool) -> (xs : List a) -> List a
133 | takeWhile p [] = []
134 | takeWhile p (x::xs) = if p x then x :: takeWhile p xs else []
142 | dropWhile : (p : a -> Bool) -> (xs : List a) -> List a
143 | dropWhile p [] = []
144 | dropWhile p (x::xs) = if p x then dropWhile p xs else x::xs
148 | find : (p : a -> Bool) -> (xs : List a) -> Maybe a
149 | find p [] = Nothing
150 | find p (x::xs) = if p x then Just x else find p xs
155 | findIndex : (a -> Bool) -> (xs : List a) -> Maybe $
Fin (length xs)
156 | findIndex _ [] = Nothing
157 | findIndex p (x :: xs) = if p x
159 | else FS <$> findIndex p xs
163 | findIndices : (a -> Bool) -> List a -> List Nat
164 | findIndices p = h 0 where
165 | h : Nat -> List a -> List Nat
167 | h lvl (x :: xs) = if p x
168 | then lvl :: h (S lvl) xs
173 | lookupBy : (a -> b -> Bool) -> a -> List (b, v) -> Maybe v
174 | lookupBy p e [] = Nothing
175 | lookupBy p e ((l, r) :: xs) =
183 | lookup : Eq a => a -> List (a, b) -> Maybe b
184 | lookup = lookupBy (==)
193 | nubBy : (p : a -> a -> Bool) -> (xs : List a) -> List a
196 | nubBy' : List a -> (a -> a -> Bool) -> List a -> List a
197 | nubBy' acc p [] = []
198 | nubBy' acc p (x::xs) =
199 | if elemBy p x acc then
202 | x :: nubBy' (x::acc) p xs
214 | nub : Eq a => List a -> List a
227 | insertAt : (idx : Nat) -> (x : a) -> (xs : List a) -> {auto 0 ok : idx `LTE` length xs} -> List a
228 | insertAt Z x xs = x :: xs
229 | insertAt {ok=LTESucc _} (S n) x (y :: ys) = y :: (insertAt n x ys)
240 | deleteAt : (idx : Nat) -> (xs : List a) -> {auto 0 prf : InBounds idx xs} -> List a
241 | deleteAt {prf=InFirst} Z (_ :: xs) = xs
242 | deleteAt {prf=InLater _} (S k) (x :: xs) = x :: deleteAt k xs
246 | deleteBy : (a -> b -> Bool) -> a -> List b -> List b
247 | deleteBy _ _ [] = []
248 | deleteBy eq x (y::ys) = if x `eq` y then ys else y :: deleteBy eq x ys
260 | delete : Eq a => a -> List a -> List a
261 | delete = deleteBy (==)
269 | deleteFirstsBy : (p : a -> b -> Bool) -> (source : List b) -> (undesirables : List a) -> List b
270 | deleteFirstsBy p = foldl (flip (deleteBy p))
285 | (\\) : Eq a => List a -> List a -> List a
286 | (\\) = foldl (flip delete)
290 | unionBy : (a -> a -> Bool) -> List a -> List a -> List a
291 | unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
300 | union : Eq a => List a -> List a -> List a
301 | union = unionBy (==)
307 | spanBy : (a -> Maybe b) -> List a -> (List b, List a)
308 | spanBy p [] = ([], [])
309 | spanBy p (x :: xs) = case p x of
310 | Nothing => ([], x :: xs)
311 | Just y => let (ys, zs) = spanBy p xs in (y :: ys, zs)
317 | span : (a -> Bool) -> List a -> (List a, List a)
318 | span p [] = ([], [])
321 | let (ys, zs) = span p xs in
330 | break : (a -> Bool) -> List a -> (List a, List a)
331 | break p xs = span (not . p) xs
334 | singleton : a -> List a
338 | split : (a -> Bool) -> List a -> List1 (List a)
341 | (chunk, []) => singleton chunk
342 | (chunk, (c :: rest)) => chunk ::: forget (split p (assert_smaller xs rest))
350 | splitAt : (n : Nat) -> (xs : List a) -> (List a, List a)
351 | splitAt Z xs = ([], xs)
352 | splitAt (S k) [] = ([], [])
353 | splitAt (S k) (x :: xs)
354 | = let (tk, dr) = splitAt k xs in
364 | partition : (p : a -> Bool) -> (xs : List a) -> (List a, List a)
365 | partition p [] = ([], [])
366 | partition p (x::xs) =
367 | let (lefts, rights) = partition p xs in
380 | inits : List a -> List (List a)
381 | inits xs = [] :: case xs of
383 | x :: xs' => map (x ::) (inits xs')
392 | tails : List a -> List (List a)
393 | tails xs = xs :: case xs of
395 | _ :: xs' => tails xs'
404 | splitOn : Eq a => a -> List a -> List1 (List a)
405 | splitOn a = split (== a)
417 | replaceAt : (idx : Nat) -> a -> (xs : List a) -> {auto 0 ok : InBounds idx xs} -> List a
418 | replaceAt Z y (_ :: xs) {ok=InFirst} = y :: xs
419 | replaceAt (S k) y (x :: xs) {ok=InLater _} = x :: replaceAt k y xs
428 | replaceWhen : (p : a -> Bool) -> (b : a) -> (l : List a) -> List a
429 | replaceWhen p b l = map (\c => if p c then b else c) l
444 | replaceOn : Eq a => (e : a) -> (b : a) -> (l : List a) -> List a
445 | replaceOn e = replaceWhen (== e)
447 | replicateTR : List a -> Nat -> a -> List a
448 | replicateTR as Z _ = as
449 | replicateTR as (S n) x = replicateTR (x :: as) n x
456 | replicate : (n : Nat) -> (x : a) -> List a
458 | replicate (S n) x = x :: replicate n x
461 | %transform "tailRecReplicate"
List.replicate = List.replicateTR Nil
466 | intersectBy : (a -> a -> Bool) -> List a -> List a -> List a
467 | intersectBy eq xs ys = [x | x <- xs, any (eq x) ys]
472 | intersect : Eq a => List a -> List a -> List a
473 | intersect = intersectBy (==)
481 | intersectAllBy : (eq : a -> a -> Bool) -> (ls : List (List a)) -> List a
482 | intersectAllBy eq [] = []
483 | intersectAllBy eq (xs :: xss) = filter (\x => all (elemBy eq x) xss) xs
491 | intersectAll : Eq a => (ls : List (List a)) -> List a
492 | intersectAll = intersectAllBy (==)
499 | Zippable List where
500 | zipWith _ [] _ = []
501 | zipWith _ _ [] = []
502 | zipWith f (x :: xs) (y :: ys) = f x y :: zipWith f xs ys
504 | zipWith3 _ [] _ _ = []
505 | zipWith3 _ _ [] _ = []
506 | zipWith3 _ _ _ [] = []
507 | zipWith3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: zipWith3 f xs ys zs
509 | unzipWith f [] = ([], [])
510 | unzipWith f (x :: xs) = let (b, c) = f x
511 | (bs, cs) = unzipWith f xs in
514 | unzipWith3 f [] = ([], [], [])
515 | unzipWith3 f (x :: xs) = let (b, c, d) = f x
516 | (bs, cs, ds) = unzipWith3 f xs in
517 | (b :: bs, c :: cs, d :: ds)
525 | data NonEmpty : (xs : List a) -> Type where
526 | IsNonEmpty : NonEmpty (x :: xs)
530 | Uninhabited (NonEmpty []) where
531 | uninhabited IsNonEmpty
impossible
536 | head : (l : List a) -> {auto 0 ok : NonEmpty l} -> a
543 | tail : (l : List a) -> {auto 0 ok : NonEmpty l} -> List a
545 | tail (_ :: xs) = xs
550 | last : (l : List a) -> {auto 0 ok : NonEmpty l} -> a
553 | last (x :: xs@(_::_)) = last xs
558 | init : (l : List a) -> {auto 0 ok : NonEmpty l} -> List a
561 | init (x :: xs@(_::_)) = x :: init xs
565 | minimum : Ord a => (xs : List a) -> {auto 0 _ : NonEmpty xs} -> a
566 | minimum (x :: xs) = foldl min x xs
570 | uncons' : List a -> Maybe (a, List a)
571 | uncons' [] = Nothing
572 | uncons' (x :: xs) = Just (x, xs)
576 | head' : List a -> Maybe a
577 | head' = map fst . uncons'
581 | tail' : List a -> Maybe (List a)
582 | tail' = map snd . uncons'
588 | last' : List a -> Maybe a
590 | last' xs@(_::_) = Just (last xs)
596 | init' : List a -> Maybe (List a)
598 | init' xs@(_::_) = Just (init xs)
609 | foldr1By : (func : a -> b -> b) -> (map : a -> b) ->
610 | (l : List a) -> {auto 0 ok : NonEmpty l} -> b
611 | foldr1By f map [] impossible
612 | foldr1By f map [x] = map x
613 | foldr1By f map (x :: xs@(_::_)) = f x (foldr1By f map xs)
624 | foldl1By : (func : b -> a -> b) -> (map : a -> b) ->
625 | (l : List a) -> {auto 0 ok : NonEmpty l} -> b
626 | foldl1By f map [] impossible
627 | foldl1By f map (x::xs) = foldl f (map x) xs
633 | foldr1 : (a -> a -> a) -> (l : List a) -> {auto 0 ok : NonEmpty l} -> a
634 | foldr1 f xs = foldr1By f id xs
640 | foldl1 : (a -> a -> a) -> (l : List a) -> {auto 0 ok : NonEmpty l} -> a
641 | foldl1 f xs = foldl1By f id xs
647 | toList1 : (l : List a) -> {auto 0 ok : NonEmpty l} -> List1 a
648 | toList1 [] impossible
649 | toList1 (x :: xs) = x ::: xs
653 | toList1' : (l : List a) -> Maybe (List1 a)
654 | toList1' [] = Nothing
655 | toList1' (x :: xs) = Just (x ::: xs)
663 | interleave : (xs, ys : List a) -> List a
664 | interleave [] ys = ys
665 | interleave (x :: xs) ys = x :: interleave ys xs
677 | mergeReplicate : (sep : a) -> (xs : List a) -> List a
678 | mergeReplicate sep [] = []
679 | mergeReplicate sep (y::ys) = sep :: y :: mergeReplicate sep ys
691 | intersperse : (sep : a) -> (xs : List a) -> List a
692 | intersperse sep [] = []
693 | intersperse sep (x::xs) = x :: mergeReplicate sep xs
705 | intercalate : (sep : List a) -> (xss : List (List a)) -> List a
706 | intercalate sep xss = concat $
intersperse sep xss
710 | catMaybes : List (Maybe a) -> List a
711 | catMaybes = mapMaybe id
719 | sorted : Ord a => List a -> Bool
720 | sorted (x :: xs @ (y :: _)) = x <= y && sorted xs
727 | mergeBy : (a -> a -> Ordering) -> List a -> List a -> List a
728 | mergeBy order [] right = right
729 | mergeBy order left [] = left
730 | mergeBy order (x::xs) (y::ys) =
734 | LT => x :: mergeBy order xs (y::ys)
735 | _ => y :: mergeBy order (x::xs) ys
739 | merge : Ord a => List a -> List a -> List a
740 | merge left right = mergeBy compare left right
747 | sortBy : (cmp : a -> a -> Ordering) -> (xs : List a) -> List a
749 | sortBy cmp [x] = [x]
750 | sortBy cmp xs = let (x, y) = split xs in
752 | (sortBy cmp (assert_smaller xs x))
753 | (sortBy cmp (assert_smaller xs y))
755 | splitRec : List b -> List a -> (List a -> List a) -> (List a, List a)
756 | splitRec (_::_::xs) (y::ys) zs = splitRec xs ys (zs . ((::) y))
757 | splitRec _ ys zs = (ys, zs [])
763 | split : List a -> (List a, List a)
764 | split xs = splitRec xs xs id
768 | sort : Ord a => List a -> List a
769 | sort = sortBy compare
778 | prefixOfBy : (match : a -> b -> Maybe m) ->
779 | (left : List a) -> (right : List b) ->
780 | Maybe (List m, List b)
781 | prefixOfBy p = go [<] where
782 | go : SnocList m -> List a -> List b -> Maybe (List m, List b)
783 | go sm [] bs = pure (sm <>> [], bs)
784 | go sm as [] = Nothing
785 | go sm (a :: as) (b :: bs) = go (sm :< !(p a b)) as bs
794 | isPrefixOfBy : (eq : a -> b -> Bool) ->
795 | (left : List a) -> (right : List b) -> Bool
796 | isPrefixOfBy p [] _ = True
797 | isPrefixOfBy p _ [] = False
798 | isPrefixOfBy p (x::xs) (y::ys) = p x y && isPrefixOfBy p xs ys
803 | isPrefixOf : Eq a => List a -> List a -> Bool
804 | isPrefixOf = isPrefixOfBy (==)
813 | suffixOfBy : (match : a -> b -> Maybe m) ->
814 | (left : List a) -> (right : List b) ->
815 | Maybe (List b, List m)
816 | suffixOfBy match left right
817 | = do (ms, bs) <- prefixOfBy match (reverse left) (reverse right)
818 | pure (reverse bs, reverse ms)
827 | isSuffixOfBy : (eq : a -> b -> Bool) ->
828 | (left : List a) -> (right : List b) -> Bool
829 | isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
834 | isSuffixOf : Eq a => List a -> List a -> Bool
835 | isSuffixOf = isSuffixOfBy (==)
840 | infixOfBy : (match : a -> b -> Maybe m) ->
841 | (left : List a) -> (right : List b) ->
842 | Maybe (List b, List m, List b)
843 | infixOfBy _ [] right = Just ([], [], right)
844 | infixOfBy p left@(_::_) right = go [<] right where
845 | go : (acc : SnocList b) -> List b -> Maybe (List b, List m, List b)
847 | go pre curr@(c::rest) = case prefixOfBy p left curr of
848 | Just (inf, post) => Just (pre <>> [], inf, post)
849 | Nothing => go (pre:<c) rest
854 | isInfixOfBy : (eq : a -> b -> Bool) ->
855 | (left : List a) -> (right : List b) -> Bool
856 | isInfixOfBy p n h = any (isPrefixOfBy p n) (tails h)
869 | isInfixOf : Eq a => List a -> List a -> Bool
870 | isInfixOf = isInfixOfBy (==)
884 | transpose : List (List a) -> List (List a)
886 | transpose (heads :: tails) = spreadHeads heads (transpose tails) where
887 | spreadHeads : List a -> List (List a) -> List (List a)
888 | spreadHeads [] tails = tails
889 | spreadHeads (head :: heads) [] = [head] :: spreadHeads heads []
890 | spreadHeads (head :: heads) (tail :: tails) = (head :: tail) :: spreadHeads heads tails
899 | groupBy : (a -> a -> Bool) -> List a -> List (List1 a)
901 | groupBy eq (h :: t) = let (ys,zs) = go h t
904 | where go : a -> List a -> (List1 a, List (List1 a))
905 | go v [] = (singleton v,[])
906 | go v (x :: xs) = let (ys,zs) = go x xs
908 | then (cons v ys, zs)
909 | else (singleton v, ys :: zs)
916 | group : Eq a => List a -> List (List1 a)
917 | group = groupBy (==)
922 | groupWith : Eq b => (a -> b) -> List a -> List (List1 a)
923 | groupWith f = groupBy (\x,y => f x == f y)
929 | groupAllWith : Ord b => (a -> b) -> List a -> List (List1 a)
930 | groupAllWith f = groupWith f . sortBy (comparing f)
941 | grouped : (n : Nat) -> {auto 0 p : IsSucc n} -> List a -> List (List a)
943 | grouped (S m) (x::xs) = go [<] [<x] m xs
945 | go : SnocList (List a) -> SnocList a -> Nat -> List a -> List (List a)
946 | go sxs sx c [] = sxs <>> [sx <>> []]
947 | go sxs sx 0 (x :: xs) = go (sxs :< (sx <>> [])) [<x] m xs
948 | go sxs sx (S k) (x :: xs) = go sxs (sx :< x) k xs
956 | Uninhabited ([] = x :: xs) where
957 | uninhabited Refl
impossible
961 | Uninhabited (x :: xs = []) where
962 | uninhabited Refl
impossible
968 | {0 xs : List a} -> Either (Uninhabited $
x === y) (Uninhabited $
xs === ys) => Uninhabited (x::xs = y::ys) where
969 | uninhabited @{Left z} Refl = uninhabited @{z} Refl
970 | uninhabited @{Right z} Refl = uninhabited @{z} Refl
974 | Biinjective Prelude.(::) where
975 | biinjective Refl = (Refl, Refl)
979 | consInjective : forall x, xs, y, ys .
980 | the (List a) (x :: xs) = the (List b) (y :: ys) -> (x = y, xs = ys)
981 | consInjective Refl = (Refl, Refl)
983 | lengthPlusIsLengthPlus : (n : Nat) -> (xs : List a) ->
984 | lengthPlus n xs = n + length xs
985 | lengthPlusIsLengthPlus n [] = sym $
plusZeroRightNeutral n
986 | lengthPlusIsLengthPlus n (x::xs) =
988 | (lengthPlusIsLengthPlus (S n) xs)
989 | (plusSuccRightSucc n (length xs))
991 | lengthTRIsLength : (xs : List a) -> lengthTR xs = length xs
992 | lengthTRIsLength = lengthPlusIsLengthPlus Z
996 | reverseReverseOnto : (l, r : List a) ->
997 | reverse (reverseOnto l r) = reverseOnto r l
998 | reverseReverseOnto _ [] = Refl
999 | reverseReverseOnto l (x :: xs) = reverseReverseOnto (x :: l) xs
1003 | reverseInvolutive : (xs : List a) -> reverse (reverse xs) = xs
1004 | reverseInvolutive = reverseReverseOnto []
1008 | consReverse : (x : a) -> (l, r : List a) ->
1009 | x :: reverseOnto r l = reverseOnto r (reverseOnto [x] (reverse l))
1010 | consReverse _ [] _ = Refl
1011 | consReverse x (y::ys) r
1012 | = rewrite consReverse x ys (y :: r) in
1013 | rewrite cong (reverseOnto r . reverse) $
consReverse x ys [y] in
1014 | rewrite reverseInvolutive (y :: reverseOnto [x] (reverse ys)) in
1019 | consTailRecAppend : (x : a) -> (l, r : List a) ->
1020 | tailRecAppend (x :: l) r = x :: (tailRecAppend l r)
1021 | consTailRecAppend x l r
1022 | = rewrite consReverse x (reverse l) r in
1023 | rewrite reverseInvolutive l in
1028 | tailRecAppendIsAppend : (xs, ys : List a) -> tailRecAppend xs ys = xs ++ ys
1029 | tailRecAppendIsAppend [] ys = Refl
1030 | tailRecAppendIsAppend (x::xs) ys =
1031 | trans (consTailRecAppend x xs ys) (cong (x ::) $
tailRecAppendIsAppend xs ys)
1035 | appendNilRightNeutral : (l : List a) -> l ++ [] = l
1036 | appendNilRightNeutral [] = Refl
1037 | appendNilRightNeutral (_::xs) = rewrite appendNilRightNeutral xs in Refl
1041 | appendAssociative : (l, c, r : List a) -> l ++ (c ++ r) = (l ++ c) ++ r
1042 | appendAssociative [] c r = Refl
1043 | appendAssociative (_::xs) c r = rewrite appendAssociative xs c r in Refl
1046 | revOnto : (xs, vs : List a) -> reverseOnto xs vs = reverse vs ++ xs
1049 | = rewrite revOnto (v :: xs) vs in
1050 | rewrite appendAssociative (reverse vs) [v] xs in
1051 | rewrite revOnto [v] vs in Refl
1055 | revAppend : (vs, ns : List a) -> reverse ns ++ reverse vs = reverse (vs ++ ns)
1056 | revAppend [] ns = rewrite appendNilRightNeutral (reverse ns) in Refl
1058 | = rewrite revOnto [v] vs in
1059 | rewrite revOnto [v] (vs ++ ns) in
1060 | rewrite sym (revAppend vs ns) in
1061 | rewrite appendAssociative (reverse ns) (reverse vs) [v] in
1067 | dropFusion : (n, m : Nat) -> (l : List t) -> drop n (drop m l) = drop (n+m) l
1068 | dropFusion Z m l = Refl
1069 | dropFusion (S n) Z l = rewrite plusZeroRightNeutral n in Refl
1070 | dropFusion (S n) (S m) [] = Refl
1071 | dropFusion (S n) (S m) (x::l) = rewrite plusAssociative n 1 m in
1072 | rewrite plusCommutative n 1 in
1077 | lengthMap : (xs : List a) -> length (map f xs) = length xs
1079 | lengthMap (x :: xs) = cong S (lengthMap xs)
1083 | lengthReplicate : (n : Nat) -> length (replicate n x) = n
1084 | lengthReplicate 0 = Refl
1085 | lengthReplicate (S k) = cong S (lengthReplicate k)
1088 | foldlAppend : (f : acc -> a -> acc) -> (init : acc) -> (xs : List a) -> (ys : List a) -> foldl f init (xs ++ ys) = foldl f (foldl f init xs) ys
1089 | foldlAppend f init [] ys = Refl
1090 | foldlAppend f init (x::xs) ys = rewrite foldlAppend f (f init x) xs ys in Refl
1093 | filterAppend : (f : a -> Bool) -> (xs, ys : List a) -> filter f (xs ++ ys) = filter f xs ++ filter f ys
1094 | filterAppend f [] ys = Refl
1095 | filterAppend f (x::xs) ys with (f x)
1096 | _ | False = rewrite filterAppend f xs ys in Refl
1097 | _ | True = rewrite filterAppend f xs ys in Refl
1100 | mapMaybeFusion : (g : b -> Maybe c) -> (f : a -> Maybe b) -> (xs : List a) -> mapMaybe g (mapMaybe f xs) = mapMaybe (f >=> g) xs
1101 | mapMaybeFusion g f [] = Refl
1102 | mapMaybeFusion g f (x::xs) with (f x)
1103 | _ | Nothing = mapMaybeFusion g f xs
1104 | _ | (Just y) with (g y)
1105 | _ | Nothing = mapMaybeFusion g f xs
1106 | _ | (Just z) = rewrite mapMaybeFusion g f xs in Refl
1109 | mapMaybeAppend : (f : a -> Maybe b) -> (xs, ys : List a) -> mapMaybe f (xs ++ ys) = mapMaybe f xs ++ mapMaybe f ys
1110 | mapMaybeAppend f [] ys = Refl
1111 | mapMaybeAppend f (x::xs) ys with (f x)
1112 | _ | Nothing = rewrite mapMaybeAppend f xs ys in Refl
1113 | _ | (Just y) = rewrite mapMaybeAppend f xs ys in Refl
1116 | mapFusion : (g : b -> c) -> (f : a -> b) -> (xs : List a) -> map g (map f xs) = map (g . f) xs
1117 | mapFusion g f [] = Refl
1118 | mapFusion g f (x::xs) = rewrite mapFusion g f xs in Refl
1121 | mapAppend : (f : a -> b) -> (xs, ys : List a) -> map f (xs ++ ys) = map f xs ++ map f ys
1122 | mapAppend f [] ys = Refl
1123 | mapAppend f (x::xs) ys = rewrite mapAppend f xs ys in Refl
1125 | 0 mapTRIsMap : (f : a -> b) -> (as : List a) -> mapTR f as === map f as
1126 | mapTRIsMap f = lemma Lin
1127 | where lemma : (sb : SnocList b)
1129 | -> mapAppend sb f as === (sb <>> map f as)
1131 | lemma sb (x :: xs) = lemma (sb :< f x) xs
1134 | 0 mapMaybeTRIsMapMaybe : (f : a -> Maybe b)
1136 | -> mapMaybeTR f as === mapMaybe f as
1137 | mapMaybeTRIsMapMaybe f = lemma Lin
1138 | where lemma : (sb : SnocList b)
1140 | -> mapMaybeAppend sb f as === (sb <>> mapMaybe f as)
1142 | lemma sb (x :: xs) with (f x)
1143 | lemma sb (x :: xs) | Nothing = lemma sb xs
1144 | lemma sb (x :: xs) | Just v = lemma (sb :< v) xs
1146 | 0 filterTRIsFilter : (f : a -> Bool)
1148 | -> filterTR f as === filter f as
1149 | filterTRIsFilter f = lemma Lin
1151 | where lemma : (sa : SnocList a)
1153 | -> filterAppend sa f as === (sa <>> filter f as)
1155 | lemma sa (x :: xs) with (f x)
1156 | lemma sa (x :: xs) | False = lemma sa xs
1157 | lemma sa (x :: xs) | True = lemma (sa :< x) xs
1159 | 0 replicateTRIsReplicate : (n : Nat) -> (x : a) -> replicateTR [] n x === replicate n x
1160 | replicateTRIsReplicate n x = trans (lemma [] n) (appendNilRightNeutral _)
1161 | where lemma1 : (as : List a) -> (m : Nat) -> (x :: replicate m x) ++ as === replicate m x ++ (x :: as)
1163 | lemma1 as (S k) = cong (x ::) (lemma1 as k)
1165 | lemma : (as : List a) -> (m : Nat) -> replicateTR as m x === replicate m x ++ as
1168 | let prf := lemma (x :: as) k
1169 | in trans prf (sym $
lemma1 as k)