author | haftmann |
Sun, 21 Aug 2022 06:18:23 +0000 | |
changeset 75936 | d2e6a1342c90 |
parent 75883 | d7e0b6620c07 |
child 75937 | 02b18f59f903 |
permissions | -rw-r--r-- |
3366 | 1 |
(* Title: HOL/Divides.thy |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
6865
5577ffe4c2f1
now div and mod are overloaded; dvd is polymorphic
paulson
parents:
3366
diff
changeset
|
3 |
Copyright 1999 University of Cambridge |
18154 | 4 |
*) |
3366 | 5 |
|
64785 | 6 |
section \<open>More on quotient and remainder\<close> |
3366 | 7 |
|
15131 | 8 |
theory Divides |
66817 | 9 |
imports Parity |
15131 | 10 |
begin |
3366 | 11 |
|
66817 | 12 |
subsection \<open>More on division\<close> |
60758 | 13 |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
14 |
subsubsection \<open>Monotonicity in the First Argument (Dividend)\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
15 |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
16 |
lemma unique_quotient_lemma: |
68626 | 17 |
assumes "b * q' + r' \<le> b * q + r" "0 \<le> r'" "r' < b" "r < b" shows "q' \<le> (q::int)" |
18 |
proof - |
|
19 |
have "r' + b * (q'-q) \<le> r" |
|
20 |
using assms by (simp add: right_diff_distrib) |
|
21 |
moreover have "0 < b * (1 + q - q') " |
|
22 |
using assms by (simp add: right_diff_distrib distrib_left) |
|
23 |
moreover have "b * q' < b * (1 + q)" |
|
24 |
using assms by (simp add: right_diff_distrib distrib_left) |
|
25 |
ultimately show ?thesis |
|
26 |
using assms by (simp add: mult_less_cancel_left) |
|
27 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
28 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
29 |
lemma unique_quotient_lemma_neg: |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
30 |
"b * q' + r' \<le> b*q + r \<Longrightarrow> r \<le> 0 \<Longrightarrow> b < r \<Longrightarrow> b < r' \<Longrightarrow> q \<le> (q'::int)" |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
31 |
using unique_quotient_lemma[where b = "-b" and r = "-r'" and r'="-r"] by auto |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
32 |
|
68631 | 33 |
lemma zdiv_mono1: |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
34 |
\<open>a div b \<le> a' div b\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
35 |
if \<open>a \<le> a'\<close> \<open>0 < b\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
36 |
for a b b' :: int |
68631 | 37 |
proof (rule unique_quotient_lemma) |
38 |
show "b * (a div b) + a mod b \<le> b * (a' div b) + a' mod b" |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
39 |
using \<open>a \<le> a'\<close> by auto |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
40 |
qed (use that in auto) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
41 |
|
68631 | 42 |
lemma zdiv_mono1_neg: |
43 |
fixes b::int |
|
44 |
assumes "a \<le> a'" "b < 0" shows "a' div b \<le> a div b" |
|
45 |
proof (rule unique_quotient_lemma_neg) |
|
46 |
show "b * (a div b) + a mod b \<le> b * (a' div b) + a' mod b" |
|
47 |
using assms(1) by auto |
|
48 |
qed (use assms in auto) |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
49 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
50 |
|
60758 | 51 |
subsubsection \<open>Monotonicity in the Second Argument (Divisor)\<close> |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
52 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
53 |
lemma q_pos_lemma: |
68631 | 54 |
fixes q'::int |
55 |
assumes "0 \<le> b'*q' + r'" "r' < b'" "0 < b'" |
|
56 |
shows "0 \<le> q'" |
|
57 |
proof - |
|
58 |
have "0 < b'* (q' + 1)" |
|
59 |
using assms by (simp add: distrib_left) |
|
60 |
with assms show ?thesis |
|
61 |
by (simp add: zero_less_mult_iff) |
|
62 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
63 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
64 |
lemma zdiv_mono2_lemma: |
68631 | 65 |
fixes q'::int |
66 |
assumes eq: "b*q + r = b'*q' + r'" and le: "0 \<le> b'*q' + r'" and "r' < b'" "0 \<le> r" "0 < b'" "b' \<le> b" |
|
67 |
shows "q \<le> q'" |
|
68 |
proof - |
|
69 |
have "0 \<le> q'" |
|
70 |
using q_pos_lemma le \<open>r' < b'\<close> \<open>0 < b'\<close> by blast |
|
71 |
moreover have "b*q = r' - r + b'*q'" |
|
72 |
using eq by linarith |
|
73 |
ultimately have "b*q < b* (q' + 1)" |
|
74 |
using mult_right_mono assms unfolding distrib_left by fastforce |
|
75 |
with assms show ?thesis |
|
76 |
by (simp add: mult_less_cancel_left_pos) |
|
77 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
78 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
79 |
lemma zdiv_mono2: |
68631 | 80 |
fixes a::int |
81 |
assumes "0 \<le> a" "0 < b'" "b' \<le> b" shows "a div b \<le> a div b'" |
|
82 |
proof (rule zdiv_mono2_lemma) |
|
83 |
have "b \<noteq> 0" |
|
84 |
using assms by linarith |
|
85 |
show "b * (a div b) + a mod b = b' * (a div b') + a mod b'" |
|
86 |
by simp |
|
87 |
qed (use assms in auto) |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
88 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
89 |
lemma zdiv_mono2_neg_lemma: |
68631 | 90 |
fixes q'::int |
91 |
assumes "b*q + r = b'*q' + r'" "b'*q' + r' < 0" "r < b" "0 \<le> r'" "0 < b'" "b' \<le> b" |
|
92 |
shows "q' \<le> q" |
|
93 |
proof - |
|
94 |
have "b'*q' < 0" |
|
95 |
using assms by linarith |
|
96 |
with assms have "q' \<le> 0" |
|
97 |
by (simp add: mult_less_0_iff) |
|
98 |
have "b*q' \<le> b'*q'" |
|
99 |
by (simp add: \<open>q' \<le> 0\<close> assms(6) mult_right_mono_neg) |
|
100 |
then have "b*q' < b* (q + 1)" |
|
101 |
using assms by (simp add: distrib_left) |
|
102 |
then show ?thesis |
|
103 |
using assms by (simp add: mult_less_cancel_left) |
|
104 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
105 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
106 |
lemma zdiv_mono2_neg: |
68631 | 107 |
fixes a::int |
108 |
assumes "a < 0" "0 < b'" "b' \<le> b" shows "a div b' \<le> a div b" |
|
109 |
proof (rule zdiv_mono2_neg_lemma) |
|
110 |
have "b \<noteq> 0" |
|
111 |
using assms by linarith |
|
112 |
show "b * (a div b) + a mod b = b' * (a div b') + a mod b'" |
|
113 |
by simp |
|
114 |
qed (use assms in auto) |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
115 |
|
75881 | 116 |
|
117 |
subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close> |
|
118 |
||
119 |
inductive eucl_rel_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" |
|
120 |
where eucl_rel_int_by0: "eucl_rel_int k 0 (0, k)" |
|
121 |
| eucl_rel_int_dividesI: "l \<noteq> 0 \<Longrightarrow> k = q * l \<Longrightarrow> eucl_rel_int k l (q, 0)" |
|
122 |
| eucl_rel_int_remainderI: "sgn r = sgn l \<Longrightarrow> \<bar>r\<bar> < \<bar>l\<bar> |
|
123 |
\<Longrightarrow> k = q * l + r \<Longrightarrow> eucl_rel_int k l (q, r)" |
|
124 |
||
125 |
lemma eucl_rel_int_iff: |
|
126 |
"eucl_rel_int k l (q, r) \<longleftrightarrow> |
|
127 |
k = l * q + r \<and> |
|
128 |
(if 0 < l then 0 \<le> r \<and> r < l else if l < 0 then l < r \<and> r \<le> 0 else q = 0)" |
|
129 |
by (cases "r = 0") |
|
130 |
(auto elim!: eucl_rel_int.cases intro: eucl_rel_int_by0 eucl_rel_int_dividesI eucl_rel_int_remainderI |
|
131 |
simp add: ac_simps sgn_1_pos sgn_1_neg) |
|
132 |
||
133 |
lemma unique_quotient: |
|
134 |
"eucl_rel_int a b (q, r) \<Longrightarrow> eucl_rel_int a b (q', r') \<Longrightarrow> q = q'" |
|
135 |
apply (rule order_antisym) |
|
136 |
apply (simp_all add: eucl_rel_int_iff linorder_neq_iff split: if_split_asm) |
|
137 |
apply (blast intro: order_eq_refl [THEN unique_quotient_lemma] order_eq_refl [THEN unique_quotient_lemma_neg] sym)+ |
|
138 |
done |
|
139 |
||
140 |
lemma unique_remainder: |
|
141 |
assumes "eucl_rel_int a b (q, r)" |
|
142 |
and "eucl_rel_int a b (q', r')" |
|
143 |
shows "r = r'" |
|
144 |
proof - |
|
145 |
have "q = q'" |
|
146 |
using assms by (blast intro: unique_quotient) |
|
147 |
then show "r = r'" |
|
148 |
using assms by (simp add: eucl_rel_int_iff) |
|
149 |
qed |
|
150 |
||
151 |
lemma eucl_rel_int: |
|
152 |
"eucl_rel_int k l (k div l, k mod l)" |
|
153 |
proof (cases k rule: int_cases3) |
|
154 |
case zero |
|
155 |
then show ?thesis |
|
156 |
by (simp add: eucl_rel_int_iff divide_int_def modulo_int_def) |
|
157 |
next |
|
158 |
case (pos n) |
|
159 |
then show ?thesis |
|
160 |
using div_mult_mod_eq [of n] |
|
161 |
by (cases l rule: int_cases3) |
|
162 |
(auto simp del: of_nat_mult of_nat_add |
|
163 |
simp add: mod_greater_zero_iff_not_dvd of_nat_mult [symmetric] of_nat_add [symmetric] algebra_simps |
|
164 |
eucl_rel_int_iff divide_int_def modulo_int_def) |
|
165 |
next |
|
166 |
case (neg n) |
|
167 |
then show ?thesis |
|
168 |
using div_mult_mod_eq [of n] |
|
169 |
by (cases l rule: int_cases3) |
|
170 |
(auto simp del: of_nat_mult of_nat_add |
|
171 |
simp add: mod_greater_zero_iff_not_dvd of_nat_mult [symmetric] of_nat_add [symmetric] algebra_simps |
|
172 |
eucl_rel_int_iff divide_int_def modulo_int_def) |
|
173 |
qed |
|
174 |
||
175 |
lemma divmod_int_unique: |
|
176 |
assumes "eucl_rel_int k l (q, r)" |
|
177 |
shows div_int_unique: "k div l = q" and mod_int_unique: "k mod l = r" |
|
178 |
using assms eucl_rel_int [of k l] |
|
179 |
using unique_quotient [of k l] unique_remainder [of k l] |
|
180 |
by auto |
|
181 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
182 |
lemma div_pos_geq: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
183 |
fixes k l :: int |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
184 |
assumes "0 < l" and "l \<le> k" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
185 |
shows "k div l = (k - l) div l + 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
186 |
proof - |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
187 |
have "k = (k - l) + l" by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
188 |
then obtain j where k: "k = j + l" .. |
63499
9c9a59949887
Tuned looping simp rules in semiring_div
eberlm <eberlm@in.tum.de>
parents:
63417
diff
changeset
|
189 |
with assms show ?thesis by (simp add: div_add_self2) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
190 |
qed |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
191 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
192 |
lemma mod_pos_geq: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
193 |
fixes k l :: int |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
194 |
assumes "0 < l" and "l \<le> k" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
195 |
shows "k mod l = (k - l) mod l" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
196 |
proof - |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
197 |
have "k = (k - l) + l" by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
198 |
then obtain j where k: "k = j + l" .. |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
199 |
with assms show ?thesis by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
200 |
qed |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
201 |
|
64635 | 202 |
lemma pos_eucl_rel_int_mult_2: |
47166 | 203 |
assumes "0 \<le> b" |
64635 | 204 |
assumes "eucl_rel_int a b (q, r)" |
205 |
shows "eucl_rel_int (1 + 2*a) (2*b) (q, 1 + 2*r)" |
|
206 |
using assms unfolding eucl_rel_int_iff by auto |
|
207 |
||
208 |
lemma neg_eucl_rel_int_mult_2: |
|
47166 | 209 |
assumes "b \<le> 0" |
64635 | 210 |
assumes "eucl_rel_int (a + 1) b (q, r)" |
211 |
shows "eucl_rel_int (1 + 2*a) (2*b) (q, 2*r - 1)" |
|
212 |
using assms unfolding eucl_rel_int_iff by auto |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
213 |
|
60758 | 214 |
text\<open>computing div by shifting\<close> |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
215 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
216 |
lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a" |
64635 | 217 |
using pos_eucl_rel_int_mult_2 [OF _ eucl_rel_int] |
47166 | 218 |
by (rule div_int_unique) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
219 |
|
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
220 |
lemma neg_zdiv_mult_2: |
35815
10e723e54076
tuned proofs (to avoid linarith error message caused by bootstrapping of HOL)
boehmes
parents:
35673
diff
changeset
|
221 |
assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a" |
64635 | 222 |
using neg_eucl_rel_int_mult_2 [OF A eucl_rel_int] |
47166 | 223 |
by (rule div_int_unique) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
224 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
225 |
lemma zdiv_numeral_Bit0 [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
226 |
"numeral (Num.Bit0 v) div numeral (Num.Bit0 w) = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
227 |
numeral v div (numeral w :: int)" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
228 |
unfolding numeral.simps unfolding mult_2 [symmetric] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
229 |
by (rule div_mult_mult1, simp) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
230 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
231 |
lemma zdiv_numeral_Bit1 [simp]: |
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
232 |
"numeral (Num.Bit1 v) div numeral (Num.Bit0 w) = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
233 |
(numeral v div (numeral w :: int))" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
234 |
unfolding numeral.simps |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
235 |
unfolding mult_2 [symmetric] add.commute [of _ 1] |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
236 |
by (rule pos_zdiv_mult_2, simp) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
237 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
238 |
lemma pos_zmod_mult_2: |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
239 |
fixes a b :: int |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
240 |
assumes "0 \<le> a" |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
241 |
shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)" |
64635 | 242 |
using pos_eucl_rel_int_mult_2 [OF assms eucl_rel_int] |
47166 | 243 |
by (rule mod_int_unique) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
244 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
245 |
lemma neg_zmod_mult_2: |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
246 |
fixes a b :: int |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
247 |
assumes "a \<le> 0" |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
248 |
shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1" |
64635 | 249 |
using neg_eucl_rel_int_mult_2 [OF assms eucl_rel_int] |
47166 | 250 |
by (rule mod_int_unique) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
251 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
252 |
lemma zmod_numeral_Bit0 [simp]: |
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
253 |
"numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
254 |
(2::int) * (numeral v mod numeral w)" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
255 |
unfolding numeral_Bit0 [of v] numeral_Bit0 [of w] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
256 |
unfolding mult_2 [symmetric] by (rule mod_mult_mult1) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
257 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
258 |
lemma zmod_numeral_Bit1 [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
259 |
"numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
260 |
2 * (numeral v mod numeral w) + (1::int)" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
261 |
unfolding numeral_Bit1 [of v] numeral_Bit0 [of w] |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
262 |
unfolding mult_2 [symmetric] add.commute [of _ 1] |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46560
diff
changeset
|
263 |
by (rule pos_zmod_mult_2, simp) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
264 |
|
64785 | 265 |
|
60758 | 266 |
subsubsection \<open>Quotients of Signs\<close> |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
267 |
|
67083 | 268 |
lemma div_eq_minus1: "0 < b \<Longrightarrow> - 1 div b = - 1" for b :: int |
269 |
by (simp add: divide_int_def) |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
270 |
|
67083 | 271 |
lemma zmod_minus1: "0 < b \<Longrightarrow> - 1 mod b = b - 1" for b :: int |
272 |
by (auto simp add: modulo_int_def) |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
273 |
|
71991 | 274 |
lemma minus_mod_int_eq: |
275 |
\<open>- k mod l = l - 1 - (k - 1) mod l\<close> if \<open>l \<ge> 0\<close> for k l :: int |
|
276 |
proof (cases \<open>l = 0\<close>) |
|
277 |
case True |
|
278 |
then show ?thesis |
|
279 |
by simp |
|
280 |
next |
|
281 |
case False |
|
282 |
with that have \<open>l > 0\<close> |
|
283 |
by simp |
|
284 |
then show ?thesis |
|
285 |
proof (cases \<open>l dvd k\<close>) |
|
286 |
case True |
|
287 |
then obtain j where \<open>k = l * j\<close> .. |
|
288 |
moreover have \<open>(l * j mod l - 1) mod l = l - 1\<close> |
|
289 |
using \<open>l > 0\<close> by (simp add: zmod_minus1) |
|
290 |
then have \<open>(l * j - 1) mod l = l - 1\<close> |
|
291 |
by (simp only: mod_simps) |
|
292 |
ultimately show ?thesis |
|
293 |
by simp |
|
294 |
next |
|
295 |
case False |
|
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
296 |
moreover have 1: \<open>0 < k mod l\<close> |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
297 |
using \<open>0 < l\<close> False le_less by fastforce |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
298 |
moreover have 2: \<open>k mod l < 1 + l\<close> |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
299 |
using \<open>0 < l\<close> pos_mod_bound[of l k] by linarith |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
300 |
from 1 2 \<open>l > 0\<close> have \<open>(k mod l - 1) mod l = k mod l - 1\<close> |
72610 | 301 |
by (simp add: zmod_trivial_iff) |
71991 | 302 |
ultimately show ?thesis |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
303 |
by (simp only: zmod_zminus1_eq_if) |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
304 |
(simp add: mod_eq_0_iff_dvd algebra_simps mod_simps) |
71991 | 305 |
qed |
306 |
qed |
|
307 |
||
68631 | 308 |
lemma div_neg_pos_less0: |
309 |
fixes a::int |
|
310 |
assumes "a < 0" "0 < b" |
|
311 |
shows "a div b < 0" |
|
312 |
proof - |
|
313 |
have "a div b \<le> - 1 div b" |
|
68644
242d298526a3
de-applying and simplifying proofs
paulson <lp15@cam.ac.uk>
parents:
68631
diff
changeset
|
314 |
using zdiv_mono1 assms by auto |
68631 | 315 |
also have "... \<le> -1" |
316 |
by (simp add: assms(2) div_eq_minus1) |
|
317 |
finally show ?thesis |
|
318 |
by force |
|
319 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
320 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
321 |
lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0" |
68631 | 322 |
by (drule zdiv_mono1_neg, auto) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
323 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
324 |
lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0" |
68631 | 325 |
by (drule zdiv_mono1, auto) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
326 |
|
61799 | 327 |
text\<open>Now for some equivalences of the form \<open>a div b >=< 0 \<longleftrightarrow> \<dots>\<close> |
328 |
conditional upon the sign of \<open>a\<close> or \<open>b\<close>. There are many more. |
|
60758 | 329 |
They should all be simp rules unless that causes too much search.\<close> |
33804 | 330 |
|
68631 | 331 |
lemma pos_imp_zdiv_nonneg_iff: |
332 |
fixes a::int |
|
333 |
assumes "0 < b" |
|
334 |
shows "(0 \<le> a div b) = (0 \<le> a)" |
|
335 |
proof |
|
336 |
show "0 \<le> a div b \<Longrightarrow> 0 \<le> a" |
|
337 |
using assms |
|
338 |
by (simp add: linorder_not_less [symmetric]) (blast intro: div_neg_pos_less0) |
|
339 |
next |
|
340 |
assume "0 \<le> a" |
|
341 |
then have "0 div b \<le> a div b" |
|
342 |
using zdiv_mono1 assms by blast |
|
343 |
then show "0 \<le> a div b" |
|
344 |
by auto |
|
345 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
346 |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
347 |
lemma pos_imp_zdiv_pos_iff: |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
348 |
"0<k \<Longrightarrow> 0 < (i::int) div k \<longleftrightarrow> k \<le> i" |
68631 | 349 |
using pos_imp_zdiv_nonneg_iff[of k i] zdiv_eq_0_iff[of i k] by arith |
350 |
||
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
351 |
lemma neg_imp_zdiv_nonneg_iff: |
68631 | 352 |
fixes a::int |
353 |
assumes "b < 0" |
|
354 |
shows "(0 \<le> a div b) = (a \<le> 0)" |
|
355 |
using assms by (simp add: div_minus_minus [of a, symmetric] pos_imp_zdiv_nonneg_iff del: div_minus_minus) |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
356 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
357 |
(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*) |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
358 |
lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)" |
68631 | 359 |
by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
360 |
|
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
361 |
(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*) |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
362 |
lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)" |
68631 | 363 |
by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
364 |
|
33804 | 365 |
lemma nonneg1_imp_zdiv_pos_iff: |
68631 | 366 |
fixes a::int |
367 |
assumes "0 \<le> a" |
|
368 |
shows "a div b > 0 \<longleftrightarrow> a \<ge> b \<and> b>0" |
|
369 |
proof - |
|
370 |
have "0 < a div b \<Longrightarrow> b \<le> a" |
|
371 |
using div_pos_pos_trivial[of a b] assms by arith |
|
372 |
moreover have "0 < a div b \<Longrightarrow> b > 0" |
|
373 |
using assms div_nonneg_neg_le0[of a b] by(cases "b=0"; force) |
|
374 |
moreover have "b \<le> a \<and> 0 < b \<Longrightarrow> 0 < a div b" |
|
375 |
using int_one_le_iff_zero_less[of "a div b"] zdiv_mono1[of b a b] by simp |
|
376 |
ultimately show ?thesis |
|
377 |
by blast |
|
378 |
qed |
|
33804 | 379 |
|
68631 | 380 |
lemma zmod_le_nonneg_dividend: "(m::int) \<ge> 0 \<Longrightarrow> m mod k \<le> m" |
381 |
by (rule split_zmod[THEN iffD2]) (fastforce dest: q_pos_lemma intro: split_mult_pos_le) |
|
60930 | 382 |
|
75876 | 383 |
lemma sgn_div_eq_sgn_mult: |
384 |
\<open>sgn (k div l) = of_bool (k div l \<noteq> 0) * sgn (k * l)\<close> |
|
385 |
for k l :: int |
|
386 |
proof (cases \<open>k div l = 0\<close>) |
|
387 |
case True |
|
388 |
then show ?thesis |
|
389 |
by simp |
|
390 |
next |
|
391 |
case False |
|
392 |
have \<open>0 \<le> \<bar>k\<bar> div \<bar>l\<bar>\<close> |
|
393 |
by (cases \<open>l = 0\<close>) (simp_all add: pos_imp_zdiv_nonneg_iff) |
|
394 |
then have \<open>\<bar>k\<bar> div \<bar>l\<bar> \<noteq> 0 \<longleftrightarrow> 0 < \<bar>k\<bar> div \<bar>l\<bar>\<close> |
|
395 |
by (simp add: less_le) |
|
396 |
also have \<open>\<dots> \<longleftrightarrow> \<bar>k\<bar> \<ge> \<bar>l\<bar>\<close> |
|
397 |
using False nonneg1_imp_zdiv_pos_iff by auto |
|
398 |
finally have *: \<open>\<bar>k\<bar> div \<bar>l\<bar> \<noteq> 0 \<longleftrightarrow> \<bar>l\<bar> \<le> \<bar>k\<bar>\<close> . |
|
399 |
show ?thesis |
|
400 |
using \<open>0 \<le> \<bar>k\<bar> div \<bar>l\<bar>\<close> False |
|
401 |
by (auto simp add: div_eq_div_abs [of k l] div_eq_sgn_abs [of k l] |
|
402 |
sgn_mult sgn_1_pos sgn_1_neg sgn_eq_0_iff nonneg1_imp_zdiv_pos_iff * dest: sgn_not_eq_imp) |
|
403 |
qed |
|
404 |
||
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
405 |
|
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
406 |
subsubsection \<open>Further properties\<close> |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
407 |
|
66817 | 408 |
lemma div_int_pos_iff: |
409 |
"k div l \<ge> 0 \<longleftrightarrow> k = 0 \<or> l = 0 \<or> k \<ge> 0 \<and> l \<ge> 0 |
|
410 |
\<or> k < 0 \<and> l < 0" |
|
411 |
for k l :: int |
|
68631 | 412 |
proof (cases "k = 0 \<or> l = 0") |
413 |
case False |
|
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
414 |
then have *: "k \<noteq> 0" "l \<noteq> 0" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
415 |
by auto |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
416 |
then have "0 \<le> k div l \<Longrightarrow> \<not> k < 0 \<Longrightarrow> 0 \<le> l" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
417 |
by (meson neg_imp_zdiv_neg_iff not_le not_less_iff_gr_or_eq) |
68631 | 418 |
then show ?thesis |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
419 |
using * by (auto simp add: pos_imp_zdiv_nonneg_iff neg_imp_zdiv_nonneg_iff) |
68631 | 420 |
qed auto |
66817 | 421 |
|
422 |
lemma mod_int_pos_iff: |
|
423 |
"k mod l \<ge> 0 \<longleftrightarrow> l dvd k \<or> l = 0 \<and> k \<ge> 0 \<or> l > 0" |
|
424 |
for k l :: int |
|
68631 | 425 |
proof (cases "l > 0") |
426 |
case False |
|
427 |
then show ?thesis |
|
69695 | 428 |
by (simp add: dvd_eq_mod_eq_0) (use neg_mod_sign [of l k] in \<open>auto simp add: le_less not_less\<close>) |
68631 | 429 |
qed auto |
66817 | 430 |
|
68631 | 431 |
text \<open>Simplify expressions in which div and mod combine numerical constants\<close> |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
432 |
|
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
433 |
lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q" |
64635 | 434 |
by (rule div_int_unique [of a b q r]) (simp add: eucl_rel_int_iff) |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
435 |
|
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
436 |
lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
437 |
by (rule div_int_unique [of a b q r], |
64635 | 438 |
simp add: eucl_rel_int_iff) |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
439 |
|
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
440 |
lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
441 |
by (rule mod_int_unique [of a b q r], |
64635 | 442 |
simp add: eucl_rel_int_iff) |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
443 |
|
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
444 |
lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
445 |
by (rule mod_int_unique [of a b q r], |
64635 | 446 |
simp add: eucl_rel_int_iff) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
447 |
|
61944 | 448 |
lemma abs_div: "(y::int) dvd x \<Longrightarrow> \<bar>x div y\<bar> = \<bar>x\<bar> div \<bar>y\<bar>" |
68631 | 449 |
unfolding dvd_def by (cases "y=0") (auto simp add: abs_mult) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
450 |
|
60758 | 451 |
text\<open>Suggested by Matthias Daum\<close> |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
452 |
lemma int_power_div_base: |
68631 | 453 |
fixes k :: int |
454 |
assumes "0 < m" "0 < k" |
|
455 |
shows "k ^ m div k = (k::int) ^ (m - Suc 0)" |
|
456 |
proof - |
|
457 |
have eq: "k ^ m = k ^ ((m - Suc 0) + Suc 0)" |
|
458 |
by (simp add: assms) |
|
459 |
show ?thesis |
|
460 |
using assms by (simp only: power_add eq) auto |
|
461 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
462 |
|
60758 | 463 |
text\<open>Suggested by Matthias Daum\<close> |
68631 | 464 |
lemma int_div_less_self: |
465 |
fixes x::int |
|
466 |
assumes "0 < x" "1 < k" |
|
467 |
shows "x div k < x" |
|
468 |
proof - |
|
469 |
have "nat x div nat k < nat x" |
|
470 |
by (simp add: assms) |
|
471 |
with assms show ?thesis |
|
472 |
by (simp add: nat_div_distrib [symmetric]) |
|
473 |
qed |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
474 |
|
66837 | 475 |
lemma mod_eq_dvd_iff_nat: |
476 |
"m mod q = n mod q \<longleftrightarrow> q dvd m - n" if "m \<ge> n" for m n q :: nat |
|
477 |
proof - |
|
478 |
have "int m mod int q = int n mod int q \<longleftrightarrow> int q dvd int m - int n" |
|
479 |
by (simp add: mod_eq_dvd_iff) |
|
480 |
with that have "int (m mod q) = int (n mod q) \<longleftrightarrow> int q dvd int (m - n)" |
|
481 |
by (simp only: of_nat_mod of_nat_diff) |
|
482 |
then show ?thesis |
|
67118 | 483 |
by simp |
66837 | 484 |
qed |
485 |
||
486 |
lemma mod_eq_nat1E: |
|
487 |
fixes m n q :: nat |
|
488 |
assumes "m mod q = n mod q" and "m \<ge> n" |
|
489 |
obtains s where "m = n + q * s" |
|
490 |
proof - |
|
491 |
from assms have "q dvd m - n" |
|
492 |
by (simp add: mod_eq_dvd_iff_nat) |
|
493 |
then obtain s where "m - n = q * s" .. |
|
494 |
with \<open>m \<ge> n\<close> have "m = n + q * s" |
|
495 |
by simp |
|
496 |
with that show thesis . |
|
497 |
qed |
|
498 |
||
499 |
lemma mod_eq_nat2E: |
|
500 |
fixes m n q :: nat |
|
501 |
assumes "m mod q = n mod q" and "n \<ge> m" |
|
502 |
obtains s where "n = m + q * s" |
|
503 |
using assms mod_eq_nat1E [of n q m] by (auto simp add: ac_simps) |
|
504 |
||
505 |
lemma nat_mod_eq_lemma: |
|
506 |
assumes "(x::nat) mod n = y mod n" and "y \<le> x" |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
507 |
shows "\<exists>q. x = y + n * q" |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
508 |
using assms by (rule mod_eq_nat1E) (rule exI) |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
509 |
|
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
510 |
lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
511 |
(is "?lhs = ?rhs") |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
512 |
proof |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
513 |
assume H: "x mod n = y mod n" |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
514 |
{assume xy: "x \<le> y" |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
515 |
from H have th: "y mod n = x mod n" by simp |
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
516 |
from nat_mod_eq_lemma[OF th xy] have ?rhs |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
517 |
proof |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
518 |
fix q |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
519 |
assume "y = x + n * q" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
520 |
then have "x + n * q = y + n * 0" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
521 |
by simp |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
522 |
then show "\<exists>q1 q2. x + n * q1 = y + n * q2" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
523 |
by blast |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
524 |
qed} |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
525 |
moreover |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
526 |
{assume xy: "y \<le> x" |
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
527 |
from nat_mod_eq_lemma[OF H xy] have ?rhs |
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
528 |
proof |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
529 |
fix q |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
530 |
assume "x = y + n * q" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
531 |
then have "x + n * 0 = y + n * q" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
532 |
by simp |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
533 |
then show "\<exists>q1 q2. x + n * q1 = y + n * q2" |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
534 |
by blast |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74101
diff
changeset
|
535 |
qed} |
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
536 |
ultimately show ?rhs using linear[of x y] by blast |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
537 |
next |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
538 |
assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
539 |
hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
540 |
thus ?lhs by simp |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
541 |
qed |
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
542 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
543 |
|
68253 | 544 |
subsection \<open>Numeral division with a pragmatic type class\<close> |
545 |
||
546 |
text \<open> |
|
547 |
The following type class contains everything necessary to formulate |
|
548 |
a division algorithm in ring structures with numerals, restricted |
|
75936 | 549 |
to its positive segments. |
68253 | 550 |
\<close> |
551 |
||
75936 | 552 |
class unique_euclidean_semiring_with_nat_division = unique_euclidean_semiring_with_nat + |
553 |
fixes divmod :: \<open>num \<Rightarrow> num \<Rightarrow> 'a \<times> 'a\<close> |
|
554 |
and divmod_step :: \<open>'a \<Rightarrow> 'a \<times> 'a \<Rightarrow> 'a \<times> 'a\<close> \<comment> \<open> |
|
555 |
These are conceptually definitions but force generated code |
|
556 |
to be monomorphic wrt. particular instances of this class which |
|
557 |
yields a significant speedup.\<close> |
|
558 |
assumes divmod_def: \<open>divmod m n = (numeral m div numeral n, numeral m mod numeral n)\<close> |
|
559 |
and divmod_step_def [simp]: \<open>divmod_step l (q, r) = |
|
560 |
(if euclidean_size l \<le> euclidean_size r then (2 * q + 1, r - l) |
|
561 |
else (2 * q, r))\<close> \<comment> \<open> |
|
562 |
This is a formulation of one step (referring to one digit position) |
|
563 |
in school-method division: compare the dividend at the current |
|
564 |
digit position with the remainder from previous division steps |
|
565 |
and evaluate accordingly.\<close> |
|
68253 | 566 |
begin |
567 |
||
568 |
lemma fst_divmod: |
|
75936 | 569 |
\<open>fst (divmod m n) = numeral m div numeral n\<close> |
68253 | 570 |
by (simp add: divmod_def) |
571 |
||
572 |
lemma snd_divmod: |
|
75936 | 573 |
\<open>snd (divmod m n) = numeral m mod numeral n\<close> |
68253 | 574 |
by (simp add: divmod_def) |
575 |
||
576 |
text \<open> |
|
75936 | 577 |
Following a formulation of school-method division. |
68253 | 578 |
If the divisor is smaller than the dividend, terminate. |
579 |
If not, shift the dividend to the right until termination |
|
580 |
occurs and then reiterate single division steps in the |
|
581 |
opposite direction. |
|
582 |
\<close> |
|
583 |
||
584 |
lemma divmod_divmod_step: |
|
75936 | 585 |
\<open>divmod m n = (if m < n then (0, numeral m) |
586 |
else divmod_step (numeral n) (divmod m (Num.Bit0 n)))\<close> |
|
587 |
proof (cases \<open>m < n\<close>) |
|
588 |
case True |
|
68253 | 589 |
then show ?thesis |
75936 | 590 |
by (simp add: prod_eq_iff fst_divmod snd_divmod flip: of_nat_numeral of_nat_div of_nat_mod) |
68253 | 591 |
next |
592 |
case False |
|
75936 | 593 |
define r s t where \<open>r = (numeral m :: nat)\<close> \<open>s = (numeral n :: nat)\<close> \<open>t = 2 * s\<close> |
594 |
then have *: \<open>numeral m = of_nat r\<close> \<open>numeral n = of_nat s\<close> \<open>numeral (num.Bit0 n) = of_nat t\<close> |
|
595 |
and \<open>\<not> s \<le> r mod s\<close> |
|
596 |
by (simp_all add: not_le) |
|
597 |
have t: \<open>2 * (r div t) = r div s - r div s mod 2\<close> |
|
598 |
\<open>r mod t = s * (r div s mod 2) + r mod s\<close> |
|
599 |
by (simp add: Rings.minus_mod_eq_mult_div Groups.mult.commute [of 2] Euclidean_Division.div_mult2_eq \<open>t = 2 * s\<close>) |
|
600 |
(use mod_mult2_eq [of r s 2] in \<open>simp add: ac_simps \<open>t = 2 * s\<close>\<close>) |
|
601 |
have rs: \<open>r div s mod 2 = 0 \<or> r div s mod 2 = Suc 0\<close> |
|
602 |
by auto |
|
603 |
from \<open>\<not> s \<le> r mod s\<close> have \<open>s \<le> r mod t \<Longrightarrow> |
|
604 |
r div s = Suc (2 * (r div t)) \<and> |
|
605 |
r mod s = r mod t - s\<close> |
|
606 |
using rs |
|
607 |
by (auto simp add: t) |
|
608 |
moreover have \<open>r mod t < s \<Longrightarrow> |
|
609 |
r div s = 2 * (r div t) \<and> |
|
610 |
r mod s = r mod t\<close> |
|
611 |
using rs |
|
612 |
by (auto simp add: t) |
|
613 |
ultimately show ?thesis |
|
614 |
by (simp add: divmod_def prod_eq_iff split_def Let_def |
|
615 |
not_less mod_eq_0_iff_dvd Rings.mod_eq_0_iff_dvd False not_le *) |
|
616 |
(simp add: flip: of_nat_numeral of_nat_mult add.commute [of 1] of_nat_div of_nat_mod of_nat_Suc of_nat_diff) |
|
68253 | 617 |
qed |
618 |
||
619 |
text \<open>The division rewrite proper -- first, trivial results involving \<open>1\<close>\<close> |
|
620 |
||
621 |
lemma divmod_trivial [simp]: |
|
71756 | 622 |
"divmod m Num.One = (numeral m, 0)" |
68253 | 623 |
"divmod num.One (num.Bit0 n) = (0, Numeral1)" |
624 |
"divmod num.One (num.Bit1 n) = (0, Numeral1)" |
|
625 |
using divmod_divmod_step [of "Num.One"] by (simp_all add: divmod_def) |
|
626 |
||
627 |
text \<open>Division by an even number is a right-shift\<close> |
|
628 |
||
629 |
lemma divmod_cancel [simp]: |
|
75936 | 630 |
\<open>divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))\<close> (is ?P) |
631 |
\<open>divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))\<close> (is ?Q) |
|
68253 | 632 |
proof - |
75936 | 633 |
define r s where \<open>r = (numeral m :: nat)\<close> \<open>s = (numeral n :: nat)\<close> |
634 |
then have *: \<open>numeral m = of_nat r\<close> \<open>numeral n = of_nat s\<close> |
|
635 |
\<open>numeral (num.Bit0 m) = of_nat (2 * r)\<close> \<open>numeral (num.Bit0 n) = of_nat (2 * s)\<close> |
|
636 |
\<open>numeral (num.Bit1 m) = of_nat (Suc (2 * r))\<close> |
|
637 |
by simp_all |
|
638 |
show ?P and ?Q |
|
639 |
by (simp_all add: divmod_def *) |
|
640 |
(simp_all flip: of_nat_numeral of_nat_div of_nat_mod of_nat_mult add.commute [of 1] of_nat_Suc |
|
641 |
add: Euclidean_Division.mod_mult_mult1 div_mult2_eq [of _ 2] mod_mult2_eq [of _ 2]) |
|
68253 | 642 |
qed |
643 |
||
644 |
text \<open>The really hard work\<close> |
|
645 |
||
646 |
lemma divmod_steps [simp]: |
|
647 |
"divmod (num.Bit0 m) (num.Bit1 n) = |
|
648 |
(if m \<le> n then (0, numeral (num.Bit0 m)) |
|
75883 | 649 |
else divmod_step (numeral (num.Bit1 n)) |
68253 | 650 |
(divmod (num.Bit0 m) |
651 |
(num.Bit0 (num.Bit1 n))))" |
|
652 |
"divmod (num.Bit1 m) (num.Bit1 n) = |
|
653 |
(if m < n then (0, numeral (num.Bit1 m)) |
|
75883 | 654 |
else divmod_step (numeral (num.Bit1 n)) |
68253 | 655 |
(divmod (num.Bit1 m) |
656 |
(num.Bit0 (num.Bit1 n))))" |
|
657 |
by (simp_all add: divmod_divmod_step) |
|
658 |
||
75936 | 659 |
lemmas divmod_algorithm_code = divmod_trivial divmod_cancel divmod_steps |
68253 | 660 |
|
661 |
text \<open>Special case: divisibility\<close> |
|
662 |
||
663 |
definition divides_aux :: "'a \<times> 'a \<Rightarrow> bool" |
|
664 |
where |
|
665 |
"divides_aux qr \<longleftrightarrow> snd qr = 0" |
|
666 |
||
667 |
lemma divides_aux_eq [simp]: |
|
668 |
"divides_aux (q, r) \<longleftrightarrow> r = 0" |
|
669 |
by (simp add: divides_aux_def) |
|
670 |
||
671 |
lemma dvd_numeral_simp [simp]: |
|
672 |
"numeral m dvd numeral n \<longleftrightarrow> divides_aux (divmod n m)" |
|
673 |
by (simp add: divmod_def mod_eq_0_iff_dvd) |
|
674 |
||
675 |
text \<open>Generic computation of quotient and remainder\<close> |
|
676 |
||
677 |
lemma numeral_div_numeral [simp]: |
|
678 |
"numeral k div numeral l = fst (divmod k l)" |
|
679 |
by (simp add: fst_divmod) |
|
680 |
||
681 |
lemma numeral_mod_numeral [simp]: |
|
682 |
"numeral k mod numeral l = snd (divmod k l)" |
|
683 |
by (simp add: snd_divmod) |
|
684 |
||
685 |
lemma one_div_numeral [simp]: |
|
686 |
"1 div numeral n = fst (divmod num.One n)" |
|
687 |
by (simp add: fst_divmod) |
|
688 |
||
689 |
lemma one_mod_numeral [simp]: |
|
690 |
"1 mod numeral n = snd (divmod num.One n)" |
|
691 |
by (simp add: snd_divmod) |
|
692 |
||
693 |
text \<open>Computing congruences modulo \<open>2 ^ q\<close>\<close> |
|
694 |
||
695 |
lemma cong_exp_iff_simps: |
|
696 |
"numeral n mod numeral Num.One = 0 |
|
697 |
\<longleftrightarrow> True" |
|
698 |
"numeral (Num.Bit0 n) mod numeral (Num.Bit0 q) = 0 |
|
699 |
\<longleftrightarrow> numeral n mod numeral q = 0" |
|
700 |
"numeral (Num.Bit1 n) mod numeral (Num.Bit0 q) = 0 |
|
701 |
\<longleftrightarrow> False" |
|
702 |
"numeral m mod numeral Num.One = (numeral n mod numeral Num.One) |
|
703 |
\<longleftrightarrow> True" |
|
704 |
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) |
|
705 |
\<longleftrightarrow> True" |
|
706 |
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) |
|
707 |
\<longleftrightarrow> False" |
|
708 |
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) |
|
709 |
\<longleftrightarrow> (numeral n mod numeral q) = 0" |
|
710 |
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) |
|
711 |
\<longleftrightarrow> False" |
|
712 |
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) |
|
713 |
\<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)" |
|
714 |
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) |
|
715 |
\<longleftrightarrow> False" |
|
716 |
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) |
|
717 |
\<longleftrightarrow> (numeral m mod numeral q) = 0" |
|
718 |
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) |
|
719 |
\<longleftrightarrow> False" |
|
720 |
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) |
|
721 |
\<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)" |
|
722 |
by (auto simp add: case_prod_beta dest: arg_cong [of _ _ even]) |
|
723 |
||
724 |
end |
|
725 |
||
75936 | 726 |
instantiation nat :: unique_euclidean_semiring_with_nat_division |
68253 | 727 |
begin |
728 |
||
729 |
definition divmod_nat :: "num \<Rightarrow> num \<Rightarrow> nat \<times> nat" |
|
730 |
where |
|
731 |
divmod'_nat_def: "divmod_nat m n = (numeral m div numeral n, numeral m mod numeral n)" |
|
732 |
||
75883 | 733 |
definition divmod_step_nat :: "nat \<Rightarrow> nat \<times> nat \<Rightarrow> nat \<times> nat" |
68253 | 734 |
where |
735 |
"divmod_step_nat l qr = (let (q, r) = qr |
|
75883 | 736 |
in if r \<ge> l then (2 * q + 1, r - l) |
68253 | 737 |
else (2 * q, r))" |
738 |
||
75936 | 739 |
instance |
740 |
by standard (simp_all add: divmod'_nat_def divmod_step_nat_def) |
|
68253 | 741 |
|
742 |
end |
|
743 |
||
744 |
declare divmod_algorithm_code [where ?'a = nat, code] |
|
745 |
||
746 |
lemma Suc_0_div_numeral [simp]: |
|
747 |
fixes k l :: num |
|
748 |
shows "Suc 0 div numeral k = fst (divmod Num.One k)" |
|
749 |
by (simp_all add: fst_divmod) |
|
750 |
||
751 |
lemma Suc_0_mod_numeral [simp]: |
|
752 |
fixes k l :: num |
|
753 |
shows "Suc 0 mod numeral k = snd (divmod Num.One k)" |
|
754 |
by (simp_all add: snd_divmod) |
|
755 |
||
75936 | 756 |
instantiation int :: unique_euclidean_semiring_with_nat_division |
68253 | 757 |
begin |
758 |
||
759 |
definition divmod_int :: "num \<Rightarrow> num \<Rightarrow> int \<times> int" |
|
760 |
where |
|
761 |
"divmod_int m n = (numeral m div numeral n, numeral m mod numeral n)" |
|
762 |
||
75883 | 763 |
definition divmod_step_int :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" |
68253 | 764 |
where |
765 |
"divmod_step_int l qr = (let (q, r) = qr |
|
75936 | 766 |
in if \<bar>l\<bar> \<le> \<bar>r\<bar> then (2 * q + 1, r - l) |
68253 | 767 |
else (2 * q, r))" |
768 |
||
769 |
instance |
|
75936 | 770 |
by standard (auto simp add: divmod_int_def divmod_step_int_def) |
68253 | 771 |
|
772 |
end |
|
773 |
||
774 |
declare divmod_algorithm_code [where ?'a = int, code] |
|
775 |
||
776 |
context |
|
777 |
begin |
|
778 |
||
779 |
qualified definition adjust_div :: "int \<times> int \<Rightarrow> int" |
|
780 |
where |
|
781 |
"adjust_div qr = (let (q, r) = qr in q + of_bool (r \<noteq> 0))" |
|
782 |
||
783 |
qualified lemma adjust_div_eq [simp, code]: |
|
784 |
"adjust_div (q, r) = q + of_bool (r \<noteq> 0)" |
|
785 |
by (simp add: adjust_div_def) |
|
786 |
||
75882 | 787 |
qualified definition adjust_mod :: "num \<Rightarrow> int \<Rightarrow> int" |
68253 | 788 |
where |
75882 | 789 |
[simp]: "adjust_mod l r = (if r = 0 then 0 else numeral l - r)" |
68253 | 790 |
|
791 |
lemma minus_numeral_div_numeral [simp]: |
|
792 |
"- numeral m div numeral n = - (adjust_div (divmod m n) :: int)" |
|
793 |
proof - |
|
794 |
have "int (fst (divmod m n)) = fst (divmod m n)" |
|
795 |
by (simp only: fst_divmod divide_int_def) auto |
|
796 |
then show ?thesis |
|
797 |
by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def) |
|
798 |
qed |
|
799 |
||
800 |
lemma minus_numeral_mod_numeral [simp]: |
|
75882 | 801 |
"- numeral m mod numeral n = adjust_mod n (snd (divmod m n) :: int)" |
68253 | 802 |
proof (cases "snd (divmod m n) = (0::int)") |
803 |
case True |
|
804 |
then show ?thesis |
|
805 |
by (simp add: mod_eq_0_iff_dvd divides_aux_def) |
|
806 |
next |
|
807 |
case False |
|
808 |
then have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)" |
|
809 |
by (simp only: snd_divmod modulo_int_def) auto |
|
810 |
then show ?thesis |
|
811 |
by (simp add: divides_aux_def adjust_div_def) |
|
812 |
(simp add: divides_aux_def modulo_int_def) |
|
813 |
qed |
|
814 |
||
815 |
lemma numeral_div_minus_numeral [simp]: |
|
816 |
"numeral m div - numeral n = - (adjust_div (divmod m n) :: int)" |
|
817 |
proof - |
|
818 |
have "int (fst (divmod m n)) = fst (divmod m n)" |
|
819 |
by (simp only: fst_divmod divide_int_def) auto |
|
820 |
then show ?thesis |
|
821 |
by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def) |
|
822 |
qed |
|
823 |
||
824 |
lemma numeral_mod_minus_numeral [simp]: |
|
75882 | 825 |
"numeral m mod - numeral n = - adjust_mod n (snd (divmod m n) :: int)" |
68253 | 826 |
proof (cases "snd (divmod m n) = (0::int)") |
827 |
case True |
|
828 |
then show ?thesis |
|
829 |
by (simp add: mod_eq_0_iff_dvd divides_aux_def) |
|
830 |
next |
|
831 |
case False |
|
832 |
then have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)" |
|
833 |
by (simp only: snd_divmod modulo_int_def) auto |
|
834 |
then show ?thesis |
|
835 |
by (simp add: divides_aux_def adjust_div_def) |
|
836 |
(simp add: divides_aux_def modulo_int_def) |
|
837 |
qed |
|
838 |
||
839 |
lemma minus_one_div_numeral [simp]: |
|
840 |
"- 1 div numeral n = - (adjust_div (divmod Num.One n) :: int)" |
|
841 |
using minus_numeral_div_numeral [of Num.One n] by simp |
|
842 |
||
843 |
lemma minus_one_mod_numeral [simp]: |
|
75882 | 844 |
"- 1 mod numeral n = adjust_mod n (snd (divmod Num.One n) :: int)" |
68253 | 845 |
using minus_numeral_mod_numeral [of Num.One n] by simp |
846 |
||
847 |
lemma one_div_minus_numeral [simp]: |
|
848 |
"1 div - numeral n = - (adjust_div (divmod Num.One n) :: int)" |
|
849 |
using numeral_div_minus_numeral [of Num.One n] by simp |
|
850 |
||
851 |
lemma one_mod_minus_numeral [simp]: |
|
75882 | 852 |
"1 mod - numeral n = - adjust_mod n (snd (divmod Num.One n) :: int)" |
68253 | 853 |
using numeral_mod_minus_numeral [of Num.One n] by simp |
854 |
||
855 |
end |
|
856 |
||
71756 | 857 |
lemma divmod_BitM_2_eq [simp]: |
858 |
\<open>divmod (Num.BitM m) (Num.Bit0 Num.One) = (numeral m - 1, (1 :: int))\<close> |
|
859 |
by (cases m) simp_all |
|
860 |
||
68253 | 861 |
lemma div_positive_int: |
862 |
"k div l > 0" if "k \<ge> l" and "l > 0" for k l :: int |
|
75936 | 863 |
using that by (simp add: nonneg1_imp_zdiv_pos_iff) |
68253 | 864 |
|
865 |
||
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
866 |
subsubsection \<open>Dedicated simproc for calculation\<close> |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
867 |
|
75936 | 868 |
lemma euclidean_size_nat_less_eq_iff: |
869 |
\<open>euclidean_size m \<le> euclidean_size n \<longleftrightarrow> m \<le> n\<close> for m n :: nat |
|
870 |
by simp |
|
871 |
||
872 |
lemma euclidean_size_int_less_eq_iff: |
|
873 |
\<open>euclidean_size k \<le> euclidean_size l \<longleftrightarrow> \<bar>k\<bar> \<le> \<bar>l\<bar>\<close> for k l :: int |
|
874 |
by auto |
|
875 |
||
60758 | 876 |
text \<open> |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
877 |
There is space for improvement here: the calculation itself |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
878 |
could be carried out outside the logic, and a generic simproc |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
879 |
(simplifier setup) for generic calculation would be helpful. |
60758 | 880 |
\<close> |
53067
ee0b7c2315d2
type class for generic division algorithm on numerals
haftmann
parents:
53066
diff
changeset
|
881 |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
882 |
simproc_setup numeral_divmod |
75936 | 883 |
("0 div 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | "0 mod 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
884 |
"0 div 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | "0 mod 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
885 |
"0 div - 1 :: int" | "0 mod - 1 :: int" | |
75936 | 886 |
"0 div numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | "0 mod numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
887 |
"0 div - numeral b :: int" | "0 mod - numeral b :: int" | |
75936 | 888 |
"1 div 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | "1 mod 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
889 |
"1 div 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | "1 mod 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
890 |
"1 div - 1 :: int" | "1 mod - 1 :: int" | |
75936 | 891 |
"1 div numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | "1 mod numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
892 |
"1 div - numeral b :: int" |"1 mod - numeral b :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
893 |
"- 1 div 0 :: int" | "- 1 mod 0 :: int" | "- 1 div 1 :: int" | "- 1 mod 1 :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
894 |
"- 1 div - 1 :: int" | "- 1 mod - 1 :: int" | "- 1 div numeral b :: int" | "- 1 mod numeral b :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
895 |
"- 1 div - numeral b :: int" | "- 1 mod - numeral b :: int" | |
75936 | 896 |
"numeral a div 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | "numeral a mod 0 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
897 |
"numeral a div 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | "numeral a mod 1 :: 'a :: unique_euclidean_semiring_with_nat_division" | |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
898 |
"numeral a div - 1 :: int" | "numeral a mod - 1 :: int" | |
75936 | 899 |
"numeral a div numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | "numeral a mod numeral b :: 'a :: unique_euclidean_semiring_with_nat_division" | |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
900 |
"numeral a div - numeral b :: int" | "numeral a mod - numeral b :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
901 |
"- numeral a div 0 :: int" | "- numeral a mod 0 :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
902 |
"- numeral a div 1 :: int" | "- numeral a mod 1 :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
903 |
"- numeral a div - 1 :: int" | "- numeral a mod - 1 :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
904 |
"- numeral a div numeral b :: int" | "- numeral a mod numeral b :: int" | |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
905 |
"- numeral a div - numeral b :: int" | "- numeral a mod - numeral b :: int") = |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
906 |
\<open> let |
69593 | 907 |
val if_cong = the (Code.get_case_cong \<^theory> \<^const_name>\<open>If\<close>); |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
908 |
fun successful_rewrite ctxt ct = |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
909 |
let |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
910 |
val thm = Simplifier.rewrite ctxt ct |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
911 |
in if Thm.is_reflexive thm then NONE else SOME thm end; |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
912 |
in fn phi => |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
913 |
let |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
914 |
val simps = Morphism.fact phi (@{thms div_0 mod_0 div_by_0 mod_by_0 div_by_1 mod_by_1 |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
915 |
one_div_numeral one_mod_numeral minus_one_div_numeral minus_one_mod_numeral |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
916 |
one_div_minus_numeral one_mod_minus_numeral |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
917 |
numeral_div_numeral numeral_mod_numeral minus_numeral_div_numeral minus_numeral_mod_numeral |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
918 |
numeral_div_minus_numeral numeral_mod_minus_numeral |
60930 | 919 |
div_minus_minus mod_minus_minus Divides.adjust_div_eq of_bool_eq one_neq_zero |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
920 |
numeral_neq_zero neg_equal_0_iff_equal arith_simps arith_special divmod_trivial |
75936 | 921 |
divmod_cancel divmod_steps divmod_step_def fst_conv snd_conv numeral_One |
60930 | 922 |
case_prod_beta rel_simps Divides.adjust_mod_def div_minus1_right mod_minus1_right |
75936 | 923 |
minus_minus numeral_times_numeral mult_zero_right mult_1_right |
924 |
euclidean_size_nat_less_eq_iff euclidean_size_int_less_eq_iff diff_nat_numeral nat_numeral} |
|
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
925 |
@ [@{lemma "0 = 0 \<longleftrightarrow> True" by simp}]); |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
926 |
fun prepare_simpset ctxt = HOL_ss |> Simplifier.simpset_map ctxt |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
927 |
(Simplifier.add_cong if_cong #> fold Simplifier.add_simp simps) |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
928 |
in fn ctxt => successful_rewrite (Simplifier.put_simpset (prepare_simpset ctxt) ctxt) end |
69216
1a52baa70aed
clarified ML_Context.expression: it is a closed expression, not a let-declaration -- thus source positions are more accurate (amending d8849cfad60f, 162a4c2e97bc);
wenzelm
parents:
68644
diff
changeset
|
929 |
end |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
930 |
\<close> |
34126 | 931 |
|
35673 | 932 |
|
60758 | 933 |
subsubsection \<open>Code generation\<close> |
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
934 |
|
68253 | 935 |
definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" |
936 |
where "divmod_nat m n = (m div n, m mod n)" |
|
937 |
||
938 |
lemma fst_divmod_nat [simp]: |
|
939 |
"fst (divmod_nat m n) = m div n" |
|
940 |
by (simp add: divmod_nat_def) |
|
941 |
||
942 |
lemma snd_divmod_nat [simp]: |
|
943 |
"snd (divmod_nat m n) = m mod n" |
|
944 |
by (simp add: divmod_nat_def) |
|
945 |
||
946 |
lemma divmod_nat_if [code]: |
|
947 |
"Divides.divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else |
|
948 |
let (q, r) = Divides.divmod_nat (m - n) n in (Suc q, r))" |
|
949 |
by (simp add: prod_eq_iff case_prod_beta not_less le_div_geq le_mod_geq) |
|
950 |
||
951 |
lemma [code]: |
|
952 |
"m div n = fst (divmod_nat m n)" |
|
953 |
"m mod n = snd (divmod_nat m n)" |
|
954 |
by simp_all |
|
955 |
||
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
956 |
lemma [code]: |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
957 |
fixes k :: int |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
958 |
shows |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
959 |
"k div 0 = 0" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
960 |
"k mod 0 = k" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
961 |
"0 div k = 0" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
962 |
"0 mod k = 0" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
963 |
"k div Int.Pos Num.One = k" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
964 |
"k mod Int.Pos Num.One = 0" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
965 |
"k div Int.Neg Num.One = - k" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
966 |
"k mod Int.Neg Num.One = 0" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
967 |
"Int.Pos m div Int.Pos n = (fst (divmod m n) :: int)" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
968 |
"Int.Pos m mod Int.Pos n = (snd (divmod m n) :: int)" |
60930 | 969 |
"Int.Neg m div Int.Pos n = - (Divides.adjust_div (divmod m n) :: int)" |
75882 | 970 |
"Int.Neg m mod Int.Pos n = Divides.adjust_mod n (snd (divmod m n) :: int)" |
60930 | 971 |
"Int.Pos m div Int.Neg n = - (Divides.adjust_div (divmod m n) :: int)" |
75882 | 972 |
"Int.Pos m mod Int.Neg n = - Divides.adjust_mod n (snd (divmod m n) :: int)" |
60868
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
973 |
"Int.Neg m div Int.Neg n = (fst (divmod m n) :: int)" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
974 |
"Int.Neg m mod Int.Neg n = - (snd (divmod m n) :: int)" |
dd18c33c001e
direct bootstrap of integer division from natural division
haftmann
parents:
60867
diff
changeset
|
975 |
by simp_all |
53069
d165213e3924
execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents:
53068
diff
changeset
|
976 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52398
diff
changeset
|
977 |
code_identifier |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52398
diff
changeset
|
978 |
code_module Divides \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith |
33364 | 979 |
|
64246 | 980 |
|
68253 | 981 |
subsection \<open>Lemmas of doubtful value\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
982 |
|
75936 | 983 |
class unique_euclidean_semiring_numeral = unique_euclidean_semiring_with_nat + linordered_semidom + |
984 |
assumes div_less: "0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a div b = 0" |
|
985 |
and mod_less: " 0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a mod b = a" |
|
986 |
and div_positive: "0 < b \<Longrightarrow> b \<le> a \<Longrightarrow> a div b > 0" |
|
987 |
and mod_less_eq_dividend: "0 \<le> a \<Longrightarrow> a mod b \<le> a" |
|
988 |
and pos_mod_bound: "0 < b \<Longrightarrow> a mod b < b" |
|
989 |
and pos_mod_sign: "0 < b \<Longrightarrow> 0 \<le> a mod b" |
|
990 |
and mod_mult2_eq: "0 \<le> c \<Longrightarrow> a mod (b * c) = b * (a div b mod c) + a mod b" |
|
991 |
and div_mult2_eq: "0 \<le> c \<Longrightarrow> a div (b * c) = a div b div c" |
|
992 |
assumes discrete: "a < b \<longleftrightarrow> a + 1 \<le> b" |
|
993 |
begin |
|
994 |
||
995 |
lemma divmod_digit_1: |
|
996 |
assumes "0 \<le> a" "0 < b" and "b \<le> a mod (2 * b)" |
|
997 |
shows "2 * (a div (2 * b)) + 1 = a div b" (is "?P") |
|
998 |
and "a mod (2 * b) - b = a mod b" (is "?Q") |
|
999 |
proof - |
|
1000 |
from assms mod_less_eq_dividend [of a "2 * b"] have "b \<le> a" |
|
1001 |
by (auto intro: trans) |
|
1002 |
with \<open>0 < b\<close> have "0 < a div b" by (auto intro: div_positive) |
|
1003 |
then have [simp]: "1 \<le> a div b" by (simp add: discrete) |
|
1004 |
with \<open>0 < b\<close> have mod_less: "a mod b < b" by (simp add: pos_mod_bound) |
|
1005 |
define w where "w = a div b mod 2" |
|
1006 |
then have w_exhaust: "w = 0 \<or> w = 1" by auto |
|
1007 |
have mod_w: "a mod (2 * b) = a mod b + b * w" |
|
1008 |
by (simp add: w_def mod_mult2_eq ac_simps) |
|
1009 |
from assms w_exhaust have "w = 1" |
|
1010 |
using mod_less by (auto simp add: mod_w) |
|
1011 |
with mod_w have mod: "a mod (2 * b) = a mod b + b" by simp |
|
1012 |
have "2 * (a div (2 * b)) = a div b - w" |
|
1013 |
by (simp add: w_def div_mult2_eq minus_mod_eq_mult_div ac_simps) |
|
1014 |
with \<open>w = 1\<close> have div: "2 * (a div (2 * b)) = a div b - 1" by simp |
|
1015 |
then show ?P and ?Q |
|
1016 |
by (simp_all add: div mod add_implies_diff [symmetric]) |
|
1017 |
qed |
|
1018 |
||
1019 |
lemma divmod_digit_0: |
|
1020 |
assumes "0 < b" and "a mod (2 * b) < b" |
|
1021 |
shows "2 * (a div (2 * b)) = a div b" (is "?P") |
|
1022 |
and "a mod (2 * b) = a mod b" (is "?Q") |
|
1023 |
proof - |
|
1024 |
define w where "w = a div b mod 2" |
|
1025 |
then have w_exhaust: "w = 0 \<or> w = 1" by auto |
|
1026 |
have mod_w: "a mod (2 * b) = a mod b + b * w" |
|
1027 |
by (simp add: w_def mod_mult2_eq ac_simps) |
|
1028 |
moreover have "b \<le> a mod b + b" |
|
1029 |
proof - |
|
1030 |
from \<open>0 < b\<close> pos_mod_sign have "0 \<le> a mod b" by blast |
|
1031 |
then have "0 + b \<le> a mod b + b" by (rule add_right_mono) |
|
1032 |
then show ?thesis by simp |
|
1033 |
qed |
|
1034 |
moreover note assms w_exhaust |
|
1035 |
ultimately have "w = 0" by auto |
|
1036 |
with mod_w have mod: "a mod (2 * b) = a mod b" by simp |
|
1037 |
have "2 * (a div (2 * b)) = a div b - w" |
|
1038 |
by (simp add: w_def div_mult2_eq minus_mod_eq_mult_div ac_simps) |
|
1039 |
with \<open>w = 0\<close> have div: "2 * (a div (2 * b)) = a div b" by simp |
|
1040 |
then show ?P and ?Q |
|
1041 |
by (simp_all add: div mod) |
|
1042 |
qed |
|
1043 |
||
1044 |
lemma mod_double_modulus: |
|
1045 |
assumes "m > 0" "x \<ge> 0" |
|
1046 |
shows "x mod (2 * m) = x mod m \<or> x mod (2 * m) = x mod m + m" |
|
1047 |
proof (cases "x mod (2 * m) < m") |
|
1048 |
case True |
|
1049 |
thus ?thesis using assms using divmod_digit_0(2)[of m x] by auto |
|
1050 |
next |
|
1051 |
case False |
|
1052 |
hence *: "x mod (2 * m) - m = x mod m" |
|
1053 |
using assms by (intro divmod_digit_1) auto |
|
1054 |
hence "x mod (2 * m) = x mod m + m" |
|
1055 |
by (subst * [symmetric], subst le_add_diff_inverse2) (use False in auto) |
|
1056 |
thus ?thesis by simp |
|
1057 |
qed |
|
1058 |
||
1059 |
end |
|
1060 |
||
1061 |
hide_fact (open) div_less mod_less mod_less_eq_dividend mod_mult2_eq div_mult2_eq |
|
1062 |
||
1063 |
instance nat :: unique_euclidean_semiring_numeral |
|
1064 |
by standard |
|
1065 |
(auto simp add: div_greater_zero_iff div_mult2_eq mod_mult2_eq) |
|
1066 |
||
1067 |
instance int :: unique_euclidean_semiring_numeral |
|
1068 |
by standard (auto intro: zmod_le_nonneg_dividend simp add: |
|
1069 |
pos_imp_zdiv_pos_iff zmod_zmult2_eq zdiv_zmult2_eq) |
|
1070 |
||
68631 | 1071 |
lemma div_geq: "m div n = Suc ((m - n) div n)" if "0 < n" and " \<not> m < n" for m n :: nat |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
1072 |
by (rule le_div_geq) (use that in \<open>simp_all add: not_less\<close>) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
1073 |
|
68631 | 1074 |
lemma mod_geq: "m mod n = (m - n) mod n" if "\<not> m < n" for m n :: nat |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
1075 |
by (rule le_mod_geq) (use that in \<open>simp add: not_less\<close>) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66806
diff
changeset
|
1076 |
|
68631 | 1077 |
lemma mod_eq_0D: "\<exists>q. m = d * q" if "m mod d = 0" for m d :: nat |
1078 |
using that by (auto simp add: mod_eq_0_iff_dvd) |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
1079 |
|
69695 | 1080 |
lemma pos_mod_conj: "0 < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b" for a b :: int |
1081 |
by simp |
|
1082 |
||
1083 |
lemma neg_mod_conj: "b < 0 \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b" for a b :: int |
|
1084 |
by simp |
|
1085 |
||
1086 |
lemma zmod_eq_0_iff: "m mod d = 0 \<longleftrightarrow> (\<exists>q. m = d * q)" for m d :: int |
|
1087 |
by (auto simp add: mod_eq_0_iff_dvd) |
|
1088 |
||
1089 |
(* REVISIT: should this be generalized to all semiring_div types? *) |
|
1090 |
lemma zmod_eq_0D [dest!]: "\<exists>q. m = d * q" if "m mod d = 0" for m d :: int |
|
1091 |
using that by auto |
|
1092 |
||
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33340
diff
changeset
|
1093 |
end |