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