@@ -9,39 +9,39 @@ import Prelude.Nat
10infixr 7 ::
10infixr 7 ::
12−data Vect : Type -> Nat -> Type where
12+data Vect : Nat -> Type -> Type where
13− Nil : Vect a Z
13+ Nil : Vect Z a
14− (::) : a -> Vect a n -> Vect a (S n)
14+ (::) : a -> Vect n a -> Vect (S n) a
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17-- Indexing into vectors
17-- Indexing into vectors
18--------------------------------------------------------------------------------
18--------------------------------------------------------------------------------
20−tail : Vect a (S n) -> Vect a n
20+tail : Vect (S n) a -> Vect n a
21tail (x::xs) = xs
21tail (x::xs) = xs
23−head : Vect a (S n) -> a
23+head : Vect (S n) a -> a
24head (x::xs) = x
24head (x::xs) = x
26−last : Vect a (S n) -> a
26+last : Vect (S n) a -> a
27last (x::[]) = x
27last (x::[]) = x
28last (x::y::ys) = last $ y::ys
28last (x::y::ys) = last $ y::ys
30−init : Vect a (S n) -> Vect a n
30+init : Vect (S n) a -> Vect n a
31init (x::[]) = []
31init (x::[]) = []
32init (x::y::ys) = x :: init (y::ys)
32init (x::y::ys) = x :: init (y::ys)
34−index : Fin n -> Vect a n -> a
34+index : Fin n -> Vect n a -> a
35index fZ (x::xs) = x
35index fZ (x::xs) = x
36index (fS k) (x::xs) = index k xs
36index (fS k) (x::xs) = index k xs
37index fZ [] impossible
37index fZ [] impossible
39−deleteAt : Fin (S n) -> Vect a (S n) -> Vect a n
39+deleteAt : Fin (S n) -> Vect (S n) a -> Vect n a
40deleteAt fZ (x::xs) = xs
40deleteAt fZ (x::xs) = xs
41deleteAt {n = S m} (fS k) (x::xs) = x :: deleteAt k xs
41deleteAt {n = S m} (fS k) (x::xs) = x :: deleteAt k xs
42deleteAt _ [] impossible
42deleteAt _ [] impossible
44−replaceAt : Fin n -> t -> Vect t n -> Vect t n
44+replaceAt : Fin n -> t -> Vect n t -> Vect n t
45replaceAt fZ y (x::xs) = y::xs
45replaceAt fZ y (x::xs) = y::xs
46replaceAt (fS k) y (x::xs) = x :: replaceAt k y xs
46replaceAt (fS k) y (x::xs) = x :: replaceAt k y xs
@@ -49,13 +49,13 @@ replaceAt (fS k) y (x::xs) = x :: replaceAt k y xs
49-- Subvectors
49-- Subvectors
50--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
52−take : Fin n -> Vect a n -> (p ** Vect a p)
52+take : Fin n -> Vect n a -> (p ** Vect p a)
53take fZ xs = (_ ** [])
53take fZ xs = (_ ** [])
54take (fS k) [] impossible
54take (fS k) [] impossible
55take (fS k) (x::xs) with (take k xs)
55take (fS k) (x::xs) with (take k xs)
56 | (_ ** tail) = (_ ** x::tail)
56 | (_ ** tail) = (_ ** x::tail)
58−drop : Fin n -> Vect a n -> (p ** Vect a p)
58+drop : Fin n -> Vect n a -> (p ** Vect p a)
59drop fZ xs = (_ ** xs)
59drop fZ xs = (_ ** xs)
60drop (fS k) [] impossible
60drop (fS k) [] impossible
61drop (fS k) (x::xs) = drop k xs
61drop (fS k) (x::xs) = drop k xs
@@ -64,11 +64,11 @@ drop (fS k) (x::xs) = drop k xs
64-- Conversions to and from list
64-- Conversions to and from list
65--------------------------------------------------------------------------------
65--------------------------------------------------------------------------------
67−toList : Vect a n -> List a
67+toList : Vect n a -> List a
68toList [] = []
68toList [] = []
69toList (x::xs) = x :: toList xs
69toList (x::xs) = x :: toList xs
71−fromList : (l : List a) -> Vect a (length l)
71+fromList : (l : List a) -> Vect (length l) a
72fromList [] = []
72fromList [] = []
73fromList (x::xs) = x :: fromList xs
73fromList (x::xs) = x :: fromList xs
@@ -76,11 +76,11 @@ fromList (x::xs) = x :: fromList xs
76-- Building (bigger) vectors
76-- Building (bigger) vectors
77--------------------------------------------------------------------------------
77--------------------------------------------------------------------------------
79−(++) : Vect a m -> Vect a n -> Vect a (m + n)
79+(++) : Vect m a -> Vect n a -> Vect (m + n) a
80(++) [] ys = ys
80(++) [] ys = ys
81(++) (x::xs) ys = x :: xs ++ ys
81(++) (x::xs) ys = x :: xs ++ ys
83−replicate : (n : Nat) -> a -> Vect a n
83+replicate : (n : Nat) -> a -> Vect n a
84replicate Z x = []
84replicate Z x = []
85replicate (S k) x = x :: replicate k x
85replicate (S k) x = x :: replicate k x
@@ -88,14 +88,14 @@ replicate (S k) x = x :: replicate k x
88-- Zips and unzips
88-- Zips and unzips
89--------------------------------------------------------------------------------
89--------------------------------------------------------------------------------
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
92zipWith f [] [] = []
92zipWith f [] [] = []
93zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
93zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
95−zip : Vect a n -> Vect b n -> Vect (a, b) n
95+zip : Vect n a -> Vect n b -> Vect n (a, b)
96zip = zipWith (\x => \y => (x,y))
96zip = zipWith (\x => \y => (x,y))
98−unzip : Vect (a, b) n -> (Vect a n, Vect b n)
98+unzip : Vect n (a, b) -> (Vect n a, Vect n b)
99unzip [] = ([], [])
99unzip [] = ([], [])
100unzip ((l, r)::xs) with (unzip xs)
100unzip ((l, r)::xs) with (unzip xs)
101 | (lefts, rights) = (l::lefts, r::rights)
101 | (lefts, rights) = (l::lefts, r::rights)
@@ -104,12 +104,12 @@ unzip ((l, r)::xs) with (unzip xs)
105--------------------------------------------------------------------------------
105--------------------------------------------------------------------------------
107−map : (a -> b) -> Vect a n -> Vect b n
107+map : (a -> b) -> Vect n a -> Vect n b
108map f [] = []
108map f [] = []
109map f (x::xs) = f x :: map f xs
109map f (x::xs) = f x :: map f xs
111-- XXX: causes Idris to enter an infinite loop when type checking in the REPL
111-- 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)
113--mapMaybe f [] = (_ ** [])
113--mapMaybe f [] = (_ ** [])
114--mapMaybe f (x::xs) = mapMaybe' (f x)
114--mapMaybe f (x::xs) = mapMaybe' (f x)
115-- XXX: working around the type restrictions on case statements
115-- XXX: working around the type restrictions on case statements
@@ -122,11 +122,11 @@ map f (x::xs) = f x :: map f xs
123--------------------------------------------------------------------------------
123--------------------------------------------------------------------------------
125−total foldl : (a -> b -> a) -> a -> Vect b m -> a
125+total foldl : (a -> b -> a) -> a -> Vect m b -> a
126foldl f e [] = e
126foldl f e [] = e
127foldl f e (x::xs) = foldl f (f e x) xs
127foldl f e (x::xs) = foldl f (f e x) xs
129−total foldr : (a -> b -> b) -> b -> Vect a m -> b
129+total foldr : (a -> b -> b) -> b -> Vect m a -> b
130foldr f e [] = e
130foldr f e [] = e
131foldr f e (x::xs) = f x (foldr f e xs)
131foldr f e (x::xs) = f x (foldr f e xs)
@@ -134,39 +134,39 @@ foldr f e (x::xs) = f x (foldr f e xs)
134-- Special folds
134-- Special folds
135--------------------------------------------------------------------------------
135--------------------------------------------------------------------------------
137−concat : Vect (Vect a n) m -> Vect a (m * n)
137+concat : Vect m (Vect n a) -> Vect (m * n) a
138concat [] = []
138concat [] = []
139concat (v::vs) = v ++ concat vs
139concat (v::vs) = v ++ concat vs
141−total and : Vect Bool m -> Bool
141+total and : Vect m Bool -> Bool
142and = foldr (&&) True
142and = foldr (&&) True
144−total or : Vect Bool m -> Bool
144+total or : Vect m Bool -> Bool
145or = foldr (||) False
145or = foldr (||) False
147−total any : (a -> Bool) -> Vect a m -> Bool
147+total any : (a -> Bool) -> Vect m a -> Bool
148−any p = Vect.or . map p
148+any p = Vect.or . Vect.map p
150−total all : (a -> Bool) -> Vect a m -> Bool
150+total all : (a -> Bool) -> Vect m a -> Bool
151−all p = Vect.and . map p
151+all p = Vect.and . Vect.map p
153--------------------------------------------------------------------------------
153--------------------------------------------------------------------------------
154-- Transformations
154-- Transformations
155--------------------------------------------------------------------------------
155--------------------------------------------------------------------------------
157−total reverse : Vect a n -> Vect a n
157+total reverse : Vect n a -> Vect n a
158reverse = reverse' []
158reverse = reverse' []
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
161 reverse' acc [] ?= acc
161 reverse' acc [] ?= acc
162 reverse' acc (x::xs) ?= reverse' (x::acc) xs
162 reverse' acc (x::xs) ?= reverse' (x::acc) xs
164−total intersperse' : a -> Vect a m -> (p ** Vect a p)
164+total intersperse' : a -> Vect m a -> (p ** Vect p a)
165intersperse' sep [] = (_ ** [])
165intersperse' sep [] = (_ ** [])
166intersperse' sep (y::ys) with (intersperse' sep ys)
166intersperse' sep (y::ys) with (intersperse' sep ys)
167 | (_ ** tail) = (_ ** sep::y::tail)
167 | (_ ** tail) = (_ ** sep::y::tail)
169−total intersperse : a -> Vect a m -> (p ** Vect a p)
169+total intersperse : a -> Vect m a -> (p ** Vect p a)
170intersperse sep [] = (_ ** [])
170intersperse sep [] = (_ ** [])
171intersperse sep (x::xs) with (intersperse' sep xs)
171intersperse sep (x::xs) with (intersperse' sep xs)
172 | (_ ** tail) = (_ ** x::tail)
172 | (_ ** tail) = (_ ** x::tail)
@@ -175,56 +175,56 @@ intersperse sep (x::xs) with (intersperse' sep xs)
175-- Membership tests
175-- Membership tests
176--------------------------------------------------------------------------------
176--------------------------------------------------------------------------------
178−elemBy : (a -> a -> Bool) -> a -> Vect a n -> Bool
178+elemBy : (a -> a -> Bool) -> a -> Vect n a -> Bool
179elemBy p e [] = False
179elemBy p e [] = False
180elemBy p e (x::xs) with (p e x)
180elemBy p e (x::xs) with (p e x)
181 | True = True
181 | True = True
182 | False = elemBy p e xs
182 | False = elemBy p e xs
184−elem : Eq a => a -> Vect a n -> Bool
184+elem : Eq a => a -> Vect n a -> Bool
185elem = elemBy (==)
185elem = elemBy (==)
187−lookupBy : (a -> a -> Bool) -> a -> Vect (a, b) n -> Maybe b
187+lookupBy : (a -> a -> Bool) -> a -> Vect n (a, b) -> Maybe b
188lookupBy p e [] = Nothing
188lookupBy p e [] = Nothing
189lookupBy p e ((l, r)::xs) with (p e l)
189lookupBy p e ((l, r)::xs) with (p e l)
190 | True = Just r
190 | True = Just r
191 | False = lookupBy p e xs
191 | False = lookupBy p e xs
193−lookup : Eq a => a -> Vect (a, b) n -> Maybe b
193+lookup : Eq a => a -> Vect n (a, b) -> Maybe b
194lookup = lookupBy (==)
194lookup = lookupBy (==)
196−hasAnyBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
196+hasAnyBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
197hasAnyBy p elems [] = False
197hasAnyBy p elems [] = False
198hasAnyBy p elems (x::xs) with (elemBy p x elems)
198hasAnyBy p elems (x::xs) with (elemBy p x elems)
199 | True = True
199 | True = True
200 | False = hasAnyBy p elems xs
200 | False = hasAnyBy p elems xs
202−hasAny : Eq a => Vect a m -> Vect a n -> Bool
202+hasAny : Eq a => Vect m a -> Vect n a -> Bool
203hasAny = hasAnyBy (==)
203hasAny = hasAnyBy (==)
205--------------------------------------------------------------------------------
205--------------------------------------------------------------------------------
206-- Searching with a predicate
206-- Searching with a predicate
207--------------------------------------------------------------------------------
207--------------------------------------------------------------------------------
209−find : (a -> Bool) -> Vect a n -> Maybe a
209+find : (a -> Bool) -> Vect n a -> Maybe a
210find p [] = Nothing
210find p [] = Nothing
211find p (x::xs) with (p x)
211find p (x::xs) with (p x)
212 | True = Just x
212 | True = Just x
213 | False = find p xs
213 | False = find p xs
215−findIndex : (a -> Bool) -> Vect a n -> Maybe Nat
215+findIndex : (a -> Bool) -> Vect n a -> Maybe Nat
216findIndex = findIndex' 0
216findIndex = findIndex' 0
218− findIndex' : Nat -> (a -> Bool) -> Vect a n -> Maybe Nat
218+ findIndex' : Nat -> (a -> Bool) -> Vect n a -> Maybe Nat
219 findIndex' cnt p [] = Nothing
219 findIndex' cnt p [] = Nothing
220 findIndex' cnt p (x::xs) with (p x)
220 findIndex' cnt p (x::xs) with (p x)
221 | True = Just cnt
221 | True = Just cnt
222 | False = findIndex' (S cnt) p xs
222 | False = findIndex' (S cnt) p xs
224−total findIndices : (a -> Bool) -> Vect a m -> (p ** Vect Nat p)
224+total findIndices : (a -> Bool) -> Vect m a -> (p ** Vect p Nat)
225findIndices = findIndices' 0
225findIndices = findIndices' 0
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)
228 findIndices' cnt p [] = (_ ** [])
228 findIndices' cnt p [] = (_ ** [])
229 findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
229 findIndices' cnt p (x::xs) with (findIndices' (S cnt) p xs)
230 | (_ ** tail) =
230 | (_ ** tail) =
@@ -233,23 +233,23 @@ findIndices = findIndices' 0
234 (_ ** tail)
234 (_ ** tail)
236−elemIndexBy : (a -> a -> Bool) -> a -> Vect a m -> Maybe Nat
236+elemIndexBy : (a -> a -> Bool) -> a -> Vect m a -> Maybe Nat
237elemIndexBy p e = findIndex $ p e
237elemIndexBy p e = findIndex $ p e
239−elemIndex : Eq a => a -> Vect a m -> Maybe Nat
239+elemIndex : Eq a => a -> Vect m a -> Maybe Nat
240elemIndex = elemIndexBy (==)
240elemIndex = elemIndexBy (==)
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)
243elemIndicesBy p e = findIndices $ p e
243elemIndicesBy p e = findIndices $ p e
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)
246elemIndices = elemIndicesBy (==)
246elemIndices = elemIndicesBy (==)
248--------------------------------------------------------------------------------
248--------------------------------------------------------------------------------
249-- Filters
249-- Filters
250--------------------------------------------------------------------------------
250--------------------------------------------------------------------------------
252−total filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
252+total filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
253filter p [] = ( _ ** [] )
253filter p [] = ( _ ** [] )
254filter p (x::xs) with (filter p xs)
254filter p (x::xs) with (filter p xs)
255 | (_ ** tail) =
255 | (_ ** tail) =
@@ -258,17 +258,17 @@ filter p (x::xs) with (filter p xs)
259 (_ ** tail)
259 (_ ** tail)
261−nubBy : (a -> a -> Bool) -> Vect a n -> (p ** Vect a p)
261+nubBy : (a -> a -> Bool) -> Vect n a -> (p ** Vect p a)
262nubBy = nubBy' []
262nubBy = nubBy' []
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)
265 nubBy' acc p [] = (_ ** [])
265 nubBy' acc p [] = (_ ** [])
266 nubBy' acc p (x::xs) with (elemBy p x acc)
266 nubBy' acc p (x::xs) with (elemBy p x acc)
267 | True = nubBy' acc p xs
267 | True = nubBy' acc p xs
268 | False with (nubBy' (x::acc) p xs)
268 | False with (nubBy' (x::acc) p xs)
269 | (_ ** tail) = (_ ** x::tail)
269 | (_ ** tail) = (_ ** x::tail)
271−nub : Eq a => Vect a n -> (p ** Vect a p)
271+nub : Eq a => Vect n a -> (p ** Vect p a)
272nub = nubBy (==)
272nub = nubBy (==)
274--------------------------------------------------------------------------------
274--------------------------------------------------------------------------------
@@ -279,31 +279,31 @@ nub = nubBy (==)
279-- Predicates
279-- Predicates
280--------------------------------------------------------------------------------
280--------------------------------------------------------------------------------
282−isPrefixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
282+isPrefixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
283isPrefixOfBy p [] right = True
283isPrefixOfBy p [] right = True
284isPrefixOfBy p left [] = False
284isPrefixOfBy p left [] = False
285isPrefixOfBy p (x::xs) (y::ys) with (p x y)
285isPrefixOfBy p (x::xs) (y::ys) with (p x y)
286 | True = isPrefixOfBy p xs ys
286 | True = isPrefixOfBy p xs ys
287 | False = False
287 | False = False
289−isPrefixOf : Eq a => Vect a m -> Vect a n -> Bool
289+isPrefixOf : Eq a => Vect m a -> Vect n a -> Bool
290isPrefixOf = isPrefixOfBy (==)
290isPrefixOf = isPrefixOfBy (==)
292−isSuffixOfBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
292+isSuffixOfBy : (a -> a -> Bool) -> Vect m a -> Vect n a -> Bool
293isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
293isSuffixOfBy p left right = isPrefixOfBy p (reverse left) (reverse right)
295−isSuffixOf : Eq a => Vect a m -> Vect a n -> Bool
295+isSuffixOf : Eq a => Vect m a -> Vect n a -> Bool
296isSuffixOf = isSuffixOfBy (==)
296isSuffixOf = isSuffixOfBy (==)
298--------------------------------------------------------------------------------
298--------------------------------------------------------------------------------
299-- Conversions
299-- Conversions
300--------------------------------------------------------------------------------
300--------------------------------------------------------------------------------
302−total maybeToVect : Maybe a -> (p ** Vect a p)
302+total maybeToVect : Maybe a -> (p ** Vect p a)
303maybeToVect Nothing = (_ ** [])
303maybeToVect Nothing = (_ ** [])
304maybeToVect (Just j) = (_ ** [j])
304maybeToVect (Just j) = (_ ** [j])
306−total vectToMaybe : Vect a n -> Maybe a
306+total vectToMaybe : Vect n a -> Maybe a
307vectToMaybe [] = Nothing
307vectToMaybe [] = Nothing
308vectToMaybe (x::xs) = Just x
308vectToMaybe (x::xs) = Just x
@@ -311,17 +311,17 @@ vectToMaybe (x::xs) = Just x
312--------------------------------------------------------------------------------
312--------------------------------------------------------------------------------
314−catMaybes : Vect (Maybe a) n -> (p ** Vect a p)
314+catMaybes : Vect n (Maybe a) -> (p ** Vect p a)
315catMaybes [] = (_ ** [])
315catMaybes [] = (_ ** [])
316catMaybes (Nothing::xs) = catMaybes xs
316catMaybes (Nothing::xs) = catMaybes xs
317catMaybes ((Just j)::xs) with (catMaybes xs)
317catMaybes ((Just j)::xs) with (catMaybes xs)
318 | (_ ** tail) = (_ ** j::tail)
318 | (_ ** tail) = (_ ** j::tail)
320−range : Vect (Fin n) n
320+range : Vect n (Fin n)
322 reverse range_
322 reverse range_
324− range_ : Vect (Fin n) n
324+ range_ : Vect n (Fin n)
325 range_ {n=Z} = Nil
325 range_ {n=Z} = Nil
326 range_ {n=(S _)} = last :: map weaken range_
326 range_ {n=(S _)} = last :: map weaken range_