@@ -9,39 +9,39 @@ import Prelude.Nat
99
1010infixr 7 ::
1111
12−data Vect : Type -> Nat -> Type where
13− Nil : Vect a Z
14− (::) : a -> Vect a n -> Vect a (S n)
12+data Vect : Nat -> Type -> Type where
13+ Nil : Vect Z a
14+ (::) : a -> Vect n a -> Vect (S n) a
1515
1616--------------------------------------------------------------------------------
1717-- Indexing into vectors
1818--------------------------------------------------------------------------------
1919
20−tail : Vect a (S n) -> Vect a n
20+tail : Vect (S n) a -> Vect n a
2121tail (x::xs) = xs
2222
23−head : Vect a (S n) -> a
23+head : Vect (S n) a -> a
2424head (x::xs) = x
2525
26−last : Vect a (S n) -> a
26+last : Vect (S n) a -> a
2727last (x::[]) = x
2828last (x::y::ys) = last $ y::ys
2929
30−init : Vect a (S n) -> Vect a n
30+init : Vect (S n) a -> Vect n a
3131init (x::[]) = []
3232init (x::y::ys) = x :: init (y::ys)
3333
34−index : Fin n -> Vect a n -> a
34+index : Fin n -> Vect n a -> a
3535index fZ (x::xs) = x
3636index (fS k) (x::xs) = index k xs
3737index fZ [] impossible
3838
39−deleteAt : Fin (S n) -> Vect a (S n) -> Vect a n
39+deleteAt : Fin (S n) -> Vect (S n) a -> Vect n a
4040deleteAt fZ (x::xs) = xs
4141deleteAt {n = S m} (fS k) (x::xs) = x :: deleteAt k xs
4242deleteAt _ [] impossible
4343
44−replaceAt : Fin n -> t -> Vect t n -> Vect t n
44+replaceAt : Fin n -> t -> Vect n t -> Vect n t
4545replaceAt fZ y (x::xs) = y::xs
4646replaceAt (fS k) y (x::xs) = x :: replaceAt k y xs
4747
@@ -49,13 +49,13 @@ replaceAt (fS k) y (x::xs) = x :: replaceAt k y xs
4949-- Subvectors
5050--------------------------------------------------------------------------------
5151
52−take : Fin n -> Vect a n -> (p ** Vect a p)
52+take : Fin n -> Vect n a -> (p ** Vect p a)
5353take fZ xs = (_ ** [])
5454take (fS k) [] impossible
5555take (fS k) (x::xs) with (take k xs)
5656 | (_ ** tail) = (_ ** x::tail)
5757
58−drop : Fin n -> Vect a n -> (p ** Vect a p)
58+drop : Fin n -> Vect n a -> (p ** Vect p a)
5959drop fZ xs = (_ ** xs)
6060drop (fS k) [] impossible
6161drop (fS k) (x::xs) = drop k xs
@@ -64,11 +64,11 @@ drop (fS k) (x::xs) = drop k xs
6464-- Conversions to and from list
6565--------------------------------------------------------------------------------
6666
67−toList : Vect a n -> List a
67+toList : Vect n a -> List a
6868toList [] = []
6969toList (x::xs) = x :: toList xs
7070
71−fromList : (l : List a) -> Vect a (length l)
71+fromList : (l : List a) -> Vect (length l) a
7272fromList [] = []
7373fromList (x::xs) = x :: fromList xs
7474
@@ -76,11 +76,11 @@ fromList (x::xs) = x :: fromList xs
7676-- Building (bigger) vectors
7777--------------------------------------------------------------------------------
7878
79−(++) : Vect a m -> Vect a n -> Vect a (m + n)
79+(++) : Vect m a -> Vect n a -> Vect (m + n) a
8080(++) [] ys = ys
8181(++) (x::xs) ys = x :: xs ++ ys
8282
83−replicate : (n : Nat) -> a -> Vect a n
83+replicate : (n : Nat) -> a -> Vect n a
8484replicate Z x = []
8585replicate (S k) x = x :: replicate k x
8686
@@ -88,14 +88,14 @@ replicate (S k) x = x :: replicate k x
8888-- Zips and unzips
8989--------------------------------------------------------------------------------
9090
91−zipWith : (a -> b -> c) -> Vect a n -> Vect b n -> Vect c n
91+zipWith : (a -> b -> c) -> Vect n a -> Vect n b -> Vect n c
9292zipWith f [] [] = []
9393zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
9494
95−zip : Vect a n -> Vect b n -> Vect (a, b) n
95+zip : Vect n a -> Vect n b -> Vect n (a, b)
9696zip = zipWith (\x => \y => (x,y))
9797
98−unzip : Vect (a, b) n -> (Vect a n, Vect b n)
98+unzip : Vect n (a, b) -> (Vect n a, Vect n b)
9999unzip [] = ([], [])
100100unzip ((l, r)::xs) with (unzip xs)
101101 | (lefts, rights) = (l::lefts, r::rights)
@@ -104,12 +104,12 @@ unzip ((l, r)::xs) with (unzip xs)
104104-- Maps
105105--------------------------------------------------------------------------------
106106
107−map : (a -> b) -> Vect a n -> Vect b n
107+map : (a -> b) -> Vect n a -> Vect n b
108108map f [] = []
109109map f (x::xs) = f x :: map f xs
110110
111111-- XXX: causes Idris to enter an infinite loop when type checking in the REPL
112−--mapMaybe : (a -> Maybe b) -> Vect a n -> (p ** Vect b p)
112+--mapMaybe : (a -> Maybe b) -> Vect n a -> (p ** Vect b p)
113113--mapMaybe f [] = (_ ** [])
114114--mapMaybe f (x::xs) = mapMaybe' (f x)
115115-- XXX: working around the type restrictions on case statements
@@ -122,11 +122,11 @@ map f (x::xs) = f x :: map f xs
122122-- Folds
123123--------------------------------------------------------------------------------
124124
125−total foldl : (a -> b -> a) -> a -> Vect b m -> a
125+total foldl : (a -> b -> a) -> a -> Vect m b -> a
126126foldl f e [] = e
127127foldl f e (x::xs) = foldl f (f e x) xs
128128
129−total foldr : (a -> b -> b) -> b -> Vect a m -> b
129+total foldr : (a -> b -> b) -> b -> Vect m a -> b
130130foldr f e [] = e
131131foldr f e (x::xs) = f x (foldr f e xs)
132132
@@ -134,39 +134,39 @@ foldr f e (x::xs) = f x (foldr f e xs)
134134-- Special folds
135135--------------------------------------------------------------------------------
136136
137−concat : Vect (Vect a n) m -> Vect a (m * n)
137+concat : Vect m (Vect n a) -> Vect (m * n) a
138138concat [] = []
139139concat (v::vs) = v ++ concat vs
140140
141−total and : Vect Bool m -> Bool
141+total and : Vect m Bool -> Bool
142142and = foldr (&&) True
143143
144−total or : Vect Bool m -> Bool
144+total or : Vect m Bool -> Bool
145145or = foldr (||) False
146146
147−total any : (a -> Bool) -> Vect a m -> Bool
148−any p = Vect.or . map p
147+total any : (a -> Bool) -> Vect m a -> Bool
148+any p = Vect.or . Vect.map p
149149
150−total all : (a -> Bool) -> Vect a m -> Bool
151−all p = Vect.and . map p
150+total all : (a -> Bool) -> Vect m a -> Bool
151+all p = Vect.and . Vect.map p
152152
153153--------------------------------------------------------------------------------
154154-- Transformations
155155--------------------------------------------------------------------------------
156156
157−total reverse : Vect a n -> Vect a n
157+total reverse : Vect n a -> Vect n a
158158reverse = reverse' []
159159 where
160− total reverse' : Vect a m -> Vect a n -> Vect a (m + n)
160+ total reverse' : Vect m a -> Vect n a -> Vect (m + n) a
161161 reverse' acc [] ?= acc
162162 reverse' acc (x::xs) ?= reverse' (x::acc) xs
163163
164−total intersperse' : a -> Vect a m -> (p ** Vect a p)
164+total intersperse' : a -> Vect m a -> (p ** Vect p a)
165165intersperse' sep [] = (_ ** [])
166166intersperse' sep (y::ys) with (intersperse' sep ys)
167167 | (_ ** tail) = (_ ** sep::y::tail)
168168
169−total intersperse : a -> Vect a m -> (p ** Vect a p)
169+total intersperse : a -> Vect m a -> (p ** Vect p a)
170170intersperse sep [] = (_ ** [])
171171intersperse sep (x::xs) with (intersperse' sep xs)
172172 | (_ ** tail) = (_ ** x::tail)
@@ -175,56 +175,56 @@ intersperse sep (x::xs) with (intersperse' sep xs)
175175-- Membership tests
176176--------------------------------------------------------------------------------
177177
178−elemBy : (a -> a -> Bool) -> a -> Vect a n -> Bool
178+elemBy : (a -> a -> Bool) -> a -> Vect n a -> Bool
179179elemBy p e [] = False
180180elemBy p e (x::xs) with (p e x)
181181 | True = True
182182 | False = elemBy p e xs
183183
184−elem : Eq a => a -> Vect a n -> Bool
184+elem : Eq a => a -> Vect n a -> Bool
185185elem = elemBy (==)
186186
187−lookupBy : (a -> a -> Bool) -> a -> Vect (a, b) n -> Maybe b
187+lookupBy : (a -> a -> Bool) -> a -> Vect n (a, b) -> Maybe b
188188lookupBy p e [] = Nothing
189189lookupBy p e ((l, r)::xs) with (p e l)
190190 | True = Just r
191191 | False = lookupBy p e xs
192192
193−lookup : Eq a => a -> Vect (a, b) n -> Maybe b
193+lookup : Eq a => a -> Vect n (a, b) -> Maybe b
194194lookup = lookupBy (==)
195195
196−hasAnyBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
196+hasAnyBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
197197hasAnyBy p elems [] = False
198198hasAnyBy p elems (x::xs) with (elemBy p x elems)
199199 | True = True
200200 | False = hasAnyBy p elems xs
201201
202−hasAny : Eq a => Vect a m -> Vect a n -> Bool
202+hasAny : Eq a => Vect m a -> Vect n a -> Bool
203203hasAny = hasAnyBy (==)
204204
205205--------------------------------------------------------------------------------
206206-- Searching with a predicate
207207--------------------------------------------------------------------------------
208208
209−find : (a -> Bool) -> Vect a n -> Maybe a
209+find : (a -> Bool) -> Vect n a -> Maybe a
210210find p [] = Nothing
211211find p (x::xs) with (p x)
212212 | True = Just x
213213 | False = find p xs
214214
215−findIndex : (a -> Bool) -> Vect a n -> Maybe Nat
215+findIndex : (a -> Bool) -> Vect n a -> Maybe Nat
216216findIndex = findIndex' 0
217217 where
218− findIndex' : Nat -> (a -> Bool) -> Vect a n -> Maybe Nat
218+ findIndex' : Nat -> (a -> Bool) -> Vect n a -> Maybe Nat
219219 findIndex' cnt p [] = Nothing
220220 findIndex' cnt p (x::xs) with (p x)
221221 | True = Just cnt
222222 | False = findIndex' (S cnt) p xs
223223
224−total findIndices : (a -> Bool) -> Vect a m -> (p ** Vect Nat p)
224+total findIndices : (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
225225findIndices = findIndices' 0
226226 where
227− total findIndices' : Nat -> (a -> Bool) -> Vect a m -> (p ** Vect Nat p)
227+ total findIndices' : Nat -> (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
228228 findIndices' cnt p [] = (_ ** [])
229229 findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
230230 | (_ ** tail) =
@@ -233,23 +233,23 @@ findIndices = findIndices' 0
233233 else
234234 (_ ** tail)
235235
236−elemIndexBy : (a -> a -> Bool) -> a -> Vect a m -> Maybe Nat
236+elemIndexBy : (a -> a -> Bool) -> a -> Vect m a -> Maybe Nat
237237elemIndexBy p e = findIndex $ p e
238238
239−elemIndex : Eq a => a -> Vect a m -> Maybe Nat
239+elemIndex : Eq a => a -> Vect m a -> Maybe Nat
240240elemIndex = elemIndexBy (==)
241241
242−total elemIndicesBy : (a -> a -> Bool) -> a -> Vect a m -> (p ** Vect Nat p)
242+total elemIndicesBy : (a -> a -> Bool) -> a -> Vect m a -> (p ** Vect p Nat)
243243elemIndicesBy p e = findIndices $ p e
244244
245−total elemIndices : Eq a => a -> Vect a m -> (p ** Vect Nat p)
245+total elemIndices : Eq a => a -> Vect m a -> (p ** Vect p Nat)
246246elemIndices = elemIndicesBy (==)
247247
248248--------------------------------------------------------------------------------
249249-- Filters
250250--------------------------------------------------------------------------------
251251
252−total filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
252+total filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
253253filter p [] = ( _ ** [] )
254254filter p (x::xs) with (filter p xs)
255255 | (_ ** tail) =
@@ -258,17 +258,17 @@ filter p (x::xs) with (filter p xs)
258258 else
259259 (_ ** tail)
260260
261−nubBy : (a -> a -> Bool) -> Vect a n -> (p ** Vect a p)
261+nubBy : (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
262262nubBy = nubBy' []
263263 where
264− nubBy' : Vect a m -> (a -> a -> Bool) -> Vect a n -> (p ** Vect a p)
264+ nubBy' : Vect m a -> (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
265265 nubBy' acc p [] = (_ ** [])
266266 nubBy' acc p (x::xs) with (elemBy p x acc)
267267 | True = nubBy' acc p xs
268268 | False with (nubBy' (x::acc) p xs)
269269 | (_ ** tail) = (_ ** x::tail)
270270
271−nub : Eq a => Vect a n -> (p ** Vect a p)
271+nub : Eq a => Vect n a -> (p ** Vect p a)
272272nub = nubBy (==)
273273
274274--------------------------------------------------------------------------------
@@ -279,31 +279,31 @@ nub = nubBy (==)
279279-- Predicates
280280--------------------------------------------------------------------------------
281281
282−isPrefixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
282+isPrefixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
283283isPrefixOfBy p [] right = True
284284isPrefixOfBy p left [] = False
285285isPrefixOfBy p (x::xs) (y::ys) with (p x y)
286286 | True = isPrefixOfBy p xs ys
287287 | False = False
288288
289−isPrefixOf : Eq a => Vect a m -> Vect a n -> Bool
289+isPrefixOf : Eq a => Vect m a -> Vect n a -> Bool
290290isPrefixOf = isPrefixOfBy (==)
291291
292−isSuffixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
292+isSuffixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
293293isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
294294
295−isSuffixOf : Eq a => Vect a m -> Vect a n -> Bool
295+isSuffixOf : Eq a => Vect m a -> Vect n a -> Bool
296296isSuffixOf = isSuffixOfBy (==)
297297
298298--------------------------------------------------------------------------------
299299-- Conversions
300300--------------------------------------------------------------------------------
301301
302−total maybeToVect : Maybe a -> (p ** Vect a p)
302+total maybeToVect : Maybe a -> (p ** Vect p a)
303303maybeToVect Nothing = (_ ** [])
304304maybeToVect (Just j) = (_ ** [j])
305305
306−total vectToMaybe : Vect a n -> Maybe a
306+total vectToMaybe : Vect n a -> Maybe a
307307vectToMaybe [] = Nothing
308308vectToMaybe (x::xs) = Just x
309309
@@ -311,17 +311,17 @@ vectToMaybe (x::xs) = Just x
311311-- Misc
312312--------------------------------------------------------------------------------
313313
314−catMaybes : Vect (Maybe a) n -> (p ** Vect a p)
314+catMaybes : Vect n (Maybe a) -> (p ** Vect p a)
315315catMaybes [] = (_ ** [])
316316catMaybes (Nothing::xs) = catMaybes xs
317317catMaybes ((Just j)::xs) with (catMaybes xs)
318318 | (_ ** tail) = (_ ** j::tail)
319319
320−range : Vect (Fin n) n
320+range : Vect n (Fin n)
321321range =
322322 reverse range_
323323 where
324− range_ : Vect (Fin n) n
324+ range_ : Vect n (Fin n)
325325 range_ {n=Z} = Nil
326326 range_ {n=(S _)} = last :: map weaken range_
327327