author | haftmann |
Fri, 19 Aug 2022 05:49:12 +0000 | |
changeset 75881 | 83e4b6a5e7de |
parent 75880 | 714fad33252e |
child 75937 | 02b18f59f903 |
permissions | -rw-r--r-- |
64785 | 1 |
(* Title: HOL/Euclidean_Division.thy |
2 |
Author: Manuel Eberl, TU Muenchen |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
*) |
|
5 |
||
66817 | 6 |
section \<open>Division in euclidean (semi)rings\<close> |
64785 | 7 |
|
8 |
theory Euclidean_Division |
|
66817 | 9 |
imports Int Lattices_Big |
64785 | 10 |
begin |
11 |
||
12 |
subsection \<open>Euclidean (semi)rings with explicit division and remainder\<close> |
|
13 |
||
66817 | 14 |
class euclidean_semiring = semidom_modulo + |
64785 | 15 |
fixes euclidean_size :: "'a \<Rightarrow> nat" |
16 |
assumes size_0 [simp]: "euclidean_size 0 = 0" |
|
17 |
assumes mod_size_less: |
|
18 |
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b" |
|
19 |
assumes size_mult_mono: |
|
20 |
"b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)" |
|
21 |
begin |
|
22 |
||
66840 | 23 |
lemma euclidean_size_eq_0_iff [simp]: |
24 |
"euclidean_size b = 0 \<longleftrightarrow> b = 0" |
|
25 |
proof |
|
26 |
assume "b = 0" |
|
27 |
then show "euclidean_size b = 0" |
|
28 |
by simp |
|
29 |
next |
|
30 |
assume "euclidean_size b = 0" |
|
31 |
show "b = 0" |
|
32 |
proof (rule ccontr) |
|
33 |
assume "b \<noteq> 0" |
|
34 |
with mod_size_less have "euclidean_size (b mod b) < euclidean_size b" . |
|
35 |
with \<open>euclidean_size b = 0\<close> show False |
|
36 |
by simp |
|
37 |
qed |
|
38 |
qed |
|
39 |
||
40 |
lemma euclidean_size_greater_0_iff [simp]: |
|
41 |
"euclidean_size b > 0 \<longleftrightarrow> b \<noteq> 0" |
|
42 |
using euclidean_size_eq_0_iff [symmetric, of b] by safe simp |
|
43 |
||
64785 | 44 |
lemma size_mult_mono': "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (b * a)" |
45 |
by (subst mult.commute) (rule size_mult_mono) |
|
46 |
||
47 |
lemma dvd_euclidean_size_eq_imp_dvd: |
|
48 |
assumes "a \<noteq> 0" and "euclidean_size a = euclidean_size b" |
|
49 |
and "b dvd a" |
|
50 |
shows "a dvd b" |
|
51 |
proof (rule ccontr) |
|
52 |
assume "\<not> a dvd b" |
|
53 |
hence "b mod a \<noteq> 0" using mod_0_imp_dvd [of b a] by blast |
|
54 |
then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd) |
|
55 |
from \<open>b dvd a\<close> have "b dvd b mod a" by (simp add: dvd_mod_iff) |
|
56 |
then obtain c where "b mod a = b * c" unfolding dvd_def by blast |
|
57 |
with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto |
|
58 |
with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b" |
|
59 |
using size_mult_mono by force |
|
60 |
moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close> |
|
61 |
have "euclidean_size (b mod a) < euclidean_size a" |
|
62 |
using mod_size_less by blast |
|
63 |
ultimately show False using \<open>euclidean_size a = euclidean_size b\<close> |
|
64 |
by simp |
|
65 |
qed |
|
66 |
||
67 |
lemma euclidean_size_times_unit: |
|
68 |
assumes "is_unit a" |
|
69 |
shows "euclidean_size (a * b) = euclidean_size b" |
|
70 |
proof (rule antisym) |
|
71 |
from assms have [simp]: "a \<noteq> 0" by auto |
|
72 |
thus "euclidean_size (a * b) \<ge> euclidean_size b" by (rule size_mult_mono') |
|
73 |
from assms have "is_unit (1 div a)" by simp |
|
74 |
hence "1 div a \<noteq> 0" by (intro notI) simp_all |
|
75 |
hence "euclidean_size (a * b) \<le> euclidean_size ((1 div a) * (a * b))" |
|
76 |
by (rule size_mult_mono') |
|
77 |
also from assms have "(1 div a) * (a * b) = b" |
|
78 |
by (simp add: algebra_simps unit_div_mult_swap) |
|
79 |
finally show "euclidean_size (a * b) \<le> euclidean_size b" . |
|
80 |
qed |
|
81 |
||
82 |
lemma euclidean_size_unit: |
|
83 |
"is_unit a \<Longrightarrow> euclidean_size a = euclidean_size 1" |
|
84 |
using euclidean_size_times_unit [of a 1] by simp |
|
85 |
||
86 |
lemma unit_iff_euclidean_size: |
|
87 |
"is_unit a \<longleftrightarrow> euclidean_size a = euclidean_size 1 \<and> a \<noteq> 0" |
|
88 |
proof safe |
|
89 |
assume A: "a \<noteq> 0" and B: "euclidean_size a = euclidean_size 1" |
|
90 |
show "is_unit a" |
|
91 |
by (rule dvd_euclidean_size_eq_imp_dvd [OF A B]) simp_all |
|
92 |
qed (auto intro: euclidean_size_unit) |
|
93 |
||
94 |
lemma euclidean_size_times_nonunit: |
|
95 |
assumes "a \<noteq> 0" "b \<noteq> 0" "\<not> is_unit a" |
|
96 |
shows "euclidean_size b < euclidean_size (a * b)" |
|
97 |
proof (rule ccontr) |
|
98 |
assume "\<not>euclidean_size b < euclidean_size (a * b)" |
|
99 |
with size_mult_mono'[OF assms(1), of b] |
|
100 |
have eq: "euclidean_size (a * b) = euclidean_size b" by simp |
|
101 |
have "a * b dvd b" |
|
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74592
diff
changeset
|
102 |
by (rule dvd_euclidean_size_eq_imp_dvd [OF _ eq]) |
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74592
diff
changeset
|
103 |
(use assms in simp_all) |
64785 | 104 |
hence "a * b dvd 1 * b" by simp |
105 |
with \<open>b \<noteq> 0\<close> have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff) |
|
106 |
with assms(3) show False by contradiction |
|
107 |
qed |
|
108 |
||
109 |
lemma dvd_imp_size_le: |
|
110 |
assumes "a dvd b" "b \<noteq> 0" |
|
111 |
shows "euclidean_size a \<le> euclidean_size b" |
|
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74592
diff
changeset
|
112 |
using assms by (auto simp: size_mult_mono) |
64785 | 113 |
|
114 |
lemma dvd_proper_imp_size_less: |
|
115 |
assumes "a dvd b" "\<not> b dvd a" "b \<noteq> 0" |
|
116 |
shows "euclidean_size a < euclidean_size b" |
|
117 |
proof - |
|
118 |
from assms(1) obtain c where "b = a * c" by (erule dvdE) |
|
119 |
hence z: "b = c * a" by (simp add: mult.commute) |
|
120 |
from z assms have "\<not>is_unit c" by (auto simp: mult.commute mult_unit_dvd_iff) |
|
121 |
with z assms show ?thesis |
|
122 |
by (auto intro!: euclidean_size_times_nonunit) |
|
123 |
qed |
|
124 |
||
66798 | 125 |
lemma unit_imp_mod_eq_0: |
126 |
"a mod b = 0" if "is_unit b" |
|
127 |
using that by (simp add: mod_eq_0_iff_dvd unit_imp_dvd) |
|
128 |
||
69695 | 129 |
lemma mod_eq_self_iff_div_eq_0: |
130 |
"a mod b = a \<longleftrightarrow> a div b = 0" (is "?P \<longleftrightarrow> ?Q") |
|
131 |
proof |
|
132 |
assume ?P |
|
133 |
with div_mult_mod_eq [of a b] show ?Q |
|
134 |
by auto |
|
135 |
next |
|
136 |
assume ?Q |
|
137 |
with div_mult_mod_eq [of a b] show ?P |
|
138 |
by simp |
|
139 |
qed |
|
140 |
||
67051 | 141 |
lemma coprime_mod_left_iff [simp]: |
142 |
"coprime (a mod b) b \<longleftrightarrow> coprime a b" if "b \<noteq> 0" |
|
75669
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents:
74592
diff
changeset
|
143 |
by (rule iffI; rule coprimeI) |
67051 | 144 |
(use that in \<open>auto dest!: dvd_mod_imp_dvd coprime_common_divisor simp add: dvd_mod_iff\<close>) |
145 |
||
146 |
lemma coprime_mod_right_iff [simp]: |
|
147 |
"coprime a (b mod a) \<longleftrightarrow> coprime a b" if "a \<noteq> 0" |
|
148 |
using that coprime_mod_left_iff [of a b] by (simp add: ac_simps) |
|
149 |
||
64785 | 150 |
end |
151 |
||
152 |
class euclidean_ring = idom_modulo + euclidean_semiring |
|
66886 | 153 |
begin |
154 |
||
67087 | 155 |
lemma dvd_diff_commute [ac_simps]: |
66886 | 156 |
"a dvd c - b \<longleftrightarrow> a dvd b - c" |
157 |
proof - |
|
158 |
have "a dvd c - b \<longleftrightarrow> a dvd (c - b) * - 1" |
|
159 |
by (subst dvd_mult_unit_iff) simp_all |
|
160 |
then show ?thesis |
|
161 |
by simp |
|
162 |
qed |
|
163 |
||
164 |
end |
|
64785 | 165 |
|
66840 | 166 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
167 |
subsection \<open>Euclidean (semi)rings with cancel rules\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
168 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
169 |
class euclidean_semiring_cancel = euclidean_semiring + |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
170 |
assumes div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
171 |
and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b" |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
172 |
begin |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
173 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
174 |
lemma div_mult_self2 [simp]: |
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
175 |
assumes "b \<noteq> 0" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
176 |
shows "(a + b * c) div b = c + a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
177 |
using assms div_mult_self1 [of b a c] by (simp add: mult.commute) |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
178 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
179 |
lemma div_mult_self3 [simp]: |
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
180 |
assumes "b \<noteq> 0" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
181 |
shows "(c * b + a) div b = c + a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
182 |
using assms by (simp add: add.commute) |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
183 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
184 |
lemma div_mult_self4 [simp]: |
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
185 |
assumes "b \<noteq> 0" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
186 |
shows "(b * c + a) div b = c + a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
187 |
using assms by (simp add: add.commute) |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
188 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
189 |
lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
190 |
proof (cases "b = 0") |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
191 |
case True then show ?thesis by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
192 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
193 |
case False |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
194 |
have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
195 |
by (simp add: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
196 |
also from False div_mult_self1 [of b a c] have |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
197 |
"\<dots> = (c + a div b) * b + (a + c * b) mod b" |
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
198 |
by (simp add: algebra_simps) |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
199 |
finally have "a = a div b * b + (a + c * b) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
200 |
by (simp add: add.commute [of a] add.assoc distrib_right) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
201 |
then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
202 |
by (simp add: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
203 |
then show ?thesis by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
204 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
205 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
206 |
lemma mod_mult_self2 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
207 |
"(a + b * c) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
208 |
by (simp add: mult.commute [of b]) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
209 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
210 |
lemma mod_mult_self3 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
211 |
"(c * b + a) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
212 |
by (simp add: add.commute) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
213 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
214 |
lemma mod_mult_self4 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
215 |
"(b * c + a) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
216 |
by (simp add: add.commute) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
217 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
218 |
lemma mod_mult_self1_is_0 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
219 |
"b * a mod b = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
220 |
using mod_mult_self2 [of 0 b a] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
221 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
222 |
lemma mod_mult_self2_is_0 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
223 |
"a * b mod b = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
224 |
using mod_mult_self1 [of 0 a b] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
225 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
226 |
lemma div_add_self1: |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
227 |
assumes "b \<noteq> 0" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
228 |
shows "(b + a) div b = a div b + 1" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
229 |
using assms div_mult_self1 [of b a 1] by (simp add: add.commute) |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
230 |
|
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
231 |
lemma div_add_self2: |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
232 |
assumes "b \<noteq> 0" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
233 |
shows "(a + b) div b = a div b + 1" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
234 |
using assms div_add_self1 [of b a] by (simp add: add.commute) |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
235 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
236 |
lemma mod_add_self1 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
237 |
"(b + a) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
238 |
using mod_mult_self1 [of a 1 b] by (simp add: add.commute) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
239 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
240 |
lemma mod_add_self2 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
241 |
"(a + b) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
242 |
using mod_mult_self1 [of a 1 b] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
243 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
244 |
lemma mod_div_trivial [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
245 |
"a mod b div b = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
246 |
proof (cases "b = 0") |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
247 |
assume "b = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
248 |
thus ?thesis by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
249 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
250 |
assume "b \<noteq> 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
251 |
hence "a div b + a mod b div b = (a mod b + a div b * b) div b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
252 |
by (rule div_mult_self1 [symmetric]) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
253 |
also have "\<dots> = a div b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
254 |
by (simp only: mod_div_mult_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
255 |
also have "\<dots> = a div b + 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
256 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
257 |
finally show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
258 |
by (rule add_left_imp_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
259 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
260 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
261 |
lemma mod_mod_trivial [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
262 |
"a mod b mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
263 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
264 |
have "a mod b mod b = (a mod b + a div b * b) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
265 |
by (simp only: mod_mult_self1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
266 |
also have "\<dots> = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
267 |
by (simp only: mod_div_mult_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
268 |
finally show ?thesis . |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
269 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
270 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
271 |
lemma mod_mod_cancel: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
272 |
assumes "c dvd b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
273 |
shows "a mod b mod c = a mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
274 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
275 |
from \<open>c dvd b\<close> obtain k where "b = c * k" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
276 |
by (rule dvdE) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
277 |
have "a mod b mod c = a mod (c * k) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
278 |
by (simp only: \<open>b = c * k\<close>) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
279 |
also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
280 |
by (simp only: mod_mult_self1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
281 |
also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
282 |
by (simp only: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
283 |
also have "\<dots> = a mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
284 |
by (simp only: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
285 |
finally show ?thesis . |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
286 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
287 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
288 |
lemma div_mult_mult2 [simp]: |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
289 |
"c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
290 |
by (drule div_mult_mult1) (simp add: mult.commute) |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
291 |
|
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
292 |
lemma div_mult_mult1_if [simp]: |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
293 |
"(c * a) div (c * b) = (if c = 0 then 0 else a div b)" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
294 |
by simp_all |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
295 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
296 |
lemma mod_mult_mult1: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
297 |
"(c * a) mod (c * b) = c * (a mod b)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
298 |
proof (cases "c = 0") |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
299 |
case True then show ?thesis by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
300 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
301 |
case False |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
302 |
from div_mult_mod_eq |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
303 |
have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" . |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
304 |
with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
305 |
= c * a + c * (a mod b)" by (simp add: algebra_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
306 |
with div_mult_mod_eq show ?thesis by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
307 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
308 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
309 |
lemma mod_mult_mult2: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
310 |
"(a * c) mod (b * c) = (a mod b) * c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
311 |
using mod_mult_mult1 [of c a b] by (simp add: mult.commute) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
312 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
313 |
lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
314 |
by (fact mod_mult_mult2 [symmetric]) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
315 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
316 |
lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
317 |
by (fact mod_mult_mult1 [symmetric]) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
318 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
319 |
lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
320 |
unfolding dvd_def by (auto simp add: mod_mult_mult1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
321 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
322 |
lemma div_plus_div_distrib_dvd_left: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
323 |
"c dvd a \<Longrightarrow> (a + b) div c = a div c + b div c" |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
324 |
by (cases "c = 0") auto |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
325 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
326 |
lemma div_plus_div_distrib_dvd_right: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
327 |
"c dvd b \<Longrightarrow> (a + b) div c = a div c + b div c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
328 |
using div_plus_div_distrib_dvd_left [of c b a] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
329 |
by (simp add: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
330 |
|
71413 | 331 |
lemma sum_div_partition: |
332 |
\<open>(\<Sum>a\<in>A. f a) div b = (\<Sum>a\<in>A \<inter> {a. b dvd f a}. f a div b) + (\<Sum>a\<in>A \<inter> {a. \<not> b dvd f a}. f a) div b\<close> |
|
333 |
if \<open>finite A\<close> |
|
334 |
proof - |
|
335 |
have \<open>A = A \<inter> {a. b dvd f a} \<union> A \<inter> {a. \<not> b dvd f a}\<close> |
|
336 |
by auto |
|
337 |
then have \<open>(\<Sum>a\<in>A. f a) = (\<Sum>a\<in>A \<inter> {a. b dvd f a} \<union> A \<inter> {a. \<not> b dvd f a}. f a)\<close> |
|
338 |
by simp |
|
339 |
also have \<open>\<dots> = (\<Sum>a\<in>A \<inter> {a. b dvd f a}. f a) + (\<Sum>a\<in>A \<inter> {a. \<not> b dvd f a}. f a)\<close> |
|
340 |
using \<open>finite A\<close> by (auto intro: sum.union_inter_neutral) |
|
341 |
finally have *: \<open>sum f A = sum f (A \<inter> {a. b dvd f a}) + sum f (A \<inter> {a. \<not> b dvd f a})\<close> . |
|
342 |
define B where B: \<open>B = A \<inter> {a. b dvd f a}\<close> |
|
343 |
with \<open>finite A\<close> have \<open>finite B\<close> and \<open>a \<in> B \<Longrightarrow> b dvd f a\<close> for a |
|
344 |
by simp_all |
|
345 |
then have \<open>(\<Sum>a\<in>B. f a) div b = (\<Sum>a\<in>B. f a div b)\<close> and \<open>b dvd (\<Sum>a\<in>B. f a)\<close> |
|
346 |
by induction (simp_all add: div_plus_div_distrib_dvd_left) |
|
347 |
then show ?thesis using * |
|
348 |
by (simp add: B div_plus_div_distrib_dvd_left) |
|
349 |
qed |
|
350 |
||
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
351 |
named_theorems mod_simps |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
352 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
353 |
text \<open>Addition respects modular equivalence.\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
354 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
355 |
lemma mod_add_left_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
356 |
"(a mod c + b) mod c = (a + b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
357 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
358 |
have "(a + b) mod c = (a div c * c + a mod c + b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
359 |
by (simp only: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
360 |
also have "\<dots> = (a mod c + b + a div c * c) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
361 |
by (simp only: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
362 |
also have "\<dots> = (a mod c + b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
363 |
by (rule mod_mult_self1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
364 |
finally show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
365 |
by (rule sym) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
366 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
367 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
368 |
lemma mod_add_right_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
369 |
"(a + b mod c) mod c = (a + b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
370 |
using mod_add_left_eq [of b c a] by (simp add: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
371 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
372 |
lemma mod_add_eq: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
373 |
"(a mod c + b mod c) mod c = (a + b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
374 |
by (simp add: mod_add_left_eq mod_add_right_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
375 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
376 |
lemma mod_sum_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
377 |
"(\<Sum>i\<in>A. f i mod a) mod a = sum f A mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
378 |
proof (induct A rule: infinite_finite_induct) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
379 |
case (insert i A) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
380 |
then have "(\<Sum>i\<in>insert i A. f i mod a) mod a |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
381 |
= (f i mod a + (\<Sum>i\<in>A. f i mod a)) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
382 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
383 |
also have "\<dots> = (f i + (\<Sum>i\<in>A. f i mod a) mod a) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
384 |
by (simp add: mod_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
385 |
also have "\<dots> = (f i + (\<Sum>i\<in>A. f i) mod a) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
386 |
by (simp add: insert.hyps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
387 |
finally show ?case |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
388 |
by (simp add: insert.hyps mod_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
389 |
qed simp_all |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
390 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
391 |
lemma mod_add_cong: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
392 |
assumes "a mod c = a' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
393 |
assumes "b mod c = b' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
394 |
shows "(a + b) mod c = (a' + b') mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
395 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
396 |
have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
397 |
unfolding assms .. |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
398 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
399 |
by (simp add: mod_add_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
400 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
401 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
402 |
text \<open>Multiplication respects modular equivalence.\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
403 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
404 |
lemma mod_mult_left_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
405 |
"((a mod c) * b) mod c = (a * b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
406 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
407 |
have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
408 |
by (simp only: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
409 |
also have "\<dots> = (a mod c * b + a div c * b * c) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
410 |
by (simp only: algebra_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
411 |
also have "\<dots> = (a mod c * b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
412 |
by (rule mod_mult_self1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
413 |
finally show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
414 |
by (rule sym) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
415 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
416 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
417 |
lemma mod_mult_right_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
418 |
"(a * (b mod c)) mod c = (a * b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
419 |
using mod_mult_left_eq [of b c a] by (simp add: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
420 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
421 |
lemma mod_mult_eq: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
422 |
"((a mod c) * (b mod c)) mod c = (a * b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
423 |
by (simp add: mod_mult_left_eq mod_mult_right_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
424 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
425 |
lemma mod_prod_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
426 |
"(\<Prod>i\<in>A. f i mod a) mod a = prod f A mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
427 |
proof (induct A rule: infinite_finite_induct) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
428 |
case (insert i A) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
429 |
then have "(\<Prod>i\<in>insert i A. f i mod a) mod a |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
430 |
= (f i mod a * (\<Prod>i\<in>A. f i mod a)) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
431 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
432 |
also have "\<dots> = (f i * ((\<Prod>i\<in>A. f i mod a) mod a)) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
433 |
by (simp add: mod_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
434 |
also have "\<dots> = (f i * ((\<Prod>i\<in>A. f i) mod a)) mod a" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
435 |
by (simp add: insert.hyps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
436 |
finally show ?case |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
437 |
by (simp add: insert.hyps mod_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
438 |
qed simp_all |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
439 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
440 |
lemma mod_mult_cong: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
441 |
assumes "a mod c = a' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
442 |
assumes "b mod c = b' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
443 |
shows "(a * b) mod c = (a' * b') mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
444 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
445 |
have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
446 |
unfolding assms .. |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
447 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
448 |
by (simp add: mod_mult_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
449 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
450 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
451 |
text \<open>Exponentiation respects modular equivalence.\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
452 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
453 |
lemma power_mod [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
454 |
"((a mod b) ^ n) mod b = (a ^ n) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
455 |
proof (induct n) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
456 |
case 0 |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
457 |
then show ?case by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
458 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
459 |
case (Suc n) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
460 |
have "(a mod b) ^ Suc n mod b = (a mod b) * ((a mod b) ^ n mod b) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
461 |
by (simp add: mod_mult_right_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
462 |
with Suc show ?case |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
463 |
by (simp add: mod_mult_left_eq mod_mult_right_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
464 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
465 |
|
71413 | 466 |
lemma power_diff_power_eq: |
467 |
\<open>a ^ m div a ^ n = (if n \<le> m then a ^ (m - n) else 1 div a ^ (n - m))\<close> |
|
468 |
if \<open>a \<noteq> 0\<close> |
|
469 |
proof (cases \<open>n \<le> m\<close>) |
|
470 |
case True |
|
471 |
with that power_diff [symmetric, of a n m] show ?thesis by simp |
|
472 |
next |
|
473 |
case False |
|
474 |
then obtain q where n: \<open>n = m + Suc q\<close> |
|
475 |
by (auto simp add: not_le dest: less_imp_Suc_add) |
|
476 |
then have \<open>a ^ m div a ^ n = (a ^ m * 1) div (a ^ m * a ^ Suc q)\<close> |
|
477 |
by (simp add: power_add ac_simps) |
|
478 |
moreover from that have \<open>a ^ m \<noteq> 0\<close> |
|
479 |
by simp |
|
480 |
ultimately have \<open>a ^ m div a ^ n = 1 div a ^ Suc q\<close> |
|
481 |
by (subst (asm) div_mult_mult1) simp |
|
482 |
with False n show ?thesis |
|
483 |
by simp |
|
484 |
qed |
|
485 |
||
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
486 |
end |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
487 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
488 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
489 |
class euclidean_ring_cancel = euclidean_ring + euclidean_semiring_cancel |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
490 |
begin |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
491 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
492 |
subclass idom_divide .. |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
493 |
|
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
494 |
lemma div_minus_minus [simp]: "(- a) div (- b) = a div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
495 |
using div_mult_mult1 [of "- 1" a b] by simp |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
496 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
497 |
lemma mod_minus_minus [simp]: "(- a) mod (- b) = - (a mod b)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
498 |
using mod_mult_mult1 [of "- 1" a b] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
499 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
500 |
lemma div_minus_right: "a div (- b) = (- a) div b" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
501 |
using div_minus_minus [of "- a" b] by simp |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
502 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
503 |
lemma mod_minus_right: "a mod (- b) = - ((- a) mod b)" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
504 |
using mod_minus_minus [of "- a" b] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
505 |
|
70147
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
506 |
lemma div_minus1_right [simp]: "a div (- 1) = - a" |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
507 |
using div_minus_right [of a 1] by simp |
1657688a6406
backed out a93e6472ac9c, which does not bring anything substantial: division_ring is not commutative in multiplication but semidom_divide is
haftmann
parents:
70094
diff
changeset
|
508 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
509 |
lemma mod_minus1_right [simp]: "a mod (- 1) = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
510 |
using mod_minus_right [of a 1] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
511 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
512 |
text \<open>Negation respects modular equivalence.\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
513 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
514 |
lemma mod_minus_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
515 |
"(- (a mod b)) mod b = (- a) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
516 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
517 |
have "(- a) mod b = (- (a div b * b + a mod b)) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
518 |
by (simp only: div_mult_mod_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
519 |
also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
520 |
by (simp add: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
521 |
also have "\<dots> = (- (a mod b)) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
522 |
by (rule mod_mult_self1) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
523 |
finally show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
524 |
by (rule sym) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
525 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
526 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
527 |
lemma mod_minus_cong: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
528 |
assumes "a mod b = a' mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
529 |
shows "(- a) mod b = (- a') mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
530 |
proof - |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
531 |
have "(- (a mod b)) mod b = (- (a' mod b)) mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
532 |
unfolding assms .. |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
533 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
534 |
by (simp add: mod_minus_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
535 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
536 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
537 |
text \<open>Subtraction respects modular equivalence.\<close> |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
538 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
539 |
lemma mod_diff_left_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
540 |
"(a mod c - b) mod c = (a - b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
541 |
using mod_add_cong [of a c "a mod c" "- b" "- b"] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
542 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
543 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
544 |
lemma mod_diff_right_eq [mod_simps]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
545 |
"(a - b mod c) mod c = (a - b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
546 |
using mod_add_cong [of a c a "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
547 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
548 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
549 |
lemma mod_diff_eq: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
550 |
"(a mod c - b mod c) mod c = (a - b) mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
551 |
using mod_add_cong [of a c "a mod c" "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
552 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
553 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
554 |
lemma mod_diff_cong: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
555 |
assumes "a mod c = a' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
556 |
assumes "b mod c = b' mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
557 |
shows "(a - b) mod c = (a' - b') mod c" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
558 |
using assms mod_add_cong [of a c a' "- b" "- b'"] mod_minus_cong [of b c "b'"] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
559 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
560 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
561 |
lemma minus_mod_self2 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
562 |
"(a - b) mod b = a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
563 |
using mod_diff_right_eq [of a b b] |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
564 |
by (simp add: mod_diff_right_eq) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
565 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
566 |
lemma minus_mod_self1 [simp]: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
567 |
"(b - a) mod b = - a mod b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
568 |
using mod_add_self2 [of "- a" b] by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
569 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
570 |
lemma mod_eq_dvd_iff: |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
571 |
"a mod c = b mod c \<longleftrightarrow> c dvd a - b" (is "?P \<longleftrightarrow> ?Q") |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
572 |
proof |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
573 |
assume ?P |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
574 |
then have "(a mod c - b mod c) mod c = 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
575 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
576 |
then show ?Q |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
577 |
by (simp add: dvd_eq_mod_eq_0 mod_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
578 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
579 |
assume ?Q |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
580 |
then obtain d where d: "a - b = c * d" .. |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
581 |
then have "a = c * d + b" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
582 |
by (simp add: algebra_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
583 |
then show ?P by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
584 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
585 |
|
66837 | 586 |
lemma mod_eqE: |
587 |
assumes "a mod c = b mod c" |
|
588 |
obtains d where "b = a + c * d" |
|
589 |
proof - |
|
590 |
from assms have "c dvd a - b" |
|
591 |
by (simp add: mod_eq_dvd_iff) |
|
592 |
then obtain d where "a - b = c * d" .. |
|
593 |
then have "b = a + c * - d" |
|
594 |
by (simp add: algebra_simps) |
|
595 |
with that show thesis . |
|
596 |
qed |
|
597 |
||
67051 | 598 |
lemma invertible_coprime: |
599 |
"coprime a c" if "a * b mod c = 1" |
|
600 |
by (rule coprimeI) (use that dvd_mod_iff [of _ c "a * b"] in auto) |
|
601 |
||
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
602 |
end |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
603 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
604 |
|
64785 | 605 |
subsection \<open>Uniquely determined division\<close> |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
606 |
|
64785 | 607 |
class unique_euclidean_semiring = euclidean_semiring + |
66840 | 608 |
assumes euclidean_size_mult: "euclidean_size (a * b) = euclidean_size a * euclidean_size b" |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
609 |
fixes division_segment :: "'a \<Rightarrow> 'a" |
66839 | 610 |
assumes is_unit_division_segment [simp]: "is_unit (division_segment a)" |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
611 |
and division_segment_mult: |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
612 |
"a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> division_segment (a * b) = division_segment a * division_segment b" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
613 |
and division_segment_mod: |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
614 |
"b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> division_segment (a mod b) = division_segment b" |
64785 | 615 |
assumes div_bounded: |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
616 |
"b \<noteq> 0 \<Longrightarrow> division_segment r = division_segment b |
64785 | 617 |
\<Longrightarrow> euclidean_size r < euclidean_size b |
618 |
\<Longrightarrow> (q * b + r) div b = q" |
|
619 |
begin |
|
620 |
||
66839 | 621 |
lemma division_segment_not_0 [simp]: |
622 |
"division_segment a \<noteq> 0" |
|
623 |
using is_unit_division_segment [of a] is_unitE [of "division_segment a"] by blast |
|
624 |
||
64785 | 625 |
lemma divmod_cases [case_names divides remainder by0]: |
626 |
obtains |
|
627 |
(divides) q where "b \<noteq> 0" |
|
628 |
and "a div b = q" |
|
629 |
and "a mod b = 0" |
|
630 |
and "a = q * b" |
|
66814 | 631 |
| (remainder) q r where "b \<noteq> 0" |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
632 |
and "division_segment r = division_segment b" |
64785 | 633 |
and "euclidean_size r < euclidean_size b" |
66814 | 634 |
and "r \<noteq> 0" |
64785 | 635 |
and "a div b = q" |
636 |
and "a mod b = r" |
|
637 |
and "a = q * b + r" |
|
638 |
| (by0) "b = 0" |
|
639 |
proof (cases "b = 0") |
|
640 |
case True |
|
641 |
then show thesis |
|
642 |
by (rule by0) |
|
643 |
next |
|
644 |
case False |
|
645 |
show thesis |
|
646 |
proof (cases "b dvd a") |
|
647 |
case True |
|
648 |
then obtain q where "a = b * q" .. |
|
649 |
with \<open>b \<noteq> 0\<close> divides |
|
650 |
show thesis |
|
651 |
by (simp add: ac_simps) |
|
652 |
next |
|
653 |
case False |
|
654 |
then have "a mod b \<noteq> 0" |
|
655 |
by (simp add: mod_eq_0_iff_dvd) |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
656 |
moreover from \<open>b \<noteq> 0\<close> \<open>\<not> b dvd a\<close> have "division_segment (a mod b) = division_segment b" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
657 |
by (rule division_segment_mod) |
64785 | 658 |
moreover have "euclidean_size (a mod b) < euclidean_size b" |
659 |
using \<open>b \<noteq> 0\<close> by (rule mod_size_less) |
|
660 |
moreover have "a = a div b * b + a mod b" |
|
661 |
by (simp add: div_mult_mod_eq) |
|
662 |
ultimately show thesis |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
663 |
using \<open>b \<noteq> 0\<close> by (blast intro!: remainder) |
64785 | 664 |
qed |
665 |
qed |
|
666 |
||
667 |
lemma div_eqI: |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
668 |
"a div b = q" if "b \<noteq> 0" "division_segment r = division_segment b" |
64785 | 669 |
"euclidean_size r < euclidean_size b" "q * b + r = a" |
670 |
proof - |
|
671 |
from that have "(q * b + r) div b = q" |
|
672 |
by (auto intro: div_bounded) |
|
673 |
with that show ?thesis |
|
674 |
by simp |
|
675 |
qed |
|
676 |
||
677 |
lemma mod_eqI: |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
678 |
"a mod b = r" if "b \<noteq> 0" "division_segment r = division_segment b" |
64785 | 679 |
"euclidean_size r < euclidean_size b" "q * b + r = a" |
680 |
proof - |
|
681 |
from that have "a div b = q" |
|
682 |
by (rule div_eqI) |
|
683 |
moreover have "a div b * b + a mod b = a" |
|
684 |
by (fact div_mult_mod_eq) |
|
685 |
ultimately have "a div b * b + a mod b = a div b * b + r" |
|
686 |
using \<open>q * b + r = a\<close> by simp |
|
687 |
then show ?thesis |
|
688 |
by simp |
|
689 |
qed |
|
690 |
||
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
691 |
subclass euclidean_semiring_cancel |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
692 |
proof |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
693 |
show "(a + c * b) div b = c + a div b" if "b \<noteq> 0" for a b c |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
694 |
proof (cases a b rule: divmod_cases) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
695 |
case by0 |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
696 |
with \<open>b \<noteq> 0\<close> show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
697 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
698 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
699 |
case (divides q) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
700 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
701 |
by (simp add: ac_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
702 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
703 |
case (remainder q r) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
704 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
705 |
by (auto intro: div_eqI simp add: algebra_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
706 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
707 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
708 |
show"(c * a) div (c * b) = a div b" if "c \<noteq> 0" for a b c |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
709 |
proof (cases a b rule: divmod_cases) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
710 |
case by0 |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
711 |
then show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
712 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
713 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
714 |
case (divides q) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
715 |
with \<open>c \<noteq> 0\<close> show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
716 |
by (simp add: mult.left_commute [of c]) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
717 |
next |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
718 |
case (remainder q r) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
719 |
from \<open>b \<noteq> 0\<close> \<open>c \<noteq> 0\<close> have "b * c \<noteq> 0" |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
720 |
by simp |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
721 |
from remainder \<open>c \<noteq> 0\<close> |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
722 |
have "division_segment (r * c) = division_segment (b * c)" |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
723 |
and "euclidean_size (r * c) < euclidean_size (b * c)" |
66840 | 724 |
by (simp_all add: division_segment_mult division_segment_mod euclidean_size_mult) |
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
725 |
with remainder show ?thesis |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
726 |
by (auto intro!: div_eqI [of _ "c * (a mod b)"] simp add: algebra_simps) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
727 |
(use \<open>b * c \<noteq> 0\<close> in simp) |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
728 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
729 |
qed |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
730 |
|
66814 | 731 |
lemma div_mult1_eq: |
732 |
"(a * b) div c = a * (b div c) + a * (b mod c) div c" |
|
733 |
proof (cases "a * (b mod c)" c rule: divmod_cases) |
|
734 |
case (divides q) |
|
735 |
have "a * b = a * (b div c * c + b mod c)" |
|
736 |
by (simp add: div_mult_mod_eq) |
|
737 |
also have "\<dots> = (a * (b div c) + q) * c" |
|
738 |
using divides by (simp add: algebra_simps) |
|
739 |
finally have "(a * b) div c = \<dots> div c" |
|
740 |
by simp |
|
741 |
with divides show ?thesis |
|
742 |
by simp |
|
743 |
next |
|
744 |
case (remainder q r) |
|
745 |
from remainder(1-3) show ?thesis |
|
746 |
proof (rule div_eqI) |
|
747 |
have "a * b = a * (b div c * c + b mod c)" |
|
748 |
by (simp add: div_mult_mod_eq) |
|
749 |
also have "\<dots> = a * c * (b div c) + q * c + r" |
|
750 |
using remainder by (simp add: algebra_simps) |
|
751 |
finally show "(a * (b div c) + a * (b mod c) div c) * c + r = a * b" |
|
752 |
using remainder(5-7) by (simp add: algebra_simps) |
|
753 |
qed |
|
754 |
next |
|
755 |
case by0 |
|
756 |
then show ?thesis |
|
757 |
by simp |
|
758 |
qed |
|
759 |
||
760 |
lemma div_add1_eq: |
|
761 |
"(a + b) div c = a div c + b div c + (a mod c + b mod c) div c" |
|
762 |
proof (cases "a mod c + b mod c" c rule: divmod_cases) |
|
763 |
case (divides q) |
|
764 |
have "a + b = (a div c * c + a mod c) + (b div c * c + b mod c)" |
|
765 |
using mod_mult_div_eq [of a c] mod_mult_div_eq [of b c] by (simp add: ac_simps) |
|
766 |
also have "\<dots> = (a div c + b div c) * c + (a mod c + b mod c)" |
|
767 |
by (simp add: algebra_simps) |
|
768 |
also have "\<dots> = (a div c + b div c + q) * c" |
|
769 |
using divides by (simp add: algebra_simps) |
|
770 |
finally have "(a + b) div c = (a div c + b div c + q) * c div c" |
|
771 |
by simp |
|
772 |
with divides show ?thesis |
|
773 |
by simp |
|
774 |
next |
|
775 |
case (remainder q r) |
|
776 |
from remainder(1-3) show ?thesis |
|
777 |
proof (rule div_eqI) |
|
778 |
have "(a div c + b div c + q) * c + r + (a mod c + b mod c) = |
|
779 |
(a div c * c + a mod c) + (b div c * c + b mod c) + q * c + r" |
|
780 |
by (simp add: algebra_simps) |
|
781 |
also have "\<dots> = a + b + (a mod c + b mod c)" |
|
782 |
by (simp add: div_mult_mod_eq remainder) (simp add: ac_simps) |
|
783 |
finally show "(a div c + b div c + (a mod c + b mod c) div c) * c + r = a + b" |
|
784 |
using remainder by simp |
|
785 |
qed |
|
786 |
next |
|
787 |
case by0 |
|
788 |
then show ?thesis |
|
789 |
by simp |
|
790 |
qed |
|
791 |
||
66886 | 792 |
lemma div_eq_0_iff: |
793 |
"a div b = 0 \<longleftrightarrow> euclidean_size a < euclidean_size b \<or> b = 0" (is "_ \<longleftrightarrow> ?P") |
|
794 |
if "division_segment a = division_segment b" |
|
795 |
proof |
|
796 |
assume ?P |
|
797 |
with that show "a div b = 0" |
|
798 |
by (cases "b = 0") (auto intro: div_eqI) |
|
799 |
next |
|
800 |
assume "a div b = 0" |
|
801 |
then have "a mod b = a" |
|
802 |
using div_mult_mod_eq [of a b] by simp |
|
803 |
with mod_size_less [of b a] show ?P |
|
804 |
by auto |
|
805 |
qed |
|
806 |
||
64785 | 807 |
end |
808 |
||
809 |
class unique_euclidean_ring = euclidean_ring + unique_euclidean_semiring |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
810 |
begin |
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
811 |
|
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
812 |
subclass euclidean_ring_cancel .. |
64785 | 813 |
|
814 |
end |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
815 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
816 |
|
69593 | 817 |
subsection \<open>Euclidean division on \<^typ>\<open>nat\<close>\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
818 |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
819 |
instantiation nat :: normalization_semidom |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
820 |
begin |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
821 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
822 |
definition normalize_nat :: "nat \<Rightarrow> nat" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
823 |
where [simp]: "normalize = (id :: nat \<Rightarrow> nat)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
824 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
825 |
definition unit_factor_nat :: "nat \<Rightarrow> nat" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
826 |
where "unit_factor n = (if n = 0 then 0 else 1 :: nat)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
827 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
828 |
lemma unit_factor_simps [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
829 |
"unit_factor 0 = (0::nat)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
830 |
"unit_factor (Suc n) = 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
831 |
by (simp_all add: unit_factor_nat_def) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
832 |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
833 |
definition divide_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
834 |
where "m div n = (if n = 0 then 0 else Max {k::nat. k * n \<le> m})" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
835 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
836 |
instance |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
837 |
by standard (auto simp add: divide_nat_def ac_simps unit_factor_nat_def intro: Max_eqI) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
838 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
839 |
end |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
840 |
|
67051 | 841 |
lemma coprime_Suc_0_left [simp]: |
842 |
"coprime (Suc 0) n" |
|
843 |
using coprime_1_left [of n] by simp |
|
844 |
||
845 |
lemma coprime_Suc_0_right [simp]: |
|
846 |
"coprime n (Suc 0)" |
|
847 |
using coprime_1_right [of n] by simp |
|
848 |
||
849 |
lemma coprime_common_divisor_nat: "coprime a b \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> x = 1" |
|
850 |
for a b :: nat |
|
851 |
by (drule coprime_common_divisor [of _ _ x]) simp_all |
|
852 |
||
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
853 |
instantiation nat :: unique_euclidean_semiring |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
854 |
begin |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
855 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
856 |
definition euclidean_size_nat :: "nat \<Rightarrow> nat" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
857 |
where [simp]: "euclidean_size_nat = id" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
858 |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
859 |
definition division_segment_nat :: "nat \<Rightarrow> nat" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
860 |
where [simp]: "division_segment_nat n = 1" |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
861 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
862 |
definition modulo_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
863 |
where "m mod n = m - (m div n * (n::nat))" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
864 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
865 |
instance proof |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
866 |
fix m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
867 |
have ex: "\<exists>k. k * n \<le> l" for l :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
868 |
by (rule exI [of _ 0]) simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
869 |
have fin: "finite {k. k * n \<le> l}" if "n > 0" for l |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
870 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
871 |
from that have "{k. k * n \<le> l} \<subseteq> {k. k \<le> l}" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
872 |
by (cases n) auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
873 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
874 |
by (rule finite_subset) simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
875 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
876 |
have mult_div_unfold: "n * (m div n) = Max {l. l \<le> m \<and> n dvd l}" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
877 |
proof (cases "n = 0") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
878 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
879 |
moreover have "{l. l = 0 \<and> l \<le> m} = {0::nat}" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
880 |
by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
881 |
ultimately show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
882 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
883 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
884 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
885 |
with ex [of m] fin have "n * Max {k. k * n \<le> m} = Max (times n ` {k. k * n \<le> m})" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
886 |
by (auto simp add: nat_mult_max_right intro: hom_Max_commute) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
887 |
also have "times n ` {k. k * n \<le> m} = {l. l \<le> m \<and> n dvd l}" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
888 |
by (auto simp add: ac_simps elim!: dvdE) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
889 |
finally show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
890 |
using False by (simp add: divide_nat_def ac_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
891 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
892 |
have less_eq: "m div n * n \<le> m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
893 |
by (auto simp add: mult_div_unfold ac_simps intro: Max.boundedI) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
894 |
then show "m div n * n + m mod n = m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
895 |
by (simp add: modulo_nat_def) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
896 |
assume "n \<noteq> 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
897 |
show "euclidean_size (m mod n) < euclidean_size n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
898 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
899 |
have "m < Suc (m div n) * n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
900 |
proof (rule ccontr) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
901 |
assume "\<not> m < Suc (m div n) * n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
902 |
then have "Suc (m div n) * n \<le> m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
903 |
by (simp add: not_less) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
904 |
moreover from \<open>n \<noteq> 0\<close> have "Max {k. k * n \<le> m} < Suc (m div n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
905 |
by (simp add: divide_nat_def) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
906 |
with \<open>n \<noteq> 0\<close> ex fin have "\<And>k. k * n \<le> m \<Longrightarrow> k < Suc (m div n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
907 |
by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
908 |
ultimately have "Suc (m div n) < Suc (m div n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
909 |
by blast |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
910 |
then show False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
911 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
912 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
913 |
with \<open>n \<noteq> 0\<close> show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
914 |
by (simp add: modulo_nat_def) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
915 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
916 |
show "euclidean_size m \<le> euclidean_size (m * n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
917 |
using \<open>n \<noteq> 0\<close> by (cases n) simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
918 |
fix q r :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
919 |
show "(q * n + r) div n = q" if "euclidean_size r < euclidean_size n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
920 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
921 |
from that have "r < n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
922 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
923 |
have "k \<le> q" if "k * n \<le> q * n + r" for k |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
924 |
proof (rule ccontr) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
925 |
assume "\<not> k \<le> q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
926 |
then have "q < k" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
927 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
928 |
then obtain l where "k = Suc (q + l)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
929 |
by (auto simp add: less_iff_Suc_add) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
930 |
with \<open>r < n\<close> that show False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
931 |
by (simp add: algebra_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
932 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
933 |
with \<open>n \<noteq> 0\<close> ex fin show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
934 |
by (auto simp add: divide_nat_def Max_eq_iff) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
935 |
qed |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
936 |
qed simp_all |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
937 |
|
66806
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents:
66798
diff
changeset
|
938 |
end |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
939 |
|
75881 | 940 |
lemma div_nat_eqI: |
941 |
"m div n = q" if "n * q \<le> m" and "m < n * Suc q" for m n q :: nat |
|
942 |
by (rule div_eqI [of _ "m - n * q"]) (use that in \<open>simp_all add: algebra_simps\<close>) |
|
943 |
||
944 |
lemma mod_nat_eqI: |
|
945 |
"m mod n = r" if "r < n" and "r \<le> m" and "n dvd m - r" for m n r :: nat |
|
946 |
by (rule mod_eqI [of _ _ "(m - r) div n"]) (use that in \<open>simp_all add: algebra_simps\<close>) |
|
947 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
948 |
text \<open>Tool support\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
949 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
950 |
ML \<open> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
951 |
structure Cancel_Div_Mod_Nat = Cancel_Div_Mod |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
952 |
( |
69593 | 953 |
val div_name = \<^const_name>\<open>divide\<close>; |
954 |
val mod_name = \<^const_name>\<open>modulo\<close>; |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
955 |
val mk_binop = HOLogic.mk_binop; |
69593 | 956 |
val dest_plus = HOLogic.dest_bin \<^const_name>\<open>Groups.plus\<close> HOLogic.natT; |
66813 | 957 |
val mk_sum = Arith_Data.mk_sum; |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
958 |
fun dest_sum tm = |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
959 |
if HOLogic.is_zero tm then [] |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
960 |
else |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
961 |
(case try HOLogic.dest_Suc tm of |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
962 |
SOME t => HOLogic.Suc_zero :: dest_sum t |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
963 |
| NONE => |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
964 |
(case try dest_plus tm of |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
965 |
SOME (t, u) => dest_sum t @ dest_sum u |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
966 |
| NONE => [tm])); |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
967 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
968 |
val div_mod_eqs = map mk_meta_eq @{thms cancel_div_mod_rules}; |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
969 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
970 |
val prove_eq_sums = Arith_Data.prove_conv2 all_tac |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
971 |
(Arith_Data.simp_all_tac @{thms add_0_left add_0_right ac_simps}) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
972 |
) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
973 |
\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
974 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
975 |
simproc_setup cancel_div_mod_nat ("(m::nat) + n") = |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
976 |
\<open>K Cancel_Div_Mod_Nat.proc\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
977 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
978 |
lemma div_mult_self_is_m [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
979 |
"m * n div n = m" if "n > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
980 |
using that by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
981 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
982 |
lemma div_mult_self1_is_m [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
983 |
"n * m div n = m" if "n > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
984 |
using that by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
985 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
986 |
lemma mod_less_divisor [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
987 |
"m mod n < n" if "n > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
988 |
using mod_size_less [of n m] that by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
989 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
990 |
lemma mod_le_divisor [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
991 |
"m mod n \<le> n" if "n > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
992 |
using that by (auto simp add: le_less) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
993 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
994 |
lemma div_times_less_eq_dividend [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
995 |
"m div n * n \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
996 |
by (simp add: minus_mod_eq_div_mult [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
997 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
998 |
lemma times_div_less_eq_dividend [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
999 |
"n * (m div n) \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1000 |
using div_times_less_eq_dividend [of m n] |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1001 |
by (simp add: ac_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1002 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1003 |
lemma dividend_less_div_times: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1004 |
"m < n + (m div n) * n" if "0 < n" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1005 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1006 |
from that have "m mod n < n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1007 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1008 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1009 |
by (simp add: minus_mod_eq_div_mult [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1010 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1011 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1012 |
lemma dividend_less_times_div: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1013 |
"m < n + n * (m div n)" if "0 < n" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1014 |
using dividend_less_div_times [of n m] that |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1015 |
by (simp add: ac_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1016 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1017 |
lemma mod_Suc_le_divisor [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1018 |
"m mod Suc n \<le> n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1019 |
using mod_less_divisor [of "Suc n" m] by arith |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1020 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1021 |
lemma mod_less_eq_dividend [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1022 |
"m mod n \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1023 |
proof (rule add_leD2) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1024 |
from div_mult_mod_eq have "m div n * n + m mod n = m" . |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1025 |
then show "m div n * n + m mod n \<le> m" by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1026 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1027 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1028 |
lemma |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1029 |
div_less [simp]: "m div n = 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1030 |
and mod_less [simp]: "m mod n = m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1031 |
if "m < n" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1032 |
using that by (auto intro: div_eqI mod_eqI) |
75881 | 1033 |
|
1034 |
lemma split_div: |
|
1035 |
\<open>P (m div n) \<longleftrightarrow> |
|
1036 |
(n = 0 \<longrightarrow> P 0) \<and> |
|
1037 |
(n \<noteq> 0 \<longrightarrow> (\<forall>i j. j < n \<and> m = n * i + j \<longrightarrow> P i))\<close> (is ?div) |
|
1038 |
and split_mod: |
|
1039 |
\<open>Q (m mod n) \<longleftrightarrow> |
|
1040 |
(n = 0 \<longrightarrow> Q m) \<and> |
|
1041 |
(n \<noteq> 0 \<longrightarrow> (\<forall>i j. j < n \<and> m = n * i + j \<longrightarrow> Q j))\<close> (is ?mod) |
|
1042 |
for m n :: nat |
|
1043 |
proof - |
|
1044 |
have *: \<open>R (m div n) (m mod n) \<longleftrightarrow> |
|
1045 |
(n = 0 \<longrightarrow> R 0 m) \<and> |
|
1046 |
(n \<noteq> 0 \<longrightarrow> (\<forall>i j. j < n \<and> m = n * i + j \<longrightarrow> R i j))\<close> for R |
|
1047 |
by (cases \<open>n = 0\<close>) auto |
|
1048 |
from * [of \<open>\<lambda>q _. P q\<close>] show ?div . |
|
1049 |
from * [of \<open>\<lambda>_ r. Q r\<close>] show ?mod . |
|
1050 |
qed |
|
1051 |
||
1052 |
declare split_div [of _ _ \<open>numeral n\<close>, linarith_split] for n |
|
1053 |
declare split_mod [of _ _ \<open>numeral n\<close>, linarith_split] for n |
|
1054 |
||
1055 |
lemma split_div': |
|
1056 |
"P (m div n) \<longleftrightarrow> n = 0 \<and> P 0 \<or> (\<exists>q. (n * q \<le> m \<and> m < n * Suc q) \<and> P q)" |
|
1057 |
proof (cases "n = 0") |
|
1058 |
case True |
|
1059 |
then show ?thesis |
|
1060 |
by simp |
|
1061 |
next |
|
1062 |
case False |
|
1063 |
then have "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> m div n = q" for q |
|
1064 |
by (auto intro: div_nat_eqI dividend_less_times_div) |
|
1065 |
then show ?thesis |
|
1066 |
by auto |
|
1067 |
qed |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1068 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1069 |
lemma le_div_geq: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1070 |
"m div n = Suc ((m - n) div n)" if "0 < n" and "n \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1071 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1072 |
from \<open>n \<le> m\<close> obtain q where "m = n + q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1073 |
by (auto simp add: le_iff_add) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1074 |
with \<open>0 < n\<close> show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1075 |
by (simp add: div_add_self1) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1076 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1077 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1078 |
lemma le_mod_geq: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1079 |
"m mod n = (m - n) mod n" if "n \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1080 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1081 |
from \<open>n \<le> m\<close> obtain q where "m = n + q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1082 |
by (auto simp add: le_iff_add) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1083 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1084 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1085 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1086 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1087 |
lemma div_if: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1088 |
"m div n = (if m < n \<or> n = 0 then 0 else Suc ((m - n) div n))" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1089 |
by (simp add: le_div_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1090 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1091 |
lemma mod_if: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1092 |
"m mod n = (if m < n then m else (m - n) mod n)" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1093 |
by (simp add: le_mod_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1094 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1095 |
lemma div_eq_0_iff: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1096 |
"m div n = 0 \<longleftrightarrow> m < n \<or> n = 0" for m n :: nat |
66886 | 1097 |
by (simp add: div_eq_0_iff) |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1098 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1099 |
lemma div_greater_zero_iff: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1100 |
"m div n > 0 \<longleftrightarrow> n \<le> m \<and> n > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1101 |
using div_eq_0_iff [of m n] by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1102 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1103 |
lemma mod_greater_zero_iff_not_dvd: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1104 |
"m mod n > 0 \<longleftrightarrow> \<not> n dvd m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1105 |
by (simp add: dvd_eq_mod_eq_0) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1106 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1107 |
lemma div_by_Suc_0 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1108 |
"m div Suc 0 = m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1109 |
using div_by_1 [of m] by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1110 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1111 |
lemma mod_by_Suc_0 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1112 |
"m mod Suc 0 = 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1113 |
using mod_by_1 [of m] by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1114 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1115 |
lemma div2_Suc_Suc [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1116 |
"Suc (Suc m) div 2 = Suc (m div 2)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1117 |
by (simp add: numeral_2_eq_2 le_div_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1118 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1119 |
lemma Suc_n_div_2_gt_zero [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1120 |
"0 < Suc n div 2" if "n > 0" for n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1121 |
using that by (cases n) simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1122 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1123 |
lemma div_2_gt_zero [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1124 |
"0 < n div 2" if "Suc 0 < n" for n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1125 |
using that Suc_n_div_2_gt_zero [of "n - 1"] by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1126 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1127 |
lemma mod2_Suc_Suc [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1128 |
"Suc (Suc m) mod 2 = m mod 2" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1129 |
by (simp add: numeral_2_eq_2 le_mod_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1130 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1131 |
lemma add_self_div_2 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1132 |
"(m + m) div 2 = m" for m :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1133 |
by (simp add: mult_2 [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1134 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1135 |
lemma add_self_mod_2 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1136 |
"(m + m) mod 2 = 0" for m :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1137 |
by (simp add: mult_2 [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1138 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1139 |
lemma mod2_gr_0 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1140 |
"0 < m mod 2 \<longleftrightarrow> m mod 2 = 1" for m :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1141 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1142 |
have "m mod 2 < 2" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1143 |
by (rule mod_less_divisor) simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1144 |
then have "m mod 2 = 0 \<or> m mod 2 = 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1145 |
by arith |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1146 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1147 |
by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1148 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1149 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1150 |
lemma mod_Suc_eq [mod_simps]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1151 |
"Suc (m mod n) mod n = Suc m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1152 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1153 |
have "(m mod n + 1) mod n = (m + 1) mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1154 |
by (simp only: mod_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1155 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1156 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1157 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1158 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1159 |
lemma mod_Suc_Suc_eq [mod_simps]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1160 |
"Suc (Suc (m mod n)) mod n = Suc (Suc m) mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1161 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1162 |
have "(m mod n + 2) mod n = (m + 2) mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1163 |
by (simp only: mod_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1164 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1165 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1166 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1167 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1168 |
lemma |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1169 |
Suc_mod_mult_self1 [simp]: "Suc (m + k * n) mod n = Suc m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1170 |
and Suc_mod_mult_self2 [simp]: "Suc (m + n * k) mod n = Suc m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1171 |
and Suc_mod_mult_self3 [simp]: "Suc (k * n + m) mod n = Suc m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1172 |
and Suc_mod_mult_self4 [simp]: "Suc (n * k + m) mod n = Suc m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1173 |
by (subst mod_Suc_eq [symmetric], simp add: mod_simps)+ |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1174 |
|
67083 | 1175 |
lemma Suc_0_mod_eq [simp]: |
1176 |
"Suc 0 mod n = of_bool (n \<noteq> Suc 0)" |
|
1177 |
by (cases n) simp_all |
|
1178 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1179 |
context |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1180 |
fixes m n q :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1181 |
begin |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1182 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1183 |
private lemma eucl_rel_mult2: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1184 |
"m mod n + n * (m div n mod q) < n * q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1185 |
if "n > 0" and "q > 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1186 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1187 |
from \<open>n > 0\<close> have "m mod n < n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1188 |
by (rule mod_less_divisor) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1189 |
from \<open>q > 0\<close> have "m div n mod q < q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1190 |
by (rule mod_less_divisor) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1191 |
then obtain s where "q = Suc (m div n mod q + s)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1192 |
by (blast dest: less_imp_Suc_add) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1193 |
moreover have "m mod n + n * (m div n mod q) < n * Suc (m div n mod q + s)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1194 |
using \<open>m mod n < n\<close> by (simp add: add_mult_distrib2) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1195 |
ultimately show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1196 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1197 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1198 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1199 |
lemma div_mult2_eq: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1200 |
"m div (n * q) = (m div n) div q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1201 |
proof (cases "n = 0 \<or> q = 0") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1202 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1203 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1204 |
by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1205 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1206 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1207 |
with eucl_rel_mult2 show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1208 |
by (auto intro: div_eqI [of _ "n * (m div n mod q) + m mod n"] |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1209 |
simp add: algebra_simps add_mult_distrib2 [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1210 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1211 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1212 |
lemma mod_mult2_eq: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1213 |
"m mod (n * q) = n * (m div n mod q) + m mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1214 |
proof (cases "n = 0 \<or> q = 0") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1215 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1216 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1217 |
by auto |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1218 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1219 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1220 |
with eucl_rel_mult2 show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1221 |
by (auto intro: mod_eqI [of _ _ "(m div n) div q"] |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1222 |
simp add: algebra_simps add_mult_distrib2 [symmetric]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1223 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1224 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1225 |
end |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1226 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1227 |
lemma div_le_mono: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1228 |
"m div k \<le> n div k" if "m \<le> n" for m n k :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1229 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1230 |
from that obtain q where "n = m + q" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1231 |
by (auto simp add: le_iff_add) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1232 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1233 |
by (simp add: div_add1_eq [of m q k]) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1234 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1235 |
|
69593 | 1236 |
text \<open>Antimonotonicity of \<^const>\<open>divide\<close> in second argument\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1237 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1238 |
lemma div_le_mono2: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1239 |
"k div n \<le> k div m" if "0 < m" and "m \<le> n" for m n k :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1240 |
using that proof (induct k arbitrary: m rule: less_induct) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1241 |
case (less k) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1242 |
show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1243 |
proof (cases "n \<le> k") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1244 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1245 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1246 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1247 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1248 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1249 |
have "(k - n) div n \<le> (k - m) div n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1250 |
using less.prems |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1251 |
by (blast intro: div_le_mono diff_le_mono2) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1252 |
also have "\<dots> \<le> (k - m) div m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1253 |
using \<open>n \<le> k\<close> less.prems less.hyps [of "k - m" m] |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1254 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1255 |
finally show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1256 |
using \<open>n \<le> k\<close> less.prems |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1257 |
by (simp add: le_div_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1258 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1259 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1260 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1261 |
lemma div_le_dividend [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1262 |
"m div n \<le> m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1263 |
using div_le_mono2 [of 1 n m] by (cases "n = 0") simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1264 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1265 |
lemma div_less_dividend [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1266 |
"m div n < m" if "1 < n" and "0 < m" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1267 |
using that proof (induct m rule: less_induct) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1268 |
case (less m) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1269 |
show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1270 |
proof (cases "n < m") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1271 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1272 |
with less show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1273 |
by (cases "n = m") simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1274 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1275 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1276 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1277 |
using less.hyps [of "m - n"] less.prems |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1278 |
by (simp add: le_div_geq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1279 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1280 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1281 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1282 |
lemma div_eq_dividend_iff: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1283 |
"m div n = m \<longleftrightarrow> n = 1" if "m > 0" for m n :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1284 |
proof |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1285 |
assume "n = 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1286 |
then show "m div n = m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1287 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1288 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1289 |
assume P: "m div n = m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1290 |
show "n = 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1291 |
proof (rule ccontr) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1292 |
have "n \<noteq> 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1293 |
by (rule ccontr) (use that P in auto) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1294 |
moreover assume "n \<noteq> 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1295 |
ultimately have "n > 1" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1296 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1297 |
with that have "m div n < m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1298 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1299 |
with P show False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1300 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1301 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1302 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1303 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1304 |
lemma less_mult_imp_div_less: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1305 |
"m div n < i" if "m < i * n" for m n i :: nat |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1306 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1307 |
from that have "i * n > 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1308 |
by (cases "i * n = 0") simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1309 |
then have "i > 0" and "n > 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1310 |
by simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1311 |
have "m div n * n \<le> m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1312 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1313 |
then have "m div n * n < i * n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1314 |
using that by (rule le_less_trans) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1315 |
with \<open>n > 0\<close> show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1316 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1317 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1318 |
|
73853 | 1319 |
lemma div_less_iff_less_mult: |
1320 |
\<open>m div q < n \<longleftrightarrow> m < n * q\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1321 |
if \<open>q > 0\<close> for m n q :: nat |
|
1322 |
proof |
|
1323 |
assume ?Q then show ?P |
|
1324 |
by (rule less_mult_imp_div_less) |
|
1325 |
next |
|
1326 |
assume ?P |
|
1327 |
then obtain h where \<open>n = Suc (m div q + h)\<close> |
|
1328 |
using less_natE by blast |
|
1329 |
moreover have \<open>m < m + (Suc h * q - m mod q)\<close> |
|
1330 |
using that by (simp add: trans_less_add1) |
|
1331 |
ultimately show ?Q |
|
1332 |
by (simp add: algebra_simps flip: minus_mod_eq_mult_div) |
|
1333 |
qed |
|
1334 |
||
1335 |
lemma less_eq_div_iff_mult_less_eq: |
|
1336 |
\<open>m \<le> n div q \<longleftrightarrow> m * q \<le> n\<close> if \<open>q > 0\<close> for m n q :: nat |
|
1337 |
using div_less_iff_less_mult [of q n m] that by auto |
|
1338 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1339 |
text \<open>A fact for the mutilated chess board\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1340 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1341 |
lemma mod_Suc: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1342 |
"Suc m mod n = (if Suc (m mod n) = n then 0 else Suc (m mod n))" (is "_ = ?rhs") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1343 |
proof (cases "n = 0") |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1344 |
case True |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1345 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1346 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1347 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1348 |
case False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1349 |
have "Suc m mod n = Suc (m mod n) mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1350 |
by (simp add: mod_simps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1351 |
also have "\<dots> = ?rhs" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1352 |
using False by (auto intro!: mod_nat_eqI intro: neq_le_trans simp add: Suc_le_eq) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1353 |
finally show ?thesis . |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1354 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1355 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1356 |
lemma Suc_times_mod_eq: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1357 |
"Suc (m * n) mod m = 1" if "Suc 0 < m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1358 |
using that by (simp add: mod_Suc) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1359 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1360 |
lemma Suc_times_numeral_mod_eq [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1361 |
"Suc (numeral k * n) mod numeral k = 1" if "numeral k \<noteq> (1::nat)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1362 |
by (rule Suc_times_mod_eq) (use that in simp) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1363 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1364 |
lemma Suc_div_le_mono [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1365 |
"m div n \<le> Suc m div n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1366 |
by (simp add: div_le_mono) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1367 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1368 |
text \<open>These lemmas collapse some needless occurrences of Suc: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1369 |
at least three Sucs, since two and fewer are rewritten back to Suc again! |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1370 |
We already have some rules to simplify operands smaller than 3.\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1371 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1372 |
lemma div_Suc_eq_div_add3 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1373 |
"m div Suc (Suc (Suc n)) = m div (3 + n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1374 |
by (simp add: Suc3_eq_add_3) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1375 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1376 |
lemma mod_Suc_eq_mod_add3 [simp]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1377 |
"m mod Suc (Suc (Suc n)) = m mod (3 + n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1378 |
by (simp add: Suc3_eq_add_3) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1379 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1380 |
lemma Suc_div_eq_add3_div: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1381 |
"Suc (Suc (Suc m)) div n = (3 + m) div n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1382 |
by (simp add: Suc3_eq_add_3) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1383 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1384 |
lemma Suc_mod_eq_add3_mod: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1385 |
"Suc (Suc (Suc m)) mod n = (3 + m) mod n" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1386 |
by (simp add: Suc3_eq_add_3) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1387 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1388 |
lemmas Suc_div_eq_add3_div_numeral [simp] = |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1389 |
Suc_div_eq_add3_div [of _ "numeral v"] for v |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1390 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1391 |
lemmas Suc_mod_eq_add3_mod_numeral [simp] = |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1392 |
Suc_mod_eq_add3_mod [of _ "numeral v"] for v |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1393 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1394 |
lemma (in field_char_0) of_nat_div: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1395 |
"of_nat (m div n) = ((of_nat m - of_nat (m mod n)) / of_nat n)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1396 |
proof - |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1397 |
have "of_nat (m div n) = ((of_nat (m div n * n + m mod n) - of_nat (m mod n)) / of_nat n :: 'a)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1398 |
unfolding of_nat_add by (cases "n = 0") simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1399 |
then show ?thesis |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1400 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1401 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1402 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1403 |
text \<open>An ``induction'' law for modulus arithmetic.\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1404 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1405 |
lemma mod_induct [consumes 3, case_names step]: |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1406 |
"P m" if "P n" and "n < p" and "m < p" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1407 |
and step: "\<And>n. n < p \<Longrightarrow> P n \<Longrightarrow> P (Suc n mod p)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1408 |
using \<open>m < p\<close> proof (induct m) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1409 |
case 0 |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1410 |
show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1411 |
proof (rule ccontr) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1412 |
assume "\<not> P 0" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1413 |
from \<open>n < p\<close> have "0 < p" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1414 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1415 |
from \<open>n < p\<close> obtain m where "0 < m" and "p = n + m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1416 |
by (blast dest: less_imp_add_positive) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1417 |
with \<open>P n\<close> have "P (p - m)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1418 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1419 |
moreover have "\<not> P (p - m)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1420 |
using \<open>0 < m\<close> proof (induct m) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1421 |
case 0 |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1422 |
then show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1423 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1424 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1425 |
case (Suc m) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1426 |
show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1427 |
proof |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1428 |
assume P: "P (p - Suc m)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1429 |
with \<open>\<not> P 0\<close> have "Suc m < p" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1430 |
by (auto intro: ccontr) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1431 |
then have "Suc (p - Suc m) = p - m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1432 |
by arith |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1433 |
moreover from \<open>0 < p\<close> have "p - Suc m < p" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1434 |
by arith |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1435 |
with P step have "P ((Suc (p - Suc m)) mod p)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1436 |
by blast |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1437 |
ultimately show False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1438 |
using \<open>\<not> P 0\<close> Suc.hyps by (cases "m = 0") simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1439 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1440 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1441 |
ultimately show False |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1442 |
by blast |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1443 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1444 |
next |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1445 |
case (Suc m) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1446 |
then have "m < p" and mod: "Suc m mod p = Suc m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1447 |
by simp_all |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1448 |
from \<open>m < p\<close> have "P m" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1449 |
by (rule Suc.hyps) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1450 |
with \<open>m < p\<close> have "P (Suc m mod p)" |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1451 |
by (rule step) |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1452 |
with mod show ?case |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1453 |
by simp |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1454 |
qed |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1455 |
|
73555 | 1456 |
lemma funpow_mod_eq: \<^marker>\<open>contributor \<open>Lars Noschinski\<close>\<close> |
1457 |
\<open>(f ^^ (m mod n)) x = (f ^^ m) x\<close> if \<open>(f ^^ n) x = x\<close> |
|
1458 |
proof - |
|
1459 |
have \<open>(f ^^ m) x = (f ^^ (m mod n + m div n * n)) x\<close> |
|
1460 |
by simp |
|
1461 |
also have \<open>\<dots> = (f ^^ (m mod n)) (((f ^^ n) ^^ (m div n)) x)\<close> |
|
1462 |
by (simp only: funpow_add funpow_mult ac_simps) simp |
|
1463 |
also have \<open>((f ^^ n) ^^ q) x = x\<close> for q |
|
1464 |
by (induction q) (use \<open>(f ^^ n) x = x\<close> in simp_all) |
|
1465 |
finally show ?thesis |
|
1466 |
by simp |
|
1467 |
qed |
|
1468 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
1469 |
|
75876 | 1470 |
subsection \<open>Elementary euclidean division on \<^typ>\<open>int\<close>\<close> |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1471 |
|
75876 | 1472 |
subsubsection \<open>Basic instantiation\<close> |
1473 |
||
1474 |
instantiation int :: "{normalization_semidom, idom_modulo}" |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1475 |
begin |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1476 |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1477 |
definition normalize_int :: \<open>int \<Rightarrow> int\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1478 |
where [simp]: \<open>normalize = (abs :: int \<Rightarrow> int)\<close> |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1479 |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1480 |
definition unit_factor_int :: \<open>int \<Rightarrow> int\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1481 |
where [simp]: \<open>unit_factor = (sgn :: int \<Rightarrow> int)\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1482 |
|
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1483 |
definition divide_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1484 |
where \<open>k div l = (sgn k * sgn l * int (nat \<bar>k\<bar> div nat \<bar>l\<bar>) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1485 |
- of_bool (l \<noteq> 0 \<and> sgn k \<noteq> sgn l \<and> \<not> l dvd k))\<close> |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1486 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1487 |
lemma divide_int_unfold: |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1488 |
\<open>(sgn k * int m) div (sgn l * int n) = (sgn k * sgn l * int (m div n) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1489 |
- of_bool ((k = 0 \<longleftrightarrow> m = 0) \<and> l \<noteq> 0 \<and> n \<noteq> 0 \<and> sgn k \<noteq> sgn l \<and> \<not> n dvd m))\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1490 |
by (simp add: divide_int_def sgn_mult nat_mult_distrib abs_mult sgn_eq_0_iff ac_simps) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1491 |
|
75876 | 1492 |
definition modulo_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
1493 |
where \<open>k mod l = sgn k * int (nat \<bar>k\<bar> mod nat \<bar>l\<bar>) + l * of_bool (sgn k \<noteq> sgn l \<and> \<not> l dvd k)\<close> |
|
1494 |
||
1495 |
lemma modulo_int_unfold: |
|
1496 |
\<open>(sgn k * int m) mod (sgn l * int n) = |
|
1497 |
sgn k * int (m mod (of_bool (l \<noteq> 0) * n)) + (sgn l * int n) * of_bool ((k = 0 \<longleftrightarrow> m = 0) \<and> sgn k \<noteq> sgn l \<and> \<not> n dvd m)\<close> |
|
1498 |
by (auto simp add: modulo_int_def sgn_mult abs_mult) |
|
1499 |
||
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1500 |
instance proof |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1501 |
fix k :: int show "k div 0 = 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1502 |
by (simp add: divide_int_def) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1503 |
next |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1504 |
fix k l :: int |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1505 |
assume "l \<noteq> 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1506 |
obtain n m and s t where k: "k = sgn s * int n" and l: "l = sgn t * int m" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1507 |
by (blast intro: int_sgnE elim: that) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1508 |
then have "k * l = sgn (s * t) * int (n * m)" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1509 |
by (simp add: ac_simps sgn_mult) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1510 |
with k l \<open>l \<noteq> 0\<close> show "k * l div l = k" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1511 |
by (simp only: divide_int_unfold) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1512 |
(auto simp add: algebra_simps sgn_mult sgn_1_pos sgn_0_0) |
75876 | 1513 |
next |
1514 |
fix k l :: int |
|
1515 |
obtain n m and s t where "k = sgn s * int n" and "l = sgn t * int m" |
|
1516 |
by (blast intro: int_sgnE elim: that) |
|
1517 |
then show "k div l * l + k mod l = k" |
|
1518 |
by (simp add: divide_int_unfold modulo_int_unfold algebra_simps modulo_nat_def of_nat_diff) |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1519 |
qed (auto simp add: sgn_mult mult_sgn_abs abs_eq_iff') |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1520 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1521 |
end |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1522 |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1523 |
|
75876 | 1524 |
subsubsection \<open>Algebraic foundations\<close> |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1525 |
|
67051 | 1526 |
lemma coprime_int_iff [simp]: |
1527 |
"coprime (int m) (int n) \<longleftrightarrow> coprime m n" (is "?P \<longleftrightarrow> ?Q") |
|
1528 |
proof |
|
1529 |
assume ?P |
|
1530 |
show ?Q |
|
1531 |
proof (rule coprimeI) |
|
1532 |
fix q |
|
1533 |
assume "q dvd m" "q dvd n" |
|
1534 |
then have "int q dvd int m" "int q dvd int n" |
|
67118 | 1535 |
by simp_all |
67051 | 1536 |
with \<open>?P\<close> have "is_unit (int q)" |
1537 |
by (rule coprime_common_divisor) |
|
1538 |
then show "is_unit q" |
|
1539 |
by simp |
|
1540 |
qed |
|
1541 |
next |
|
1542 |
assume ?Q |
|
1543 |
show ?P |
|
1544 |
proof (rule coprimeI) |
|
1545 |
fix k |
|
1546 |
assume "k dvd int m" "k dvd int n" |
|
1547 |
then have "nat \<bar>k\<bar> dvd m" "nat \<bar>k\<bar> dvd n" |
|
67118 | 1548 |
by simp_all |
67051 | 1549 |
with \<open>?Q\<close> have "is_unit (nat \<bar>k\<bar>)" |
1550 |
by (rule coprime_common_divisor) |
|
1551 |
then show "is_unit k" |
|
1552 |
by simp |
|
1553 |
qed |
|
1554 |
qed |
|
1555 |
||
1556 |
lemma coprime_abs_left_iff [simp]: |
|
1557 |
"coprime \<bar>k\<bar> l \<longleftrightarrow> coprime k l" for k l :: int |
|
1558 |
using coprime_normalize_left_iff [of k l] by simp |
|
1559 |
||
1560 |
lemma coprime_abs_right_iff [simp]: |
|
1561 |
"coprime k \<bar>l\<bar> \<longleftrightarrow> coprime k l" for k l :: int |
|
1562 |
using coprime_abs_left_iff [of l k] by (simp add: ac_simps) |
|
1563 |
||
1564 |
lemma coprime_nat_abs_left_iff [simp]: |
|
1565 |
"coprime (nat \<bar>k\<bar>) n \<longleftrightarrow> coprime k (int n)" |
|
1566 |
proof - |
|
1567 |
define m where "m = nat \<bar>k\<bar>" |
|
1568 |
then have "\<bar>k\<bar> = int m" |
|
1569 |
by simp |
|
1570 |
moreover have "coprime k (int n) \<longleftrightarrow> coprime \<bar>k\<bar> (int n)" |
|
1571 |
by simp |
|
1572 |
ultimately show ?thesis |
|
1573 |
by simp |
|
1574 |
qed |
|
1575 |
||
1576 |
lemma coprime_nat_abs_right_iff [simp]: |
|
1577 |
"coprime n (nat \<bar>k\<bar>) \<longleftrightarrow> coprime (int n) k" |
|
1578 |
using coprime_nat_abs_left_iff [of k n] by (simp add: ac_simps) |
|
1579 |
||
1580 |
lemma coprime_common_divisor_int: "coprime a b \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> \<bar>x\<bar> = 1" |
|
1581 |
for a b :: int |
|
1582 |
by (drule coprime_common_divisor [of _ _ x]) simp_all |
|
1583 |
||
75876 | 1584 |
|
1585 |
subsubsection \<open>Basic conversions\<close> |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1586 |
|
75876 | 1587 |
lemma div_abs_eq_div_nat: |
1588 |
"\<bar>k\<bar> div \<bar>l\<bar> = int (nat \<bar>k\<bar> div nat \<bar>l\<bar>)" |
|
1589 |
by (auto simp add: divide_int_def) |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1590 |
|
75876 | 1591 |
lemma div_eq_div_abs: |
1592 |
\<open>k div l = sgn k * sgn l * (\<bar>k\<bar> div \<bar>l\<bar>) |
|
1593 |
- of_bool (l \<noteq> 0 \<and> sgn k \<noteq> sgn l \<and> \<not> l dvd k)\<close> |
|
1594 |
for k l :: int |
|
1595 |
by (simp add: divide_int_def [of k l] div_abs_eq_div_nat) |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1596 |
|
75876 | 1597 |
lemma div_abs_eq: |
1598 |
\<open>\<bar>k\<bar> div \<bar>l\<bar> = sgn k * sgn l * (k div l + of_bool (sgn k \<noteq> sgn l \<and> \<not> l dvd k))\<close> |
|
1599 |
for k l :: int |
|
1600 |
by (simp add: div_eq_div_abs [of k l] ac_simps) |
|
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1601 |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1602 |
lemma mod_abs_eq_div_nat: |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1603 |
"\<bar>k\<bar> mod \<bar>l\<bar> = int (nat \<bar>k\<bar> mod nat \<bar>l\<bar>)" |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1604 |
by (simp add: modulo_int_def) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1605 |
|
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1606 |
lemma mod_eq_mod_abs: |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1607 |
\<open>k mod l = sgn k * (\<bar>k\<bar> mod \<bar>l\<bar>) + l * of_bool (sgn k \<noteq> sgn l \<and> \<not> l dvd k)\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1608 |
for k l :: int |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1609 |
by (simp add: modulo_int_def [of k l] mod_abs_eq_div_nat) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1610 |
|
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1611 |
lemma mod_abs_eq: |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1612 |
\<open>\<bar>k\<bar> mod \<bar>l\<bar> = sgn k * (k mod l - l * of_bool (sgn k \<noteq> sgn l \<and> \<not> l dvd k))\<close> |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1613 |
for k l :: int |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1614 |
by (auto simp: mod_eq_mod_abs [of k l]) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1615 |
|
75876 | 1616 |
lemma div_sgn_abs_cancel: |
1617 |
fixes k l v :: int |
|
1618 |
assumes "v \<noteq> 0" |
|
1619 |
shows "(sgn v * \<bar>k\<bar>) div (sgn v * \<bar>l\<bar>) = \<bar>k\<bar> div \<bar>l\<bar>" |
|
1620 |
using assms by (simp add: sgn_mult abs_mult sgn_0_0 |
|
1621 |
divide_int_def [of "sgn v * \<bar>k\<bar>" "sgn v * \<bar>l\<bar>"] flip: div_abs_eq_div_nat) |
|
1622 |
||
1623 |
lemma div_eq_sgn_abs: |
|
1624 |
fixes k l v :: int |
|
1625 |
assumes "sgn k = sgn l" |
|
1626 |
shows "k div l = \<bar>k\<bar> div \<bar>l\<bar>" |
|
1627 |
using assms by (auto simp add: div_abs_eq) |
|
1628 |
||
1629 |
lemma div_dvd_sgn_abs: |
|
1630 |
fixes k l :: int |
|
1631 |
assumes "l dvd k" |
|
1632 |
shows "k div l = (sgn k * sgn l) * (\<bar>k\<bar> div \<bar>l\<bar>)" |
|
1633 |
using assms by (auto simp add: div_abs_eq ac_simps) |
|
1634 |
||
1635 |
lemma div_noneq_sgn_abs: |
|
1636 |
fixes k l :: int |
|
1637 |
assumes "l \<noteq> 0" |
|
1638 |
assumes "sgn k \<noteq> sgn l" |
|
1639 |
shows "k div l = - (\<bar>k\<bar> div \<bar>l\<bar>) - of_bool (\<not> l dvd k)" |
|
1640 |
using assms by (auto simp add: div_abs_eq ac_simps sgn_0_0 dest!: sgn_not_eq_imp) |
|
1641 |
||
1642 |
||
1643 |
subsubsection \<open>Euclidean division\<close> |
|
1644 |
||
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1645 |
instantiation int :: unique_euclidean_ring |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1646 |
begin |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1647 |
|
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1648 |
definition euclidean_size_int :: "int \<Rightarrow> nat" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1649 |
where [simp]: "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1650 |
|
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1651 |
definition division_segment_int :: "int \<Rightarrow> int" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1652 |
where "division_segment_int k = (if k \<ge> 0 then 1 else - 1)" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1653 |
|
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1654 |
lemma division_segment_eq_sgn: |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1655 |
"division_segment k = sgn k" if "k \<noteq> 0" for k :: int |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1656 |
using that by (simp add: division_segment_int_def) |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1657 |
|
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1658 |
lemma abs_division_segment [simp]: |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1659 |
"\<bar>division_segment k\<bar> = 1" for k :: int |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1660 |
by (simp add: division_segment_int_def) |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1661 |
|
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1662 |
lemma abs_mod_less: |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1663 |
"\<bar>k mod l\<bar> < \<bar>l\<bar>" if "l \<noteq> 0" for k l :: int |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1664 |
proof - |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1665 |
obtain n m and s t where "k = sgn s * int n" and "l = sgn t * int m" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1666 |
by (blast intro: int_sgnE elim: that) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1667 |
with that show ?thesis |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1668 |
by (auto simp add: modulo_int_unfold abs_mult mod_greater_zero_iff_not_dvd |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1669 |
simp flip: right_diff_distrib dest!: sgn_not_eq_imp) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1670 |
(simp add: sgn_0_0) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1671 |
qed |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1672 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1673 |
lemma sgn_mod: |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1674 |
"sgn (k mod l) = sgn l" if "l \<noteq> 0" "\<not> l dvd k" for k l :: int |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1675 |
proof - |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1676 |
obtain n m and s t where "k = sgn s * int n" and "l = sgn t * int m" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1677 |
by (blast intro: int_sgnE elim: that) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1678 |
with that show ?thesis |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1679 |
by (auto simp add: modulo_int_unfold sgn_mult mod_greater_zero_iff_not_dvd |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1680 |
simp flip: right_diff_distrib dest!: sgn_not_eq_imp) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1681 |
qed |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1682 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1683 |
instance proof |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1684 |
fix k l :: int |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1685 |
show "division_segment (k mod l) = division_segment l" if |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1686 |
"l \<noteq> 0" and "\<not> l dvd k" |
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1687 |
using that by (simp add: division_segment_eq_sgn dvd_eq_mod_eq_0 sgn_mod) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1688 |
next |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1689 |
fix l q r :: int |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1690 |
obtain n m and s t |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1691 |
where l: "l = sgn s * int n" and q: "q = sgn t * int m" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1692 |
by (blast intro: int_sgnE elim: that) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1693 |
assume \<open>l \<noteq> 0\<close> |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1694 |
with l have "s \<noteq> 0" and "n > 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1695 |
by (simp_all add: sgn_0_0) |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1696 |
assume "division_segment r = division_segment l" |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1697 |
moreover have "r = sgn r * \<bar>r\<bar>" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1698 |
by (simp add: sgn_mult_abs) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1699 |
moreover define u where "u = nat \<bar>r\<bar>" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1700 |
ultimately have "r = sgn l * int u" |
66838
17989f6bc7b2
clarified uniqueness criterion for euclidean rings
haftmann
parents:
66837
diff
changeset
|
1701 |
using division_segment_eq_sgn \<open>l \<noteq> 0\<close> by (cases "r = 0") simp_all |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1702 |
with l \<open>n > 0\<close> have r: "r = sgn s * int u" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1703 |
by (simp add: sgn_mult) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1704 |
assume "euclidean_size r < euclidean_size l" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1705 |
with l r \<open>s \<noteq> 0\<close> have "u < n" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1706 |
by (simp add: abs_mult) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1707 |
show "(q * l + r) div l = q" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1708 |
proof (cases "q = 0 \<or> r = 0") |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1709 |
case True |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1710 |
then show ?thesis |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1711 |
proof |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1712 |
assume "q = 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1713 |
then show ?thesis |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1714 |
using l r \<open>u < n\<close> by (simp add: divide_int_unfold) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1715 |
next |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1716 |
assume "r = 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1717 |
from \<open>r = 0\<close> have *: "q * l + r = sgn (t * s) * int (n * m)" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1718 |
using q l by (simp add: ac_simps sgn_mult) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1719 |
from \<open>s \<noteq> 0\<close> \<open>n > 0\<close> show ?thesis |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1720 |
by (simp only: *, simp only: * q l divide_int_unfold) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1721 |
(auto simp add: sgn_mult ac_simps) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1722 |
qed |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1723 |
next |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1724 |
case False |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1725 |
with q r have "t \<noteq> 0" and "m > 0" and "s \<noteq> 0" and "u > 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1726 |
by (simp_all add: sgn_0_0) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1727 |
moreover from \<open>0 < m\<close> \<open>u < n\<close> have "u \<le> m * n" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1728 |
using mult_le_less_imp_less [of 1 m u n] by simp |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1729 |
ultimately have *: "q * l + r = sgn (s * t) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1730 |
* int (if t < 0 then m * n - u else m * n + u)" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1731 |
using l q r |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1732 |
by (simp add: sgn_mult algebra_simps of_nat_diff) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1733 |
have "(m * n - u) div n = m - 1" if "u > 0" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1734 |
using \<open>0 < m\<close> \<open>u < n\<close> that |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1735 |
by (auto intro: div_nat_eqI simp add: algebra_simps) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1736 |
moreover have "n dvd m * n - u \<longleftrightarrow> n dvd u" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1737 |
using \<open>u \<le> m * n\<close> dvd_diffD1 [of n "m * n" u] |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1738 |
by auto |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1739 |
ultimately show ?thesis |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1740 |
using \<open>s \<noteq> 0\<close> \<open>m > 0\<close> \<open>u > 0\<close> \<open>u < n\<close> \<open>u \<le> m * n\<close> |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1741 |
by (simp only: *, simp only: l q divide_int_unfold) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1742 |
(auto simp add: sgn_mult sgn_0_0 sgn_1_pos algebra_simps dest: dvd_imp_le) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1743 |
qed |
68536 | 1744 |
qed (use mult_le_mono2 [of 1] in \<open>auto simp add: division_segment_int_def not_le zero_less_mult_iff mult_less_0_iff abs_mult sgn_mult abs_mod_less sgn_mod nat_mult_distrib\<close>) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1745 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1746 |
end |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1747 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66814
diff
changeset
|
1748 |
|
71157 | 1749 |
subsection \<open>Special case: euclidean rings containing the natural numbers\<close> |
1750 |
||
1751 |
class unique_euclidean_semiring_with_nat = semidom + semiring_char_0 + unique_euclidean_semiring + |
|
1752 |
assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n" |
|
1753 |
and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1" |
|
1754 |
and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a" |
|
1755 |
begin |
|
1756 |
||
1757 |
lemma division_segment_eq_iff: |
|
1758 |
"a = b" if "division_segment a = division_segment b" |
|
1759 |
and "euclidean_size a = euclidean_size b" |
|
1760 |
using that division_segment_euclidean_size [of a] by simp |
|
1761 |
||
1762 |
lemma euclidean_size_of_nat [simp]: |
|
1763 |
"euclidean_size (of_nat n) = n" |
|
1764 |
proof - |
|
1765 |
have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n" |
|
1766 |
by (fact division_segment_euclidean_size) |
|
1767 |
then show ?thesis by simp |
|
1768 |
qed |
|
1769 |
||
1770 |
lemma of_nat_euclidean_size: |
|
1771 |
"of_nat (euclidean_size a) = a div division_segment a" |
|
1772 |
proof - |
|
1773 |
have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a" |
|
1774 |
by (subst nonzero_mult_div_cancel_left) simp_all |
|
1775 |
also have "\<dots> = a div division_segment a" |
|
1776 |
by simp |
|
1777 |
finally show ?thesis . |
|
1778 |
qed |
|
1779 |
||
1780 |
lemma division_segment_1 [simp]: |
|
1781 |
"division_segment 1 = 1" |
|
1782 |
using division_segment_of_nat [of 1] by simp |
|
1783 |
||
1784 |
lemma division_segment_numeral [simp]: |
|
1785 |
"division_segment (numeral k) = 1" |
|
1786 |
using division_segment_of_nat [of "numeral k"] by simp |
|
1787 |
||
1788 |
lemma euclidean_size_1 [simp]: |
|
1789 |
"euclidean_size 1 = 1" |
|
1790 |
using euclidean_size_of_nat [of 1] by simp |
|
1791 |
||
1792 |
lemma euclidean_size_numeral [simp]: |
|
1793 |
"euclidean_size (numeral k) = numeral k" |
|
1794 |
using euclidean_size_of_nat [of "numeral k"] by simp |
|
1795 |
||
1796 |
lemma of_nat_dvd_iff: |
|
1797 |
"of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q") |
|
1798 |
proof (cases "m = 0") |
|
1799 |
case True |
|
1800 |
then show ?thesis |
|
1801 |
by simp |
|
1802 |
next |
|
1803 |
case False |
|
1804 |
show ?thesis |
|
1805 |
proof |
|
1806 |
assume ?Q |
|
1807 |
then show ?P |
|
1808 |
by auto |
|
1809 |
next |
|
1810 |
assume ?P |
|
1811 |
with False have "of_nat n = of_nat n div of_nat m * of_nat m" |
|
1812 |
by simp |
|
1813 |
then have "of_nat n = of_nat (n div m * m)" |
|
1814 |
by (simp add: of_nat_div) |
|
1815 |
then have "n = n div m * m" |
|
1816 |
by (simp only: of_nat_eq_iff) |
|
1817 |
then have "n = m * (n div m)" |
|
1818 |
by (simp add: ac_simps) |
|
1819 |
then show ?Q .. |
|
1820 |
qed |
|
1821 |
qed |
|
1822 |
||
1823 |
lemma of_nat_mod: |
|
1824 |
"of_nat (m mod n) = of_nat m mod of_nat n" |
|
1825 |
proof - |
|
1826 |
have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m" |
|
1827 |
by (simp add: div_mult_mod_eq) |
|
1828 |
also have "of_nat m = of_nat (m div n * n + m mod n)" |
|
1829 |
by simp |
|
1830 |
finally show ?thesis |
|
1831 |
by (simp only: of_nat_div of_nat_mult of_nat_add) simp |
|
1832 |
qed |
|
1833 |
||
1834 |
lemma one_div_two_eq_zero [simp]: |
|
1835 |
"1 div 2 = 0" |
|
1836 |
proof - |
|
1837 |
from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0" |
|
1838 |
by (simp only:) simp |
|
1839 |
then show ?thesis |
|
1840 |
by simp |
|
1841 |
qed |
|
1842 |
||
1843 |
lemma one_mod_two_eq_one [simp]: |
|
1844 |
"1 mod 2 = 1" |
|
1845 |
proof - |
|
1846 |
from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1" |
|
1847 |
by (simp only:) simp |
|
1848 |
then show ?thesis |
|
1849 |
by simp |
|
1850 |
qed |
|
1851 |
||
1852 |
lemma one_mod_2_pow_eq [simp]: |
|
1853 |
"1 mod (2 ^ n) = of_bool (n > 0)" |
|
1854 |
proof - |
|
1855 |
have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))" |
|
1856 |
using of_nat_mod [of 1 "2 ^ n"] by simp |
|
1857 |
also have "\<dots> = of_bool (n > 0)" |
|
1858 |
by simp |
|
1859 |
finally show ?thesis . |
|
1860 |
qed |
|
1861 |
||
1862 |
lemma one_div_2_pow_eq [simp]: |
|
1863 |
"1 div (2 ^ n) = of_bool (n = 0)" |
|
1864 |
using div_mult_mod_eq [of 1 "2 ^ n"] by auto |
|
1865 |
||
1866 |
lemma div_mult2_eq': |
|
1867 |
"a div (of_nat m * of_nat n) = a div of_nat m div of_nat n" |
|
1868 |
proof (cases a "of_nat m * of_nat n" rule: divmod_cases) |
|
1869 |
case (divides q) |
|
1870 |
then show ?thesis |
|
1871 |
using nonzero_mult_div_cancel_right [of "of_nat m" "q * of_nat n"] |
|
1872 |
by (simp add: ac_simps) |
|
1873 |
next |
|
1874 |
case (remainder q r) |
|
1875 |
then have "division_segment r = 1" |
|
1876 |
using division_segment_of_nat [of "m * n"] by simp |
|
1877 |
with division_segment_euclidean_size [of r] |
|
1878 |
have "of_nat (euclidean_size r) = r" |
|
1879 |
by simp |
|
1880 |
have "a mod (of_nat m * of_nat n) div (of_nat m * of_nat n) = 0" |
|
1881 |
by simp |
|
1882 |
with remainder(6) have "r div (of_nat m * of_nat n) = 0" |
|
1883 |
by simp |
|
1884 |
with \<open>of_nat (euclidean_size r) = r\<close> |
|
1885 |
have "of_nat (euclidean_size r) div (of_nat m * of_nat n) = 0" |
|
1886 |
by simp |
|
1887 |
then have "of_nat (euclidean_size r div (m * n)) = 0" |
|
1888 |
by (simp add: of_nat_div) |
|
1889 |
then have "of_nat (euclidean_size r div m div n) = 0" |
|
1890 |
by (simp add: div_mult2_eq) |
|
1891 |
with \<open>of_nat (euclidean_size r) = r\<close> have "r div of_nat m div of_nat n = 0" |
|
1892 |
by (simp add: of_nat_div) |
|
1893 |
with remainder(1) |
|
1894 |
have "q = (r div of_nat m + q * of_nat n * of_nat m div of_nat m) div of_nat n" |
|
1895 |
by simp |
|
1896 |
with remainder(5) remainder(7) show ?thesis |
|
1897 |
using div_plus_div_distrib_dvd_right [of "of_nat m" "q * (of_nat m * of_nat n)" r] |
|
1898 |
by (simp add: ac_simps) |
|
1899 |
next |
|
1900 |
case by0 |
|
1901 |
then show ?thesis |
|
1902 |
by auto |
|
1903 |
qed |
|
1904 |
||
1905 |
lemma mod_mult2_eq': |
|
1906 |
"a mod (of_nat m * of_nat n) = of_nat m * (a div of_nat m mod of_nat n) + a mod of_nat m" |
|
1907 |
proof - |
|
1908 |
have "a div (of_nat m * of_nat n) * (of_nat m * of_nat n) + a mod (of_nat m * of_nat n) = a div of_nat m div of_nat n * of_nat n * of_nat m + (a div of_nat m mod of_nat n * of_nat m + a mod of_nat m)" |
|
1909 |
by (simp add: combine_common_factor div_mult_mod_eq) |
|
1910 |
moreover have "a div of_nat m div of_nat n * of_nat n * of_nat m = of_nat n * of_nat m * (a div of_nat m div of_nat n)" |
|
1911 |
by (simp add: ac_simps) |
|
1912 |
ultimately show ?thesis |
|
1913 |
by (simp add: div_mult2_eq' mult_commute) |
|
1914 |
qed |
|
1915 |
||
1916 |
lemma div_mult2_numeral_eq: |
|
1917 |
"a div numeral k div numeral l = a div numeral (k * l)" (is "?A = ?B") |
|
1918 |
proof - |
|
1919 |
have "?A = a div of_nat (numeral k) div of_nat (numeral l)" |
|
1920 |
by simp |
|
1921 |
also have "\<dots> = a div (of_nat (numeral k) * of_nat (numeral l))" |
|
1922 |
by (fact div_mult2_eq' [symmetric]) |
|
1923 |
also have "\<dots> = ?B" |
|
1924 |
by simp |
|
1925 |
finally show ?thesis . |
|
1926 |
qed |
|
1927 |
||
1928 |
lemma numeral_Bit0_div_2: |
|
1929 |
"numeral (num.Bit0 n) div 2 = numeral n" |
|
1930 |
proof - |
|
1931 |
have "numeral (num.Bit0 n) = numeral n + numeral n" |
|
1932 |
by (simp only: numeral.simps) |
|
1933 |
also have "\<dots> = numeral n * 2" |
|
1934 |
by (simp add: mult_2_right) |
|
1935 |
finally have "numeral (num.Bit0 n) div 2 = numeral n * 2 div 2" |
|
1936 |
by simp |
|
1937 |
also have "\<dots> = numeral n" |
|
1938 |
by (rule nonzero_mult_div_cancel_right) simp |
|
1939 |
finally show ?thesis . |
|
1940 |
qed |
|
1941 |
||
1942 |
lemma numeral_Bit1_div_2: |
|
1943 |
"numeral (num.Bit1 n) div 2 = numeral n" |
|
1944 |
proof - |
|
1945 |
have "numeral (num.Bit1 n) = numeral n + numeral n + 1" |
|
1946 |
by (simp only: numeral.simps) |
|
1947 |
also have "\<dots> = numeral n * 2 + 1" |
|
1948 |
by (simp add: mult_2_right) |
|
1949 |
finally have "numeral (num.Bit1 n) div 2 = (numeral n * 2 + 1) div 2" |
|
1950 |
by simp |
|
1951 |
also have "\<dots> = numeral n * 2 div 2 + 1 div 2" |
|
1952 |
using dvd_triv_right by (rule div_plus_div_distrib_dvd_left) |
|
1953 |
also have "\<dots> = numeral n * 2 div 2" |
|
1954 |
by simp |
|
1955 |
also have "\<dots> = numeral n" |
|
1956 |
by (rule nonzero_mult_div_cancel_right) simp |
|
1957 |
finally show ?thesis . |
|
1958 |
qed |
|
1959 |
||
1960 |
lemma exp_mod_exp: |
|
1961 |
\<open>2 ^ m mod 2 ^ n = of_bool (m < n) * 2 ^ m\<close> |
|
1962 |
proof - |
|
1963 |
have \<open>(2::nat) ^ m mod 2 ^ n = of_bool (m < n) * 2 ^ m\<close> (is \<open>?lhs = ?rhs\<close>) |
|
1964 |
by (auto simp add: not_less monoid_mult_class.power_add dest!: le_Suc_ex) |
|
1965 |
then have \<open>of_nat ?lhs = of_nat ?rhs\<close> |
|
1966 |
by simp |
|
1967 |
then show ?thesis |
|
1968 |
by (simp add: of_nat_mod) |
|
1969 |
qed |
|
1970 |
||
71412 | 1971 |
lemma mask_mod_exp: |
71408 | 1972 |
\<open>(2 ^ n - 1) mod 2 ^ m = 2 ^ min m n - 1\<close> |
1973 |
proof - |
|
1974 |
have \<open>(2 ^ n - 1) mod 2 ^ m = 2 ^ min m n - (1::nat)\<close> (is \<open>?lhs = ?rhs\<close>) |
|
1975 |
proof (cases \<open>n \<le> m\<close>) |
|
1976 |
case True |
|
1977 |
then show ?thesis |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
1978 |
by (simp add: Suc_le_lessD) |
71408 | 1979 |
next |
1980 |
case False |
|
1981 |
then have \<open>m < n\<close> |
|
1982 |
by simp |
|
1983 |
then obtain q where n: \<open>n = Suc q + m\<close> |
|
1984 |
by (auto dest: less_imp_Suc_add) |
|
1985 |
then have \<open>min m n = m\<close> |
|
1986 |
by simp |
|
1987 |
moreover have \<open>(2::nat) ^ m \<le> 2 * 2 ^ q * 2 ^ m\<close> |
|
1988 |
using mult_le_mono1 [of 1 \<open>2 * 2 ^ q\<close> \<open>2 ^ m\<close>] by simp |
|
1989 |
with n have \<open>2 ^ n - 1 = (2 ^ Suc q - 1) * 2 ^ m + (2 ^ m - (1::nat))\<close> |
|
1990 |
by (simp add: monoid_mult_class.power_add algebra_simps) |
|
1991 |
ultimately show ?thesis |
|
1992 |
by (simp only: euclidean_semiring_cancel_class.mod_mult_self3) simp |
|
1993 |
qed |
|
1994 |
then have \<open>of_nat ?lhs = of_nat ?rhs\<close> |
|
1995 |
by simp |
|
1996 |
then show ?thesis |
|
1997 |
by (simp add: of_nat_mod of_nat_diff) |
|
1998 |
qed |
|
1999 |
||
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71413
diff
changeset
|
2000 |
lemma of_bool_half_eq_0 [simp]: |
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71413
diff
changeset
|
2001 |
\<open>of_bool b div 2 = 0\<close> |
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71413
diff
changeset
|
2002 |
by simp |
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71413
diff
changeset
|
2003 |
|
71157 | 2004 |
end |
2005 |
||
2006 |
class unique_euclidean_ring_with_nat = ring + unique_euclidean_semiring_with_nat |
|
2007 |
||
2008 |
instance nat :: unique_euclidean_semiring_with_nat |
|
2009 |
by standard (simp_all add: dvd_eq_mod_eq_0) |
|
2010 |
||
2011 |
instance int :: unique_euclidean_ring_with_nat |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2012 |
by standard (auto simp add: divide_int_def division_segment_int_def elim: contrapos_np) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2013 |
|
75876 | 2014 |
|
2015 |
subsection \<open>More on euclidean division on \<^typ>\<open>int\<close>\<close> |
|
2016 |
||
2017 |
subsubsection \<open>Trivial reduction steps\<close> |
|
2018 |
||
2019 |
lemma div_pos_pos_trivial [simp]: |
|
2020 |
"k div l = 0" if "k \<ge> 0" and "k < l" for k l :: int |
|
2021 |
using that by (simp add: unique_euclidean_semiring_class.div_eq_0_iff division_segment_int_def) |
|
2022 |
||
2023 |
lemma mod_pos_pos_trivial [simp]: |
|
2024 |
"k mod l = k" if "k \<ge> 0" and "k < l" for k l :: int |
|
2025 |
using that by (simp add: mod_eq_self_iff_div_eq_0) |
|
2026 |
||
2027 |
lemma div_neg_neg_trivial [simp]: |
|
2028 |
"k div l = 0" if "k \<le> 0" and "l < k" for k l :: int |
|
2029 |
using that by (cases "k = 0") (simp, simp add: unique_euclidean_semiring_class.div_eq_0_iff division_segment_int_def) |
|
2030 |
||
2031 |
lemma mod_neg_neg_trivial [simp]: |
|
2032 |
"k mod l = k" if "k \<le> 0" and "l < k" for k l :: int |
|
2033 |
using that by (simp add: mod_eq_self_iff_div_eq_0) |
|
2034 |
||
2035 |
lemma div_pos_neg_trivial: |
|
2036 |
"k div l = - 1" if "0 < k" and "k + l \<le> 0" for k l :: int |
|
2037 |
proof (cases \<open>l = - k\<close>) |
|
2038 |
case True |
|
2039 |
with that show ?thesis |
|
2040 |
by (simp add: divide_int_def) |
|
2041 |
next |
|
2042 |
case False |
|
2043 |
show ?thesis |
|
2044 |
apply (rule div_eqI [of _ "k + l"]) |
|
2045 |
using False that apply (simp_all add: division_segment_int_def) |
|
2046 |
done |
|
2047 |
qed |
|
2048 |
||
2049 |
lemma mod_pos_neg_trivial: |
|
2050 |
"k mod l = k + l" if "0 < k" and "k + l \<le> 0" for k l :: int |
|
2051 |
proof (cases \<open>l = - k\<close>) |
|
2052 |
case True |
|
2053 |
with that show ?thesis |
|
2054 |
by (simp add: divide_int_def) |
|
2055 |
next |
|
2056 |
case False |
|
2057 |
show ?thesis |
|
2058 |
apply (rule mod_eqI [of _ _ \<open>- 1\<close>]) |
|
2059 |
using False that apply (simp_all add: division_segment_int_def) |
|
2060 |
done |
|
2061 |
qed |
|
2062 |
||
2063 |
text \<open>There is neither \<open>div_neg_pos_trivial\<close> nor \<open>mod_neg_pos_trivial\<close> |
|
2064 |
because \<^term>\<open>0 div l = 0\<close> would supersede it.\<close> |
|
2065 |
||
2066 |
||
75877 | 2067 |
subsubsection \<open>Laws for unary minus\<close> |
2068 |
||
2069 |
lemma zmod_zminus1_not_zero: |
|
2070 |
fixes k l :: int |
|
2071 |
shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0" |
|
2072 |
by (simp add: mod_eq_0_iff_dvd) |
|
2073 |
||
2074 |
lemma zmod_zminus2_not_zero: |
|
2075 |
fixes k l :: int |
|
2076 |
shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0" |
|
2077 |
by (simp add: mod_eq_0_iff_dvd) |
|
2078 |
||
2079 |
lemma zdiv_zminus1_eq_if: |
|
2080 |
\<open>(- a) div b = (if a mod b = 0 then - (a div b) else - (a div b) - 1)\<close> |
|
2081 |
if \<open>b \<noteq> 0\<close> for a b :: int |
|
2082 |
using that sgn_not_eq_imp [of b \<open>- a\<close>] |
|
2083 |
by (cases \<open>a = 0\<close>) (auto simp add: div_eq_div_abs [of \<open>- a\<close> b] div_eq_div_abs [of a b] sgn_eq_0_iff) |
|
2084 |
||
2085 |
lemma zdiv_zminus2_eq_if: |
|
2086 |
\<open>a div (- b) = (if a mod b = 0 then - (a div b) else - (a div b) - 1)\<close> |
|
2087 |
if \<open>b \<noteq> 0\<close> for a b :: int |
|
2088 |
using that by (auto simp add: zdiv_zminus1_eq_if div_minus_right) |
|
2089 |
||
2090 |
lemma zmod_zminus1_eq_if: |
|
2091 |
\<open>(- a) mod b = (if a mod b = 0 then 0 else b - (a mod b))\<close> |
|
2092 |
for a b :: int |
|
2093 |
by (cases \<open>b = 0\<close>) |
|
2094 |
(auto simp flip: minus_div_mult_eq_mod simp add: zdiv_zminus1_eq_if algebra_simps) |
|
2095 |
||
2096 |
lemma zmod_zminus2_eq_if: |
|
2097 |
\<open>a mod (- b) = (if a mod b = 0 then 0 else (a mod b) - b)\<close> |
|
2098 |
for a b :: int |
|
2099 |
by (auto simp add: zmod_zminus1_eq_if mod_minus_right) |
|
2100 |
||
2101 |
||
75876 | 2102 |
subsubsection \<open>Borders\<close> |
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2103 |
|
75876 | 2104 |
lemma pos_mod_bound [simp]: |
2105 |
"k mod l < l" if "l > 0" for k l :: int |
|
2106 |
proof - |
|
2107 |
obtain m and s where "k = sgn s * int m" |
|
2108 |
by (rule int_sgnE) |
|
2109 |
moreover from that obtain n where "l = sgn 1 * int n" |
|
2110 |
by (cases l) simp_all |
|
2111 |
moreover from this that have "n > 0" |
|
2112 |
by simp |
|
2113 |
ultimately show ?thesis |
|
2114 |
by (simp only: modulo_int_unfold) |
|
2115 |
(auto simp add: mod_greater_zero_iff_not_dvd sgn_1_pos) |
|
2116 |
qed |
|
2117 |
||
2118 |
lemma neg_mod_bound [simp]: |
|
2119 |
"l < k mod l" if "l < 0" for k l :: int |
|
2120 |
proof - |
|
2121 |
obtain m and s where "k = sgn s * int m" |
|
2122 |
by (rule int_sgnE) |
|
2123 |
moreover from that obtain q where "l = sgn (- 1) * int (Suc q)" |
|
2124 |
by (cases l) simp_all |
|
2125 |
moreover define n where "n = Suc q" |
|
2126 |
then have "Suc q = n" |
|
2127 |
by simp |
|
2128 |
ultimately show ?thesis |
|
2129 |
by (simp only: modulo_int_unfold) |
|
2130 |
(auto simp add: mod_greater_zero_iff_not_dvd sgn_1_neg) |
|
2131 |
qed |
|
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2132 |
|
75876 | 2133 |
lemma pos_mod_sign [simp]: |
2134 |
"0 \<le> k mod l" if "l > 0" for k l :: int |
|
2135 |
proof - |
|
2136 |
obtain m and s where "k = sgn s * int m" |
|
2137 |
by (rule int_sgnE) |
|
2138 |
moreover from that obtain n where "l = sgn 1 * int n" |
|
2139 |
by (cases l) auto |
|
2140 |
moreover from this that have "n > 0" |
|
2141 |
by simp |
|
2142 |
ultimately show ?thesis |
|
2143 |
by (simp only: modulo_int_unfold) (auto simp add: sgn_1_pos) |
|
2144 |
qed |
|
2145 |
||
2146 |
lemma neg_mod_sign [simp]: |
|
2147 |
"k mod l \<le> 0" if "l < 0" for k l :: int |
|
2148 |
proof - |
|
2149 |
obtain m and s where "k = sgn s * int m" |
|
2150 |
by (rule int_sgnE) |
|
2151 |
moreover from that obtain q where "l = sgn (- 1) * int (Suc q)" |
|
2152 |
by (cases l) simp_all |
|
2153 |
moreover define n where "n = Suc q" |
|
2154 |
then have "Suc q = n" |
|
2155 |
by simp |
|
2156 |
moreover have \<open>int (m mod n) \<le> int n\<close> |
|
2157 |
using \<open>Suc q = n\<close> by simp |
|
2158 |
then have \<open>sgn s * int (m mod n) \<le> int n\<close> |
|
2159 |
by (cases s \<open>0::int\<close> rule: linorder_cases) simp_all |
|
2160 |
ultimately show ?thesis |
|
2161 |
by (simp only: modulo_int_unfold) auto |
|
2162 |
qed |
|
2163 |
||
2164 |
||
75881 | 2165 |
subsubsection \<open>Splitting Rules for div and mod\<close> |
2166 |
||
2167 |
lemma split_zdiv: |
|
2168 |
\<open>P (n div k) \<longleftrightarrow> |
|
2169 |
(k = 0 \<longrightarrow> P 0) \<and> |
|
2170 |
(0 < k \<longrightarrow> (\<forall>i j. 0 \<le> j \<and> j < k \<and> n = k * i + j \<longrightarrow> P i)) \<and> |
|
2171 |
(k < 0 \<longrightarrow> (\<forall>i j. k < j \<and> j \<le> 0 \<and> n = k * i + j \<longrightarrow> P i))\<close> (is ?div) |
|
2172 |
and split_zmod: |
|
2173 |
\<open>Q (n mod k) \<longleftrightarrow> |
|
2174 |
(k = 0 \<longrightarrow> Q n) \<and> |
|
2175 |
(0 < k \<longrightarrow> (\<forall>i j. 0 \<le> j \<and> j < k \<and> n = k * i + j \<longrightarrow> Q j)) \<and> |
|
2176 |
(k < 0 \<longrightarrow> (\<forall>i j. k < j \<and> j \<le> 0 \<and> n = k * i + j \<longrightarrow> Q j))\<close> (is ?mod) |
|
2177 |
for n k :: int |
|
2178 |
proof - |
|
2179 |
have *: \<open>R (n div k) (n mod k) \<longleftrightarrow> |
|
2180 |
(k = 0 \<longrightarrow> R 0 n) \<and> |
|
2181 |
(0 < k \<longrightarrow> (\<forall>i j. 0 \<le> j \<and> j < k \<and> n = k * i + j \<longrightarrow> R i j)) \<and> |
|
2182 |
(k < 0 \<longrightarrow> (\<forall>i j. k < j \<and> j \<le> 0 \<and> n = k * i + j \<longrightarrow> R i j))\<close> for R |
|
2183 |
by (cases \<open>k = 0\<close>) |
|
2184 |
(auto simp add: linorder_class.neq_iff) |
|
2185 |
from * [of \<open>\<lambda>q _. P q\<close>] show ?div . |
|
2186 |
from * [of \<open>\<lambda>_ r. Q r\<close>] show ?mod . |
|
2187 |
qed |
|
2188 |
||
2189 |
text \<open>Enable (lin)arith to deal with \<^const>\<open>divide\<close> and \<^const>\<open>modulo\<close> |
|
2190 |
when these are applied to some constant that is of the form |
|
2191 |
\<^term>\<open>numeral k\<close>:\<close> |
|
2192 |
declare split_zdiv [of _ _ \<open>numeral n\<close>, linarith_split] for n |
|
2193 |
declare split_zdiv [of _ _ \<open>- numeral n\<close>, linarith_split] for n |
|
2194 |
declare split_zmod [of _ _ \<open>numeral n\<close>, linarith_split] for n |
|
2195 |
declare split_zmod [of _ _ \<open>- numeral n\<close>, linarith_split] for n |
|
2196 |
||
2197 |
lemma zdiv_eq_0_iff: |
|
2198 |
"i div k = 0 \<longleftrightarrow> k = 0 \<or> 0 \<le> i \<and> i < k \<or> i \<le> 0 \<and> k < i" (is "?L = ?R") |
|
2199 |
for i k :: int |
|
2200 |
proof |
|
2201 |
assume ?L |
|
2202 |
moreover have "?L \<longrightarrow> ?R" |
|
2203 |
by (rule split_zdiv [THEN iffD2]) simp |
|
2204 |
ultimately show ?R |
|
2205 |
by blast |
|
2206 |
next |
|
2207 |
assume ?R then show ?L |
|
2208 |
by auto |
|
2209 |
qed |
|
2210 |
||
2211 |
lemma zmod_trivial_iff: |
|
2212 |
fixes i k :: int |
|
2213 |
shows "i mod k = i \<longleftrightarrow> k = 0 \<or> 0 \<le> i \<and> i < k \<or> i \<le> 0 \<and> k < i" |
|
2214 |
proof - |
|
2215 |
have "i mod k = i \<longleftrightarrow> i div k = 0" |
|
2216 |
using div_mult_mod_eq [of i k] by safe auto |
|
2217 |
with zdiv_eq_0_iff |
|
2218 |
show ?thesis |
|
2219 |
by simp |
|
2220 |
qed |
|
2221 |
||
2222 |
||
75876 | 2223 |
subsubsection \<open>Algebraic rewrites\<close> |
71157 | 2224 |
|
74592 | 2225 |
lemma zdiv_zmult2_eq: |
2226 |
\<open>a div (b * c) = (a div b) div c\<close> if \<open>c \<ge> 0\<close> for a b c :: int |
|
2227 |
proof (cases \<open>b \<ge> 0\<close>) |
|
2228 |
case True |
|
2229 |
with that show ?thesis |
|
2230 |
using div_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp |
|
2231 |
next |
|
2232 |
case False |
|
2233 |
with that show ?thesis |
|
2234 |
using div_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp |
|
2235 |
qed |
|
2236 |
||
75876 | 2237 |
lemma zdiv_zmult2_eq': |
2238 |
\<open>k div (l * j) = ((sgn j * k) div l) div \<bar>j\<bar>\<close> for k l j :: int |
|
2239 |
proof - |
|
2240 |
have \<open>k div (l * j) = (sgn j * k) div (sgn j * (l * j))\<close> |
|
2241 |
by (simp add: sgn_0_0) |
|
2242 |
also have \<open>sgn j * (l * j) = l * \<bar>j\<bar>\<close> |
|
2243 |
by (simp add: mult.left_commute [of _ l] abs_sgn) (simp add: ac_simps) |
|
2244 |
also have \<open>(sgn j * k) div (l * \<bar>j\<bar>) = ((sgn j * k) div l) div \<bar>j\<bar>\<close> |
|
2245 |
by (simp add: zdiv_zmult2_eq) |
|
2246 |
finally show ?thesis . |
|
2247 |
qed |
|
2248 |
||
74592 | 2249 |
lemma zmod_zmult2_eq: |
2250 |
\<open>a mod (b * c) = b * (a div b mod c) + a mod b\<close> if \<open>c \<ge> 0\<close> for a b c :: int |
|
2251 |
proof (cases \<open>b \<ge> 0\<close>) |
|
2252 |
case True |
|
2253 |
with that show ?thesis |
|
2254 |
using mod_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp |
|
2255 |
next |
|
2256 |
case False |
|
2257 |
with that show ?thesis |
|
2258 |
using mod_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp |
|
2259 |
qed |
|
2260 |
||
75881 | 2261 |
lemma half_nonnegative_int_iff [simp]: |
2262 |
\<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
2263 |
by auto |
|
2264 |
||
2265 |
lemma half_negative_int_iff [simp]: |
|
2266 |
\<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
2267 |
by auto |
|
2268 |
||
75876 | 2269 |
|
2270 |
subsubsection \<open>Distributive laws for conversions.\<close> |
|
2271 |
||
75875
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2272 |
lemma zdiv_int: |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2273 |
"int (a div b) = int a div int b" |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2274 |
by (fact of_nat_div) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2275 |
|
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2276 |
lemma zmod_int: |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2277 |
"int (a mod b) = int a mod int b" |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2278 |
by (fact of_nat_mod) |
48d032035744
streamlined primitive definitions for integer division
haftmann
parents:
75669
diff
changeset
|
2279 |
|
75876 | 2280 |
lemma nat_div_distrib: |
2281 |
\<open>nat (x div y) = nat x div nat y\<close> if \<open>0 \<le> x\<close> |
|
2282 |
using that by (simp add: divide_int_def sgn_if) |
|
2283 |
||
2284 |
lemma nat_div_distrib': |
|
2285 |
\<open>nat (x div y) = nat x div nat y\<close> if \<open>0 \<le> y\<close> |
|
2286 |
using that by (simp add: divide_int_def sgn_if) |
|
2287 |
||
2288 |
lemma nat_mod_distrib: \<comment> \<open>Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't\<close> |
|
2289 |
\<open>nat (x mod y) = nat x mod nat y\<close> if \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> |
|
2290 |
using that by (simp add: modulo_int_def sgn_if) |
|
2291 |
||
71157 | 2292 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2293 |
subsection \<open>Code generation\<close> |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2294 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2295 |
code_identifier |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2296 |
code_module Euclidean_Division \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith |
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2297 |
|
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66807
diff
changeset
|
2298 |
end |