Debian Codegraph @

src/Data/List/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lists, basic types and operations
5 ------------------------------------------------------------------------
6
7 -- See README.Data.List for examples of how to use and reason about
8 -- lists.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Data.List.Base where
13
14 open import Algebra.Bundles.Raw using (RawMagma; RawMonoid)
15 open import Data.Bool.Base as Bool
16 using (Bool; false; true; not; _∧_; _∨_; if_then_else_)
17 open import Data.Fin.Base using (Fin; zero; suc)
18 open import Data.Maybe.Base as Maybe using (Maybe; nothing; just; maybe′)
19 open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
20 open import Data.Product.Base as Product using (_×_; _,_; map₁; map₂′)
21 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
22 open import Data.These.Base as These using (These; this; that; these)
23 open import Function.Base
24 using (id; _∘_ ; _∘′_; _∘₂_; _$_; const; flip)
25 open import Level using (Level)
26 open import Relation.Unary using (Pred; Decidable)
27 open import Relation.Binary.Core using (Rel)
28 import Relation.Binary.Definitions as B
29 open import Relation.Binary.PropositionalEquality.Core using (_≡_)
30 open import Relation.Nullary.Decidable.Core using (T?; does; ¬?)
31
32 private
33 variable
34 a b c p ℓ : Level
35 A : Set a
36 B : Set b
37 C : Set c
38
39 ------------------------------------------------------------------------
40 -- Types
41
42 open import Agda.Builtin.List public
43 using (List; []; _∷_)
44
45 ------------------------------------------------------------------------
46 -- Operations for transforming lists
47
48 map : (A → B) → List A → List B
49 map f [] = []
50 map f (x ∷ xs) = f x ∷ map f xs
51
52 infixr 5 _++_
53
54 _++_ : List A → List A → List A
55 [] ++ ys = ys
56 (x ∷ xs) ++ ys = x ∷ (xs ++ ys)
57
58 intersperse : A → List A → List A
59 intersperse x [] = []
60 intersperse x (y ∷ []) = y ∷ []
61 intersperse x (y ∷ ys) = y ∷ x ∷ intersperse x ys
62
63 intercalate : List A → List (List A) → List A
64 intercalate xs [] = []
65 intercalate xs (ys ∷ []) = ys
66 intercalate xs (ys ∷ yss) = ys ++ xs ++ intercalate xs yss
67
68 cartesianProductWith : (A → B → C) → List A → List B → List C
69 cartesianProductWith f [] _ = []
70 cartesianProductWith f (x ∷ xs) ys = map (f x) ys ++ cartesianProductWith f xs ys
71
72 cartesianProduct : List A → List B → List (A × B)
73 cartesianProduct = cartesianProductWith _,_
74
75 ------------------------------------------------------------------------
76 -- Aligning and zipping
77
78 alignWith : (These A B → C) → List A → List B → List C
79 alignWith f [] bs = map (f ∘′ that) bs
80 alignWith f as [] = map (f ∘′ this) as
81 alignWith f (a ∷ as) (b ∷ bs) = f (these a b) ∷ alignWith f as bs
82
83 zipWith : (A → B → C) → List A → List B → List C
84 zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys
85 zipWith f _ _ = []
86
87 unalignWith : (A → These B C) → List A → List B × List C
88 unalignWith f [] = [] , []
89 unalignWith f (a ∷ as) with f a
90 ... | this b = Product.map₁ (b ∷_) (unalignWith f as)
91 ... | that c = Product.map₂ (c ∷_) (unalignWith f as)
92 ... | these b c = Product.map (b ∷_) (c ∷_) (unalignWith f as)
93
94 unzipWith : (A → B × C) → List A → List B × List C
95 unzipWith f [] = [] , []
96 unzipWith f (xy ∷ xys) = Product.zip _∷_ _∷_ (f xy) (unzipWith f xys)
97
98 partitionSumsWith : (A → B ⊎ C) → List A → List B × List C
99 partitionSumsWith f = unalignWith (These.fromSum ∘′ f)
100
101 align : List A → List B → List (These A B)
102 align = alignWith id
103
104 zip : List A → List B → List (A × B)
105 zip = zipWith (_,_)
106
107 unalign : List (These A B) → List A × List B
108 unalign = unalignWith id
109
110 unzip : List (A × B) → List A × List B
111 unzip = unzipWith id
112
113 partitionSums : List (A ⊎ B) → List A × List B
114 partitionSums = partitionSumsWith id
115
116 merge : {R : Rel A ℓ} → B.Decidable R → List A → List A → List A
117 merge R? [] ys = ys
118 merge R? xs [] = xs
119 merge R? x∷xs@(x ∷ xs) y∷ys@(y ∷ ys) = if does (R? x y)
120 then x ∷ merge R? xs y∷ys
121 else y ∷ merge R? x∷xs ys
122
123 ------------------------------------------------------------------------
124 -- Operations for reducing lists
125
126 foldr : (A → B → B) → B → List A → B
127 foldr c n [] = n
128 foldr c n (x ∷ xs) = c x (foldr c n xs)
129
130 foldl : (A → B → A) → A → List B → A
131 foldl c n [] = n
132 foldl c n (x ∷ xs) = foldl c (c n x) xs
133
134 concat : List (List A) → List A
135 concat = foldr _++_ []
136
137 concatMap : (A → List B) → List A → List B
138 concatMap f = concat ∘ map f
139
140 ap : List (A → B) → List A → List B
141 ap fs as = concatMap (flip map as) fs
142
143 catMaybes : List (Maybe A) → List A
144 catMaybes = foldr (maybe′ _∷_ id) []
145
146 mapMaybe : (A → Maybe B) → List A → List B
147 mapMaybe p = catMaybes ∘ map p
148
149 null : List A → Bool
150 null [] = true
151 null (x ∷ xs) = false
152
153 length : List A → ℕ
154 length = foldr (const suc) 0
155
156 ------------------------------------------------------------------------
157 -- Operations for constructing lists
158
159 [_] : A → List A
160 [ x ] = x ∷ []
161
162 fromMaybe : Maybe A → List A
163 fromMaybe (just x) = [ x ]
164 fromMaybe nothing = []
165
166 replicate : ℕ → A → List A
167 replicate zero x = []
168 replicate (suc n) x = x ∷ replicate n x
169
170 iterate : (A → A) → A → ℕ → List A
171 iterate f e zero = []
172 iterate f e (suc n) = e ∷ iterate f (f e) n
173
174 inits : List A → List (List A)
175 inits {A = A} = λ xs → [] ∷ tail xs
176 module Inits where
177 tail : List A → List (List A)
178 tail [] = []
179 tail (x ∷ xs) = [ x ] ∷ map (x ∷_) (tail xs)
180
181 tails : List A → List (List A)
182 tails {A = A} = λ xs → xs ∷ tail xs
183 module Tails where
184 tail : List A → List (List A)
185 tail [] = []
186 tail (_ ∷ xs) = xs ∷ tail xs
187
188 insertAt : (xs : List A) → Fin (suc (length xs)) → A → List A
189 insertAt xs zero v = v ∷ xs
190 insertAt (x ∷ xs) (suc i) v = x ∷ insertAt xs i v
191
192 updateAt : (xs : List A) → Fin (length xs) → (A → A) → List A
193 updateAt (x ∷ xs) zero f = f x ∷ xs
194 updateAt (x ∷ xs) (suc i) f = x ∷ updateAt xs i f
195
196 -- Tabulation
197
198 applyUpTo : (ℕ → A) → ℕ → List A
199 applyUpTo f zero = []
200 applyUpTo f (suc n) = f zero ∷ applyUpTo (f ∘ suc) n
201
202 applyDownFrom : (ℕ → A) → ℕ → List A
203 applyDownFrom f zero = []
204 applyDownFrom f (suc n) = f n ∷ applyDownFrom f n
205
206 tabulate : ∀ {n} (f : Fin n → A) → List A
207 tabulate {n = zero} f = []
208 tabulate {n = suc n} f = f zero ∷ tabulate (f ∘ suc)
209
210 lookup : ∀ (xs : List A) → Fin (length xs) → A
211 lookup (x ∷ xs) zero = x
212 lookup (x ∷ xs) (suc i) = lookup xs i
213
214 -- Numerical
215
216 upTo : ℕ → List ℕ
217 upTo = applyUpTo id
218
219 downFrom : ℕ → List ℕ
220 downFrom = applyDownFrom id
221
222 allFin : ∀ n → List (Fin n)
223 allFin n = tabulate id
224
225 unfold : ∀ (P : ℕ → Set b)
226 (f : ∀ {n} → P (suc n) → Maybe (A × P n)) →
227 ∀ {n} → P n → List A
228 unfold P f {n = zero} s = []
229 unfold P f {n = suc n} s = maybe′ (λ (x , s′) → x ∷ unfold P f s′) [] (f s)
230
231 ------------------------------------------------------------------------
232 -- Operations for reversing lists
233
234 reverseAcc : List A → List A → List A
235 reverseAcc = foldl (flip _∷_)
236
237 reverse : List A → List A
238 reverse = reverseAcc []
239
240 -- "Reverse append" xs ʳ++ ys = reverse xs ++ ys
241
242 infixr 5 _ʳ++_
243
244 _ʳ++_ : List A → List A → List A
245 _ʳ++_ = flip reverseAcc
246
247 -- Snoc: Cons, but from the right.
248
249 infixl 6 _∷ʳ_
250
251 _∷ʳ_ : List A → A → List A
252 xs ∷ʳ x = xs ++ [ x ]
253
254
255
256 -- Backwards initialisation
257
258 infixl 5 _∷ʳ′_
259
260 data InitLast {A : Set a} : List A → Set a where
261 [] : InitLast []
262 _∷ʳ′_ : (xs : List A) (x : A) → InitLast (xs ∷ʳ x)
263
264 initLast : (xs : List A) → InitLast xs
265 initLast [] = []
266 initLast (x ∷ xs) with initLast xs
267 ... | [] = [] ∷ʳ′ x
268 ... | ys ∷ʳ′ y = (x ∷ ys) ∷ʳ′ y
269
270 -- uncons, but from the right
271 unsnoc : List A → Maybe (List A × A)
272 unsnoc as with initLast as
273 ... | [] = nothing
274 ... | xs ∷ʳ′ x = just (xs , x)
275
276 ------------------------------------------------------------------------
277 -- Operations for deconstructing lists
278
279 -- Note that although the following three combinators can be useful for
280 -- programming, when proving it is often a better idea to manually
281 -- destruct a list argument as each branch of the pattern-matching will
282 -- have a refined type.
283
284 uncons : List A → Maybe (A × List A)
285 uncons [] = nothing
286 uncons (x ∷ xs) = just (x , xs)
287
288 head : List A → Maybe A
289 head [] = nothing
290 head (x ∷ _) = just x
291
292 tail : List A → Maybe (List A)
293 tail [] = nothing
294 tail (_ ∷ xs) = just xs
295
296 last : List A → Maybe A
297 last [] = nothing
298 last (x ∷ []) = just x
299 last (_ ∷ xs) = last xs
300
301 take : ℕ → List A → List A
302 take zero xs = []
303 take (suc n) [] = []
304 take (suc n) (x ∷ xs) = x ∷ take n xs
305
306 drop : ℕ → List A → List A
307 drop zero xs = xs
308 drop (suc n) [] = []
309 drop (suc n) (x ∷ xs) = drop n xs
310
311 splitAt : ℕ → List A → List A × List A
312 splitAt zero xs = ([] , xs)
313 splitAt (suc n) [] = ([] , [])
314 splitAt (suc n) (x ∷ xs) = Product.map₁ (x ∷_) (splitAt n xs)
315
316 removeAt : (xs : List A) → Fin (length xs) → List A
317 removeAt (x ∷ xs) zero = xs
318 removeAt (x ∷ xs) (suc i) = x ∷ removeAt xs i
319
320 ------------------------------------------------------------------------
321 -- Operations for filtering lists
322
323 -- The following are a variety of functions that can be used to
324 -- construct sublists using a predicate.
325 --
326 -- Each function has two forms. The first main variant uses a
327 -- proof-relevant decidable predicate, while the second variant uses
328 -- a irrelevant boolean predicate and are suffixed with a `ᵇ` character,
329 -- typed as \^b.
330 --
331 -- The decidable versions have several advantages: 1) easier to prove
332 -- properties, 2) better meta-variable inference and 3) most of the rest
333 -- of the library is set-up to work with decidable predicates. However,
334 -- in rare cases the boolean versions can be useful, mainly when one
335 -- wants to minimise dependencies.
336 --
337 -- In summary, in most cases you probably want to use the decidable
338 -- versions over the boolean versions, e.g. use `takeWhile (_≤? 10) xs`
339 -- rather than `takeWhileᵇ (_≤ᵇ 10) xs`.
340
341 takeWhile : ∀ {P : Pred A p} → Decidable P → List A → List A
342 takeWhile P? [] = []
343 takeWhile P? (x ∷ xs) with does (P? x)
344 ... | true = x ∷ takeWhile P? xs
345 ... | false = []
346
347 takeWhileᵇ : (A → Bool) → List A → List A
348 takeWhileᵇ p = takeWhile (T? ∘ p)
349
350 dropWhile : ∀ {P : Pred A p} → Decidable P → List A → List A
351 dropWhile P? [] = []
352 dropWhile P? (x ∷ xs) with does (P? x)
353 ... | true = dropWhile P? xs
354 ... | false = x ∷ xs
355
356 dropWhileᵇ : (A → Bool) → List A → List A
357 dropWhileᵇ p = dropWhile (T? ∘ p)
358
359 filter : ∀ {P : Pred A p} → Decidable P → List A → List A
360 filter P? [] = []
361 filter P? (x ∷ xs) with does (P? x)
362 ... | false = filter P? xs
363 ... | true = x ∷ filter P? xs
364
365 filterᵇ : (A → Bool) → List A → List A
366 filterᵇ p = filter (T? ∘ p)
367
368 partition : ∀ {P : Pred A p} → Decidable P → List A → (List A × List A)
369 partition P? [] = ([] , [])
370 partition P? (x ∷ xs) with does (P? x) | partition P? xs
371 ... | true | (ys , zs) = (x ∷ ys , zs)
372 ... | false | (ys , zs) = (ys , x ∷ zs)
373
374 partitionᵇ : (A → Bool) → List A → List A × List A
375 partitionᵇ p = partition (T? ∘ p)
376
377 span : ∀ {P : Pred A p} → Decidable P → List A → (List A × List A)
378 span P? [] = ([] , [])
379 span P? ys@(x ∷ xs) with does (P? x)
380 ... | true = Product.map (x ∷_) id (span P? xs)
381 ... | false = ([] , ys)
382
383
384 spanᵇ : (A → Bool) → List A → List A × List A
385 spanᵇ p = span (T? ∘ p)
386
387 break : ∀ {P : Pred A p} → Decidable P → List A → (List A × List A)
388 break P? = span (¬? ∘ P?)
389
390 breakᵇ : (A → Bool) → List A → List A × List A
391 breakᵇ p = break (T? ∘ p)
392
393 -- The predicate `P` represents the notion of newline character for the
394 -- type `A`. It is used to split the input list into a list of lines.
395 -- Some lines may be empty if the input contains at least two
396 -- consecutive newline characters.
397 linesBy : ∀ {P : Pred A p} → Decidable P → List A → List (List A)
398 linesBy {A = A} P? = go nothing where
399
400 go : Maybe (List A) → List A → List (List A)
401 go acc [] = maybe′ ([_] ∘′ reverse) [] acc
402 go acc (c ∷ cs) = if does (P? c)
403 then reverse acc′ ∷ go nothing cs
404 else go (just (c ∷ acc′)) cs
405 where acc′ = Maybe.fromMaybe [] acc
406
407 linesByᵇ : (A → Bool) → List A → List (List A)
408 linesByᵇ p = linesBy (T? ∘ p)
409
410 -- The predicate `P` represents the notion of space character for the
411 -- type `A`. It is used to split the input list into a list of words.
412 -- All the words are non empty and the output does not contain any space
413 -- characters.
414 wordsBy : ∀ {P : Pred A p} → Decidable P → List A → List (List A)
415 wordsBy {A = A} P? = go [] where
416
417 cons : List A → List (List A) → List (List A)
418 cons [] ass = ass
419 cons as ass = reverse as ∷ ass
420
421 go : List A → List A → List (List A)
422 go acc [] = cons acc []
423 go acc (c ∷ cs) = if does (P? c)
424 then cons acc (go [] cs)
425 else go (c ∷ acc) cs
426
427 wordsByᵇ : (A → Bool) → List A → List (List A)
428 wordsByᵇ p = wordsBy (T? ∘ p)
429
430 derun : ∀ {R : Rel A p} → B.Decidable R → List A → List A
431 derun R? [] = []
432 derun R? (x ∷ []) = x ∷ []
433 derun R? (x ∷ xs@(y ∷ _)) with does (R? x y) | derun R? xs
434 ... | true | ys = ys
435 ... | false | ys = x ∷ ys
436
437 derunᵇ : (A → A → Bool) → List A → List A
438 derunᵇ r = derun (T? ∘₂ r)
439
440 deduplicate : ∀ {R : Rel A p} → B.Decidable R → List A → List A
441 deduplicate R? [] = []
442 deduplicate R? (x ∷ xs) = x ∷ filter (¬? ∘ R? x) (deduplicate R? xs)
443
444 deduplicateᵇ : (A → A → Bool) → List A → List A
445 deduplicateᵇ r = deduplicate (T? ∘₂ r)
446
447 -- Finds the first element satisfying the boolean predicate
448 find : ∀ {P : Pred A p} → Decidable P → List A → Maybe A
449 find P? [] = nothing
450 find P? (x ∷ xs) = if does (P? x) then just x else find P? xs
451
452 findᵇ : (A → Bool) → List A → Maybe A
453 findᵇ p = find (T? ∘ p)
454
455 -- Finds the index of the first element satisfying the boolean predicate
456 findIndex : ∀ {P : Pred A p} → Decidable P → (xs : List A) → Maybe $ Fin (length xs)
457 findIndex P? [] = nothing
458 findIndex P? (x ∷ xs) = if does (P? x)
459 then just zero
460 else Maybe.map suc (findIndex P? xs)
461
462 findIndexᵇ : (A → Bool) → (xs : List A) → Maybe $ Fin (length xs)
463 findIndexᵇ p = findIndex (T? ∘ p)
464
465 -- Finds indices of all the elements satisfying the boolean predicate
466 findIndices : ∀ {P : Pred A p} → Decidable P → (xs : List A) → List $ Fin (length xs)
467 findIndices P? [] = []
468 findIndices P? (x ∷ xs) = if does (P? x)
469 then zero ∷ indices
470 else indices
471 where indices = map suc (findIndices P? xs)
472
473 findIndicesᵇ : (A → Bool) → (xs : List A) → List $ Fin (length xs)
474 findIndicesᵇ p = findIndices (T? ∘ p)
475
476 ------------------------------------------------------------------------
477 -- Actions on single elements
478
479 infixl 5 _[_]%=_ _[_]∷=_
480
481 -- xs [ i ]%= f modifies the i-th element of xs according to f
482
483 _[_]%=_ : (xs : List A) → Fin (length xs) → (A → A) → List A
484 xs [ i ]%= f = updateAt xs i f
485
486 -- xs [ i ]≔ y overwrites the i-th element of xs with y
487
488 _[_]∷=_ : (xs : List A) → Fin (length xs) → A → List A
489 xs [ k ]∷= v = xs [ k ]%= const v
490
491 ------------------------------------------------------------------------
492 -- Conditional versions of cons and snoc
493
494 infixr 5 _?∷_
495 _?∷_ : Maybe A → List A → List A
496 _?∷_ = maybe′ _∷_ id
497
498 infixl 6 _∷ʳ?_
499 _∷ʳ?_ : List A → Maybe A → List A
500 xs ∷ʳ? x = maybe′ (xs ∷ʳ_) xs x
501
502 ------------------------------------------------------------------------
503 -- Raw algebraic bundles
504
505 module _ (A : Set a) where
506 ++-rawMagma : RawMagma a _
507 ++-rawMagma = record
508 { Carrier = List A
509 ; _≈_ = _≡_
510 ; _∙_ = _++_
511 }
512
513 ++-[]-rawMonoid : RawMonoid a _
514 ++-[]-rawMonoid = record
515 { Carrier = List A
516 ; _≈_ = _≡_
517 ; _∙_ = _++_
518 ; ε = []
519 }
520
521 ------------------------------------------------------------------------
522 -- DEPRECATED
523 ------------------------------------------------------------------------
524 -- Please use the new names as continuing support for the old names is
525 -- not guaranteed.
526
527 -- Version 1.4
528
529 infixl 5 _∷ʳ'_
530 _∷ʳ'_ : (xs : List A) (x : A) → InitLast (xs ∷ʳ x)
531 _∷ʳ'_ = InitLast._∷ʳ′_
532 {-# WARNING_ON_USAGE _∷ʳ'_
533 "Warning: _∷ʳ'_ (ending in an apostrophe) was deprecated in v1.4.
534 Please use _∷ʳ′_ (ending in a prime) instead."
535 #-}
536
537 -- Version 2.0
538
539 infixl 5 _─_
540 _─_ = removeAt
541 {-# WARNING_ON_USAGE _─_
542 "Warning: _─_ was deprecated in v2.0.
543 Please use removeAt instead."
544 #-}
545
546 -- Version 2.1
547
548 scanr : (A → B → B) → B → List A → List B
549 scanr f e [] = e ∷ []
550 scanr f e (x ∷ xs) with scanr f e xs
551 ... | [] = [] -- dead branch
552 ... | ys@(y ∷ _) = f x y ∷ ys
553 {-# WARNING_ON_USAGE scanr
554 "Warning: scanr was deprecated in v2.1.
555 Please use Data.List.Scans.Base.scanr instead."
556 #-}
557
558 scanl : (A → B → A) → A → List B → List A
559 scanl f e [] = e ∷ []
560 scanl f e (x ∷ xs) = e ∷ scanl f (f e x) xs
561 {-# WARNING_ON_USAGE scanl
562 "Warning: scanl was deprecated in v2.1.
563 Please use Data.List.Scans.Base.scanl instead."
564 #-}
565
566 -- Version 2.3
567
568 and : List Bool → Bool
569 and = foldr _∧_ true
570
571 all : (A → Bool) → List A → Bool
572 all p = and ∘ map p
573 {-# WARNING_ON_USAGE and
574 "Warning: and was deprecated in v2.3.
575 Please use Data.Bool.ListAction.and instead."
576 #-}
577 {-# WARNING_ON_USAGE all
578 "Warning: all was deprecated in v2.3.
579 Please use Data.Nat.ListAction.all instead."
580 #-}
581
582 or : List Bool → Bool
583 or = foldr _∨_ false
584
585 any : (A → Bool) → List A → Bool
586 any p = or ∘ map p
587 {-# WARNING_ON_USAGE or
588 "Warning: or was deprecated in v2.3.
589 Please use Data.Bool.ListAction.or instead."
590 #-}
591 {-# WARNING_ON_USAGE any
592 "Warning: any was deprecated in v2.3.
593 Please use Data.Bool.ListAction.any instead."
594 #-}
595
596 sum : List ℕ → ℕ
597 sum = foldr ℕ._+_ 0
598 {-# WARNING_ON_USAGE sum
599 "Warning: sum was deprecated in v2.3.
600 Please use Data.Nat.ListAction.sum instead."
601 #-}
602
603 product : List ℕ → ℕ
604 product = foldr ℕ._*_ 1
605 {-# WARNING_ON_USAGE product
606 "Warning: product was deprecated in v2.3.
607 Please use Data.Nat.ListAction.product instead."
608 #-}