@@ -26,6 +26,7 @@ private
26
26
A : Set a
27
27
B : Set b
28
28
C : Set c
29
+ m n : ℕ
29
30
30
31
------------------------------------------------------------------------
31
32
-- Types
@@ -34,121 +35,121 @@ infixr 5 _∷_
34
35
35
36
data Vec (A : Set a) : ℕ → Set a where
36
37
[] : Vec A zero
37
- _∷_ : ∀ {n} (x : A) (xs : Vec A n) → Vec A (suc n)
38
+ _∷_ : ∀ (x : A) (xs : Vec A n) → Vec A (suc n)
38
39
39
40
infix 4 _[_]=_
40
41
41
- data _[_]=_ {A : Set a} : ∀ {n} → Vec A n → Fin n → A → Set a where
42
- here : ∀ {n} {x} {xs : Vec A n} → x ∷ xs [ zero ]= x
43
- there : ∀ {n} { i} {x y} {xs : Vec A n}
44
- (xs[i]=x : xs [ i ]= x) → y ∷ xs [ suc i ]= x
42
+ data _[_]=_ {A : Set a} : Vec A n → Fin n → A → Set a where
43
+ here : ∀ {x} {xs : Vec A n} → x ∷ xs [ zero ]= x
44
+ there : ∀ {i} {x y} {xs : Vec A n}
45
+ (xs[i]=x : xs [ i ]= x) → y ∷ xs [ suc i ]= x
45
46
46
47
------------------------------------------------------------------------
47
48
-- Basic operations
48
49
49
- length : ∀ {n} → Vec A n → ℕ
50
+ length : Vec A n → ℕ
50
51
length {n = n} _ = n
51
52
52
- head : ∀ {n} → Vec A (1 + n) → A
53
+ head : Vec A (1 + n) → A
53
54
head (x ∷ xs) = x
54
55
55
- tail : ∀ {n} → Vec A (1 + n) → Vec A n
56
+ tail : Vec A (1 + n) → Vec A n
56
57
tail (x ∷ xs) = xs
57
58
58
- lookup : ∀ {n} → Vec A n → Fin n → A
59
+ lookup : Vec A n → Fin n → A
59
60
lookup (x ∷ xs) zero = x
60
61
lookup (x ∷ xs) (suc i) = lookup xs i
61
62
62
- insert : ∀ {n} → Vec A n → Fin (suc n) → A → Vec A (suc n)
63
+ insert : Vec A n → Fin (suc n) → A → Vec A (suc n)
63
64
insert xs zero v = v ∷ xs
64
65
insert (x ∷ xs) (suc i) v = x ∷ insert xs i v
65
66
66
- remove : ∀ {n} → Vec A (suc n) → Fin (suc n) → Vec A n
67
+ remove : Vec A (suc n) → Fin (suc n) → Vec A n
67
68
remove (_ ∷ xs) zero = xs
68
69
remove (x ∷ y ∷ xs) (suc i) = x ∷ remove (y ∷ xs) i
69
70
70
- updateAt : ∀ {n} → Fin n → (A → A) → Vec A n → Vec A n
71
+ updateAt : Fin n → (A → A) → Vec A n → Vec A n
71
72
updateAt zero f (x ∷ xs) = f x ∷ xs
72
73
updateAt (suc i) f (x ∷ xs) = x ∷ updateAt i f xs
73
74
74
75
-- xs [ i ]%= f modifies the i-th element of xs according to f
75
76
76
77
infixl 6 _[_]%=_
77
78
78
- _[_]%=_ : ∀ {n} → Vec A n → Fin n → (A → A) → Vec A n
79
+ _[_]%=_ : Vec A n → Fin n → (A → A) → Vec A n
79
80
xs [ i ]%= f = updateAt i f xs
80
81
81
82
-- xs [ i ]≔ y overwrites the i-th element of xs with y
82
83
83
84
infixl 6 _[_]≔_
84
85
85
- _[_]≔_ : ∀ {n} → Vec A n → Fin n → A → Vec A n
86
+ _[_]≔_ : Vec A n → Fin n → A → Vec A n
86
87
xs [ i ]≔ y = xs [ i ]%= const y
87
88
88
89
------------------------------------------------------------------------
89
90
-- Operations for transforming vectors
90
91
91
- map : ∀ {n} → (A → B) → Vec A n → Vec B n
92
+ map : (A → B) → Vec A n → Vec B n
92
93
map f [] = []
93
94
map f (x ∷ xs) = f x ∷ map f xs
94
95
95
96
-- Concatenation.
96
97
97
98
infixr 5 _++_
98
99
99
- _++_ : ∀ {m n} → Vec A m → Vec A n → Vec A (m + n)
100
+ _++_ : Vec A m → Vec A n → Vec A (m + n)
100
101
[] ++ ys = ys
101
102
(x ∷ xs) ++ ys = x ∷ (xs ++ ys)
102
103
103
- concat : ∀ {m n} → Vec (Vec A m) n → Vec A (n * m)
104
+ concat : Vec (Vec A m) n → Vec A (n * m)
104
105
concat [] = []
105
106
concat (xs ∷ xss) = xs ++ concat xss
106
107
107
108
-- Align, Restrict, and Zip.
108
109
109
- alignWith : ∀ {m n} → (These A B → C) → Vec A m → Vec B n → Vec C (m ⊔ n)
110
+ alignWith : (These A B → C) → Vec A m → Vec B n → Vec C (m ⊔ n)
110
111
alignWith f [] bs = map (f ∘′ that) bs
111
112
alignWith f as@(_ ∷ _) [] = map (f ∘′ this) as
112
113
alignWith f (a ∷ as) (b ∷ bs) = f (these a b) ∷ alignWith f as bs
113
114
114
- restrictWith : ∀ {m n} → (A → B → C) → Vec A m → Vec B n → Vec C (m ⊓ n)
115
+ restrictWith : (A → B → C) → Vec A m → Vec B n → Vec C (m ⊓ n)
115
116
restrictWith f [] bs = []
116
117
restrictWith f (_ ∷ _) [] = []
117
118
restrictWith f (a ∷ as) (b ∷ bs) = f a b ∷ restrictWith f as bs
118
119
119
- zipWith : ∀ {n} → (A → B → C) → Vec A n → Vec B n → Vec C n
120
+ zipWith : (A → B → C) → Vec A n → Vec B n → Vec C n
120
121
zipWith f [] [] = []
121
122
zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys
122
123
123
- unzipWith : ∀ {n} → (A → B × C) → Vec A n → Vec B n × Vec C n
124
+ unzipWith : (A → B × C) → Vec A n → Vec B n × Vec C n
124
125
unzipWith f [] = [] , []
125
126
unzipWith f (a ∷ as) = Prod.zip _∷_ _∷_ (f a) (unzipWith f as)
126
127
127
- align : ∀ {m n} → Vec A m → Vec B n → Vec (These A B) (m ⊔ n)
128
+ align : Vec A m → Vec B n → Vec (These A B) (m ⊔ n)
128
129
align = alignWith id
129
130
130
- restrict : ∀ {m n} → Vec A m → Vec B n → Vec (A × B) (m ⊓ n)
131
+ restrict : Vec A m → Vec B n → Vec (A × B) (m ⊓ n)
131
132
restrict = restrictWith _,_
132
133
133
- zip : ∀ {n} → Vec A n → Vec B n → Vec (A × B) n
134
+ zip : Vec A n → Vec B n → Vec (A × B) n
134
135
zip = zipWith _,_
135
136
136
- unzip : ∀ {n} → Vec (A × B) n → Vec A n × Vec B n
137
+ unzip : Vec (A × B) n → Vec A n × Vec B n
137
138
unzip = unzipWith id
138
139
139
140
-- Interleaving.
140
141
141
142
infixr 5 _⋎_
142
143
143
- _⋎_ : ∀ {m n} → Vec A m → Vec A n → Vec A (m +⋎ n)
144
+ _⋎_ : Vec A m → Vec A n → Vec A (m +⋎ n)
144
145
[] ⋎ ys = ys
145
146
(x ∷ xs) ⋎ ys = x ∷ (ys ⋎ xs)
146
147
147
148
-- Pointwise application
148
149
149
150
infixl 4 _⊛_
150
151
151
- _⊛_ : ∀ {n} → Vec (A → B) n → Vec A n → Vec B n
152
+ _⊛_ : Vec (A → B) n → Vec A n → Vec B n
152
153
[] ⊛ [] = []
153
154
(f ∷ fs) ⊛ (x ∷ xs) = f x ∷ (fs ⊛ xs)
154
155
@@ -157,27 +158,27 @@ _⊛_ : ∀ {n} → Vec (A → B) n → Vec A n → Vec B n
157
158
module CartesianBind where
158
159
infixl 1 _>>=_
159
160
160
- _>>=_ : ∀ {m n} → Vec A m → (A → Vec B n) → Vec B (m * n)
161
+ _>>=_ : Vec A m → (A → Vec B n) → Vec B (m * n)
161
162
xs >>= f = concat (map f xs)
162
163
163
164
infixl 4 _⊛*_
164
165
165
- _⊛*_ : ∀ {m n} → Vec (A → B) m → Vec A n → Vec B (m * n)
166
+ _⊛*_ : Vec (A → B) m → Vec A n → Vec B (m * n)
166
167
fs ⊛* xs = fs CartesianBind.>>= λ f → map f xs
167
168
168
- allPairs : ∀ {m n} → Vec A m → Vec B n → Vec (A × B) (m * n)
169
+ allPairs : Vec A m → Vec B n → Vec (A × B) (m * n)
169
170
allPairs xs ys = map _,_ xs ⊛* ys
170
171
171
172
-- Diagonal
172
173
173
- diagonal : ∀ {n} → Vec (Vec A n) n → Vec A n
174
+ diagonal : Vec (Vec A n) n → Vec A n
174
175
diagonal [] = []
175
176
diagonal (xs ∷ xss) = head xs ∷ diagonal (map tail xss)
176
177
177
178
module DiagonalBind where
178
179
infixl 1 _>>=_
179
180
180
- _>>=_ : ∀ {n} → Vec A n → (A → Vec B n) → Vec B n
181
+ _>>=_ : Vec A n → (A → Vec B n) → Vec B n
181
182
xs >>= f = diagonal (map f xs)
182
183
183
184
------------------------------------------------------------------------
@@ -190,43 +191,37 @@ module _ (A : Set a) (B : ℕ → Set b) where
190
191
FoldrOp = ∀ {n} → A → B n → B (suc n)
191
192
FoldlOp = ∀ {n} → B n → A → B (suc n)
192
193
193
- foldr : ∀ (B : ℕ → Set b) {m} →
194
- FoldrOp A B →
195
- B zero →
196
- Vec A m → B m
197
- foldr B _⊕_ n [] = n
198
- foldr B _⊕_ n (x ∷ xs) = x ⊕ foldr B _⊕_ n xs
194
+ foldr : ∀ (B : ℕ → Set b) → FoldrOp A B → B zero → Vec A n → B n
195
+ foldr B _⊕_ e [] = e
196
+ foldr B _⊕_ e (x ∷ xs) = x ⊕ foldr B _⊕_ e xs
199
197
200
- foldl : ∀ (B : ℕ → Set b) {m} →
201
- FoldlOp A B →
202
- B zero →
203
- Vec A m → B m
204
- foldl B _⊕_ n [] = n
205
- foldl B _⊕_ n (x ∷ xs) = foldl (B ∘ suc) _⊕_ (n ⊕ x) xs
198
+ foldl : ∀ (B : ℕ → Set b) → FoldlOp A B → B zero → Vec A n → B n
199
+ foldl B _⊕_ e [] = e
200
+ foldl B _⊕_ e (x ∷ xs) = foldl (B ∘ suc) _⊕_ (e ⊕ x) xs
206
201
207
202
-- Non-dependent folds
208
203
209
- foldr′ : ∀ {n} → (A → B → B) → B → Vec A n → B
210
- foldr′ _⊕_ = foldr _ λ {n} → _⊕_
204
+ foldr′ : (A → B → B) → B → Vec A n → B
205
+ foldr′ _⊕_ = foldr _ _⊕_
211
206
212
- foldl′ : ∀ {n} → (B → A → B) → B → Vec A n → B
213
- foldl′ _⊕_ = foldl _ λ {n} → _⊕_
207
+ foldl′ : (B → A → B) → B → Vec A n → B
208
+ foldl′ _⊕_ = foldl _ _⊕_
214
209
215
210
-- Non-empty folds
216
211
217
- foldr₁ : ∀ {n} → (A → A → A) → Vec A (suc n) → A
212
+ foldr₁ : (A → A → A) → Vec A (suc n) → A
218
213
foldr₁ _⊕_ (x ∷ []) = x
219
214
foldr₁ _⊕_ (x ∷ y ∷ ys) = x ⊕ foldr₁ _⊕_ (y ∷ ys)
220
215
221
- foldl₁ : ∀ {n} → (A → A → A) → Vec A (suc n) → A
216
+ foldl₁ : (A → A → A) → Vec A (suc n) → A
222
217
foldl₁ _⊕_ (x ∷ xs) = foldl _ _⊕_ x xs
223
218
224
219
-- Special folds
225
220
226
- sum : ∀ {n} → Vec ℕ n → ℕ
221
+ sum : Vec ℕ n → ℕ
227
222
sum = foldr _ _+_ 0
228
223
229
- count : ∀ {P : Pred A p} → Decidable P → ∀ {n} → Vec A n → ℕ
224
+ count : ∀ {P : Pred A p} → Decidable P → Vec A n → ℕ
230
225
count P? [] = zero
231
226
count P? (x ∷ xs) with does (P? x)
232
227
... | true = suc (count P? xs)
@@ -238,11 +233,11 @@ count P? (x ∷ xs) with does (P? x)
238
233
[_] : A → Vec A 1
239
234
[ x ] = x ∷ []
240
235
241
- replicate : ∀ {n} → A → Vec A n
236
+ replicate : A → Vec A n
242
237
replicate {n = zero} x = []
243
238
replicate {n = suc n} x = x ∷ replicate x
244
239
245
- tabulate : ∀ {n} → (Fin n → A) → Vec A n
240
+ tabulate : (Fin n → A) → Vec A n
246
241
tabulate {n = zero} f = []
247
242
tabulate {n = suc n} f = f zero ∷ tabulate (f ∘ suc)
248
243
@@ -275,18 +270,18 @@ group (suc n) k .(ys ++ zs) | (ys , zs , refl) with group n k zs
275
270
group (suc n) k .(ys ++ concat zss) | (ys , ._ , refl) | (zss , refl) =
276
271
((ys ∷ zss) , refl)
277
272
278
- split : ∀ {n} → Vec A n → Vec A ⌈ n /2⌉ × Vec A ⌊ n /2⌋
273
+ split : Vec A n → Vec A ⌈ n /2⌉ × Vec A ⌊ n /2⌋
279
274
split [] = ([] , [])
280
275
split (x ∷ []) = (x ∷ [] , [])
281
276
split (x ∷ y ∷ xs) = Prod.map (x ∷_) (y ∷_) (split xs)
282
277
283
- uncons : ∀ {n} → Vec A (suc n) → A × Vec A n
278
+ uncons : Vec A (suc n) → A × Vec A n
284
279
uncons (x ∷ xs) = x , xs
285
280
286
281
------------------------------------------------------------------------
287
282
-- Operations for converting between lists
288
283
289
- toList : ∀ {n} → Vec A n → List A
284
+ toList : Vec A n → List A
290
285
toList [] = List.[]
291
286
toList (x ∷ xs) = List._∷_ x (toList xs)
292
287
@@ -301,42 +296,40 @@ fromList (List._∷_ x xs) = x ∷ fromList xs
301
296
302
297
infixl 5 _∷ʳ_
303
298
304
- _∷ʳ_ : ∀ {n} → Vec A n → A → Vec A (suc n)
299
+ _∷ʳ_ : Vec A n → A → Vec A (suc n)
305
300
[] ∷ʳ y = [ y ]
306
301
(x ∷ xs) ∷ʳ y = x ∷ (xs ∷ʳ y)
307
302
308
303
-- vanilla reverse
309
304
310
- reverse : ∀ {n} → Vec A n → Vec A n
311
- reverse {A = A} = foldl (Vec A ) (λ rev x → x ∷ rev) []
305
+ reverse : Vec A n → Vec A n
306
+ reverse = foldl (Vec _ ) (λ rev x → x ∷ rev) []
312
307
313
308
-- reverse-append
314
309
315
310
infix 5 _ʳ++_
316
311
317
- _ʳ++_ : ∀ {m n} → Vec A m → Vec A n → Vec A (m + n)
318
- _ʳ++_ {A = A} {n = n} xs ys = foldl (( Vec A) ∘ (_+ n )) (λ rev x → x ∷ rev) ys xs
312
+ _ʳ++_ : Vec A m → Vec A n → Vec A (m + n)
313
+ xs ʳ++ ys = foldl (Vec _ ∘ (_+ _ )) (λ rev x → x ∷ rev) ys xs
319
314
320
315
-- init and last
321
316
322
- initLast : ∀ {n} (xs : Vec A (1 + n)) →
323
- ∃₂ λ (ys : Vec A n) (y : A) → xs ≡ ys ∷ʳ y
324
- initLast {n = zero} (x ∷ []) = ([] , x , refl)
325
- initLast {n = suc n} (x ∷ xs) with initLast xs
326
- initLast {n = suc n} (x ∷ .(ys ∷ʳ y)) | (ys , y , refl) =
327
- ((x ∷ ys) , y , refl)
317
+ initLast : ∀ (xs : Vec A (1 + n)) → ∃₂ λ ys y → xs ≡ ys ∷ʳ y
318
+ initLast {n = zero} (x ∷ []) = ([] , x , refl)
319
+ initLast {n = suc n} (x ∷ xs) with initLast xs
320
+ ... | (ys , y , refl) = (x ∷ ys , y , refl)
328
321
329
- init : ∀ {n} → Vec A (1 + n) → Vec A n
330
- init xs with initLast xs
331
- init .(ys ∷ʳ y) | (ys , y , refl) = ys
322
+ init : Vec A (1 + n) → Vec A n
323
+ init xs with initLast xs
324
+ ... | (ys , y , refl) = ys
332
325
333
- last : ∀ {n} → Vec A (1 + n) → A
334
- last xs with initLast xs
335
- last .(ys ∷ʳ y) | (ys , y , refl) = y
326
+ last : Vec A (1 + n) → A
327
+ last xs with initLast xs
328
+ ... | (ys , y , refl) = y
336
329
337
330
------------------------------------------------------------------------
338
331
-- Other operations
339
332
340
- transpose : ∀ {m n} → Vec (Vec A n) m → Vec (Vec A m) n
333
+ transpose : Vec (Vec A n) m → Vec (Vec A m) n
341
334
transpose [] = replicate []
342
335
transpose (as ∷ ass) = replicate _∷_ ⊛ as ⊛ transpose ass
0 commit comments