This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_solver.v
executable file
·2178 lines (1943 loc) · 77.3 KB
/
list_solver.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 Coq.Logic.FunctionalExtensionality.
Require Import Coq.Logic.PropExtensionality.
Require Import Zlist.sublist.
Require Import Coq.micromega.Lia.
Require Import Zlist.Zlength_solver.
Import ListNotations.
(* Alias functional_extensionality and propositional_extensionality. *)
Definition extensionality : forall {A B: Type} (f g : A -> B), (forall x, f x = g x) -> f = g := @functional_extensionality.
Definition prop_ext : forall {A B : Prop}, A <-> B -> A = B := propositional_extensionality.
(* stuff moved from functional_base*)
(* Definition Vubyte (c: Byte.int) : val :=
Vint (Int.repr (Byte.unsigned c)).
Definition Vbyte (c: Byte.int) : val :=
Vint (Int.repr (Byte.signed c)).
Ltac fold_Vbyte :=
repeat match goal with |- context [Vint (Int.repr (Byte.signed ?c))] =>
fold (Vbyte c)
end.
Instance Inhabitant_val : Inhabitant val := Vundef.
Instance Inhabitant_int: Inhabitant int := Int.zero.
Instance Inhabitant_byte: Inhabitant byte := Byte.zero.
Instance Inhabitant_int64: Inhabitant Int64.int := Int64.zero.
Instance Inhabitant_ptrofs: Inhabitant Ptrofs.int := Ptrofs.zero.
Instance Inhabitant_float : Inhabitant float := Float.zero.
Instance Inhabitant_float32 : Inhabitant float32 := Float32.zero. *)
(** This file provides a almost-complete solver for list with concatenation.
Its core symbols include:
Zlength
Znth
Zrepeat
app
sublist
map.
And it also interprets these symbols by convernting to core symbols:
list_repeat (Z.to_nat _)
nil
cons
upd_Znth. *)
(** * list_form *)
Lemma list_repeat_Zrepeat : forall (A : Type) (x : A) (n : Z),
list_repeat (Z.to_nat n) x = Zrepeat x n.
Proof. intros *. rewrite <- repeat_list_repeat. auto. Qed.
Lemma cons_Zrepeat_1_app : forall (A : Type) (x : A) (al : list A),
x :: al = Zrepeat x 1 ++ al.
Proof. auto. Qed.
Lemma upd_Znth_unfold : forall (A : Type) (n : Z) (al : list A) (x : A),
0 <= n < Zlength al ->
upd_Znth n al x = sublist 0 n al ++ [x] ++ sublist (n+1) (Zlength al) al.
Proof. intros. rewrite upd_Znth_old_upd_Znth; auto. Qed.
(** * Znth_solve *)
(** Znth_solve is a tactic that simplifies and solves proof goal related to terms headed by Znth. *)
(* Auxilary lemmas for Znth_solve. *)
Lemma Znth_Zrepeat : forall (A : Type) (d : Inhabitant A) (i n : Z) (x : A),
0 <= i < n ->
Znth i (Zrepeat x n) = x.
Proof. intros. unfold Zrepeat. rewrite repeat_list_repeat. apply Znth_list_repeat_inrange; auto. Qed.
Definition Znth_app1 := app_Znth1.
Definition Znth_app2 := app_Znth2.
Lemma Znth_upd_Znth_same : forall (A : Type) (d : Inhabitant A) (i j : Z) (l : list A) (x : A),
0 <= i < Zlength l ->
i = j ->
Znth i (upd_Znth j l x) = x.
Proof.
intros. subst. apply upd_Znth_same; auto.
Qed.
Lemma Znth_upd_Znth_diff : forall (A : Type) (d : Inhabitant A) (i j : Z) (l : list A) (x : A),
i <> j ->
Znth i (upd_Znth j l x) = Znth i l.
Proof.
intros.
destruct (Sumbool.sumbool_and _ _ _ _ (Z_le_dec 0 i) (Z_lt_dec i (Zlength l)));
destruct (Sumbool.sumbool_and _ _ _ _ (Z_le_dec 0 j) (Z_lt_dec j (Zlength l))).
- rewrite upd_Znth_diff; auto.
- rewrite upd_Znth_out_of_range; auto. lia.
- rewrite !Znth_outofbounds; auto. lia.
rewrite Zlength_upd_Znth. lia.
- rewrite upd_Znth_out_of_range; auto. lia.
Qed.
(** * list extentionality *)
(* To prove equality between two lists, a convenient way is to apply extentionality
and prove their length are equal and each corresponding entries are equal.
It is convenient because then we can use Znth_solve to solve it. *)
Lemma nth_eq_ext : forall (A : Type) (default : A) (al bl : list A),
length al = length bl ->
(forall (i : nat), (0 <= i < length al)%nat -> nth i al default = nth i bl default) ->
al = bl.
Proof.
intros. generalize dependent bl.
induction al; intros;
destruct bl; try discriminate; auto.
f_equal.
- apply (H0 0%nat). simpl. lia.
- apply IHal.
+ simpl in H. lia.
+ intros. apply (H0 (S i)). simpl. lia.
Qed.
Lemma Znth_eq_ext : forall {A : Type} {d : Inhabitant A} (al bl : list A),
Zlength al = Zlength bl ->
(forall (i : Z), 0 <= i < Zlength al -> Znth i al = Znth i bl) ->
al = bl.
Proof.
intros. rewrite !Zlength_correct in *. apply nth_eq_ext with d.
- lia.
- intros. rewrite <- (Nat2Z.id i).
specialize (H0 (Z.of_nat i) ltac:(lia)).
rewrite !nth_Znth by (rewrite !Zlength_correct in *; lia).
apply H0.
Qed.
Hint Rewrite list_repeat_Zrepeat cons_Zrepeat_1_app : list_solve_rewrite.
Hint Rewrite app_nil_r app_nil_l : list_solve_rewrite.
(* Hint Rewrite upd_Znth_unfold using Zlength_solve : list_solve_rewrite. *)
Ltac list_form :=
autorewrite with list_solve_rewrite in *.
(** * Znth_solve *)
(** Znth_solve is a tactic that simplifies and solves proof goal related to terms headed by Znth. *)
Hint Rewrite @Znth_list_repeat_inrange using Zlength_solve : Znth.
Hint Rewrite @Znth_sublist using Zlength_solve : Znth.
Hint Rewrite Znth_app1 Znth_app2 using Zlength_solve : Znth.
Hint Rewrite Znth_Zrepeat using Zlength_solve : Znth.
Hint Rewrite Znth_upd_Znth_same Znth_upd_Znth_diff using Zlength_solve : Znth.
(* Hint Rewrite (@Znth_map _ Inhabitant_float) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_float32) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_ptrofs) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_int64) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_byte) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_int) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_val) using Zlength_solve : Znth. *)
Hint Rewrite (@Znth_map _ Inhabitant_Z) using Zlength_solve : Znth.
Hint Rewrite (@Znth_map _ Inhabitant_nat) using Zlength_solve : Znth.
Create HintDb Znth_solve_hint.
(** Znth_solve_rec is the main loop of Znth_solve. It tries to simplify
the goal and branches when encountering an uncertain concatenation. *)
Ltac Znth_solve_rec :=
autorewrite with Znth;
Zlength_simplify;
auto with Znth_solve_hint;
try match goal with
| |- context [Znth ?n (app ?al ?bl)] =>
let H := fresh in
pose (H := Z_lt_le_dec n (Zlength al));
Zlength_simplify_in H; destruct H;
Znth_solve_rec
| |- context [Znth ?n (upd_Znth ?i ?l ?x)] =>
let H := fresh in
pose (H := Z.eq_dec n i);
Zlength_simplify_in H; destruct H;
Znth_solve_rec
| |- context [Znth ?n (map ?f ?l)] =>
unshelve erewrite @Znth_map by Zlength_solve
(* only 1 : auto with typeclass_instances *)
end.
Ltac Znth_solve :=
Zlength_simplify_in_all;
Znth_solve_rec.
(** Znth_solve2 is like Znth_solve, but it also branches concatenation in context. *)
Ltac Znth_solve2 :=
Zlength_simplify_in_all; autorewrite with Znth in *; try Zlength_solve; try congruence; (* try solve [exfalso; auto]; *)
try first
[ match goal with
| |- context [Znth ?n (?al ++ ?bl)] =>
let H := fresh in
pose (H := Z_lt_le_dec n (Zlength al)); Zlength_simplify_in_all; destruct H; Znth_solve2
end
| match goal with
| |- context [Znth ?n (upd_Znth ?i ?l ?x)] =>
let H := fresh in
pose (H := Z.eq_dec n i); Zlength_simplify_in_all; destruct H; Znth_solve2
end
| match goal with
| |- context [Znth ?n (map ?f ?l)] =>
unshelve erewrite @Znth_map by Zlength_solve
(* only 1 : auto with typeclass_instances *)
end
| match goal with
| H0 : context [Znth ?n (?al ++ ?bl)] |- _ =>
let H := fresh in
pose (H := Z_lt_le_dec n (Zlength al)); Zlength_simplify_in_all; destruct H; Znth_solve2
end
| match goal with
| H0 : context [Znth ?n (upd_Znth ?i ?l ?x)] |- _ =>
let H := fresh in
pose (H := Z.eq_dec n i); Zlength_simplify_in_all; destruct H; Znth_solve2
end
| match goal with
| H0 : context [Znth ?n (map ?f ?l)] |- _ =>
unshelve erewrite @Znth_map in H0 by Zlength_solve
(* only 1 : auto with typeclass_instances *)
end
].
(*************** fapply & fassumption *************)
Lemma imp_refl' : forall P Q : Prop,
P = Q -> P -> Q.
Proof. intros. congruence. Qed.
Ltac fapply H :=
match type of H with ?HH =>
eapply (imp_refl' HH); only 2 : exact H
end.
Ltac fassumption :=
first
[ assumption
| match goal with
| H : _ |- _ => fapply H; repeat f_equal; match goal with |- (_ = _)%Z => idtac end; Zlength_solve
end
].
(* Begin: copied from veric/Coqlib4.v *)
Lemma prop_unext: forall P Q: Prop, P=Q -> (P<->Q).
Proof. intros. subst; split; auto. Qed.
(* End: copied from veric/Coqlib4.v *)
(******************* eq_solve ********************)
Ltac eq_solve_with tac :=
solve [
repeat multimatch goal with
| |- @eq Z _ _ => lia
| |- @eq (_ -> _) _ _ => apply functional_extensionality; intros
| |- _ <-> _ => apply prop_unext
| _ => f_equal
| _ => tac
end
].
Tactic Notation "eq_solve" "with" tactic(tac) := eq_solve_with (tac).
Tactic Notation "eq_solve" := eq_solve with fail.
#[export] Hint Extern 1 (@eq _ _ _) => eq_solve : Znth_solve_hint.
(* #[export] Hint Extern 1 (@eq _ _ _) => fassumption : Znth_solve_hint.
#[export] Hint Extern 1 (@eq _ _ _) => congruence : Znth_solve_hint. *)
(*************** range definitions **********************)
Definition forall_i (lo hi : Z) (P : Z -> Prop) :=
forall i, lo <= i < hi -> P i.
Definition forall_range {A : Type} {d : Inhabitant A} (lo hi : Z) (l : list A) (P : A -> Prop) :=
forall_i lo hi (fun i => P (Znth i l)).
Definition forall_i_range {A : Type} {d : Inhabitant A} (lo hi : Z) (l : list A) (P : Z -> A -> Prop) :=
forall_i lo hi (fun i => P i (Znth i l)).
Definition forall_range2 {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al : list A) (bl : list B) (P : A -> B -> Prop) :=
forall_i lo hi (fun i => P (Znth i al) (Znth (i + offset) bl)).
Definition forall_triangle {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (al : list A) (bl : list B) (P : A -> B -> Prop) :=
forall i j, x1 <= i < x2 /\ y1 <= j < y2 /\ i <= j + offset -> P (Znth i al) (Znth j bl).
(****************** range lemmas ************************)
Lemma rangei_split : forall (lo mi hi : Z) (P : Z -> Prop),
lo <= mi <= hi ->
forall_i lo hi P <-> forall_i lo mi P /\ forall_i mi hi P.
Proof.
intros. unfold forall_i. split; intros.
- (* -> *)
split; intros; apply H0; lia.
- (* <- *)
destruct H0.
destruct (Z_lt_le_dec i mi).
+ apply H0; lia.
+ apply H2; lia.
Qed.
Lemma rangei_implies : forall (lo hi : Z) (P Q : Z -> Prop),
forall_i lo hi (fun i => P i -> Q i) ->
forall_i lo hi P ->
forall_i lo hi Q.
Proof. unfold forall_i; intros; auto. Qed.
Lemma rangei_iff : forall (lo hi : Z) (P Q : Z -> Prop),
forall_i lo hi (fun i => P i <-> Q i) ->
forall_i lo hi P <-> forall_i lo hi Q.
Proof.
intros. split; apply rangei_implies; unfold forall_i in *; intros; apply H; auto.
Qed.
Lemma rangei_shift : forall (lo hi offset : Z) (P : Z -> Prop),
forall_i lo hi P <-> forall_i (lo + offset) (hi + offset) (fun i => P (i - offset)).
Proof.
intros. unfold forall_i. split; intros.
- (* -> *)
apply H; lia.
- (* <- *)
replace i with (i + offset - offset) by lia.
apply H; lia.
Qed.
Lemma rangei_and : forall (lo hi : Z) (P Q : Z -> Prop),
forall_i lo hi (fun i => P i /\ Q i) <->
forall_i lo hi P /\ forall_i lo hi Q.
Proof.
unfold forall_i; intros; split; intros.
+ split; intros; specialize (H i ltac:(assumption)); tauto.
+ destruct H. auto.
Qed.
Lemma rangei_shift' : forall (lo hi lo' hi' : Z) (P Q : Z -> Prop),
let offset := lo' - lo in
offset = hi' - hi ->
forall_i lo' hi' (fun i => Q i <-> P (i - offset)) ->
forall_i lo hi P <-> forall_i lo' hi' Q.
Proof.
intros.
replace lo' with (lo + offset) by lia.
replace hi' with (hi + offset) by lia.
rewrite rangei_iff with (P := Q) (Q := fun i => P (i - offset)) by fassumption.
apply rangei_shift.
Qed.
Lemma forall_range_empty : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (l : list A) (P : A -> Prop),
lo = hi ->
forall_range lo hi l P <->
True.
Proof.
intros; split; unfold forall_range, forall_i; intros; auto; lia.
Qed.
Lemma forall_range_Zrepeat : forall {A : Type} {d : Inhabitant A} (lo hi n : Z) (x : A) (P : A -> Prop),
0 <= lo < hi /\ hi <= n ->
forall_range lo hi (Zrepeat x n) P ->
P x.
Proof.
unfold forall_range, forall_i. intros.
fapply (H0 lo ltac:(lia)). f_equal. Znth_solve.
Qed.
Lemma forall_range_app1 : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : A -> Prop),
0 <= lo <= hi /\ hi <= Zlength al ->
forall_range lo hi (al ++ bl) P <->
forall_range lo hi al P.
Proof.
unfold forall_range. intros. apply rangei_iff.
unfold forall_i. intros. apply prop_unext, f_equal. Znth_solve.
Qed.
Lemma forall_range_app2 : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : A -> Prop),
Zlength al <= lo <= hi /\ hi <= Zlength al + Zlength bl ->
forall_range lo hi (al ++ bl) P ->
forall_range (lo - Zlength al) (hi - Zlength al) bl P.
Proof.
unfold forall_range. intros. eapply rangei_shift'. 3 : apply H0.
+ lia.
+ unfold forall_i. intros. apply prop_unext, f_equal. Znth_solve.
Qed.
Lemma forall_range_app : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : A -> Prop),
0 <= lo <= Zlength al /\ Zlength al <= hi <= Zlength al + Zlength bl ->
forall_range lo hi (al ++ bl) P ->
forall_range lo (Zlength al) al P /\
forall_range 0 (hi - Zlength al) bl P.
Proof.
unfold forall_range. intros. split; intro; intros.
- specialize (H0 i ltac:(lia)). simpl in H0. autorewrite with Znth in H0. apply H0.
- specialize (H0 (i + Zlength al) ltac:(lia)). simpl in H0. autorewrite with Znth in H0. fassumption.
Qed.
Lemma forall_range_upd_Znth : forall {A : Type} {d : Inhabitant A} (lo hi i : Z) (al : list A) (x : A) (P : A -> Prop),
0 <= i < Zlength al ->
forall_range lo hi (upd_Znth i al x) P <->
forall_range lo hi (sublist 0 i al ++ (Zrepeat x 1) ++ sublist (i+1) (Zlength al) al) P.
Proof.
intros.
rewrite upd_Znth_unfold by Zlength_solve.
rewrite cons_Zrepeat_1_app, app_nil_r.
reflexivity.
Qed.
Lemma forall_range_sublist : forall {A : Type} {d : Inhabitant A} (lo hi lo' hi' : Z) (l : list A) (P : A -> Prop),
0 <= lo <= hi /\ hi <= hi' - lo' /\ 0 <= lo' <= hi' /\ hi' <= Zlength l ->
forall_range lo hi (sublist lo' hi' l) P ->
forall_range (lo+lo') (hi+lo') l P.
Proof.
unfold forall_range, forall_i. intros.
fapply (H0 (i - lo') ltac:(lia)). f_equal. Znth_solve.
Qed.
Lemma forall_range_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi : Z) (l : list A) (f : A -> B) (P : B -> Prop),
0 <= lo <= hi /\ hi <= Zlength l ->
forall_range lo hi (map f l) P ->
forall_range lo hi l (fun x => P (f x)).
Proof.
unfold forall_range, forall_i. intros.
fapply (H0 i ltac:(lia)). f_equal. rewrite Znth_map by lia. auto.
Qed.
Lemma rangei_uni_empty : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (l : list A) (P : Z -> A -> Prop),
lo = hi->
forall_i_range lo hi l P <->
True.
Proof.
intros; split; unfold forall_i_range, forall_i; intros; auto; lia.
Qed.
Lemma rangei_uni_Zrepeat : forall {A : Type} {d : Inhabitant A} (lo hi n : Z) (x : A) (P : Z -> A -> Prop),
0 <= lo < hi /\ hi <= n ->
forall_i_range lo hi (Zrepeat x n) P ->
forall_i lo hi (fun i => P i x).
Proof.
unfold forall_i_range, forall_i. intros.
fapply (H0 i ltac:(lia)). f_equal. Znth_solve.
Qed.
Lemma rangei_uni_app1 : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : Z -> A -> Prop),
0 <= lo <= hi /\ hi <= Zlength al ->
forall_i_range lo hi (al ++ bl) P <->
forall_i_range lo hi al P.
Proof.
unfold forall_i_range. intros. apply rangei_iff.
unfold forall_i. intros. apply prop_unext. f_equal. Znth_solve.
Qed.
Lemma rangei_uni_app2 : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : Z -> A -> Prop),
Zlength al <= lo <= hi /\ hi <= Zlength al + Zlength bl ->
forall_i_range lo hi (al ++ bl) P ->
forall_i_range (lo - Zlength al) (hi - Zlength al) bl (fun i => P (i + Zlength al)).
Proof.
unfold forall_i_range. intros. eapply rangei_shift'. 3 : apply H0.
+ lia.
+ unfold forall_i. intros. apply prop_unext. f_equal.
- lia.
- Znth_solve.
Qed.
Lemma rangei_uni_app : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (al bl : list A) (P : Z -> A -> Prop),
0 <= lo <= Zlength al /\ Zlength al <= hi <= Zlength al + Zlength bl ->
forall_i_range lo hi (al ++ bl) P ->
forall_i_range lo (Zlength al) al P /\
forall_i_range 0 (hi - Zlength al) bl (fun i => P (i + Zlength al)).
Proof.
unfold forall_i_range. intros. split; intro; intros.
- specialize (H0 i ltac:(lia)). simpl in H0. autorewrite with Znth in H0. apply H0.
- specialize (H0 (i + Zlength al) ltac:(lia)). simpl in H0. autorewrite with Znth in H0. fassumption.
Qed.
Lemma rangei_uni_sublist : forall {A : Type} {d : Inhabitant A}
(lo hi lo' hi' : Z) (l : list A) (P : Z -> A -> Prop),
0 <= lo <= hi /\ hi <= hi' - lo' /\ 0 <= lo' <= hi' /\ hi' <= Zlength l ->
forall_i_range lo hi (sublist lo' hi' l) P ->
forall_i_range (lo+lo') (hi+lo') l (fun i => P (i - lo')).
Proof.
unfold forall_i_range, forall_i. intros.
fapply (H0 (i - lo') ltac:(lia)). f_equal. Znth_solve.
Qed.
Lemma rangei_uni_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi : Z) (l : list A) (f : A -> B) (P : Z -> B -> Prop),
0 <= lo <= hi /\ hi <= Zlength l ->
forall_i_range lo hi (map f l) P ->
forall_i_range lo hi l (fun i x => P i (f x)).
Proof.
unfold forall_i_range, forall_i. intros.
fapply (H0 i ltac:(lia)). f_equal. rewrite Znth_map by lia. auto.
Qed.
Lemma forall_range2_rangei_uniA : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al : list A) (bl : list B) (P : A -> B -> Prop),
forall_range2 lo hi offset al bl P <->
forall_i_range lo hi al (fun i x => P x (Znth (i + offset) bl)).
Proof.
unfold forall_range2, forall_i_range, forall_i. reflexivity.
Qed.
Lemma forall_range2_rangei_uniB : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al : list A) (bl : list B) (P : A -> B -> Prop),
forall_range2 lo hi offset al bl P <->
forall_i_range (lo+offset) (hi+offset) bl (fun i => P (Znth (i - offset) al)).
Proof.
unfold forall_range2, forall_i_range.
intros. split; intros.
+ eapply rangei_shift'. 3 : exact H.
- lia.
- unfold forall_i. intros. eq_solve.
+ eapply rangei_shift'. 3 : exact H.
- eq_solve.
- unfold forall_i. intros. eq_solve.
Qed.
Lemma forall_range2_empty : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
lo = hi ->
forall_range2 lo hi offset l l' P <->
True.
Proof.
intros; split; unfold forall_range2, forall_i; intros; auto; lia.
Qed.
Lemma forall_range2A_app1 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
0 <= lo <= hi /\ hi <= Zlength al ->
forall_range2 lo hi offset (al ++ bl) l' P <->
forall_range2 lo hi offset al l' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniA. apply rangei_uni_app1.
Qed.
Lemma forall_range2A_app2 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
Zlength al <= lo <= hi /\ hi <= Zlength al + Zlength bl ->
forall_range2 lo hi offset (al ++ bl) l' P ->
forall_range2 (lo - Zlength al) (hi - Zlength al) (offset + Zlength al) bl l' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniA. intros.
apply rangei_uni_app2 in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2A_app : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
0 <= lo <= Zlength al /\ Zlength al <= hi <= Zlength al + Zlength bl ->
forall_range2 lo hi offset (al ++ bl) l' P ->
forall_range2 lo (Zlength al) offset al l' P /\
forall_range2 0 (hi - Zlength al) (offset + Zlength al) bl l' P.
Proof.
intros *. do 3 rewrite forall_range2_rangei_uniA. intros.
apply rangei_uni_app in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2A_upd_Znth : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset i : Z) (l : list A) (x : A) (l' : list B) (P : A -> B -> Prop),
0 <= i < Zlength l ->
forall_range2 lo hi offset (upd_Znth i l x) l' P <->
forall_range2 lo hi offset (sublist 0 i l ++ (Zrepeat x 1) ++ sublist (i+1) (Zlength l) l) l' P.
Proof.
intros.
rewrite upd_Znth_unfold by Zlength_solve.
rewrite cons_Zrepeat_1_app, app_nil_r.
reflexivity.
Qed.
Lemma forall_range2A_sublist : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi lo' hi' offset : Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
0 <= lo <= hi /\ hi <= hi' - lo' /\ 0 <= lo' <= hi' /\ hi' <= Zlength l ->
forall_range2 lo hi offset (sublist lo' hi' l) l' P ->
forall_range2 (lo+lo') (hi+lo') (offset-lo') l l' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniA. intros.
apply rangei_uni_sublist in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2A_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B} {C : Type} {dc : Inhabitant C}
(lo hi offset : Z) (l : list A) (l' : list B) (f : A -> C) (P : C -> B -> Prop),
0 <= lo <= hi /\ hi <= Zlength l ->
forall_range2 lo hi offset (map f l) l' P ->
forall_range2 lo hi offset l l' (fun x => P (f x)).
Proof.
unfold forall_range2, forall_i. intros.
fapply (H0 i ltac:(lia)).
eq_solve with (rewrite Znth_map by lia).
Qed.
Lemma forall_range2A_Zrepeat : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi n offset : Z) (x : A) (l' : list B) (P : A -> B -> Prop),
0 <= lo < hi /\ hi <= n ->
forall_range2 lo hi offset (Zrepeat x n) l' P ->
forall_range (lo + offset) (hi + offset) l' (P x).
Proof.
unfold forall_range2, forall_range. intros.
eapply rangei_shift'. 3 : exact H0.
+ lia.
+ unfold forall_i. intros. eq_solve with Znth_solve.
Qed.
Lemma forall_range2B_app1 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
0 <= lo + offset <= hi + offset /\ hi + offset <= Zlength al' ->
forall_range2 lo hi offset l (al' ++ bl') P <->
forall_range2 lo hi offset l al' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniB. apply rangei_uni_app1.
Qed.
Lemma forall_range2B_app2 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
Zlength al' <= lo + offset <= hi + offset /\ hi + offset <= Zlength al' + Zlength bl' ->
forall_range2 lo hi offset l (al' ++ bl') P ->
forall_range2 lo hi (offset - Zlength al') l bl' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniB. intros.
apply rangei_uni_app2 in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2B_app : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
0 <= lo + offset <= Zlength al' /\ Zlength al' <= hi + offset <= Zlength al' + Zlength bl' ->
forall_range2 lo hi offset l (al' ++ bl') P ->
forall_range2 lo (Zlength al' - offset) offset l al' P /\
forall_range2 (Zlength al' - offset) hi (offset - Zlength al') l bl' P.
Proof.
intros *. do 3 rewrite forall_range2_rangei_uniB. intros.
apply rangei_uni_app in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2B_upd_Znth : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi offset i : Z) (l : list A) (l' : list B) (x : B) (P : A -> B -> Prop),
0 <= i < Zlength l' ->
forall_range2 lo hi offset l (upd_Znth i l' x) P <->
forall_range2 lo hi offset l (sublist 0 i l' ++ (Zrepeat x 1) ++ sublist (i+1) (Zlength l') l') P.
Proof.
intros.
rewrite upd_Znth_unfold by Zlength_solve.
rewrite cons_Zrepeat_1_app, app_nil_r.
reflexivity.
Qed.
Lemma forall_range2B_sublist : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi lo' hi' offset : Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
0 <= lo + offset <= hi + offset /\ hi + offset <= hi' - lo' /\ 0 <= lo' <= hi' /\ hi' <= Zlength l' ->
forall_range2 lo hi offset l (sublist lo' hi' l') P ->
forall_range2 lo hi (offset + lo') l l' P.
Proof.
intros *. do 2 rewrite forall_range2_rangei_uniB. intros.
apply rangei_uni_sublist in H0. 2 : assumption.
fapply H0. eq_solve.
Qed.
Lemma forall_range2B_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B} {C : Type} {dc : Inhabitant C}
(lo hi offset : Z) (l : list A) (l' : list B) (f : B -> C) (P : A -> C -> Prop),
0 <= lo + offset <= hi + offset /\ hi + offset <= Zlength l' ->
forall_range2 lo hi offset l (map f l') P ->
forall_range2 lo hi offset l l' (fun x y => P x (f y)).
Proof.
unfold forall_range2, forall_i. intros.
fapply (H0 i ltac:(lia)).
eq_solve with (rewrite Znth_map by lia).
Qed.
Lemma forall_range2B_Zrepeat : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi n offset : Z) (l : list A) (x : B) (P : A -> B -> Prop),
0 <= lo + offset < hi + offset /\ hi + offset <= n ->
forall_range2 lo hi offset l (Zrepeat x n) P ->
forall_range lo hi l (fun y => P y x).
Proof.
unfold forall_range2, forall_range. intros.
eapply rangei_shift'. 3 : exact H0.
+ lia.
+ unfold forall_i. intros. eq_solve with Znth_solve.
Qed.
Lemma forall_triangle_rangei_uniA : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (al : list A) (bl : list B) (P : A -> B -> Prop),
forall_triangle x1 x2 y1 y2 offset al bl P <->
forall_i_range x1 x2 al (fun i x => forall_i_range y1 y2 bl (fun j y => i <= j + offset -> P x y)).
Proof.
unfold forall_triangle, forall_i_range, forall_i. intuition.
Qed.
Lemma forall_triangle_rangei_uniB : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (al : list A) (bl : list B) (P : A -> B -> Prop),
forall_triangle x1 x2 y1 y2 offset al bl P <->
forall_i_range y1 y2 bl (fun j y => forall_i_range x1 x2 al (fun i x => i <= j + offset -> P x y)).
Proof.
unfold forall_triangle, forall_i_range, forall_i. intuition.
Qed.
Lemma forall_triangle_emptyA : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
x1 = x2 ->
forall_triangle x1 x2 y1 y2 offset l l' P <->
True.
Proof.
intros; split; unfold forall_triangle; intros; auto; lia.
Qed.
Lemma forall_triangle_emptyB : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
y1 = y2 ->
forall_triangle x1 x2 y1 y2 offset l l' P <->
True.
Proof.
intros; split; unfold forall_triangle; intros; auto; lia.
Qed.
Lemma forall_triangle_emptyAgtB : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
x1 >= y2 + offset ->
forall_triangle x1 x2 y1 y2 offset l l' P <->
True.
Proof.
intros; split; unfold forall_triangle; intros; auto; lia.
Qed.
Lemma forall_triangleA_app1 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
0 <= x1 <= x2 /\ x2 <= Zlength al ->
forall_triangle x1 x2 y1 y2 offset (al ++ bl) l' P <->
forall_triangle x1 x2 y1 y2 offset al l' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniA. apply rangei_uni_app1.
Qed.
Lemma forall_triangleA_app2 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset: Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
Zlength al <= x1 <= x2 /\ x2 <= Zlength al + Zlength bl ->
forall_triangle x1 x2 y1 y2 offset (al ++ bl) l' P ->
forall_triangle (x1 - Zlength al) (x2 - Zlength al) y1 y2 (offset - Zlength al) bl l' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniA. intros.
apply rangei_uni_app2 in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleA_app : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (al bl : list A) (l' : list B) (P : A -> B -> Prop),
0 <= x1 <= Zlength al /\ Zlength al <= x2 <= Zlength al + Zlength bl ->
forall_triangle x1 x2 y1 y2 offset (al ++ bl) l' P ->
forall_triangle x1 (Zlength al) y1 y2 offset al l' P /\
forall_triangle 0 (x2 - Zlength al) y1 y2 (offset - Zlength al) bl l' P.
Proof.
intros *. do 3 rewrite forall_triangle_rangei_uniA. intros.
apply rangei_uni_app in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleA_upd_Znth : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset i : Z) (l : list A) (x : A) (l' : list B) (P : A -> B -> Prop),
0 <= i < Zlength l ->
forall_triangle x1 x2 y1 y2 offset (upd_Znth i l x) l' P <->
forall_triangle x1 x2 y1 y2 offset (sublist 0 i l ++ (Zrepeat x 1) ++ sublist (i+1) (Zlength l) l) l' P.
Proof.
intros.
rewrite upd_Znth_unfold by Zlength_solve.
rewrite cons_Zrepeat_1_app, app_nil_r.
reflexivity.
Qed.
Lemma forall_triangleA_sublist : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(lo hi x1 x2 y1 y2 offset : Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
0 <= x1 <= x2 /\ x2 <= hi - lo /\ 0 <= lo <= hi /\ hi <= Zlength l ->
forall_triangle x1 x2 y1 y2 offset (sublist lo hi l) l' P ->
forall_triangle (x1 + lo) (x2 + lo) y1 y2 (offset + lo) l l' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniA. intros.
apply rangei_uni_sublist in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleA_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B} {C : Type} {dc : Inhabitant C}
(x1 x2 y1 y2 offset : Z) (l : list A) (l' : list B) (f : A -> C) (P : C -> B -> Prop),
0 <= x1 <= x2 /\ x2 <= Zlength l ->
forall_triangle x1 x2 y1 y2 offset (map f l) l' P ->
forall_triangle x1 x2 y1 y2 offset l l' (fun x => P (f x)).
Proof.
unfold forall_triangle, forall_i. intros.
fapply (H0 i j ltac:(lia)).
eq_solve with (rewrite Znth_map by lia).
Qed.
Lemma forall_triangleA_Zrepeat : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(n x1 x2 y1 y2 offset : Z) (x : A) (l' : list B) (P : A -> B -> Prop),
0 <= x1 < x2 /\ x2 <= n ->
forall_triangle x1 x2 y1 y2 offset (Zrepeat x n) l' P <->
forall_range (Z.max y1 (x1 - offset)) (Z.max y2 (x1 - offset)) l' (P x).
Proof.
unfold forall_triangle, forall_range, forall_i. intros. split; intros.
- fapply (H0 x1 i ltac:(lia)).
eq_solve with (rewrite Znth_Zrepeat by lia).
- fapply (H0 j ltac:(lia)).
eq_solve with (rewrite Znth_Zrepeat by lia).
Qed.
Lemma forall_triangleB_app1 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
0 <= y1 <= y2 /\ y2 <= Zlength al' ->
forall_triangle x1 x2 y1 y2 offset l (al' ++ bl') P <->
forall_triangle x1 x2 y1 y2 offset l al' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniB. apply rangei_uni_app1.
Qed.
Lemma forall_triangleB_app2 : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
Zlength al' <= y1 <= y2 /\ y2 <= Zlength al' + Zlength bl' ->
forall_triangle x1 x2 y1 y2 offset l (al' ++ bl') P ->
forall_triangle x1 x2 (y1 - Zlength al') (y2 - Zlength al') (offset + Zlength al') l bl' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniB. intros.
apply rangei_uni_app2 in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleB_app : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset : Z) (l : list A) (al' bl' : list B) (P : A -> B -> Prop),
0 <= y1 <= Zlength al' /\ Zlength al' <= y2 <= Zlength al' + Zlength bl' ->
forall_triangle x1 x2 y1 y2 offset l (al' ++ bl') P ->
forall_triangle x1 x2 y1 (Zlength al') offset l al' P /\
forall_triangle x1 x2 0 (y2 - Zlength al') (offset + Zlength al') l bl' P.
Proof.
intros *. do 3 rewrite forall_triangle_rangei_uniB. intros.
apply rangei_uni_app in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleB_upd_Znth : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 offset i : Z) (l : list A) (l' : list B) (x : B) (P : A -> B -> Prop),
0 <= i < Zlength l' ->
forall_triangle x1 x2 y1 y2 offset l (upd_Znth i l' x) P <->
forall_triangle x1 x2 y1 y2 offset l (sublist 0 i l' ++ (Zrepeat x 1) ++ sublist (i+1) (Zlength l') l') P.
Proof.
intros.
rewrite upd_Znth_unfold by Zlength_solve.
rewrite cons_Zrepeat_1_app, app_nil_r.
reflexivity.
Qed.
Lemma forall_triangleB_sublist : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 lo hi offset : Z) (l : list A) (l' : list B) (P : A -> B -> Prop),
0 <= y1 <= y2 /\ y2 <= hi - lo /\ 0 <= lo <= hi /\ hi <= Zlength l' ->
forall_triangle x1 x2 y1 y2 offset l (sublist lo hi l') P ->
forall_triangle x1 x2 (y1 + lo) (y2 + lo) (offset - lo) l l' P.
Proof.
intros *. do 2 rewrite forall_triangle_rangei_uniB. intros.
apply rangei_uni_sublist in H0. 2 : assumption.
fapply H0.
repeat first [
progress f_equal
| apply extensionality; intros
].
apply prop_ext. split; intros; apply H1; lia.
Qed.
Lemma forall_triangleB_map : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B} {C : Type} {dc : Inhabitant C}
(x1 x2 y1 y2 offset : Z) (l : list A) (l' : list B) (f : B -> C) (P : A -> C -> Prop),
0 <= y1 <= y2 /\ y2 <= Zlength l' ->
forall_triangle x1 x2 y1 y2 offset l (map f l') P ->
forall_triangle x1 x2 y1 y2 offset l l' (fun x y => P x (f y)).
Proof.
unfold forall_triangle, forall_i. intros.
fapply (H0 i j ltac:(lia)).
eq_solve with (rewrite Znth_map by lia).
Qed.
Lemma forall_triangleB_Zrepeat : forall {A : Type} {da : Inhabitant A} {B : Type} {db : Inhabitant B}
(x1 x2 y1 y2 n offset : Z) (l : list A) (x : B) (P : A -> B -> Prop),
0 <= y1 < y2 /\ y2 <= n ->
forall_triangle x1 x2 y1 y2 offset l (Zrepeat x n) P <->
forall_range (Z.min x1 (y2 + offset)) (Z.min x2 (y2 + offset)) l (fun y => P y x).
Proof.
unfold forall_triangle, forall_range, forall_i. intros. split; intros.
- fapply (H0 i (y2 - 1) ltac:(lia)).
eq_solve with (rewrite Znth_Zrepeat by lia).
- fapply (H0 i ltac:(lia)).
eq_solve with (rewrite Znth_Zrepeat by lia).
Qed.
(** * range_form *)
(** range_form is a tactic that rewrites properties using quantifier or equantion
of list to range properties defined above. *)
Ltac apply_in lem H :=
apply lem in H.
Ltac apply_in_hyps' lem :=
repeat match goal with
| H : ?T |- _ => apply lem in H; let n := numgoals in guard n = 1
end.
Tactic Notation "apply_in_hyps" uconstr(lem) := apply_in_hyps' lem.
Ltac apply_in_hyps_with_Zlength_solve lem :=
repeat match goal with
| H : _ |- _ => first [apply -> lem in H | apply lem in H]; [idtac | Zlength_solve ..]
end.
Ltac apply_in_hyps_with_Zlength_solve_destruct lem :=
repeat match goal with
| H : _ |- _ => first [apply -> lem in H | apply lem in H]; [destruct H | Zlength_solve ..]
end.
Lemma not_In_forall_range_iff : forall {A : Type} {d : Inhabitant A} (x : A) (l : list A),
~ In x l <-> forall_range 0 (Zlength l) l (fun e => e <> x).
Proof.
unfold forall_range, forall_i. intros; split; intros.
- intro. apply H. subst x. apply Znth_In. auto.
- intro. induction l; auto.
inversion H0.
+ subst a. apply H with 0. Zlength_solve.
autorewrite with sublist. auto.
+ apply IHl; auto. intros.
specialize (H (i+1) ltac:(Zlength_solve)). autorewrite with list_solve_rewrite Znth in *.
fassumption.
Qed.
Lemma not_In_forall_range : forall {A : Type} {d : Inhabitant A} (x : A) (l : list A),
~ In x l -> forall_range 0 (Zlength l) l (fun e => e <> x).
Proof. intros. apply not_In_forall_range_iff. auto. Qed.
Lemma eq_forall_range2_no_offset : forall {A : Type} {d : Inhabitant A} (lo hi : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth i l1 = Znth i l2) ->
forall_range2 lo hi 0 l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. replace (i + 0) with i by lia. auto. Qed.
Lemma eq_forall_range2_offset : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth i l1 = Znth (i + offset) l2) ->
forall_range2 lo hi offset l1 l2 eq.
Proof. unfold forall_range2, forall_i. auto. Qed.
Lemma eq_forall_range2_left_offset : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth i l1 = Znth (offset + i) l2) ->
forall_range2 lo hi offset l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. replace (i + offset) with (offset + i) by lia. auto. Qed.
Lemma eq_forall_range2_minus_offset : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth i l1 = Znth (i - offset) l2) ->
forall_range2 lo hi (-offset) l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. replace (i + - offset) with (i - offset) by lia. auto. Qed.
Lemma eq_forall_range2_reverse : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth (i + offset) l1 = Znth i l2) ->
forall_range2 (lo + offset) (hi + offset) (-offset) l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. fapply (H (i - offset) ltac:(lia)). eq_solve. Qed.
Lemma eq_forall_range2_reverse_left_offset : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth (offset + i) l1 = Znth i l2) ->
forall_range2 (lo + offset) (hi + offset) (-offset) l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. fapply (H (i - offset) ltac:(lia)). eq_solve. Qed.
Lemma eq_forall_range2_reverse_minus_offset : forall {A : Type} {d : Inhabitant A} (lo hi offset : Z) (l1 l2 : list A),
(forall i, lo <= i < hi -> Znth (i - offset) l1 = Znth i l2) ->
forall_range2 (lo - offset) (hi - offset) offset l1 l2 eq.
Proof. unfold forall_range2, forall_i. intros. fapply (H (i + offset) ltac:(lia)). eq_solve. Qed.
Lemma In_Znth_iff : forall {A : Type} {d : Inhabitant A} (l : list A) (x : A),
In x l <-> exists i, 0 <= i < Zlength l /\ Znth i l = x.
Proof.
intros. split; intro.
- induction l; inversion H.
+ exists 0. autorewrite with sublist. split; auto. pose proof (Zlength_nonneg l); lia.
+ specialize (IHl H0). destruct IHl as [i []].
exists (i + 1). autorewrite with sublist. rewrite Znth_pos_cons by lia.
rewrite Z.add_simpl_r. split; auto. lia.
- destruct H as [i []]. subst x. apply Znth_In. auto.
Qed.
Lemma list_eq_forall_range2 : forall {A} {d : Inhabitant A} al bl,
al = bl ->
Zlength al = Zlength bl /\ forall_range2 0 (Zlength al) 0 al bl eq.
Proof.
intros. subst; unfold forall_range2, forall_i; intuition.
Qed.