-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathListExtras.v
1458 lines (1295 loc) · 41.6 KB
/
ListExtras.v
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From VLSM.Lib Require Import Itauto.
From stdpp Require Import prelude finite.
From Coq Require Import FinFun.
From VLSM.Lib Require Import Preamble.
(** * Utility: Lemmas About Lists *)
(**
A list is either empty or it can be decomposed into an initial prefix
and the last element.
*)
Lemma has_last_or_null :
forall {A : Type} (l : list A),
{l' : list A & {a : A | l = l' ++ (a :: nil)}} + {l = nil}.
Proof.
destruct l.
- by right.
- by left; apply exists_last.
Qed.
(**
Decompose a list in into a prefix <<l'>> and the last element <<a>>
with an equation <<Heq>> stating that <<l = l' ++ [a]>>.
*)
Ltac destruct_list_last l l' a Heq :=
destruct (has_last_or_null l) as [[l' [a Heq]] | Heq]; rewrite Heq in *; swap 1 2.
Lemma last_not_null :
forall {A : Type} (l : list A) (a : A),
l ++ [a] <> [].
Proof.
by destruct l.
Qed.
(** Return the last element of the list if it's present and [None] otherwise. *)
Definition last_error {A : Type} (l : list A) : option A :=
match l with
| [] => None
| a :: t => Some (List.last t a)
end.
Lemma unfold_last_hd :
forall {A : Type} (random a b : A) (l : list A),
List.last (a :: b :: l) random = List.last (b :: l) random.
Proof. done. Qed.
Lemma swap_head_last :
forall {A : Type} (random a b c : A) (l : list A),
List.last (a :: b :: c :: l) random = List.last (b :: a :: c :: l) random.
Proof.
by induction l.
Qed.
Lemma remove_hd_last :
forall {A : Type} (hd1 hd2 d1 d2 : A) (tl : list A),
List.last (hd1 :: hd2 :: tl) d1 = List.last (hd2 :: tl) d2.
Proof.
induction tl; cbn; [done |].
by destruct tl.
Qed.
Lemma unroll_last :
forall {A : Type} (random a : A) (l : list A),
List.last (a :: l) random = List.last l a.
Proof.
induction l as [| h [| h' t]]; [done.. |].
by rewrite swap_head_last, unfold_last_hd, IHl, unfold_last_hd.
Qed.
Lemma last_app :
forall {A : Type} (l1 l2 : list A) (def : A),
List.last (l1 ++ l2) def = List.last l2 (List.last l1 def).
Proof.
induction l1; intros; [done |].
by rewrite <- !app_comm_cons, !unroll_last.
Qed.
Lemma last_map :
forall {A B : Type} (f : A -> B) (t : list A) (h : A) (def : B),
List.last (map f (h :: t)) def = f (List.last t h).
Proof.
induction t; intros; [done |].
by rewrite map_cons, !unroll_last.
Qed.
Lemma last_error_some :
forall {A : Type} (l : list A) (s random : A) (Herr : last_error l = Some s),
List.last l random = s.
Proof.
by destruct l; [| inversion 1; apply unroll_last].
Qed.
Lemma incl_singleton :
forall {A : Type} (l : list A) (a : A),
l ⊆ [a] -> forall b : A, b ∈ l -> b = a.
Proof.
intros * Hsub * Hin.
by apply elem_of_list_singleton, Hsub.
Qed.
Lemma Exists_first :
forall {A : Type} (P : A -> Prop) (Pdec : forall a : A, Decision (P a)) (l : list A),
Exists P l ->
exists (prefix : list A) (suffix : list A) (first : A),
P first /\
l = prefix ++ [first] ++ suffix /\
~ Exists P prefix.
Proof.
induction l as [| h t]; intros Hex; [by inversion Hex |].
destruct (decide (P h)).
- exists [], t, h.
rewrite Exists_nil.
by itauto.
- apply Exists_cons in Hex as []; [done |].
destruct (IHt H) as (prefix & suffix & first & p & -> & Hnex).
exists (h :: prefix), suffix, first.
rewrite Exists_cons.
by itauto.
Qed.
Lemma map_list_subseteq :
forall {A B : Type} (f : A -> B) (l1 l2 : list A),
l1 ⊆ l2 -> map f l1 ⊆ map f l2.
Proof.
unfold subseteq, list_subseteq.
intros * Hsub b Hin.
rewrite elem_of_list_fmap in Hin |- *.
destruct Hin as (x & -> & Hin').
exists x.
by split; [| apply Hsub].
Qed.
Lemma app_cons {A} :
forall (a : A) (l : list A),
[a] ++ l = a :: l.
Proof. done. Qed.
Lemma last_error_is_last {A} :
forall (l : list A) (x : A),
last_error (l ++ [x]) = Some x.
Proof.
intros [] x; cbn; [done |].
by rewrite last_app.
Qed.
Lemma nth_error_last :
forall {A : Type} (n : nat) (l : list A) (Hlast : S n = length l) (_last : A),
nth_error l n = Some (List.last l _last).
Proof.
intros.
destruct_list_last l h t Heq; [by destruct n |].
rewrite app_length in Hlast; cbn in Hlast.
rewrite nth_error_app2, last_app by lia; cbn.
by replace (n - length h) with 0 by lia.
Qed.
Lemma take_prefix :
forall {A : Type} (l : list A) (n1 n2 : nat),
n1 <= n2 -> take n1 (take n2 l) = take n1 l.
Proof.
by intros; rewrite take_take, min_l.
Qed.
Lemma prefix_of_take :
forall {A : Type} (l : list A) (n : nat),
take n l `prefix_of` l.
Proof. by eexists; symmetry; apply take_drop. Qed.
(**
Compute the sublist of list <<l>> which starts at index <<n1>>
and ends before index <<n2>>.
For example, <<list_segment [0; 1; 2; 3; 4; 5] 2 4 = [2; 3]>>.
*)
Definition list_segment {A : Type} (l : list A) (n1 n2 : nat) : list A :=
drop n1 (take n2 l).
Lemma take_segment_suffix :
forall {A : Type} (l : list A) (n1 n2 : nat),
n1 <= n2 -> take n1 l ++ list_segment l n1 n2 ++ drop n2 l = l.
Proof.
intros.
unfold list_segment.
rewrite <- (take_drop n2 l) at 4.
rewrite <- (take_drop n1 (take n2 l)) at 2.
by rewrite <- app_assoc, take_prefix.
Qed.
(**
Annotate each element of a list with the proof that it satisfies the
given decidable predicate.
*)
Fixpoint list_annotate
{A : Type} {P : A -> Prop} {Pdec : forall a, Decision (P a)} {l : list A}
: Forall P l -> list (dsig P) :=
match l with
| [] => fun _ => []
| h :: t => fun Hs => dexist h (Forall_inv Hs) :: list_annotate (Forall_inv_tail Hs)
end.
Section sec_list_annotate_props.
Context
{A : Type}
{P : A -> Prop}
`{Pdec : forall a, Decision (P a)}
.
Lemma list_annotate_length :
forall (l : list A) (Hs : Forall P l),
length (list_annotate Hs) = length l.
Proof.
by induction l; cbn; intros; [| rewrite IHl].
Qed.
Lemma list_annotate_pi :
forall (l : list A) (Hs : Forall P l) (Hs' : Forall P l),
list_annotate Hs = list_annotate Hs'.
Proof.
induction l; cbn; intros; [done |].
by f_equal; [apply dsig_eq | apply IHl].
Qed.
Lemma list_annotate_eq :
forall (l1 l2 : list A) (Hl1 : Forall P l1) (Hl2 : Forall P l2),
list_annotate Hl1 = list_annotate Hl2 <-> l1 = l2.
Proof.
split; [| by intros ->; apply list_annotate_pi].
revert l2 Hl1 Hl2.
induction l1 as [| h1 t1]; intros [| h2 t2] * Heq; [done.. |].
inversion Heq; subst.
by apply IHt1 in H1 as ->.
Qed.
Lemma list_annotate_app :
forall (l1 l2 : list A) (Hs : Forall P (l1 ++ l2)),
list_annotate Hs =
list_annotate (proj1 (proj1 (@Forall_app _ P l1 l2) Hs)) ++
list_annotate (proj2 (proj1 (@Forall_app _ P l1 l2) Hs)).
Proof.
induction l1; cbn; intros; [by apply list_annotate_pi |].
f_equal; [by apply dsig_eq |].
by rewrite IHl1; f_equal; apply list_annotate_pi.
Qed.
Lemma nth_error_list_annotate :
forall (n : nat) (l : list A) (Hs : Forall P l),
exists (oa : option (dsig P)),
nth_error (list_annotate Hs) n = oa /\ option_map (@proj1_sig _ _) oa = nth_error l n.
Proof.
induction n as [| n']; intros [| h t] Hs; cbn.
- by exists None.
- inversion Hs; subst.
by exists (Some (dexist h (Forall_inv Hs))).
- by exists None.
- by eauto.
Qed.
Lemma elem_of_list_annotate :
forall (l : list A) (Hs : Forall P l) (a : {x : A | bool_decide (P x)}),
a ∈ list_annotate Hs <-> `a ∈ l.
Proof.
induction l; cbn; intros.
- by rewrite !elem_of_nil.
- by rewrite !elem_of_cons, IHl, (dsig_eq P a0); cbn.
Qed.
Lemma list_annotate_NoDup :
forall (l : list A) (Hs : Forall P l),
NoDup l -> NoDup (list_annotate Hs).
Proof.
induction 1; cbn; constructor; [| by apply IHNoDup].
by rewrite elem_of_list_annotate.
Qed.
Lemma list_annotate_forget :
forall (l : list A) (Hs : Forall P l),
map proj1_sig (list_annotate Hs) = l.
Proof.
by induction l; cbn; intros; [| rewrite IHl].
Qed.
End sec_list_annotate_props.
(**
Compute the index of the <<n>>-th element of the list that satisfies the
predicate <<P>>.
*)
Fixpoint nth_error_filter_index
{A : Type} (P : A -> Prop) `{forall (x : A), Decision (P x)}
(l : list A) (n : nat) : option nat :=
match l with
| [] => None
| h :: t =>
if decide (P h) then
match n with
| 0 => Some 0
| S n' => option_map S (nth_error_filter_index P t n')
end
else
option_map S (nth_error_filter_index P t n)
end.
Lemma nth_error_filter_index_le `{forall (x : A), Decision (P x)} :
forall (l : list A) (n1 n2 in1 in2 : nat),
n1 <= n2 ->
nth_error_filter_index P l n1 = Some in1 ->
nth_error_filter_index P l n2 = Some in2 ->
in1 <= in2.
Proof.
induction l; cbn; intros * Hle Hin1 Hin2; [by inversion Hin1 |].
destruct (decide (P a)).
- destruct n1, n2.
+ by congruence.
+ by inversion Hin1; lia.
+ by lia.
+ unfold option_map in Hin1, Hin2.
destruct (nth_error_filter_index P l n1) eqn: Hin1'; inversion Hin1;
subst; clear Hin1.
destruct (nth_error_filter_index P l n2) eqn: Hin2'; inversion Hin2;
subst; clear Hin2.
by eapply le_n_S, IHl; cycle 1; [done.. | lia].
- destruct (nth_error_filter_index P l n1) eqn: Hin1'; inversion Hin1;
subst; clear Hin1.
destruct (nth_error_filter_index P l n2) eqn: Hin2'; inversion Hin2;
subst; clear Hin2.
by eapply le_n_S, IHl.
Qed.
Lemma Forall_filter :
forall {A : Type} (P : A -> Prop) {Pdec : forall a : A, Decision (P a)} (l : list A),
Forall P (filter P l).
Proof.
induction l as [| h t]; cbn; [by constructor |].
by case_decide; [constructor |].
Qed.
(**
Compute the sublist of a list that contains only elements that satisfy the
given decidable predicate. Each element of the resulting list is paired with
the proof that it satisfies the predicate.
*)
Fixpoint filter_annotate
{A : Type} (P : A -> Prop) {Pdec : forall a : A, Decision (P a)}
(l : list A) : list (dsig P) :=
match l with
| [] => []
| h :: t =>
match decide (P h) with
| left p => dexist h p :: filter_annotate P t
| right _ => filter_annotate P t
end
end.
Lemma filter_annotate_length :
forall {A : Type} (P : A -> Prop) {Pdec : forall a : A, Decision (P a)} (l : list A),
length (filter_annotate P l) = length (filter P l).
Proof.
induction l as [| h t]; cbn; [done |].
case_decide; cbn; [| done].
by rewrite IHt.
Qed.
Lemma filter_annotate_unroll :
forall {A : Type} (P : A -> Prop) {Pdec : forall a : A, Decision (P a)} (a : A) (l : list A),
filter_annotate P (a :: l) =
let fa := filter_annotate P l in
match decide (P a) with
| left pa => dexist _ pa :: fa
| _ => fa
end.
Proof. done. Qed.
Lemma filter_annotate_app :
forall {A : Type} (P : A -> Prop) {Pdec : forall a : A, Decision (P a)} (l1 l2 : list A),
filter_annotate P (l1 ++ l2) = filter_annotate P l1 ++ filter_annotate P l2.
Proof.
induction l1; cbn; intros; [done |].
case_decide; cbn; [| done].
by rewrite IHl1.
Qed.
(**
Filters a list through a predicate, then transforms each element using a
function which depends on the fact that the predicate holds.
*)
Definition list_filter_map
{A B : Type} (P : A -> Prop) {Pdec : forall a, Decision (P a)}
(f : dsig P -> B) (l : list A) : list B :=
map f (filter_annotate P l).
Lemma list_filter_map_app :
forall {A B : Type} (P : A -> Prop) {Pdec : forall a, Decision (P a)}
(f : dsig P -> B) (l1 l2 : list A),
list_filter_map P f (l1 ++ l2) = list_filter_map P f l1 ++ list_filter_map P f l2.
Proof.
by intros; unfold list_filter_map; rewrite filter_annotate_app, map_app.
Qed.
Lemma take_nth :
forall {A : Type} (i n : nat) (s : list A),
i < n -> nth_error (take n s) i = nth_error s i.
Proof.
induction i; intros n [| h t] []; cbn; [done.. | |].
- by apply IHi; lia.
- by apply IHi; lia.
Qed.
Lemma nth_error_length :
forall {A : Type} (n : nat) (l : list A) (a : A),
nth_error l n = Some a -> S n <= length l.
Proof.
induction n as [| n']; intros [| h t] a; inversion 1; cbn; [by lia |].
by eapply le_n_S, IHn'.
Qed.
Lemma take_nth_last :
forall {A : Type} (l : list A) (n : nat) (nth : A) (Hnth : nth_error l n = Some nth) (_last : A),
nth = List.last (take (S n) l) _last.
Proof.
intros.
apply Some_inj.
erewrite <- Hnth, <- nth_error_last.
- by rewrite take_nth; [done | lia].
- rewrite take_length.
by apply nth_error_length in Hnth; lia.
Qed.
Lemma drop_S_tail :
forall {A : Type} (l : list A) (n : nat),
drop (S n) l = drop n (tail l).
Proof.
by intros A [] n; cbn; [rewrite drop_nil |].
Qed.
Lemma drop_tail_comm :
forall {A : Type} (l : list A) (n : nat),
drop n (tail l) = tail (drop n l).
Proof.
intros A l n; revert l; induction n; intros l.
- by rewrite !drop_0.
- by rewrite !drop_S_tail, IHn.
Qed.
Lemma drop_lookup :
forall {A : Type} (s : list A) (n i : nat),
n <= i -> drop n s !! (i - n) = s !! i.
Proof.
intros.
rewrite lookup_drop.
by replace (n + (i - n)) with i by lia.
Qed.
Lemma drop_nth :
forall {A : Type} (i n : nat) (l : list A),
n <= i -> nth_error (drop n l) (i - n) = nth_error l i.
Proof.
induction i; intros [| n'] [| h t] Hi; cbn; try done.
- by inversion Hi.
- by apply nth_error_None; cbn; lia.
- by apply IHi; lia.
Qed.
Lemma drop_last :
forall {A : Type} (i : nat) (l : list A) (_default : A),
i < length l -> List.last (drop i l) _default = List.last l _default.
Proof.
induction i; intros [| h t] * Hlt; cbn in *; [done.. |].
rewrite IHi by lia.
destruct t; [| done].
by inversion Hlt; lia.
Qed.
Lemma list_segment_nth :
forall {A : Type} (l : list A) (n1 n2 : nat) (i : nat),
n1 <= n2 -> n1 <= i -> i < n2 ->
nth_error (list_segment l n1 n2) (i - n1) = nth_error l i.
Proof.
intros.
unfold list_segment.
rewrite drop_nth; [| done].
by apply take_nth.
Qed.
Lemma list_segment_app :
forall {A : Type} (l : list A) (n1 n2 n3 : nat),
n1 <= n2 -> n2 <= n3 ->
list_segment l n1 n2 ++ list_segment l n2 n3 = list_segment l n1 n3.
Proof.
intros.
unfold list_segment.
assert (Hle : n1 <= n3) by lia.
pose proof (Hl1 := take_segment_suffix l n1 n3 Hle).
rewrite <- (take_segment_suffix l n2 n3) in Hl1 at 4 by done.
rewrite !app_assoc in Hl1.
apply app_inv_tail in Hl1.
rewrite <- (take_drop n1 (take n2 l)), <- app_assoc, (take_prefix l n1 n2) in Hl1 by done.
by apply app_inv_head in Hl1.
Qed.
Lemma list_segment_singleton :
forall {A : Type} (l : list A) (n : nat) (a : A),
nth_error l n = Some a -> list_segment l n (S n) = [a].
Proof.
intros * Hnth.
apply nth_error_split in Hnth as (l1 & l2 & -> & <-).
unfold list_segment.
rewrite <- app_cons, app_assoc, take_app_length', drop_app_length; [done |].
by rewrite app_length; cbn; lia.
Qed.
Lemma nth_error_map :
forall {A B : Type} (f : A -> B) (l : list A) (n : nat),
nth_error (List.map f l) n = option_map f (nth_error l n).
Proof.
by induction l; intros [| n]; cbn; [.. | apply IHl].
Qed.
Lemma omap_app_rev :
forall {A B : Type} (f : A -> option B) (l : list A) (l1' l2' : list B),
omap f l = l1' ++ l2' ->
exists l1 l2 : list A, l = l1 ++ l2 /\ omap f l1 = l1' /\ omap f l2 = l2'.
Proof.
induction l; intros * Happ_rev.
- symmetry in Happ_rev.
apply app_eq_nil in Happ_rev as [Hl1' Hl2']; subst.
by exists [], [].
- cbn in Happ_rev.
destruct (f a) eqn: Hfa; cycle 1.
+ destruct (IHl _ _ Happ_rev) as (_l1 & l2 & -> & <- & <-).
by exists (a :: _l1), l2; cbn; rewrite Hfa.
+ destruct l1' as [| _b l1']; cbn in Happ_rev; inversion Happ_rev; subst.
* by exists [], (a :: l); cbn; rewrite Hfa.
* destruct (IHl _ _ H1) as (_l1 & l2 & -> & <- & <-).
by exists (a :: _l1), l2; cbn; rewrite Hfa.
Qed.
Lemma omap_length :
forall {A B : Type} (f : A -> option B) (l : list A),
Forall (fun a => f a <> None) l -> length (omap f l) = length l.
Proof.
induction 1; cbn; [done |].
by case_match; cbn; congruence.
Qed.
Lemma omap_nth :
forall {A B : Type} (f : A -> option B) (l : list A) (i : nat) (dummya : A) (dummyb : B),
Forall (fun a => f a <> None) l -> i < length l ->
Some (nth i (omap f l) dummyb) = f (nth i l dummya).
Proof.
intros * Hall; revert i.
induction Hall; cbn; intros; [by lia |].
destruct i, (f x); cbn; [done.. | | done].
by apply IHHall; lia.
Qed.
(** [omap] can be expressed as a [list_filter_map]. *)
Lemma omap_as_filter :
forall {A B : Type} (f : A -> option B) (l : list A),
omap f l = list_filter_map (is_Some ∘ f) (fun x => is_Some_proj (proj2_dsig x)) l.
Proof.
induction l; cbn; [done |].
case_decide; cbn.
- by rewrite IHl; cbv; destruct H as [? ->].
- by destruct (f a); [contradict H |].
Qed.
Lemma omap_Forall :
forall {A B : Type} (f : A -> option B) (l : list A),
Forall (fun x => ~ is_Some (f x)) l ->
omap f l = [].
Proof.
induction 1 as [| ? ? H]; cbn; [done |].
by case_match; [contradict H |].
Qed.
Lemma NoDup_omap :
forall {A B : Type} (f : A -> option B) (l : list A),
(forall a1 a2 : A, is_Some (f a1) -> is_Some (f a2) -> f a1 = f a2 -> a1 = a2) ->
NoDup l -> NoDup (omap f l).
Proof.
intros A B f l Hinj Hnd.
induction Hnd as [| h t Hnin Hnd IH]; cbn; [by constructor |].
destruct (f h) eqn: Heq; [| by apply IH].
constructor; [| by apply IH].
intros Hin; apply elem_of_list_omap in Hin as (x & Hinx & Hfx).
assert (x = h) by (apply Hinj; [done.. | congruence]).
by congruence.
Qed.
(** Unpack list of [option A] into list of [A]. *)
Definition cat_option {A : Type} : list (option A) -> list A :=
omap id.
Lemma cat_option_length :
forall {A : Type} (l : list (option A)),
Forall (fun a => a <> None) l -> length (cat_option l) = length l.
Proof.
by intros; apply omap_length.
Qed.
Lemma cat_option_length_le :
forall {A : Type} (l : list (option A)),
length (cat_option l) <= length l.
Proof.
by induction l as [| []]; cbn; [| lia..].
Qed.
Lemma cat_option_app :
forall {A : Type} (l1 l2 : list (option A)),
cat_option (l1 ++ l2) = cat_option l1 ++ cat_option l2.
Proof.
by unfold cat_option; intros; rewrite omap_app.
Qed.
Lemma cat_option_nth :
forall {A : Type} (l : list (option A)) (i : nat) (dummya : A),
Forall (fun a => a <> None) l -> i < length l ->
Some (nth i (cat_option l) dummya) = (nth i l (Some dummya)).
Proof.
by unfold cat_option; intros; erewrite omap_nth.
Qed.
Lemma nth_error_eq :
forall {A : Type} (l1 l2 : list A),
(forall n : nat, nth_error l1 n = nth_error l2 n) -> l1 = l2.
Proof.
induction l1; intros [| a2 l2] Hnth; [done | ..].
- by specialize (Hnth 0); inversion Hnth.
- by specialize (Hnth 0); inversion Hnth.
- f_equal.
+ by specialize (Hnth 0); cbn in Hnth; congruence.
+ apply IHl1; intros n.
by specialize (Hnth (S n)); cbn in Hnth.
Qed.
(**
Compute the list of all decompositions of the given list <<l>> into
triples <<(l1, x, l2)>> such that <<l = l1 ++ x :: l2>>.
*)
Fixpoint one_element_decompositions {A : Type} (l : list A) : list (list A * A * list A) :=
match l with
| [] => []
| a :: l' =>
([], a, l')
:: map
(fun t => match t with (l1, b, l2) => (a :: l1, b, l2) end)
(one_element_decompositions l')
end.
Lemma elem_of_one_element_decompositions :
forall {A : Type} (l pre suf : list A) (x : A),
(pre, x, suf) ∈ one_element_decompositions l
<->
pre ++ [x] ++ suf = l.
Proof.
induction l; split; cbn; intros Hin.
- by inversion Hin.
- by destruct pre; inversion Hin.
- rewrite elem_of_cons, elem_of_list_fmap in Hin.
destruct Hin as [[= -> -> ->] | (((pre', x'), suf') & [= -> -> ->] & Hin)]; [done |].
by apply (IHl pre' suf' x') in Hin; subst.
- destruct pre; inversion Hin; subst; clear Hin; [by left |].
right.
apply elem_of_list_fmap.
exists (pre, x, suf).
by rewrite IHl.
Qed.
(**
Compute the list of all decompositions of the given list <<l>> into
tuples <<(l1, x, l2, y, l3)>> such that <<l = l1 ++ x :: l2 ++ y :: l3>>.
*)
Definition two_element_decompositions
{A : Type} (l : list A) : list (list A * A * list A * A * list A) :=
flat_map
(fun t =>
match t with
(l1, e1, l2) =>
map
(fun t => match t with (l2', e2, l3) => (l1, e1, l2', e2, l3) end)
(one_element_decompositions l2)
end)
(one_element_decompositions l).
Lemma elem_of_two_element_decompositions :
forall {A : Type} (l pre mid suf : list A) (x y : A),
(pre, x, mid, y, suf) ∈ two_element_decompositions l
<->
pre ++ [x] ++ mid ++ [y] ++ suf = l.
Proof.
intros.
unfold two_element_decompositions.
rewrite elem_of_list_In, in_flat_map; setoid_rewrite <- elem_of_list_In.
split.
- intros [((pre', x'), sufx) [Hdecx Hin]].
apply elem_of_list_fmap in Hin as [[[mid' y'] suf'] [[= -> -> -> -> ->] Hin]].
by apply elem_of_one_element_decompositions in Hdecx as <-, Hin as <-.
- intros H.
apply elem_of_one_element_decompositions in H.
exists (pre, x, mid ++ [y] ++ suf).
split; [done |].
apply elem_of_list_fmap.
exists (mid, y, suf).
by rewrite elem_of_one_element_decompositions.
Qed.
Lemma order_decompositions :
forall {A : Type} (pre1 suf1 pre2 suf2 : list A),
pre1 ++ suf1 = pre2 ++ suf2 ->
pre1 = pre2 \/ (exists suf1', pre1 = pre2 ++ suf1') \/ (exists suf2', pre2 = pre1 ++ suf2').
Proof.
induction pre1; destruct pre2; cbn; [by eauto.. |].
inversion 1; subst.
by edestruct IHpre1 as [| [[] | []]]; [done | subst; eauto..].
Qed.
Lemma list_max_exists :
forall (l : list nat),
l <> [] -> list_max l ∈ l.
Proof.
induction l as [| h t]; cbn; intros; [done |].
destruct (PeanoNat.Nat.max_spec h (foldr Init.Nat.max 0 t))
as [[Hlt ->] | [Hle ->]]; [| by left].
right; apply IHt.
by destruct t; [cbn in Hlt; lia |].
Qed.
(**
Returns all values which occur with maximum frequency in the given list.
Note that these values are returned with their original multiplicity.
*)
Definition mode `{EqDecision A} (l : list A) : list A :=
let mode_value := list_max (List.map (count_occ decide_eq l) l) in
filter (fun a => (count_occ decide_eq l a) = mode_value) l.
Example mode1 : mode [1; 1; 2; 3; 3] = [1; 1; 3; 3].
Proof. by itauto. Qed.
(**
Computes the list <<suff>> which satisfies <<pref ++ suff = l>> or
reports that no such list exists.
*)
Fixpoint complete_prefix `{EqDecision A} (l pref : list A) : option (list A) :=
match l, pref with
| l , [] => Some l
| [], b :: pref' => None
| a :: l', b :: pref' => if decide (a = b) then complete_prefix l' pref' else None
end.
Example complete_prefix_some : complete_prefix [1; 2; 3; 4] [1; 2] = Some [3; 4].
Proof. by itauto. Qed.
Example complete_prefix_none : complete_prefix [1; 2; 3; 4] [1; 3] = None.
Proof. by itauto. Qed.
Lemma complete_prefix_empty `{EqDecision A} :
forall (l : list A),
complete_prefix l [] = Some l.
Proof.
by induction l.
Qed.
Lemma complete_prefix_correct `{EqDecision A} :
forall (l pref suff : list A),
l = pref ++ suff <-> complete_prefix l pref = Some suff.
Proof.
induction l; intros [] suff; cbn; [by itauto congruence.. |].
case_decide; [| by itauto congruence].
by rewrite <- IHl; itauto congruence.
Qed.
(**
Computes the list <<pref>> which satisfies <<pref ++ suff = l>> or
reports that no such list exists.
*)
Definition complete_suffix `{EqDecision A} (l suff : list A) : option (list A) :=
match complete_prefix (rev l) (rev suff) with
| None => None
| Some ls => Some (rev ls)
end.
Example complete_suffix_some : complete_suffix [1; 2; 3; 4] [3; 4] = Some [1; 2].
Proof. by itauto. Qed.
Lemma complete_suffix_correct `{EqDecision A} :
forall (l pref suff : list A),
l = pref ++ suff <-> complete_suffix l suff = Some pref.
Proof.
split; unfold complete_suffix.
- intros ->.
replace (complete_prefix _ _) with (Some (rev pref)).
+ by rewrite rev_involutive.
+ symmetry; apply complete_prefix_correct.
by rewrite rev_app_distr.
- destruct (complete_prefix (rev l) (rev suff)) eqn: Heq; intros [= <-].
apply complete_prefix_correct in Heq.
apply rev_eq_app in Heq.
by rewrite rev_involutive in Heq.
Qed.
Lemma complete_suffix_empty `{EqDecision A} :
forall (l : list A),
complete_suffix l [] = Some l.
Proof.
unfold complete_suffix; cbn; intros.
by rewrite complete_prefix_empty, rev_involutive.
Qed.
(** Keep elements which are [inl] but throw away those that are [inr]. *)
Definition list_sum_project_left {A B : Type} (x : list (A + B)) : list A :=
omap sum_project_left x.
(** Keep elements which are [inr] but throw away those that are [inl]. *)
Definition list_sum_project_right {A B : Type} (x : list (A + B)) : list B :=
omap sum_project_right x.
Lemma fold_right_andb_false :
forall (l : list bool),
fold_right andb false l = false.
Proof.
induction l; cbn; [done |].
by rewrite IHl, andb_false_r.
Qed.
Lemma sumbool_forall :
forall {A : Type} {P Q : A -> Prop},
(forall x : A, {P x} + {Q x}) ->
forall l : list A, {Forall P l} + {Exists Q l}.
Proof.
intros A P Q Hdec.
induction l; [by left; constructor |].
destruct (Hdec a); [| by right; constructor].
by destruct IHl; [left | right]; constructor.
Qed.
Lemma list_sum_map :
forall {A : Type} (f g : A -> nat) (l : list A),
(forall x : A, x ∈ l -> f x <= g x) ->
list_sum (map f l) <= list_sum (map g l).
Proof.
induction l as [| h t]; cbn; intros Hle; [done |].
apply PeanoNat.Nat.add_le_mono.
- by apply Hle; left.
- apply IHt; intros x Hin.
by apply Hle; right.
Qed.
Lemma list_sum_decrease :
forall {A : Type} (f g : A -> nat) (l : list A),
(forall a, a ∈ l -> f a <= g a) ->
Exists (fun a => f a < g a) l ->
list_sum (map f l) < list_sum (map g l).
Proof.
intros A f g l Hall Hex.
induction Hex; cbn.
- apply PeanoNat.Nat.add_lt_le_mono; [done |].
apply list_sum_map.
intros a' Hin.
by apply Hall; right.
- apply PeanoNat.Nat.add_le_lt_mono; [by apply Hall; left |].
apply IHHex.
intros a Hin.
by apply Hall; right.
Qed.
(**
Nearly the natural induction principle for [fold_left].
Useful if you can think of [fold_left f] as transforming
a list into a [B -> B] function, and can describe the
effect with a [P : list A -> relation B].
The assumption [Hstep] could be weakened by replacing [r]
with [fold_left f l (f x a)], but that isn't useful in
natural examples.
*)
Lemma fold_left_ind :
forall {A B : Type} (f : B -> A -> B) (P : list A -> B -> B -> Prop)
(Hstart : forall x, P nil x x)
(Hstep : forall x a l r, P l (f x a) r -> P (a :: l) x r),
forall l x, P l x (fold_left f l x).
Proof.
induction l.
- by apply Hstart.
- by intros x; apply Hstep, IHl.
Qed.
(**
An induction principle for [fold_left] which
decomposes the list from the right.
*)
Lemma fold_left_ind_rev :
forall {A B : Type} (f : B -> A -> B) (x0 : B) (P : list A -> B -> Prop)
(Hstart : P nil x0)
(Hstep : forall l a x, P l x -> P (l ++ [a]) (f x a)),
forall l, P l (fold_left f l x0).
Proof.
induction l using rev_ind.
- by apply Hstart.
- by rewrite fold_left_app; apply Hstep.
Qed.
Section sec_suffix_quantifiers.
(** ** Quantifiers for all suffixes
In this section we define inductive quantifiers for lists that are concerned
with predicates over the sublists of the list instead of the elements. They
are analogous to [Streams.ForAll] and [Streams.Exists]. We prove several
properties about them.
Among the definitions, the more useful are [ForAllSuffix2] and [ExistsSuffix2]
as they allow us to quantify over relations between consecutive elements.
*)
Context
[A : Type]
(P : list A -> Prop)
.
Inductive ExistsSuffix : list A -> Prop :=
| SHere : forall l, P l -> ExistsSuffix l
| SFurther : forall a l, ExistsSuffix l -> ExistsSuffix (a :: l).
Inductive ForAllSuffix : list A -> Prop :=
| SNil : P [] -> ForAllSuffix []
| SHereAndFurther : forall a l, P (a :: l) -> ForAllSuffix l -> ForAllSuffix (a :: l).
Lemma fsHere :
forall (l : list A),
ForAllSuffix l -> P l.
Proof. by inversion 1. Qed.
Lemma fsFurther :
forall (a : A) (l : list A),
ForAllSuffix (a :: l) -> ForAllSuffix l.
Proof. by inversion 1. Qed.
Lemma ForAll_drop :
forall (m : nat) (l : list A),
ForAllSuffix l -> ForAllSuffix (drop m l).
Proof.
induction m; intros [] Hall; cbn; [done.. |].
by apply fsFurther in Hall; apply IHm.
Qed.
Lemma ForAllSuffix_induction :
forall (Inv : list A -> Prop)
(InvThenP : forall l, Inv l -> P l)
(InvIsStable : forall a l, Inv (a :: l) -> Inv l),
forall l, Inv l -> ForAllSuffix l.
Proof.
induction l; intros.
- by constructor; apply InvThenP.
- constructor.
+ by apply InvThenP.
+ by eapply IHl, InvIsStable.
Qed.
End sec_suffix_quantifiers.
Lemma ForAllSuffix_subsumption :
forall {A : Type} (P Q : list A -> Prop) (HPQ : forall l, P l -> Q l),
forall (l : list A), ForAllSuffix P l -> ForAllSuffix Q l.
Proof.
by induction 2; constructor; auto.
Qed.
Definition ForAllSuffix2 [A : Type] (R : A -> A -> Prop) : list A -> Prop :=
ForAllSuffix (fun l => match l with | a :: b :: _ => R a b | _ => True end).
Lemma fsFurther2_transitive :
forall {A : Type} (R : A -> A -> Prop) {HT : Transitive R} (a b : A) (l : list A),
ForAllSuffix2 R (a :: b :: l) -> ForAllSuffix2 R (a :: l).
Proof.
inversion 2; subst.
destruct l.
- by repeat constructor.
- inversion H3; subst.
by constructor; [transitivity b |].
Qed.
Lemma ForAllSuffix2_filter :
forall {A : Type} (R : A -> A -> Prop) {HT : Transitive R}
(P : A -> Prop) {Pdec : forall a, Decision (P a)},
forall l, ForAllSuffix2 R l -> ForAllSuffix2 R (filter P l).
Proof.
induction l; intros Hl; [done |].
unfold filter; cbn.
spec IHl; [by apply fsFurther in Hl |].
case_decide; [| done].
constructor; [| done].
clear IHl H; induction l; cbn; [done |].
case_decide.
- by inversion Hl.
- by apply IHl; eapply fsFurther2_transitive.
Qed.
Lemma list_subseteq_tran :
forall (A : Type) (l m n : list A),
l ⊆ m -> m ⊆ n -> l ⊆ n.
Proof.