@@ -1,56 +1,279 @@
1module prelude.vect
1module prelude.vect
4import prelude.fin
3import prelude.fin
6%access public
7%access public
8infixr 10 ::
9infixr 10 ::
10data Vect : Set -> Nat -> Set where
11data Vect : Set -> Nat -> Set where
11− Nil : Vect a 0
12+ Nil : Vect a O
12− (::) : a -> Vect a k -> Vect a (1 + k)
13+ (::) : a -> Vect a n -> Vect a (S n)
15+--------------------------------------------------------------------------------
16+-- Indexing into vectors
17+--------------------------------------------------------------------------------
14tail : Vect a (S n) -> Vect a n
19tail : Vect a (S n) -> Vect a n
15−tail (x :: xs) = xs
20+tail (x::xs) = xs
22+head : Vect a (S n) -> a
25+last : Vect a (S n) -> a
27+last (x::y::ys) = last $ y::ys
29+init : Vect a (S n) -> Vect a n
31+init (x::y::ys) = x :: init (y::ys)
17−lookup : Fin n -> Vect a n -> a
33+index : Fin n -> Vect a n -> a
18−lookup fO (x :: xs) = x
34+index fO (x::xs) = x
19−lookup (fS k) (x :: xs) = lookup k xs
35+index (fS k) (x::xs) = index k xs
20−lookup fO [] impossible
36+index fO [] impossible
21−lookup (fS _) [] impossible
37+index (fS _) [] impossible
39+--------------------------------------------------------------------------------
40+-- Conversions to and from list
41+--------------------------------------------------------------------------------
43+total toList : Vect a n -> List a
45+toList (x::xs) = x :: toList xs
47+total fromList : (l : List a) -> Vect a (length l)
49+fromList (x::xs) = x :: fromList xs
51+--------------------------------------------------------------------------------
52+-- Building bigger vectors
53+--------------------------------------------------------------------------------
23−(++) : Vect a n -> Vect a m -> Vect a (n + m)
55+(++) : Vect a m -> Vect a n -> Vect a (m + n)
24−(++) [] ys = ys
56+(++) [] ys = ys
25−(++) (x :: xs) ys = x :: xs ++ ys
57+(++) (x::xs) ys = x :: xs ++ ys
59+--------------------------------------------------------------------------------
61+--------------------------------------------------------------------------------
63+total map : (a -> b) -> Vect a n -> Vect b n
65+map f (x::xs) = f x :: map f xs
67+--------------------------------------------------------------------------------
69+--------------------------------------------------------------------------------
71+total foldl : (a -> b -> a) -> a -> Vect b m -> a
73+foldl f e (x::xs) = foldl f (f e x) xs
75+total foldr : (a -> b -> b) -> b -> Vect a m -> b
77+foldr f e (x::xs) = f x (foldr f e xs)
79+--------------------------------------------------------------------------------
81+--------------------------------------------------------------------------------
83+total and : Vect Bool m -> Bool
86+total or : Vect Bool m -> Bool
89+total any : (a -> Bool) -> Vect a m -> Bool
92+total all : (a -> Bool) -> Vect a m -> Bool
95+--------------------------------------------------------------------------------
97+--------------------------------------------------------------------------------
99+total reverse : Vect a n -> Vect a n
100+reverse = reverse' []
102+ total reverse' : Vect a m -> Vect a n -> Vect a (m + n)
103+ reverse' acc [] ?= acc
104+ reverse' acc (x::xs) ?= reverse' (x::acc) xs
106+total intersperse' : a -> Vect a m -> (p ** Vect a p)
107+intersperse' sep [] = (_ ** [])
108+intersperse' sep (y::ys) with (intersperse' sep ys)
109+ | (_ ** tail) = (_ ** sep::y::tail)
111+total intersperse : a -> Vect a m -> (p ** Vect a p)
112+intersperse sep [] = (_ ** [])
113+intersperse sep (x::xs) with (intersperse' sep xs)
114+ | (_ ** tail) = (_ ** x::tail)
116+--------------------------------------------------------------------------------
118+--------------------------------------------------------------------------------
120+elemBy : (a -> a -> Bool) -> a -> Vect a n -> Bool
121+elemBy p e [] = False
122+elemBy p e (x::xs) with (p e x)
124+ | False = elemBy p e xs
126+elem : Eq a => a -> Vect a n -> Bool
129+lookupBy : (a -> a -> Bool) -> a -> Vect (a, b) n -> Maybe b
130+lookupBy p e [] = Nothing
131+lookupBy p e ((l, r)::xs) with (p e l)
133+ | False = lookupBy p e xs
135+lookup : Eq a => a -> Vect (a, b) n -> Maybe b
136+lookup = lookupBy (==)
138+hasAnyBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
139+hasAnyBy p elems [] = False
140+hasAnyBy p elems (x::xs) with (elemBy p x elems)
142+ | False = hasAnyBy p elems xs
144+hasAny : Eq a => Vect a m -> Vect a n -> Bool
145+hasAny = hasAnyBy (==)
147+--------------------------------------------------------------------------------
148+-- Searching with a predicate
149+--------------------------------------------------------------------------------
151+find : (a -> Bool) -> Vect a n -> Maybe a
153+find p (x::xs) with (p x)
157+findIndex : (a -> Bool) -> Vect a n -> Maybe Nat
158+findIndex = findIndex' 0
160+ findIndex' : Nat -> (a -> Bool) -> Vect a n -> Maybe Nat
161+ findIndex' cnt p [] = Nothing
162+ findIndex' cnt p (x::xs) with (p x)
164+ | False = findIndex' (S cnt) p xs
166+total findIndices : (a -> Bool) -> Vect a m -> (p ** Vect Nat p)
167+findIndices = findIndices' 0
169+ total findIndices' : Nat -> (a -> Bool) -> Vect a m -> (p ** Vect Nat p)
170+ findIndices' cnt p [] = (_ ** [])
171+ findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
178+elemIndexBy : (a -> a -> Bool) -> a -> Vect a m -> Maybe Nat
179+elemIndexBy p e = findIndex $ p e
181+elemIndex : Eq a => a -> Vect a m -> Maybe Nat
182+elemIndex = elemIndexBy (==)
184+total elemIndicesBy : (a -> a -> Bool) -> a -> Vect a m -> (p ** Vect Nat p)
185+elemIndicesBy p e = findIndices $ p e
187+total elemIndices : Eq a => a -> Vect a m -> (p ** Vect Nat p)
188+elemIndices = elemIndicesBy (==)
190+--------------------------------------------------------------------------------
192+--------------------------------------------------------------------------------
27−filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
194+total filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
28filter p [] = ( _ ** [] )
195filter p [] = ( _ ** [] )
29−filter p (x :: xs)
196+filter p (x::xs) with (filter p xs)
30− = let (_ ** xs') = filter p xs in
197+ | (_ ** tail) =
31− if (p x) then ( _ ** x :: xs' ) else ( _ ** xs' )
198+ if p x then
203+nubBy : (a -> a -> Bool) -> Vect a n -> (p ** Vect a p)
206+ nubBy' : Vect a m -> (a -> a -> Bool) -> Vect a n -> (p ** Vect a p)
207+ nubBy' acc p [] = (_ ** [])
208+ nubBy' acc p (x::xs) with (elemBy p x acc)
209+ | True = nubBy' acc p xs
210+ | False with (nubBy' (x::acc) p xs)
211+ | (_ ** tail) = (_ ** x::tail)
213+nub : Eq a => Vect a n -> (p ** Vect a p)
216+--------------------------------------------------------------------------------
217+-- Splitting and breaking lists
218+--------------------------------------------------------------------------------
222+--------------------------------------------------------------------------------
224+--------------------------------------------------------------------------------
226+isPrefixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
227+isPrefixOfBy p [] right = True
228+isPrefixOfBy p left [] = False
229+isPrefixOfBy p (x::xs) (y::ys) with (p x y)
230+ | True = isPrefixOfBy p xs ys
233+isPrefixOf : Eq a => Vect a m -> Vect a n -> Bool
234+isPrefixOf = isPrefixOfBy (==)
236+isSuffixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
237+isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
239+isSuffixOf : Eq a => Vect a m -> Vect a n -> Bool
240+isSuffixOf = isSuffixOfBy (==)
242+--------------------------------------------------------------------------------
244+--------------------------------------------------------------------------------
246+total maybeToVect : Maybe a -> (p ** Vect a p)
247+maybeToVect Nothing = (_ ** [])
248+maybeToVect (Just j) = (_ ** [j])
250+total vectToMaybe : Vect a n -> Maybe a
251+vectToMaybe [] = Nothing
252+vectToMaybe (x::xs) = Just x
33−map : (a -> b) -> Vect a n -> Vect b n
254+--------------------------------------------------------------------------------
34−map f [] = []
255+-- Misc
35−map f (x :: xs) = f x :: map f xs
256+--------------------------------------------------------------------------------
37−reverse : Vect a n -> Vect a n
258+catMaybes : Vect (Maybe a) n -> (p ** Vect a p)
38−reverse xs = revAcc [] xs where
259+catMaybes [] = (_ ** [])
39− revAcc : Vect a n -> Vect a m -> Vect a (n + m)
260+catMaybes (Nothing::xs) = catMaybes xs
40− revAcc acc [] ?= acc
261+catMaybes ((Just j)::xs) with (catMaybes xs)
41− revAcc acc (x :: xs) ?= revAcc (x :: acc) xs
262+ | (_ ** tail) = (_ ** j::tail)
264+--------------------------------------------------------------------------------
43−---------- Proofs ----------
265+-- Proofs
266+--------------------------------------------------------------------------------
45−revAcc_lemma_2 = proof {
268+prelude.vect.reverse'_lemma_2 = proof {
47− rewrite plusSuccRightSucc n k;
270+ rewrite (plusSuccRightSucc m n1);
48 exact value;
271 exact value;
51−revAcc_lemma_1 = proof {
274+prelude.vect.reverse'_lemma_1 = proof {
53− rewrite sym (plusZeroRightNeutral n);
276+ rewrite sym (plusZeroRightNeutral m);
54 exact value;
277 exact value;