-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoreList.v
1343 lines (1133 loc) · 36.2 KB
/
MoreList.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 Coq Require Export Arith Lia List Bool Permutation.
Require Import MoreFun.
Import Basics ListNotations.
(** Some complements on Coq lists *)
Lemma nil_carac {A} (l:list A) : l = [] <-> forall x, ~In x l.
Proof.
split.
- now intros ->.
- destruct l; trivial. simpl. intros H. specialize (H a); intuition.
Qed.
Lemma rev_switch {A} (l l' : list A) : rev l = l' -> l = rev l'.
Proof.
intros. now rewrite <- (rev_involutive l), H.
Qed.
Lemma rev_inj {A} (l l' : list A) : rev l = rev l' -> l = l'.
Proof.
intro H. now rewrite <- (rev_involutive l), H, rev_involutive.
Qed.
Lemma rev_repeat {A} (a:A) n : rev (repeat a n) = repeat a n.
Proof.
induction n; trivial.
rewrite <- Nat.add_1_r at 1. rewrite repeat_app, rev_app_distr.
simpl. now f_equal.
Qed.
Lemma app_inv {A} (u u' v v':list A) :
length u = length v -> u++u' = v++v' -> u=v /\ u'=v'.
Proof.
revert v. induction u; destruct v; simpl; try easy.
intros [= E] [= <- E']. apply IHu in E'; trivial. intuition congruence.
Qed.
Lemma app_inv' {A} (u u' v v' : list A) :
length u' = length v' -> u ++ u' = v ++ v' -> u = v /\ u' = v'.
Proof.
intros L E. apply app_inv; trivial.
apply (f_equal (@length A)) in E. rewrite !app_length in E. lia.
Qed.
Lemma split_inv {A} (x:A) u u' v v' :
~In x u -> ~In x u' -> u++x::v = u'++x::v' -> u=u' /\ v=v'.
Proof.
revert u' v v'.
induction u as [|y u IH]; intros [|y' u'] v v'.
- intros _ _. simpl. split; auto. congruence.
- intros _ NI. simpl in *. intros [= <- _]. destruct NI. now left.
- intros NI _. simpl in *. intros [= -> _]. destruct NI. now left.
- intros NI NI'. simpl in *. intros [= <- E]. apply IH in E; try tauto.
split; now f_equal.
Qed.
Lemma last_nth {A}(l : list A) d :
last l d = nth (length l - 1) l d.
Proof.
induction l as [|x [|y l] IH]; simpl; auto.
destruct l; simpl; auto.
Qed.
Lemma last_cons {A} (a:A) l d : l<>[] -> last (a::l) d = last l d.
Proof.
now destruct l.
Qed.
Lemma last_app {A} u v (d:A) : v<>[] -> last (u++v) d = last v d.
Proof.
intros Hv.
assert (Hv' : length v <> 0) by now destruct v.
rewrite !last_nth, app_length, app_nth2 by lia. f_equal; lia.
Qed.
Lemma last_seq a n d : last (seq a (S n)) d = a+n.
Proof.
rewrite seq_S. apply last_last.
Qed.
Lemma nth_map_indep {A B}(f : A -> B) l n d d' :
n < length l -> nth n (map f l) d = f (nth n l d').
Proof.
intros L. rewrite nth_indep with (d':=f d') by now rewrite map_length.
apply map_nth.
Qed.
Lemma seq_S a b : List.seq a (S b) = List.seq a b ++ [a+b].
Proof.
revert a.
induction b; simpl; intros. f_equal; lia.
rewrite Nat.add_succ_r, <- Nat.add_succ_l. now rewrite <- IHb.
Qed.
Lemma map_add_seq len start n :
map (Nat.add n) (seq start len) = seq (n+start) len.
Proof.
revert start.
induction n; simpl; intros.
- now rewrite map_id.
- now rewrite <- seq_shift, <- IHn, map_map.
Qed.
Lemma map_repeat {A B}(f : A -> B) a n :
map f (repeat a n) = repeat (f a) n.
Proof.
induction n; simpl; f_equal; auto.
Qed.
(** Insertion at the right spot in a sorted [nat list] *)
Fixpoint insert (x : nat) (l : list nat) :=
match l with
| [] => [x]
| y::l' => if x <=? y then x::l else y::insert x l'
end.
Lemma insert_0 l : insert 0 l = 0 :: l.
Proof.
induction l; simpl; auto.
Qed.
Lemma map_pred_insert x l:
map Nat.pred (insert x l) = insert (Nat.pred x) (map Nat.pred l).
Proof.
induction l; simpl; auto.
do 2 case Nat.leb_spec; intros; try lia; simpl; auto.
- replace a with 0 in * by lia.
replace x with 1 in * by lia. simpl. f_equal.
rewrite IHl. simpl. apply insert_0.
- f_equal; auto.
Qed.
Lemma insert_in x l y : In y (insert x l) <-> x = y \/ In y l.
Proof.
induction l; simpl; auto.
- intuition.
- case Nat.leb; simpl; rewrite ?IHl; intuition.
Qed.
(** More on count_occ *)
Lemma count_occ_seq n x :
count_occ Nat.eq_dec (seq 0 n) x = if x <? n then 1 else 0.
Proof.
induction n; auto.
rewrite seq_S, count_occ_app, IHn; simpl.
do 2 case Nat.ltb_spec; destruct Nat.eq_dec; lia.
Qed.
Lemma count_occ_repeat [A](dec: forall x y : A, {x = y} + {x <> y}) x n y :
count_occ dec (repeat x n) y = if dec x y then n else 0.
Proof.
induction n; simpl; destruct dec; simpl; congruence.
Qed.
Lemma count_occ_remove [A](dec: forall x y : A, {x = y} + {x <> y}) l x y :
count_occ dec (remove dec x l) y =
if dec x y then 0 else count_occ dec l y.
Proof.
induction l; repeat (simpl; destruct dec); congruence.
Qed.
(** More on filter *)
Lemma filter_nop {A} (f:A->bool) l :
(forall x, In x l -> f x = false) -> filter f l = [].
Proof.
induction l; simpl; intros H; auto.
rewrite H; firstorder.
Qed.
Lemma filter_all {A} (f:A->bool) l :
(forall x, In x l -> f x = true) -> filter f l = l.
Proof.
induction l; simpl; intros H; auto.
rewrite H; firstorder. f_equal. firstorder.
Qed.
Lemma map_filter {A B} (f:A->B)(h:B->bool) l :
filter h (map f l) = map f (filter (compose h f) l).
Proof.
induction l; simpl; auto. unfold compose.
destruct (h (f a)); simpl; f_equal; auto.
Qed.
Lemma filter_length_le {A} (f:A->bool) l :
length (filter f l) <= length l.
Proof.
induction l; simpl; auto. destruct (f a); simpl; lia.
Qed.
(** More on flat_map *)
Lemma flat_map_length {A B} (f:A->list B) l k :
(forall a, In a l -> length (f a) = k) ->
length (flat_map f l) = k * length l.
Proof.
intros H.
induction l as [|x l IH]; simpl. lia.
rewrite app_length, IH. rewrite H; simpl; auto. lia. simpl in *. auto.
Qed.
Lemma map_flatmap {A B C} (f:B->C)(g:A -> list B) l :
map f (flat_map g l) = flat_map (fun x => map f (g x)) l.
Proof.
induction l; simpl; auto. rewrite map_app. now f_equal.
Qed.
Lemma flatmap_map {A B C} (f:A->B)(g:B -> list C) l :
flat_map g (map f l) = flat_map (compose g f) l.
Proof.
rewrite !flat_map_concat_map. f_equal. apply map_map.
Qed.
(** More on Coq Permutation predicate *)
Lemma eq_Permutation {A} (l l':list A) : l = l' -> Permutation l l'.
Proof.
intros <-. apply Permutation_refl.
Qed.
Lemma Permutation_sym_iff {A} (l:list A) l' :
Permutation l l' <-> Permutation l' l.
Proof.
split; apply Permutation_sym.
Qed.
Lemma Permutation_filter {A} (f: A -> bool) l l' :
Permutation l l' -> Permutation (filter f l) (filter f l').
Proof.
induction 1; simpl; try constructor.
- destruct (f x); auto.
- destruct (f x), (f y); auto. constructor.
- econstructor; eauto.
Qed.
(** More about [list_sum], the sum of a [nat list] *)
Lemma list_sum_cons x l : list_sum (x::l) = x + list_sum l.
Proof.
reflexivity.
Qed.
Lemma list_sum_rev l : list_sum (rev l) = list_sum l.
Proof.
induction l; simpl; auto.
rewrite list_sum_app, IHl. simpl; lia.
Qed.
Lemma length_concat {A} (l:list (list A)) :
length (concat l) = list_sum (map (@length _) l).
Proof.
induction l; simpl; trivial.
rewrite app_length. now f_equal.
Qed.
(** index : first position of a value in a list.
Returns the length of the list if the element is not in the list. *)
Fixpoint index x (l:list nat) :=
match l with
| [] => 0
| y::l' => if x =? y then 0 else S (index x l')
end.
Lemma nth_index x l : In x l -> nth (index x l) l 0 = x.
Proof.
induction l.
- inversion 1.
- simpl. intros [->|IN].
+ now rewrite Nat.eqb_refl.
+ case Nat.eqb_spec; auto.
Qed.
Lemma index_nth n l :
NoDup l -> n < length l -> index (nth n l 0) l = n.
Proof.
revert n.
induction l.
- inversion 2.
- destruct n; simpl.
+ now rewrite Nat.eqb_refl.
+ inversion_clear 1. intros H. rewrite IHl by (trivial;lia).
case Nat.eqb_spec; trivial.
intros E. destruct H0. rewrite <- E. apply nth_In. lia.
Qed.
Lemma index_lt_len x l : In x l -> index x l < length l.
Proof.
induction l.
- inversion 1.
- simpl. intros [->|IN].
+ rewrite Nat.eqb_refl. lia.
+ case Nat.eqb_spec; intros. lia. auto with *.
Qed.
Lemma index_notin x l : ~In x l -> index x l = length l.
Proof.
induction l; simpl. easy. case Nat.eqb_spec; intuition lia.
Qed.
(** Inserting a value at some position in a list *)
Fixpoint insert_at {A} n x (l:list A) :=
match n, l with
| O, _ => x::l
| S n, a::l => a::insert_at n x l
| _, [] => [x]
end.
Lemma remove_insert n x l :
~In x l -> remove Nat.eq_dec x (insert_at n x l) = l.
Proof.
revert l.
induction n.
- intros l NI. simpl. destruct Nat.eq_dec; try lia.
apply notin_remove; intuition.
- intros [|a l].
+ intros _. simpl. destruct Nat.eq_dec; try lia; auto.
+ intros NI. simpl in *. destruct Nat.eq_dec; subst; intuition.
f_equal. now apply IHn.
Qed.
Lemma insert_permut {A} n x (l:list A) :
Permutation (insert_at n x l) (x::l).
Proof.
revert l.
induction n; simpl; auto. destruct l; simpl; auto.
apply perm_trans with (a::x::l); auto. constructor.
Qed.
Lemma insert_length {A} n x (l:list A) :
length (insert_at n x l) = S (length l).
Proof.
change (S (length l)) with (length (x::l)).
apply Permutation_length, insert_permut.
Qed.
Lemma insert_at_in {A} n x y (l:list A) :
In y (insert_at n x l) <-> In y (x :: l).
Proof.
split; apply Permutation_in; auto using Permutation_sym, insert_permut.
Qed.
Lemma nth_insert_at {A} n k x (l:list A) d : n <= length l ->
nth k (insert_at n x l) d =
match Nat.compare k n with
| Lt => nth k l d
| Eq => x
| Gt => nth (k-1) l d
end.
Proof.
revert k l.
induction n; simpl; intros k l Hn.
- case Nat.compare_spec; intros Hk; subst; auto.
inversion Hk.
destruct k; try lia. f_equal. lia.
- destruct l; simpl in *; try lia.
destruct k; auto. rewrite IHn by lia. simpl Nat.compare.
case Nat.compare_spec; intros Hk; subst; auto.
simpl. rewrite Nat.sub_0_r. destruct k; try lia. f_equal. lia.
Qed.
Lemma insert_at_perm {A} (l:list A) i x :
Permutation (insert_at i x l) (x::l).
Proof.
revert i. induction l; destruct i; simpl; try easy.
rewrite perm_swap. now apply perm_skip.
Qed.
(** remove_at : dual of insert_at *)
Fixpoint remove_at {A} i (l:list A) :=
match l, i with
| [], _ => []
| x::l, 0 => l
| x::l, S i => x :: remove_at i l
end.
Lemma remove_at_nth {A} (l:list A) (d:A) i j :
nth j (remove_at i l) d = if j <? i then nth j l d else nth (S j) l d.
Proof.
revert i j. induction l; destruct i, j; simpl; trivial.
- now case Nat.ltb.
- apply (IHl i j).
Qed.
Lemma remove_at_length {A} (l:list A) i :
(i < length l)%nat -> length (remove_at i l) = pred (length l).
Proof.
revert i. induction l; destruct i; simpl; intros; trivial. rewrite IHl; lia.
Qed.
Lemma insert_at_remove_at {A} (l:list A) i (d:A) :
(i < length l)%nat ->
insert_at i (nth i l d) (remove_at i l) = l.
Proof.
revert i. induction l; destruct i; simpl; trivial; try lia.
intros Hi. f_equal. apply IHl. lia.
Qed.
Lemma remove_at_notIn {A} (l:list A) i d :
NoDup l -> i < length l -> ~In (nth i l d) (remove_at i l).
Proof.
intros Hl Hi IN.
destruct (In_nth _ _ d IN) as (j & Hj & E).
rewrite remove_at_nth in E.
rewrite NoDup_nth with (d:=d) in Hl.
rewrite remove_at_length in Hj by trivial.
destruct (Nat.ltb_spec j i).
- specialize (Hl j i lia Hi E). lia.
- specialize (Hl (S j) i lia Hi E). lia.
Qed.
Lemma remove_at_In {A} (l:list A) i j d :
i < length l -> j < length l -> i<>j -> In (nth j l d) (remove_at i l).
Proof.
intros Hi Hj Hij.
destruct (Nat.ltb j i) eqn:E.
- assert (H := remove_at_nth l d i j). rewrite E in H.
rewrite <- H. apply nth_In.
apply Nat.ltb_lt in E.
rewrite remove_at_length; trivial. lia.
- rewrite Nat.ltb_nlt, Nat.nlt_ge in E.
assert (H := remove_at_nth l d i (j-1)).
destruct (Nat.ltb_spec (j-1) i); try lia.
replace (S (j-1)) with j in H by lia.
rewrite <- H. apply nth_In.
rewrite remove_at_length; trivial. lia.
Qed.
(** Lists with empty intersection *)
Definition EmptyInter {A} (u v : list A) := forall a, ~(In a u /\ In a v).
Lemma app_nodup {A} (l l':list A) :
NoDup l -> NoDup l' -> EmptyInter l l' -> NoDup (l++l').
Proof.
revert l'.
induction l as [|x l IH]; simpl; trivial.
intros l'. inversion_clear 1. intros Hl' EI. constructor.
- specialize (EI x). simpl in EI. rewrite in_app_iff. intuition.
- apply IH; auto. intros y. specialize (EI y). simpl in EI. intuition.
Qed.
Lemma flat_map_nodup {A B} (f:A -> list B) l :
(forall a, In a l -> NoDup (f a)) ->
(forall a a', In a l -> In a' l -> a<>a' -> EmptyInter (f a) (f a')) ->
NoDup l ->
NoDup (flat_map f l).
Proof.
induction l as [|x l IH]; simpl; intros H1 H2 H3.
- constructor.
- inversion_clear H3.
apply app_nodup; auto.
intros y (Hy1,Hy2). rewrite in_flat_map in Hy2.
destruct Hy2 as (x' & IN & IN').
refine (H2 x x' _ _ _ y _); auto; congruence.
Qed.
(** In a list, moving all the occurrences of a value at front. *)
Definition movefront [A](dec : forall x y : A, {x = y} + {x <> y}) x l :=
repeat x (count_occ dec l x) ++ remove dec x l.
Lemma movefront_perm [A](dec : forall x y : A, {x = y} + {x <> y}) x l :
Permutation l (movefront dec x l).
Proof.
rewrite (Permutation_count_occ dec). intros y. unfold movefront.
rewrite count_occ_app, count_occ_remove, count_occ_repeat.
destruct dec; subst; lia.
Qed.
(** Upper bound of the elements of a list *)
Fixpoint minlist a l :=
match l with
| [] => a
| b::l => Nat.min b (minlist a l)
end.
Fixpoint maxlist a l :=
match l with
| [] => a
| b::l => Nat.max b (maxlist a l)
end.
Definition extrems l :=
match l with
| [] => (0,0)
| a::l => (minlist a l, maxlist a l)
end.
Lemma minlist_spec a l x : In x (a::l) -> minlist a l <= x.
Proof.
induction l as [|b l IH].
- intros [->|[ ]]. simpl. auto.
- simpl in *. intuition; lia.
Qed.
Lemma minlist_in a l : In (minlist a l) (a::l).
Proof.
induction l as [|b l IH].
- simpl; now left.
- simpl minlist.
destruct (Nat.min_spec b (minlist a l)) as [(_,->)|(_,->)];
simpl in *; intuition.
Qed.
Lemma maxlist_spec a l x : In x (a::l) -> x <= maxlist a l.
Proof.
induction l as [|b l IH].
- intros [->|[ ]]. simpl. auto.
- simpl in *. intuition; lia.
Qed.
Lemma maxlist0_above l n : In n l -> n <= maxlist 0 l.
Proof.
revert n.
induction l; inversion 1; simpl; subst. apply Nat.le_max_l.
transitivity (maxlist 0 l); auto. apply Nat.le_max_r.
Qed.
Lemma maxlist_in a l : In (maxlist a l) (a::l).
Proof.
induction l as [|b l IH].
- simpl; now left.
- simpl maxlist.
destruct (Nat.max_spec b (maxlist a l)) as [(_,->)|(_,->)];
simpl in *; intuition.
Qed.
Lemma extrems_spec l a b x : In x l -> extrems l = (a,b) -> a<=x<=b.
Proof.
intros IN.
destruct l as [|n l]; try easy.
simpl. intros [= <- <-]. split.
now apply minlist_spec. now apply maxlist_spec.
Qed.
Lemma extrems_in1 l : l<>[] -> In (fst (extrems l)) l.
Proof.
destruct l. easy. intros _. simpl fst. apply minlist_in.
Qed.
Lemma extrems_in2 l : l<>[] -> In (snd (extrems l)) l.
Proof.
destruct l. easy. intros _. simpl fst. apply maxlist_in.
Qed.
(** Decreasing all elements of a list *)
Definition decr x y := Nat.sub y x.
Lemma decr_0 x : decr 0 x = x.
Proof.
apply Nat.sub_0_r.
Qed.
Lemma map_decr_0 l : map (decr 0) l = l.
Proof.
rewrite <- (map_id l) at 2. apply map_ext, decr_0.
Qed.
Lemma map_decr_1 l : map (decr 1) l = map pred l.
Proof.
apply map_ext. intros; unfold decr. lia.
Qed.
Lemma map_decr_decr k k' l :
map (decr k) (map (decr k') l) = map (decr (k+k')) l.
Proof.
rewrite map_map. apply map_ext. unfold decr; lia.
Qed.
Lemma map_decr_S k l :
map (decr (S k)) l = map (decr k) (map pred l).
Proof.
now rewrite <- map_decr_1, map_decr_decr, Nat.add_1_r.
Qed.
Lemma map_decr_S' k l :
map (decr (S k)) l = map pred (map (decr k) l).
Proof.
now rewrite <- map_decr_1, map_decr_decr.
Qed.
(** take : the list of the n first elements of a infinite sequence
(given as a function over nat). A.k.a List.init in OCaml. *)
Definition take {A} n (f:nat -> A) : list A := map f (seq 0 n).
Notation list_init := take (only parsing).
Lemma take_S {A} n (f:nat -> A) : take (S n) f = take n f ++ [f n].
Proof.
unfold take. now rewrite seq_S, map_app.
Qed.
Lemma take_add {A} a b (f:nat->A) :
take (a+b) f = take a f ++ map f (seq a b).
Proof.
unfold take. now rewrite seq_app, map_app.
Qed.
Lemma take_S_shift {A} n (f:nat -> A) :
take (S n) f = f 0 :: take n (fun n => f (S n)).
Proof.
cbn. f_equal. now rewrite <- seq_shift, map_map.
Qed.
Lemma take_length {A} n (f:nat -> A) : length (take n f) = n.
Proof.
unfold take. now rewrite map_length, seq_length.
Qed.
Lemma take_nth {A} f n m (a:A) : m < n -> nth m (take n f) a = f m.
Proof.
intros L. unfold take.
rewrite nth_map_indep with (d':=0) by now rewrite seq_length.
f_equal. now apply seq_nth.
Qed.
Lemma in_take {A} (f:nat->A) n x :
In x (take n f) <-> exists i, f i = x /\ i < n.
Proof.
unfold take. rewrite in_map_iff. setoid_rewrite in_seq. firstorder lia.
Qed.
Lemma take_carac {A} n (u:list A) (f:nat->A) :
length u = n ->
(forall m a, m<n -> nth m u a = f m) ->
take n f = u.
Proof.
revert u f.
induction n.
- destruct u; simpl; easy.
- intros u f Hu H. rewrite take_S.
destruct (rev u) as [|a ru] eqn:E.
+ apply rev_switch in E. now subst u.
+ apply rev_switch in E. simpl in E. subst u.
rewrite app_length in Hu. simpl in Hu.
f_equal.
* apply IHn; try lia.
intros m b Hm.
specialize (H m b). rewrite app_nth1 in H; auto; lia.
* f_equal.
specialize (H n a). rewrite app_nth2 in H; auto; try lia.
replace (n - length (rev ru)) with 0 in H by lia. simpl in H.
symmetry. apply H; auto.
Qed.
Lemma cumul_alt f n : cumul f n = list_sum (take n f).
Proof.
induction n. trivial. unfold take in *.
rewrite seq_S, map_app, list_sum_app, <- IHn. simpl. lia.
Qed.
(** Counting values in [list nat] *)
Fixpoint nbocc (a:nat) (l:list nat) :=
match l with
| [] => 0
| b::l' => nbocc a l' + if b =? a then 1 else 0
end.
(** [nbocc] is similar to [count_occ Nat.eq_dec], but more convenient
(order of arguments, Nat.eqb instead of Nat.eq_dec) *)
Lemma nbocc_alt a l : nbocc a l = count_occ Nat.eq_dec l a.
Proof.
induction l; simpl; auto. rewrite IHl.
case Nat.eq_dec; case Nat.eqb_spec; lia.
Qed.
Lemma nbocc_app a u v : nbocc a (u++v) = nbocc a u + nbocc a v.
Proof.
induction u; simpl; auto; lia.
Qed.
Lemma nbocc_le_length k u : nbocc k u <= length u.
Proof.
induction u; simpl; trivial. case Nat.eqb; lia.
Qed.
Lemma nbocc_total_lt u k :
Forall (fun n => n < k) u ->
length u = cumul (fun n => nbocc n u) k.
Proof.
induction u; simpl; intros H.
- now rewrite cumul_0.
- inversion_clear H. rewrite cumul_add. rewrite IHu by trivial.
rewrite cumul_eqb; simpl; lia.
Qed.
Lemma nbocc_total_le u k :
Forall (fun n => n <= k) u ->
length u = cumul (fun n => nbocc n u) (S k).
Proof.
intros H. apply nbocc_total_lt. eapply Forall_impl; eauto.
simpl; intros; lia.
Qed.
Lemma nbocc_concat a l :
nbocc a (concat l) = list_sum (map (nbocc a) l).
Proof.
induction l as [|w l IH]; simpl; auto.
rewrite nbocc_app. now f_equal.
Qed.
Lemma nbocc_notin x l : ~In x l -> nbocc x l = 0.
Proof.
induction l; simpl; trivial.
intros H. apply Decidable.not_or in H.
case Nat.eqb_spec; try lia. intros. now rewrite IHl.
Qed.
Lemma count_nbocc f a n : count f a n = nbocc a (take n f).
Proof.
induction n. simpl; auto. rewrite take_S.
rewrite nbocc_app. simpl. now f_equal.
Qed.
(** Same as nbocc but for counting value above a given threshold. *)
Fixpoint nbabove (a:nat) (l:list nat) :=
match l with
| [] => 0
| b::l' => nbabove a l' + if a <=? b then 1 else 0
end.
Lemma nbabove_app a u v : nbabove a (u++v) = nbabove a u + nbabove a v.
Proof.
induction u; simpl; auto; lia.
Qed.
Lemma nbabove_le_length k u : nbabove k u <= length u.
Proof.
induction u; simpl; trivial. case Nat.leb; lia.
Qed.
Lemma nbabove_0 x l : Forall (fun y => y < x) l -> nbabove x l = 0.
Proof.
induction l; simpl; trivial.
inversion 1; subst.
case Nat.leb_spec; try lia. intros. now rewrite IHl.
Qed.
Lemma count_above_nbabove f a n : count_above f a n = nbabove a (take n f).
Proof.
induction n. simpl; auto. rewrite take_S.
rewrite nbabove_app. simpl. now f_equal.
Qed.
(** Some predicates on words : Prefix, Suffix, Sub *)
Definition Prefix {A} (u v : list A) := exists w, u++w = v.
Definition Suffix {A} (u v : list A) := exists w, w++u = v.
Definition Sub {A} (u v : list A) := exists w w', w++u++w' = v.
Lemma Prefix_id {A} (w : list A) : Prefix w w.
Proof.
exists []. apply app_nil_r.
Qed.
Lemma Prefix_trans {A} (u v w : list A) :
Prefix u v -> Prefix v w -> Prefix u w.
Proof.
intros (u',<-) (v',<-). exists (u'++v'). apply app_assoc.
Qed.
Lemma Prefix_len {A} (u v : list A) : Prefix u v -> length u <= length v.
Proof.
intros (w,<-). rewrite app_length. lia.
Qed.
Lemma Prefix_incl {A} (u v: list A) : Prefix u v -> incl u v.
Proof.
intros (w,<-). now apply incl_appl.
Qed.
Lemma Prefix_antisym {A} (u v : list A) : Prefix u v -> Prefix v u -> u = v.
Proof.
intros (u',<-) P. apply Prefix_len in P. rewrite app_length in P.
assert (length u' = 0) by lia.
destruct u'. now rewrite app_nil_r. easy.
Qed.
Lemma Prefix_nil {A} (u : list A) : Prefix u [] -> u = [].
Proof.
intros Pr. apply Prefix_len in Pr. simpl in Pr. now destruct u.
Qed.
Lemma Prefix_cons {A} (a:A) u v : Prefix u v -> Prefix (a::u) (a::v).
Proof.
intros (w,<-). now exists w.
Qed.
Lemma Prefix_nth {A} (u v : list A) :
Prefix u v <->
length u <= length v /\
forall n a, n < length u -> nth n u a = nth n v a.
Proof.
split.
- intros (w & <-).
rewrite app_length. split. lia. intros. rewrite app_nth1; auto.
- revert v. induction u; intros v (LE,H).
+ now exists v.
+ simpl in LE. destruct v; try easy. simpl in LE.
assert (P : Prefix u v).
{ apply IHu. split. lia.
intros n b H'. apply (H (S n) b); simpl; auto with arith. }
destruct P as (w & P).
exists w. simpl. f_equal; auto.
apply (H 0 a). simpl. lia.
Qed.
(** Specialized version when [A=nat] we can exploit a different default
value and get rid of [length u <= length v]. Could be generalize to
any non-singleton domain. *)
Lemma Prefix_nth_nat (u v : list nat) :
Prefix u v <-> forall n a, n < length u -> nth n u a = nth n v a.
Proof.
split.
- intros (w & <-). intros. rewrite app_nth1; auto.
- revert v. induction u; intros v H.
+ now exists v.
+ destruct v.
* exfalso. specialize (H 0 (S a)). simpl in H. lia.
* assert (P : Prefix u v).
{ apply IHu. intros m b H'. apply (H (S m) b). simpl. lia. }
destruct P as (w & P).
exists w. simpl. f_equal; auto.
apply (H 0 a). simpl. lia.
Qed.
Lemma Prefix_cons_inv {A} (a:A) u v :
Prefix u (a::v) -> u = [] \/ exists u', u = a::u' /\ Prefix u' v.
Proof.
intros (w,E).
destruct u as [|a' u'].
- now left.
- right. exists u'. injection E as -> E'. split; auto. now exists w.
Qed.
Lemma Prefix_Prefix {A} (u v w : list A) : length u <= length v ->
Prefix u w -> Prefix v w -> Prefix u v.
Proof.
intros L. rewrite !Prefix_nth. intros (LE,P) (LE',P').
split. lia. intros n a Hn. now rewrite P, P' by lia.
Qed.
Lemma Prefix_app_r {A} (u v w : list A) :
Prefix v w -> Prefix (u++v) (u++w).
Proof.
intros (v' & <-). exists v'. now rewrite app_assoc.
Qed.
Lemma Prefix_app {A} (u v w : list A) :
Prefix u (v++w) -> Prefix u v \/ exists u', u = v++u' /\ Prefix u' w.
Proof.
revert v w.
induction u.
- intros v w _. left. now exists v.
- intros v w (t,E). simpl in E.
destruct v.
+ right. exists (a::u). split; auto. now exists t.
+ injection E as <- E.
destruct (IHu v w) as [IH|(u',IH)]; try now exists t.
* left. now apply Prefix_cons.
* right. exists u'. simpl; split. now f_equal. apply IH.
Qed.
Lemma Prefix_seq (w : list nat) a n :
Prefix w (List.seq a n) -> w = List.seq a (length w).
Proof.
revert w a.
induction n as [|n IH]; simpl; intros w a P.
- apply Prefix_nil in P. now subst w.
- apply Prefix_cons_inv in P. destruct P as [->|(w' & -> & P)]; trivial.
simpl. f_equal; auto.
Qed.
Lemma Prefix_rev_Suffix {A} (u v : list A) :
Prefix (rev u) (rev v) <-> Suffix u v.
Proof.
split; intros (w,E).
- exists (rev w).
rewrite <- (rev_involutive w), <- rev_app_distr in E.
now apply rev_inj in E.
- exists (rev w). now rewrite <- rev_app_distr, E.
Qed.
Lemma Suffix_rev_Prefix {A} (u v : list A) :
Suffix (rev u) (rev v) <-> Prefix u v.
Proof.
now rewrite <- Prefix_rev_Suffix, !rev_involutive.
Qed.
Lemma Prefix_take {A}(w:nat->A) n m : n <= m -> Prefix (take n w) (take m w).
Proof.
intros H.
apply Prefix_nth; rewrite !take_length; split; trivial.
intros p a Hp. rewrite !take_nth; trivial; lia.
Qed.
Lemma list_sum_prefix (u v:list nat) :
Prefix u v -> list_sum u <= list_sum v.
Proof.
intros (u',<-). rewrite list_sum_app. lia.
Qed.
Lemma Suffix_id {A} (w : list A) : Suffix w w.
Proof.
now exists [].
Qed.
Lemma Suffix_trans {A} (u v w : list A) :
Suffix u v -> Suffix v w -> Suffix u w.
Proof.
intros (u',<-) (v',<-). exists (v'++u'). now rewrite app_assoc.
Qed.
Lemma Suffix_len {A} (u v : list A) : Suffix u v -> length u <= length v.
Proof.
intros (w,<-). rewrite app_length. lia.
Qed.
Lemma Suffix_nil {A} (u : list A) : Suffix u [] -> u = [].
Proof.
intros Su. apply Suffix_len in Su. simpl in Su. now destruct u.
Qed.
Lemma Suffix_app_l {A} (l u v : list A) : Suffix u v -> Suffix u (l++v).
Proof.
intros (w,<-). exists (l++w). now rewrite app_assoc.
Qed.
Lemma Suffix_app_r {A} (u v r : list A) : Suffix u v -> Suffix (u++r) (v++r).
Proof.
intros (w,<-). exists w. now rewrite app_assoc.
Qed.
Lemma Suffix_cons_inv {A} (a:A) u v :
Suffix u (a::v) -> u = a::v \/ Suffix u v.
Proof.
intros ([|a' w],E).
- now left.
- right. injection E as -> E. now exists w.
Qed.
Lemma Suffix_Suffix {A} (u v w : list A) : length u <= length v ->
Suffix u w -> Suffix v w -> Suffix u v.
Proof.
rewrite <- !Prefix_rev_Suffix. intro. apply Prefix_Prefix.
now rewrite !rev_length.
Qed.
Lemma Suffix_app_inv {A} (u v w : list A) :
Suffix u (v++w) -> Suffix u w \/ exists u', u = u'++w /\ Suffix u' v.
Proof.
revert u. induction v as [|a v IH]; intros u H.
- now left.
- simpl in H. apply Suffix_cons_inv in H. destruct H as [->|H].
+ right. exists (a::v); split; auto. apply Suffix_id.
+ apply IH in H. destruct H as [H|(u' & E & H)].
* now left.
* right. exists u'; split; auto. now apply (Suffix_app_l [a]).
Qed.
Lemma Suffix_seq (w : list nat) a n :
Suffix w (List.seq a n) -> w = List.seq (a+n-length w) (length w).
Proof.
revert w a.
induction n as [|n IH]; simpl; intros w a P.
- apply Suffix_nil in P. now subst w.
- apply Suffix_cons_inv in P. destruct P as [E|P].
+ replace (length w) with (S n).
2:{ subst. simpl. now rewrite seq_length. }
replace (a+_-_) with a by lia. trivial.
+ apply IH in P. rewrite P at 1. f_equal. lia.
Qed.
Lemma Sub_id {A} (w : list A) : Sub w w.
Proof.
exists [], []. now rewrite app_nil_r.
Qed.
Lemma Sub_nil_l {A} (u : list A) : Sub [] u.
Proof.
now exists [], u.
Qed.
Lemma Prefix_Sub {A} (u v : list A) : Prefix u v -> Sub u v.
Proof.
intros (w,<-). now exists [], w.