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