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