-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.v
4151 lines (3912 loc) · 112 KB
/
Common.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
Require Import List.
Require Import BinPos.
Require Import Bool.
Require Import Coq.Arith.PeanoNat.
Require Import Coq.Arith.Compare_dec.
Require Import Sumbool.
Require Import Basics.
Require Import sflib.
Require Import Omega.
Require Import Sorting.Permutation.
Require Import Lia.
(* Some helpful lemmas regarding List *)
(* If List.length l = 1, l = h::nil. *)
Lemma list_len1:
forall {X:Type} (l:list X)
(H:List.length l = 1),
exists h, l = h::nil.
Proof.
intros.
destruct l.
- simpl in H. inversion H.
- destruct l.
+ eexists. reflexivity.
+ simpl in H. inversion H.
Qed.
(* If List.length l = 2, l = h1::h2::nil. *)
Lemma list_len2:
forall {X:Type} (l:list X)
(H:List.length l = 2),
exists h1 h2, l = h1::h2::nil.
Proof.
intros.
destruct l.
- simpl in H. inversion H.
- destruct l.
+ simpl in H. inversion H.
+ destruct l.
* eexists. eexists. reflexivity.
* simpl in H. inversion H.
Qed.
Lemma firstn_app_decompose {X:Type}:
forall (l l1 l2:list X) n
(HL:l = l1 ++ l2)
(HLEN:List.length l1 = n),
firstn n l = l1.
Proof.
intros.
generalize dependent l.
generalize dependent n.
induction l1.
- simpl. intros. rewrite <- HLEN. reflexivity.
- simpl. intros.
destruct n.
+ inversion HLEN.
+ inversion HLEN.
destruct l.
inversion HL.
inversion HL.
simpl. rewrite H0. rewrite IHl1. reflexivity.
congruence. reflexivity.
Qed.
Lemma firstn_In {X:Type}:
forall (l:list X) n x l'
(HF:List.firstn n l = l')
(HIN:List.In x l'),
List.In x l.
Proof.
intros.
generalize dependent l.
generalize dependent l'.
induction n.
{ intros. simpl in HF. inv HF. inv HIN. }
{ simpl. intros.
destruct l. inv HF. inv HIN.
destruct l'; try congruence.
inv HF. inv HIN. left. ss.
right. eapply IHn. eassumption. ss.
}
Qed.
Lemma skipn_In {X:Type}:
forall (l:list X) n x l'
(HF:List.skipn n l = l')
(HIN:List.In x l'),
List.In x l.
Proof.
intros.
generalize dependent l'.
generalize dependent l.
induction n.
{ intros. simpl in HF. congruence. }
{ simpl. intros.
destruct l. congruence.
eapply IHn in HF. right. ss. ss.
}
Qed.
Lemma skipn_length {X:Type}:
forall n (l:list X),
List.length (List.skipn n l) = (List.length l) - n.
Proof.
intro.
induction n.
{ simpl. intros. omega. }
{ intros.
destruct l.
simpl. omega.
simpl. rewrite IHn. reflexivity.
}
Qed.
Lemma skipn_app_decompose {X:Type}:
forall (l l1 l2:list X) n
(HL:l = l1 ++ l2)
(HLEN:List.length l1 = n),
skipn n l = l2.
Proof.
intros.
generalize dependent l.
generalize dependent n.
induction l1.
- simpl. intros. rewrite HL. rewrite <- HLEN. reflexivity.
- simpl. intros.
destruct n.
+ inversion HLEN.
+ inversion HLEN.
destruct l.
inversion HL.
inversion HL.
simpl. rewrite H0. rewrite IHl1. reflexivity.
congruence. reflexivity.
Qed.
Lemma skipn_all {X:Type}:
forall (l:list X) n
(HLEN:List.length l <= n),
skipn n l = nil.
Proof.
intros.
generalize dependent n.
induction l.
- simpl. intros. destruct n; reflexivity.
- simpl. intros.
destruct n.
+ inversion HLEN.
+ simpl. apply IHl.
apply le_S_n. assumption.
Qed.
Lemma app_decompose {X:Type} (n:nat):
forall (l:list X)
(HLEN:n <= List.length l),
exists l1 l2, (l = l1 ++ l2 /\ List.length l1 = n).
Proof.
intros.
generalize dependent n.
induction l.
- simpl. intros. inversion HLEN.
exists nil. exists nil. split; reflexivity.
- simpl. intros.
destruct n.
+ exists nil. exists (a::l). split; reflexivity.
+ apply le_S_n in HLEN.
apply IHl in HLEN.
destruct HLEN. destruct H.
destruct H.
exists (a::x). exists x0.
rewrite H. split. reflexivity. simpl. congruence.
Qed.
Lemma firstn_firstn_skipn {X:Type}:
forall n1 n2 (l:list X),
firstn n1 l ++ firstn n2 (skipn n1 l) = firstn (n1+n2) l.
Proof.
intros.
assert (HD := app_decompose n1 l).
assert (HDEC := Compare_dec.le_gt_dec n1 (List.length l)).
destruct HDEC as [HDEC | HDEC].
- apply HD in HDEC.
destruct HDEC as [l1 [l2 [HDEC1 HDEC2]]].
rewrite firstn_app_decompose with (l0 := l) (l3 := l1) (l4 := l2).
rewrite <- HDEC2.
rewrite HDEC1.
rewrite firstn_app_2.
rewrite skipn_app_decompose with (l3 := l1) (l4 := l2).
reflexivity. reflexivity. reflexivity. congruence. congruence.
- assert (length l <= n1).
{ apply Gt.gt_le_S in HDEC.
apply PeanoNat.Nat.le_trans with (m := S (length l)).
auto. assumption. }
rewrite firstn_all2.
rewrite firstn_all2 with (n:= n1+n2).
rewrite skipn_all. rewrite firstn_nil.
rewrite app_nil_r. reflexivity.
assumption.
apply Gt.gt_le_S in HDEC.
apply PeanoNat.Nat.le_trans with (m := n1).
apply PeanoNat.Nat.le_trans with (m := S (length l)).
auto. assumption. apply PeanoNat.Nat.le_add_r.
assumption.
Qed.
(* If the result of List.combine is nil, and
their length is the same. input is both nil *)
Lemma combine_length_nil:
forall {X Y:Type} (l1: list X) (l2:list Y)
(HLEN:List.length l1 = List.length l2)
(HNIL:List.combine l1 l2 = nil),
l1 = nil /\ l2 = nil.
Proof.
intros.
destruct l1; destruct l2.
- split; reflexivity.
- simpl in HLEN. inversion HLEN.
- simpl in HLEN. inversion HLEN.
- simpl in HNIL. inversion HNIL.
Qed.
Lemma combine_length_some:
forall {X Y:Type} (l1: list X) (l2:list Y) a t
(HLEN:List.length l1 = List.length l2)
(HSOME:List.combine l1 l2 = a::t),
l1 = (a.(fst))::((List.split t).(fst)) /\
l2 = (a.(snd))::((List.split t).(snd)).
Proof.
intros.
assert (split (combine l1 l2) = (l1, l2)).
{ apply combine_split. assumption. }
destruct l1; destruct l2.
- simpl in HSOME; inversion HSOME.
- simpl in HLEN; inversion HLEN.
- simpl in HLEN; inversion HLEN.
- simpl in HSOME.
inversion HSOME.
simpl in H.
remember (split (combine l1 l2)) as q.
destruct q.
inversion H.
simpl.
split; reflexivity.
Qed.
(* l = combine (fst (split l), snd (split l)). *)
Lemma combine_fst_snd_split:
forall {X Y:Type} (l:list (X*Y)),
l = List.combine (fst (List.split l)) (snd (List.split l)).
Proof.
intros.
induction l.
- reflexivity.
- destruct a.
remember (split l) as p.
simpl.
rewrite <- Heqp.
destruct p.
simpl. rewrite IHl.
reflexivity.
Qed.
Lemma combine_map_In:
forall {X Y:Type} (ly:list Y) (f:Y -> X) (x:X) (y:Y) (lx:list X)
(HX:x = f y)
(HLX:lx = List.map f ly)
(HIN:List.In y ly),
List.In (x, y) (List.combine lx ly).
Proof.
induction ly.
- intros. simpl in HIN. inversion HIN.
- simpl. intros.
destruct lx; inversion HLX.
simpl.
rewrite HX.
destruct HIN.
+ left. congruence.
+ right. apply IHly with (f := f).
reflexivity. reflexivity. assumption.
Qed.
Lemma map_In:
forall {X Y:Type} (l:list X) (f:X -> Y) (y:Y) x
(HIN:List.In x l)
(HY:y = f x),
List.In y (List.map f l).
Proof.
induction l.
intros. inv HIN.
intros. simpl in HIN.
destruct HIN. simpl. left. congruence.
simpl. right. eapply IHl. eassumption. assumption.
Qed.
Lemma In_pair_split_snd {X Y:Type}:
forall (x:X) (y:Y) l (HIN:List.In (x, y) l),
List.In y (snd (List.split l)).
Proof.
induction l. eauto. intros.
simpl in *. destruct HIN.
{ rewrite H. destruct (List.split l).
simpl. left. reflexivity. }
{ destruct a. apply IHl in H. destruct (List.split l).
simpl. right. eauto. }
Qed.
Lemma In_split2 {X:Type}:
forall x1 x2 (HDIFF:x1 <> x2) (l:list X)
(HIN1:List.In x1 l)
(HIN2:List.In x2 l),
exists l1 l2 l3, l = l1++x1::l2++x2::l3 \/
l = l1++x2::l2++x1::l3.
Proof.
intros.
apply List.in_split in HIN1.
destruct HIN1 as [l1 [l2 HIN1]].
rewrite HIN1 in HIN2.
apply List.in_app_or in HIN2.
destruct HIN2.
{ apply List.in_split in H.
destruct H as [l3 [l4 H]].
rewrite H in HIN1.
exists l3, l4, l2.
right. rewrite <- List.app_assoc in HIN1.
rewrite <- List.app_comm_cons in HIN1.
assumption. }
{ simpl in H.
destruct H. congruence.
apply List.in_split in H.
destruct H as [l3 [l4 H]].
rewrite H in HIN1.
exists l1, l3, l4.
left. assumption.
}
Qed.
Lemma In_swap {X:Type}:
forall (n m x:X) l
(HIN:List.In x (n::m::l)),
List.In x (m::n::l).
Proof.
intros.
inv HIN. right. left. ss.
inv H. left. ss.
right. right. ss.
Qed.
(* Filtered list is shorter than the original list. *)
Lemma filter_length:
forall {X:Type} (l:list X) f,
List.length (List.filter f l) <= List.length l.
Proof.
intros.
induction l.
- simpl. auto.
- simpl.
destruct (f a).
+ simpl.
apply Le.le_n_S.
assumption.
+ apply le_S.
assumption.
Qed.
Lemma filter_true {X:Type}:
forall (l : list X),
List.filter (fun (x:X) => true) l = l.
Proof.
induction l.
{ reflexivity. }
{ simpl. rewrite IHl. reflexivity. }
Qed.
Lemma filter_reorder {X:Type}:
forall f1 f2 (l:list X),
List.filter f1 (List.filter f2 l) =
List.filter f2 (List.filter f1 l).
Proof.
induction l. reflexivity.
simpl. des_ifs; simpl; des_ifs; congruence.
Qed.
Lemma filter_map_combine {X Y:Type}:
forall (l1 l2:list X) (l3 l4:list Y) (ff:X -> bool) (fm:X -> Y)
(HFILTER:l2 = List.filter ff l1)
(HMAP1:l3 = List.map fm l1)
(HMAP2:l4 = List.map fm l2),
List.combine l4 l2 = List.filter (fun itm => ff itm.(snd)) (List.combine l3 l1).
Proof.
induction l1.
{ simpl. intros. subst l2. subst l3. simpl in HMAP2.
subst l4. reflexivity. }
{ simpl. intros.
destruct (ff a) eqn:HCOND.
{ destruct l2. inv HFILTER.
simpl in HMAP2.
destruct l3; destruct l4; try ss.
inv HFILTER. inv HMAP1. inv HMAP2.
rewrite HCOND.
erewrite IHl1; reflexivity.
}
{ destruct l3; ss.
inv HMAP1. rewrite HCOND.
erewrite IHl1; reflexivity.
}
}
Qed.
Lemma split_filter_combine_map2_snd {X Y:Type}:
forall (l2 l4:list Y) (l1 l3:list X) (f:(X * Y) -> bool) (g:Y -> X)
(HS: (l3, l4) = List.split (List.filter f (List.combine l1 l2)))
(HMAP: l1 = List.map g l2),
l4 = List.filter (fun x => f (g x, x)) l2.
Proof.
induction l2.
{ simpl in *. intros. subst l1. simpl in HS. congruence. }
{ simpl in *.
intros.
destruct l1; try congruence.
simpl in HS.
inv HMAP.
destruct (f (g a, a)) eqn:HCOND.
{ simpl in HS.
remember (List.split (List.filter f (List.combine (List.map g l2) l2))) as hs.
destruct hs.
inv HS.
erewrite <- IHl2. reflexivity. eassumption. reflexivity.
}
{ simpl in HS.
eapply IHl2 in HS.
eassumption.
reflexivity.
}
}
Qed.
Lemma app_equal {X:Type}:
forall (l1' l2' l1 l2:list X) (x x':X)
(HNOTIN1:~List.In x' l1)
(HNOTIN1':~List.In x l1')
(HEQ:l1' ++ x' :: l2' = l1 ++ x :: l2),
l1 = l1' /\ l2 = l2' /\ x' = x.
Proof.
intros.
generalize dependent l1'.
induction l1.
- intros. simpl in HEQ.
destruct l1'. simpl in HEQ.
inversion HEQ. split. reflexivity. split; congruence.
simpl in HEQ. inversion HEQ. rewrite H0 in HNOTIN1'.
exfalso. apply HNOTIN1'. constructor.
reflexivity.
- simpl. intros.
destruct l1'.
+ simpl in HEQ. inversion HEQ. rewrite H0 in HNOTIN1.
exfalso. apply HNOTIN1. constructor. reflexivity.
+ simpl in HEQ.
inversion HEQ. rewrite H0 in *. clear H0.
assert (l1 = l1' /\ l2 = l2' /\ x' = x).
{ apply IHl1. simpl in HNOTIN1.
apply Decidable.not_or in HNOTIN1. destruct HNOTIN1. assumption.
simpl in HNOTIN1'. apply Decidable.not_or in HNOTIN1'.
destruct HNOTIN1'. assumption.
assumption. }
destruct H. destruct H0.
split. congruence. split; congruence.
Qed.
(* the result of List.filter satisfies forallb. *)
Lemma filter_forallb: forall {X:Type} (l:list X) f,
List.forallb f (List.filter f l) = true.
Proof.
intros.
induction l. reflexivity. simpl.
destruct (f a) eqn:H. simpl. rewrite H. rewrite IHl. auto.
assumption.
Qed.
Lemma filter_app {X:Type}:
forall (l1 l2:list X) (f:X -> bool),
List.filter f (l1++l2) = (List.filter f l1) ++ (List.filter f l2).
Proof.
intros.
induction l1.
- simpl. reflexivity.
- simpl. destruct (f a). rewrite IHl1. reflexivity.
assumption.
Qed.
Lemma forallb_map:
forall {X Y:Type} (l: list X) (l':list Y)
(f:X -> Y) (g:Y -> bool) (h:X -> bool) b
(HMAP:l' = List.map f l)
(HFORALLB:forallb g l' = b)
(HEQ:forall x, (compose g f) x = h x),
forallb h l = b.
Proof.
intros.
generalize dependent l'.
induction l.
- simpl. intros. rewrite HMAP in *. simpl in HFORALLB. congruence.
- simpl. intros. rewrite HMAP in HFORALLB.
simpl in HFORALLB.
destruct l'. inversion HMAP.
inversion HMAP.
unfold compose in *.
destruct (g (f a)) eqn:HGF.
+ simpl. erewrite IHl. rewrite <- HEQ. rewrite HGF. reflexivity. eassumption.
simpl in HFORALLB. rewrite H1. assumption.
+ simpl in HFORALLB. simpl. rewrite <- HEQ. rewrite HGF. simpl. assumption.
Qed.
Lemma Forall2_samelist {X:Type}:
forall (l:list X) (f:X -> X -> Prop)
(HF:forall X, f X X),
List.Forall2 f l l.
Proof.
induction l.
{ intros. constructor. }
{ intros.
constructor. apply HF. eapply IHl.
assumption.
}
Qed.
Lemma Forall2_implies {X Y:Type}:
forall (l1:list X) (l2:list Y) (f g:X -> Y -> Prop)
(HFORALL2:List.Forall2 f l1 l2)
(HIMPLIES:forall x y, f x y -> g x y),
List.Forall2 g l1 l2.
Proof.
intros.
induction HFORALL2.
{ constructor. }
{ constructor. apply HIMPLIES. assumption.
assumption. }
Qed.
Lemma Forall2_trans {X:Type}:
forall (l1 l2 l3:list X)
(f:X -> X -> Prop)
(HTRANS:forall x y z, f x y -> f y z -> f x z)
(HFORALL1:List.Forall2 f l1 l2)
(HFORALL2:List.Forall2 f l2 l3),
List.Forall2 f l1 l3.
Proof.
intros.
generalize dependent l3.
induction HFORALL1.
{ intros. destruct l3. constructor.
inv HFORALL2. }
{ intros. destruct l3. inv HFORALL2.
inv HFORALL2.
constructor. eapply HTRANS. eassumption. ss.
eapply IHHFORALL1. assumption.
}
Qed.
Lemma Forall_app {X:Type}:
forall (l1 l2:list X) (f:X -> Prop)
(HF:Forall f (l1++l2)),
Forall f l1 /\ Forall f l2.
Proof.
intros.
induction l1.
simpl in HF. split. ss. ss.
simpl in HF. inv HF. split. constructor. ss.
apply IHl1 in H2. inv H2. ss.
apply IHl1 in H2. inv H2. ss.
Qed.
Lemma Forall_app2 {X:Type}:
forall (l1 l2:list X) (f:X -> Prop)
(HF1:Forall f l1)
(HF2:Forall f l2),
Forall f (l1 ++ l2).
Proof.
intros.
induction l1.
simpl. ss.
inv HF1. apply IHl1 in H2. simpl. constructor.
ss. ss.
Qed.
Lemma Forall_and {X:Type}:
forall (l:list X) (f g:X -> Prop)
(HF:List.Forall f l)
(HG:List.Forall g l),
List.Forall (fun x => f x /\ g x) l.
Proof.
intros.
induction l.
{ constructor. }
{ inv HF. inv HG.
constructor. split; ss. eapply IHl; eauto.
}
Qed.
Lemma Forall_repeat {X:Type}:
forall x n (f:X -> Prop)
(HF:f x),
Forall f (List.repeat x n).
Proof.
intros.
induction n.
simpl. ss.
simpl. constructor. ss. ss.
Qed.
Lemma forallb_In {X:Type}:
forall (l:list X) (f:X -> bool) i
(HFORALLB:List.forallb f l = true)
(HIN:List.In i l),
f i = true.
Proof.
intros.
rewrite List.forallb_forall in HFORALLB.
apply HFORALLB in HIN.
assumption.
Qed.
Lemma forallb_Permutation {X:Type}:
forall (l1 l2:list X) (HPERM:Permutation l1 l2) f,
List.forallb f l1 = List.forallb f l2.
Proof.
intros.
induction HPERM.
{ reflexivity. }
{ simpl. rewrite IHHPERM. reflexivity. }
{ simpl.
rewrite andb_assoc.
rewrite andb_assoc.
rewrite andb_comm with (b1 := f y). reflexivity. }
{ congruence. }
Qed.
Lemma forallb_implies:
forall {X:Type} (l:list X) (f g:X -> bool)
(HIMP:forall x, f x = true -> g x = true)
(HFORALLB:List.forallb f l = true),
List.forallb g l = true.
Proof.
intros.
induction l.
- reflexivity.
- simpl. simpl in HFORALLB.
rewrite andb_true_iff in *.
destruct HFORALLB.
split. apply HIMP. assumption. apply IHl. assumption.
Qed.
Lemma concat_Permutation {X:Type}:
forall (l1 l2:list (list X))
(HFORALL:List.Forall2 (fun x y => Permutation x y) l1 l2),
Permutation (List.concat l1) (List.concat l2).
Proof.
intros.
generalize dependent l2.
induction l1.
{ intros. inv HFORALL. eauto. }
{ simpl. intros.
destruct l2. inv HFORALL.
inv HFORALL. simpl.
apply Permutation_app. assumption.
apply IHl1. assumption.
}
Qed.
Lemma split_map_fst:
forall {X Y Z:Type} (l:list (X * Y)) (f:X * Y -> Z) (g:X -> Z)
(HEQ:forall x y, f (x, y) = g x),
List.map f l = List.map g (fst (split l)).
Proof.
intros.
induction l.
reflexivity.
simpl. destruct a.
remember (split l) as p.
destruct p.
simpl in *.
rewrite HEQ. congruence.
Qed.
Lemma split_map_snd:
forall {X Y Z:Type} (l:list (X * Y)) (f:X * Y -> Z) (g:Y -> Z)
(HEQ:forall x y, f (x, y) = g y),
List.map f l = List.map g (snd (split l)).
Proof.
intros.
induction l.
reflexivity.
simpl. destruct a.
remember (split l) as p.
destruct p.
simpl in *.
rewrite HEQ. congruence.
Qed.
Lemma map_fst_split {X Y:Type}:
forall (l:list (X * Y)),
List.map fst l = (List.split l).(fst).
Proof.
intros.
induction l.
- reflexivity.
- simpl. destruct a.
remember (split l) as p.
destruct p. simpl. rewrite IHl. reflexivity.
Qed.
Lemma existsb_rev:
forall {X:Type} (f:X -> bool) (l:list X),
List.existsb f (List.rev l) = List.existsb f l.
Proof.
intros.
induction l.
- reflexivity.
- simpl in *.
rewrite existsb_app.
simpl.
rewrite orb_comm.
rewrite orb_comm with (b1 := f a).
simpl.
rewrite IHl. reflexivity.
Qed.
(* Why do I need this? *)
Lemma list_eq:
forall {X:Type} (a b:X) (c d:list X)
(HEQ:a = b)
(HEQ2:c = d),
a::c = b::d.
Proof.
intros.
rewrite HEQ.
rewrite HEQ2.
reflexivity.
Qed.
(* If map f b = a,
and p = split (filter g (combine a b)),
map f p.snd = p.fst. *)
Lemma split_filter_combine_map:
forall {X Y:Type} (a:list X) (b:list Y) p f g
(HMAP:List.map f b = a)
(HP:p = List.split (List.filter g (List.combine a b))),
List.map f p.(snd) = p.(fst).
Proof.
intros.
remember (combine a b) as ab.
generalize dependent a.
generalize dependent b.
generalize dependent p.
induction ab as [| abh abt].
- intros. simpl in HP. rewrite HP. reflexivity.
- intros.
destruct (split (filter g abt)) as [abtl abtr] eqn:HS.
simpl in HP.
destruct a as [| ah at'].
{ simpl in Heqab. inversion Heqab. }
destruct b as [| bh bt].
{ simpl in Heqab. inversion Heqab. }
destruct (g abh).
+ destruct abh as [abhl abhr].
simpl in Heqab.
inversion Heqab.
rewrite H0 in *. clear H0.
rewrite H1 in *. clear H1. clear Heqab.
simpl in HP.
rewrite HS in HP.
rewrite HP.
simpl.
simpl in HMAP.
inversion HMAP.
rewrite H0 in *. clear H0.
rewrite H1 in *. clear HMAP.
apply list_eq. reflexivity.
assert (abtr = snd (split (filter g abt))).
{ rewrite HS. reflexivity. }
assert (abtl = fst (split (filter g abt))).
{ rewrite HS. reflexivity. }
rewrite H. rewrite H0.
eapply IHabt.
* assumption.
* apply H1.
* assumption.
+ apply IHabt with (b := bt) (a := at').
* rewrite HP. assumption.
* simpl in HMAP.
inversion HMAP. reflexivity.
* simpl in Heqab.
inversion Heqab.
reflexivity.
Qed.
Lemma In_map:
forall {X Y:Type} (l:list X) (f:X -> Y) (y:Y)
(HIN:List.In y (List.map f l)),
exists (x:X), f x = y /\ List.In x l.
Proof.
induction l.
intros. simpl in HIN. inversion HIN.
intros.
simpl in HIN.
destruct HIN.
- eexists. split. eassumption. constructor. reflexivity.
- apply IHl in H.
destruct H as [xH H].
destruct H as [H1 H2].
eexists.
split. eassumption. simpl. right. assumption.
Qed.
Lemma In_notIn_neq {X:Type}:
forall (l:list X) (x1 x2:X)
(HNOTIN:~List.In x1 l)
(HIN:List.In x2 l),
x1 <> x2.
Proof.
intros.
intros HEQ.
apply HNOTIN.
congruence.
Qed.
Lemma last_cons {X:Type}:
forall (l:list X) h h' h'',
List.last (l ++ (h::nil)) h'' = List.last (h'::l ++ (h::nil)) h''.
Proof.
intros.
generalize dependent h'.
induction l.
- simpl. reflexivity.
- intros. simpl. reflexivity.
Qed.
Lemma last_element {X:Type}:
forall (l:list X) h1 h3 h2
(HLAST:List.last (l ++ (h1::nil)) h3 = h2),
h1 = h2.
Proof.
intros.
induction l.
- simpl in HLAST. congruence.
- simpl.
replace ((a::l)++h1::nil) with (a::l++h1::nil) in HLAST.
rewrite <- last_cons in HLAST.
apply IHl. assumption.
reflexivity.
Qed.
Lemma last_head {X:Type}:
forall (l:list X) (HLEN:List.length l > 0) x
(HLAST: List.last l x = x),
List.hd x (List.rev l) = x.
Proof.
intros.
generalize dependent HLEN.
generalize dependent HLAST.
apply rev_ind with (l := l).
- intros. simpl in HLEN. inversion HLEN.
- intros.
assert (x0 = x).
{ eapply last_element. eapply HLAST. }
rewrite H0 in *.
rewrite rev_unit.
reflexivity.
Qed.
Lemma list_segmentize8_l {X:Type}:
forall (bs:list X),
exists b1 b2, bs = b1 ++ b2 /\
Nat.modulo (List.length b2) 8 = 0 /\
List.length b1 < 8.
Proof.
intros.
induction bs.
- exists nil. eexists nil.
split. reflexivity. split. reflexivity. simpl. omega.
- inversion IHbs as [b1 [b2 IH]].
destruct IH as [H1 [H2 H3]].
destruct b1 as [ | h1 b1].
{ eexists (a::nil). eexists b2.
split. rewrite H1. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h2 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h3 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::h2::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h4 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::h2::h3::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h5 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::h2::h3::h4::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h6 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::h2::h3::h4::h5::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
destruct b1 as [ | h7 b1].
{ simpl in H1.
rewrite H1.
eexists (a::h1::h2::h3::h4::h5::h6::nil). eexists b2.
split. reflexivity.
split. assumption.
simpl. omega. }
simpl in H1.
rewrite H1.
eexists nil.
eexists (a::h1::h2::h3::h4::h5::h6::h7::b1 ++ b2).
split. reflexivity.
split.
assert (a :: h1 :: h2 :: h3 :: h4 :: h5 :: h6 :: h7 :: b1 ++ b2 =
(a :: h1 :: h2 :: h3 :: h4 :: h5 :: h6 :: h7 :: b1) ++ b2).
{ reflexivity. }
rewrite H.
rewrite app_length.
replace (length (a :: h1 :: h2 :: h3 :: h4 :: h5 :: h6 :: h7 :: b1)) with
(8 + length b1).
simpl in H3.
destruct b1.
+ rewrite <- Nat.add_mod_idemp_l.
simpl.
apply H2.
omega.
+ simpl in H3.
omega.
+ simpl. reflexivity.
+ simpl. omega.
Qed.
Lemma list_segmentize8_r {X:Type}:
forall (bs:list X),
exists b1 b2, bs = b1 ++ b2 /\
Nat.modulo (List.length b1) 8 = 0 /\
List.length b2 < 8.
Proof.
intros.
assert (exists b1' b2', (rev bs) = b1' ++ b2' /\
Nat.modulo (List.length b2') 8 = 0 /\
List.length b1' < 8).
{ eapply list_segmentize8_l. }
destruct H as [b1' H].
destruct H as [b2' H].
destruct H as [H1 [H2 H3]].
rewrite <- rev_involutive with (l := b1') in H1.
rewrite <- rev_involutive with (l := b2') in H1.
rewrite <- rev_app_distr in H1.
assert (bs = rev b2' ++ rev b1').
{ rewrite <- rev_involutive with (l := bs).
rewrite H1.
rewrite rev_involutive.
reflexivity. }
exists (rev b2').
exists (rev b1').
split.
- assumption.
- split.
rewrite rev_length. assumption.
rewrite rev_length. assumption.
Qed.
Lemma list_split8_l {X:Type}:
forall (bs:list X) n
(HLEN:n = List.length bs)
(HLEN2:Nat.modulo n 8 = 0)
(HNEQ:n <> 0),
exists b1 b2, bs = b1 ++ b2 /\
List.length b1 = 8 /\
Nat.modulo (List.length b2) 8 = 0.
Proof.
intros.
destruct bs as [| h1 bs].
{ simpl in HLEN. omega. }
destruct bs as [| h2 bs].
{ simpl in HLEN. rewrite HLEN in HLEN2. inversion HLEN2. }