@@ -2,6 +2,7 @@ module prelude.list
3import builtins
3import builtins
5import prelude.maybe
6import prelude.maybe
6import prelude.nat
7import prelude.nat
@@ -107,6 +108,72 @@ length (x::xs) = 1 + length xs
107(++) [] right = right
108(++) [] right = right
108(++) (x::xs) right = x :: (xs ++ right)
109(++) (x::xs) right = x :: (xs ++ right)
111+--------------------------------------------------------------------------------
113+--------------------------------------------------------------------------------
115+instance (Eq a) => Eq (List a) where
117+ (==) (x::xs) (y::ys) =
125+instance Ord a => Ord (List a) where
129+ compare (x::xs) (y::ys) =
135+instance Semigroup (List a) where
138+instance Monoid (List a) where
141+-- XXX: unification failure
142+-- instance VerifiedSemigroup (List a) where
143+-- semigroupOpIsAssociative = appendAssociative
145+--------------------------------------------------------------------------------
147+--------------------------------------------------------------------------------
149+zipWith : (f : a -> b -> c) -> (l : List a) -> (r : List b) ->
150+ (length l = length r) -> List c
151+zipWith f [] [] p = []
152+zipWith f (x::xs) (y::ys) p = f x y :: (zipWith f xs ys ?zipWithTailProof)
154+zipWith3 : (f : a -> b -> c -> d) -> (x : List a) -> (y : List b) ->
155+ (z : List c) -> (length x = length y) -> (length y = length z) -> List d
156+zipWith3 f [] [] [] p q = []
157+zipWith3 f (x::xs) (y::ys) (z::zs) p q =
158+ f x y z :: (zipWith3 f xs ys zs ?zipWith3TailProof ?zipWith3TailProof')
160+zip : (l : List a) -> (r : List b) -> (length l = length r) -> List (a, b)
161+zip = zipWith (\x => \y => (x, y))
163+zip3 : (x : List a) -> (y : List b) -> (z : List c) -> (length x = length y) ->
164+ (length y = length z) -> List (a, b, c)
165+zip3 = zipWith3 (\x => \y => \z => (x, y, z))
167+unzip : List (a, b) -> (List a, List b)
169+unzip ((l, r)::xs) with (unzip xs)
170+ | (lefts, rights) = (l::lefts, r::rights)
172+unzip3 : List (a, b, c) -> (List a, List b, List c)
173+unzip3 [] = ([], [], [])
174+unzip3 ((l, c, r)::xs) with (unzip3 xs)
175+ | (lefts, centres, rights) = (l::lefts, c::centres, r::rights)
110--------------------------------------------------------------------------------
177--------------------------------------------------------------------------------
112--------------------------------------------------------------------------------
179--------------------------------------------------------------------------------
@@ -138,8 +205,12 @@ foldr f e (x::xs) = f x (foldr f e xs)
138-- Special folds
205-- Special folds
139--------------------------------------------------------------------------------
206--------------------------------------------------------------------------------
208+mconcat : Monoid a => List a -> a
209+mconcat = foldr (<+>) neutral
141concat : List (List a) -> List a
211concat : List (List a) -> List a
142−concat = foldr (++) []
212+concat [] = []
213+concat (x::xs) = x ++ concat xs
144concatMap : (a -> List b) -> List a -> List b
215concatMap : (a -> List b) -> List a -> List b
145concatMap f [] = []
216concatMap f [] = []
@@ -403,31 +474,33 @@ catMaybes (x::xs) =
403 Just j => j :: catMaybes xs
474 Just j => j :: catMaybes xs
405--------------------------------------------------------------------------------
476--------------------------------------------------------------------------------
406−-- Instances
477+-- Properties
407--------------------------------------------------------------------------------
478--------------------------------------------------------------------------------
409−instance (Eq a) => Eq (List a) where
480+-- append
410− (==) [] [] = True
481+appendNilRightNeutral : (l : List a) ->
411− (==) (a::restA) (b::restB) =
482+ l ++ [] = l
412− if a == b
483+appendNilRightNeutral [] = refl
413− then restA == restB
484+appendNilRightNeutral (x::xs) =
414− else False
485+ let inductiveHypothesis = appendNilRightNeutral xs in
415− (==) _ _ = False
486+ ?appendNilRightNeutralStepCase
418−instance Ord a => Ord (List a) where
488+appendAssociative : (l : List a) -> (c : List a) -> (r : List a) ->
419− compare [] [] = EQ
489+ l ++ (c ++ r) = (l ++ c) ++ r
420− compare [] _ = LT
490+appendAssociative [] c r = refl
421− compare _ [] = GT
491+appendAssociative (x::xs) c r =
422− compare (a::restA) (b::restB) =
492+ let inductiveHypothesis = appendAssociative xs c r in
423− if a /= b
493+ ?appendAssociativeStepCase
425− else compare restA restB
427−--------------------------------------------------------------------------------
495+-- length
428−-- Properties
496+lengthAppend : (left : List a) -> (right : List a) ->
429−--------------------------------------------------------------------------------
497+ length (left ++ right) = length left + length right
498+lengthAppend [] right = refl
499+lengthAppend (x::xs) right =
500+ let inductiveHypothesis = lengthAppend xs right in
501+ ?lengthAppendStepCase
431mapPreservesLength : (f : a -> b) -> (l : List a) ->
504mapPreservesLength : (f : a -> b) -> (l : List a) ->
432 length (map f l) = length l
505 length (map f l) = length l
433mapPreservesLength f [] = refl
506mapPreservesLength f [] = refl
@@ -449,20 +522,7 @@ mapFusion f g (x::xs) =
449 let inductiveHypothesis = mapFusion f g xs in
522 let inductiveHypothesis = mapFusion f g xs in
450 ?mapFusionStepCase
523 ?mapFusionStepCase
452−appendNilRightNeutral : (l : List a) ->
525+-- hasAny
454−appendNilRightNeutral [] = refl
455−appendNilRightNeutral (x::xs) =
456− let inductiveHypothesis = appendNilRightNeutral xs in
457− ?appendNilRightNeutralStepCase
459−appendAssociative : (l : List a) -> (c : List a) -> (r : List a) ->
460− (l ++ c) ++ r = l ++ (c ++ r)
461−appendAssociative [] c r = refl
462−appendAssociative (x::xs) c r =
463− let inductiveHypothesis = appendAssociative xs c r in
464− ?appendAssociativeStepCase
466hasAnyByNilFalse : (p : a -> a -> Bool) -> (l : List a) ->
526hasAnyByNilFalse : (p : a -> a -> Bool) -> (l : List a) ->
467 hasAnyBy p [] l = False
527 hasAnyBy p [] l = False
468hasAnyByNilFalse p [] = refl
528hasAnyByNilFalse p [] = refl
@@ -470,16 +530,9 @@ hasAnyByNilFalse p (x::xs) =
470 let inductiveHypothesis = hasAnyByNilFalse p xs in
530 let inductiveHypothesis = hasAnyByNilFalse p xs in
471 ?hasAnyByNilFalseStepCase
531 ?hasAnyByNilFalseStepCase
473−lengthAppend : (left : List a) -> (right : List a) ->
474− length (left ++ right) = length left + length right
475−lengthAppend [] right = refl
476−lengthAppend (x::xs) right =
477− let inductiveHypothesis = lengthAppend xs right in
478− ?lengthAppendStepCase
480hasAnyNilFalse : Eq a => (l : List a) -> hasAny [] l = False
533hasAnyNilFalse : Eq a => (l : List a) -> hasAny [] l = False
481hasAnyNilFalse l = ?hasAnyNilFalseBody
534hasAnyNilFalse l = ?hasAnyNilFalseBody
483--------------------------------------------------------------------------------
536--------------------------------------------------------------------------------
485--------------------------------------------------------------------------------
538--------------------------------------------------------------------------------
@@ -542,3 +595,21 @@ mapPreservesLengthStepCase = proof {
598+zipWithTailProof = proof {
600+ rewrite (succInjective (length xs) (length ys) p);
604+zipWith3TailProof = proof {
606+ rewrite (succInjective (length xs) (length ys) p);
610+zipWith3TailProof' = proof {
612+ rewrite (succInjective (length ys) (length zs) q);