forked from inQWIRE/QuantumLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rings.v
3156 lines (2460 loc) · 84.4 KB
/
Rings.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import List.
Require Export Complex.
(*
Require Export Quantum.
Require Import FTA.
*)
(* how should I do this? *)
(*
Lemma Submonoid_theorem : forall (R : Set) `{Monoid R} (S : Set)
(S0 : S) (Splus : S -> S -> S) (StoR : S -> R),
(forall s1 s2, StoR (Splus s1 s2) = ((StoR s1) + (StoR s2))%G) ->
Monoid S.
Proof. intros.
split.
Lemma Subring_theorem : forall (R : Set) `{Comm_Ring R} (S : Set) (S0 S1 : S) (Splus : S -> S -> S)
(Sopp : S -> S) (Smult : S -> S -> S) (StoR : S -> R),
(forall s1 s2, StoR (Splus s1 s2) = ((StoR s1) + (StoR s2))%G) ->
(forall s, StoR (Sopp s) = (- (StoR s))%G) ->
Monoid S.
Proof. intros.
*)
Open Scope C_scope.
(* exponentiation by positives *)
Fixpoint Cpow_pos (c : C) (p : positive) : C :=
match p with
| xH => c
| xO p' => (Cpow_pos c p') * (Cpow_pos c p')
| xI p' => c * (Cpow_pos c p') * (Cpow_pos c p')
end.
Lemma Cpow_pos_to_nat : forall (c : C) (p : positive),
Cpow_pos c p = c ^ (Pos.to_nat p).
Proof. intros.
induction p.
- rewrite Pos2Nat.inj_xI, Nat.mul_comm; simpl.
rewrite Cpow_mult, IHp.
lca.
- rewrite Pos2Nat.inj_xO, Nat.mul_comm; simpl.
rewrite Cpow_mult, IHp.
lca.
- lca.
Qed.
Lemma Cpow_pos_nonzero : forall (c : C) (p : positive), c <> C0 -> Cpow_pos c p <> C0.
Proof. intros.
rewrite Cpow_pos_to_nat.
apply Cpow_nonzero.
easy.
Qed.
Lemma Cpow_pos_add_r : forall (c : C) (p1 p2 : positive),
Cpow_pos c (p1 + p2) = (Cpow_pos c p1) * (Cpow_pos c p2).
Proof. intros.
do 3 rewrite Cpow_pos_to_nat.
rewrite Pos2Nat.inj_add, Cpow_add.
easy.
Qed.
Lemma Cpow_pos_mult_r : forall (c : C) (p1 p2 : positive),
Cpow_pos c (p1 * p2) = Cpow_pos (Cpow_pos c p1) p2.
Proof. intros.
do 3 rewrite Cpow_pos_to_nat.
rewrite Pos2Nat.inj_mul, Cpow_mult.
easy.
Qed.
Lemma Cpow_pos_mult_l : forall (c1 c2 : C) (p : positive),
Cpow_pos (c1 * c2) p = (Cpow_pos c1 p) * (Cpow_pos c2 p).
Proof. intros.
do 3 rewrite Cpow_pos_to_nat.
rewrite Cpow_mul_l.
easy.
Qed.
Lemma Cpow_pos_succ : forall (c : C) (p : positive),
Cpow_pos c (Pos.succ p) = c * Cpow_pos c p.
Proof. intros.
induction p; simpl; auto.
- rewrite IHp; lca.
- lca.
Qed.
Lemma Cpow_pos_pred_double : forall (c : C) (p : positive),
c <> 0 ->
Cpow_pos c (Pos.pred_double p) = (/ c) * (Cpow_pos c p * Cpow_pos c p).
Proof. intros.
induction p; simpl; auto.
- repeat rewrite Cmult_assoc.
rewrite Cinv_l; try lca; auto.
- rewrite IHp.
repeat rewrite Cmult_assoc.
rewrite Cinv_r; try lca; auto.
- rewrite Cmult_assoc, Cinv_l; try lca; auto.
Qed.
Lemma Cpow_pos_inv : forall (c : C) (p : positive),
c <> 0 ->
Cpow_pos (/ c) p = / (Cpow_pos c p).
Proof. intros.
induction p; simpl.
- rewrite IHp.
repeat rewrite Cinv_mult_distr; auto.
2 : apply Cmult_neq_0.
all : try apply Cpow_pos_nonzero; auto.
- rewrite IHp.
rewrite Cinv_mult_distr; try apply Cpow_pos_nonzero; auto.
- easy.
Qed.
Lemma Cpow_pos_real : forall (c : C) (p : positive),
snd c = 0 -> snd (Cpow_pos c p) = 0.
Proof. intros.
induction p; simpl.
- rewrite IHp, H; lra.
- rewrite IHp; lra.
- easy.
Qed.
Lemma Cpow_pos_1 : forall (p : positive),
Cpow_pos C1 p = C1.
Proof. intros.
induction p; simpl.
- rewrite IHp; lca.
- rewrite IHp; lca.
- lca.
Qed.
(* exponentiation by integers *)
Definition Cpow_int (c : C) (z : Z) : C :=
match z with
| Z0 => C1
| Zpos p => Cpow_pos c p
| Zneg p => / Cpow_pos c p
end.
Infix "^^" := Cpow_int (at level 10) : C_scope.
Lemma Cpow_int_nonzero : forall (c : C) (z : Z), c <> C0 -> c ^^ z <> C0.
Proof. intros.
destruct z.
- apply C1_neq_C0.
- apply Cpow_pos_nonzero; easy.
- apply nonzero_div_nonzero.
apply Cpow_pos_nonzero; easy.
Qed.
Lemma Cpow_int_add_1 : forall (c : C) (z : Z),
c <> C0 -> c ^^ (1 + z) = c * c^^z.
Proof. intros.
destruct z; try lca.
- destruct p; simpl; try lca.
rewrite Cpow_pos_succ; lca.
- destruct p; simpl.
+ rewrite <- Cmult_assoc, (Cinv_mult_distr c); auto.
rewrite Cmult_assoc, Cinv_r; try lca; auto.
apply Cmult_neq_0; apply Cpow_pos_nonzero; easy.
+ rewrite Cpow_pos_pred_double, Cinv_mult_distr, Cinv_inv; auto.
apply nonzero_div_nonzero; auto.
apply Cmult_neq_0; apply Cpow_pos_nonzero; easy.
+ rewrite Cinv_r; easy.
Qed.
Lemma Cpow_int_minus_1 : forall (c : C) (z : Z),
c <> C0 -> c ^^ (-1 + z) = / c * c^^z.
Proof. intros.
destruct z; try lca.
- destruct p; simpl; try lca.
+ repeat rewrite Cmult_assoc.
rewrite Cinv_l; auto; lca.
+ rewrite Cpow_pos_pred_double; auto.
+ rewrite Cinv_l; easy.
- destruct p; simpl; try lca.
+ rewrite Cpow_pos_succ, <- Cinv_mult_distr; auto.
apply f_equal; lca.
repeat apply Cmult_neq_0; try apply Cpow_pos_nonzero; auto.
+ rewrite <- Cmult_assoc, Cinv_mult_distr; auto.
apply Cmult_neq_0; apply Cpow_pos_nonzero; auto.
+ rewrite Cinv_mult_distr; easy.
Qed.
Lemma Cpow_int_add_nat : forall (c : C) (n : nat) (z : Z),
c <> C0 -> c ^^ (Z.of_nat n + z) = c^^(Z.of_nat n) * c^^z.
Proof. intros.
induction n; try lca.
replace (S n + z)%Z with (1 + (n + z))%Z by lia.
replace (Z.of_nat (S n)) with (1 + Z.of_nat n)%Z by lia.
repeat rewrite Cpow_int_add_1; auto.
rewrite IHn; lca.
Qed.
Lemma Cpow_int_minus_nat : forall (c : C) (n : nat) (z : Z),
c <> C0 -> c ^^ (- Z.of_nat n + z) = c^^(- Z.of_nat n) * c^^z.
Proof. intros.
induction n; try lca.
replace (- S n + z)%Z with (- 1 + (- n + z))%Z by lia.
replace (- (S n))%Z with (- 1 + - n)%Z by lia.
repeat rewrite Cpow_int_minus_1; auto.
rewrite IHn; lca.
Qed.
Theorem Cpow_int_add_r : forall (c : C) (z1 z2 : Z),
c <> C0 -> c ^^ (z1 + z2) = c^^z1 * c^^z2.
Proof. intros.
destruct (Z_plusminus_nat z1) as [n [H0 | H0]].
- rewrite H0, Cpow_int_add_nat; easy.
- rewrite H0, Cpow_int_minus_nat; easy.
Qed.
Lemma Cpow_int_mult_r : forall (c : C) (z1 z2 : Z),
c <> C0 -> c ^^ (z1 * z2) = (c ^^ z1) ^^ z2.
Proof. intros.
destruct z1; destruct z2; try lca; simpl.
all : try (rewrite Cpow_pos_1; lca).
all : rewrite Cpow_pos_mult_r; try lca.
all : rewrite Cpow_pos_inv; try apply Cpow_pos_nonzero; auto.
rewrite Cinv_inv; auto.
do 2 apply Cpow_pos_nonzero; easy.
Qed.
Lemma Cpow_int_mult_l : forall (c1 c2 : C) (z : Z),
c1 <> C0 -> c2 <> C0 -> (c1 * c2) ^^ z = (c1 ^^ z) * (c2 ^^ z).
Proof. intros.
destruct z; try lca; simpl.
rewrite Cpow_pos_mult_l; easy.
rewrite Cpow_pos_mult_l, Cinv_mult_distr; auto;
apply Cpow_pos_nonzero; easy.
Qed.
Lemma Cpow_inv1 : forall (c : C) (z : Z), c <> C0 -> c^^(-z) = / (c^^z).
Proof. intros.
replace (-z)%Z with (z * -1)%Z by lia.
rewrite Cpow_int_mult_r; easy.
Qed.
Lemma Cpow_inv2 : forall (c : C) (z : Z), c <> C0 -> (/ c)^^z = / (c^^z).
Proof. intros.
replace z with (-1 * -z)%Z by lia.
rewrite Cpow_int_mult_r.
replace ((/ c) ^^ (-1)) with (/ / c) by easy.
replace (-1 * - z)%Z with z by lia.
rewrite Cinv_inv, Cpow_inv1; auto.
apply nonzero_div_nonzero; auto.
Qed.
(* checking that Cpow_int is consistent with Cpow on nats *)
Lemma Cpow_int_cons : forall (c : C) (n : nat),
c^n = c ^^ n.
Proof. intros.
destruct n; try lca.
unfold Cpow_int.
destruct (Z.of_nat (S n)) eqn:E; try easy.
rewrite Cpow_pos_to_nat.
apply f_equal_gen; try apply f_equal; auto.
apply Pos2Z.inj_pos in E.
rewrite <- E, SuccNat2Pos.id_succ.
easy.
Qed.
Lemma Cpow_int_real : forall (c : C) (z : Z),
c <> 0 -> snd c = 0 -> snd (c ^^ z) = 0.
Proof. intros.
destruct z; auto.
- simpl; apply Cpow_pos_real; auto.
- replace (Z.neg p) with (- Z.pos p)%Z by lia.
rewrite Cpow_inv1; auto.
apply div_real.
apply Cpow_pos_real; auto.
Qed.
(* foreboding: translating between Cpow_int and Zpow *)
Lemma ZtoC_pow_nat : forall (z : Z) (n : nat),
RtoC (IZR (z^n)%Z) = (RtoC (IZR z))^^n.
Proof. intros.
induction n; try easy.
rewrite <- Cpow_int_cons in *.
replace (S n) with (1 + n)%nat by lia.
rewrite Nat2Z.inj_add, Z.pow_add_r, Cpow_add_r; try lia.
rewrite mult_IZR, <- IHn, RtoC_mult, RtoC_pow, pow_IZR.
apply f_equal_gen; auto.
Qed.
(* foreboding: translating between Cpow_int and Zpow *)
Lemma ZtoC_pow : forall (z n : Z),
(n >= 0)%Z ->
RtoC (IZR (z^n)%Z) = (RtoC (IZR z))^^n.
Proof. intros.
destruct (Z_plusminus_nat n) as [x [H0 | H0]]; subst.
- rewrite ZtoC_pow_nat; easy.
- destruct x; try lia.
replace (-O)%Z with (Z.of_nat O) by lia.
rewrite ZtoC_pow_nat; easy.
Qed.
Hint Rewrite plus_IZR minus_IZR opp_IZR mult_IZR ZtoC_pow : ZtoR_db.
(* NB: use plus_IZR, mult_IZR, RtoC_plus, RtoC_mult to move between types: *)
(* quick helper tactic. TODO: centralize these *)
Ltac fastZtoC := repeat (autorewrite with ZtoR_db; autorewrite with RtoC_db; try lia).
(*
*
*
*)
(* fst d is the 2-adic value of d, snd d is (odd leftover part / 2) *)
(* should this be type or Set ?*)
(* TODO: could make Dpos Dneg, like Z, but this makes for a lot of cases *)
Inductive Dyadic : Set :=
| D0 : Dyadic
| Dn : Z -> Z -> Dyadic. (* fst arg = 2-adic value, snd arg = (odd_part / 2) *)
Definition D1 : Dyadic := Dn 0 0.
Definition D2 : Dyadic := Dn 1 0.
Definition Dhalf : Dyadic := Dn (Z.opp 1) 0.
Declare Scope D_scope.
Delimit Scope D_scope with D.
Open Scope D_scope.
Bind Scope D_scope with Dyadic.
Open Scope Z_scope.
Coercion IZR : Z >-> R.
Definition DtoC (x : Dyadic) : C :=
match x with
| D0 => C0
| Dn n x => (C2^^n * (C2 * x + 1))
end.
Coercion DtoC : Dyadic >-> C.
(* Coercions make things soo nice! *)
Lemma DtoC_D0 : DtoC D0 = C0.
Proof. easy. Qed.
Lemma DtoC_D1 : DtoC D1 = C1.
Proof. lca. Qed.
Lemma DtoC_D2 : DtoC D2 = C2.
Proof. lca. Qed.
Lemma DtoC_Dhalf : DtoC Dhalf = / C2.
Proof. unfold DtoC, Dhalf.
rewrite Cpow_inv1.
lca.
apply RtoC_neq.
lra.
Qed.
Lemma DtoC_neq_0 : forall (z z0 : Z),
(C2 ^^ z * (C2 * z0 + C1))%C <> C0.
Proof. intros.
apply Cmult_neq_0.
apply Cpow_int_nonzero.
apply RtoC_neq; lra.
replace (C2 * z0 + C1)%C with (RtoC (IZR (2 * z0 + 1)%Z)).
unfold not; intros.
apply RtoC_inj in H.
apply eq_IZR in H.
lia.
fastZtoC.
easy.
Qed.
Lemma move_power_2 : forall (z1 z2 : Z) (c1 c2 : C),
(C2 ^^ z1 * c1 = C2 ^^ z2 * c2)%C -> (c1 = C2 ^^ (z2 - z1) * c2)%C.
Proof. intros.
apply (Cmult_simplify (C2 ^^ (-z1)) (C2 ^^ (-z1))) in H; auto.
do 2 rewrite Cmult_assoc, <- Cpow_int_add_r in H.
replace (- z1 + z1) with 0 in H by lia; simpl in H.
rewrite Cmult_1_l in H; subst.
replace (- z1 + z2) with (z2 - z1) by lia.
easy.
all : apply RtoC_neq; lra.
Qed.
(* TODO: move these next two lemmas somewhere else, they are never used here *)
Lemma log2_inj_0 : forall (n : nat),
(C2 ^ n)%C = C1 -> n = O.
Proof. intros.
destruct n; try easy; simpl in *.
assert (H' : forall n, (C2 ^ n)%C = RtoC (IZR ((2 ^ n)%Z))).
{ induction n0; auto.
replace (C2 ^ S n0)%C with (C2 * (C2 ^ n0))%C by easy.
rewrite IHn0.
autorewrite with ZtoR_db; try lia.
replace (Z.of_nat (S n0)) with (1 + n0)%Z by lia.
rewrite Cpow_int_add_r; try easy.
apply RtoC_neq; lra. }
rewrite H' in H.
simpl in H.
rewrite <- RtoC_mult in H.
apply RtoC_inj in H.
rewrite <- mult_IZR in H.
apply eq_IZR in H.
lia.
Qed.
Lemma log2_inj : forall (n1 n2 : nat),
Cpow C2 n1 = Cpow C2 n2 -> n1 = n2.
Proof. induction n1.
- intros.
replace (C2 ^ 0)%C with C1 in H by easy.
symmetry in H.
apply log2_inj_0 in H; easy.
- intros.
destruct n2.
replace (C2 ^ 0)%C with C1 in H by easy.
apply log2_inj_0 in H; easy.
simpl in H.
rewrite (IHn1 n2); auto.
apply Cmult_cancel_l in H; auto.
apply RtoC_neq; lra.
Qed.
Lemma DtoC_inj_uneq_case : forall z z0 z1 z2 : Z,
z < z1 ->
(C2 ^^ z * (C2 * z0 + C1))%C = (C2 ^^ z1 * (C2 * z2 + C1))%C ->
False.
Proof. intros.
apply move_power_2 in H0.
assert (H' : 0 < z1 - z). lia.
destruct (Z_plusminus_nat (z1 - z)) as [x [H1 | H1]]; try lia.
rewrite H1 in *.
destruct x; try easy.
rewrite <- ZtoC_pow_nat in H0.
repeat rewrite <- RtoC_mult, <- mult_IZR in H0.
repeat rewrite <- RtoC_plus, <- plus_IZR in H0.
repeat rewrite <- RtoC_mult, <- mult_IZR in H0.
apply RtoC_inj in H0.
apply eq_IZR in H0.
replace (Z.of_nat (S x)) with (1 + x)%Z in H0 by lia.
rewrite Z.pow_add_r in H0; try lia.
Qed.
(* idea: WLOG v(x) <= v(y), equality case is easy, then reflect back to Z *)
Lemma DtoC_inj : forall (x y : Dyadic),
DtoC x = DtoC y -> x = y.
Proof.
intros.
unfold DtoC in *.
destruct x; destruct y; try easy.
- assert (H' := DtoC_neq_0 z z0).
rewrite H in H'; easy.
- assert (H' := DtoC_neq_0 z z0).
rewrite H in H'; easy.
- destruct (Ztrichotomy_inf z z1) as [[H0 | H0] | H0].
+ apply DtoC_inj_uneq_case in H; lia.
+ subst.
apply move_power_2 in H.
replace (z1 - z1)%Z with 0%Z in H by lia.
rewrite Cmult_1_l in H.
apply (Cplus_simplify _ _ (-C1) (-C1)) in H; auto.
replace (C2 * z0 + C1 + - C1)%C with (C2 * z0)%C in H by lca.
replace (C2 * z2 + C1 + - C1)%C with (C2 * z2)%C in H by lca.
apply Cmult_cancel_l in H.
apply RtoC_inj in H.
apply eq_IZR in H; subst; easy.
apply RtoC_neq; lra.
+ symmetry in H.
apply DtoC_inj_uneq_case in H; lia.
Qed.
(* this is actually overkill because Z is decible, but shows usefulness of DtoC_inj *)
Lemma Deq_dec (d1 d2 : Dyadic) : { d1 = d2 } + { ~ (d1 = d2) }.
Proof.
destruct (Ceq_dec (DtoC d1) (DtoC d2)).
- apply DtoC_inj in e.
left; easy.
- right.
unfold not; intros; apply n.
rewrite H; easy.
Qed.
(* TODO: create 'lda', which is like lca but for dyadics *)
(*
(* lca, a great tactic for solving computations or basic equality checking *)
Lemma c_proj_eq : forall (c1 c2 : C), fst c1 = fst c2 -> snd c1 = snd c2 -> c1 = c2.
Proof. intros c1 c2 H1 H2. destruct c1, c2. simpl in *. subst. reflexivity. Qed.
(* essentially, we just bootsrap coq's lra *)
Ltac lca := eapply c_proj_eq; simpl; lra.
*)
(** *** Arithmetic operations *)
(* this one will be terrible... but just need to prove DtoC_plus! *)
Definition Dplus (x y : Dyadic) : Dyadic :=
match (x, y) with
| (D0, _) => y
| (_, D0) => x
| (Dn n x', Dn m y') =>
match Ztrichotomy_inf n m with
| inleft (left _) => (* x has lower power of 2 *)
Dn n (2^(m-n-1) * (2*y'+1) + x')
| inleft (right _) => (* equal power of 2 *)
match odd_part ((2*x'+1) + (2*y'+1)) with
| Z0 => D0
| _ => Dn (n + two_val ((2*x'+1) + (2*y'+1)))
(((odd_part ((2*x'+1) + (2*y'+1))) - 1) / 2)
end (* y has lower power of 2 *)
| inright _ => Dn m (2^(n-m-1) * (2*x'+1) + y')
end
end.
Definition Dopp (x : Dyadic) : Dyadic :=
match x with
| D0 => D0
| Dn n x => Dn n (-x-1)
end.
Definition Dminus (x y : Dyadic) : Dyadic := Dplus x (Dopp y).
(* this one is much easier! *)
Definition Dmult (x y : Dyadic) : Dyadic :=
match (x, y) with
| (D0, _) => D0
| (_, D0) => D0
| (Dn n x, Dn m y) => Dn (n + m) (2*x*y + x + y)
end.
Fixpoint Dpow (d : Dyadic) (n : nat) : Dyadic :=
match n with
| 0%nat => D1
| S n' => Dmult d (Dpow d n')
end.
Infix "+," := Dplus (at level 50, left associativity) : D_scope.
Infix "*," := Dmult (at level 40, left associativity) : D_scope.
Notation "-, d" := (Dopp d) (at level 35) : D_scope.
Infix "-," := Dminus (at level 50, left associativity) : D_scope.
Infix "^," := Dpow (at level 30) : D_scope.
(* showing compatability *)
(* could make this shorter, but it was also expected that this would be the hardest part,
so its messiness is warrented *)
Lemma DtoC_plus : forall d1 d2, DtoC (d1 +, d2) = (DtoC d1 + DtoC d2)%C.
Proof. intros.
destruct d1; destruct d2; simpl; try lca.
unfold Dplus; destruct (Ztrichotomy_inf z z1); try destruct s.
- unfold Dplus, DtoC.
fastZtoC.
rewrite (Cmult_plus_distr_l _ _ z0), (Cmult_assoc C2).
replace (C2 * C2 ^^ (z1 - z - 1))%C with (C2 ^^ (z1 - z)).
rewrite <- Cplus_assoc, (Cmult_plus_distr_l _ _ (C2 * z0 + C1)), Cmult_assoc.
replace (C2 ^^ z * C2 ^^ (z1 - z))%C with (C2 ^^ z1).
lca.
rewrite <- Cpow_int_add_r.
apply f_equal; lia.
apply RtoC_neq; lra.
replace (Cmult C2) with (Cmult (C2 ^^ 1)) by (apply f_equal; easy).
rewrite <- Cpow_int_add_r.
apply f_equal; lia.
apply RtoC_neq; lra.
- subst.
replace (2 * z0 + 1 + (2 * z2 + 1))%Z with (2 * (z0 + z2 + 1))%Z by lia.
rewrite odd_part_reduce.
destruct (Z.eq_dec (z0 + z2 + 1) Z0).
rewrite e. simpl.
replace (C2 ^^ z1 * (C2 * z0 + C1) + C2 ^^ z1 * (C2 * z2 + C1))%C with
(C2 ^^ z1 * C2 * (z0 + z2 + C1))%C by lca.
do 2 rewrite <- RtoC_plus, <- plus_IZR.
rewrite e.
lca.
assert (H' : odd_part (z0 + z2 + 1) <> 0).
unfold not; intros; apply n.
apply odd_part_0; auto.
destruct (odd_part (z0 + z2 + 1)) eqn:E; try easy.
all : unfold DtoC;
repeat (repeat rewrite <- RtoC_plus, <- plus_IZR;
repeat rewrite <- RtoC_mult, <- mult_IZR).
all : rewrite <- E, odd_part_odd, Cpow_int_add_r, <- Cmult_plus_distr_l,
<- Cmult_assoc; auto; try apply f_equal.
all : try (apply RtoC_neq; lra).
all : rewrite <- ZtoC_pow, <- odd_part_reduce; try apply two_val_ge_0.
all : rewrite <- RtoC_mult, <- mult_IZR, <- RtoC_plus, <- plus_IZR.
all : rewrite <- twoadic_breakdown; try lia.
all : fastZtoC; lca.
- unfold Dplus, DtoC.
fastZtoC.
rewrite (Cmult_plus_distr_l _ _ z2), (Cmult_assoc C2).
replace (C2 * C2 ^^ (z - z1 - 1))%C with (C2 ^^ (z - z1)).
rewrite <- Cplus_assoc, (Cmult_plus_distr_l _ _ (C2 * z2 + C1)), Cmult_assoc.
replace (C2 ^^ z1 * C2 ^^ (z - z1))%C with (C2 ^^ z).
lca.
rewrite <- Cpow_int_add_r.
apply f_equal; lia.
apply RtoC_neq; lra.
replace (Cmult C2) with (Cmult (C2 ^^ 1)) by (apply f_equal; easy).
rewrite <- Cpow_int_add_r.
apply f_equal; lia.
apply RtoC_neq; lra.
Qed.
Lemma DtoC_opp : forall d, DtoC (-, d) = (- (DtoC d))%C.
Proof. intros.
destruct d; try lca.
unfold Dopp, DtoC, Zminus.
fastZtoC.
rewrite Copp_mult_distr_r.
lca.
Qed.
Lemma DtoC_minus : forall d1 d2, DtoC (d1 -, d2) = ((DtoC d1) - (DtoC d2))%C.
Proof. intros.
unfold Cminus, Dminus.
rewrite DtoC_plus, DtoC_opp.
easy.
Qed.
(* this is surprisingly easy! *)
Lemma DtoC_mult : forall d1 d2, DtoC (d1 *, d2) = (DtoC d1 * DtoC d2)%C.
Proof. intros.
destruct d1; destruct d2; try lca.
unfold Dmult, DtoC.
fastZtoC.
rewrite Cpow_int_add_r.
lca.
apply RtoC_neq; lra.
Qed.
Lemma DtoC_pow : forall d n, DtoC (d ^, n) = ((DtoC d) ^ n)%C.
Proof. intros.
induction n.
- lca.
- simpl.
rewrite DtoC_mult, IHn.
easy.
Qed.
Hint Rewrite DtoC_plus DtoC_opp DtoC_minus DtoC_mult DtoC_pow : DtoC_db.
Ltac lDa_try1 := apply DtoC_inj; autorewrite with DtoC_db; lca.
(* Note that once we have a injective homomorphism into C,
we get all the ring axioms for free! *)
(* TODO: generalize this in Summation.v *)
Lemma Dplus_comm : forall d1 d2, d1 +, d2 = d2 +, d1.
Proof. intros. lDa_try1. Qed.
Lemma Dplus_assoc : forall d1 d2 d3, d1 +, (d2 +, d3) = d1 +, d2 +, d3.
Proof. intros. lDa_try1. Qed.
Lemma Dplus_0_r : forall d, d +, D0 = d.
Proof. intros. lDa_try1. Qed.
Lemma Dplus_0_l : forall d, D0 +, d = d.
Proof. intros. lDa_try1. Qed.
Lemma Dminus_0_r : forall d, d -, D0 = d.
Proof. intros. lDa_try1. Qed.
Lemma Dminus_0_l : forall d, D0 -, d = -, d.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_0_r : forall d, d *, D0 = D0.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_0_l : forall d, D0 *, d = D0.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_comm : forall d1 d2, d1 *, d2 = d2 *, d1.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_assoc : forall d1 d2 d3, d1 *, (d2 *, d3) = d1 *, d2 *, d3.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_1_r : forall d, d *, D1 = d.
Proof. intros. lDa_try1. Qed.
Lemma Dmult_1_l : forall d, D1 *, d = d.
Proof. intros. lDa_try1. Qed.
Lemma Dopp_mult_distr_l : forall d1 d2, -, (d1 *, d2) = (-, d1) *, d2.
Proof. intros. lDa_try1. Qed.
Lemma Dopp_mult_distr_r : forall d1 d2, -, (d1 *, d2) = d1 *, (-, d2).
Proof. intros. lDa_try1. Qed.
Lemma Dopp_mult_neg_l : forall d, -, d = (-, D1) *, d.
Proof. intros. lDa_try1. Qed.
Lemma Dopp_mult_neg_r : forall d, -, d = d *, (-, D1).
Proof. intros. lDa_try1. Qed.
Lemma Dminus_diag : forall d1 d2 : Dyadic, d1 = d2 -> d1 -, d2 = D0.
Proof. intros; subst.
apply DtoC_inj.
rewrite DtoC_minus.
replace (DtoC D0) with C0 by lca.
apply Cminus_diag.
easy.
Qed.
Lemma Dminus_eq_0 : forall r1 r2 : Dyadic, r1 -, r2 = D0 -> r1 = r2.
Proof. intros.
unfold Dminus, Dopp, Dplus.
apply DtoC_inj.
apply Cminus_eq_0.
rewrite <- DtoC_minus, H.
easy.
Qed.
Lemma Dmult_neq_0 : forall d1 d2 : Dyadic, d1 <> D0 -> d2 <> D0 -> d1 *, d2 <> D0.
Proof. intros.
unfold not; intros.
apply (f_equal_gen DtoC DtoC) in H1; auto.
rewrite DtoC_mult, DtoC_D0 in H1.
apply Cmult_integral in H1.
destruct H1.
apply H; apply DtoC_inj; easy.
apply H0; apply DtoC_inj; easy.
Qed.
Lemma Dmult_integral : forall d1 d2 : Dyadic, d1 *, d2 = D0 -> d1 = D0 \/ d2 = D0.
Proof. intros.
destruct (Deq_dec d1 D0); try (left; easy).
destruct (Deq_dec d2 D0); try (right; easy).
apply (Dmult_neq_0 d1 d2) in n0; auto.
easy.
Qed.
Lemma Dpow_add : forall (d : Dyadic) (n m : nat), d ^, (n + m) = d^,n *, d^,m.
Proof. intros.
apply DtoC_inj.
rewrite DtoC_mult.
do 3 rewrite DtoC_pow.
rewrite Cpow_add; easy.
Qed.
Lemma Dpow_mult : forall (d : Dyadic) (n m : nat), d ^, (n * m) = (d ^, n) ^, m.
Proof. intros.
apply DtoC_inj.
do 3 rewrite DtoC_pow.
rewrite Cpow_mult; easy.
Qed.
(* we showcase two different approaches to numerical proofs based on the above *)
Lemma Dhalf_double_1 : Dhalf +, Dhalf = D1.
Proof. unfold Dhalf, Dplus; simpl.
replace (0 / 2) with 0%Z.
easy.
rewrite Zdiv.Zdiv_0_l.
easy.
Qed.
Lemma Dhalf_double_2 : Dhalf +, Dhalf = D1.
Proof. apply DtoC_inj.
rewrite DtoC_plus.
rewrite DtoC_Dhalf, DtoC_D1.
lca.
Qed.
(* I prefer the second way *)
Lemma Dmult_2_half : D2 *, Dhalf = D1.
Proof. apply DtoC_inj.
rewrite DtoC_mult.
rewrite DtoC_Dhalf, DtoC_D2, DtoC_D1.
lca.
Qed.
Lemma Dmult_half_2 : Dhalf *, D2 = D1.
Proof. apply DtoC_inj.
rewrite DtoC_mult.
rewrite DtoC_Dhalf, DtoC_D2, DtoC_D1.
lca.
Qed.
Lemma Dpow_2_pos : forall (n : positive), D2 ^, (Pos.to_nat n) = Dn (Z.pos n) 0.
Proof. intros.
apply DtoC_inj.
rewrite DtoC_pow; simpl.
rewrite Cpow_pos_to_nat, Cmult_0_r, Cplus_0_l, Cmult_1_r, Cmult_1_r.
easy.
Qed.
Lemma Dpow_2_nat : forall (n : nat), D2 ^, (S n) = Dn (Z.of_nat (S n)) 0.
Proof. intros; simpl.
rewrite <- Dpow_2_pos.
replace (D2 *, D2 ^, n) with (D2 ^, (S n)) by easy.
apply f_equal_gen; auto.
rewrite SuccNat2Pos.id_succ; easy.
Qed.
(* some more specific lemmas: *)
Lemma Dyadic_real : forall (d : Dyadic), snd (DtoC d) = 0.
Proof. intros.
destruct d; unfold DtoC; simpl; try lra.
rewrite Cpow_int_real; simpl; try lra.
apply RtoC_neq; lra.
Qed.
Lemma Cmult_d_l : forall (d : Dyadic) (r1 r2 : R),
(d * (r1, r2))%C = (fst (DtoC d) * r1, fst (DtoC d) * r2)%R.
Proof. intros.
unfold Cmult, DtoC; destruct d; simpl.
all : apply injective_projections; simpl; try lra.
all : replace (snd (C2 ^^ z)) with 0%R; try lra.
all : rewrite Cpow_int_real; auto.
all : apply C0_fst_neq; simpl; lra.
Qed.
Lemma Cconj_D : forall (d : Dyadic), Cconj d = d.
Proof. intros.
destruct (DtoC d) eqn:E.
unfold Cconj; simpl.
replace r0 with (snd (DtoC d)) by (rewrite E; easy).
rewrite Dyadic_real.
apply c_proj_eq; simpl; lra.
Qed.
Lemma Dyadic_is_0 : forall (d : Dyadic),
fst (DtoC d) = 0 ->
d = D0.
Proof. intros.
apply DtoC_inj.
apply injective_projections; auto.
do 2 rewrite Dyadic_real.
easy.
Qed.
(*
*
*
*)
(* now we add Z *)
Close Scope Z_scope.
Open Scope D_scope.
(* the nonzero case is quite adhoc here, might want to use more machinery from above *)
Definition ZtoD (z : Z) : Dyadic :=
match z with
| Z0 => D0
| Zpos p => Dn (two_val z) ((odd_part z - 1) / 2)
| Zneg p => Dn (two_val z) ((odd_part z - 1) / 2)
end.
Lemma ZtoD_0 : ZtoD 0 = D0.
Proof. easy. Qed.
Lemma ZtoD_1 : ZtoD 1 = D1.
Proof. easy. Qed.
Lemma ZtoD_2 : ZtoD 2 = D2.
Proof. easy. Qed.
Lemma ZtoDtoC_is_ZtoRtoC : forall (z : Z), DtoC (ZtoD z) = RtoC (IZR z).
Proof. intros.
destruct z; try lca;
destruct p;
unfold ZtoD, DtoC;
rewrite <- RtoC_mult, <- mult_IZR, <- RtoC_plus, <- plus_IZR, odd_part_odd;
try lia; rewrite <- ZtoC_pow; try apply two_val_ge_0;
rewrite <- RtoC_mult, <- mult_IZR, <- twoadic_breakdown; try lia;
easy.
Qed.
(*
(*TODO: deal with this warning *)
(* This should really be Z -> D -> Q -> R -> C *)
Coercion ZtoD : Z >-> Dyadic.
*)
(* we now see the power of the injections and ZtoDtoC_is_ZtoRtoC for these next lemmas ! *)
(* TODO: automate proofs like this? *)
Lemma ZtoD_inj : forall (x y : Z),
ZtoD x = ZtoD y -> x = y.
Proof. intros.
apply eq_IZR; apply RtoC_inj.
do 2 rewrite <- ZtoDtoC_is_ZtoRtoC.
rewrite H.
easy.
Qed.
Lemma ZtoD_plus : forall (z1 z2 : Z), ZtoD (z1 + z2)%Z = ZtoD z1 +, ZtoD z2.
Proof. intros.
apply DtoC_inj.