-
Notifications
You must be signed in to change notification settings - Fork 1
/
manger_llex.v
1567 lines (1418 loc) · 47.2 KB
/
manger_llex.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.Lists.List.
Require Import Coq.Strings.String.
Require Import CAS.coq.common.compute.
Require Import CAS.coq.po.properties.
Require Import CAS.coq.po.trivial.
Require Import CAS.coq.eqv.properties.
Require Import CAS.coq.eqv.product.
Require Import CAS.coq.eqv.set.
Require Import CAS.coq.eqv.reduce.
Require Import CAS.coq.eqv.minset.
Require Import CAS.coq.eqv.manger_sets.
Require Import CAS.coq.sg.properties.
Require Import CAS.coq.sg.reduce.
Require Import CAS.coq.sg.union.
Require Import CAS.coq.sg.minset_union.
Require Import CAS.coq.theory.set.
Require Import CAS.coq.uop.properties.
Require Import CAS.coq.uop.commutative_composition.
Import ListNotations.
(*
A = type of active component
P = type of passive component
See cas/coq/uop/commutative_composition.v
for a description of the composition
of two reductions, r1 and 2, that
commute:
r1 (r1 s) = r1 (r2 s).
I believe this is the case for our manger reductions:
r1 = uop_manger_phase_1
r2 = uop_manger_phase_2
both considered as reductions over
b = bop_union (brel_product eqA eqP).
*)
Section Theory.
Variables
(A P : Type)
(zeroP : P) (* 0 *)
(eqA lteA : brel A)
(eqP : brel P)
(addP : binary_op P)
(wA : A)
(wP : P)
(fA : A -> A)
(ntA : brel_not_trivial A eqA fA)
(conA : brel_congruence A eqA eqA)
(refA : brel_reflexive A eqA)
(symA : brel_symmetric A eqA)
(trnA : brel_transitive A eqA)
(conP : brel_congruence P eqP eqP)
(cong_addP : bop_congruence P eqP addP)
(refP : brel_reflexive P eqP)
(symP : brel_symmetric P eqP)
(trnP : brel_transitive P eqP)
(conLte : brel_congruence A eqA lteA)
(refLte : brel_reflexive A lteA)
(trnLte : brel_transitive A lteA)
(ntot : brel_not_total A lteA)
(addP_assoc : bop_associative P eqP addP)
(addP_com : bop_commutative P eqP addP)
(* idempotence is baked in this addP_gen_idempotent but it can be proved *)
(zeropLid : ∀ (p : P), eqP (addP zeroP p) p = true)
(zeropRid : ∀ (p : P), eqP (addP p zeroP) p = true)
(addP_gen_idempotent : ∀ x y : P, eqP x y = true → eqP (addP x y) y = true).
Local Definition eqAP : brel (A * P)
:= brel_product eqA eqP.
Local Definition eqSAP : brel (finite_set (A * P))
:= brel_set eqAP.
Local Definition bSAP : binary_op (finite_set (A * P))
:= bop_union eqAP.
Lemma conAP : brel_congruence (A * P) eqAP eqAP.
Proof. apply brel_product_congruence; auto. Qed.
Lemma refAP : brel_reflexive (A * P) eqAP.
Proof. apply brel_product_reflexive; auto. Qed.
Lemma symAP : brel_symmetric (A * P) eqAP.
Proof. apply brel_product_symmetric; auto. Qed.
Lemma trnAP : brel_transitive (A * P) eqAP.
Proof. apply brel_product_transitive; auto. Qed.
Lemma conSAP : brel_congruence (finite_set (A * P)) eqSAP eqSAP.
Proof. apply brel_set_congruence.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
Lemma refSAP : brel_reflexive (finite_set (A * P)) eqSAP.
Proof. apply brel_set_reflexive.
- apply refAP.
- apply symAP.
Qed.
Lemma symSAP : brel_symmetric (finite_set (A * P)) eqSAP.
Proof. apply brel_set_symmetric. Qed.
Lemma trnSAP : brel_transitive (finite_set (A * P)) eqSAP.
Proof. apply brel_set_transitive.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
Lemma bSAP_cong : bop_congruence _ eqSAP bSAP.
Proof. apply bop_union_congruence.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
Lemma bSAP_associative : bop_associative _ eqSAP bSAP.
Proof. apply bop_union_associative.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
Lemma bSAP_commutative : bop_commutative _ eqSAP bSAP.
Proof. apply bop_union_commutative.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
Lemma bSAP_idempotent : bop_idempotent _ eqSAP bSAP.
Proof. apply bop_union_idempotent.
- apply refAP.
- apply symAP.
- apply trnAP.
Qed.
(* Ha! this is interesting.
The witness from sg.union.bop_union_not_selective for our type eqAP is (X, Y), where
X = (wA, wP) ::nil
Y = (fA wA, wP) ::nil
where
Variable fA : A -> A.
Variable ntA brel_not_trivial A eqA fA.
Ouch! This does not work for Lemma nsel_witness_is_a_reduction_witness below.
That lemma needs
uop_manger { (wA, wP), (fA wA, wP)} not in { { (wA, wP) }, { (fA wA, wP)}}
BUT, wA <> fA wA, so the minset of P2 will choose one of them!!! Ouch!
Solution? It seems we need to come up with a bespoke proof of bSAP_not_selective
just for this file, so that the witness makes this Lemma true.
But the witness has to be simple enough so that
we can prove fst_nsel_witness_is_fixed_point and snd_nsel_witness_is_fixed_point.
So, we need X and Y such that
1) uop_manger X = X
2) uop_manger Y = Y
3) uop_manger (X union Y) not in {X, Y}.
This can be done. Here's how.
The whole scheme of manger only makes sence when lteA is a partial order.
So, let (a1, a2) be the witness for (ntot : brel_not_total A lteA),
then
X = {
Y = {(a2, wP)}
will work since
uop_manger {(a1, wP), (a2, wP)} = {(a1, wP), (a2, wP)}.
Here is the bespoke proof of lack of selectivity for union:
*)
Lemma bSAP_not_selective : bop_not_selective _ eqSAP bSAP.
Proof. destruct ntot as [[a1 a2] [L R]].
exists ((a1, wP)::nil, (a2, wP)::nil). split.
- case_eq(eqSAP (bSAP ((a1, wP) :: nil) ((a2, wP) :: nil)) ((a1, wP) :: nil)); intro H1; auto.
apply brel_set_elim_prop in H1.
destruct H1 as [H1 H2].
assert (H3 : in_set eqAP (bSAP ((a1, wP) :: nil) ((a2, wP) :: nil)) (a2, wP) = true).
{
apply in_set_bop_union_intro.
+ apply symAP.
+ apply trnAP.
+ right. apply in_set_cons_intro.
* apply symAP.
* left. apply refAP.
}
assert (H4 := H1 _ H3).
apply in_set_cons_elim in H4.
+ destruct H4 as [H4 | H4].
* apply brel_product_elim in H4. destruct H4 as [H4 H5].
assert (H6 := refLte a1).
assert (H7 := conLte _ _ _ _ (refA a1) H4).
rewrite H7 in H6. rewrite H6 in L.
exact L.
* compute in H4.
discriminate H4.
+ apply symAP.
+ apply symAP.
+ apply trnAP.
- case_eq(eqSAP (bSAP ((a1, wP) :: nil) ((a2, wP) :: nil)) ((a2, wP) :: nil)); intro H1; auto.
apply brel_set_elim_prop in H1.
destruct H1 as [H1 H2].
assert (H3 : in_set eqAP (bSAP ((a1, wP) :: nil) ((a2, wP) :: nil)) (a1, wP) = true).
{
apply in_set_bop_union_intro.
+ apply symAP.
+ apply trnAP.
+ left. apply in_set_cons_intro.
* apply symAP.
* left. apply refAP.
}
assert (H4 := H1 _ H3).
apply in_set_cons_elim in H4.
+ destruct H4 as [H4 | H4].
* apply brel_product_elim in H4. destruct H4 as [H4 H5].
assert (H6 := refLte a1).
assert (H7 := conLte _ _ _ _ (refA a1) H4).
rewrite H6 in H7. rewrite H7 in L.
exact L.
* compute in H4.
discriminate H4.
+ apply symAP.
+ apply symAP.
+ apply trnAP.
Defined.
Local Notation "x =S= y" := (eqSAP x y = true) (at level 70,
only parsing).
Local Notation "[P1]" := (uop_manger_phase_1 eqA addP)
(only parsing). (* Phase 1 reduction *)
Local Notation "[P2]" := (@uop_manger_phase_2 A P lteA)
(only parsing). (* Phase 2 reduction *)
(* These admitted lemmas will come from Matrix.algorithm because
Tim is working on it, so for the moment I am admitting it. *)
(* Unfold matrix_algorithms.sum_fn
and then use to get rid of dependency from algorithms files *)
Lemma sum_fn_congruence_general_set :
forall (Xa Xb : finite_set (A * P)),
Xa =S= Xb ->
eqP (matrix_algorithms.sum_fn zeroP addP snd Xa)
(matrix_algorithms.sum_fn zeroP addP snd Xb) = true.
Proof.
Admitted.
(* End of admit that will come from library *)
(* Move these lemmas to respective files *)
Lemma sum_fn_distribute :
forall (X Y : finite_set (A * P)),
eqP
(matrix_algorithms.sum_fn zeroP addP snd (X ++ Y))
(addP
(matrix_algorithms.sum_fn zeroP addP snd X)
(matrix_algorithms.sum_fn zeroP addP snd Y)) = true.
Proof.
intros *.
eapply matrix_algorithms.sum_fn_distributes_over_concat;
auto.
unfold bop_is_id; split;
[eapply zeropLid | eapply zeropRid].
Qed.
Lemma sum_fn_commutative :
forall (X Y : finite_set (A * P)),
eqP
(matrix_algorithms.sum_fn zeroP addP snd (X ++ Y))
(matrix_algorithms.sum_fn zeroP addP snd (Y ++ X)) = true.
Proof.
intros *.
eapply trnP;
[eapply sum_fn_distribute |].
eapply symP, trnP;
[eapply sum_fn_distribute|].
eapply trnP;
[eapply addP_com| eapply refP].
Qed.
Lemma sum_fn_base_case :
forall (Y : finite_set (A * P)),
eqP
(fold_right addP zeroP (map snd (uop_duplicate_elim eqAP Y)))
(addP zeroP (fold_right addP zeroP (map snd Y))) = true.
Proof.
induction Y as [|(au, bu) Y IHy].
+
cbn; eapply symP.
rewrite zeropRid;
exact eq_refl.
+
cbn;
case_eq (in_set eqAP Y (au, bu));
intros Ha.
++
eapply symP, trnP with
(addP zeroP (fold_right addP zeroP (map snd Y))).
remember ((map snd Y)) as Ya.
eapply trnP with
(addP bu (fold_right addP zeroP Ya));
[eapply zeropLid | eapply symP].
eapply trnP with
(fold_right addP zeroP Ya);
[eapply zeropLid|].
eapply symP.
eapply fold_right_idempotent_aux_one;
try assumption.
intros *;
eapply symP, addP_assoc.
intros * Hu Hv;
eapply cong_addP; try assumption.
eapply map_in_set with (au := au) (bu := bu) in Ha;
try assumption.
destruct Ha as (Hal & Har).
subst; exact Har.
now rewrite refA.
now rewrite refP.
eapply symP, IHy.
++
cbn; eapply symP, trnP with
(addP bu (fold_right addP zeroP (map snd Y))).
eapply zeropLid.
eapply cong_addP;
[now rewrite refP|].
eapply symP, trnP.
eapply IHy.
eapply zeropLid.
Qed.
(* Easy. This proof certainly needs idempontence because of duplicate
elimination during bop_union *)
Lemma sum_fn_bop_union_dist :
forall (X Y : finite_set (A * P)),
eqP
(matrix_algorithms.sum_fn zeroP addP snd (bop_union eqAP X Y))
(addP
(matrix_algorithms.sum_fn zeroP addP snd X)
(matrix_algorithms.sum_fn zeroP addP snd Y)) = true.
Proof.
induction X as [|(ax, bx) X IHx].
+
intros *; cbn;
unfold matrix_algorithms.sum_fn.
eapply sum_fn_base_case.
+
intros *; cbn.
case_eq (in_set eqAP (X ++ Y) (ax, bx));
intro Ha.
++
(* idempotence *)
specialize (IHx Y).
unfold matrix_algorithms.sum_fn, bop_union,
bop_concat in * |- *.
eapply trnP;
[eapply IHx|].
eapply trnP.
eapply symP, fold_right_distributes;
try assumption.
eapply symP, trnP.
eapply addP_assoc.
eapply trnP with
(addP bx (fold_right addP zeroP (map snd X ++ map snd Y))),
symP.
eapply cong_addP;
[eapply refP|].
eapply symP, fold_right_distributes;
try assumption.
eapply symP, fold_right_idempotent_aux_one;
try assumption.
intros *;
eapply symP, addP_assoc.
intros * Hu Hv;
eapply cong_addP; try assumption.
eapply map_in_set with (au := ax) (bu := bx) in Ha;
try assumption.
destruct Ha as (Hal & Har).
rewrite map_app in Har; exact Har.
now rewrite refA.
now rewrite refP.
++
cbn;
unfold matrix_algorithms.sum_fn, bop_union,
bop_concat in * |- *.
eapply symP, trnP.
eapply addP_assoc.
eapply cong_addP;
[now rewrite refP| eapply symP, IHx].
Qed.
Lemma in_set_filter :
forall (X : finite_set (A * P)) ap ax bx,
eqA ax ap = true ->
in_set eqAP X (ax, bx) = true ->
in_set eqAP (filter (λ '(x, _), eqA x ap) X) (ax, bx) = true.
Proof.
induction X as [|(au, bu) X IHx].
+
cbn; intros;
try congruence.
+
cbn; intros * Ha Hb.
case_eq (and.bop_and (eqA ax au) (eqP bx bu));
intro Hc; rewrite Hc in Hb; cbn in Hb.
++
eapply Bool.andb_true_iff in Hc.
destruct Hc as [Hcl Hcr].
case_eq (eqA au ap); intro Hd.
cbn; rewrite Hcl, Hcr;
reflexivity.
rewrite (trnA _ _ _ (symA _ _ Hcl) Ha) in Hd;
congruence.
++
case_eq (eqA au ap); intro Hd.
cbn; rewrite (trnA _ _ _ Ha (symA _ _ Hd)) in Hc |- *;
cbn in Hc |- *; rewrite Hc; cbn.
eapply IHx; try assumption.
eapply IHx; try assumption.
Qed.
Lemma uop_dup_dup_elim :
forall (X : finite_set (A * P)) ax bx,
in_set eqAP X (ax, bx) = true ->
uop_duplicate_elim eqAP ((ax, bx) :: X) =
uop_duplicate_elim eqAP X.
Proof.
intros * Ha.
cbn; rewrite Ha;
reflexivity.
Qed.
Lemma uop_dup_filter_elim :
forall (X : finite_set (A * P)) ap,
uop_duplicate_elim eqAP (filter (λ '(x, _), eqA x ap) X) =
filter (λ '(x, _), eqA x ap) (uop_duplicate_elim eqAP X).
Proof.
induction X as [|(ax, bx) X IHx].
+
intros *; cbn;
exact eq_refl.
+
intros *; cbn.
case_eq (eqA ax ap);
case_eq (in_set eqAP X (ax, bx));
intros Ha Hb.
++
(* we know that (ax, bx) is a
member of filter *)
rewrite <-IHx.
pose proof in_set_filter X ap ax bx Hb
Ha as Hc.
rewrite uop_dup_dup_elim;
[exact eq_refl | exact Hc].
++
rewrite uop_duplicate_elim_lemma_2;
cbn. rewrite Hb, IHx;
reflexivity.
case_eq (in_set eqAP (filter (λ '(x, _), eqA x ap) X) (ax, bx));
intro Hc; try reflexivity.
eapply in_set_filter_elim in Hc;
[rewrite Ha in Hc; destruct Hc; congruence|].
intros (au, bu) (cu, du) He.
eapply brel_product_elim in He.
destruct He as (Hel & Her).
eapply conA; try assumption.
now rewrite refA.
++
eapply IHx.
++
cbn; rewrite Hb;
eapply IHx.
Qed.
Lemma bop_union_filter_push :
forall (X Y : finite_set (A * P)) ap,
bop_union eqAP
(filter (λ '(x, _), eqA x ap) X)
(filter (λ '(x, _), eqA x ap) Y) =
filter (λ '(x, _), eqA x ap) (bop_union eqAP X Y).
Proof.
induction X as [|(ax, bx) X IHx].
+
intros *; cbn.
eapply uop_dup_filter_elim.
+
intros *; cbn.
case_eq (eqA ax ap);
case_eq (in_set eqAP (X ++ Y) (ax, bx));
intros Ha Hb.
++
remember (filter (λ '(x, _), eqA x ap) X) as Xa.
remember (filter (λ '(x, _), eqA x ap) Y) as Ya.
rewrite <-app_comm_cons, uop_dup_dup_elim; subst;
[eapply IHx |].
repeat rewrite <-list_filter_lib_filter_same;
rewrite <-filter_app, list_filter_lib_filter_same.
eapply in_set_filter_intro;
[eapply symAP| | refine(pair Hb Ha)].
intros (au, bu) (cu, du) He.
eapply brel_product_elim in He.
destruct He as (Hel & Her).
eapply conA; try assumption.
now rewrite refA.
++
remember (filter (λ '(x, _), eqA x ap) X) as Xa.
remember (filter (λ '(x, _), eqA x ap) Y) as Ya.
rewrite <-app_comm_cons, uop_duplicate_elim_lemma_2;
cbn. rewrite Hb. f_equal.
subst; eapply IHx.
subst.
repeat rewrite <-list_filter_lib_filter_same.
rewrite <-filter_app, list_filter_lib_filter_same.
case_eq (in_set eqAP (filter (λ '(x, _), eqA x ap) (X ++ Y)) (ax, bx));
intro Hc; try reflexivity.
eapply in_set_filter_elim in Hc;
[rewrite Ha in Hc; destruct Hc; congruence|].
intros (au, bu) (cu, du) He.
eapply brel_product_elim in He.
destruct He as (Hel & Her).
eapply conA; try assumption.
now rewrite refA.
++
eapply IHx.
++
cbn; rewrite Hb.
eapply IHx.
Qed.
(* end of lemma movement *)
Lemma matrix_sum_filter_push_inside :
forall (X Y : finite_set (A * P)) au bu ap,
eqA au ap = true ->
eqP
(matrix_algorithms.sum_fn zeroP addP snd
((au, bu) :: filter (λ '(x, _), eqA x ap) (X ++ Y)))
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap) (X ++ ((au, bu) :: Y)))) = true.
Proof.
intros * Ha.
repeat rewrite <-list_filter_lib_filter_same;
repeat rewrite filter_app; cbn;
rewrite Ha; cbn.
remember (List.filter (λ '(x, _), eqA x ap) X) as Xa.
remember ((au, bu) :: List.filter (λ '(x, _), eqA x ap) Y)
as Ya.
eapply symP, trnP;
[eapply sum_fn_commutative |].
subst; cbn.
eapply cong_addP;
[eapply refP |].
eapply sum_fn_commutative.
Qed.
Lemma matrix_sum_fn_addition :
forall (X Y : finite_set (A * P)) ap,
no_dup eqA (map fst Y) = true ->
eqP
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap) (X ++ Y)))
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap)
(fold_left (manger_merge_sets_new eqA addP) X Y))) = true.
Proof.
induction X as [|(au, bu) X IHx].
+ intros * Ha. cbn.
eapply refP.
+
simpl; intros * Ha.
unfold manger_merge_sets_new at 2;
unfold manger_merge_sets_new_aux;
cbn.
remember (filter (λ '(s2, _), negb (eqA au s2)) Y) as Ya;
destruct (fold_left
(λ '(s1, t1) '(_, t2), (s1, addP t1 t2))
(filter (λ '(s2, _), eqA au s2) Y) (au, bu))
as (aw, av) eqn:Hb;
rewrite fold_left_simp in Hb;
inversion Hb; clear Hb;
rename H0 into Hb;
rename H1 into Hc;
rewrite Hc, <-Hb;
assert (Hd : no_dup eqA (map fst (Ya ++ [(au, av)])) = true).
rewrite map_app; cbn; subst.
eapply no_dup_filter; auto.
(* Y is duplicate free *)
rewrite <-Hb in Hc.
specialize (IHx (Ya ++ [(au, av)]) ap Hd).
eapply symP in IHx.
eapply symP.
eapply trnP;
[eapply IHx |].
(* This one is bit easy goal! Thanks Tim for
the idea! *)
case_eq (eqA au ap);
intro He.
++
eapply symP, trnP;
[eapply matrix_sum_filter_push_inside;
exact He|].
repeat rewrite <-list_filter_lib_filter_same,
filter_app;
repeat rewrite list_filter_lib_filter_same.
eapply trnP;
[eapply sum_fn_distribute |].
eapply symP, trnP;
[eapply sum_fn_distribute |].
eapply cong_addP;
[eapply refP|].
rewrite HeqYa.
(* requires some thinking! *)
repeat rewrite <-list_filter_lib_filter_same,
filter_app.
repeat rewrite <-list_filter_lib_filter_same.
(* why rewriting is such a mess! *)
rewrite <-filter_arg_swap_gen
with (ax := ap) at 1; auto.
rewrite filter_empty; auto.
cbn; rewrite He; cbn.
rewrite <-Hc.
(* start *)
eapply trnP with
(addP
(fold_right (λ t1 t2 : P, addP t1 t2) bu
(map snd (filter (λ '(s2, _), eqA au s2) Y))) zeroP).
remember ((map snd (filter (λ '(s2, _), eqA au s2) Y))) as Yt.
eapply trnP;
[eapply zeropRid| eapply symP].
eapply trnP;
[eapply zeropRid | eapply symP].
eapply fold_left_right_symmetric;
try auto.
(* end *)
eapply trnP with
((fold_right (λ t1 t2 : P, addP t1 t2) bu
(map snd (filter (λ '(s2, _), eqA au s2) Y)))).
eapply zeropRid.
eapply trnP;
[eapply fold_right_zero |]; auto.
eapply cong_addP;
[eapply refP|].
repeat rewrite <-list_filter_lib_filter_same.
rewrite <-filter_arg_swap_gen
with (ax := ap) at 1; auto.
repeat rewrite list_filter_lib_filter_same.
erewrite filter_equality with (a := au); auto.
++
repeat rewrite <-list_filter_lib_filter_same;
repeat rewrite filter_app;
repeat rewrite list_filter_lib_filter_same.
eapply trnP;
[eapply sum_fn_distribute |].
eapply symP, trnP;
[eapply sum_fn_distribute |].
eapply cong_addP;
[eapply refP|].
cbn; rewrite He, app_nil_r.
rewrite HeqYa.
(* requires some thinking! *)
repeat rewrite <-list_filter_lib_filter_same.
eapply symP.
rewrite <-filter_arg_swap_gen
with (ax := ap) at 1; auto.
rewrite filter_filter; auto.
rewrite <-filter_arg_swap_gen
with (ax := ap) at 1; auto.
case_eq (eqA ap au);
intro Hf; try reflexivity.
rewrite (symA _ _ Hf) in He;
congruence.
Qed.
Lemma sum_fn_cong_bop_union_X :
forall (X Y : finite_set (A * P)) ap,
eqP
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap)
(bop_union eqAP
(fold_left (manger_merge_sets_new eqA addP) X []) Y)))
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap) (bop_union eqAP X Y))) = true.
Proof.
intros *;
repeat rewrite <-bop_union_filter_push.
remember ((filter (λ '(x, _), eqA x ap) Y)) as Ya.
remember ((filter (λ '(x, _), eqA x ap)
(fold_left (manger_merge_sets_new eqA addP) X []))) as Xa.
remember ((filter (λ '(x, _), eqA x ap) X)) as Xb.
eapply trnP;
[eapply sum_fn_bop_union_dist |].
eapply symP, trnP;
[eapply sum_fn_bop_union_dist |].
eapply cong_addP;
[| now rewrite refP].
subst.
replace (X) with (X ++ []) at 1.
now erewrite matrix_sum_fn_addition.
now rewrite app_nil_r.
Qed.
Lemma sum_fn_cong_bop_union_Y :
forall (X Y : finite_set (A * P)) ap,
eqP
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap)
(bop_union eqAP X
(fold_left (manger_merge_sets_new eqA addP) Y []))))
(matrix_algorithms.sum_fn zeroP addP snd
(filter (λ '(x, _), eqA x ap) (bop_union eqAP X Y))) = true.
Proof.
intros *.
repeat rewrite <-bop_union_filter_push.
remember ((filter (λ '(x, _), eqA x ap) X)) as Xa.
remember (filter (λ '(x, _), eqA x ap)
(fold_left (manger_merge_sets_new eqA addP) Y [])) as Ya.
remember ((filter (λ '(x, _), eqA x ap) Y)) as Yb.
eapply trnP;
[eapply sum_fn_bop_union_dist |].
eapply symP, trnP;
[eapply sum_fn_bop_union_dist |].
eapply cong_addP;
[now rewrite refP|].
subst.
replace (Y) with (Y ++ []) at 1.
now erewrite matrix_sum_fn_addition.
now rewrite app_nil_r.
Qed.
Lemma bop_congruence_bProp_fst :
forall (a : A),
theory.bProp_congruence (A * P) (brel_product eqA eqP)
(λ '(x, _), eqA x a).
Proof.
intros a.
unfold theory.bProp_congruence.
intros (aa, ap) (ba, bp) He.
apply brel_product_elim in He.
destruct He as [Hel Her].
case_eq (eqA aa a); intro Hf.
eapply symA in Hel.
rewrite (trnA _ _ _ Hel Hf);
reflexivity.
case_eq (eqA ba a); intro Hg.
rewrite (trnA _ _ _ Hel Hg) in Hf;
congruence.
reflexivity.
Qed.
Lemma bop_congruence_bProp_snd :
forall (pa : A),theory.bProp_congruence _
(brel_product eqA eqP)
(λ '(s2, _), eqA pa s2).
Proof.
intros pa (aa, ap) (ba, bp) He.
apply brel_product_elim in He.
destruct He as [Hel Her].
case_eq (eqA pa aa); intro Hf.
rewrite (trnA pa aa ba Hf Hel);
reflexivity.
case_eq (eqA pa ba); intro Hg.
apply symA in Hel.
rewrite (trnA pa ba aa Hg Hel) in Hf;
congruence.
reflexivity.
Qed.
(* show [P1] is a reduction *)
Lemma P1_cong : uop_congruence _ eqSAP [P1].
Proof.
intros X Y Ha.
eapply brel_set_intro_prop;
[exact refAP| refine(pair _ _); intros (ap, bp) Hb].
+
unfold uop_manger_phase_1,
manger_phase_1_auxiliary in Hb |- *;
rewrite manger_merge_set_funex in Hb |- *.
eapply in_set_fold_left_mmsn_intro with
(zeroP := zeroP); try assumption.
++
eapply in_set_fold_left_mmsn_elim with
(zeroP := zeroP) in Hb; try assumption.
destruct Hb as [(q & Hbl) Hbr].
eapply brel_set_elim_prop in Ha;
[|exact symAP | eapply trnAP].
destruct Ha as [Hal Har].
exists q; eapply Hal;
rewrite app_nil_r in Hbl;
exact Hbl.
cbn; reflexivity.
++
eapply in_set_fold_left_mmsn_elim with
(zeroP := zeroP) in Hb; cbn; try assumption;
try reflexivity.
destruct Hb as [Hbl Hbr].
rewrite app_nil_r in Hbr |- *.
rewrite <-list_filter_lib_filter_same,
filter_arg_swap_gen with (a := ap),
list_filter_lib_filter_same; try assumption;
try (apply refA).
assert (Hc : (filter (λ '(x, _), eqA x ap) X) =S=
(filter (λ '(x, _), eqA x ap) Y)).
eapply filter_congruence_gen; try assumption;
try (apply bop_congruence_bProp_fst).
eapply symP, trnP.
eapply sum_fn_congruence_general_set,
brel_set_symmetric; exact Hc.
eapply symP; exact Hbr.
+
unfold uop_manger_phase_1,
manger_phase_1_auxiliary in Hb |- *;
rewrite manger_merge_set_funex in Hb |- *.
eapply in_set_fold_left_mmsn_intro with
(zeroP := zeroP); try assumption.
++
eapply in_set_fold_left_mmsn_elim with
(zeroP := zeroP) in Hb; try assumption.
destruct Hb as [(q & Hbl) Hbr].
eapply brel_set_elim_prop in Ha;
[|exact symAP | eapply trnAP].
destruct Ha as [Hal Har].
exists q; eapply Har;
rewrite app_nil_r in Hbl;
exact Hbl.
cbn; reflexivity.
++
eapply in_set_fold_left_mmsn_elim with
(zeroP := zeroP) in Hb; cbn; try assumption;
try reflexivity.
destruct Hb as [Hbl Hbr].
rewrite app_nil_r in Hbr |- *.
rewrite <-list_filter_lib_filter_same,
filter_arg_swap_gen with (a := ap),
list_filter_lib_filter_same; try assumption;
try (apply refA).
assert (Hc : (filter (λ '(x, _), eqA x ap) X) =S=
(filter (λ '(x, _), eqA x ap) Y)).
eapply filter_congruence_gen; try assumption;
try (apply bop_congruence_bProp_fst).
eapply symP, trnP.
eapply sum_fn_congruence_general_set;
exact Hc.
eapply symP; exact Hbr.
Qed.
Lemma P1_idem : uop_idempotent _ eqSAP [P1].
Proof.
eapply uop_manger_phase_1_uop_idempotent;
try assumption.
unfold bop_idempotent;
intros.
eapply addP_gen_idempotent;
now rewrite refP.
Qed.
Lemma P1_left : bop_left_uop_invariant _
eqSAP (bop_reduce [P1] bSAP) [P1].
Proof.
intros X Y;
eapply brel_set_intro_prop;
[exact refAP| refine(pair _ _); intros (ap, bp) Ha].
+
unfold bop_reduce in Ha |- *.
eapply in_set_uop_manger_phase_1_elim in Ha;
auto.
destruct Ha as ((q & Hal) & Har).
unfold bSAP in Hal.
eapply in_set_bop_union_elim in Hal;
[|eapply symAP].
eapply in_set_uop_manger_phase_1_intro;
auto.
++
destruct Hal as [Hall | Halr].
+++
eapply in_set_uop_manger_phase_1_elim in Hall;
auto.
destruct Hall as ((q' & Halll) & Hallr).
exists q'.
eapply in_set_bop_union_intro;
[eapply symAP | eapply trnAP | ].
left; exact Halll.
+++
exists q.
eapply in_set_bop_union_intro;
[eapply symAP | eapply trnAP | ].
right; exact Halr.
++
unfold uop_manger_phase_1,
manger_phase_1_auxiliary in Har |- *;
rewrite manger_merge_set_funex in Har.
eapply trnP;[exact Har |
rewrite list_filter_lib_filter_same;
eapply sum_fn_cong_bop_union_X].
+
unfold bop_reduce in Ha |- *.
eapply in_set_uop_manger_phase_1_elim in Ha;
auto.
destruct Ha as ((q & Hal) & Har).
unfold bSAP in Hal.
eapply in_set_bop_union_elim in Hal;
[|eapply symAP].
eapply in_set_uop_manger_phase_1_intro;
auto.
++
destruct Hal as [Hall | Halr].
+++
eexists.
eapply in_set_bop_union_intro;
[apply symAP | eapply trnAP|].
left;
eapply in_set_uop_manger_phase_1_intro; auto;
exists q; exact Hall.
+++
exists q.
eapply in_set_bop_union_intro;
[eapply symAP | eapply trnAP | ].
right; exact Halr.
++
unfold uop_manger_phase_1,
manger_phase_1_auxiliary in *;
rewrite manger_merge_set_funex in *.
eapply trnP;[exact Har |
rewrite list_filter_lib_filter_same;
eapply symP, sum_fn_cong_bop_union_X].
Qed.
Lemma P1_right : bop_right_uop_invariant _ eqSAP (bop_reduce [P1] bSAP) [P1].
Proof.
intros X Y.
eapply brel_set_intro_prop;
[exact refAP| refine(pair _ _); intros (ap, bp) Ha].
+
unfold bop_reduce in Ha |- *.
eapply in_set_uop_manger_phase_1_elim in Ha;
auto.
destruct Ha as ((q & Hal) & Har).
unfold bSAP in Hal.
eapply in_set_bop_union_elim in Hal;
[|eapply symAP].
eapply in_set_uop_manger_phase_1_intro;
auto.
++
destruct Hal as [Hall | Halr].
+++
exists q.
eapply in_set_bop_union_intro;
[apply symAP | eapply trnAP|].
left; exact Hall.
+++
eapply in_set_uop_manger_phase_1_elim
in Halr; auto;
destruct Halr as ((q' & Halrl) & Halrr).