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