-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdyadic.v
More file actions
3671 lines (3352 loc) · 89.1 KB
/
dyadic.v
File metadata and controls
3671 lines (3352 loc) · 89.1 KB
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 ZArith PArith QArith ProofIrrelevance.
Require Import Lia.
Record DO : Set :=
Dmake { num : Z;
den : positive }.
Definition pow_pos (p r : positive) : positive :=
Pos.iter (Pos.mul p) 1%positive r.
Lemma pow_pos_Zpow_pos p r : Zpos (pow_pos p r) = Z.pow_pos (Zpos p) r.
Proof.
unfold pow_pos, Z.pow_pos.
rewrite !Pos2Nat.inj_iter; generalize (Pos.to_nat r) as n; intro.
revert p; induction n; auto.
intros p; simpl; rewrite <-IHn; auto.
Qed.
Definition DO_to_Q (d : DO) :=
Qmake (num d) (shift_pos (den d) 1).
(* Coercion DO_to_Q : DO >-> Q. *)
Definition DO0 : DO := Dmake 0 1.
Definition DO1 : DO := Dmake 2 1.
Lemma DO_to_Q0' : DO_to_Q DO0 = 0 # 2.
Proof. auto. Qed.
Lemma DO_to_Q0 : DO_to_Q DO0 == 0.
Proof. rewrite DO_to_Q0'; unfold Qeq; simpl; auto. Qed.
Lemma DO_to_Q1' : DO_to_Q DO1 = 2 # 2.
Proof. auto. Qed.
Lemma DO_to_Q1 : DO_to_Q DO1 == 1.
Proof. rewrite DO_to_Q1'; unfold Qeq; simpl; auto. Qed.
Definition DOadd (d1 d2 : DO) : DO :=
match d1, d2 with
| Dmake x1 y1, Dmake x2 y2 =>
if Pos.ltb y1 y2 then
Dmake (Z.pow_pos 2 (y2 - y1) * x1 + x2) y2
else if Pos.ltb y2 y1 then
Dmake (Z.pow_pos 2 (y1 - y2) * x2 + x1) y1
else Dmake (x1 + x2) y1
end.
Lemma Qdiv_mult (s q r : Q) :
~ s == 0 ->
(q / r) == (q * s) / (r * s).
Proof.
intros H; unfold Qdiv.
assert (q * s * /(r * s) == q * /r) as ->.
{ rewrite Qinv_mult_distr, (Qmult_comm (/r)), Qmult_assoc.
rewrite <-(Qmult_assoc q), Qmult_inv_r, Qmult_1_r; auto.
apply Qeq_refl. }
apply Qeq_refl.
Qed.
Lemma Qdiv_1_r q : q / 1 == q.
Proof.
unfold Qdiv, Qinv; simpl; rewrite Qmult_1_r.
apply Qeq_refl.
Qed.
Lemma Zdiv_pos0 (x y : positive) : (Zpos (x~0) / Zpos (y~0) = Zpos x / Zpos y)%Z.
Proof.
rewrite Pos2Z.inj_xO, (Pos2Z.inj_xO y).
rewrite Zdiv_mult_cancel_l; auto.
inversion 1.
Qed.
Lemma Zpow_pos_2exp (x y : nat) :
(y < x)%nat ->
Z.pow 2 (Z.of_nat (x - y)) = (Z.pow 2 (Z.of_nat x) / Z.pow 2 (Z.of_nat y))%Z.
Proof.
intros H; rewrite <-!two_power_nat_equiv; unfold two_power_nat.
revert y H; induction x; simpl.
{ destruct y; try solve[inversion 1]. }
destruct y; simpl.
{ intros H; rewrite Zdiv_1_r; auto. }
intros H.
rewrite IHx.
{ rewrite Zdiv_pos0; auto. }
apply lt_S_n; auto.
Qed.
Lemma Pos_iter_swap' T f g (r : T) (x : positive) :
(forall t, f (g t) = t) ->
Pos.iter f (Pos.iter g r x) x = r.
Proof.
rewrite 2!Pos2Nat.inj_iter.
assert (H: exists y, Pos.to_nat x = Pos.to_nat y).
{ exists x; auto. }
revert H; generalize (Pos.to_nat x) as n; intros n H.
revert r; induction n; simpl; auto.
intros r H2.
destruct (Nat.eq_dec n 0).
{ subst n.
simpl.
rewrite H2; auto. }
assert (H3: exists y, n = Pos.to_nat y).
{ exists (Pos.of_nat n).
rewrite Nat2Pos.id; auto. }
destruct H3 as [y H3].
subst n.
rewrite <-Pos2Nat.inj_iter.
rewrite <-Pos.iter_swap.
rewrite H2.
rewrite Pos2Nat.inj_iter.
apply IHn; auto.
exists y; auto.
Qed.
Lemma Pos_lt_Zpos_Zlt x y :
(x < y)%positive ->
(Zpos' x < Zpos' y)%Z.
Proof.
unfold Z.lt; simpl; rewrite <-Pos.ltb_lt.
rewrite Pos.ltb_compare.
destruct (Pos.compare x y); auto; try solve[inversion 1].
Qed.
Lemma Zlt_le x y : (x < y -> x <= y)%Z.
Proof.
unfold Z.le; intros H.
generalize (Zlt_compare _ _ H).
destruct (Z.compare x y); try solve[inversion 1|auto].
intros _; inversion 1.
Qed.
Lemma Zpow_pos_div x y :
(y < x)%positive ->
(Z.pow_pos 2 x # 1) * / (Z.pow_pos 2 y # 1) == Z.pow_pos 2 (x - y) # 1.
Proof.
intros H; rewrite !Z.pow_pos_fold.
assert (Zpos (x - y) = (Zpos x - Zpos y)%Z) as ->.
{ apply Pos2Z.inj_sub; auto. }
rewrite Z.pow_sub_r; try omega.
rewrite <-Z.pow_sub_r.
{ unfold Qmult, Qinv; simpl.
assert (exists p, Z.pow_pos 2 y = Zpos p).
{ unfold Z.pow_pos.
rewrite Pos2Nat.inj_iter.
generalize (Pos.to_nat y); induction n.
{ simpl. exists 1%positive; auto. }
simpl in IHn|-*.
destruct IHn as [p H2]; rewrite H2; exists (p~0%positive); auto. }
destruct H0 as [p H1]; rewrite H1; simpl.
unfold Qeq; simpl; rewrite <-H1.
rewrite Z.pos_sub_gt; auto.
rewrite 2!Z.pow_pos_fold.
assert (2 ^ Z.pos (x - y) * 2 ^ Z.pos y = 2 ^ Z.pos x)%Z as ->.
{ assert (Zpos (x - y) = (Zpos x - Zpos y)%Z) as ->.
{ rewrite <-Z_pos_sub_gt.
{ rewrite <-Pos2Z.add_pos_neg.
unfold Z.sub; auto. }
rewrite Pos.gt_lt_iff; auto. }
assert (Hbounds : (0 <= Z.pos y <= Z.pos x)%Z).
{ split.
{ apply Pos2Z.is_nonneg. }
apply Zlt_le.
apply Pos_lt_Zpos_Zlt; auto. }
rewrite Z.pow_sub_r; auto; [|inversion 1].
rewrite <-Z.shiftr_div_pow2; [|apply Pos2Z.is_nonneg].
rewrite <-Z.shiftl_mul_pow2; [|apply Pos2Z.is_nonneg].
rewrite <-Z.shiftl_1_l.
rewrite Z.shiftr_shiftl_l; [|apply Pos2Z.is_nonneg].
rewrite Z.shiftl_shiftl.
{ rewrite Z.sub_simpl_r; auto. }
destruct Hbounds.
apply Zle_minus_le_0; auto. }
rewrite 2!Zmult_1_r; auto. }
{ inversion 1. }
split.
{ apply Pos2Z.is_nonneg. }
unfold Z.le, Z.compare; rewrite H; inversion 1.
split.
{ apply Pos2Z.is_nonneg. }
unfold Z.le, Z.compare; rewrite H; inversion 1.
Qed.
Lemma Qinv_neq (n : Q) : ~0 == n -> ~0 == / n.
Proof.
unfold Qeq, Qinv; simpl.
destruct (Qnum _); simpl; auto.
{ intros _ H.
generalize (Pos2Z.is_pos (Qden n * 1)).
rewrite <-H; inversion 1. }
intros _ H.
generalize (Zlt_neg_0 (Qden n * 1)).
rewrite <-H; inversion 1.
Qed.
Lemma Qdiv_neq_0 n m : ~n==0 -> ~m==0 -> ~(n / m == 0).
Proof.
intros H H1 H2.
unfold Qdiv in H2.
apply Qmult_integral in H2; destruct H2; auto.
assert (H2: ~0 == m).
{ intros H2; rewrite H2 in H1; apply H1; apply Qeq_refl. }
apply (Qinv_neq _ H2); rewrite H0; apply Qeq_refl.
Qed.
Lemma Qmake_neq_0 n m : (~n=0)%Z -> ~(n # m) == 0.
Proof.
intros H; unfold Qeq; simpl; intros H2.
rewrite Zmult_1_r in H2; subst n; apply H; auto.
Qed.
Lemma Zpow_pos_neq_0 n m : (n<>0 -> Z.pow_pos n m <> 0)%Z.
Proof.
intros H0.
unfold Z.pow_pos.
apply Pos.iter_invariant.
{ intros x H H2.
apply Zmult_integral in H2; destruct H2.
{ subst; apply H0; auto. }
subst x; apply H; auto. }
inversion 1.
Qed.
Lemma Zmult_pow_plus x y r :
(r <> 0)%Z ->
x * inject_Z (Z.pow r (Z.pos y)) / inject_Z (Z.pow r (Z.pos y+Z.pos y)) ==
x / inject_Z (Z.pow r (Z.pos y)).
Proof.
intros H; unfold inject_Z.
assert (Hy: (Z.pos y >= 0)%Z).
{ generalize (Pos2Z.is_nonneg y).
unfold Z.le, Z.ge; intros H2 H3.
destruct (Zle_compare 0 (Z.pos y)); auto. }
rewrite Zpower_exp; auto.
unfold Qdiv.
rewrite <-Qmult_assoc.
assert (r^(Z.pos y) * r^(Z.pos y) # 1 == (r^(Z.pos y)#1) * (r^(Z.pos y)#1)) as ->.
{ unfold Qmult; simpl; apply Qeq_refl. }
rewrite Qinv_mult_distr.
rewrite (Qmult_assoc (r^(Z.pos y)#1)).
rewrite Qmult_inv_r, Qmult_1_l.
{ apply Qeq_refl. }
apply Qmake_neq_0; intros H2.
apply (Zpow_pos_neq_0 _ _ H H2).
Qed.
Lemma DOadd_ok d1 d2 :
DO_to_Q (DOadd d1 d2) == DO_to_Q d1 + DO_to_Q d2.
Proof.
destruct d1, d2; simpl.
generalize den0 as X; intro.
generalize num0 as Y; intro.
generalize den1 as Z; intro.
generalize num1 as W; intro.
unfold Qplus; simpl.
rewrite !shift_pos_correct, Qmake_Qdiv, !Pos2Z.inj_mul, !shift_pos_correct.
rewrite !Zmult_1_r, !inject_Z_plus, !inject_Z_mult.
assert (inject_Z (Z.pow_pos 2 X) * inject_Z (Z.pow_pos 2 Z) =
inject_Z (Z.pow_pos 2 (X + Z))) as ->.
{ rewrite <-inject_Z_mult.
symmetry; rewrite !Zpower_pos_nat.
rewrite Pos2Nat.inj_add, Zpower_nat_is_exp; auto. }
destruct (Pos.ltb X Z) eqn:H.
{ rewrite (Qdiv_mult (1 / inject_Z (Z.pow_pos 2 X))).
assert (((inject_Z Y * inject_Z (Z.pow_pos 2 Z) +
inject_Z W * inject_Z (Z.pow_pos 2 X)) *
(1 / inject_Z (Z.pow_pos 2 X)) ==
inject_Z Y * inject_Z (Z.pow_pos 2 (Z - X)) + inject_Z W)) as ->.
{ unfold Qdiv; rewrite Qmult_1_l.
rewrite Qmult_plus_distr_l.
unfold inject_Z.
rewrite <-Qmult_assoc.
assert ((Z.pow_pos 2 Z # 1) * / (Z.pow_pos 2 X # 1) ==
Z.pow_pos 2 (Z - X) # 1) as ->.
{ rewrite Zpow_pos_div.
apply Qeq_refl.
rewrite <-Pos.ltb_lt; auto. }
apply Qplus_inj_l.
rewrite <-Qmult_assoc, Qmult_inv_r.
{ rewrite Qmult_1_r; apply Qeq_refl. }
rewrite Zpower_pos_nat, Zpower_nat_Z.
unfold Qeq; simpl; rewrite Zmult_1_r; apply Z.pow_nonzero.
{ omega. }
omega. }
assert (inject_Z (Z.pow_pos 2 (X + Z)) * (1 / inject_Z (Z.pow_pos 2 X)) ==
inject_Z (Z.pow_pos 2 Z)) as ->.
{ unfold Qdiv.
rewrite Qmult_assoc, Qmult_comm, Qmult_assoc.
rewrite (Qmult_comm (/_)).
assert (inject_Z (Z.pow_pos 2 (X + Z)) * / inject_Z (Z.pow_pos 2 X) ==
inject_Z (Z.pow_pos 2 Z)) as ->.
{ rewrite Zpower_pos_is_exp, inject_Z_mult.
rewrite (Qmult_comm (inject_Z (Z.pow_pos 2 X))).
rewrite <-Qmult_assoc, Qmult_inv_r.
{ rewrite Qmult_1_r; apply Qeq_refl. }
unfold inject_Z; rewrite Zpower_pos_nat, Zpower_nat_Z.
unfold Qeq; simpl; rewrite Zmult_1_r; apply Z.pow_nonzero.
{ omega. }
omega. }
rewrite Qmult_1_r; apply Qeq_refl. }
unfold DO_to_Q; simpl.
rewrite <-inject_Z_mult, <-inject_Z_plus.
assert (Z.pow_pos 2 Z = Z.pow_pos 2 Z * Z.pos 1)%Z as ->.
{ rewrite Zmult_1_r; auto. }
rewrite <-shift_pos_correct, <-Qmake_Qdiv.
rewrite Zmult_comm; apply Qeq_refl; auto.
apply Qdiv_neq_0. { apply Q_apart_0_1. }
unfold inject_Z; apply Qmake_neq_0.
apply Zpow_pos_neq_0. inversion 1. }
destruct (Pos.ltb Z X) eqn:H'.
{ rewrite (Qdiv_mult (1 / inject_Z (Z.pow_pos 2 Z))).
assert (((inject_Z Y * inject_Z (Z.pow_pos 2 Z) +
inject_Z W * inject_Z (Z.pow_pos 2 X)) *
(1 / inject_Z (Z.pow_pos 2 Z)) ==
inject_Z Y + inject_Z W * inject_Z (Z.pow_pos 2 (X - Z)))) as ->.
{ unfold Qdiv; rewrite Qmult_1_l.
rewrite Qmult_plus_distr_l.
unfold inject_Z.
rewrite <-(Qmult_assoc (W # 1)).
assert ((Z.pow_pos 2 X # 1) * / (Z.pow_pos 2 Z # 1) ==
Z.pow_pos 2 (X - Z) # 1) as ->.
{ rewrite Zpow_pos_div.
apply Qeq_refl.
rewrite <-Pos.ltb_lt; auto. }
apply Qplus_inj_r.
rewrite <-Qmult_assoc, Qmult_inv_r.
{ rewrite Qmult_1_r; apply Qeq_refl. }
rewrite Zpower_pos_nat, Zpower_nat_Z.
unfold Qeq; simpl; rewrite Zmult_1_r; apply Z.pow_nonzero.
{ omega. }
omega. }
assert (inject_Z (Z.pow_pos 2 (X + Z)) * (1 / inject_Z (Z.pow_pos 2 Z)) ==
inject_Z (Z.pow_pos 2 X)) as ->.
{ unfold Qdiv.
rewrite Qmult_assoc, Qmult_comm, Qmult_assoc.
rewrite (Qmult_comm (/_)).
assert (inject_Z (Z.pow_pos 2 (X + Z)) * / inject_Z (Z.pow_pos 2 Z) ==
inject_Z (Z.pow_pos 2 X)) as ->.
{ rewrite Zpower_pos_is_exp, inject_Z_mult.
rewrite <-Qmult_assoc, Qmult_inv_r.
{ rewrite Qmult_1_r; apply Qeq_refl. }
unfold inject_Z; rewrite Zpower_pos_nat, Zpower_nat_Z.
unfold Qeq; simpl; rewrite Zmult_1_r; apply Z.pow_nonzero.
{ omega. }
omega. }
rewrite Qmult_1_r; apply Qeq_refl. }
unfold DO_to_Q; simpl.
rewrite <-inject_Z_mult, <-inject_Z_plus.
assert (Z.pow_pos 2 X = Z.pow_pos 2 X * Z.pos 1)%Z as ->.
{ rewrite Zmult_1_r; auto. }
rewrite <-shift_pos_correct, <-Qmake_Qdiv.
rewrite Zmult_comm, Z.add_comm; apply Qeq_refl.
apply Qdiv_neq_0. { apply Q_apart_0_1. }
unfold inject_Z; apply Qmake_neq_0.
apply Zpow_pos_neq_0. inversion 1. }
assert (H1: X = Z).
{ generalize H'; rewrite Pos.ltb_antisym.
generalize H; unfold Pos.ltb, Pos.leb.
destruct (X ?= Z)%positive eqn:H2; try solve[inversion 1|inversion 2].
intros _ _.
apply Pos.compare_eq; auto. }
(* eq case *)
subst Z; unfold DO_to_Q; simpl; clear H H'.
unfold Qdiv; rewrite Qmult_plus_distr_l.
assert (inject_Z Y * inject_Z (Z.pow_pos 2 X) *
/ inject_Z (Z.pow_pos 2 (X + X)) ==
inject_Z Y / inject_Z (Z.pow_pos 2 X)) as ->.
{ apply Zmult_pow_plus; inversion 1. }
assert (inject_Z W * inject_Z (Z.pow_pos 2 X) *
/ inject_Z (Z.pow_pos 2 (X + X)) ==
inject_Z W / inject_Z (Z.pow_pos 2 X)) as ->.
{ apply Zmult_pow_plus; inversion 1. }
unfold Qdiv; rewrite <-Qmult_plus_distr_l, Qmake_Qdiv, inject_Z_plus.
unfold Qdiv; rewrite shift_pos_correct, Zmult_1_r; apply Qeq_refl.
Qed.
Definition DOmult (d1 d2 : DO) : DO :=
match d1, d2 with
| Dmake x1 y1, Dmake x2 y2 =>
Dmake (x1 * x2) (y1 + y2)
end.
Lemma shift_nat1_mult n m :
(shift_nat n 1 * shift_nat m 1 = shift_nat n (shift_nat m 1))%positive.
Proof.
induction n; simpl; auto.
rewrite IHn; auto.
Qed.
Lemma DOmult_ok d1 d2 :
DO_to_Q (DOmult d1 d2) = DO_to_Q d1 * DO_to_Q d2.
Proof.
destruct d1, d2; simpl.
generalize den0 as X; intro.
generalize num0 as Y; intro.
generalize den1 as Z; intro.
generalize num1 as W; intro.
unfold DO_to_Q; simpl.
unfold Qmult; simpl.
rewrite !shift_pos_nat, Pos2Nat.inj_add, shift_nat_plus.
rewrite shift_nat1_mult; auto.
Qed.
Definition DOopp (d : DO) : DO :=
match d with
| Dmake x y => Dmake (-x) y
end.
Lemma DOopp_ok d : DO_to_Q (DOopp d) = Qopp (DO_to_Q d).
Proof.
destruct d; simpl.
unfold DO_to_Q; simpl.
unfold Qopp; simpl; auto.
Qed.
Definition DOsub (d1 d2 : DO) : DO := DOadd d1 (DOopp d2).
Lemma DOsub_ok d1 d2 :
DO_to_Q (DOsub d1 d2) == DO_to_Q d1 - DO_to_Q d2.
Proof.
unfold DOsub.
rewrite DOadd_ok.
rewrite DOopp_ok.
unfold Qminus; apply Qeq_refl.
Qed.
Definition DOle (d1 d2 : DO) : Prop :=
Qle (DO_to_Q d1) (DO_to_Q d2).
(*TODO: There's probably a more efficient way to implement the following:*)
Definition DOle_bool (d1 d2 : DO) : bool :=
Qle_bool (DO_to_Q d1) (DO_to_Q d2).
Lemma DOle_bool_iff d1 d2 : (DOle_bool d1 d2 = true) <-> DOle d1 d2.
Proof.
unfold DOle_bool, DOle.
apply Qle_bool_iff.
Qed.
Definition DOlt (d1 d2 : DO) : Prop :=
Qlt (DO_to_Q d1) (DO_to_Q d2).
Definition DOlt_bool (d1 d2 : DO) : bool :=
match DO_to_Q d1 ?= DO_to_Q d2 with
| Lt => true
| _ => false
end.
Lemma DOlt_bool_iff d1 d2 : (DOlt_bool d1 d2 = true) <-> DOlt d1 d2.
Proof.
unfold DOlt_bool; split.
destruct (Qcompare_spec (DO_to_Q d1) (DO_to_Q d2));
try solve[inversion 1|auto].
unfold DOlt; rewrite Qlt_alt; intros ->; auto.
Qed.
Lemma DOeq_dec (d1 d2 : DO) : {d1=d2} + {d1<>d2}.
Proof.
destruct d1, d2.
destruct (Z.eq_dec num0 num1).
{ destruct (positive_eq_dec den0 den1).
left; subst; f_equal.
right; inversion 1; subst; apply n; auto. }
right; inversion 1; subst; auto.
Qed.
(*(* MICROBENCHMARK *)
Fixpoint f (n : nat) (d : D) : D :=
match n with
| O => d
| S n' => DOadd d (f n' d)
end.
Time Compute f 5000 (Dmake 3 2).
(*Finished transaction in 0.012 secs (0.012u,0.s) (successful)*)
Fixpoint g (n : nat) (q : Q) : Q :=
match n with
| O => q
| S n' => Qplus q (g n' q)
end.
Time Compute g 5000 (Qmake 3 2).
(*Finished transaction in 0.847 secs (0.848u,0.s) (successful)*)
(*Speedup on this microbenchmark: 70x*)*)
Delimit Scope DO_scope with DO.
Bind Scope DO_scope with DO.
Arguments Dmake _%Z _%positive.
Infix "<" := DOlt : DO_scope.
Infix "<=" := DOle : DO_scope.
Notation "x > y" := (DOlt y x)(only parsing) : DO_scope.
Notation "x >= y" := (DOle y x)(only parsing) : DO_scope.
Notation "x <= y <= z" := (x<=y/\y<=z) : DO_scope.
Infix "+" := DOadd : DO_scope.
Notation "- x" := (DOopp x) : DO_scope.
Infix "-" := DOsub : DO_scope.
Infix "*" := DOmult : DO_scope.
Notation "'0'" := DO0 : DO_scope.
Notation "'1'" := DO1 : DO_scope.
(** DOmax *)
Definition DOmax (d1 d2 : DO) : DO :=
if DOlt_bool d1 d2 then d2 else d1.
(** The smallest power of 2 greater than a given rational *)
Definition Zsize (z : Z) : positive :=
match z with
| Z0 => 1
| Zpos p => Pos.size p
| Zneg p => Pos.size p
end.
Definition Plub_aux (x : Z) (y : positive) : positive :=
Zsize x - y.
Definition DOlub (max : DO) : DO :=
match max with
| Dmake x y => Dmake 1 (Plub_aux x y)
end.
Lemma Zpos_2_mult (x : Z) (y : positive) :
(x <= Z.pos y)%Z -> (x * 2 <= Z.pos y~0)%Z.
Proof.
intros H.
rewrite Zmult_comm.
rewrite (Pos2Z.inj_xO y).
apply Zmult_le_compat_l; auto.
omega.
Qed.
Lemma two_power_pos_le x y :
(x <= y)%positive -> (two_power_pos x <= two_power_pos y)%Z.
Proof.
intros H.
rewrite !two_power_pos_nat.
rewrite Pos2Nat.inj_le in H.
unfold two_power_nat, shift_nat.
revert H.
generalize (Pos.to_nat x) as x'; intro.
generalize (Pos.to_nat y) as y'; intro.
revert y'.
induction x'; simpl.
{ intros y' _; induction y'; simpl; try solve[intros; omega].
rewrite Pos2Z.inj_xO.
assert ((1=1*1)%Z) as -> by (rewrite Zmult_1_r; auto).
apply Zmult_le_compat; try omega. }
induction y'; try solve[intros; omega].
simpl; intros H.
rewrite Pos2Z.inj_xO.
rewrite
(Pos2Z.inj_xO
(nat_rect (fun _ : nat => positive) 1%positive
(fun _ : nat => xO) y')).
apply Zmult_le_compat; try omega.
{ apply IHx'; omega. }
clear - x'.
induction x'; try (simpl; omega).
simpl; rewrite Pos2Z.inj_xO.
assert ((0=0*0)%Z) as -> by (rewrite Zmult_0_r; auto).
apply Zmult_le_compat; try omega.
Qed.
Lemma Zpow_pos_size_le x : (x <= Z.pow_pos 2 (Zsize x))%Z.
Proof.
destruct x; simpl.
{ rewrite <-two_power_pos_correct.
unfold two_power_pos; rewrite shift_pos_equiv; simpl; omega. }
{ generalize (Pos.lt_le_incl _ _ (Pos.size_gt p)).
rewrite <-Pos2Z.inj_pow_pos; auto. }
rewrite <-Pos2Z.inj_pow_pos.
apply Zle_neg_pos.
Qed.
Lemma Pos_succ_sub_1 p : (Pos.succ p - 1 = p)%positive.
Proof.
set (P := fun p => (Pos.succ p - 1)%positive = p).
change (P p); apply Pos.peano_ind; try reflexivity.
intros r; unfold P; intros <-.
rewrite <-Pos2Nat.inj_iff.
rewrite nat_of_P_minus_morphism.
{ rewrite !Pos2Nat.inj_succ; auto. }
apply nat_of_P_gt_Gt_compare_complement_morphism.
rewrite !Pos2Nat.inj_succ.
rewrite Pos2Nat.inj_1.
omega.
Qed.
Lemma Pos_le_1_add_sub x : (x <= 1 + (x - 1))%positive.
Proof.
set (P := fun x => (x <= 1 + (x - 1))%positive).
change (P x).
apply Pos.peano_ind.
{ unfold P; simpl. apply Pos.le_1_l. }
intros p; unfold P; intros H.
rewrite Pos_succ_sub_1.
rewrite <-Pos.add_1_l.
apply Pos.le_refl.
Qed.
Lemma Pos_succ_lt_2_false p : (Pos.succ p < 2)%positive -> False.
Proof.
rewrite Pos2Nat.inj_lt.
rewrite Pos2Nat.inj_succ.
unfold Pos.to_nat; simpl.
intros H.
assert (H2: (2 < 2)%nat).
{ apply Nat.le_lt_trans with (m := S (Pos.iter_op Init.Nat.add p 1%nat)); auto.
assert (H3: (1 <= Pos.iter_op Init.Nat.add p 1)%nat) by apply le_Pmult_nat.
apply Peano.le_n_S; auto. }
omega.
Qed.
Lemma Pos2Nat_inj_2 : Pos.to_nat 2 = 2%nat.
Proof. unfold Pos.to_nat; simpl; auto. Qed.
Lemma Pos_le_2_add_sub x :
(1 + (x - 1) <= 2 + (x - 2))%positive.
Proof.
rewrite Pos2Nat.inj_le.
rewrite !Pos2Nat.inj_add.
assert (Pos.to_nat 1 = 1%nat) as -> by auto.
assert (Pos.to_nat 2 = 2%nat) as -> by auto.
destruct (Pos.ltb_spec x 1).
{ elimtype False.
apply (Pos.nlt_1_r _ H). }
destruct (Pos.eqb_spec x 1).
{ subst x.
simpl.
rewrite Pos.sub_le; auto. }
assert (H2: Pos.compare_cont Eq x 1 = Gt).
{ rewrite Pos.compare_cont_spec.
rewrite Pos.compare_antisym.
rewrite <-Pos.leb_le in H.
rewrite Pos.leb_compare in H.
generalize H; clear H.
destruct (Pos.compare 1 x) eqn:H; simpl; auto.
{ rewrite Pos.compare_eq_iff in H; subst x; elimtype False; auto. }
inversion 1. }
rewrite nat_of_P_minus_morphism; auto.
destruct (Pos.ltb_spec x 2).
{ (*x=1*)
elimtype False; apply n.
rewrite Pos.le_lteq in H.
destruct H; auto.
rewrite Pos2Nat.inj_lt in H, H0.
rewrite <-Pos2Nat.inj_iff.
clear - H H0.
rewrite Pos2Nat.inj_1 in H|-*.
rewrite Pos2Nat_inj_2 in H0.
omega. }
destruct (Pos.eqb_spec x 2).
{ (*x=2*)
subst x.
simpl.
omega. }
assert (H3: Pos.compare_cont Eq x 2 = Gt).
{ apply nat_of_P_gt_Gt_compare_complement_morphism.
rewrite Pos2Nat.inj_le in H, H0.
rewrite Pos2Nat.inj_1 in H.
rewrite Pos2Nat_inj_2 in H0|-*.
assert (H1: Pos.to_nat x <> 2%nat).
{ intros Hx.
rewrite <-Pos2Nat.inj_iff, Hx in n0.
auto. }
omega. }
rewrite nat_of_P_minus_morphism; auto.
simpl.
assert (Pos.to_nat 1 = 1%nat) as -> by auto.
assert (Pos.to_nat 2 = 2%nat) as -> by auto.
apply Peano.le_n_S.
generalize (Pos.to_nat x) as m; intro.
induction m; try solve[omega].
Qed.
Lemma Psize_minus_aux (x y : positive) : (x <= Pos.div2 (2^y) + (x - y))%positive.
Proof.
revert y.
apply Pos.peano_ind.
{ unfold Pos.pow, Pos.mul, Pos.iter, Pos.div2.
apply Pos_le_1_add_sub. }
intros p H.
rewrite Pos.pow_succ_r; simpl.
eapply Pos.le_trans; [apply H|].
clear H.
set (P := fun p =>
forall x, (Pos.div2 (2 ^ p) + (x - p) <= 2 ^ p + (x - Pos.succ p))%positive).
revert x.
change (P p).
apply Pos.peano_ind.
{ unfold P.
intros x.
unfold Pos.pow, Pos.mul, Pos.iter, Pos.div2.
apply Pos_le_2_add_sub. }
intros r; unfold P; simpl; intros IH x.
rewrite Pos.pow_succ_r.
unfold Pos.div2, Pos.mul.
generalize (2^r)%positive as y; intro.
generalize (Pos.succ r) as z; intro.
assert (H: (x - z <= Pos.succ (x - Pos.succ z))%positive).
{ rewrite Pos.sub_succ_r.
destruct (Pos.eqb_spec (x-z) 1).
{ rewrite e; simpl.
rewrite Pos2Nat.inj_le, Pos2Nat.inj_1, Pos2Nat_inj_2; omega. }
rewrite Pos.succ_pred; auto.
apply Pos.le_refl. }
generalize H.
generalize (x - Pos.succ z)%positive as q; intro.
clear IH H; intros H.
set (Q := fun y => (y + (x - z) <= y~0 + q)%positive).
change (Q y).
apply Pos.peano_ind.
{ unfold Q.
assert (2 + q = 1 + Pos.succ q)%positive as ->.
{ rewrite <-Pos.add_1_l, Pos.add_assoc; auto. }
apply Pos.add_le_mono_l; auto. }
intros t; unfold Q; intros IH.
rewrite Pplus_one_succ_l.
rewrite <-Pos.add_assoc.
rewrite Pos.add_xO.
rewrite <-Pos.add_assoc.
apply Pos.add_le_mono; auto.
apply Pos.le_1_l.
Qed.
Lemma Psize_exp_div y : (Pos.div2 (2 ^ (Pos.size y)) <= y)%positive.
Proof.
generalize (Pos.size_le y).
destruct (2 ^ Pos.size y)%positive; simpl.
{ unfold Pos.le, Pos.compare; simpl.
intros H H2.
apply nat_of_P_gt_Gt_compare_morphism in H2.
apply H.
rewrite Pos.compare_cont_Gt_Gt.
rewrite Pos2Nat.inj_ge; omega. }
{ unfold Pos.le, Pos.compare; simpl.
intros H H2.
apply H; auto. }
intros _; apply Pos.le_1_l.
Qed.
Local Open Scope DO_scope.
Lemma DOlub_mult_le1 d : d * DOlub d <= 1.
Proof.
unfold DOle; rewrite DOmult_ok.
unfold DO_to_Q, Qle; destruct d as [x y]; simpl.
rewrite Zmult_1_r; apply Zpos_2_mult.
rewrite Pos2Z.inj_mul, !shift_pos_correct, !Zmult_1_r.
rewrite <-Zpower_pos_is_exp.
unfold Plub_aux.
assert (H : (x <= Z.pow_pos 2 (Zsize x))%Z).
{ apply Zpow_pos_size_le. }
eapply Z.le_trans; [apply H|].
rewrite <-!two_power_pos_correct.
apply two_power_pos_le.
rewrite Pos2Nat.inj_le; generalize (Zsize x) as z; intro.
clear H.
rewrite Pos2Nat.inj_add.
destruct (Pos.ltb_spec y z) as [H|H].
{ rewrite Pos2Nat.inj_sub; auto.
omega. }
assert ((z - y = 1)%positive) as ->.
{ apply Pos.sub_le; auto. }
revert H; rewrite Pos2Nat.inj_le.
rewrite Pos2Nat.inj_1.
omega.
Qed.
Lemma DOlub_nonneg (d : DO) :
0 <= d -> 0 <= DOlub d.
Proof.
destruct d; simpl; intros H.
unfold DOle; rewrite DO_to_Q0; unfold DO_to_Q; simpl.
unfold Qle; simpl; omega.
Qed.
Lemma DOlub_ok (d : DO) :
0 <= d ->
DOle 0 (d * DOlub d) /\ DOle (d * DOlub d) 1.
Proof.
intros H.
split.
{ unfold DOle; rewrite DOmult_ok.
rewrite DO_to_Q0; apply Qmult_le_0_compat.
{ rewrite <-DO_to_Q0; auto. }
rewrite <-DO_to_Q0; apply DOlub_nonneg; auto. }
apply DOlub_mult_le1.
Qed.
Fixpoint DOred' (n : Z) (d : nat) : (Z * nat) :=
match d with
| O => (n,d)
| S d' => if Zeven_dec n then DOred' (Z.div2 n) d'
else (n,d)
end.
Lemma DOred'P n d : Zodd (fst (DOred' n d)) \/ (snd (DOred' n d) = 0%nat).
Proof.
revert n; induction d; auto.
intros n; simpl; destruct (Zeven_dec n).
{ apply (IHd (Z.div2 n)). }
left; simpl.
destruct (Zodd_dec n); auto.
destruct (Zeven_odd_dec n); auto.
elimtype False; apply n0; auto.
Qed.
Definition DO_of_DOred' (p : Z*nat) : DO :=
let (x,y) := p in Dmake x (Pos.of_nat (S y)).
Definition DOred (d : DO) : DO :=
DO_of_DOred' (DOred' (num d) (pred (Pos.to_nat (den d)))).
Lemma DOredP d : Zodd (num (DOred d)) \/ (den (DOred d) = 1%positive).
Proof.
unfold DOred; destruct d as [x y]; simpl.
destruct (DOred'P x (pred (Pos.to_nat y))).
{ unfold DO_of_DOred'.
destruct (DOred' _ _); auto. }
destruct (DOred' _ _); right; simpl in *.
rewrite H; auto.
Qed.
Lemma DO_of_DOred'_correct x y :
DO_to_Q (DO_of_DOred' (DOred' x y)) == DO_to_Q (DO_of_DOred' (x,y)).
Proof.
revert x; induction y.
{ intros x; apply Qeq_refl. }
intros x.
unfold DOred'; fold DOred'.
destruct (Zeven_dec x) as [pf|pf].
{ rewrite IHy.
unfold DO_to_Q; simpl.
unfold Qeq; simpl.
pattern x at 2.
rewrite (Zeven_div2 x pf).
rewrite 2!shift_pos_correct, 2!Zmult_1_r.
rewrite 2!Zpower_pos_nat.
rewrite Pos2Nat.inj_succ.
rewrite Zpower_nat_succ_r.
rewrite Zmult_assoc.
pattern (Z.div2 x * 2)%Z; rewrite Zmult_comm; auto. }
apply Qeq_refl.
Qed.
Lemma DOred_correct d : DO_to_Q (DOred d) == DO_to_Q d.
Proof.
unfold DOred.
destruct d as [x y] eqn:Heq.
simpl.
rewrite DO_of_DOred'_correct.
unfold DO_of_DOred'.
rewrite <-Pos.of_nat_succ.
generalize (Pos2Nat.is_pos y).
destruct (Pos.to_nat y) eqn:Heq'; try omega.
intros _; simpl.
rewrite (SuccNat2Pos.inv n y); auto.
apply Qeq_refl.
Qed.
Lemma gcd_2_odd_1 x : Zodd x -> Z.gcd x 2 = 1%Z.
Proof.
generalize (Z.gcd_divide_r x 2).
intros H.
generalize (Znumtheory.Zdivide_bounds _ _ H).
generalize (Z.gcd_nonneg x 2); intros H2 H3 H4.
assert (H5: (Z.abs (Z.gcd x 2) <= Z.abs 2)%Z).
{ apply H3; inversion 1. }
destruct (Z.abs_eq_iff (Z.gcd x 2)) as [_ Y].
rewrite (Y H2) in H5. clear Y.
simpl in H5.
clear - H2 H4 H5.
assert (H6: (Z.gcd x 2 = 0 \/ Z.gcd x 2 = 1 \/ Z.gcd x 2 = 2)%Z).
{ omega. }
clear H2 H5.
destruct H6.
{ apply Z.gcd_eq_0_l in H; subst x.
inversion H4. }
destruct H; auto.
generalize (Z.gcd_divide_l x 2); rewrite H.
intros H2; apply Znumtheory.Zdivide_mod in H2.
rewrite Zmod_odd in H2.
rewrite <-Zodd_bool_iff in H4; rewrite H4 in H2; inversion H2.
Qed.
Lemma gcd_2_even_2 x : Zeven x -> Z.gcd x 2 = 2%Z.
Proof.
generalize (Z.gcd_divide_r x 2).
intros H.
generalize (Znumtheory.Zdivide_bounds _ _ H).
generalize (Z.gcd_nonneg x 2); intros H2 H3 H4.
assert (H5: (Z.abs (Z.gcd x 2) <= Z.abs 2)%Z).
{ apply H3; inversion 1. }
destruct (Z.abs_eq_iff (Z.gcd x 2)) as [_ Y].
rewrite (Y H2) in H5. clear Y.
simpl in H5.
clear - H2 H4 H5.
assert (H6: (Z.gcd x 2 = 0 \/ Z.gcd x 2 = 1 \/ Z.gcd x 2 = 2)%Z).
{ omega. }
clear H2 H5.
destruct H6.
{ apply Z.gcd_eq_0_l in H; subst x.
auto. }
destruct H; auto.
elimtype False.
rewrite Znumtheory.Zgcd_1_rel_prime in H.
destruct H.
assert (H2: (2 | x)%Z).
{ apply Znumtheory.Zmod_divide.
{ inversion 1. }
rewrite Zmod_odd.
rewrite Zodd_even_bool.
rewrite <-Zeven_bool_iff in H4; rewrite H4.
auto. }
assert (H3: (2 | 2)%Z).
{ exists 1%Z; auto. }
assert (Hcontra: (2 | 1)%Z).
{ apply H1; auto. }
assert (2 <= 1)%Z.
{ apply Z.divide_pos_le; auto.
omega. }
omega.
Qed.
Lemma gcd_x_times2_1 x y : Zodd x -> Z.gcd x y = 1%Z -> Z.gcd x (2*y) = 1%Z.
Proof.
intros Hodd H.
generalize (Znumtheory.Zgcd_is_gcd x y) as H2; intro.
apply Znumtheory.Zis_gcd_gcd; try omega.
inversion H2.
constructor; try apply Z.divide_1_l.
intros w H4 H5.
rewrite H in H3.
apply Znumtheory.Gauss in H5; auto.
rewrite <-Znumtheory.Zgcd_1_rel_prime.
destruct (Zeven_odd_dec w).
{ rewrite Zeven_ex_iff in z.
destruct z as [m H6].
rewrite H6 in H4.
clear - Hodd H4.
elimtype False.
destruct H4 as [y H4].
rewrite Zmult_assoc in H4.
rewrite (Zmult_comm y) in H4.
rewrite <-Zmult_assoc in H4.
assert (H5: Zeven x).
{ rewrite H4.
apply Zeven_2p. }
apply Zodd_not_Zeven in Hodd; auto. }
apply gcd_2_odd_1; auto.
Qed.
Lemma gcd_pow2_odd_1 x n : Zodd x -> Z.gcd x (Zpower_nat 2 n) = 1%Z.
Proof.
induction n.
{ simpl.
rewrite Z.gcd_1_r; auto. }
rewrite Zpower_nat_succ_r.
intros Hodd.
generalize (IHn Hodd).
intros H.
apply gcd_x_times2_1; auto.
Qed.
Lemma Qred_odd_pow2 x n : Zodd x -> Qred (x # pow_pos 2 n) = x # (pow_pos 2 n).
Proof.
unfold Qred.
generalize (Z.ggcd_gcd x (Z.pos (pow_pos 2 n))).
generalize (Z.ggcd_correct_divisors x (Z.pos (pow_pos 2 n))).
destruct (Z.ggcd x (Z.pos (pow_pos 2 n))) as [a [b c]]; simpl.
intros [H0 H] H2 H3.
subst a.
assert (H2: Z.gcd x (Z.pos (pow_pos 2 n)) = 1%Z).
{ rewrite pow_pos_Zpow_pos, Zpower_pos_nat.
apply gcd_pow2_odd_1; auto. }
rewrite H2, Zmult_1_l in H.
subst c.
rewrite H2, Zmult_1_l in H0.
subst b.
auto.
Qed.
Lemma Qred_odd_2 x : Zodd x -> Qred (x # 2) = x # 2.
Proof.
unfold Qred.
generalize (Z.ggcd_gcd x 2).
generalize (Z.ggcd_correct_divisors x 2).
destruct (Z.ggcd x 2) as [a [b c]]; simpl.
intros [H0 H] H2 H3.
subst a.
assert (H2: Z.gcd x 2 = 1%Z).
{ apply gcd_2_odd_1; auto. }