author | haftmann |
Sat, 27 Jun 2015 20:20:36 +0200 | |
changeset 60599 | f8bb070dc98b |
parent 60598 | 78ca5674c66a |
child 60600 | 87fbfea0bd0a |
permissions | -rw-r--r-- |
58023 | 1 |
(* Author: Manuel Eberl *) |
2 |
||
60526 | 3 |
section \<open>Abstract euclidean algorithm\<close> |
58023 | 4 |
|
5 |
theory Euclidean_Algorithm |
|
60571 | 6 |
imports Complex_Main "~~/src/HOL/Library/Polynomial" |
58023 | 7 |
begin |
60433 | 8 |
|
60526 | 9 |
text \<open> |
58023 | 10 |
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be |
11 |
implemented. It must provide: |
|
12 |
\begin{itemize} |
|
13 |
\item division with remainder |
|
14 |
\item a size function such that @{term "size (a mod b) < size b"} |
|
15 |
for any @{term "b \<noteq> 0"} |
|
60438 | 16 |
\item a normalization factor such that two associated numbers are equal iff |
17 |
they are the same when divd by their normalization factors. |
|
58023 | 18 |
\end{itemize} |
19 |
The existence of these functions makes it possible to derive gcd and lcm functions |
|
20 |
for any Euclidean semiring. |
|
60526 | 21 |
\<close> |
58023 | 22 |
class euclidean_semiring = semiring_div + |
23 |
fixes euclidean_size :: "'a \<Rightarrow> nat" |
|
60438 | 24 |
fixes normalization_factor :: "'a \<Rightarrow> 'a" |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
25 |
assumes mod_size_less: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
26 |
"b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b" |
58023 | 27 |
assumes size_mult_mono: |
28 |
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a" |
|
60438 | 29 |
assumes normalization_factor_is_unit [intro,simp]: |
30 |
"a \<noteq> 0 \<Longrightarrow> is_unit (normalization_factor a)" |
|
31 |
assumes normalization_factor_mult: "normalization_factor (a * b) = |
|
32 |
normalization_factor a * normalization_factor b" |
|
33 |
assumes normalization_factor_unit: "is_unit a \<Longrightarrow> normalization_factor a = a" |
|
34 |
assumes normalization_factor_0 [simp]: "normalization_factor 0 = 0" |
|
58023 | 35 |
begin |
36 |
||
60438 | 37 |
lemma normalization_factor_dvd [simp]: |
38 |
"a \<noteq> 0 \<Longrightarrow> normalization_factor a dvd b" |
|
58023 | 39 |
by (rule unit_imp_dvd, simp) |
40 |
||
60438 | 41 |
lemma normalization_factor_1 [simp]: |
42 |
"normalization_factor 1 = 1" |
|
43 |
by (simp add: normalization_factor_unit) |
|
58023 | 44 |
|
60438 | 45 |
lemma normalization_factor_0_iff [simp]: |
46 |
"normalization_factor a = 0 \<longleftrightarrow> a = 0" |
|
58023 | 47 |
proof |
60438 | 48 |
assume "normalization_factor a = 0" |
49 |
hence "\<not> is_unit (normalization_factor a)" |
|
60433 | 50 |
by simp |
51 |
then show "a = 0" by auto |
|
52 |
qed simp |
|
58023 | 53 |
|
60438 | 54 |
lemma normalization_factor_pow: |
55 |
"normalization_factor (a ^ n) = normalization_factor a ^ n" |
|
56 |
by (induct n) (simp_all add: normalization_factor_mult power_Suc2) |
|
58023 | 57 |
|
60438 | 58 |
lemma normalization_correct [simp]: |
59 |
"normalization_factor (a div normalization_factor a) = (if a = 0 then 0 else 1)" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
60 |
proof (cases "a = 0", simp) |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
61 |
assume "a \<noteq> 0" |
60438 | 62 |
let ?nf = "normalization_factor" |
60526 | 63 |
from normalization_factor_is_unit[OF \<open>a \<noteq> 0\<close>] have "?nf a \<noteq> 0" |
60433 | 64 |
by auto |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
65 |
have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)" |
60438 | 66 |
by (simp add: normalization_factor_mult) |
60526 | 67 |
also have "a div ?nf a * ?nf a = a" using \<open>a \<noteq> 0\<close> |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
68 |
by simp |
60526 | 69 |
also have "?nf (?nf a) = ?nf a" using \<open>a \<noteq> 0\<close> |
60438 | 70 |
normalization_factor_is_unit normalization_factor_unit by simp |
71 |
finally have "normalization_factor (a div normalization_factor a) = 1" |
|
60526 | 72 |
using \<open>?nf a \<noteq> 0\<close> by (metis div_mult_self2_is_id div_self) |
73 |
with \<open>a \<noteq> 0\<close> show ?thesis by simp |
|
58023 | 74 |
qed |
75 |
||
60438 | 76 |
lemma normalization_0_iff [simp]: |
77 |
"a div normalization_factor a = 0 \<longleftrightarrow> a = 0" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
78 |
by (cases "a = 0", simp, subst unit_eq_div1, blast, simp) |
58023 | 79 |
|
60438 | 80 |
lemma mult_div_normalization [simp]: |
81 |
"b * (1 div normalization_factor a) = b div normalization_factor a" |
|
60433 | 82 |
by (cases "a = 0") simp_all |
83 |
||
58023 | 84 |
lemma associated_iff_normed_eq: |
60599 | 85 |
"associated a b \<longleftrightarrow> a div normalization_factor a = b div normalization_factor b" (is "?P \<longleftrightarrow> ?Q") |
86 |
proof (cases "a = 0 \<or> b = 0") |
|
87 |
case True then show ?thesis by (auto dest: sym) |
|
58023 | 88 |
next |
60599 | 89 |
case False then have "a \<noteq> 0" and "b \<noteq> 0" by auto |
90 |
show ?thesis |
|
91 |
proof |
|
92 |
assume ?Q |
|
93 |
from \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> |
|
94 |
have "is_unit (normalization_factor a div normalization_factor b)" |
|
95 |
by auto |
|
96 |
moreover from \<open>?Q\<close> \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> |
|
97 |
have "a = (normalization_factor a div normalization_factor b) * b" |
|
98 |
by (simp add: ac_simps div_mult_swap unit_eq_div1) |
|
99 |
ultimately show "associated a b" by (rule is_unit_associatedI) |
|
100 |
next |
|
101 |
assume ?P |
|
102 |
then obtain c where "is_unit c" and "a = c * b" |
|
103 |
by (blast elim: associated_is_unitE) |
|
104 |
then show ?Q |
|
105 |
by (auto simp add: normalization_factor_mult normalization_factor_unit) |
|
58023 | 106 |
qed |
60599 | 107 |
qed |
58023 | 108 |
|
109 |
lemma normed_associated_imp_eq: |
|
60438 | 110 |
"associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b" |
58023 | 111 |
by (simp add: associated_iff_normed_eq, elim disjE, simp_all) |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
112 |
|
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
113 |
lemma normed_dvd [iff]: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
114 |
"a div normalization_factor a dvd a" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
115 |
proof (cases "a = 0") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
116 |
case True then show ?thesis by simp |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
117 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
118 |
case False |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
119 |
then have "a = a div normalization_factor a * normalization_factor a" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
120 |
by (auto intro: unit_div_mult_self) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
121 |
then show ?thesis .. |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
122 |
qed |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
123 |
|
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
124 |
lemma dvd_normed [iff]: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
125 |
"a dvd a div normalization_factor a" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
126 |
proof (cases "a = 0") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
127 |
case True then show ?thesis by simp |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
128 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
129 |
case False |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
130 |
then have "a div normalization_factor a = a * (1 div normalization_factor a)" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
131 |
by (auto intro: unit_mult_div_div) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
132 |
then show ?thesis .. |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
133 |
qed |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
134 |
|
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
135 |
lemma associated_normed: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
136 |
"associated (a div normalization_factor a) a" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
137 |
by (rule associatedI) simp_all |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
138 |
|
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
139 |
lemma normalization_factor_dvd' [simp]: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
140 |
"normalization_factor a dvd a" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
141 |
by (cases "a = 0", simp_all) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
142 |
|
60438 | 143 |
lemmas normalization_factor_dvd_iff [simp] = |
144 |
unit_dvd_iff [OF normalization_factor_is_unit] |
|
58023 | 145 |
|
146 |
lemma euclidean_division: |
|
147 |
fixes a :: 'a and b :: 'a |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
148 |
assumes "b \<noteq> 0" and "\<not> b dvd a" |
58023 | 149 |
obtains s and t where "a = s * b + t" |
150 |
and "euclidean_size t < euclidean_size b" |
|
151 |
proof - |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
152 |
from div_mod_equality [of a b 0] |
58023 | 153 |
have "a = a div b * b + a mod b" by simp |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
154 |
with that and assms show ?thesis by (auto simp add: mod_size_less) |
58023 | 155 |
qed |
156 |
||
157 |
lemma dvd_euclidean_size_eq_imp_dvd: |
|
158 |
assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b" |
|
159 |
shows "a dvd b" |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
160 |
proof (rule ccontr) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
161 |
assume "\<not> a dvd b" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
162 |
then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd) |
58023 | 163 |
from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff) |
164 |
from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast |
|
60526 | 165 |
with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto |
166 |
with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b" |
|
58023 | 167 |
using size_mult_mono by force |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
168 |
moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close> |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
169 |
have "euclidean_size (b mod a) < euclidean_size a" |
58023 | 170 |
using mod_size_less by blast |
171 |
ultimately show False using size_eq by simp |
|
172 |
qed |
|
173 |
||
174 |
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" |
|
175 |
where |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
176 |
"gcd_eucl a b = (if b = 0 then a div normalization_factor a |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
177 |
else if b dvd a then b div normalization_factor b |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
178 |
else gcd_eucl b (a mod b))" |
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
179 |
by pat_completeness simp |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
180 |
termination |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
181 |
by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less) |
58023 | 182 |
|
183 |
declare gcd_eucl.simps [simp del] |
|
184 |
||
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
185 |
lemma gcd_eucl_induct [case_names zero mod]: |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
186 |
assumes H1: "\<And>b. P b 0" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
187 |
and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
188 |
shows "P a b" |
58023 | 189 |
proof (induct a b rule: gcd_eucl.induct) |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
190 |
case ("1" a b) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
191 |
show ?case |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
192 |
proof (cases "b = 0") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
193 |
case True then show "P a b" by simp (rule H1) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
194 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
195 |
case False |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
196 |
have "P b (a mod b)" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
197 |
proof (cases "b dvd a") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
198 |
case False with \<open>b \<noteq> 0\<close> show "P b (a mod b)" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
199 |
by (rule "1.hyps") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
200 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
201 |
case True then have "a mod b = 0" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
202 |
by (simp add: mod_eq_0_iff_dvd) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
203 |
then show "P b (a mod b)" by simp (rule H1) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
204 |
qed |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
205 |
with \<open>b \<noteq> 0\<close> show "P a b" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
206 |
by (blast intro: H2) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
207 |
qed |
58023 | 208 |
qed |
209 |
||
210 |
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" |
|
211 |
where |
|
60438 | 212 |
"lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))" |
58023 | 213 |
|
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
214 |
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open> |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
215 |
Somewhat complicated definition of Lcm that has the advantage of working |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
216 |
for infinite sets as well\<close> |
58023 | 217 |
where |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
218 |
"Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
219 |
let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
220 |
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n) |
60438 | 221 |
in l div normalization_factor l |
58023 | 222 |
else 0)" |
223 |
||
224 |
definition Gcd_eucl :: "'a set \<Rightarrow> 'a" |
|
225 |
where |
|
226 |
"Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}" |
|
227 |
||
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
228 |
lemma gcd_eucl_0: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
229 |
"gcd_eucl a 0 = a div normalization_factor a" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
230 |
by (simp add: gcd_eucl.simps [of a 0]) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
231 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
232 |
lemma gcd_eucl_0_left: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
233 |
"gcd_eucl 0 a = a div normalization_factor a" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
234 |
by (simp add: gcd_eucl.simps [of 0 a]) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
235 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
236 |
lemma gcd_eucl_non_0: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
237 |
"b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
238 |
by (cases "b dvd a") |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
239 |
(simp_all add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0]) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
240 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
241 |
lemma gcd_eucl_code [code]: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
242 |
"gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
243 |
by (auto simp add: gcd_eucl_non_0 gcd_eucl_0 gcd_eucl_0_left) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
244 |
|
58023 | 245 |
end |
246 |
||
60598
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
247 |
class euclidean_ring = euclidean_semiring + idom |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
248 |
begin |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
249 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
250 |
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
251 |
"euclid_ext a b = |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
252 |
(if b = 0 then |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
253 |
let c = 1 div normalization_factor a in (c, 0, a * c) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
254 |
else if b dvd a then |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
255 |
let c = 1 div normalization_factor b in (0, c, b * c) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
256 |
else |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
257 |
case euclid_ext b (a mod b) of |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
258 |
(s, t, c) \<Rightarrow> (t, s - t * (a div b), c))" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
259 |
by pat_completeness simp |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
260 |
termination |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
261 |
by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
262 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
263 |
declare euclid_ext.simps [simp del] |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
264 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
265 |
lemma euclid_ext_0: |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
266 |
"euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
267 |
by (simp add: euclid_ext.simps [of a 0]) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
268 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
269 |
lemma euclid_ext_left_0: |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
270 |
"euclid_ext 0 a = (0, 1 div normalization_factor a, a div normalization_factor a)" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
271 |
by (simp add: euclid_ext.simps [of 0 a]) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
272 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
273 |
lemma euclid_ext_non_0: |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
274 |
"b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
275 |
(s, t, c) \<Rightarrow> (t, s - t * (a div b), c))" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
276 |
by (cases "b dvd a") |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
277 |
(simp_all add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
278 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
279 |
lemma euclid_ext_code [code]: |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
280 |
"euclid_ext a b = (if b = 0 then (1 div normalization_factor a, 0, a div normalization_factor a) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
281 |
else let (s, t, c) = euclid_ext b (a mod b) in (t, s - t * (a div b), c))" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
282 |
by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
283 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
284 |
lemma euclid_ext_correct: |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
285 |
"case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
286 |
proof (induct a b rule: gcd_eucl_induct) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
287 |
case (zero a) then show ?case |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
288 |
by (simp add: euclid_ext_0 ac_simps) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
289 |
next |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
290 |
case (mod a b) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
291 |
obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
292 |
by (cases "euclid_ext b (a mod b)") blast |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
293 |
with mod have "c = s * b + t * (a mod b)" by simp |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
294 |
also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
295 |
by (simp add: algebra_simps) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
296 |
also have "(a div b) * b + a mod b = a" using mod_div_equality . |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
297 |
finally show ?case |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
298 |
by (subst euclid_ext.simps) (simp add: stc mod ac_simps) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
299 |
qed |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
300 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
301 |
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
302 |
where |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
303 |
"euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
304 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
305 |
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div normalization_factor a, 0)" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
306 |
by (simp add: euclid_ext'_def euclid_ext_0) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
307 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
308 |
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div normalization_factor a)" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
309 |
by (simp add: euclid_ext'_def euclid_ext_left_0) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
310 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
311 |
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)), |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
312 |
fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))" |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
313 |
by (simp add: euclid_ext'_def euclid_ext_non_0 split_def) |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
314 |
|
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
315 |
end |
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset
|
316 |
|
58023 | 317 |
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd + |
318 |
assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl" |
|
319 |
assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl" |
|
320 |
begin |
|
321 |
||
322 |
lemma gcd_0_left: |
|
60438 | 323 |
"gcd 0 a = a div normalization_factor a" |
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
324 |
unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left) |
58023 | 325 |
|
326 |
lemma gcd_0: |
|
60438 | 327 |
"gcd a 0 = a div normalization_factor a" |
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
328 |
unfolding gcd_gcd_eucl by (fact gcd_eucl_0) |
58023 | 329 |
|
330 |
lemma gcd_non_0: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
331 |
"b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)" |
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
332 |
unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0) |
58023 | 333 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
334 |
lemma gcd_dvd1 [iff]: "gcd a b dvd a" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
335 |
and gcd_dvd2 [iff]: "gcd a b dvd b" |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
336 |
by (induct a b rule: gcd_eucl_induct) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
337 |
(simp_all add: gcd_0 gcd_non_0 dvd_mod_iff) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
338 |
|
58023 | 339 |
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m" |
340 |
by (rule dvd_trans, assumption, rule gcd_dvd1) |
|
341 |
||
342 |
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n" |
|
343 |
by (rule dvd_trans, assumption, rule gcd_dvd2) |
|
344 |
||
345 |
lemma gcd_greatest: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
346 |
fixes k a b :: 'a |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
347 |
shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b" |
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
348 |
proof (induct a b rule: gcd_eucl_induct) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
349 |
case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
350 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
351 |
case (mod a b) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
352 |
then show ?case |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
353 |
by (simp add: gcd_non_0 dvd_mod_iff) |
58023 | 354 |
qed |
355 |
||
356 |
lemma dvd_gcd_iff: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
357 |
"k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b" |
58023 | 358 |
by (blast intro!: gcd_greatest intro: dvd_trans) |
359 |
||
360 |
lemmas gcd_greatest_iff = dvd_gcd_iff |
|
361 |
||
362 |
lemma gcd_zero [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
363 |
"gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0" |
58023 | 364 |
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+ |
365 |
||
60438 | 366 |
lemma normalization_factor_gcd [simp]: |
367 |
"normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b") |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
368 |
by (induct a b rule: gcd_eucl_induct) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
369 |
(auto simp add: gcd_0 gcd_non_0) |
58023 | 370 |
|
371 |
lemma gcdI: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
372 |
"k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k) |
60438 | 373 |
\<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b" |
58023 | 374 |
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest) |
375 |
||
376 |
sublocale gcd!: abel_semigroup gcd |
|
377 |
proof |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
378 |
fix a b c |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
379 |
show "gcd (gcd a b) c = gcd a (gcd b c)" |
58023 | 380 |
proof (rule gcdI) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
381 |
have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
382 |
then show "gcd (gcd a b) c dvd a" by (rule dvd_trans) |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
383 |
have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
384 |
hence "gcd (gcd a b) c dvd b" by (rule dvd_trans) |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
385 |
moreover have "gcd (gcd a b) c dvd c" by simp |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
386 |
ultimately show "gcd (gcd a b) c dvd gcd b c" |
58023 | 387 |
by (rule gcd_greatest) |
60438 | 388 |
show "normalization_factor (gcd (gcd a b) c) = (if gcd (gcd a b) c = 0 then 0 else 1)" |
58023 | 389 |
by auto |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
390 |
fix l assume "l dvd a" and "l dvd gcd b c" |
58023 | 391 |
with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2] |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
392 |
have "l dvd b" and "l dvd c" by blast+ |
60526 | 393 |
with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c" |
58023 | 394 |
by (intro gcd_greatest) |
395 |
qed |
|
396 |
next |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
397 |
fix a b |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
398 |
show "gcd a b = gcd b a" |
58023 | 399 |
by (rule gcdI) (simp_all add: gcd_greatest) |
400 |
qed |
|
401 |
||
402 |
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> |
|
60438 | 403 |
normalization_factor d = (if d = 0 then 0 else 1) \<and> |
58023 | 404 |
(\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b" |
405 |
by (rule, auto intro: gcdI simp: gcd_greatest) |
|
406 |
||
407 |
lemma gcd_dvd_prod: "gcd a b dvd k * b" |
|
408 |
using mult_dvd_mono [of 1] by auto |
|
409 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
410 |
lemma gcd_1_left [simp]: "gcd 1 a = 1" |
58023 | 411 |
by (rule sym, rule gcdI, simp_all) |
412 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
413 |
lemma gcd_1 [simp]: "gcd a 1 = 1" |
58023 | 414 |
by (rule sym, rule gcdI, simp_all) |
415 |
||
416 |
lemma gcd_proj2_if_dvd: |
|
60438 | 417 |
"b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
418 |
by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0) |
58023 | 419 |
|
420 |
lemma gcd_proj1_if_dvd: |
|
60438 | 421 |
"a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a" |
58023 | 422 |
by (subst gcd.commute, simp add: gcd_proj2_if_dvd) |
423 |
||
60438 | 424 |
lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n" |
58023 | 425 |
proof |
60438 | 426 |
assume A: "gcd m n = m div normalization_factor m" |
58023 | 427 |
show "m dvd n" |
428 |
proof (cases "m = 0") |
|
429 |
assume [simp]: "m \<noteq> 0" |
|
60438 | 430 |
from A have B: "m = gcd m n * normalization_factor m" |
58023 | 431 |
by (simp add: unit_eq_div2) |
432 |
show ?thesis by (subst B, simp add: mult_unit_dvd_iff) |
|
433 |
qed (insert A, simp) |
|
434 |
next |
|
435 |
assume "m dvd n" |
|
60438 | 436 |
then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd) |
58023 | 437 |
qed |
438 |
||
60438 | 439 |
lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m" |
58023 | 440 |
by (subst gcd.commute, simp add: gcd_proj1_iff) |
441 |
||
442 |
lemma gcd_mod1 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
443 |
"gcd (a mod b) b = gcd a b" |
58023 | 444 |
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) |
445 |
||
446 |
lemma gcd_mod2 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
447 |
"gcd a (b mod a) = gcd a b" |
58023 | 448 |
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) |
449 |
||
450 |
lemma gcd_mult_distrib': |
|
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
451 |
"c div normalization_factor c * gcd a b = gcd (c * a) (c * b)" |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
452 |
proof (cases "c = 0") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
453 |
case True then show ?thesis by (simp_all add: gcd_0) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
454 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
455 |
case False then have [simp]: "is_unit (normalization_factor c)" by simp |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
456 |
show ?thesis |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
457 |
proof (induct a b rule: gcd_eucl_induct) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
458 |
case (zero a) show ?case |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
459 |
proof (cases "a = 0") |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
460 |
case True then show ?thesis by (simp add: gcd_0) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
461 |
next |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
462 |
case False then have "is_unit (normalization_factor a)" by simp |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
463 |
then show ?thesis |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
464 |
by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
465 |
qed |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
466 |
case (mod a b) |
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset
|
467 |
then show ?case by (simp add: mult_mod_right gcd.commute) |
58023 | 468 |
qed |
469 |
qed |
|
470 |
||
471 |
lemma gcd_mult_distrib: |
|
60438 | 472 |
"k * gcd a b = gcd (k*a) (k*b) * normalization_factor k" |
58023 | 473 |
proof- |
60438 | 474 |
let ?nf = "normalization_factor" |
58023 | 475 |
from gcd_mult_distrib' |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
476 |
have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" .. |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
477 |
also have "... = k * gcd a b div ?nf k" |
60438 | 478 |
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd) |
58023 | 479 |
finally show ?thesis |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
480 |
by simp |
58023 | 481 |
qed |
482 |
||
483 |
lemma euclidean_size_gcd_le1 [simp]: |
|
484 |
assumes "a \<noteq> 0" |
|
485 |
shows "euclidean_size (gcd a b) \<le> euclidean_size a" |
|
486 |
proof - |
|
487 |
have "gcd a b dvd a" by (rule gcd_dvd1) |
|
488 |
then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast |
|
60526 | 489 |
with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto |
58023 | 490 |
qed |
491 |
||
492 |
lemma euclidean_size_gcd_le2 [simp]: |
|
493 |
"b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b" |
|
494 |
by (subst gcd.commute, rule euclidean_size_gcd_le1) |
|
495 |
||
496 |
lemma euclidean_size_gcd_less1: |
|
497 |
assumes "a \<noteq> 0" and "\<not>a dvd b" |
|
498 |
shows "euclidean_size (gcd a b) < euclidean_size a" |
|
499 |
proof (rule ccontr) |
|
500 |
assume "\<not>euclidean_size (gcd a b) < euclidean_size a" |
|
60526 | 501 |
with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a" |
58023 | 502 |
by (intro le_antisym, simp_all) |
503 |
with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd) |
|
504 |
hence "a dvd b" using dvd_gcd_D2 by blast |
|
60526 | 505 |
with \<open>\<not>a dvd b\<close> show False by contradiction |
58023 | 506 |
qed |
507 |
||
508 |
lemma euclidean_size_gcd_less2: |
|
509 |
assumes "b \<noteq> 0" and "\<not>b dvd a" |
|
510 |
shows "euclidean_size (gcd a b) < euclidean_size b" |
|
511 |
using assms by (subst gcd.commute, rule euclidean_size_gcd_less1) |
|
512 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
513 |
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c" |
58023 | 514 |
apply (rule gcdI) |
515 |
apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps) |
|
516 |
apply (rule gcd_dvd2) |
|
517 |
apply (rule gcd_greatest, simp add: unit_simps, assumption) |
|
60438 | 518 |
apply (subst normalization_factor_gcd, simp add: gcd_0) |
58023 | 519 |
done |
520 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
521 |
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c" |
58023 | 522 |
by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute) |
523 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
524 |
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c" |
60433 | 525 |
by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1) |
58023 | 526 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
527 |
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c" |
60433 | 528 |
by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2) |
58023 | 529 |
|
60438 | 530 |
lemma gcd_idem: "gcd a a = a div normalization_factor a" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
531 |
by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all) |
58023 | 532 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
533 |
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b" |
58023 | 534 |
apply (rule gcdI) |
535 |
apply (simp add: ac_simps) |
|
536 |
apply (rule gcd_dvd2) |
|
537 |
apply (rule gcd_greatest, erule (1) gcd_greatest, assumption) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
538 |
apply simp |
58023 | 539 |
done |
540 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
541 |
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b" |
58023 | 542 |
apply (rule gcdI) |
543 |
apply simp |
|
544 |
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2) |
|
545 |
apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
546 |
apply simp |
58023 | 547 |
done |
548 |
||
549 |
lemma comp_fun_idem_gcd: "comp_fun_idem gcd" |
|
550 |
proof |
|
551 |
fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a" |
|
552 |
by (simp add: fun_eq_iff ac_simps) |
|
553 |
next |
|
554 |
fix a show "gcd a \<circ> gcd a = gcd a" |
|
555 |
by (simp add: fun_eq_iff gcd_left_idem) |
|
556 |
qed |
|
557 |
||
558 |
lemma coprime_dvd_mult: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
559 |
assumes "gcd c b = 1" and "c dvd a * b" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
560 |
shows "c dvd a" |
58023 | 561 |
proof - |
60438 | 562 |
let ?nf = "normalization_factor" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
563 |
from assms gcd_mult_distrib [of a c b] |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
564 |
have A: "a = gcd (a * c) (a * b) * ?nf a" by simp |
60526 | 565 |
from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest) |
58023 | 566 |
qed |
567 |
||
568 |
lemma coprime_dvd_mult_iff: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
569 |
"gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)" |
58023 | 570 |
by (rule, rule coprime_dvd_mult, simp_all) |
571 |
||
572 |
lemma gcd_dvd_antisym: |
|
573 |
"gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d" |
|
574 |
proof (rule gcdI) |
|
575 |
assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b" |
|
576 |
have "gcd c d dvd c" by simp |
|
577 |
with A show "gcd a b dvd c" by (rule dvd_trans) |
|
578 |
have "gcd c d dvd d" by simp |
|
579 |
with A show "gcd a b dvd d" by (rule dvd_trans) |
|
60438 | 580 |
show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
581 |
by simp |
58023 | 582 |
fix l assume "l dvd c" and "l dvd d" |
583 |
hence "l dvd gcd c d" by (rule gcd_greatest) |
|
584 |
from this and B show "l dvd gcd a b" by (rule dvd_trans) |
|
585 |
qed |
|
586 |
||
587 |
lemma gcd_mult_cancel: |
|
588 |
assumes "gcd k n = 1" |
|
589 |
shows "gcd (k * m) n = gcd m n" |
|
590 |
proof (rule gcd_dvd_antisym) |
|
591 |
have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps) |
|
60526 | 592 |
also note \<open>gcd k n = 1\<close> |
58023 | 593 |
finally have "gcd (gcd (k * m) n) k = 1" by simp |
594 |
hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps) |
|
595 |
moreover have "gcd (k * m) n dvd n" by simp |
|
596 |
ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest) |
|
597 |
have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all |
|
598 |
then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest) |
|
599 |
qed |
|
600 |
||
601 |
lemma coprime_crossproduct: |
|
602 |
assumes [simp]: "gcd a d = 1" "gcd b c = 1" |
|
603 |
shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs") |
|
604 |
proof |
|
605 |
assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono) |
|
606 |
next |
|
607 |
assume ?lhs |
|
60526 | 608 |
from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) |
58023 | 609 |
hence "a dvd b" by (simp add: coprime_dvd_mult_iff) |
60526 | 610 |
moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) |
58023 | 611 |
hence "b dvd a" by (simp add: coprime_dvd_mult_iff) |
60526 | 612 |
moreover from \<open>?lhs\<close> have "c dvd d * b" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
613 |
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) |
58023 | 614 |
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute) |
60526 | 615 |
moreover from \<open>?lhs\<close> have "d dvd c * a" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
616 |
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) |
58023 | 617 |
hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute) |
618 |
ultimately show ?rhs unfolding associated_def by simp |
|
619 |
qed |
|
620 |
||
621 |
lemma gcd_add1 [simp]: |
|
622 |
"gcd (m + n) n = gcd m n" |
|
623 |
by (cases "n = 0", simp_all add: gcd_non_0) |
|
624 |
||
625 |
lemma gcd_add2 [simp]: |
|
626 |
"gcd m (m + n) = gcd m n" |
|
627 |
using gcd_add1 [of n m] by (simp add: ac_simps) |
|
628 |
||
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
629 |
lemma gcd_add_mult: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
630 |
"gcd m (k * m + n) = gcd m n" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
631 |
proof - |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
632 |
have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
633 |
by (fact gcd_mod2) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
634 |
then show ?thesis by simp |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
635 |
qed |
58023 | 636 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
637 |
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1" |
58023 | 638 |
by (rule sym, rule gcdI, simp_all) |
639 |
||
640 |
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)" |
|
59061 | 641 |
by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2) |
58023 | 642 |
|
643 |
lemma div_gcd_coprime: |
|
644 |
assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0" |
|
645 |
defines [simp]: "d \<equiv> gcd a b" |
|
646 |
defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d" |
|
647 |
shows "gcd a' b' = 1" |
|
648 |
proof (rule coprimeI) |
|
649 |
fix l assume "l dvd a'" "l dvd b'" |
|
650 |
then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
651 |
moreover have "a = a' * d" "b = b' * d" by simp_all |
58023 | 652 |
ultimately have "a = (l * d) * s" "b = (l * d) * t" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
653 |
by (simp_all only: ac_simps) |
58023 | 654 |
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left) |
655 |
hence "l*d dvd d" by (simp add: gcd_greatest) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
656 |
then obtain u where "d = l * d * u" .. |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
657 |
then have "d * (l * u) = d" by (simp add: ac_simps) |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
658 |
moreover from nz have "d \<noteq> 0" by simp |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
659 |
with div_mult_self1_is_id have "d * (l * u) div d = l * u" . |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
660 |
ultimately have "1 = l * u" |
60526 | 661 |
using \<open>d \<noteq> 0\<close> by simp |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
662 |
then show "l dvd 1" .. |
58023 | 663 |
qed |
664 |
||
665 |
lemma coprime_mult: |
|
666 |
assumes da: "gcd d a = 1" and db: "gcd d b = 1" |
|
667 |
shows "gcd d (a * b) = 1" |
|
668 |
apply (subst gcd.commute) |
|
669 |
using da apply (subst gcd_mult_cancel) |
|
670 |
apply (subst gcd.commute, assumption) |
|
671 |
apply (subst gcd.commute, rule db) |
|
672 |
done |
|
673 |
||
674 |
lemma coprime_lmult: |
|
675 |
assumes dab: "gcd d (a * b) = 1" |
|
676 |
shows "gcd d a = 1" |
|
677 |
proof (rule coprimeI) |
|
678 |
fix l assume "l dvd d" and "l dvd a" |
|
679 |
hence "l dvd a * b" by simp |
|
60526 | 680 |
with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest) |
58023 | 681 |
qed |
682 |
||
683 |
lemma coprime_rmult: |
|
684 |
assumes dab: "gcd d (a * b) = 1" |
|
685 |
shows "gcd d b = 1" |
|
686 |
proof (rule coprimeI) |
|
687 |
fix l assume "l dvd d" and "l dvd b" |
|
688 |
hence "l dvd a * b" by simp |
|
60526 | 689 |
with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest) |
58023 | 690 |
qed |
691 |
||
692 |
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1" |
|
693 |
using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast |
|
694 |
||
695 |
lemma gcd_coprime: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
696 |
assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" |
58023 | 697 |
shows "gcd a' b' = 1" |
698 |
proof - |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
699 |
from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp |
58023 | 700 |
with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" . |
701 |
also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+ |
|
702 |
also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+ |
|
703 |
finally show ?thesis . |
|
704 |
qed |
|
705 |
||
706 |
lemma coprime_power: |
|
707 |
assumes "0 < n" |
|
708 |
shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1" |
|
709 |
using assms proof (induct n) |
|
710 |
case (Suc n) then show ?case |
|
711 |
by (cases n) (simp_all add: coprime_mul_eq) |
|
712 |
qed simp |
|
713 |
||
714 |
lemma gcd_coprime_exists: |
|
715 |
assumes nz: "gcd a b \<noteq> 0" |
|
716 |
shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1" |
|
717 |
apply (rule_tac x = "a div gcd a b" in exI) |
|
718 |
apply (rule_tac x = "b div gcd a b" in exI) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
719 |
apply (insert nz, auto intro: div_gcd_coprime) |
58023 | 720 |
done |
721 |
||
722 |
lemma coprime_exp: |
|
723 |
"gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1" |
|
724 |
by (induct n, simp_all add: coprime_mult) |
|
725 |
||
726 |
lemma coprime_exp2 [intro]: |
|
727 |
"gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1" |
|
728 |
apply (rule coprime_exp) |
|
729 |
apply (subst gcd.commute) |
|
730 |
apply (rule coprime_exp) |
|
731 |
apply (subst gcd.commute) |
|
732 |
apply assumption |
|
733 |
done |
|
734 |
||
735 |
lemma gcd_exp: |
|
736 |
"gcd (a^n) (b^n) = (gcd a b) ^ n" |
|
737 |
proof (cases "a = 0 \<and> b = 0") |
|
738 |
assume "a = 0 \<and> b = 0" |
|
739 |
then show ?thesis by (cases n, simp_all add: gcd_0_left) |
|
740 |
next |
|
741 |
assume A: "\<not>(a = 0 \<and> b = 0)" |
|
742 |
hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)" |
|
743 |
using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime) |
|
744 |
hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp |
|
745 |
also note gcd_mult_distrib |
|
60438 | 746 |
also have "normalization_factor ((gcd a b)^n) = 1" |
747 |
by (simp add: normalization_factor_pow A) |
|
58023 | 748 |
also have "(gcd a b)^n * (a div gcd a b)^n = a^n" |
749 |
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) |
|
750 |
also have "(gcd a b)^n * (b div gcd a b)^n = b^n" |
|
751 |
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) |
|
752 |
finally show ?thesis by simp |
|
753 |
qed |
|
754 |
||
755 |
lemma coprime_common_divisor: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
756 |
"gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
757 |
apply (subgoal_tac "a dvd gcd a b") |
59061 | 758 |
apply simp |
58023 | 759 |
apply (erule (1) gcd_greatest) |
760 |
done |
|
761 |
||
762 |
lemma division_decomp: |
|
763 |
assumes dc: "a dvd b * c" |
|
764 |
shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c" |
|
765 |
proof (cases "gcd a b = 0") |
|
766 |
assume "gcd a b = 0" |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
767 |
hence "a = 0 \<and> b = 0" by simp |
58023 | 768 |
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp |
769 |
then show ?thesis by blast |
|
770 |
next |
|
771 |
let ?d = "gcd a b" |
|
772 |
assume "?d \<noteq> 0" |
|
773 |
from gcd_coprime_exists[OF this] |
|
774 |
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" |
|
775 |
by blast |
|
776 |
from ab'(1) have "a' dvd a" unfolding dvd_def by blast |
|
777 |
with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp |
|
778 |
from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp |
|
779 |
hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac) |
|
60526 | 780 |
with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp |
58023 | 781 |
with coprime_dvd_mult[OF ab'(3)] |
782 |
have "a' dvd c" by (subst (asm) ac_simps, blast) |
|
783 |
with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac) |
|
784 |
then show ?thesis by blast |
|
785 |
qed |
|
786 |
||
60433 | 787 |
lemma pow_divs_pow: |
58023 | 788 |
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0" |
789 |
shows "a dvd b" |
|
790 |
proof (cases "gcd a b = 0") |
|
791 |
assume "gcd a b = 0" |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
792 |
then show ?thesis by simp |
58023 | 793 |
next |
794 |
let ?d = "gcd a b" |
|
795 |
assume "?d \<noteq> 0" |
|
796 |
from n obtain m where m: "n = Suc m" by (cases n, simp_all) |
|
60526 | 797 |
from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero) |
798 |
from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>] |
|
58023 | 799 |
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" |
800 |
by blast |
|
801 |
from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n" |
|
802 |
by (simp add: ab'(1,2)[symmetric]) |
|
803 |
hence "?d^n * a'^n dvd ?d^n * b'^n" |
|
804 |
by (simp only: power_mult_distrib ac_simps) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
805 |
with zn have "a'^n dvd b'^n" by simp |
58023 | 806 |
hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m) |
807 |
hence "a' dvd b'^m * b'" by (simp add: m ac_simps) |
|
808 |
with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]] |
|
809 |
have "a' dvd b'" by (subst (asm) ac_simps, blast) |
|
810 |
hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp) |
|
811 |
with ab'(1,2) show ?thesis by simp |
|
812 |
qed |
|
813 |
||
60433 | 814 |
lemma pow_divs_eq [simp]: |
58023 | 815 |
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b" |
60433 | 816 |
by (auto intro: pow_divs_pow dvd_power_same) |
58023 | 817 |
|
60433 | 818 |
lemma divs_mult: |
58023 | 819 |
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1" |
820 |
shows "m * n dvd r" |
|
821 |
proof - |
|
822 |
from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'" |
|
823 |
unfolding dvd_def by blast |
|
824 |
from mr n' have "m dvd n'*n" by (simp add: ac_simps) |
|
825 |
hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp |
|
826 |
then obtain k where k: "n' = m*k" unfolding dvd_def by blast |
|
827 |
with n' have "r = m * n * k" by (simp add: mult_ac) |
|
828 |
then show ?thesis unfolding dvd_def by blast |
|
829 |
qed |
|
830 |
||
831 |
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1" |
|
832 |
by (subst add_commute, simp) |
|
833 |
||
834 |
lemma setprod_coprime [rule_format]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
835 |
"(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1" |
58023 | 836 |
apply (cases "finite A") |
837 |
apply (induct set: finite) |
|
838 |
apply (auto simp add: gcd_mult_cancel) |
|
839 |
done |
|
840 |
||
841 |
lemma coprime_divisors: |
|
842 |
assumes "d dvd a" "e dvd b" "gcd a b = 1" |
|
843 |
shows "gcd d e = 1" |
|
844 |
proof - |
|
845 |
from assms obtain k l where "a = d * k" "b = e * l" |
|
846 |
unfolding dvd_def by blast |
|
847 |
with assms have "gcd (d * k) (e * l) = 1" by simp |
|
848 |
hence "gcd (d * k) e = 1" by (rule coprime_lmult) |
|
849 |
also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps) |
|
850 |
finally have "gcd e d = 1" by (rule coprime_lmult) |
|
851 |
then show ?thesis by (simp add: ac_simps) |
|
852 |
qed |
|
853 |
||
854 |
lemma invertible_coprime: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
855 |
assumes "a * b mod m = 1" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
856 |
shows "coprime a m" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
857 |
proof - |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
858 |
from assms have "coprime m (a * b mod m)" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
859 |
by simp |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
860 |
then have "coprime m (a * b)" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
861 |
by simp |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
862 |
then have "coprime m a" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
863 |
by (rule coprime_lmult) |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
864 |
then show ?thesis |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
865 |
by (simp add: ac_simps) |
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
866 |
qed |
58023 | 867 |
|
868 |
lemma lcm_gcd: |
|
60438 | 869 |
"lcm a b = a * b div (gcd a b * normalization_factor (a*b))" |
58023 | 870 |
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def) |
871 |
||
872 |
lemma lcm_gcd_prod: |
|
60438 | 873 |
"lcm a b * gcd a b = a * b div normalization_factor (a*b)" |
58023 | 874 |
proof (cases "a * b = 0") |
60438 | 875 |
let ?nf = normalization_factor |
58023 | 876 |
assume "a * b \<noteq> 0" |
58953 | 877 |
hence "gcd a b \<noteq> 0" by simp |
58023 | 878 |
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" |
879 |
by (simp add: mult_ac) |
|
60526 | 880 |
also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)" |
60432 | 881 |
by (simp add: div_mult_swap mult.commute) |
58023 | 882 |
finally show ?thesis . |
58953 | 883 |
qed (auto simp add: lcm_gcd) |
58023 | 884 |
|
885 |
lemma lcm_dvd1 [iff]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
886 |
"a dvd lcm a b" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
887 |
proof (cases "a*b = 0") |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
888 |
assume "a * b \<noteq> 0" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
889 |
hence "gcd a b \<noteq> 0" by simp |
60438 | 890 |
let ?c = "1 div normalization_factor (a * b)" |
60526 | 891 |
from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
892 |
from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b" |
60432 | 893 |
by (simp add: div_mult_swap unit_div_commute) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
894 |
hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp |
60526 | 895 |
with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b" |
58023 | 896 |
by (subst (asm) div_mult_self2_is_id, simp_all) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
897 |
also have "... = a * (?c * b div gcd a b)" |
58023 | 898 |
by (metis div_mult_swap gcd_dvd2 mult_assoc) |
899 |
finally show ?thesis by (rule dvdI) |
|
58953 | 900 |
qed (auto simp add: lcm_gcd) |
58023 | 901 |
|
902 |
lemma lcm_least: |
|
903 |
"\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k" |
|
904 |
proof (cases "k = 0") |
|
60438 | 905 |
let ?nf = normalization_factor |
58023 | 906 |
assume "k \<noteq> 0" |
907 |
hence "is_unit (?nf k)" by simp |
|
908 |
hence "?nf k \<noteq> 0" by (metis not_is_unit_0) |
|
909 |
assume A: "a dvd k" "b dvd k" |
|
60526 | 910 |
hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto |
58023 | 911 |
from A obtain r s where ar: "k = a * r" and bs: "k = b * s" |
912 |
unfolding dvd_def by blast |
|
60526 | 913 |
with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0" |
58953 | 914 |
by auto (drule sym [of 0], simp) |
58023 | 915 |
hence "is_unit (?nf (r * s))" by simp |
916 |
let ?c = "?nf k div ?nf (r*s)" |
|
60526 | 917 |
from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div) |
58023 | 918 |
hence "?c \<noteq> 0" using not_is_unit_0 by fast |
919 |
from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)" |
|
58953 | 920 |
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps) |
58023 | 921 |
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)" |
60526 | 922 |
by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps) |
923 |
also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close> |
|
58023 | 924 |
by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps) |
925 |
finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b" |
|
926 |
by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac) |
|
927 |
hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)" |
|
928 |
by (simp add: algebra_simps) |
|
60526 | 929 |
hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close> |
58023 | 930 |
by (metis div_mult_self2_is_id) |
931 |
also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)" |
|
932 |
by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') |
|
933 |
also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b" |
|
934 |
by (simp add: algebra_simps) |
|
60526 | 935 |
finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close> |
58023 | 936 |
by (metis mult.commute div_mult_self2_is_id) |
60526 | 937 |
hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close> |
58023 | 938 |
by (metis div_mult_self2_is_id mult_assoc) |
60526 | 939 |
also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close> |
58023 | 940 |
by (simp add: unit_simps) |
941 |
finally show ?thesis by (rule dvdI) |
|
942 |
qed simp |
|
943 |
||
944 |
lemma lcm_zero: |
|
945 |
"lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0" |
|
946 |
proof - |
|
60438 | 947 |
let ?nf = normalization_factor |
58023 | 948 |
{ |
949 |
assume "a \<noteq> 0" "b \<noteq> 0" |
|
950 |
hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors) |
|
60526 | 951 |
moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp |
58023 | 952 |
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp) |
953 |
} moreover { |
|
954 |
assume "a = 0 \<or> b = 0" |
|
955 |
hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd) |
|
956 |
} |
|
957 |
ultimately show ?thesis by blast |
|
958 |
qed |
|
959 |
||
960 |
lemmas lcm_0_iff = lcm_zero |
|
961 |
||
962 |
lemma gcd_lcm: |
|
963 |
assumes "lcm a b \<noteq> 0" |
|
60438 | 964 |
shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))" |
58023 | 965 |
proof- |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
966 |
from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero) |
60438 | 967 |
let ?c = "normalization_factor (a * b)" |
60526 | 968 |
from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors) |
58023 | 969 |
hence "is_unit ?c" by simp |
970 |
from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b" |
|
60526 | 971 |
by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac) |
972 |
also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)" |
|
973 |
by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd') |
|
60433 | 974 |
finally show ?thesis . |
58023 | 975 |
qed |
976 |
||
60438 | 977 |
lemma normalization_factor_lcm [simp]: |
978 |
"normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)" |
|
58023 | 979 |
proof (cases "a = 0 \<or> b = 0") |
980 |
case True then show ?thesis |
|
58953 | 981 |
by (auto simp add: lcm_gcd) |
58023 | 982 |
next |
983 |
case False |
|
60438 | 984 |
let ?nf = normalization_factor |
58023 | 985 |
from lcm_gcd_prod[of a b] |
986 |
have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)" |
|
60438 | 987 |
by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult) |
58023 | 988 |
also have "... = (if a*b = 0 then 0 else 1)" |
58953 | 989 |
by simp |
990 |
finally show ?thesis using False by simp |
|
58023 | 991 |
qed |
992 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
993 |
lemma lcm_dvd2 [iff]: "b dvd lcm a b" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
994 |
using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps) |
58023 | 995 |
|
996 |
lemma lcmI: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
997 |
"\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l; |
60438 | 998 |
normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b" |
58023 | 999 |
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least) |
1000 |
||
1001 |
sublocale lcm!: abel_semigroup lcm |
|
1002 |
proof |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1003 |
fix a b c |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1004 |
show "lcm (lcm a b) c = lcm a (lcm b c)" |
58023 | 1005 |
proof (rule lcmI) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1006 |
have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1007 |
then show "a dvd lcm (lcm a b) c" by (rule dvd_trans) |
58023 | 1008 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1009 |
have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1010 |
hence "b dvd lcm (lcm a b) c" by (rule dvd_trans) |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1011 |
moreover have "c dvd lcm (lcm a b) c" by simp |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1012 |
ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least) |
58023 | 1013 |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1014 |
fix l assume "a dvd l" and "lcm b c dvd l" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1015 |
have "b dvd lcm b c" by simp |
60526 | 1016 |
from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1017 |
have "c dvd lcm b c" by simp |
60526 | 1018 |
from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans) |
1019 |
from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least) |
|
1020 |
from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least) |
|
58023 | 1021 |
qed (simp add: lcm_zero) |
1022 |
next |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1023 |
fix a b |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1024 |
show "lcm a b = lcm b a" |
58023 | 1025 |
by (simp add: lcm_gcd ac_simps) |
1026 |
qed |
|
1027 |
||
1028 |
lemma dvd_lcm_D1: |
|
1029 |
"lcm m n dvd k \<Longrightarrow> m dvd k" |
|
1030 |
by (rule dvd_trans, rule lcm_dvd1, assumption) |
|
1031 |
||
1032 |
lemma dvd_lcm_D2: |
|
1033 |
"lcm m n dvd k \<Longrightarrow> n dvd k" |
|
1034 |
by (rule dvd_trans, rule lcm_dvd2, assumption) |
|
1035 |
||
1036 |
lemma gcd_dvd_lcm [simp]: |
|
1037 |
"gcd a b dvd lcm a b" |
|
1038 |
by (metis dvd_trans gcd_dvd2 lcm_dvd2) |
|
1039 |
||
1040 |
lemma lcm_1_iff: |
|
1041 |
"lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b" |
|
1042 |
proof |
|
1043 |
assume "lcm a b = 1" |
|
59061 | 1044 |
then show "is_unit a \<and> is_unit b" by auto |
58023 | 1045 |
next |
1046 |
assume "is_unit a \<and> is_unit b" |
|
59061 | 1047 |
hence "a dvd 1" and "b dvd 1" by simp_all |
1048 |
hence "is_unit (lcm a b)" by (rule lcm_least) |
|
60438 | 1049 |
hence "lcm a b = normalization_factor (lcm a b)" |
1050 |
by (subst normalization_factor_unit, simp_all) |
|
60526 | 1051 |
also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close> |
59061 | 1052 |
by auto |
58023 | 1053 |
finally show "lcm a b = 1" . |
1054 |
qed |
|
1055 |
||
1056 |
lemma lcm_0_left [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1057 |
"lcm 0 a = 0" |
58023 | 1058 |
by (rule sym, rule lcmI, simp_all) |
1059 |
||
1060 |
lemma lcm_0 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1061 |
"lcm a 0 = 0" |
58023 | 1062 |
by (rule sym, rule lcmI, simp_all) |
1063 |
||
1064 |
lemma lcm_unique: |
|
1065 |
"a dvd d \<and> b dvd d \<and> |
|
60438 | 1066 |
normalization_factor d = (if d = 0 then 0 else 1) \<and> |
58023 | 1067 |
(\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b" |
1068 |
by (rule, auto intro: lcmI simp: lcm_least lcm_zero) |
|
1069 |
||
1070 |
lemma dvd_lcm_I1 [simp]: |
|
1071 |
"k dvd m \<Longrightarrow> k dvd lcm m n" |
|
1072 |
by (metis lcm_dvd1 dvd_trans) |
|
1073 |
||
1074 |
lemma dvd_lcm_I2 [simp]: |
|
1075 |
"k dvd n \<Longrightarrow> k dvd lcm m n" |
|
1076 |
by (metis lcm_dvd2 dvd_trans) |
|
1077 |
||
1078 |
lemma lcm_1_left [simp]: |
|
60438 | 1079 |
"lcm 1 a = a div normalization_factor a" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1080 |
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) |
58023 | 1081 |
|
1082 |
lemma lcm_1_right [simp]: |
|
60438 | 1083 |
"lcm a 1 = a div normalization_factor a" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1084 |
using lcm_1_left [of a] by (simp add: ac_simps) |
58023 | 1085 |
|
1086 |
lemma lcm_coprime: |
|
60438 | 1087 |
"gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)" |
58023 | 1088 |
by (subst lcm_gcd) simp |
1089 |
||
1090 |
lemma lcm_proj1_if_dvd: |
|
60438 | 1091 |
"b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1092 |
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) |
58023 | 1093 |
|
1094 |
lemma lcm_proj2_if_dvd: |
|
60438 | 1095 |
"a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b" |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1096 |
using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps) |
58023 | 1097 |
|
1098 |
lemma lcm_proj1_iff: |
|
60438 | 1099 |
"lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m" |
58023 | 1100 |
proof |
60438 | 1101 |
assume A: "lcm m n = m div normalization_factor m" |
58023 | 1102 |
show "n dvd m" |
1103 |
proof (cases "m = 0") |
|
1104 |
assume [simp]: "m \<noteq> 0" |
|
60438 | 1105 |
from A have B: "m = lcm m n * normalization_factor m" |
58023 | 1106 |
by (simp add: unit_eq_div2) |
1107 |
show ?thesis by (subst B, simp) |
|
1108 |
qed simp |
|
1109 |
next |
|
1110 |
assume "n dvd m" |
|
60438 | 1111 |
then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd) |
58023 | 1112 |
qed |
1113 |
||
1114 |
lemma lcm_proj2_iff: |
|
60438 | 1115 |
"lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n" |
58023 | 1116 |
using lcm_proj1_iff [of n m] by (simp add: ac_simps) |
1117 |
||
1118 |
lemma euclidean_size_lcm_le1: |
|
1119 |
assumes "a \<noteq> 0" and "b \<noteq> 0" |
|
1120 |
shows "euclidean_size a \<le> euclidean_size (lcm a b)" |
|
1121 |
proof - |
|
1122 |
have "a dvd lcm a b" by (rule lcm_dvd1) |
|
1123 |
then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast |
|
60526 | 1124 |
with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero) |
58023 | 1125 |
then show ?thesis by (subst A, intro size_mult_mono) |
1126 |
qed |
|
1127 |
||
1128 |
lemma euclidean_size_lcm_le2: |
|
1129 |
"a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)" |
|
1130 |
using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps) |
|
1131 |
||
1132 |
lemma euclidean_size_lcm_less1: |
|
1133 |
assumes "b \<noteq> 0" and "\<not>b dvd a" |
|
1134 |
shows "euclidean_size a < euclidean_size (lcm a b)" |
|
1135 |
proof (rule ccontr) |
|
1136 |
from assms have "a \<noteq> 0" by auto |
|
1137 |
assume "\<not>euclidean_size a < euclidean_size (lcm a b)" |
|
60526 | 1138 |
with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a" |
58023 | 1139 |
by (intro le_antisym, simp, intro euclidean_size_lcm_le1) |
1140 |
with assms have "lcm a b dvd a" |
|
1141 |
by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero) |
|
1142 |
hence "b dvd a" by (rule dvd_lcm_D2) |
|
60526 | 1143 |
with \<open>\<not>b dvd a\<close> show False by contradiction |
58023 | 1144 |
qed |
1145 |
||
1146 |
lemma euclidean_size_lcm_less2: |
|
1147 |
assumes "a \<noteq> 0" and "\<not>a dvd b" |
|
1148 |
shows "euclidean_size b < euclidean_size (lcm a b)" |
|
1149 |
using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps) |
|
1150 |
||
1151 |
lemma lcm_mult_unit1: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1152 |
"is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c" |
58023 | 1153 |
apply (rule lcmI) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1154 |
apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1) |
58023 | 1155 |
apply (rule lcm_dvd2) |
1156 |
apply (rule lcm_least, simp add: unit_simps, assumption) |
|
60438 | 1157 |
apply (subst normalization_factor_lcm, simp add: lcm_zero) |
58023 | 1158 |
done |
1159 |
||
1160 |
lemma lcm_mult_unit2: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1161 |
"is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1162 |
using lcm_mult_unit1 [of a c b] by (simp add: ac_simps) |
58023 | 1163 |
|
1164 |
lemma lcm_div_unit1: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1165 |
"is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c" |
60433 | 1166 |
by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) |
58023 | 1167 |
|
1168 |
lemma lcm_div_unit2: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1169 |
"is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c" |
60433 | 1170 |
by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2) |
58023 | 1171 |
|
1172 |
lemma lcm_left_idem: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1173 |
"lcm a (lcm a b) = lcm a b" |
58023 | 1174 |
apply (rule lcmI) |
1175 |
apply simp |
|
1176 |
apply (subst lcm.assoc [symmetric], rule lcm_dvd2) |
|
1177 |
apply (rule lcm_least, assumption) |
|
1178 |
apply (erule (1) lcm_least) |
|
1179 |
apply (auto simp: lcm_zero) |
|
1180 |
done |
|
1181 |
||
1182 |
lemma lcm_right_idem: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1183 |
"lcm (lcm a b) b = lcm a b" |
58023 | 1184 |
apply (rule lcmI) |
1185 |
apply (subst lcm.assoc, rule lcm_dvd1) |
|
1186 |
apply (rule lcm_dvd2) |
|
1187 |
apply (rule lcm_least, erule (1) lcm_least, assumption) |
|
1188 |
apply (auto simp: lcm_zero) |
|
1189 |
done |
|
1190 |
||
1191 |
lemma comp_fun_idem_lcm: "comp_fun_idem lcm" |
|
1192 |
proof |
|
1193 |
fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a" |
|
1194 |
by (simp add: fun_eq_iff ac_simps) |
|
1195 |
next |
|
1196 |
fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def |
|
1197 |
by (intro ext, simp add: lcm_left_idem) |
|
1198 |
qed |
|
1199 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1200 |
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1201 |
and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'" |
60438 | 1202 |
and normalization_factor_Lcm [simp]: |
1203 |
"normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" |
|
58023 | 1204 |
proof - |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1205 |
have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm A dvd l') \<and> |
60438 | 1206 |
normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1207 |
proof (cases "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)") |
58023 | 1208 |
case False |
1209 |
hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def) |
|
1210 |
with False show ?thesis by auto |
|
1211 |
next |
|
1212 |
case True |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1213 |
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1214 |
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1215 |
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1216 |
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
58023 | 1217 |
apply (subst n_def) |
1218 |
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) |
|
1219 |
apply (rule exI[of _ l\<^sub>0]) |
|
1220 |
apply (simp add: l\<^sub>0_props) |
|
1221 |
done |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1222 |
from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" |
58023 | 1223 |
unfolding l_def by simp_all |
1224 |
{ |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1225 |
fix l' assume "\<forall>a\<in>A. a dvd l'" |
60526 | 1226 |
with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest) |
1227 |
moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1228 |
ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')" |
58023 | 1229 |
by (intro exI[of _ "gcd l l'"], auto) |
1230 |
hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le) |
|
1231 |
moreover have "euclidean_size (gcd l l') \<le> n" |
|
1232 |
proof - |
|
1233 |
have "gcd l l' dvd l" by simp |
|
1234 |
then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast |
|
60526 | 1235 |
with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto |
58023 | 1236 |
hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)" |
1237 |
by (rule size_mult_mono) |
|
60526 | 1238 |
also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> .. |
1239 |
also note \<open>euclidean_size l = n\<close> |
|
58023 | 1240 |
finally show "euclidean_size (gcd l l') \<le> n" . |
1241 |
qed |
|
1242 |
ultimately have "euclidean_size l = euclidean_size (gcd l l')" |
|
60526 | 1243 |
by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>) |
1244 |
with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd) |
|
58023 | 1245 |
hence "l dvd l'" by (blast dest: dvd_gcd_D2) |
1246 |
} |
|
1247 |
||
60526 | 1248 |
with \<open>(\<forall>a\<in>A. a dvd l)\<close> and normalization_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close> |
60438 | 1249 |
have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and> |
1250 |
(\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and> |
|
1251 |
normalization_factor (l div normalization_factor l) = |
|
1252 |
(if l div normalization_factor l = 0 then 0 else 1)" |
|
58023 | 1253 |
by (auto simp: unit_simps) |
60438 | 1254 |
also from True have "l div normalization_factor l = Lcm A" |
58023 | 1255 |
by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def) |
1256 |
finally show ?thesis . |
|
1257 |
qed |
|
1258 |
note A = this |
|
1259 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1260 |
{fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast} |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1261 |
{fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast} |
60438 | 1262 |
from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast |
58023 | 1263 |
qed |
1264 |
||
1265 |
lemma LcmI: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1266 |
"(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow> |
60438 | 1267 |
normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A" |
58023 | 1268 |
by (intro normed_associated_imp_eq) |
1269 |
(auto intro: Lcm_dvd dvd_Lcm simp: associated_def) |
|
1270 |
||
1271 |
lemma Lcm_subset: |
|
1272 |
"A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B" |
|
1273 |
by (blast intro: Lcm_dvd dvd_Lcm) |
|
1274 |
||
1275 |
lemma Lcm_Un: |
|
1276 |
"Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)" |
|
1277 |
apply (rule lcmI) |
|
1278 |
apply (blast intro: Lcm_subset) |
|
1279 |
apply (blast intro: Lcm_subset) |
|
1280 |
apply (intro Lcm_dvd ballI, elim UnE) |
|
1281 |
apply (rule dvd_trans, erule dvd_Lcm, assumption) |
|
1282 |
apply (rule dvd_trans, erule dvd_Lcm, assumption) |
|
1283 |
apply simp |
|
1284 |
done |
|
1285 |
||
1286 |
lemma Lcm_1_iff: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1287 |
"Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)" |
58023 | 1288 |
proof |
1289 |
assume "Lcm A = 1" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1290 |
then show "\<forall>a\<in>A. is_unit a" by auto |
58023 | 1291 |
qed (rule LcmI [symmetric], auto) |
1292 |
||
1293 |
lemma Lcm_no_units: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1294 |
"Lcm A = Lcm (A - {a. is_unit a})" |
58023 | 1295 |
proof - |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1296 |
have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1297 |
hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})" |
58023 | 1298 |
by (simp add: Lcm_Un[symmetric]) |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1299 |
also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff) |
58023 | 1300 |
finally show ?thesis by simp |
1301 |
qed |
|
1302 |
||
1303 |
lemma Lcm_empty [simp]: |
|
1304 |
"Lcm {} = 1" |
|
1305 |
by (simp add: Lcm_1_iff) |
|
1306 |
||
1307 |
lemma Lcm_eq_0 [simp]: |
|
1308 |
"0 \<in> A \<Longrightarrow> Lcm A = 0" |
|
1309 |
by (drule dvd_Lcm) simp |
|
1310 |
||
1311 |
lemma Lcm0_iff': |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1312 |
"Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" |
58023 | 1313 |
proof |
1314 |
assume "Lcm A = 0" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1315 |
show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" |
58023 | 1316 |
proof |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1317 |
assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1318 |
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1319 |
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1320 |
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1321 |
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" |
58023 | 1322 |
apply (subst n_def) |
1323 |
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) |
|
1324 |
apply (rule exI[of _ l\<^sub>0]) |
|
1325 |
apply (simp add: l\<^sub>0_props) |
|
1326 |
done |
|
1327 |
from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all |
|
60438 | 1328 |
hence "l div normalization_factor l \<noteq> 0" by simp |
1329 |
also from ex have "l div normalization_factor l = Lcm A" |
|
58023 | 1330 |
by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def) |
60526 | 1331 |
finally show False using \<open>Lcm A = 0\<close> by contradiction |
58023 | 1332 |
qed |
1333 |
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False) |
|
1334 |
||
1335 |
lemma Lcm0_iff [simp]: |
|
1336 |
"finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A" |
|
1337 |
proof - |
|
1338 |
assume "finite A" |
|
1339 |
have "0 \<in> A \<Longrightarrow> Lcm A = 0" by (intro dvd_0_left dvd_Lcm) |
|
1340 |
moreover { |
|
1341 |
assume "0 \<notin> A" |
|
1342 |
hence "\<Prod>A \<noteq> 0" |
|
60526 | 1343 |
apply (induct rule: finite_induct[OF \<open>finite A\<close>]) |
58023 | 1344 |
apply simp |
1345 |
apply (subst setprod.insert, assumption, assumption) |
|
1346 |
apply (rule no_zero_divisors) |
|
1347 |
apply blast+ |
|
1348 |
done |
|
60526 | 1349 |
moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1350 |
ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast |
58023 | 1351 |
with Lcm0_iff' have "Lcm A \<noteq> 0" by simp |
1352 |
} |
|
1353 |
ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast |
|
1354 |
qed |
|
1355 |
||
1356 |
lemma Lcm_no_multiple: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1357 |
"(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0" |
58023 | 1358 |
proof - |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1359 |
assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1360 |
hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast |
58023 | 1361 |
then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False) |
1362 |
qed |
|
1363 |
||
1364 |
lemma Lcm_insert [simp]: |
|
1365 |
"Lcm (insert a A) = lcm a (Lcm A)" |
|
1366 |
proof (rule lcmI) |
|
1367 |
fix l assume "a dvd l" and "Lcm A dvd l" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1368 |
hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm) |
60526 | 1369 |
with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd) |
58023 | 1370 |
qed (auto intro: Lcm_dvd dvd_Lcm) |
1371 |
||
1372 |
lemma Lcm_finite: |
|
1373 |
assumes "finite A" |
|
1374 |
shows "Lcm A = Finite_Set.fold lcm 1 A" |
|
60526 | 1375 |
by (induct rule: finite.induct[OF \<open>finite A\<close>]) |
58023 | 1376 |
(simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm]) |
1377 |
||
60431
db9c67b760f1
dropped warnings by dropping ineffective code declarations
haftmann
parents:
60430
diff
changeset
|
1378 |
lemma Lcm_set [code_unfold]: |
58023 | 1379 |
"Lcm (set xs) = fold lcm xs 1" |
1380 |
using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps) |
|
1381 |
||
1382 |
lemma Lcm_singleton [simp]: |
|
60438 | 1383 |
"Lcm {a} = a div normalization_factor a" |
58023 | 1384 |
by simp |
1385 |
||
1386 |
lemma Lcm_2 [simp]: |
|
1387 |
"Lcm {a,b} = lcm a b" |
|
1388 |
by (simp only: Lcm_insert Lcm_empty lcm_1_right) |
|
1389 |
(cases "b = 0", simp, rule lcm_div_unit2, simp) |
|
1390 |
||
1391 |
lemma Lcm_coprime: |
|
1392 |
assumes "finite A" and "A \<noteq> {}" |
|
1393 |
assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1" |
|
60438 | 1394 |
shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" |
58023 | 1395 |
using assms proof (induct rule: finite_ne_induct) |
1396 |
case (insert a A) |
|
1397 |
have "Lcm (insert a A) = lcm a (Lcm A)" by simp |
|
60438 | 1398 |
also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast |
58023 | 1399 |
also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2) |
1400 |
also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto |
|
60438 | 1401 |
with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))" |
58023 | 1402 |
by (simp add: lcm_coprime) |
1403 |
finally show ?case . |
|
1404 |
qed simp |
|
1405 |
||
1406 |
lemma Lcm_coprime': |
|
1407 |
"card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1) |
|
60438 | 1408 |
\<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" |
58023 | 1409 |
by (rule Lcm_coprime) (simp_all add: card_eq_0_iff) |
1410 |
||
1411 |
lemma Gcd_Lcm: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1412 |
"Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}" |
58023 | 1413 |
by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def) |
1414 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1415 |
lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1416 |
and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A" |
60438 | 1417 |
and normalization_factor_Gcd [simp]: |
1418 |
"normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)" |
|
58023 | 1419 |
proof - |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1420 |
fix a assume "a \<in> A" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1421 |
hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1422 |
then show "Gcd A dvd a" by (simp add: Gcd_Lcm) |
58023 | 1423 |
next |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1424 |
fix g' assume "\<forall>a\<in>A. g' dvd a" |
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1425 |
hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast |
58023 | 1426 |
then show "g' dvd Gcd A" by (simp add: Gcd_Lcm) |
1427 |
next |
|
60438 | 1428 |
show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
1429 |
by (simp add: Gcd_Lcm) |
58023 | 1430 |
qed |
1431 |
||
1432 |
lemma GcdI: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1433 |
"(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow> |
60438 | 1434 |
normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A" |
58023 | 1435 |
by (intro normed_associated_imp_eq) |
1436 |
(auto intro: Gcd_dvd dvd_Gcd simp: associated_def) |
|
1437 |
||
1438 |
lemma Lcm_Gcd: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1439 |
"Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}" |
58023 | 1440 |
by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd) |
1441 |
||
1442 |
lemma Gcd_0_iff: |
|
1443 |
"Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}" |
|
1444 |
apply (rule iffI) |
|
1445 |
apply (rule subsetI, drule Gcd_dvd, simp) |
|
1446 |
apply (auto intro: GcdI[symmetric]) |
|
1447 |
done |
|
1448 |
||
1449 |
lemma Gcd_empty [simp]: |
|
1450 |
"Gcd {} = 0" |
|
1451 |
by (simp add: Gcd_0_iff) |
|
1452 |
||
1453 |
lemma Gcd_1: |
|
1454 |
"1 \<in> A \<Longrightarrow> Gcd A = 1" |
|
1455 |
by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd) |
|
1456 |
||
1457 |
lemma Gcd_insert [simp]: |
|
1458 |
"Gcd (insert a A) = gcd a (Gcd A)" |
|
1459 |
proof (rule gcdI) |
|
1460 |
fix l assume "l dvd a" and "l dvd Gcd A" |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1461 |
hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd) |
60526 | 1462 |
with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd) |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
1463 |
qed auto |
58023 | 1464 |
|
1465 |
lemma Gcd_finite: |
|
1466 |
assumes "finite A" |
|
1467 |
shows "Gcd A = Finite_Set.fold gcd 0 A" |
|
60526 | 1468 |
by (induct rule: finite.induct[OF \<open>finite A\<close>]) |
58023 | 1469 |
(simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd]) |
1470 |
||
60431
db9c67b760f1
dropped warnings by dropping ineffective code declarations
haftmann
parents:
60430
diff
changeset
|
1471 |
lemma Gcd_set [code_unfold]: |
58023 | 1472 |
"Gcd (set xs) = fold gcd xs 0" |
1473 |
using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps) |
|
1474 |
||
60438 | 1475 |
lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a" |
58023 | 1476 |
by (simp add: gcd_0) |
1477 |
||
1478 |
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b" |
|
1479 |
by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp) |
|
1480 |
||
60439
b765e08f8bc0
proper subclass instances for existing gcd (semi)rings
haftmann
parents:
60438
diff
changeset
|
1481 |
subclass semiring_gcd |
b765e08f8bc0
proper subclass instances for existing gcd (semi)rings
haftmann
parents:
60438
diff
changeset
|
1482 |
by unfold_locales (simp_all add: gcd_greatest_iff) |
b765e08f8bc0
proper subclass instances for existing gcd (semi)rings
haftmann
parents:
60438
diff
changeset
|
1483 |
|
58023 | 1484 |
end |
1485 |
||
60526 | 1486 |
text \<open> |
58023 | 1487 |
A Euclidean ring is a Euclidean semiring with additive inverses. It provides a |
1488 |
few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring. |
|
60526 | 1489 |
\<close> |
58023 | 1490 |
|
1491 |
class euclidean_ring_gcd = euclidean_semiring_gcd + idom |
|
1492 |
begin |
|
1493 |
||
1494 |
subclass euclidean_ring .. |
|
1495 |
||
60439
b765e08f8bc0
proper subclass instances for existing gcd (semi)rings
haftmann
parents:
60438
diff
changeset
|
1496 |
subclass ring_gcd .. |
b765e08f8bc0
proper subclass instances for existing gcd (semi)rings
haftmann
parents:
60438
diff
changeset
|
1497 |
|
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1498 |
lemma euclid_ext_gcd [simp]: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1499 |
"(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1500 |
by (induct a b rule: gcd_eucl_induct) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1501 |
(simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1502 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1503 |
lemma euclid_ext_gcd' [simp]: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1504 |
"euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1505 |
by (insert euclid_ext_gcd[of a b], drule (1) subst, simp) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1506 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1507 |
lemma euclid_ext'_correct: |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1508 |
"fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1509 |
proof- |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1510 |
obtain s t c where "euclid_ext a b = (s,t,c)" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1511 |
by (cases "euclid_ext a b", blast) |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1512 |
with euclid_ext_correct[of a b] euclid_ext_gcd[of a b] |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1513 |
show ?thesis unfolding euclid_ext'_def by simp |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1514 |
qed |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1515 |
|
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1516 |
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b" |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1517 |
using euclid_ext'_correct by blast |
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1518 |
|
58023 | 1519 |
lemma gcd_neg1 [simp]: |
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1520 |
"gcd (-a) b = gcd a b" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
1521 |
by (rule sym, rule gcdI, simp_all add: gcd_greatest) |
58023 | 1522 |
|
1523 |
lemma gcd_neg2 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1524 |
"gcd a (-b) = gcd a b" |
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset
|
1525 |
by (rule sym, rule gcdI, simp_all add: gcd_greatest) |
58023 | 1526 |
|
1527 |
lemma gcd_neg_numeral_1 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1528 |
"gcd (- numeral n) a = gcd (numeral n) a" |
58023 | 1529 |
by (fact gcd_neg1) |
1530 |
||
1531 |
lemma gcd_neg_numeral_2 [simp]: |
|
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1532 |
"gcd a (- numeral n) = gcd a (numeral n)" |
58023 | 1533 |
by (fact gcd_neg2) |
1534 |
||
1535 |
lemma gcd_diff1: "gcd (m - n) n = gcd m n" |
|
1536 |
by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric], subst gcd_add1, simp) |
|
1537 |
||
1538 |
lemma gcd_diff2: "gcd (n - m) n = gcd m n" |
|
1539 |
by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1) |
|
1540 |
||
1541 |
lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1" |
|
1542 |
proof - |
|
1543 |
have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute) |
|
1544 |
also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp |
|
1545 |
also have "\<dots> = 1" by (rule coprime_plus_one) |
|
1546 |
finally show ?thesis . |
|
1547 |
qed |
|
1548 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1549 |
lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b" |
58023 | 1550 |
by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero) |
1551 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1552 |
lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b" |
58023 | 1553 |
by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero) |
1554 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1555 |
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a" |
58023 | 1556 |
by (fact lcm_neg1) |
1557 |
||
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset
|
1558 |
lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)" |
58023 | 1559 |
by (fact lcm_neg2) |
1560 |
||
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1561 |
end |
58023 | 1562 |
|
1563 |
||
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1564 |
subsection \<open>Typical instances\<close> |
58023 | 1565 |
|
1566 |
instantiation nat :: euclidean_semiring |
|
1567 |
begin |
|
1568 |
||
1569 |
definition [simp]: |
|
1570 |
"euclidean_size_nat = (id :: nat \<Rightarrow> nat)" |
|
1571 |
||
1572 |
definition [simp]: |
|
60438 | 1573 |
"normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)" |
58023 | 1574 |
|
1575 |
instance proof |
|
59061 | 1576 |
qed simp_all |
58023 | 1577 |
|
1578 |
end |
|
1579 |
||
1580 |
instantiation int :: euclidean_ring |
|
1581 |
begin |
|
1582 |
||
1583 |
definition [simp]: |
|
1584 |
"euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)" |
|
1585 |
||
1586 |
definition [simp]: |
|
60438 | 1587 |
"normalization_factor_int = (sgn :: int \<Rightarrow> int)" |
58023 | 1588 |
|
60580 | 1589 |
instance |
1590 |
proof (default, goals) |
|
1591 |
case 2 |
|
1592 |
then show ?case by (auto simp add: abs_mult nat_mult_distrib) |
|
58023 | 1593 |
next |
60580 | 1594 |
case 3 |
1595 |
then show ?case by (simp add: zsgn_def) |
|
58023 | 1596 |
next |
60580 | 1597 |
case 5 |
1598 |
then show ?case by (auto simp: zsgn_def) |
|
58023 | 1599 |
next |
60580 | 1600 |
case 6 |
1601 |
then show ?case by (auto split: abs_split simp: zsgn_def) |
|
58023 | 1602 |
qed (auto simp: sgn_times split: abs_split) |
1603 |
||
1604 |
end |
|
1605 |
||
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset
|
1606 |
instantiation poly :: (field) euclidean_ring |
60571 | 1607 |
begin |
1608 |
||
1609 |
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat" |
|
1610 |
where "euclidean_size = (degree :: 'a poly \<Rightarrow> nat)" |
|
1611 |
||
1612 |
definition normalization_factor_poly :: "'a poly \<Rightarrow> 'a poly" |
|
1613 |
where "normalization_factor p = monom (coeff p (degree p)) 0" |
|
1614 |
||
1615 |
instance |
|
1616 |
proof (default, unfold euclidean_size_poly_def normalization_factor_poly_def) |
|
1617 |
fix p q :: "'a poly" |
|
1618 |
assume "q \<noteq> 0" and "\<not> q dvd p" |
|
1619 |
then show "degree (p mod q) < degree q" |
|
1620 |
using degree_mod_less [of q p] by (simp add: mod_eq_0_iff_dvd) |
|
1621 |
next |
|
1622 |
fix p q :: "'a poly" |
|
1623 |
assume "q \<noteq> 0" |
|
1624 |
from \<open>q \<noteq> 0\<close> show "degree p \<le> degree (p * q)" |
|
1625 |
by (rule degree_mult_right_le) |
|
1626 |
from \<open>q \<noteq> 0\<close> show "is_unit (monom (coeff q (degree q)) 0)" |
|
1627 |
by (auto intro: is_unit_monom_0) |
|
1628 |
next |
|
1629 |
fix p :: "'a poly" |
|
1630 |
show "monom (coeff p (degree p)) 0 = p" if "is_unit p" |
|
1631 |
using that by (fact is_unit_monom_trival) |
|
1632 |
next |
|
1633 |
fix p q :: "'a poly" |
|
1634 |
show "monom (coeff (p * q) (degree (p * q))) 0 = |
|
1635 |
monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0" |
|
1636 |
by (simp add: monom_0 coeff_degree_mult) |
|
1637 |
next |
|
1638 |
show "monom (coeff 0 (degree 0)) 0 = 0" |
|
1639 |
by simp |
|
1640 |
qed |
|
1641 |
||
58023 | 1642 |
end |
60571 | 1643 |
|
1644 |
end |