-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArith.v
82 lines (73 loc) · 1.82 KB
/
Arith.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
From FloatCohorts Require Import Tactics.
Require Export ZArith PArith Lia Floats.SpecFloat.
Open Scope Z.
Definition div2_opt (p : positive) : option positive :=
match p with
| xO p' => Some p'
| _ => None
end.
Lemma div2_opt_correct (p d : positive) :
div2_opt p = Some d <-> p = (d * 2)%positive.
Proof.
split; intros.
- destruct p; inversion H.
cbn.
rewrite Pos.mul_comm.
reflexivity.
-
rewrite H.
cbn.
rewrite Pos.mul_comm.
reflexivity.
Qed.
Lemma Zpow_divide (b p1 p2 : Z) :
0 < b ->
0 <= p1 <= p2 ->
(b ^ p1 | b ^ p2).
Proof.
intros B P.
rewrite <-Z.mod_divide by (apply Z.pow_nonzero; lia).
replace p2 with ((p2 - p1) + p1) by lia.
rewrite Z.pow_add_r by lia.
apply Z_mod_mult.
Qed.
Lemma digits2_pos_redundant (p : positive) :
digits2_pos p = Pos.size p.
Proof. reflexivity. Qed.
Lemma pos_size_Z_log2 (p : positive) :
Z.pos (Pos.size p) = Z.log2 (Z.pos p) + 1.
Proof.
destruct p; cbn; lia.
Qed.
Lemma pos_size_mul_pow_two (p q : positive) :
Pos.size (p * 2 ^ q) = (Pos.size p + q)%positive.
Proof.
enough (Z.pos (Pos.size (p * 2 ^ q)) = (Z.pos (Pos.size p)) + (Z.pos q)) by lia.
repeat rewrite pos_size_Z_log2.
rewrite Pos2Z.inj_mul, Pos2Z.inj_pow.
remember (Z.pos p) as zp.
remember (Z.pos q) as zq.
rewrite Z.log2_mul_pow2; lia.
Qed.
Lemma pos_size_monotone (p1 p2 : positive) :
(p1 <= p2)%positive ->
(Pos.size p1 <= Pos.size p2)%positive.
Proof.
generalize dependent p2.
induction p1; cbn in *; [ | | lia].
all: intros.
all: destruct p2; cbn in *; [ | | lia].
all: assert (p1 <= p2)%positive by lia.
all: apply IHp1 in H0.
all: lia.
Qed.
Lemma pos_size_monotone_inv (p1 p2 : positive) :
(Pos.size p1 < Pos.size p2)%positive ->
(p1 < p2)%positive.
Proof.
intros.
apply Pos.lt_nle.
intros C.
apply pos_size_monotone in C.
lia.
Qed.