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