-
Notifications
You must be signed in to change notification settings - Fork 63
/
lists.lagda.md
786 lines (660 loc) · 25.2 KB
/
lists.lagda.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# Lists
```agda
module univalent-combinatorics.lists where
```
<details><summary>Imports</summary>
```agda
open import elementary-number-theory.addition-natural-numbers
open import elementary-number-theory.multiplication-natural-numbers
open import elementary-number-theory.natural-numbers
open import foundation.booleans
open import foundation.cartesian-product-types
open import foundation.contractible-types
open import foundation.coproduct-types
open import foundation.decidable-equality
open import foundation.decidable-types
open import foundation.dependent-pair-types
open import foundation.empty-types
open import foundation.equivalences
open import foundation.functions
open import foundation.functoriality-dependent-pair-types
open import foundation.homotopies
open import foundation.identity-types
open import foundation.negation
open import foundation.raising-universe-levels
open import foundation.sets
open import foundation.truncated-types
open import foundation.truncation-levels
open import foundation.unit-type
open import foundation.universe-levels
open import group-theory.monoids
```
</details>
## Idea
The type of lists of elements of a type `A` is defined inductively, with an
empty list and an operation that extends a list with one element from `A`.
## Definition
### The inductive definition of the type of lists
```agda
data list {l : Level} (A : UU l) : UU l where
nil : list A
cons : A → list A → list A
```
### Predicates on the type of lists
```agda
is-nil-list : {l : Level} {A : UU l} → list A → UU l
is-nil-list l = (l = nil)
is-nonnil-list : {l : Level} {A : UU l} → list A → UU l
is-nonnil-list l = ¬ (is-nil-list l)
is-cons-list : {l : Level} {A : UU l} → list A → UU l
is-cons-list {l1} {A} l = Σ (A × list A) (λ (a , l') → l = cons a l')
```
### Operations
```agda
snoc : {l : Level} {A : UU l} → list A → A → list A
snoc nil a = cons a nil
snoc (cons b l) a = cons b (snoc l a)
in-list : {l : Level} {A : UU l} → A → list A
in-list a = cons a nil
fold-list :
{l1 l2 : Level} {A : UU l1} {B : UU l2} (b : B) (μ : A → (B → B)) →
list A → B
fold-list b μ nil = b
fold-list b μ (cons a l) = μ a (fold-list b μ l)
map-list :
{l1 l2 : Level} {A : UU l1} {B : UU l2} (f : A → B) →
list A → list B
map-list f = fold-list nil (λ a → cons (f a))
length-list : {l : Level} {A : UU l} → list A → ℕ
length-list = fold-list 0 (λ a → succ-ℕ)
sum-list-ℕ : list ℕ → ℕ
sum-list-ℕ = fold-list 0 add-ℕ
product-list-ℕ : list ℕ → ℕ
product-list-ℕ = fold-list 1 mul-ℕ
concat-list : {l : Level} {A : UU l} → list A → (list A → list A)
concat-list {l} {A} = fold-list id (λ a f → (cons a) ∘ f)
flatten-list : {l : Level} {A : UU l} → list (list A) → list A
flatten-list = fold-list nil concat-list
reverse-list : {l : Level} {A : UU l} → list A → list A
reverse-list nil = nil
reverse-list (cons a l) = concat-list (reverse-list l) (in-list a)
elem-list :
{l1 : Level} {A : UU l1} →
has-decidable-equality A →
A → list A → bool
elem-list d x nil = false
elem-list d x (cons x' l) with (d x x')
... | inl _ = true
... | inr _ = elem-list d x l
union-list :
{l1 : Level} {A : UU l1} →
has-decidable-equality A →
list A → list A → list A
union-list d nil l' = l'
union-list d (cons x l) l' with (elem-list d x l')
... | true = l'
... | false = cons x l'
data _∈-list_ {l : Level} {A : UU l} : A → list A → UU l where
is-head : (a : A) (l : list A) → a ∈-list (cons a l)
is-in-tail : (a x : A) (l : list A) → a ∈-list l → a ∈-list (cons x l)
unit-list : {l : Level} {A : UU l} → A → list A
unit-list a = cons a nil
```
## Properties
### A list that uses cons is not nil
```agda
is-nonnil-cons-list :
{l : Level} {A : UU l} →
(a : A) → (l : list A) → is-nonnil-list (cons a l)
is-nonnil-cons-list a l ()
is-nonnil-is-cons-list :
{l : Level} {A : UU l} →
(l : list A) → is-cons-list l → is-nonnil-list l
is-nonnil-is-cons-list l ((a , l') , refl) q =
is-nonnil-cons-list a l' q
```
### A list that uses cons is not nil
```agda
is-cons-is-nonnil-list :
{l : Level} {A : UU l} →
(l : list A) → is-nonnil-list l → is-cons-list l
is-cons-is-nonnil-list nil p = ex-falso (p refl)
is-cons-is-nonnil-list (cons x l) p = ((x , l) , refl)
head-is-nonnil-list :
{l : Level} {A : UU l} →
(l : list A) → is-nonnil-list l → A
head-is-nonnil-list l p =
pr1 (pr1 (is-cons-is-nonnil-list l p))
tail-is-nonnil-list :
{l : Level} {A : UU l} →
(l : list A) → is-nonnil-list l → list A
tail-is-nonnil-list l p =
pr2 (pr1 (is-cons-is-nonnil-list l p))
```
### Characterizing the identity type of lists
```agda
Eq-list : {l1 : Level} {A : UU l1} → list A → list A → UU l1
Eq-list {l1} nil nil = raise-unit l1
Eq-list {l1} nil (cons x l') = raise-empty l1
Eq-list {l1} (cons x l) nil = raise-empty l1
Eq-list {l1} (cons x l) (cons x' l') = (Id x x') × Eq-list l l'
refl-Eq-list : {l1 : Level} {A : UU l1} (l : list A) → Eq-list l l
refl-Eq-list nil = raise-star
refl-Eq-list (cons x l) = pair refl (refl-Eq-list l)
Eq-eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) → Id l l' → Eq-list l l'
Eq-eq-list l .l refl = refl-Eq-list l
eq-Eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) → Eq-list l l' → Id l l'
eq-Eq-list nil nil (map-raise star) = refl
eq-Eq-list nil (cons x l') (map-raise f) = ex-falso f
eq-Eq-list (cons x l) nil (map-raise f) = ex-falso f
eq-Eq-list (cons x l) (cons .x l') (pair refl e) =
ap (cons x) (eq-Eq-list l l' e)
square-eq-Eq-list :
{l1 : Level} {A : UU l1} {x : A} {l l' : list A} (p : Id l l') →
Id (Eq-eq-list (cons x l) (cons x l') (ap (cons x) p))
(pair refl (Eq-eq-list l l' p))
square-eq-Eq-list refl = refl
issec-eq-Eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) (e : Eq-list l l') →
Id (Eq-eq-list l l' (eq-Eq-list l l' e)) e
issec-eq-Eq-list nil nil e = eq-is-contr is-contr-raise-unit
issec-eq-Eq-list nil (cons x l') e = ex-falso (is-empty-raise-empty e)
issec-eq-Eq-list (cons x l) nil e = ex-falso (is-empty-raise-empty e)
issec-eq-Eq-list (cons x l) (cons .x l') (pair refl e) =
( square-eq-Eq-list (eq-Eq-list l l' e)) ∙
( ap (pair refl) (issec-eq-Eq-list l l' e))
eq-Eq-refl-Eq-list :
{l1 : Level} {A : UU l1} (l : list A) →
Id (eq-Eq-list l l (refl-Eq-list l)) refl
eq-Eq-refl-Eq-list nil = refl
eq-Eq-refl-Eq-list (cons x l) = ap (ap (cons x)) (eq-Eq-refl-Eq-list l)
isretr-eq-Eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) (p : Id l l') →
Id (eq-Eq-list l l' (Eq-eq-list l l' p)) p
isretr-eq-Eq-list nil .nil refl = refl
isretr-eq-Eq-list (cons x l) .(cons x l) refl = eq-Eq-refl-Eq-list (cons x l)
is-equiv-Eq-eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) → is-equiv (Eq-eq-list l l')
is-equiv-Eq-eq-list l l' =
is-equiv-has-inverse
( eq-Eq-list l l')
( issec-eq-Eq-list l l')
( isretr-eq-Eq-list l l')
equiv-Eq-list :
{l1 : Level} {A : UU l1} (l l' : list A) → Id l l' ≃ Eq-list l l'
equiv-Eq-list l l' =
pair (Eq-eq-list l l') (is-equiv-Eq-eq-list l l')
is-contr-total-Eq-list :
{l1 : Level} {A : UU l1} (l : list A) →
is-contr (Σ (list A) (Eq-list l))
is-contr-total-Eq-list {A = A} l =
is-contr-equiv'
( Σ (list A) (Id l))
( equiv-tot (equiv-Eq-list l))
( is-contr-total-path l)
is-trunc-Eq-list :
(k : 𝕋) {l : Level} {A : UU l} → is-trunc (succ-𝕋 (succ-𝕋 k)) A →
(l l' : list A) → is-trunc (succ-𝕋 k) (Eq-list l l')
is-trunc-Eq-list k H nil nil =
is-trunc-is-contr (succ-𝕋 k) is-contr-raise-unit
is-trunc-Eq-list k H nil (cons x l') =
is-trunc-is-empty k is-empty-raise-empty
is-trunc-Eq-list k H (cons x l) nil =
is-trunc-is-empty k is-empty-raise-empty
is-trunc-Eq-list k H (cons x l) (cons y l') =
is-trunc-prod (succ-𝕋 k) (H x y) (is-trunc-Eq-list k H l l')
is-trunc-list :
(k : 𝕋) {l : Level} {A : UU l} → is-trunc (succ-𝕋 (succ-𝕋 k)) A →
is-trunc (succ-𝕋 (succ-𝕋 k)) (list A)
is-trunc-list k H l l' =
is-trunc-equiv
( succ-𝕋 k)
( Eq-list l l')
( equiv-Eq-list l l')
( is-trunc-Eq-list k H l l')
is-set-list :
{l : Level} {A : UU l} → is-set A → is-set (list A)
is-set-list = is-trunc-list neg-two-𝕋
list-Set : {l : Level} → Set l → Set l
list-Set A = pair (list (type-Set A)) (is-set-list (is-set-type-Set A))
has-decidable-equality-list :
{l1 : Level} {A : UU l1} →
has-decidable-equality A → has-decidable-equality (list A)
has-decidable-equality-list d nil nil = inl refl
has-decidable-equality-list d nil (cons x l) =
inr (map-inv-raise ∘ Eq-eq-list nil (cons x l))
has-decidable-equality-list d (cons x l) nil =
inr (map-inv-raise ∘ Eq-eq-list (cons x l) nil)
has-decidable-equality-list d (cons x l) (cons x' l') =
is-decidable-iff
( eq-Eq-list (cons x l) (cons x' l'))
( Eq-eq-list (cons x l) (cons x' l'))
( is-decidable-prod
( d x x')
( is-decidable-iff
( Eq-eq-list l l')
( eq-Eq-list l l')
( has-decidable-equality-list d l l')))
is-decidable-left-factor :
{l1 l2 : Level} {A : UU l1} {B : UU l2} →
is-decidable (A × B) → B → is-decidable A
is-decidable-left-factor (inl (pair x y)) b = inl x
is-decidable-left-factor (inr f) b = inr (λ a → f (pair a b))
is-decidable-right-factor :
{l1 l2 : Level} {A : UU l1} {B : UU l2} →
is-decidable (A × B) → A → is-decidable B
is-decidable-right-factor (inl (pair x y)) a = inl y
is-decidable-right-factor (inr f) a = inr (λ b → f (pair a b))
has-decidable-equality-has-decidable-equality-list :
{l1 : Level} {A : UU l1} →
has-decidable-equality (list A) → has-decidable-equality A
has-decidable-equality-has-decidable-equality-list d x y =
is-decidable-left-factor
( is-decidable-iff
( Eq-eq-list (cons x nil) (cons y nil))
( eq-Eq-list (cons x nil) (cons y nil))
( d (cons x nil) (cons y nil)))
( raise-star)
```
### Functoriality of the list operation
First we introduce the functoriality of the list operation, because it will come
in handy when we try to define and prove more advanced things.
```agda
functor-list :
{l1 l2 : Level} {A : UU l1} {B : UU l2} (f : A → B) →
list A → list B
functor-list f nil = nil
functor-list f (cons a x) = cons (f a) (functor-list f x)
identity-law-functor-list :
{l1 : Level} {A : UU l1} →
functor-list (id {A = A}) ~ id
identity-law-functor-list nil = refl
identity-law-functor-list (cons a x) =
ap (cons a) (identity-law-functor-list x)
composition-law-functor-list :
{l1 l2 l3 : Level} {A : UU l1} {B : UU l2} {C : UU l3} →
(f : A → B) (g : B → C) →
functor-list (g ∘ f) ~ (functor-list g ∘ functor-list f)
composition-law-functor-list f g nil = refl
composition-law-functor-list f g (cons a x) =
ap (cons (g (f a))) (composition-law-functor-list f g x)
```
### List concatenation is associative and unital
Concatenation of lists is an associative operation and nil is the unit for list
concatenation.
```agda
assoc-concat-list :
{l1 : Level} {A : UU l1} (x y z : list A) →
Id (concat-list (concat-list x y) z) (concat-list x (concat-list y z))
assoc-concat-list nil y z = refl
assoc-concat-list (cons a x) y z = ap (cons a) (assoc-concat-list x y z)
left-unit-law-concat-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (concat-list nil x) x
left-unit-law-concat-list x = refl
right-unit-law-concat-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (concat-list x nil) x
right-unit-law-concat-list nil = refl
right-unit-law-concat-list (cons a x) =
ap (cons a) (right-unit-law-concat-list x)
list-Monoid : {l : Level} (X : Set l) → Monoid l
list-Monoid X =
pair
( pair (list-Set X) (pair concat-list assoc-concat-list))
( pair nil (pair left-unit-law-concat-list right-unit-law-concat-list))
```
### The length operation behaves well with respect to the other list operations
```agda
length-nil :
{l1 : Level} {A : UU l1} →
Id (length-list {A = A} nil) zero-ℕ
length-nil = refl
length-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id (length-list (concat-list x y)) (add-ℕ (length-list x) (length-list y))
length-concat-list nil y = inv (left-unit-law-add-ℕ (length-list y))
length-concat-list (cons a x) y =
( ap succ-ℕ (length-concat-list x y)) ∙
( inv (left-successor-law-add-ℕ (length-list x) (length-list y)))
length-functor-list :
{l1 l2 : Level} {A : UU l1} {B : UU l2} (f : A → B) (l : list A) →
Id (length-list (map-list f l)) (length-list l)
length-functor-list f nil = refl
length-functor-list f (cons x l) =
ap succ-ℕ (length-functor-list f l)
is-nil-is-zero-length-list :
{l1 : Level} {A : UU l1}
(l : list A) →
is-zero-ℕ (length-list l) →
is-nil-list l
is-nil-is-zero-length-list nil p = refl
is-nonnil-is-nonzero-length-list :
{l1 : Level} {A : UU l1}
(l : list A) →
is-nonzero-ℕ (length-list l) →
is-nonnil-list l
is-nonnil-is-nonzero-length-list nil p q = p refl
is-nonnil-is-nonzero-length-list (cons x l) p ()
is-nonzero-length-is-nonnil-list :
{l1 : Level} {A : UU l1}
(l : list A) →
is-nonnil-list l →
is-nonzero-ℕ (length-list l)
is-nonzero-length-is-nonnil-list nil p q = p refl
lenght-tail-is-nonnil-list :
{l1 : Level} {A : UU l1}
(l : list A) → (p : is-nonnil-list l) →
succ-ℕ (length-list (tail-is-nonnil-list l p)) =
length-list l
lenght-tail-is-nonnil-list nil p = ex-falso (p refl)
lenght-tail-is-nonnil-list (cons x l) p = refl
```
### Properties of flattening
```agda
flatten-unit-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (flatten-list (unit-list x)) x
flatten-unit-list x = right-unit-law-concat-list x
length-flatten-list :
{l1 : Level} {A : UU l1} (x : list (list A)) →
Id ( length-list (flatten-list x))
( sum-list-ℕ (functor-list length-list x))
length-flatten-list nil = refl
length-flatten-list (cons a x) =
( length-concat-list a (flatten-list x)) ∙
( ap (add-ℕ (length-list a)) (length-flatten-list x))
flatten-concat-list :
{l1 : Level} {A : UU l1} (x y : list (list A)) →
Id ( flatten-list (concat-list x y))
( concat-list (flatten-list x) (flatten-list y))
flatten-concat-list nil y = refl
flatten-concat-list (cons a x) y =
( ap (concat-list a) (flatten-concat-list x y)) ∙
( inv (assoc-concat-list a (flatten-list x) (flatten-list y)))
flatten-flatten-list :
{l1 : Level} {A : UU l1} (x : list (list (list A))) →
Id ( flatten-list (flatten-list x))
( flatten-list (functor-list flatten-list x))
flatten-flatten-list nil = refl
flatten-flatten-list (cons a x) =
( flatten-concat-list a (flatten-list x)) ∙
( ap (concat-list (flatten-list a)) (flatten-flatten-list x))
```
### Properties of list reversal
```agda
reverse-unit-list :
{l1 : Level} {A : UU l1} (a : A) →
Id (reverse-list (unit-list a)) (unit-list a)
reverse-unit-list a = refl
length-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (length-list (reverse-list x)) (length-list x)
length-reverse-list nil = refl
length-reverse-list (cons a x) =
( length-concat-list (reverse-list x) (unit-list a)) ∙
( ap succ-ℕ (length-reverse-list x))
reverse-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( reverse-list (concat-list x y))
( concat-list (reverse-list y) (reverse-list x))
reverse-concat-list nil y =
inv (right-unit-law-concat-list (reverse-list y))
reverse-concat-list (cons a x) y =
( ap (λ t → concat-list t (unit-list a)) (reverse-concat-list x y)) ∙
( assoc-concat-list (reverse-list y) (reverse-list x) (unit-list a))
reverse-flatten-list :
{l1 : Level} {A : UU l1} (x : list (list A)) →
Id ( reverse-list (flatten-list x))
( flatten-list (reverse-list (functor-list reverse-list x)))
reverse-flatten-list nil = refl
reverse-flatten-list (cons a x) =
( reverse-concat-list a (flatten-list x)) ∙
( ( ap (λ t → concat-list t (reverse-list a)) (reverse-flatten-list x)) ∙
( ( ap
( concat-list
( flatten-list (reverse-list (functor-list reverse-list x))))
( inv (flatten-unit-list (reverse-list a)))) ∙
( inv
( flatten-concat-list
( reverse-list (functor-list reverse-list x))
( unit-list (reverse-list a))))))
reverse-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (reverse-list (reverse-list x)) x
reverse-reverse-list nil = refl
reverse-reverse-list (cons a x) =
( reverse-concat-list (reverse-list x) (unit-list a)) ∙
( ap (concat-list (unit-list a)) (reverse-reverse-list x))
```
## Head and tail operations
We define the head and tail operations, and we define the operations of picking
and removing the last element from a list.
```agda
head-snoc-list :
{l : Level} {A : UU l} (l : list A) → A → A
head-snoc-list nil a = a
head-snoc-list (cons h l) a = h
head-list :
{l1 : Level} {A : UU l1} → list A → list A
head-list nil = nil
head-list (cons a x) = unit-list a
tail-list :
{l1 : Level} {A : UU l1} → list A → list A
tail-list nil = nil
tail-list (cons a x) = x
last-element-list :
{l1 : Level} {A : UU l1} → list A → list A
last-element-list nil = nil
last-element-list (cons a nil) = unit-list a
last-element-list (cons a (cons b x)) = last-element-list (cons b x)
remove-last-element-list :
{l1 : Level} {A : UU l1} → list A → list A
remove-last-element-list nil = nil
remove-last-element-list (cons a nil) = nil
remove-last-element-list (cons a (cons b x)) =
cons a (remove-last-element-list (cons b x))
cons' :
{l1 : Level} {A : UU l1} → list A → A → list A
cons' x a = concat-list x (unit-list a)
```
### Properties of heads and tails and their duals
```agda
eta-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (concat-list (head-list x) (tail-list x)) x
eta-list nil = refl
eta-list (cons a x) = refl
eta-list' :
{l1 : Level} {A : UU l1} (x : list A) →
Id (concat-list (remove-last-element-list x) (last-element-list x)) x
eta-list' nil = refl
eta-list' (cons a nil) = refl
eta-list' (cons a (cons b x)) = ap (cons a) (eta-list' (cons b x))
last-element-cons' :
{l1 : Level} {A : UU l1} (x : list A) (a : A) →
Id (last-element-list (cons' x a)) (unit-list a)
last-element-cons' nil a = refl
last-element-cons' (cons b nil) a = refl
last-element-cons' (cons b (cons c x)) a =
last-element-cons' (cons c x) a
head-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( head-list (concat-list x y))
( head-list (concat-list (head-list x) (head-list y)))
head-concat-list nil nil = refl
head-concat-list nil (cons x y) = refl
head-concat-list (cons a x) y = refl
tail-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( tail-list (concat-list x y))
( concat-list (tail-list x) (tail-list (concat-list (head-list x) y)))
tail-concat-list nil y = refl
tail-concat-list (cons a x) y = refl
last-element-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( last-element-list (concat-list x y))
( last-element-list
( concat-list (last-element-list x) (last-element-list y)))
last-element-concat-list nil nil = refl
last-element-concat-list nil (cons b nil) = refl
last-element-concat-list nil (cons b (cons c y)) =
last-element-concat-list nil (cons c y)
last-element-concat-list (cons a nil) nil = refl
last-element-concat-list (cons a nil) (cons b nil) = refl
last-element-concat-list (cons a nil) (cons b (cons c y)) =
last-element-concat-list (cons a nil) (cons c y)
last-element-concat-list (cons a (cons b x)) y =
last-element-concat-list (cons b x) y
remove-last-element-concat-list :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( remove-last-element-list (concat-list x y))
( concat-list
( remove-last-element-list (concat-list x (head-list y)))
( remove-last-element-list y))
remove-last-element-concat-list nil nil = refl
remove-last-element-concat-list nil (cons a nil) = refl
remove-last-element-concat-list nil (cons a (cons b y)) = refl
remove-last-element-concat-list (cons a nil) nil = refl
remove-last-element-concat-list (cons a nil) (cons b y) = refl
remove-last-element-concat-list (cons a (cons b x)) y =
ap (cons a) (remove-last-element-concat-list (cons b x) y)
head-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (head-list (reverse-list x)) (last-element-list x)
head-reverse-list nil = refl
head-reverse-list (cons a nil) = refl
head-reverse-list (cons a (cons b x)) =
( ap head-list
( assoc-concat-list (reverse-list x) (unit-list b) (unit-list a))) ∙
( ( head-concat-list
( reverse-list x)
( concat-list (unit-list b) (unit-list a))) ∙
( ( inv (head-concat-list (reverse-list x) (unit-list b))) ∙
( head-reverse-list (cons b x))))
last-element-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (last-element-list (reverse-list x)) (head-list x)
last-element-reverse-list x =
( inv (head-reverse-list (reverse-list x))) ∙
( ap head-list (reverse-reverse-list x))
tail-concat-list' :
{l1 : Level} {A : UU l1} (x y : list A) →
Id ( tail-list (concat-list x y))
( concat-list
( tail-list x)
( tail-list (concat-list (last-element-list x) y)))
tail-concat-list' nil y = refl
tail-concat-list' (cons a nil) y = refl
tail-concat-list' (cons a (cons b x)) y =
ap (cons b) (tail-concat-list' (cons b x) y)
tail-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (tail-list (reverse-list x)) (reverse-list (remove-last-element-list x))
tail-reverse-list nil = refl
tail-reverse-list (cons a nil) = refl
tail-reverse-list (cons a (cons b x)) =
( tail-concat-list' (reverse-list (cons b x)) (unit-list a)) ∙
( ( ap
( λ t → concat-list
( tail-list (reverse-list (cons b x)))
( tail-list (concat-list t (unit-list a))))
( last-element-cons' (reverse-list x) b)) ∙
( ap (λ t → concat-list t (unit-list a)) (tail-reverse-list (cons b x))))
remove-last-element-reverse-list :
{l1 : Level} {A : UU l1} (x : list A) →
Id (remove-last-element-list (reverse-list x)) (reverse-list (tail-list x))
remove-last-element-reverse-list x =
( inv (reverse-reverse-list (remove-last-element-list (reverse-list x)))) ∙
( ( inv (ap reverse-list (tail-reverse-list (reverse-list x)))) ∙
( ap (reverse-list ∘ tail-list) (reverse-reverse-list x)))
--------------------------------------------------------------------------------
map-algebra-list :
{l1 : Level} (A : UU l1) →
unit + (A × list A) → list A
map-algebra-list A (inl star) = nil
map-algebra-list A (inr (pair a x)) = cons a x
inv-map-algebra-list :
{l1 : Level} (A : UU l1) →
list A → unit + (A × list A)
inv-map-algebra-list A nil = inl star
inv-map-algebra-list A (cons a x) = inr (pair a x)
issec-inv-map-algebra-list :
{l1 : Level} (A : UU l1) →
(map-algebra-list A ∘ inv-map-algebra-list A) ~ id
issec-inv-map-algebra-list A nil = refl
issec-inv-map-algebra-list A (cons a x) = refl
isretr-inv-map-algebra-list :
{l1 : Level} (A : UU l1) →
(inv-map-algebra-list A ∘ map-algebra-list A) ~ id
isretr-inv-map-algebra-list A (inl star) = refl
isretr-inv-map-algebra-list A (inr (pair a x)) = refl
is-equiv-map-algebra-list :
{l1 : Level} (A : UU l1) → is-equiv (map-algebra-list A)
is-equiv-map-algebra-list A =
is-equiv-has-inverse
( inv-map-algebra-list A)
( issec-inv-map-algebra-list A)
( isretr-inv-map-algebra-list A)
```
### `map-list` preserves concatenation
```agda
module _
{l1 l2 : Level} {A : UU l1} {B : UU l2} (f : A → B)
where
preserves-concat-map-list :
(l k : list A) →
Id ( map-list f (concat-list l k))
( concat-list (map-list f l) (map-list f k))
preserves-concat-map-list nil k = refl
preserves-concat-map-list (cons x l) k =
ap (cons (f x)) (preserves-concat-map-list l k)
```
### If a list has an element then it is non empty
```agda
is-nonnil-elem-list :
{l : Level} {A : UU l} →
(d : has-decidable-equality A) →
(a : A) →
(l : list A) →
elem-list d a l = true →
is-nonnil-list l
is-nonnil-elem-list d a nil ()
is-nonnil-elem-list d a (cons x l) p ()
```
### If the union of two lists is empty, then these two lists are empty
```agda
is-nil-union-is-nil-list :
{l : Level} {A : UU l} →
(d : has-decidable-equality A) →
(l l' : list A) →
union-list d l l' = nil →
is-nil-list l × is-nil-list l'
is-nil-union-is-nil-list d nil l' p = (refl , p)
is-nil-union-is-nil-list d (cons x l) l' p with (elem-list d x l') in q
... | true =
ex-falso (is-nonnil-elem-list d x l' q p )
-- ( is-nonnil-elem-list d x l' q
-- (pr2 (is-nil-union-is-nil-list d l l' p)))
... | false = ex-falso (is-nonnil-cons-list x l' p) -- (is-nonnil-cons-list x (union-list d l l') p)
```
### Multiplication of a list of elements in a monoid
```agda
module _
{l : Level} (M : Monoid l)
where
mul-list-Monoid : list (type-Monoid M) → type-Monoid M
mul-list-Monoid nil = unit-Monoid M
mul-list-Monoid (cons x l) = mul-Monoid M x (mul-list-Monoid l)
distributive-mul-list-Monoid :
(l1 l2 : list (type-Monoid M)) →
Id ( mul-list-Monoid (concat-list l1 l2))
( mul-Monoid M (mul-list-Monoid l1) (mul-list-Monoid l2))
distributive-mul-list-Monoid nil l2 =
inv (left-unit-law-mul-Monoid M (mul-list-Monoid l2))
distributive-mul-list-Monoid (cons x l1) l2 =
( ap (mul-Monoid M x) (distributive-mul-list-Monoid l1 l2)) ∙
( inv (associative-mul-Monoid M x (mul-list-Monoid l1) (mul-list-Monoid l2)))
```