-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils1.v
3363 lines (3068 loc) · 90.5 KB
/
utils1.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
Set Implicit Arguments.
Unset Strict Implicit.
Require Export nnum_base.
(*
Additional results for various stuffs
*)
Module Utils1.
Section VarSec1.
Variables (pl ml : E2) (g : E) (e u : E).
Hypothesis (Hring : is_ring pl ml g e u).
Notation opp := (Lci.inverse_of pl g e).
Notation inv := (Lci.inverse_of ml g u).
Lemma ring_distrib_l : distrib_l pl ml g.
Proof.
am.
Qed.
Lemma ring_distrib_r : distrib_r pl ml g.
Proof.
am.
Qed.
Lemma ring_inverses_switch : forall x y,
Lci.are_inverse pl g e x y -> forall z, Lci.are_inverse ml g u x z ->
forall k, Lci.are_inverse ml g u y k = Lci.are_inverse pl g e z k.
Proof.
ir. ap iff_eq;ir;uh H;uh H0;uh H1;ee.
uhg;ee;try am.
assert (is_neutre ml g u). ap Hring.
uh H11;ee.
wr (H12 z H3). wr H5.
assert (associative ml g). ap Hring.
rw H14;au.
transitivity (pl (ml (ml z y) k) (ml u k)).
ap uneq. symmetry;au.
wr ring_distrib_r;au.
wr H8.
wr ring_distrib_l;au. rw H10.
rw (ring_0_multr Hring);au.
ap (ring_0_multl Hring). am.
ap Hring;am.
assert (is_neutre ml g u). ap Hring.
uh H11;ee.
wr (H12 z H3). wr H5.
assert (associative ml g). ap Hring.
rw H14;au.
transitivity (pl (ml u k) (ml (ml z y) k)).
ap uneq2;au. symmetry;au.
wr ring_distrib_r;au.
wr H8.
wr ring_distrib_l;au. rw H9.
rw (ring_0_multr Hring);au.
ap (ring_0_multl Hring). am.
ap Hring;am.
uhg;ee;try am.
assert (y = opp x).
ap inverse_of_eq. ap group_is_monoid;am.
uhg;ee;am.
assert (k = opp z).
ap inverse_of_eq. ap group_is_monoid;am.
uhg;ee;am.
rw H11;rw H12.
wr (inverse_ml_insert_right Hring).
wr (inverse_ml_insert_left Hring).
wr inverse_inverse.
am. am. ap Hring;am.
am. am. wr H11. am. am.
assert (y = opp x).
ap inverse_of_eq. ap group_is_monoid;am.
uhg;ee;am.
assert (k = opp z).
ap inverse_of_eq. ap group_is_monoid;am.
uhg;ee;am.
rw H11;rw H12.
wr (inverse_ml_insert_right Hring).
wr (inverse_ml_insert_left Hring).
wr inverse_inverse.
am. am. ap Hring;am.
am. am. wr H12. am. am.
Qed.
Lemma ring_opp_inv : forall x, inversible ml g u x -> opp (inv x) = inv (opp x).
Proof.
ir.
apply inversible_inverse_of in H.
Focus 2. am.
assert (inc x g). am.
assert (is_group pl g e). am.
cp (group_inverse_of H1 H0).
cp (ring_inverses_switch H2 H).
assert (inc (inv x) g). am.
apply (group_inverse_of H1) in H4.
cp H4. wri H3 H5. clear H3.
ap inverse_of_eq. am. am.
Qed.
End VarSec1.
Section VarSec2.
Variables (pl ml : E2) (g e u : E).
Lemma ring_prove_commut : is_monoid pl g e -> is_regular pl g ->
is_monoid ml g u -> distributes pl ml g ->
commutative pl g.
Proof.
uhg;ir.
assert (pl (pl x x) (pl y y) = pl (pl x (pl y x)) y).
transitivity (ml (pl x y) (pl u u));uh H2;ee.
symmetry. rw H5;au.
rw H2;au. rw H2;au.
ap uneq2;ap uneq2;ap H1;am.
ap H;am.
rw H2;au. rw H5;au.
assert (is_neutre ml g u). am.
uh H6;ee. rw H7;au. rw H7;au.
assert (associative pl g). am. assert (is_lci pl g). am.
uh H10.
wr H9;au. wr H9;au. wr H9;au.
ap H;am.
uh H0;ee.
apply H0 with x;au. ap H;am. ap H;am.
apply H6 with y;au. ap H. am. ap H;am.
ap H. am. ap H;am.
wr H5.
uh H;ee. uh H.
rw H8;au. rw H8;au.
Qed.
End VarSec2.
Lemma axioms_emptyset_eq : forall f, axioms f -> domain f = emptyset ->
f = emptyset.
Proof.
ir. uh H;ee.
ap empty_emptyset.
ir. cp H2;apply H in H3.
nin H3.
assert (inc a (domain f)). ap domain_inc. exists b;am.
rwi H0 H3. apply emptyset_empty in H3;am.
Qed.
(*
predecessor
*)
Definition pred x := union x.
Lemma pred_0 : pred n0 = n0.
Proof.
uf n0;ap empty_emptyset.
ir. ufi pred H. apply union_ex in H. nin H;ee.
eapply emptyset_empty;am.
Qed.
Lemma pred_S : forall n, inc n nNum -> pred (oS n) = n.
Proof.
ir. ap ordinal_union_S. ap nNum_ordinal;am.
Qed.
Lemma emptyset_range : range emptyset = emptyset.
Proof.
ap empty_emptyset. ir.
apply Im_ex in H. nin H;ee;apply Z_sub in H. emptyset_auto.
Qed.
Lemma nNum_S_S_inc : forall n, inc n nNum -> forall m, inc m nNum ->
inc (oS n) (oS m) -> inc n m.
Proof.
ir.
wr nLt_inc_rw;nSolveS. rw nLt_S_S;nSolveS.
rw nLt_inc_rw;nSolveS.
Qed.
Lemma nNum_inc_S_S : forall n m, inc m nNum ->
inc n m -> inc (oS n) (oS m).
Proof.
ir.
cp (omega_transitiveT H H0).
wr nLt_inc_rw;nSolveS. wr nLt_S_S;nSolveS.
rw nLt_inc_rw;nSolveS.
Qed.
Lemma tack_on_inc_eq : forall x y, inc y x -> tack_on x y = x.
Proof.
ir. ap extensionality;uhg;ir.
rwi tack_on_inc H0. nin H0;subst;am.
rw tack_on_inc;au.
Qed.
Lemma trans_bijective_fast : forall a b f,
(forall x, inc x a -> forall y, inc y a -> f x = f y -> (inc (f x) b & x=y)) ->
(forall y, inc y b -> exists x, inc x a & f x = y) ->
Application.bijective a b f.
Proof.
ir;uhg;ee;uhg;ir.
cp (H x H1 x H1).
ap H2. tv.
ap H. am. am. am.
ap H0. am.
Qed.
Lemma nLeq_nPlus_l : forall n, inc n nNum -> forall m, inc m nNum ->
nLeq n (nPlus n m).
Proof.
ir. assert (nLeq (nPlus n n0) (nPlus n m)).
ap nLeq_nPlus_compat;nSolveS.
rwi nPlus_0 H1. am. am.
Qed.
Lemma n0_inc_S : forall n, inc n nNum -> inc n0 (oS n).
Proof.
ir. wr nLt_inc_rw;nSolveS;rw nLt_S_rw;nSolveS;wr nLeq_S_S;nSolveS.
Qed.
Lemma nonempty_card : forall x, nonempty x = sub n1 (card x).
Proof.
ir.
assert (is_ordinal (card x)). ap Sow.sow_ordinal.
assert (is_ordinal n1). ap nNum_ordinal;nSolveS.
destruct ordinal_inc_eq_inc with (card x) n1.
am. am.
ufi n1 H1. rwi oS_inc H1.
nin H1. apply emptyset_empty in H1;nin H1.
ap iff_eq;ir.
nin H2.
cp (card_equipotent x).
nin H3. apply (bijective_of_map H3) in H2.
rwi H1 H2;apply emptyset_empty in H2;nin H2.
assert (inc emptyset (card x)).
ap H2. uf n1;rw oS_inc;au.
rwi H1 H3. apply emptyset_empty in H3;nin H3.
nin H1.
ap iff_eq;ir.
rw H1;ap sub_refl.
cp (card_equipotent x).
rwi H1 H3. nin H3.
assert (inc n0 n1).
uf n0;uf n1;rw oS_inc;au.
apply H3 in H4. nin H4;ee.
exists x1;am.
ap iff_eq;ir.
apply ordinal_inc_sub in H1. am.
am.
assert (inc n0 n1).
uf n0;uf n1;rw oS_inc;au.
cp (card_equipotent x).
nin H4.
apply H2 in H3. apply H4 in H3.
nin H3;ee;exists x1;am.
Qed.
Lemma union_Im_cond_strict : forall f f' a,
(forall x, inc x a -> forall x', inc x' a -> forall z, inc z (f x) -> inc z (f x') -> x=x') ->
forall g, (forall x, inc x a -> bijective (f x) (f' x) (g x)) ->
forall h h', inc h (Im g a) -> inc h' (Im g a) ->
nonempty (inter2 (domain h) (domain h')) -> h = h'.
Proof.
ir.
Im_nin H1;Im_nin H2;subst.
cp (H0 x H1);cp (H0 x0 H2).
assert (domain (g x) = f x). uh H4;ee;ap H4.
assert (domain (g x0) = f x0). uh H5;ee;ap H5.
rwi H6 H3;rwi H7 H3.
nin H3.
apply inter2_and in H3;ee.
ap uneq.
eapply H. am. am. am. am.
Qed.
Lemma union_Im_bijective : forall f f' a,
(forall x, inc x a -> forall x', inc x' a -> forall z, inc z (f x) -> inc z (f x') -> x=x') ->
(forall x, inc x a -> forall x', inc x' a -> forall z, inc z (f' x) -> inc z (f' x') -> x=x') ->
forall g, (forall x, inc x a -> bijective (f x) (f' x) (g x)) ->
bijective (union (Im f a)) (union (Im f' a)) (union (Im g a)).
Proof.
ir.
pose (e := (Im g a)).
assert (forall h, inc h e -> exists x, inc x a & bijective (f x) (f' x) h).
ir. Im_nin H2. subst.
exists x;ee;au.
assert (forall h, inc h e -> axioms h).
ir;apply H2 in H3. nin H3;ee;am.
assert (forall x, inc x a -> domain (g x) = (f x)).
ap H1.
assert (union_strict_cond e). rw union_strict_cond_rw.
ir.
Im_nin H5;Im_nin H6;subst.
rwi H4 H7;try am; rwi H4 H7;try am.
nin H7. apply inter2_and in H7;ee.
ap uneq. apply H with x1;am.
cp (union_axioms H3 H5).
cp (union_domain H3 H5).
cp (union_range H3 H5).
cp (union_ev H3 H5).
clear H2 H3 H5.
assert (forall x, inc x a -> forall k, inc k (f x) -> ev (union e) k = ev (g x) k).
ir.
assert (inc k (domain (g x))). rw H4;au.
apply H9 in H5. am.
ap Im_inc. am. clear H9.
uhg;ee.
uhg;ir;ee.
am.
ufi e H7;rw H7.
ap extensionality_rw.
intros k.
ap iff_eq;ir.
union_nin H3. Im_nin H3;subst.
Im_nin H3. subst. rwi H4 H5;try am.
ap union_inc. econstructor;ee. ap Im_inc. am.
am.
union_nin H3. Im_nin H3;subst.
ap union_inc. econstructor;ee.
ap Im_inc. ap Im_inc. am.
rw H4. am. am.
rw range_Im_rw;try am.
uhg;ir.
Im_nin H3.
subst. ufi e H7;rwi H7 H3.
union_nin H3. Im_nin H3;subst. Im_nin H3;subst.
rwi H4 H5;try am.
ufi e H2.
rw (H2 x0 H3 x H5). ap union_inc. econstructor.
ee. ap Im_inc. am. eapply trans_of_map. ap H1. am.
am.
uhg;ir.
union_nin H3. Im_nin H3;subst.
union_nin H5;Im_nin H5;subst.
ufi e H2;rwi (H2 x1 H3 x H10) H9;rwi (H2 x2 H5 y H11) H9.
cp (H1 x1 H3);cp (H1 x2 H5).
cp H10. apply (bijective_of_map H12) in H14.
cp H11. apply (bijective_of_map H13) in H15.
rwi H9 H14.
assert (x1 = x2).
apply H0 with (ev (g x2) y);am.
subst.
apply H13;am.
uhg;ir.
union_nin H3. Im_nin H3. subst.
cp (H1 x0 H3).
cp H5. apply H9 in H10. nin H10;ee;subst.
econstructor;ee. ap union_inc;econstructor. ee.
ap Im_inc. am. am.
ufi e H2. rw (H2 x0 H3 x H10). tv.
Qed.
(*
functions on nats as lists
*)
Module EList.
Definition is_elist l := A (Function.axioms l) (inc (domain l) nNum).
Definition enil := emptyset.
Definition list_length l := domain l.
Lemma list_length_inc : forall l, is_elist l -> inc (list_length l) nNum.
Proof.
ir;am.
Qed.
Lemma is_elist_nil : is_elist enil.
Proof.
uhg;ee.
ap emptyset_axioms.
uf enil;rw emptyset_domain. nSolveS.
Qed.
Lemma nil_length : list_length enil = n0.
Proof.
ap emptyset_domain.
Qed.
Definition econs x l := L (oS (list_length l)) (fun n => by_cases
(fun _ : n=n0 => x)
(fun _ : _ => ev l (pred n))).
Lemma econs_length : forall x l, list_length (econs x l) = oS (list_length l).
Proof.
ir. ap create_domain.
Qed.
Lemma econs_0 : forall l, is_elist l -> forall x, ev (econs x l) n0 = x.
Proof.
ir.
uf econs. rw create_ev.
rw by_cases_if. tv. tv.
uh H. uf list_length.
ap ordinal_inc_S_all. ap nNum_ordinal;am.
Qed.
Lemma econs_S : forall l, is_elist l -> forall n, inc n (list_length l) -> forall x,
ev (econs x l) (oS n) = ev l n.
Proof.
ir.
assert (inc n nNum).
cp omega_ordinal. uh H1. ee.
eapply H1. Focus 2. am.
am.
uf econs;rw create_ev.
rw by_cases_if_not. rw pred_S. tv.
am. ap oS_emptyset_neq.
ap ordinal_S_inc_inc. ap nNum_ordinal;am.
ap nNum_ordinal;am.
am.
Qed.
Lemma econs_list : forall l, is_elist l -> forall x, is_elist (econs x l).
Proof.
ir. uhg;ee. ap create_axioms.
uf econs. rw create_domain. nSolveS.
Qed.
Lemma econs_inj : forall l, is_elist l -> forall l', is_elist l' ->
forall x y, econs x l = econs y l' -> A (x=y) (l=l').
Proof.
ir. ee.
cp (uneq (fun f => ev f n0) H1). simpl in H2.
rwi econs_0 H2. rwi econs_0 H2. am.
am. am.
assert (list_length l = list_length l').
cp (uneq list_length H1).
rwi econs_length H2. rwi econs_length H2.
ap oS_N_inj. am. am.
ap function_extensionality. am. am. am.
ir.
cp (uneq (fun l => ev l (oS x0)) H1). simpl in H4.
rwi econs_S H4. rwi econs_S H4. am.
am. wr H2;am. am. am.
Qed.
Definition tail l := L (pred (list_length l)) (fun n => ev l (oS n)).
Lemma tail_list : forall l, is_elist l -> is_elist (tail l).
Proof.
ir. uhg;ee. ap create_axioms.
uf tail. rw create_domain.
uh H;ee. uf list_length. apply nNum_destruct in H0.
nin H0.
rw H0. fold n0. rw pred_0. nSolveS.
nin H0;ee. rw H1. rw pred_S;au.
Qed.
Lemma tail_nil : tail enil = enil.
Proof.
uf tail. rw nil_length. rw pred_0.
ap function_extensionality. ap create_axioms. ap emptyset_axioms.
rw create_domain. symmetry.
ap nil_length.
ir. rwi create_domain H. apply emptyset_empty in H. nin H.
Qed.
Lemma tail_length : forall l, list_length (tail l) = pred (list_length l).
Proof.
ir;ap create_domain.
Qed.
Lemma elist_destruct : forall l, is_elist l -> (l=enil \/ exists x,exists tl, is_elist tl & l = econs x tl).
Proof.
ir.
uh H;ee.
apply nNum_destruct in H0. nin H0.
left. ap function_extensionality.
am. ap emptyset_axioms.
rw H0;symmetry;ap emptyset_domain.
ir. rwi H0 H1;apply emptyset_empty in H1;nin H1.
nin H0;ee;subst.
right.
exists (ev l n0).
exists (tail l).
ee. ap tail_list. uhg. ee. am. rw H1;nSolveS.
ap function_extensionality.
am.
ap econs_list. ap tail_list. uhg;ee.
am. rw H1;nSolveS.
uf econs;rw create_domain. uf list_length;uf tail.
rw create_domain. uf list_length. rw H1.
ap uneq. symmetry. ap pred_S;am.
ir.
assert (inc x0 nNum).
cp omega_ordinal. uh H3;ee. eapply H3.
Focus 2. ap H2. rw H1. nSolveS.
apply nNum_destruct in H3. nin H3.
subst. rw econs_0. reflexivity.
ap tail_list. uhg;ee. am. rw H1;nSolveS.
nin H3;ee;subst.
rw econs_S. uf tail. rw create_ev.
tv.
uf list_length. rw H1. rw pred_S. rwi H1 H2.
wr nLt_inc_rw;au. wri nLt_inc_rw H2;nSolveS.
rw nLt_S_S;nSolveS.
am. ap tail_list;uhg;ee. am. rw H1;nSolveS.
rw tail_length. uf list_length. rw H1. rw pred_S;nSolveS.
rwi H1 H2.
wr nLt_inc_rw;au. wri nLt_inc_rw H2;nSolveS.
rw nLt_S_S;nSolveS.
Qed.
Lemma tail_cons : forall l, is_elist l -> forall x, tail (econs x l) = l.
Proof.
ir.
ap function_extensionality.
ap create_axioms. am.
uf tail;rw create_domain. rw econs_length.
rw pred_S;try am. reflexivity.
ir.
ufi tail H0;rwi create_domain H0.
uf tail;rw create_ev;try am.
rwi econs_length H0. rwi pred_S H0;try am.
rw econs_S. tv.
am. am.
Qed.
Lemma length_0_nil : forall l, is_elist l -> list_length l = n0 -> l = enil.
Proof.
ir. apply elist_destruct in H. nin H. am.
nin H;nin H;ee;subst.
rwi econs_length H0. apply oS_emptyset_neq in H0. nin H0.
Qed.
Inductive elist_lt : E2P :=
| elist_lt_cons : forall x l, is_elist l -> elist_lt l (econs x l)
.
Lemma elist_lt_wf : well_founded elist_lt.
Proof.
uf well_founded.
pose (p n := forall l, is_elist l -> list_length l = n -> Acc elist_lt l).
assert (forall n, inc n nNum -> p n). ap nNum_rect;uf p;clear p.
ir. assert (l = enil).
ap length_0_nil. am. am.
subst. constructor.
ir. inversion H1;subst.
wri H2 H0. rwi econs_length H0.
apply oS_emptyset_neq in H0. nin H0.
ir.
constructor. ir.
inversion H3;subst.
rwi econs_length H2. apply oS_N_inj in H2;try am.
ap H0. am. am.
ir. apply by_cases with (is_elist a);ir.
eapply H. am. am. reflexivity.
constructor. ir. inversion H1;subst.
nin H0. ap econs_list. am.
Qed.
Lemma list_not_nil : forall l, is_elist l -> l<>enil ->
l = econs (ev l n0) (tail l).
Proof.
ir.
apply elist_destruct in H;nin H. nin H0;am.
nin H;nin H;ee.
etransitivity. am. ap uneq2.
cp (uneq (fun l' => ev l' n0) H1).
simpl in H2. rwi econs_0 H2. au. am.
symmetry in H1. cp (uneq tail H1).
rwi tail_cons H2. am. am.
Qed.
Lemma elist_lt_inv : forall l, is_elist l -> forall x l', elist_lt l' (econs x l) -> l' = l.
Proof.
ir. inversion H0;subst.
apply econs_inj in H1. am. am. am.
Qed.
Definition elist_rec : forall P : Type,
P ->
(forall l : E, P -> forall x : E, P) ->
(forall l : E, P) ->
forall l : E, P.
intros P Hnil IH Hreb.
eapply Fix. ap elist_lt_wf.
ir.
apply by_cases with (x=enil);ir.
ap Hnil.
apply by_cases with (is_elist x);ir.
eapply IH. exact (tail x).
apply X with (tail x).
cp (tail_list H0). apply list_not_nil in H.
assert (elist_lt (tail x) (econs (ev x n0) (tail x))). constructor. am.
wri H H2. am.
am.
exact (ev x n0).
eapply Hreb. exact x.
Defined.
Lemma elist_rec_nil : forall P (Hnil : P) IH Hreb, elist_rec Hnil IH Hreb enil = Hnil.
Proof.
ir. uf elist_rec. rw Fix_eq. rw by_cases_if. tv. tv.
ir. ap uneq.
ap arrow_extensionality. ir.
ap uneq2. ap arrow_extensionality;ir.
rw H. tv.
tv.
Qed.
Lemma elist_rec_not : forall P (Hnil : P) IH Hreb, forall l (Hnot : ~ is_elist l),
elist_rec Hnil IH Hreb l = Hreb l.
Proof.
ir. uf elist_rec. rw Fix_eq. rw by_cases_if_not.
erewrite by_cases_if_not. reflexivity.
uhg;ir;ap Hnot. subst. am.
uhg;ir. ap Hnot;subst;ap is_elist_nil.
ir. ap uneq.
ap arrow_extensionality. ir.
ap uneq2. ap arrow_extensionality;ir.
rw H. tv.
tv.
Qed.
Lemma elist_rec_cons : forall P (Hnil : P) IH Hreb, forall l (Hl : is_elist l) x,
elist_rec Hnil IH Hreb (econs x l) = IH l (elist_rec Hnil IH Hreb l) x.
Proof.
ir. uf elist_rec. rw Fix_eq.
rw by_cases_if_not. erewrite by_cases_if.
rw tail_cons.
ap uneq. ap econs_0. am. am.
ap econs_list;am.
uhg;ir.
cp (uneq list_length H).
rwi econs_length H0. rwi nil_length H0.
eapply oS_emptyset_neq. am.
ir. ap uneq.
ap arrow_extensionality. ir.
ap uneq2. ap arrow_extensionality;ir.
rw H. tv.
tv.
Qed.
Lemma elist_ind : forall P : EP,
P enil ->
(forall l, is_elist l -> P l -> forall x, P (econs x l)) ->
forall l, is_elist l -> P l.
Proof.
intros P Hnil IH.
intro l. cp (elist_lt_wf l). nin H.
ir. apply by_cases with (x=enil);ir.
subst. am.
apply list_not_nil in H2. rw H2. ap IH. ap tail_list;am.
ap H0. assert (elist_lt (tail x) (econs (ev x n0) (tail x))). constructor.
ap tail_list;am. wri H2 H3. am.
ap tail_list;am.
am.
Qed.
Definition fold_right (f : E2) (l a : E) : E.
ap elist_rec. exact a.
intros tl r x.
exact (f x r).
ir. exact a.
exact l.
Defined.
Lemma fold_right_not : forall f l a, ~ is_elist l -> fold_right f l a = a.
Proof.
ir. uf fold_right. rw elist_rec_not. tv. am.
Qed.
Lemma fold_right_nil : forall f a, fold_right f enil a = a.
Proof.
ir; ap elist_rec_nil.
Qed.
Lemma fold_right_cons : forall x l, is_elist l -> forall f a,
fold_right f (econs x l) a = f x (fold_right f l a).
Proof.
ir;uf fold_right.
rw elist_rec_cons. tv. am.
Qed.
Definition list_of l a := exists n, inc n nNum & is_map n a l.
Lemma list_of_list : forall l a, list_of l a -> is_elist l.
Proof.
ir. nin H;ee. uhg. ee. am. uh H0;ee. rw H1. am.
Qed.
Lemma list_of_inc : forall l a, list_of l a -> forall n, inc n (list_length l) ->
inc (ev l n) a.
Proof.
ir. nin H. ee. replace (list_length l) with x in *.
eapply trans_of_map. am. am.
symmetry;am.
Qed.
Lemma nil_list_of : forall a, list_of enil a.
Proof.
ir;exists n0;ee. nSolveS.
uhg;ee. ap emptyset_axioms. ap emptyset_domain.
uhg;ir.
ufi enil H. rwi emptyset_range H. emptyset_auto.
Qed.
Lemma cons_list_of : forall a x, inc x a -> forall l, list_of l a ->
list_of (econs x l) a.
Proof.
ir. nin H0. ee.
exists (oS x0). ee.
nSolveS.
assert (x0 = (list_length l)).
symmetry;am.
assert (forall x1, inc x1 (oS x0) -> inc x1 nNum).
ap omega_ordinal. nSolveS.
rw H2. ap map_of_trans.
uhg;ir. apply by_cases with (x1=n0);ir.
rw by_cases_if. am. am.
rw by_cases_if_not.
rwi H2 H3.
cp (H3 x1 H4).
apply nNum_destruct in H6. nin H6. nin H5;am.
nin H6;ee;subst.
rw pred_S. eapply trans_of_map. am.
wr nLt_inc_rw;try am. rw nLt_S_S;try am.
rw nLt_inc_rw;nSolveS. am.
am.
Qed.
Lemma list_of_tail : forall a l, list_of l a -> list_of (tail l) a.
Proof.
ir.
assert (is_elist l). eapply list_of_list. am.
apply by_cases with (l = enil);ir. subst.
rw tail_nil. ap nil_list_of.
nin H;ee. apply nNum_destruct in H. nin H.
nin H1. apply length_0_nil. am.
uf n0;wr H. am.
nin H;ee;subst.
exists x0. ee. am.
uf tail. assert (list_length l = oS x0).
am.
rw H3. rw pred_S.
ap map_of_trans. uhg;ir.
eapply trans_of_map. am.
assert (inc x nNum). cp omega_ordinal. uh H5;ee;eapply H5.
ap H. am.
wr nLt_inc_rw;nSolveS. wr nLt_S_S;nSolveS. rw nLt_inc_rw;nSolveS.
am.
Qed.
Lemma list_list_of : forall l, is_elist l -> forall a, Application.axioms (list_length l) a (ev l) ->
list_of l a.
Proof.
ir.
exists (list_length l). ee.
am.
uhg;ee. am.
tv.
rw range_Im_rw. uhg;ir.
Im_nin H1. subst. ap H0. am.
am.
Qed.
Lemma list_of_cons : forall a x l, is_elist l -> list_of (econs x l) a ->
A (inc x a) (list_of l a).
Proof.
ir. nin H0. nin H0.
apply nNum_destruct in H0. nin H0.
ap False_rect. subst.
assert (list_length (econs x l) = n0). am.
rwi econs_length H0. eapply oS_emptyset_neq;am.
nin H0;nin H0;subst.
assert (forall x0, inc x0 x1 -> inc x0 nNum). ap omega_ordinal. am.
ee. replace x with (ev (econs x l) n0). eapply trans_of_map.
am. wr nLt_inc_rw;nSolveS. rw nLt_S_rw;nSolveS.
wr nLeq_S_S;nSolveS.
ap econs_0. am.
ap list_list_of. am.
uhg;ir.
replace (ev l x0) with (ev (econs x l) (oS x0)).
eapply trans_of_map. am.
assert (inc x0 nNum). cp (omega_ordinal).
uh H4;ee;eapply H4. ap H. am.
wr nLt_inc_rw;nSolveS. wr nLt_S_S;nSolveS.
rw nLt_inc_rw;nSolveS.
replace x1 with (list_length l). am.
eapply oS_N_inj. ap H.
wr (econs_length x). am.
rw econs_S. tv. am. am.
Qed.
Lemma fold_right_inc : forall a b f, (forall x, inc x a -> forall y, inc y b -> inc (f x y) b) ->
forall l, list_of l a -> forall r, inc r b -> inc (fold_right f l r) b.
Proof.
intros a b f Hf.
pose (p l := list_of l a -> forall r : E, (r ∈ b) -> (fold_right f l r ∈ b)).
assert (forall l, is_elist l -> p l).
ap elist_ind;uf p;clear p;ir.
rw fold_right_nil. am.
rw fold_right_cons. ap Hf. apply list_of_cons in H1. am.
am.
ap H0. apply list_of_cons in H1. am.
am. am. am.
ir. ap H. eapply list_of_list. am. am. am.
Qed.
Lemma trans_of_list : forall l a, list_of l a -> Application.axioms (list_length l) a (ev l).
Proof.
ir. nin H;ee. replace (list_length l) with x. ap trans_of_map. am.
symmetry. am.
Qed.
(*
fold_left f a nil = a
fold_left f a (x::tl) = fold_left f (f a x) tl
*)
Definition fold_left : E2 -> E2.
intro f.
assert (E -> E1).
(* l => a => fold_left f a l*)
ap (elist_rec (P := E1)).
exact (fun a => a).
intros tl rtl x a. (* rtl = fun z => fold_left f z tl *)
exact (rtl (f a x)).
intros notL a. exact a.
intros a l. exact (X l a).
Defined.
Lemma fold_left_not : forall l, ~ is_elist l -> forall f a, fold_left f a l = a.
Proof.
ir. uf fold_left. rw elist_rec_not. tv. am.
Qed.
Lemma fold_left_nil : forall f a, fold_left f a enil = a.
Proof.
ir. uf fold_left;rw elist_rec_nil. tv.
Qed.
Lemma fold_left_cons : forall l, is_elist l -> forall x f a,
fold_left f a (econs x l) = fold_left f (f a x) l.
Proof.
ir. uf fold_left.
rw elist_rec_cons. tv.
am.
Qed.
Definition rev l := fold_left (fun r x => econs x r) enil l.
Lemma rev_nil : rev enil = enil.
Proof.
uf rev. rw fold_left_nil. tv.
Qed.
Lemma rev_nil_aux : forall l, fold_left (fun r x => econs x r) l enil = l.
Proof.
ir. ap fold_left_nil.
Qed.
Lemma rev_cons_aux : forall l x l', is_elist l' ->
fold_left (fun r x => econs x r) l (econs x l') = fold_left (fun r x => econs x r) (econs x l) l'.
Proof.
ir. rw fold_left_cons. tv. am.
Qed.
Inductive is_elist_alt : EP :=
| nil_elist_alt : is_elist_alt enil
| cons_elist_alt : forall l, is_elist_alt l -> forall x, is_elist_alt (econs x l).
Lemma elist_alt_eq : is_elist =is_elist_alt.
Proof.
ap arrow_extensionality.
assert (forall a, is_elist a -> is_elist_alt a). ap elist_ind.
constructor. ir. constructor. am.
ir. ap iff_eq;ir. au.
nin H0. ap is_elist_nil. ap econs_list. am.
Qed.
Lemma rev_aux_ev_end : forall l', is_elist l' -> forall l, is_elist l ->
forall n, inc n (list_length l) ->
ev (fold_left (fun r x => econs x r) l l') (nPlus (list_length l') n) = ev l n.
Proof.
intros l' Hl'. cp Hl'. rwi elist_alt_eq H. nin H.
ir. rw nil_length. rw nPlus_0_l. rw fold_left_nil. tv.
cp (omega_ordinal). uh H1;ee;eapply H1. ap H. tv.
wri elist_alt_eq H.
cp (IHis_elist_alt H). clear IHis_elist_alt.
ir.
assert (inc n nNum). cp omega_ordinal. uh H3;ee;eapply H3. ap H1. am.
rw fold_left_cons.
rw econs_length. rw nPlus_S_l. wr nPlus_S. rw H0.
rw econs_S. tv. am. am. ap econs_list. am.
rw econs_length. wr nLt_inc_rw;nSolveS.
wr nLt_S_S;nSolveS. rw nLt_inc_rw;nSolveS.
am. am. am. am. am.
Qed.
Lemma rev_aux_ev_begin : forall l', is_elist l' -> forall l, is_elist l ->
forall n, inc n nNum -> forall k, inc k nNum ->
list_length l' = oS (nPlus n k) ->
ev (fold_left (fun r x => econs x r) l l') n = ev l' k.
Proof.
intros l' Hl'.
cp Hl'. rwi elist_alt_eq H. nin H.
ir. rwi nil_length H2.
symmetry in H2;apply oS_emptyset_neq in H2;nin H2.
cp H. wri elist_alt_eq H0.
cp (IHis_elist_alt H0). clear IHis_elist_alt.
intro l'. ir.
rwi econs_length H5. apply oS_N_inj in H5;nSolveS.
rw fold_left_cons.
apply nNum_destruct in H4;nin H4. subst.
rwi nPlus_0 H5. subst.
wr (nPlus_0 H3). rw rev_aux_ev_end. rw econs_0. rw econs_0.
tv. am. am. am. ap econs_list;am.
rw econs_length. wr nLt_inc_rw;nSolveS.
rw nLt_S_rw;nSolveS. wr nLeq_S_S;nSolveS. am.
destruct H4 as [k' H4];ee;subst.
rwi nPlus_S H5;nSolveS.
erewrite H1. rw econs_S. ap eq_refl.
am.
wr nLt_inc_rw;nSolveS. rw nLt_S_rw;nSolveS.
rw nLeq_rw;nSolveS. exists n. ee. am. rw nPlus_S_l;nSolveS.
rw nPlus_comm. am.
ap econs_list;am. am. am. am. am.
Qed.
Lemma rev_pr : forall l, is_elist l -> forall i, inc i nNum -> forall k, inc k nNum ->
list_length l = oS (nPlus i k) ->
ev (rev l) i = ev l k.
Proof.
ir. uf rev.
ap rev_aux_ev_begin.
am. ap is_elist_nil. am. am. am.
Qed.
Inductive list_of_alt : E2P :=
| list_of_nil_alt : forall a, list_of_alt enil a
| list_of_cons_alt : forall a l, list_of_alt l a -> forall x, inc x a -> list_of_alt (econs x l) a
.
Lemma list_of_alt_eq : list_of = list_of_alt.
Proof.
ap arrow_extensionality. intro l. ap arrow_extensionality.
ir. ap iff_eq;ir.
cp (list_of_list H). rwi elist_alt_eq H0. nin H0.
constructor. apply list_of_cons in H. ee.
apply IHis_elist_alt in H1. constructor. am. am.
rw elist_alt_eq;am.
nin H. ap nil_list_of.
ap cons_list_of. am. am.
Qed.
Lemma list_of_ind : forall (a : E) (P : EP),
P enil ->
(forall l, list_of l a -> P l -> forall x, inc x a -> P (econs x l)) ->
forall l, list_of l a -> P l.
Proof.
intros a P Hnil IH.
pose (p l := list_of l a -> P l).
assert (forall l, is_elist l -> p l).
ap elist_ind;uf p;clear p;ir.
am. apply list_of_cons in H1. ee.
ap IH. am. ap H0. am. am.
am.
ir. ap H. eapply list_of_list. am.
am.
Qed.
Lemma rev_aux_length : forall l', is_elist l' -> forall l, is_elist l ->
list_length (fold_left (fun r x => econs x r) l l') = nPlus (list_length l) (list_length l').
Proof.
intros l' Hl'. cp Hl'.
rwi elist_alt_eq H. nin H.
ir. rw fold_left_nil. rw nil_length. rw nPlus_0. tv. am.
wri elist_alt_eq H.
cp (IHis_elist_alt H). clear IHis_elist_alt.
intro l'. ir.
rw fold_left_cons. rw H0. rw econs_length. rw econs_length.
rw nPlus_S;nSolveS. rw nPlus_S_l;nSolveS.
ap econs_list;am. am.
Qed.