Debian Codegraph @

src/Data/Vec/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors, basic types and operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Vec.Base where
10
11 open import Data.Bool.Base using (Bool; true; false; if_then_else_)
12 open import Data.Nat.Base
13 open import Data.Fin.Base using (Fin; zero; suc)
14 open import Data.List.Base as List using (List)
15 open import Data.Product.Base as Product using (∃; ∃₂; _×_; _,_; proj₁; proj₂)
16 open import Data.These.Base as These using (These; this; that; these)
17 open import Function.Base using (const; _∘′_; id; _∘_; _$_)
18 open import Level using (Level)
19 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; trans; cong)
20 open import Relation.Nullary.Decidable.Core using (does; T?)
21 open import Relation.Unary using (Pred; Decidable)
22
23 private
24 variable
25 a b c p : Level
26 A : Set a
27 B : Set b
28 C : Set c
29 m n : ℕ
30
31 ------------------------------------------------------------------------
32 -- Types
33
34 infixr 5 _∷_
35
36 data Vec (A : Set a) : ℕ → Set a where
37 [] : Vec A zero
38 _∷_ : ∀ (x : A) (xs : Vec A n) → Vec A (suc n)
39
40 infix 4 _[_]=_
41
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
46
47 ------------------------------------------------------------------------
48 -- Basic operations
49
50 length : Vec A n → ℕ
51 length {n = n} _ = n
52
53 head : Vec A (1 + n) → A
54 head (x ∷ xs) = x
55
56 tail : Vec A (1 + n) → Vec A n
57 tail (x ∷ xs) = xs
58
59 lookup : Vec A n → Fin n → A
60 lookup (x ∷ xs) zero = x
61 lookup (x ∷ xs) (suc i) = lookup xs i
62
63 iterate : (A → A) → A → ∀ n → Vec A n
64 iterate s z zero = []
65 iterate s z (suc n) = z ∷ iterate s (s z) n
66
67 insertAt : Vec A n → Fin (suc n) → A → Vec A (suc n)
68 insertAt xs zero v = v ∷ xs
69 insertAt (x ∷ xs) (suc i) v = x ∷ insertAt xs i v
70
71 removeAt : Vec A (suc n) → Fin (suc n) → Vec A n
72 removeAt (x ∷ xs) zero = xs
73 removeAt (x ∷ xs@(_ ∷ _)) (suc i) = x ∷ removeAt xs i
74
75 updateAt : Vec A n → Fin n → (A → A) → Vec A n
76 updateAt (x ∷ xs) zero f = f x ∷ xs
77 updateAt (x ∷ xs) (suc i) f = x ∷ updateAt xs i f
78
79 -- xs [ i ]%= f modifies the i-th element of xs according to f
80
81 infixl 6 _[_]%=_ _[_]≔_
82
83 _[_]%=_ : Vec A n → Fin n → (A → A) → Vec A n
84 xs [ i ]%= f = updateAt xs i f
85
86 -- xs [ i ]≔ y overwrites the i-th element of xs with y
87
88 _[_]≔_ : Vec A n → Fin n → A → Vec A n
89 xs [ i ]≔ y = xs [ i ]%= const y
90
91 ------------------------------------------------------------------------
92 -- Operations for transforming vectors
93
94 -- See README.Data.Vec.Relation.Binary.Equality.Cast for the reasoning
95 -- system of `cast`-ed equality.
96 cast : .(eq : m ≡ n) → Vec A m → Vec A n
97 cast {n = zero} eq [] = []
98 cast {n = suc _} eq (x ∷ xs) = x ∷ cast (cong pred eq) xs
99
100 map : (A → B) → Vec A n → Vec B n
101 map f [] = []
102 map f (x ∷ xs) = f x ∷ map f xs
103
104 -- Concatenation.
105
106 infixr 5 _++_
107
108 _++_ : Vec A m → Vec A n → Vec A (m + n)
109 [] ++ ys = ys
110 (x ∷ xs) ++ ys = x ∷ (xs ++ ys)
111
112 concat : Vec (Vec A m) n → Vec A (n * m)
113 concat [] = []
114 concat (xs ∷ xss) = xs ++ concat xss
115
116 -- Align, Restrict, and Zip.
117
118 alignWith : (These A B → C) → Vec A m → Vec B n → Vec C (m ⊔ n)
119 alignWith f [] bs = map (f ∘′ that) bs
120 alignWith f as@(_ ∷ _) [] = map (f ∘′ this) as
121 alignWith f (a ∷ as) (b ∷ bs) = f (these a b) ∷ alignWith f as bs
122
123 restrictWith : (A → B → C) → Vec A m → Vec B n → Vec C (m ⊓ n)
124 restrictWith f [] bs = []
125 restrictWith f (_ ∷ _) [] = []
126 restrictWith f (a ∷ as) (b ∷ bs) = f a b ∷ restrictWith f as bs
127
128 zipWith : (A → B → C) → Vec A n → Vec B n → Vec C n
129 zipWith f [] [] = []
130 zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys
131
132 unzipWith : (A → B × C) → Vec A n → Vec B n × Vec C n
133 unzipWith f [] = [] , []
134 unzipWith f (a ∷ as) = Product.zip _∷_ _∷_ (f a) (unzipWith f as)
135
136 align : Vec A m → Vec B n → Vec (These A B) (m ⊔ n)
137 align = alignWith id
138
139 restrict : Vec A m → Vec B n → Vec (A × B) (m ⊓ n)
140 restrict = restrictWith _,_
141
142 zip : Vec A n → Vec B n → Vec (A × B) n
143 zip = zipWith _,_
144
145 unzip : Vec (A × B) n → Vec A n × Vec B n
146 unzip = unzipWith id
147
148 -- Interleaving.
149
150 infixr 5 _⋎_
151
152 _⋎_ : Vec A m → Vec A n → Vec A (m +⋎ n)
153 [] ⋎ ys = ys
154 (x ∷ xs) ⋎ ys = x ∷ (ys ⋎ xs)
155
156 -- Pointwise application
157
158 infixl 4 _⊛_
159
160 _⊛_ : Vec (A → B) n → Vec A n → Vec B n
161 [] ⊛ [] = []
162 (f ∷ fs) ⊛ (x ∷ xs) = f x ∷ (fs ⊛ xs)
163
164 -- Multiplication
165
166 module CartesianBind where
167 infixl 1 _>>=_
168
169 _>>=_ : Vec A m → (A → Vec B n) → Vec B (m * n)
170 xs >>= f = concat (map f xs)
171
172 infixl 4 _⊛*_
173
174 _⊛*_ : Vec (A → B) m → Vec A n → Vec B (m * n)
175 fs ⊛* xs = fs CartesianBind.>>= λ f → map f xs
176
177 allPairs : Vec A m → Vec B n → Vec (A × B) (m * n)
178 allPairs xs ys = map _,_ xs ⊛* ys
179
180 -- Diagonal
181
182 diagonal : Vec (Vec A n) n → Vec A n
183 diagonal [] = []
184 diagonal (xs ∷ xss) = head xs ∷ diagonal (map tail xss)
185
186 module DiagonalBind where
187 infixl 1 _>>=_
188
189 _>>=_ : Vec A n → (A → Vec B n) → Vec B n
190 xs >>= f = diagonal (map f xs)
191
192
193 ------------------------------------------------------------------------
194 -- Operations for reducing vectors
195
196 -- Dependent folds
197
198 module _ (A : Set a) (B : ℕ → Set b) where
199
200 FoldrOp = ∀ {n} → A → B n → B (suc n)
201 FoldlOp = ∀ {n} → B n → A → B (suc n)
202
203 foldr : ∀ (B : ℕ → Set b) → FoldrOp A B → B zero → Vec A n → B n
204 foldr B _⊕_ e [] = e
205 foldr B _⊕_ e (x ∷ xs) = x ⊕ foldr B _⊕_ e xs
206
207 foldl : ∀ (B : ℕ → Set b) → FoldlOp A B → B zero → Vec A n → B n
208 foldl B _⊕_ e [] = e
209 foldl B _⊕_ e (x ∷ xs) = foldl (B ∘ suc) _⊕_ (e ⊕ x) xs
210
211 -- Non-dependent folds
212
213 foldr′ : (A → B → B) → B → Vec A n → B
214 foldr′ _⊕_ = foldr _ _⊕_
215
216 foldl′ : (B → A → B) → B → Vec A n → B
217 foldl′ _⊕_ = foldl _ _⊕_
218
219 -- Non-empty folds
220
221 foldr₁ : (A → A → A) → Vec A (suc n) → A
222 foldr₁ _⊕_ (x ∷ []) = x
223 foldr₁ _⊕_ (x ∷ y ∷ ys) = x ⊕ foldr₁ _⊕_ (y ∷ ys)
224
225 foldl₁ : (A → A → A) → Vec A (suc n) → A
226 foldl₁ _⊕_ (x ∷ xs) = foldl _ _⊕_ x xs
227
228 -- Special folds
229
230 sum : Vec ℕ n → ℕ
231 sum = foldr _ _+_ 0
232
233 count : ∀ {P : Pred A p} → Decidable P → Vec A n → ℕ
234 count P? [] = zero
235 count P? (x ∷ xs) = if does (P? x) then suc else id $ count P? xs
236
237 countᵇ : (A → Bool) → Vec A n → ℕ
238 countᵇ p = count (T? ∘ p)
239
240 ------------------------------------------------------------------------
241 -- Operations for building vectors
242
243 [_] : A → Vec A 1
244 [ x ] = x ∷ []
245
246 replicate : (n : ℕ) → A → Vec A n
247 replicate zero x = []
248 replicate (suc n) x = x ∷ replicate n x
249
250 tabulate : (Fin n → A) → Vec A n
251 tabulate {n = zero} f = []
252 tabulate {n = suc n} f = f zero ∷ tabulate (f ∘ suc)
253
254 allFin : ∀ n → Vec (Fin n) n
255 allFin _ = tabulate id
256
257 ------------------------------------------------------------------------
258 -- Operations for dividing vectors
259
260 splitAt : ∀ m {n} (xs : Vec A (m + n)) →
261 ∃₂ λ (ys : Vec A m) (zs : Vec A n) → xs ≡ ys ++ zs
262 splitAt zero xs = [] , xs , refl
263 splitAt (suc m) (x ∷ xs) =
264 let ys , zs , eq = splitAt m xs in x ∷ ys , zs , cong (x ∷_) eq
265
266 take : ∀ m {n} → Vec A (m + n) → Vec A m
267 take m xs = proj₁ (splitAt m xs)
268
269 drop : ∀ m {n} → Vec A (m + n) → Vec A n
270 drop m xs = proj₁ (proj₂ (splitAt m xs))
271
272 group : ∀ n k (xs : Vec A (n * k)) →
273 ∃ λ (xss : Vec (Vec A k) n) → xs ≡ concat xss
274 group zero k [] = ([] , refl)
275 group (suc n) k xs =
276 let ys , zs , eq-split = splitAt k xs in
277 let zss , eq-group = group n k zs in
278 (ys ∷ zss) , trans eq-split (cong (ys ++_) eq-group)
279
280 split : Vec A n → Vec A ⌈ n /2⌉ × Vec A ⌊ n /2⌋
281 split [] = ([] , [])
282 split (x ∷ []) = (x ∷ [] , [])
283 split (x ∷ y ∷ xs) = Product.map (x ∷_) (y ∷_) (split xs)
284
285 uncons : Vec A (suc n) → A × Vec A n
286 uncons (x ∷ xs) = x , xs
287
288 ------------------------------------------------------------------------
289 -- Operations involving ≤
290
291 -- Take the first 'm' elements of a vector.
292 truncate : ∀ {m n} → m ≤ n → Vec A n → Vec A m
293 truncate {m = zero} _ _ = []
294 truncate (s≤s le) (x ∷ xs) = x ∷ (truncate le xs)
295
296 -- Pad out a vector with extra elements.
297 padRight : ∀ {m n} → m ≤ n → A → Vec A m → Vec A n
298 padRight z≤n a xs = replicate _ a
299 padRight (s≤s le) a (x ∷ xs) = x ∷ padRight le a xs
300
301 ------------------------------------------------------------------------
302 -- Operations for converting between lists
303
304 toList : Vec A n → List A
305 toList [] = List.[]
306 toList (x ∷ xs) = List._∷_ x (toList xs)
307
308 fromList : (xs : List A) → Vec A (List.length xs)
309 fromList List.[] = []
310 fromList (List._∷_ x xs) = x ∷ fromList xs
311
312 ------------------------------------------------------------------------
313 -- Operations for reversing vectors
314
315 -- snoc
316
317 infixl 5 _∷ʳ_
318
319 _∷ʳ_ : Vec A n → A → Vec A (suc n)
320 [] ∷ʳ y = [ y ]
321 (x ∷ xs) ∷ʳ y = x ∷ (xs ∷ʳ y)
322
323 -- vanilla reverse
324
325 reverse : Vec A n → Vec A n
326 reverse = foldl (Vec _) (λ rev x → x ∷ rev) []
327
328 -- reverse-append
329
330 infix 5 _ʳ++_
331
332 _ʳ++_ : Vec A m → Vec A n → Vec A (m + n)
333 xs ʳ++ ys = foldl (Vec _ ∘ (_+ _)) (λ rev x → x ∷ rev) ys xs
334
335 -- init and last
336
337 initLast : ∀ (xs : Vec A (1 + n)) → ∃₂ λ ys y → xs ≡ ys ∷ʳ y
338 initLast {n = zero} (x ∷ []) = [] , x , refl
339 initLast {n = suc n} (x ∷ xs) =
340 let ys , y , eq = initLast xs in
341 x ∷ ys , y , cong (x ∷_) eq
342
343 init : Vec A (1 + n) → Vec A n
344 init xs = proj₁ (initLast xs)
345
346 last : Vec A (1 + n) → A
347 last xs = proj₁ (proj₂ (initLast xs))
348
349 ------------------------------------------------------------------------
350 -- Other operations
351
352 transpose : Vec (Vec A n) m → Vec (Vec A m) n
353 transpose {n = n} [] = replicate n []
354 transpose {n = n} (as ∷ ass) = ((replicate n _∷_) ⊛ as) ⊛ transpose ass
355
356 ------------------------------------------------------------------------
357 -- DEPRECATED
358 ------------------------------------------------------------------------
359 -- Please use the new names as continuing support for the old names is
360 -- not guaranteed.
361
362 -- Version 2.0
363
364 remove = removeAt
365 {-# WARNING_ON_USAGE remove
366 "Warning: remove was deprecated in v2.0.
367 Please use removeAt instead."
368 #-}
369 insert = insertAt
370 {-# WARNING_ON_USAGE insert
371 "Warning: insert was deprecated in v2.0.
372 Please use insertAt instead."
373 #-}