src/HOL/GCD.thy
author haftmann
Thu May 24 09:26:26 2018 +0000 (22 months ago)
changeset 68270 2bc921b2159b
parent 67399 eab6ce8368fa
child 68708 77858f347020
permissions -rw-r--r--
treat gcd_eq_1_imp_coprime analogously to mod_0_imp_dvd
wenzelm@63489
     1
(*  Title:      HOL/GCD.thy
wenzelm@63489
     2
    Author:     Christophe Tabacznyj
wenzelm@63489
     3
    Author:     Lawrence C. Paulson
wenzelm@63489
     4
    Author:     Amine Chaieb
wenzelm@63489
     5
    Author:     Thomas M. Rasmussen
wenzelm@63489
     6
    Author:     Jeremy Avigad
wenzelm@63489
     7
    Author:     Tobias Nipkow
huffman@31706
     8
haftmann@32479
     9
This file deals with the functions gcd and lcm.  Definitions and
haftmann@32479
    10
lemmas are proved uniformly for the natural numbers and integers.
huffman@31706
    11
huffman@31706
    12
This file combines and revises a number of prior developments.
huffman@31706
    13
huffman@31706
    14
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
wenzelm@58623
    15
and Lawrence C. Paulson, based on @{cite davenport92}. They introduced
huffman@31706
    16
gcd, lcm, and prime for the natural numbers.
huffman@31706
    17
huffman@31706
    18
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
huffman@31706
    19
extended gcd, lcm, primes to the integers. Amine Chaieb provided
huffman@31706
    20
another extension of the notions to the integers, and added a number
huffman@31706
    21
of results to "Primes" and "GCD". IntPrimes also defined and developed
huffman@31706
    22
the congruence relations on the integers. The notion was extended to
berghofe@34915
    23
the natural numbers by Chaieb.
huffman@31706
    24
avigad@32036
    25
Jeremy Avigad combined all of these, made everything uniform for the
avigad@32036
    26
natural numbers and the integers, and added a number of new theorems.
avigad@32036
    27
nipkow@31798
    28
Tobias Nipkow cleaned up a lot.
wenzelm@21256
    29
*)
wenzelm@21256
    30
wenzelm@60758
    31
section \<open>Greatest common divisor and least common multiple\<close>
wenzelm@21256
    32
wenzelm@21256
    33
theory GCD
haftmann@65555
    34
  imports Groups_List 
huffman@31706
    35
begin
huffman@31706
    36
haftmann@64850
    37
subsection \<open>Abstract bounded quasi semilattices as common foundation\<close>
wenzelm@65552
    38
wenzelm@65552
    39
locale bounded_quasi_semilattice = abel_semigroup +
haftmann@65555
    40
  fixes top :: 'a  ("\<^bold>\<top>") and bot :: 'a  ("\<^bold>\<bottom>")
haftmann@64850
    41
    and normalize :: "'a \<Rightarrow> 'a"
haftmann@64850
    42
  assumes idem_normalize [simp]: "a \<^bold>* a = normalize a"
haftmann@64850
    43
    and normalize_left_idem [simp]: "normalize a \<^bold>* b = a \<^bold>* b"
haftmann@64850
    44
    and normalize_idem [simp]: "normalize (a \<^bold>* b) = a \<^bold>* b"
haftmann@65555
    45
    and normalize_top [simp]: "normalize \<^bold>\<top> = \<^bold>\<top>"
haftmann@65555
    46
    and normalize_bottom [simp]: "normalize \<^bold>\<bottom> = \<^bold>\<bottom>"
haftmann@65555
    47
    and top_left_normalize [simp]: "\<^bold>\<top> \<^bold>* a = normalize a"
haftmann@65555
    48
    and bottom_left_bottom [simp]: "\<^bold>\<bottom> \<^bold>* a = \<^bold>\<bottom>"
haftmann@64850
    49
begin
haftmann@64850
    50
haftmann@64850
    51
lemma left_idem [simp]:
haftmann@64850
    52
  "a \<^bold>* (a \<^bold>* b) = a \<^bold>* b"
haftmann@64850
    53
  using assoc [of a a b, symmetric] by simp
haftmann@64850
    54
haftmann@64850
    55
lemma right_idem [simp]:
haftmann@64850
    56
  "(a \<^bold>* b) \<^bold>* b = a \<^bold>* b"
haftmann@64850
    57
  using left_idem [of b a] by (simp add: ac_simps)
haftmann@64850
    58
haftmann@64850
    59
lemma comp_fun_idem: "comp_fun_idem f"
haftmann@64850
    60
  by standard (simp_all add: fun_eq_iff ac_simps)
haftmann@64850
    61
haftmann@64850
    62
interpretation comp_fun_idem f
haftmann@64850
    63
  by (fact comp_fun_idem)
haftmann@64850
    64
haftmann@64850
    65
lemma top_right_normalize [simp]:
haftmann@65555
    66
  "a \<^bold>* \<^bold>\<top> = normalize a"
haftmann@64850
    67
  using top_left_normalize [of a] by (simp add: ac_simps)
wenzelm@65552
    68
haftmann@64850
    69
lemma bottom_right_bottom [simp]:
haftmann@65555
    70
  "a \<^bold>* \<^bold>\<bottom> = \<^bold>\<bottom>"
haftmann@64850
    71
  using bottom_left_bottom [of a] by (simp add: ac_simps)
haftmann@64850
    72
haftmann@64850
    73
lemma normalize_right_idem [simp]:
haftmann@64850
    74
  "a \<^bold>* normalize b = a \<^bold>* b"
haftmann@64850
    75
  using normalize_left_idem [of b a] by (simp add: ac_simps)
haftmann@64850
    76
wenzelm@65552
    77
end
haftmann@64850
    78
haftmann@64850
    79
locale bounded_quasi_semilattice_set = bounded_quasi_semilattice
haftmann@64850
    80
begin
haftmann@64850
    81
haftmann@64850
    82
interpretation comp_fun_idem f
haftmann@64850
    83
  by (fact comp_fun_idem)
haftmann@64850
    84
haftmann@64850
    85
definition F :: "'a set \<Rightarrow> 'a"
haftmann@64850
    86
where
haftmann@65555
    87
  eq_fold: "F A = (if finite A then Finite_Set.fold f \<^bold>\<top> A else \<^bold>\<bottom>)"
haftmann@65555
    88
haftmann@65555
    89
lemma infinite [simp]:
haftmann@65555
    90
  "infinite A \<Longrightarrow> F A = \<^bold>\<bottom>"
haftmann@65555
    91
  by (simp add: eq_fold)
haftmann@64850
    92
haftmann@64850
    93
lemma set_eq_fold [code]:
haftmann@65555
    94
  "F (set xs) = fold f xs \<^bold>\<top>"
haftmann@64850
    95
  by (simp add: eq_fold fold_set_fold)
haftmann@64850
    96
haftmann@64850
    97
lemma empty [simp]:
haftmann@65555
    98
  "F {} = \<^bold>\<top>"
haftmann@64850
    99
  by (simp add: eq_fold)
haftmann@64850
   100
haftmann@64850
   101
lemma insert [simp]:
haftmann@64850
   102
  "F (insert a A) = a \<^bold>* F A"
haftmann@64850
   103
  by (cases "finite A") (simp_all add: eq_fold)
haftmann@64850
   104
haftmann@64850
   105
lemma normalize [simp]:
haftmann@64850
   106
  "normalize (F A) = F A"
haftmann@64850
   107
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@64850
   108
haftmann@64850
   109
lemma in_idem:
haftmann@64850
   110
  assumes "a \<in> A"
haftmann@64850
   111
  shows "a \<^bold>* F A = F A"
haftmann@64850
   112
  using assms by (induct A rule: infinite_finite_induct)
haftmann@64850
   113
    (auto simp add: left_commute [of a])
haftmann@64850
   114
haftmann@64850
   115
lemma union:
haftmann@64850
   116
  "F (A \<union> B) = F A \<^bold>* F B"
haftmann@64850
   117
  by (induct A rule: infinite_finite_induct)
haftmann@64850
   118
    (simp_all add: ac_simps)
haftmann@64850
   119
haftmann@64850
   120
lemma remove:
haftmann@64850
   121
  assumes "a \<in> A"
haftmann@64850
   122
  shows "F A = a \<^bold>* F (A - {a})"
haftmann@64850
   123
proof -
haftmann@64850
   124
  from assms obtain B where "A = insert a B" and "a \<notin> B"
haftmann@64850
   125
    by (blast dest: mk_disjoint_insert)
haftmann@64850
   126
  with assms show ?thesis by simp
haftmann@64850
   127
qed
haftmann@64850
   128
haftmann@64850
   129
lemma insert_remove:
haftmann@64850
   130
  "F (insert a A) = a \<^bold>* F (A - {a})"
haftmann@64850
   131
  by (cases "a \<in> A") (simp_all add: insert_absorb remove)
haftmann@64850
   132
haftmann@64850
   133
lemma subset:
haftmann@64850
   134
  assumes "B \<subseteq> A"
haftmann@64850
   135
  shows "F B \<^bold>* F A = F A"
haftmann@64850
   136
  using assms by (simp add: union [symmetric] Un_absorb1)
wenzelm@65552
   137
haftmann@64850
   138
end
wenzelm@63489
   139
haftmann@62345
   140
subsection \<open>Abstract GCD and LCM\<close>
huffman@31706
   141
nipkow@31992
   142
class gcd = zero + one + dvd +
wenzelm@41550
   143
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@41550
   144
    and lcm :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
huffman@31706
   145
haftmann@60686
   146
class Gcd = gcd +
haftmann@63025
   147
  fixes Gcd :: "'a set \<Rightarrow> 'a"
haftmann@63025
   148
    and Lcm :: "'a set \<Rightarrow> 'a"
haftmann@62350
   149
begin
haftmann@62350
   150
haftmann@63025
   151
abbreviation GREATEST_COMMON_DIVISOR :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
wenzelm@63489
   152
  where "GREATEST_COMMON_DIVISOR A f \<equiv> Gcd (f ` A)"
haftmann@62350
   153
haftmann@63025
   154
abbreviation LEAST_COMMON_MULTIPLE :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
wenzelm@63489
   155
  where "LEAST_COMMON_MULTIPLE A f \<equiv> Lcm (f ` A)"
haftmann@62350
   156
haftmann@62350
   157
end
haftmann@62350
   158
haftmann@62350
   159
syntax
haftmann@63025
   160
  "_GCD1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3GCD _./ _)" [0, 10] 10)
haftmann@63025
   161
  "_GCD"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3GCD _\<in>_./ _)" [0, 0, 10] 10)
haftmann@63025
   162
  "_LCM1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3LCM _./ _)" [0, 10] 10)
haftmann@63025
   163
  "_LCM"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3LCM _\<in>_./ _)" [0, 0, 10] 10)
haftmann@62350
   164
translations
haftmann@63025
   165
  "GCD x y. B"   \<rightleftharpoons> "GCD x. GCD y. B"
haftmann@63025
   166
  "GCD x. B"     \<rightleftharpoons> "CONST GREATEST_COMMON_DIVISOR CONST UNIV (\<lambda>x. B)"
haftmann@63025
   167
  "GCD x. B"     \<rightleftharpoons> "GCD x \<in> CONST UNIV. B"
haftmann@63025
   168
  "GCD x\<in>A. B"   \<rightleftharpoons> "CONST GREATEST_COMMON_DIVISOR A (\<lambda>x. B)"
haftmann@63025
   169
  "LCM x y. B"   \<rightleftharpoons> "LCM x. LCM y. B"
haftmann@63025
   170
  "LCM x. B"     \<rightleftharpoons> "CONST LEAST_COMMON_MULTIPLE CONST UNIV (\<lambda>x. B)"
haftmann@63025
   171
  "LCM x. B"     \<rightleftharpoons> "LCM x \<in> CONST UNIV. B"
haftmann@63025
   172
  "LCM x\<in>A. B"   \<rightleftharpoons> "CONST LEAST_COMMON_MULTIPLE A (\<lambda>x. B)"
haftmann@62350
   173
haftmann@62350
   174
print_translation \<open>
haftmann@63025
   175
  [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax GREATEST_COMMON_DIVISOR} @{syntax_const "_GCD"},
haftmann@63025
   176
    Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax LEAST_COMMON_MULTIPLE} @{syntax_const "_LCM"}]
haftmann@62350
   177
\<close> \<comment> \<open>to avoid eta-contraction of body\<close>
haftmann@60686
   178
haftmann@60686
   179
class semiring_gcd = normalization_semidom + gcd +
haftmann@59008
   180
  assumes gcd_dvd1 [iff]: "gcd a b dvd a"
wenzelm@59977
   181
    and gcd_dvd2 [iff]: "gcd a b dvd b"
wenzelm@59977
   182
    and gcd_greatest: "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> c dvd gcd a b"
haftmann@60686
   183
    and normalize_gcd [simp]: "normalize (gcd a b) = gcd a b"
haftmann@60686
   184
    and lcm_gcd: "lcm a b = normalize (a * b) div gcd a b"
wenzelm@63489
   185
begin
wenzelm@63489
   186
wenzelm@63489
   187
lemma gcd_greatest_iff [simp]: "a dvd gcd b c \<longleftrightarrow> a dvd b \<and> a dvd c"
haftmann@60686
   188
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@60686
   189
wenzelm@63489
   190
lemma gcd_dvdI1: "a dvd c \<Longrightarrow> gcd a b dvd c"
haftmann@60689
   191
  by (rule dvd_trans) (rule gcd_dvd1)
haftmann@60689
   192
wenzelm@63489
   193
lemma gcd_dvdI2: "b dvd c \<Longrightarrow> gcd a b dvd c"
haftmann@60689
   194
  by (rule dvd_trans) (rule gcd_dvd2)
haftmann@60689
   195
wenzelm@63489
   196
lemma dvd_gcdD1: "a dvd gcd b c \<Longrightarrow> a dvd b"
haftmann@62345
   197
  using gcd_dvd1 [of b c] by (blast intro: dvd_trans)
haftmann@62345
   198
wenzelm@63489
   199
lemma dvd_gcdD2: "a dvd gcd b c \<Longrightarrow> a dvd c"
haftmann@62345
   200
  using gcd_dvd2 [of b c] by (blast intro: dvd_trans)
haftmann@62345
   201
wenzelm@63489
   202
lemma gcd_0_left [simp]: "gcd 0 a = normalize a"
haftmann@60688
   203
  by (rule associated_eqI) simp_all
haftmann@60686
   204
wenzelm@63489
   205
lemma gcd_0_right [simp]: "gcd a 0 = normalize a"
haftmann@60688
   206
  by (rule associated_eqI) simp_all
wenzelm@63489
   207
wenzelm@63489
   208
lemma gcd_eq_0_iff [simp]: "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
wenzelm@63489
   209
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   210
proof
wenzelm@63489
   211
  assume ?P
wenzelm@63489
   212
  then have "0 dvd gcd a b"
wenzelm@63489
   213
    by simp
wenzelm@63489
   214
  then have "0 dvd a" and "0 dvd b"
wenzelm@63489
   215
    by (blast intro: dvd_trans)+
wenzelm@63489
   216
  then show ?Q
wenzelm@63489
   217
    by simp
haftmann@60686
   218
next
wenzelm@63489
   219
  assume ?Q
wenzelm@63489
   220
  then show ?P
wenzelm@63489
   221
    by simp
haftmann@60686
   222
qed
haftmann@60686
   223
wenzelm@63489
   224
lemma unit_factor_gcd: "unit_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)"
haftmann@60686
   225
proof (cases "gcd a b = 0")
wenzelm@63489
   226
  case True
wenzelm@63489
   227
  then show ?thesis by simp
haftmann@60686
   228
next
haftmann@60686
   229
  case False
haftmann@60686
   230
  have "unit_factor (gcd a b) * normalize (gcd a b) = gcd a b"
haftmann@60686
   231
    by (rule unit_factor_mult_normalize)
haftmann@60686
   232
  then have "unit_factor (gcd a b) * gcd a b = gcd a b"
haftmann@60686
   233
    by simp
haftmann@60686
   234
  then have "unit_factor (gcd a b) * gcd a b div gcd a b = gcd a b div gcd a b"
haftmann@60686
   235
    by simp
wenzelm@63489
   236
  with False show ?thesis
wenzelm@63489
   237
    by simp
haftmann@60686
   238
qed
haftmann@60686
   239
haftmann@67051
   240
lemma is_unit_gcd_iff [simp]:
haftmann@67051
   241
  "is_unit (gcd a b) \<longleftrightarrow> gcd a b = 1"
haftmann@60690
   242
  by (cases "a = 0 \<and> b = 0") (auto simp add: unit_factor_gcd dest: is_unit_unit_factor)
haftmann@60690
   243
wenzelm@61605
   244
sublocale gcd: abel_semigroup gcd
haftmann@60686
   245
proof
haftmann@60686
   246
  fix a b c
haftmann@60686
   247
  show "gcd a b = gcd b a"
haftmann@60688
   248
    by (rule associated_eqI) simp_all
haftmann@60686
   249
  from gcd_dvd1 have "gcd (gcd a b) c dvd a"
haftmann@60686
   250
    by (rule dvd_trans) simp
haftmann@60686
   251
  moreover from gcd_dvd1 have "gcd (gcd a b) c dvd b"
haftmann@60686
   252
    by (rule dvd_trans) simp
haftmann@60686
   253
  ultimately have P1: "gcd (gcd a b) c dvd gcd a (gcd b c)"
haftmann@60686
   254
    by (auto intro!: gcd_greatest)
haftmann@60686
   255
  from gcd_dvd2 have "gcd a (gcd b c) dvd b"
haftmann@60686
   256
    by (rule dvd_trans) simp
haftmann@60686
   257
  moreover from gcd_dvd2 have "gcd a (gcd b c) dvd c"
haftmann@60686
   258
    by (rule dvd_trans) simp
haftmann@60686
   259
  ultimately have P2: "gcd a (gcd b c) dvd gcd (gcd a b) c"
haftmann@60686
   260
    by (auto intro!: gcd_greatest)
haftmann@60688
   261
  from P1 P2 show "gcd (gcd a b) c = gcd a (gcd b c)"
haftmann@60688
   262
    by (rule associated_eqI) simp_all
haftmann@60686
   263
qed
haftmann@60686
   264
haftmann@64850
   265
sublocale gcd: bounded_quasi_semilattice gcd 0 1 normalize
haftmann@64850
   266
proof
haftmann@64850
   267
  show "gcd a a = normalize a" for a
haftmann@64850
   268
  proof -
haftmann@64850
   269
    have "a dvd gcd a a"
haftmann@64850
   270
      by (rule gcd_greatest) simp_all
haftmann@64850
   271
    then show ?thesis
haftmann@64850
   272
      by (auto intro: associated_eqI)
haftmann@64850
   273
  qed
haftmann@64850
   274
  show "gcd (normalize a) b = gcd a b" for a b
haftmann@64850
   275
    using gcd_dvd1 [of "normalize a" b]
haftmann@60688
   276
    by (auto intro: associated_eqI)
haftmann@67051
   277
  show "gcd 1 a = 1" for a
haftmann@64850
   278
    by (rule associated_eqI) simp_all
haftmann@64850
   279
qed simp_all
wenzelm@65552
   280
haftmann@64850
   281
lemma gcd_self: "gcd a a = normalize a"
haftmann@64850
   282
  by (fact gcd.idem_normalize)
haftmann@64850
   283
haftmann@64850
   284
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
haftmann@64850
   285
  by (fact gcd.left_idem)
haftmann@64850
   286
haftmann@64850
   287
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
haftmann@64850
   288
  by (fact gcd.right_idem)
haftmann@64850
   289
wenzelm@63489
   290
lemma gcd_mult_left: "gcd (c * a) (c * b) = normalize c * gcd a b"
haftmann@60686
   291
proof (cases "c = 0")
wenzelm@63489
   292
  case True
wenzelm@63489
   293
  then show ?thesis by simp
haftmann@60686
   294
next
haftmann@60686
   295
  case False
wenzelm@63489
   296
  then have *: "c * gcd a b dvd gcd (c * a) (c * b)"
haftmann@60686
   297
    by (auto intro: gcd_greatest)
wenzelm@63489
   298
  moreover from False * have "gcd (c * a) (c * b) dvd c * gcd a b"
haftmann@60686
   299
    by (metis div_dvd_iff_mult dvd_mult_left gcd_dvd1 gcd_dvd2 gcd_greatest mult_commute)
haftmann@60686
   300
  ultimately have "normalize (gcd (c * a) (c * b)) = normalize (c * gcd a b)"
haftmann@60688
   301
    by (auto intro: associated_eqI)
wenzelm@63489
   302
  then show ?thesis
wenzelm@63489
   303
    by (simp add: normalize_mult)
haftmann@60686
   304
qed
haftmann@60686
   305
wenzelm@63489
   306
lemma gcd_mult_right: "gcd (a * c) (b * c) = gcd b a * normalize c"
haftmann@60686
   307
  using gcd_mult_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   308
wenzelm@63489
   309
lemma mult_gcd_left: "c * gcd a b = unit_factor c * gcd (c * a) (c * b)"
haftmann@60686
   310
  by (simp add: gcd_mult_left mult.assoc [symmetric])
haftmann@60686
   311
wenzelm@63489
   312
lemma mult_gcd_right: "gcd a b * c = gcd (a * c) (b * c) * unit_factor c"
haftmann@60686
   313
  using mult_gcd_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   314
wenzelm@63489
   315
lemma dvd_lcm1 [iff]: "a dvd lcm a b"
haftmann@60686
   316
proof -
haftmann@60686
   317
  have "normalize (a * b) div gcd a b = normalize a * (normalize b div gcd a b)"
haftmann@60686
   318
    by (simp add: lcm_gcd normalize_mult div_mult_swap)
haftmann@60686
   319
  then show ?thesis
haftmann@60686
   320
    by (simp add: lcm_gcd)
haftmann@60686
   321
qed
wenzelm@63489
   322
wenzelm@63489
   323
lemma dvd_lcm2 [iff]: "b dvd lcm a b"
haftmann@60686
   324
proof -
haftmann@60686
   325
  have "normalize (a * b) div gcd a b = normalize b * (normalize a div gcd a b)"
haftmann@60686
   326
    by (simp add: lcm_gcd normalize_mult div_mult_swap ac_simps)
haftmann@60686
   327
  then show ?thesis
haftmann@60686
   328
    by (simp add: lcm_gcd)
haftmann@60686
   329
qed
haftmann@60686
   330
wenzelm@63489
   331
lemma dvd_lcmI1: "a dvd b \<Longrightarrow> a dvd lcm b c"
wenzelm@63489
   332
  by (rule dvd_trans) (assumption, blast)
wenzelm@63489
   333
wenzelm@63489
   334
lemma dvd_lcmI2: "a dvd c \<Longrightarrow> a dvd lcm b c"
haftmann@60689
   335
  by (rule dvd_trans) (assumption, blast)
haftmann@60689
   336
wenzelm@63489
   337
lemma lcm_dvdD1: "lcm a b dvd c \<Longrightarrow> a dvd c"
haftmann@62345
   338
  using dvd_lcm1 [of a b] by (blast intro: dvd_trans)
haftmann@62345
   339
wenzelm@63489
   340
lemma lcm_dvdD2: "lcm a b dvd c \<Longrightarrow> b dvd c"
haftmann@62345
   341
  using dvd_lcm2 [of a b] by (blast intro: dvd_trans)
haftmann@62345
   342
haftmann@60686
   343
lemma lcm_least:
haftmann@60686
   344
  assumes "a dvd c" and "b dvd c"
haftmann@60686
   345
  shows "lcm a b dvd c"
haftmann@60686
   346
proof (cases "c = 0")
wenzelm@63489
   347
  case True
wenzelm@63489
   348
  then show ?thesis by simp
haftmann@60686
   349
next
wenzelm@63489
   350
  case False
wenzelm@63489
   351
  then have *: "is_unit (unit_factor c)"
wenzelm@63489
   352
    by simp
haftmann@60686
   353
  show ?thesis
haftmann@60686
   354
  proof (cases "gcd a b = 0")
wenzelm@63489
   355
    case True
wenzelm@63489
   356
    with assms show ?thesis by simp
haftmann@60686
   357
  next
wenzelm@63489
   358
    case False
wenzelm@63489
   359
    then have "a \<noteq> 0 \<or> b \<noteq> 0"
wenzelm@63489
   360
      by simp
haftmann@60686
   361
    with \<open>c \<noteq> 0\<close> assms have "a * b dvd a * c" "a * b dvd c * b"
haftmann@60686
   362
      by (simp_all add: mult_dvd_mono)
haftmann@60686
   363
    then have "normalize (a * b) dvd gcd (a * c) (b * c)"
haftmann@60686
   364
      by (auto intro: gcd_greatest simp add: ac_simps)
haftmann@60686
   365
    then have "normalize (a * b) dvd gcd (a * c) (b * c) * unit_factor c"
wenzelm@63489
   366
      using * by (simp add: dvd_mult_unit_iff)
haftmann@60686
   367
    then have "normalize (a * b) dvd gcd a b * c"
haftmann@60686
   368
      by (simp add: mult_gcd_right [of a b c])
haftmann@60686
   369
    then have "normalize (a * b) div gcd a b dvd c"
haftmann@60686
   370
      using False by (simp add: div_dvd_iff_mult ac_simps)
wenzelm@63489
   371
    then show ?thesis
wenzelm@63489
   372
      by (simp add: lcm_gcd)
haftmann@60686
   373
  qed
haftmann@60686
   374
qed
haftmann@60686
   375
wenzelm@63489
   376
lemma lcm_least_iff [simp]: "lcm a b dvd c \<longleftrightarrow> a dvd c \<and> b dvd c"
haftmann@60686
   377
  by (blast intro!: lcm_least intro: dvd_trans)
haftmann@60686
   378
wenzelm@63489
   379
lemma normalize_lcm [simp]: "normalize (lcm a b) = lcm a b"
haftmann@60686
   380
  by (simp add: lcm_gcd dvd_normalize_div)
haftmann@60686
   381
wenzelm@63489
   382
lemma lcm_0_left [simp]: "lcm 0 a = 0"
wenzelm@63489
   383
  by (simp add: lcm_gcd)
wenzelm@63489
   384
wenzelm@63489
   385
lemma lcm_0_right [simp]: "lcm a 0 = 0"
haftmann@60686
   386
  by (simp add: lcm_gcd)
wenzelm@63489
   387
wenzelm@63489
   388
lemma lcm_eq_0_iff: "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
wenzelm@63489
   389
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   390
proof
wenzelm@63489
   391
  assume ?P
wenzelm@63489
   392
  then have "0 dvd lcm a b"
wenzelm@63489
   393
    by simp
haftmann@60686
   394
  then have "0 dvd normalize (a * b) div gcd a b"
haftmann@60686
   395
    by (simp add: lcm_gcd)
haftmann@60686
   396
  then have "0 * gcd a b dvd normalize (a * b)"
haftmann@60686
   397
    using dvd_div_iff_mult [of "gcd a b" _ 0] by (cases "gcd a b = 0") simp_all
haftmann@60686
   398
  then have "normalize (a * b) = 0"
haftmann@60686
   399
    by simp
wenzelm@63489
   400
  then show ?Q
wenzelm@63489
   401
    by simp
haftmann@60686
   402
next
wenzelm@63489
   403
  assume ?Q
wenzelm@63489
   404
  then show ?P
wenzelm@63489
   405
    by auto
haftmann@60686
   406
qed
haftmann@60686
   407
wenzelm@63489
   408
lemma lcm_eq_1_iff [simp]: "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@61913
   409
  by (auto intro: associated_eqI)
haftmann@61913
   410
wenzelm@63489
   411
lemma unit_factor_lcm: "unit_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
haftmann@60686
   412
  by (simp add: unit_factor_gcd dvd_unit_factor_div lcm_gcd)
haftmann@60686
   413
wenzelm@61605
   414
sublocale lcm: abel_semigroup lcm
haftmann@60686
   415
proof
haftmann@60686
   416
  fix a b c
haftmann@60686
   417
  show "lcm a b = lcm b a"
haftmann@60686
   418
    by (simp add: lcm_gcd ac_simps normalize_mult dvd_normalize_div)
haftmann@60688
   419
  have "lcm (lcm a b) c dvd lcm a (lcm b c)"
haftmann@60688
   420
    and "lcm a (lcm b c) dvd lcm (lcm a b) c"
haftmann@60688
   421
    by (auto intro: lcm_least
haftmann@60686
   422
      dvd_trans [of b "lcm b c" "lcm a (lcm b c)"]
haftmann@60686
   423
      dvd_trans [of c "lcm b c" "lcm a (lcm b c)"]
haftmann@60686
   424
      dvd_trans [of a "lcm a b" "lcm (lcm a b) c"]
haftmann@60686
   425
      dvd_trans [of b "lcm a b" "lcm (lcm a b) c"])
haftmann@60686
   426
  then show "lcm (lcm a b) c = lcm a (lcm b c)"
haftmann@60688
   427
    by (rule associated_eqI) simp_all
haftmann@60686
   428
qed
haftmann@60686
   429
haftmann@64850
   430
sublocale lcm: bounded_quasi_semilattice lcm 1 0 normalize
haftmann@64850
   431
proof
haftmann@64850
   432
  show "lcm a a = normalize a" for a
haftmann@64850
   433
  proof -
haftmann@64850
   434
    have "lcm a a dvd a"
haftmann@64850
   435
      by (rule lcm_least) simp_all
haftmann@64850
   436
    then show ?thesis
haftmann@64850
   437
      by (auto intro: associated_eqI)
haftmann@64850
   438
  qed
haftmann@64850
   439
  show "lcm (normalize a) b = lcm a b" for a b
haftmann@64850
   440
    using dvd_lcm1 [of "normalize a" b] unfolding normalize_dvd_iff
haftmann@60688
   441
    by (auto intro: associated_eqI)
haftmann@64850
   442
  show "lcm 1 a = normalize a" for a
haftmann@64850
   443
    by (rule associated_eqI) simp_all
haftmann@64850
   444
qed simp_all
haftmann@64850
   445
haftmann@64850
   446
lemma lcm_self: "lcm a a = normalize a"
haftmann@64850
   447
  by (fact lcm.idem_normalize)
haftmann@64850
   448
haftmann@64850
   449
lemma lcm_left_idem: "lcm a (lcm a b) = lcm a b"
haftmann@64850
   450
  by (fact lcm.left_idem)
haftmann@64850
   451
haftmann@64850
   452
lemma lcm_right_idem: "lcm (lcm a b) b = lcm a b"
haftmann@64850
   453
  by (fact lcm.right_idem)
haftmann@61913
   454
wenzelm@63489
   455
lemma gcd_mult_lcm [simp]: "gcd a b * lcm a b = normalize a * normalize b"
haftmann@60686
   456
  by (simp add: lcm_gcd normalize_mult)
haftmann@60686
   457
wenzelm@63489
   458
lemma lcm_mult_gcd [simp]: "lcm a b * gcd a b = normalize a * normalize b"
wenzelm@63489
   459
  using gcd_mult_lcm [of a b] by (simp add: ac_simps)
haftmann@60686
   460
haftmann@60686
   461
lemma gcd_lcm:
haftmann@60686
   462
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@60686
   463
  shows "gcd a b = normalize (a * b) div lcm a b"
haftmann@60686
   464
proof -
haftmann@60686
   465
  from assms have "lcm a b \<noteq> 0"
haftmann@60686
   466
    by (simp add: lcm_eq_0_iff)
wenzelm@63489
   467
  have "gcd a b * lcm a b = normalize a * normalize b"
wenzelm@63489
   468
    by simp
haftmann@60686
   469
  then have "gcd a b * lcm a b div lcm a b = normalize (a * b) div lcm a b"
haftmann@60686
   470
    by (simp_all add: normalize_mult)
haftmann@60686
   471
  with \<open>lcm a b \<noteq> 0\<close> show ?thesis
haftmann@64240
   472
    using nonzero_mult_div_cancel_right [of "lcm a b" "gcd a b"] by simp
haftmann@60686
   473
qed
haftmann@60686
   474
haftmann@64850
   475
lemma lcm_1_left: "lcm 1 a = normalize a"
haftmann@64850
   476
  by (fact lcm.top_left_normalize)
haftmann@64850
   477
haftmann@64850
   478
lemma lcm_1_right: "lcm a 1 = normalize a"
haftmann@64850
   479
  by (fact lcm.top_right_normalize)
wenzelm@63489
   480
wenzelm@63489
   481
lemma lcm_mult_left: "lcm (c * a) (c * b) = normalize c * lcm a b"
haftmann@60686
   482
  by (cases "c = 0")
haftmann@60686
   483
    (simp_all add: gcd_mult_right lcm_gcd div_mult_swap normalize_mult ac_simps,
haftmann@60686
   484
      simp add: dvd_div_mult2_eq mult.left_commute [of "normalize c", symmetric])
haftmann@60686
   485
wenzelm@63489
   486
lemma lcm_mult_right: "lcm (a * c) (b * c) = lcm b a * normalize c"
haftmann@60686
   487
  using lcm_mult_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   488
wenzelm@63489
   489
lemma mult_lcm_left: "c * lcm a b = unit_factor c * lcm (c * a) (c * b)"
haftmann@60686
   490
  by (simp add: lcm_mult_left mult.assoc [symmetric])
haftmann@60686
   491
wenzelm@63489
   492
lemma mult_lcm_right: "lcm a b * c = lcm (a * c) (b * c) * unit_factor c"
haftmann@60686
   493
  using mult_lcm_left [of c a b] by (simp add: ac_simps)
eberlm@62429
   494
eberlm@62429
   495
lemma gcdI:
wenzelm@63489
   496
  assumes "c dvd a" and "c dvd b"
wenzelm@63489
   497
    and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
eberlm@62429
   498
    and "normalize c = c"
eberlm@62429
   499
  shows "c = gcd a b"
eberlm@62429
   500
  by (rule associated_eqI) (auto simp: assms intro: gcd_greatest)
eberlm@62429
   501
wenzelm@63489
   502
lemma gcd_unique:
wenzelm@63489
   503
  "d dvd a \<and> d dvd b \<and> normalize d = d \<and> (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
eberlm@62429
   504
  by rule (auto intro: gcdI simp: gcd_greatest)
eberlm@62429
   505
eberlm@62429
   506
lemma gcd_dvd_prod: "gcd a b dvd k * b"
eberlm@62429
   507
  using mult_dvd_mono [of 1] by auto
eberlm@62429
   508
eberlm@62429
   509
lemma gcd_proj2_if_dvd: "b dvd a \<Longrightarrow> gcd a b = normalize b"
eberlm@62429
   510
  by (rule gcdI [symmetric]) simp_all
eberlm@62429
   511
eberlm@62429
   512
lemma gcd_proj1_if_dvd: "a dvd b \<Longrightarrow> gcd a b = normalize a"
eberlm@62429
   513
  by (rule gcdI [symmetric]) simp_all
eberlm@62429
   514
eberlm@62429
   515
lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"
eberlm@62429
   516
proof
wenzelm@63489
   517
  assume *: "gcd m n = normalize m"
eberlm@62429
   518
  show "m dvd n"
eberlm@62429
   519
  proof (cases "m = 0")
wenzelm@63489
   520
    case True
wenzelm@63489
   521
    with * show ?thesis by simp
wenzelm@63489
   522
  next
wenzelm@63489
   523
    case [simp]: False
wenzelm@63489
   524
    from * have **: "m = gcd m n * unit_factor m"
eberlm@62429
   525
      by (simp add: unit_eq_div2)
wenzelm@63489
   526
    show ?thesis
wenzelm@63489
   527
      by (subst **) (simp add: mult_unit_dvd_iff)
wenzelm@63489
   528
  qed
eberlm@62429
   529
next
eberlm@62429
   530
  assume "m dvd n"
wenzelm@63489
   531
  then show "gcd m n = normalize m"
wenzelm@63489
   532
    by (rule gcd_proj1_if_dvd)
eberlm@62429
   533
qed
wenzelm@63489
   534
eberlm@62429
   535
lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
eberlm@62429
   536
  using gcd_proj1_iff [of n m] by (simp add: ac_simps)
eberlm@62429
   537
eberlm@62429
   538
lemma gcd_mult_distrib': "normalize c * gcd a b = gcd (c * a) (c * b)"
eberlm@62429
   539
  by (rule gcdI) (auto simp: normalize_mult gcd_greatest mult_dvd_mono gcd_mult_left[symmetric])
eberlm@62429
   540
wenzelm@63489
   541
lemma gcd_mult_distrib: "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"
eberlm@62429
   542
proof-
eberlm@62429
   543
  have "normalize k * gcd a b = gcd (k * a) (k * b)"
eberlm@62429
   544
    by (simp add: gcd_mult_distrib')
eberlm@62429
   545
  then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"
eberlm@62429
   546
    by simp
eberlm@62429
   547
  then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"
eberlm@62429
   548
    by (simp only: ac_simps)
eberlm@62429
   549
  then show ?thesis
eberlm@62429
   550
    by simp
eberlm@62429
   551
qed
eberlm@62429
   552
wenzelm@63489
   553
lemma lcm_mult_unit1: "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
eberlm@62429
   554
  by (rule associated_eqI) (simp_all add: mult_unit_dvd_iff dvd_lcmI1)
eberlm@62429
   555
wenzelm@63489
   556
lemma lcm_mult_unit2: "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
eberlm@62429
   557
  using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
eberlm@62429
   558
eberlm@62429
   559
lemma lcm_div_unit1:
eberlm@62429
   560
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
wenzelm@63489
   561
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1)
wenzelm@63489
   562
wenzelm@63489
   563
lemma lcm_div_unit2: "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
eberlm@62429
   564
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
eberlm@62429
   565
haftmann@64850
   566
lemma normalize_lcm_left: "lcm (normalize a) b = lcm a b"
haftmann@64850
   567
  by (fact lcm.normalize_left_idem)
haftmann@64850
   568
haftmann@64850
   569
lemma normalize_lcm_right: "lcm a (normalize b) = lcm a b"
haftmann@64850
   570
  by (fact lcm.normalize_right_idem)
eberlm@62429
   571
eberlm@62429
   572
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
eberlm@62429
   573
  apply (rule gcdI)
wenzelm@63489
   574
     apply simp_all
wenzelm@63489
   575
  apply (rule dvd_trans)
wenzelm@63489
   576
   apply (rule gcd_dvd1)
wenzelm@63489
   577
  apply (simp add: unit_simps)
eberlm@62429
   578
  done
eberlm@62429
   579
eberlm@62429
   580
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
wenzelm@63489
   581
  apply (subst gcd.commute)
wenzelm@63489
   582
  apply (subst gcd_mult_unit1)
wenzelm@63489
   583
   apply assumption
wenzelm@63489
   584
  apply (rule gcd.commute)
wenzelm@63489
   585
  done
eberlm@62429
   586
eberlm@62429
   587
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
eberlm@62429
   588
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
eberlm@62429
   589
eberlm@62429
   590
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
eberlm@62429
   591
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
eberlm@62429
   592
haftmann@64850
   593
lemma normalize_gcd_left: "gcd (normalize a) b = gcd a b"
haftmann@64850
   594
  by (fact gcd.normalize_left_idem)
haftmann@64850
   595
haftmann@64850
   596
lemma normalize_gcd_right: "gcd a (normalize b) = gcd a b"
haftmann@64850
   597
  by (fact gcd.normalize_right_idem)
eberlm@62429
   598
eberlm@62429
   599
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
eberlm@62429
   600
  by standard (simp_all add: fun_eq_iff ac_simps)
eberlm@62429
   601
eberlm@62429
   602
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
eberlm@62429
   603
  by standard (simp_all add: fun_eq_iff ac_simps)
eberlm@62429
   604
wenzelm@63489
   605
lemma gcd_dvd_antisym: "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
eberlm@62429
   606
proof (rule gcdI)
wenzelm@63489
   607
  assume *: "gcd a b dvd gcd c d"
wenzelm@63489
   608
    and **: "gcd c d dvd gcd a b"
wenzelm@63489
   609
  have "gcd c d dvd c"
wenzelm@63489
   610
    by simp
wenzelm@63489
   611
  with * show "gcd a b dvd c"
wenzelm@63489
   612
    by (rule dvd_trans)
wenzelm@63489
   613
  have "gcd c d dvd d"
wenzelm@63489
   614
    by simp
wenzelm@63489
   615
  with * show "gcd a b dvd d"
wenzelm@63489
   616
    by (rule dvd_trans)
eberlm@62429
   617
  show "normalize (gcd a b) = gcd a b"
eberlm@62429
   618
    by simp
eberlm@62429
   619
  fix l assume "l dvd c" and "l dvd d"
wenzelm@63489
   620
  then have "l dvd gcd c d"
wenzelm@63489
   621
    by (rule gcd_greatest)
wenzelm@63489
   622
  from this and ** show "l dvd gcd a b"
wenzelm@63489
   623
    by (rule dvd_trans)
eberlm@62429
   624
qed
eberlm@62429
   625
eberlm@62429
   626
lemma gcd_add1 [simp]: "gcd (m + n) n = gcd m n"
eberlm@62429
   627
  by (rule gcdI [symmetric]) (simp_all add: dvd_add_left_iff)
eberlm@62429
   628
eberlm@62429
   629
lemma gcd_add2 [simp]: "gcd m (m + n) = gcd m n"
eberlm@62429
   630
  using gcd_add1 [of n m] by (simp add: ac_simps)
eberlm@62429
   631
eberlm@62429
   632
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
eberlm@62429
   633
  by (rule gcdI [symmetric]) (simp_all add: dvd_add_right_iff)
eberlm@62429
   634
wenzelm@63489
   635
lemma lcm_gcd_prod: "lcm a b * gcd a b = normalize (a * b)"
eberlm@62429
   636
  by (simp add: lcm_gcd)
eberlm@62429
   637
eberlm@62429
   638
declare unit_factor_lcm [simp]
eberlm@62429
   639
eberlm@62429
   640
lemma lcmI:
eberlm@62429
   641
  assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
eberlm@62429
   642
    and "normalize c = c"
eberlm@62429
   643
  shows "c = lcm a b"
eberlm@62429
   644
  by (rule associated_eqI) (auto simp: assms intro: lcm_least)
eberlm@62429
   645
wenzelm@63489
   646
lemma gcd_dvd_lcm [simp]: "gcd a b dvd lcm a b"
eberlm@62429
   647
  using gcd_dvd2 by (rule dvd_lcmI2)
eberlm@62429
   648
eberlm@62429
   649
lemmas lcm_0 = lcm_0_right
eberlm@62429
   650
eberlm@62429
   651
lemma lcm_unique:
wenzelm@63489
   652
  "a dvd d \<and> b dvd d \<and> normalize d = d \<and> (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
eberlm@62429
   653
  by rule (auto intro: lcmI simp: lcm_least lcm_eq_0_iff)
eberlm@62429
   654
wenzelm@63489
   655
lemma lcm_proj1_if_dvd: "b dvd a \<Longrightarrow> lcm a b = normalize a"
wenzelm@63489
   656
  apply (cases "a = 0")
wenzelm@63489
   657
   apply simp
wenzelm@63489
   658
  apply (rule sym)
wenzelm@63489
   659
  apply (rule lcmI)
wenzelm@63489
   660
     apply simp_all
wenzelm@63489
   661
  done
wenzelm@63489
   662
wenzelm@63489
   663
lemma lcm_proj2_if_dvd: "a dvd b \<Longrightarrow> lcm a b = normalize b"
eberlm@62429
   664
  using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
eberlm@62429
   665
wenzelm@63489
   666
lemma lcm_proj1_iff: "lcm m n = normalize m \<longleftrightarrow> n dvd m"
eberlm@62429
   667
proof
wenzelm@63489
   668
  assume *: "lcm m n = normalize m"
eberlm@62429
   669
  show "n dvd m"
eberlm@62429
   670
  proof (cases "m = 0")
wenzelm@63489
   671
    case True
wenzelm@63489
   672
    then show ?thesis by simp
wenzelm@63489
   673
  next
wenzelm@63489
   674
    case [simp]: False
wenzelm@63489
   675
    from * have **: "m = lcm m n * unit_factor m"
eberlm@62429
   676
      by (simp add: unit_eq_div2)
wenzelm@63489
   677
    show ?thesis by (subst **) simp
wenzelm@63489
   678
  qed
eberlm@62429
   679
next
eberlm@62429
   680
  assume "n dvd m"
wenzelm@63489
   681
  then show "lcm m n = normalize m"
wenzelm@63489
   682
    by (rule lcm_proj1_if_dvd)
eberlm@62429
   683
qed
eberlm@62429
   684
wenzelm@63489
   685
lemma lcm_proj2_iff: "lcm m n = normalize n \<longleftrightarrow> m dvd n"
eberlm@62429
   686
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
eberlm@62429
   687
haftmann@64850
   688
lemma lcm_mult_distrib': "normalize c * lcm a b = lcm (c * a) (c * b)"
haftmann@64850
   689
  by (rule lcmI) (auto simp: normalize_mult lcm_least mult_dvd_mono lcm_mult_left [symmetric])
haftmann@64850
   690
haftmann@64850
   691
lemma lcm_mult_distrib: "k * lcm a b = lcm (k * a) (k * b) * unit_factor k"
haftmann@64850
   692
proof-
haftmann@64850
   693
  have "normalize k * lcm a b = lcm (k * a) (k * b)"
haftmann@64850
   694
    by (simp add: lcm_mult_distrib')
haftmann@64850
   695
  then have "normalize k * lcm a b * unit_factor k = lcm (k * a) (k * b) * unit_factor k"
haftmann@64850
   696
    by simp
haftmann@64850
   697
  then have "normalize k * unit_factor k * lcm a b  = lcm (k * a) (k * b) * unit_factor k"
haftmann@64850
   698
    by (simp only: ac_simps)
haftmann@64850
   699
  then show ?thesis
haftmann@64850
   700
    by simp
haftmann@64850
   701
qed
haftmann@64850
   702
haftmann@63924
   703
lemma dvd_productE:
haftmann@67051
   704
  assumes "p dvd a * b"
haftmann@63924
   705
  obtains x y where "p = x * y" "x dvd a" "y dvd b"
haftmann@63924
   706
proof (cases "a = 0")
haftmann@63924
   707
  case True
haftmann@63924
   708
  thus ?thesis by (intro that[of p 1]) simp_all
haftmann@63924
   709
next
haftmann@63924
   710
  case False
haftmann@63924
   711
  define x y where "x = gcd a p" and "y = p div x"
haftmann@63924
   712
  have "p = x * y" by (simp add: x_def y_def)
haftmann@63924
   713
  moreover have "x dvd a" by (simp add: x_def)
haftmann@63924
   714
  moreover from assms have "p dvd gcd (b * a) (b * p)"
haftmann@63924
   715
    by (intro gcd_greatest) (simp_all add: mult.commute)
haftmann@63924
   716
  hence "p dvd b * gcd a p" by (simp add: gcd_mult_distrib)
wenzelm@65552
   717
  with False have "y dvd b"
haftmann@63924
   718
    by (simp add: x_def y_def div_dvd_iff_mult assms)
haftmann@63924
   719
  ultimately show ?thesis by (rule that)
haftmann@63924
   720
qed
haftmann@63924
   721
haftmann@60686
   722
end
haftmann@60686
   723
haftmann@62345
   724
class ring_gcd = comm_ring_1 + semiring_gcd
eberlm@62429
   725
begin
eberlm@62429
   726
wenzelm@63489
   727
lemma gcd_neg1 [simp]: "gcd (-a) b = gcd a b"
wenzelm@63489
   728
  by (rule sym, rule gcdI) (simp_all add: gcd_greatest)
wenzelm@63489
   729
wenzelm@63489
   730
lemma gcd_neg2 [simp]: "gcd a (-b) = gcd a b"
wenzelm@63489
   731
  by (rule sym, rule gcdI) (simp_all add: gcd_greatest)
wenzelm@63489
   732
wenzelm@63489
   733
lemma gcd_neg_numeral_1 [simp]: "gcd (- numeral n) a = gcd (numeral n) a"
eberlm@62429
   734
  by (fact gcd_neg1)
eberlm@62429
   735
wenzelm@63489
   736
lemma gcd_neg_numeral_2 [simp]: "gcd a (- numeral n) = gcd a (numeral n)"
eberlm@62429
   737
  by (fact gcd_neg2)
eberlm@62429
   738
eberlm@62429
   739
lemma gcd_diff1: "gcd (m - n) n = gcd m n"
wenzelm@63489
   740
  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric], subst gcd_add1, simp)
eberlm@62429
   741
eberlm@62429
   742
lemma gcd_diff2: "gcd (n - m) n = gcd m n"
wenzelm@63489
   743
  by (subst gcd_neg1[symmetric]) (simp only: minus_diff_eq gcd_diff1)
eberlm@62429
   744
eberlm@62429
   745
lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
wenzelm@63489
   746
  by (rule sym, rule lcmI) (simp_all add: lcm_least lcm_eq_0_iff)
eberlm@62429
   747
eberlm@62429
   748
lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
wenzelm@63489
   749
  by (rule sym, rule lcmI) (simp_all add: lcm_least lcm_eq_0_iff)
eberlm@62429
   750
eberlm@62429
   751
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
eberlm@62429
   752
  by (fact lcm_neg1)
eberlm@62429
   753
eberlm@62429
   754
lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
eberlm@62429
   755
  by (fact lcm_neg2)
eberlm@62429
   756
eberlm@62429
   757
end
haftmann@62345
   758
haftmann@60686
   759
class semiring_Gcd = semiring_gcd + Gcd +
haftmann@60686
   760
  assumes Gcd_dvd: "a \<in> A \<Longrightarrow> Gcd A dvd a"
haftmann@60686
   761
    and Gcd_greatest: "(\<And>b. b \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> a dvd Gcd A"
haftmann@60686
   762
    and normalize_Gcd [simp]: "normalize (Gcd A) = Gcd A"
haftmann@62345
   763
  assumes dvd_Lcm: "a \<in> A \<Longrightarrow> a dvd Lcm A"
haftmann@62345
   764
    and Lcm_least: "(\<And>b. b \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> Lcm A dvd a"
haftmann@62345
   765
    and normalize_Lcm [simp]: "normalize (Lcm A) = Lcm A"
haftmann@60686
   766
begin
haftmann@60686
   767
wenzelm@63489
   768
lemma Lcm_Gcd: "Lcm A = Gcd {b. \<forall>a\<in>A. a dvd b}"
haftmann@62345
   769
  by (rule associated_eqI) (auto intro: Gcd_dvd dvd_Lcm Gcd_greatest Lcm_least)
haftmann@62345
   770
wenzelm@63489
   771
lemma Gcd_Lcm: "Gcd A = Lcm {b. \<forall>a\<in>A. b dvd a}"
haftmann@62345
   772
  by (rule associated_eqI) (auto intro: Gcd_dvd dvd_Lcm Gcd_greatest Lcm_least)
haftmann@62345
   773
wenzelm@63489
   774
lemma Gcd_empty [simp]: "Gcd {} = 0"
haftmann@60686
   775
  by (rule dvd_0_left, rule Gcd_greatest) simp
haftmann@60686
   776
wenzelm@63489
   777
lemma Lcm_empty [simp]: "Lcm {} = 1"
haftmann@62345
   778
  by (auto intro: associated_eqI Lcm_least)
haftmann@62345
   779
wenzelm@63489
   780
lemma Gcd_insert [simp]: "Gcd (insert a A) = gcd a (Gcd A)"
haftmann@62345
   781
proof -
haftmann@62345
   782
  have "Gcd (insert a A) dvd gcd a (Gcd A)"
haftmann@62345
   783
    by (auto intro: Gcd_dvd Gcd_greatest)
haftmann@62345
   784
  moreover have "gcd a (Gcd A) dvd Gcd (insert a A)"
haftmann@62345
   785
  proof (rule Gcd_greatest)
haftmann@62345
   786
    fix b
haftmann@62345
   787
    assume "b \<in> insert a A"
haftmann@62345
   788
    then show "gcd a (Gcd A) dvd b"
haftmann@62345
   789
    proof
wenzelm@63489
   790
      assume "b = a"
wenzelm@63489
   791
      then show ?thesis
wenzelm@63489
   792
        by simp
haftmann@62345
   793
    next
haftmann@62345
   794
      assume "b \<in> A"
wenzelm@63489
   795
      then have "Gcd A dvd b"
wenzelm@63489
   796
        by (rule Gcd_dvd)
wenzelm@63489
   797
      moreover have "gcd a (Gcd A) dvd Gcd A"
wenzelm@63489
   798
        by simp
wenzelm@63489
   799
      ultimately show ?thesis
wenzelm@63489
   800
        by (blast intro: dvd_trans)
haftmann@62345
   801
    qed
haftmann@62345
   802
  qed
haftmann@62345
   803
  ultimately show ?thesis
haftmann@62345
   804
    by (auto intro: associated_eqI)
haftmann@62345
   805
qed
haftmann@62345
   806
wenzelm@63489
   807
lemma Lcm_insert [simp]: "Lcm (insert a A) = lcm a (Lcm A)"
haftmann@62345
   808
proof (rule sym)
haftmann@62345
   809
  have "lcm a (Lcm A) dvd Lcm (insert a A)"
haftmann@62345
   810
    by (auto intro: dvd_Lcm Lcm_least)
haftmann@62345
   811
  moreover have "Lcm (insert a A) dvd lcm a (Lcm A)"
haftmann@62345
   812
  proof (rule Lcm_least)
haftmann@62345
   813
    fix b
haftmann@62345
   814
    assume "b \<in> insert a A"
haftmann@62345
   815
    then show "b dvd lcm a (Lcm A)"
haftmann@62345
   816
    proof
wenzelm@63489
   817
      assume "b = a"
wenzelm@63489
   818
      then show ?thesis by simp
haftmann@62345
   819
    next
haftmann@62345
   820
      assume "b \<in> A"
wenzelm@63489
   821
      then have "b dvd Lcm A"
wenzelm@63489
   822
        by (rule dvd_Lcm)
wenzelm@63489
   823
      moreover have "Lcm A dvd lcm a (Lcm A)"
wenzelm@63489
   824
        by simp
wenzelm@63489
   825
      ultimately show ?thesis
wenzelm@63489
   826
        by (blast intro: dvd_trans)
haftmann@62345
   827
    qed
haftmann@62345
   828
  qed
haftmann@62345
   829
  ultimately show "lcm a (Lcm A) = Lcm (insert a A)"
haftmann@62345
   830
    by (rule associated_eqI) (simp_all add: lcm_eq_0_iff)
haftmann@62345
   831
qed
haftmann@62345
   832
eberlm@62429
   833
lemma LcmI:
wenzelm@63489
   834
  assumes "\<And>a. a \<in> A \<Longrightarrow> a dvd b"
wenzelm@63489
   835
    and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> b dvd c"
wenzelm@63489
   836
    and "normalize b = b"
wenzelm@63489
   837
  shows "b = Lcm A"
eberlm@62429
   838
  by (rule associated_eqI) (auto simp: assms dvd_Lcm intro: Lcm_least)
eberlm@62429
   839
wenzelm@63489
   840
lemma Lcm_subset: "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
eberlm@62429
   841
  by (blast intro: Lcm_least dvd_Lcm)
eberlm@62429
   842
wenzelm@63489
   843
lemma Lcm_Un: "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
eberlm@62429
   844
  apply (rule lcmI)
wenzelm@63489
   845
     apply (blast intro: Lcm_subset)
wenzelm@63489
   846
    apply (blast intro: Lcm_subset)
wenzelm@63489
   847
   apply (intro Lcm_least ballI, elim UnE)
wenzelm@63489
   848
    apply (rule dvd_trans, erule dvd_Lcm, assumption)
wenzelm@63489
   849
   apply (rule dvd_trans, erule dvd_Lcm, assumption)
eberlm@62429
   850
  apply simp
eberlm@62429
   851
  done
wenzelm@63489
   852
wenzelm@63489
   853
lemma Gcd_0_iff [simp]: "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
wenzelm@63489
   854
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   855
proof
haftmann@60686
   856
  assume ?P
haftmann@60686
   857
  show ?Q
haftmann@60686
   858
  proof
haftmann@60686
   859
    fix a
haftmann@60686
   860
    assume "a \<in> A"
wenzelm@63489
   861
    then have "Gcd A dvd a"
wenzelm@63489
   862
      by (rule Gcd_dvd)
wenzelm@63489
   863
    with \<open>?P\<close> have "a = 0"
wenzelm@63489
   864
      by simp
wenzelm@63489
   865
    then show "a \<in> {0}"
wenzelm@63489
   866
      by simp
haftmann@60686
   867
  qed
haftmann@60686
   868
next
haftmann@60686
   869
  assume ?Q
haftmann@60686
   870
  have "0 dvd Gcd A"
haftmann@60686
   871
  proof (rule Gcd_greatest)
haftmann@60686
   872
    fix a
haftmann@60686
   873
    assume "a \<in> A"
wenzelm@63489
   874
    with \<open>?Q\<close> have "a = 0"
wenzelm@63489
   875
      by auto
wenzelm@63489
   876
    then show "0 dvd a"
wenzelm@63489
   877
      by simp
haftmann@60686
   878
  qed
wenzelm@63489
   879
  then show ?P
wenzelm@63489
   880
    by simp
haftmann@60686
   881
qed
haftmann@60686
   882
wenzelm@63489
   883
lemma Lcm_1_iff [simp]: "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
wenzelm@63489
   884
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   885
proof
haftmann@60686
   886
  assume ?P
haftmann@60686
   887
  show ?Q
haftmann@60686
   888
  proof
haftmann@60686
   889
    fix a
haftmann@60686
   890
    assume "a \<in> A"
haftmann@60686
   891
    then have "a dvd Lcm A"
haftmann@60686
   892
      by (rule dvd_Lcm)
haftmann@60686
   893
    with \<open>?P\<close> show "is_unit a"
haftmann@60686
   894
      by simp
haftmann@60686
   895
  qed
haftmann@60686
   896
next
haftmann@60686
   897
  assume ?Q
haftmann@60686
   898
  then have "is_unit (Lcm A)"
haftmann@60686
   899
    by (blast intro: Lcm_least)
haftmann@60686
   900
  then have "normalize (Lcm A) = 1"
haftmann@60686
   901
    by (rule is_unit_normalize)
haftmann@60686
   902
  then show ?P
haftmann@60686
   903
    by simp
haftmann@60686
   904
qed
haftmann@60686
   905
wenzelm@63489
   906
lemma unit_factor_Lcm: "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
haftmann@62345
   907
proof (cases "Lcm A = 0")
wenzelm@63489
   908
  case True
wenzelm@63489
   909
  then show ?thesis
wenzelm@63489
   910
    by simp
haftmann@62345
   911
next
haftmann@62345
   912
  case False
haftmann@62345
   913
  with unit_factor_normalize have "unit_factor (normalize (Lcm A)) = 1"
haftmann@62345
   914
    by blast
haftmann@62345
   915
  with False show ?thesis
haftmann@62345
   916
    by simp
haftmann@62345
   917
qed
haftmann@62345
   918
eberlm@62429
   919
lemma unit_factor_Gcd: "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
wenzelm@63489
   920
  by (simp add: Gcd_Lcm unit_factor_Lcm)
eberlm@62429
   921
eberlm@62429
   922
lemma GcdI:
wenzelm@63489
   923
  assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a"
wenzelm@63489
   924
    and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
eberlm@62429
   925
    and "normalize b = b"
eberlm@62429
   926
  shows "b = Gcd A"
eberlm@62429
   927
  by (rule associated_eqI) (auto simp: assms Gcd_dvd intro: Gcd_greatest)
eberlm@62429
   928
haftmann@62345
   929
lemma Gcd_eq_1_I:
haftmann@62345
   930
  assumes "is_unit a" and "a \<in> A"
haftmann@62345
   931
  shows "Gcd A = 1"
haftmann@62345
   932
proof -
haftmann@62345
   933
  from assms have "is_unit (Gcd A)"
haftmann@62345
   934
    by (blast intro: Gcd_dvd dvd_unit_imp_unit)
haftmann@62345
   935
  then have "normalize (Gcd A) = 1"
haftmann@62345
   936
    by (rule is_unit_normalize)
haftmann@62345
   937
  then show ?thesis
haftmann@62345
   938
    by simp
haftmann@62345
   939
qed
haftmann@62345
   940
haftmann@60686
   941
lemma Lcm_eq_0_I:
haftmann@60686
   942
  assumes "0 \<in> A"
haftmann@60686
   943
  shows "Lcm A = 0"
haftmann@60686
   944
proof -
haftmann@60686
   945
  from assms have "0 dvd Lcm A"
haftmann@60686
   946
    by (rule dvd_Lcm)
haftmann@60686
   947
  then show ?thesis
haftmann@60686
   948
    by simp
haftmann@60686
   949
qed
haftmann@60686
   950
wenzelm@63489
   951
lemma Gcd_UNIV [simp]: "Gcd UNIV = 1"
haftmann@62345
   952
  using dvd_refl by (rule Gcd_eq_1_I) simp
haftmann@62345
   953
wenzelm@63489
   954
lemma Lcm_UNIV [simp]: "Lcm UNIV = 0"
haftmann@61929
   955
  by (rule Lcm_eq_0_I) simp
haftmann@60686
   956
haftmann@61929
   957
lemma Lcm_0_iff:
haftmann@61929
   958
  assumes "finite A"
haftmann@61929
   959
  shows "Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
haftmann@61929
   960
proof (cases "A = {}")
wenzelm@63489
   961
  case True
wenzelm@63489
   962
  then show ?thesis by simp
haftmann@61929
   963
next
wenzelm@63489
   964
  case False
wenzelm@63489
   965
  with assms show ?thesis
wenzelm@63489
   966
    by (induct A rule: finite_ne_induct) (auto simp add: lcm_eq_0_iff)
haftmann@60686
   967
qed
haftmann@61929
   968
wenzelm@63489
   969
lemma Gcd_image_normalize [simp]: "Gcd (normalize ` A) = Gcd A"
haftmann@62345
   970
proof -
haftmann@62345
   971
  have "Gcd (normalize ` A) dvd a" if "a \<in> A" for a
haftmann@62345
   972
  proof -
wenzelm@63489
   973
    from that obtain B where "A = insert a B"
wenzelm@63489
   974
      by blast
haftmann@62350
   975
    moreover have "gcd (normalize a) (Gcd (normalize ` B)) dvd normalize a"
haftmann@62345
   976
      by (rule gcd_dvd1)
haftmann@62345
   977
    ultimately show "Gcd (normalize ` A) dvd a"
haftmann@62345
   978
      by simp
haftmann@62345
   979
  qed
haftmann@62345
   980
  then have "Gcd (normalize ` A) dvd Gcd A" and "Gcd A dvd Gcd (normalize ` A)"
haftmann@62345
   981
    by (auto intro!: Gcd_greatest intro: Gcd_dvd)
haftmann@62345
   982
  then show ?thesis
haftmann@62345
   983
    by (auto intro: associated_eqI)
haftmann@62345
   984
qed
haftmann@62345
   985
haftmann@62346
   986
lemma Gcd_eqI:
haftmann@62346
   987
  assumes "normalize a = a"
haftmann@62346
   988
  assumes "\<And>b. b \<in> A \<Longrightarrow> a dvd b"
haftmann@62346
   989
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> c dvd b) \<Longrightarrow> c dvd a"
haftmann@62346
   990
  shows "Gcd A = a"
haftmann@62346
   991
  using assms by (blast intro: associated_eqI Gcd_greatest Gcd_dvd normalize_Gcd)
haftmann@62346
   992
wenzelm@63489
   993
lemma dvd_GcdD: "x dvd Gcd A \<Longrightarrow> y \<in> A \<Longrightarrow> x dvd y"
wenzelm@63489
   994
  using Gcd_dvd dvd_trans by blast
wenzelm@63489
   995
wenzelm@63489
   996
lemma dvd_Gcd_iff: "x dvd Gcd A \<longleftrightarrow> (\<forall>y\<in>A. x dvd y)"
eberlm@63359
   997
  by (blast dest: dvd_GcdD intro: Gcd_greatest)
eberlm@63359
   998
nipkow@67399
   999
lemma Gcd_mult: "Gcd (( * ) c ` A) = normalize c * Gcd A"
eberlm@63359
  1000
proof (cases "c = 0")
wenzelm@63489
  1001
  case True
wenzelm@63489
  1002
  then show ?thesis by auto
wenzelm@63489
  1003
next
eberlm@63359
  1004
  case [simp]: False
nipkow@67399
  1005
  have "Gcd (( * ) c ` A) div c dvd Gcd A"
eberlm@63359
  1006
    by (intro Gcd_greatest, subst div_dvd_iff_mult)
eberlm@63359
  1007
       (auto intro!: Gcd_greatest Gcd_dvd simp: mult.commute[of _ c])
nipkow@67399
  1008
  then have "Gcd (( * ) c ` A) dvd c * Gcd A"
eberlm@63359
  1009
    by (subst (asm) div_dvd_iff_mult) (auto intro: Gcd_greatest simp: mult_ac)
eberlm@63359
  1010
  also have "c * Gcd A = (normalize c * Gcd A) * unit_factor c"
eberlm@63359
  1011
    by (subst unit_factor_mult_normalize [symmetric]) (simp only: mult_ac)
nipkow@67399
  1012
  also have "Gcd (( * ) c ` A) dvd \<dots> \<longleftrightarrow> Gcd (( * ) c ` A) dvd normalize c * Gcd A"
eberlm@63359
  1013
    by (simp add: dvd_mult_unit_iff)
nipkow@67399
  1014
  finally have "Gcd (( * ) c ` A) dvd normalize c * Gcd A" .
nipkow@67399
  1015
  moreover have "normalize c * Gcd A dvd Gcd (( * ) c ` A)"
eberlm@63359
  1016
    by (intro Gcd_greatest) (auto intro: mult_dvd_mono Gcd_dvd)
nipkow@67399
  1017
  ultimately have "normalize (Gcd (( * ) c ` A)) = normalize (normalize c * Gcd A)"
eberlm@63359
  1018
    by (rule associatedI)
wenzelm@63489
  1019
  then show ?thesis
wenzelm@63489
  1020
    by (simp add: normalize_mult)
wenzelm@63489
  1021
qed
eberlm@63359
  1022
haftmann@62346
  1023
lemma Lcm_eqI:
haftmann@62346
  1024
  assumes "normalize a = a"
wenzelm@63489
  1025
    and "\<And>b. b \<in> A \<Longrightarrow> b dvd a"
haftmann@62346
  1026
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> b dvd c) \<Longrightarrow> a dvd c"
haftmann@62346
  1027
  shows "Lcm A = a"
haftmann@62346
  1028
  using assms by (blast intro: associated_eqI Lcm_least dvd_Lcm normalize_Lcm)
haftmann@62346
  1029
wenzelm@63489
  1030
lemma Lcm_dvdD: "Lcm A dvd x \<Longrightarrow> y \<in> A \<Longrightarrow> y dvd x"
wenzelm@63489
  1031
  using dvd_Lcm dvd_trans by blast
wenzelm@63489
  1032
wenzelm@63489
  1033
lemma Lcm_dvd_iff: "Lcm A dvd x \<longleftrightarrow> (\<forall>y\<in>A. y dvd x)"
eberlm@63359
  1034
  by (blast dest: Lcm_dvdD intro: Lcm_least)
eberlm@63359
  1035
wenzelm@63489
  1036
lemma Lcm_mult:
eberlm@63359
  1037
  assumes "A \<noteq> {}"
nipkow@67399
  1038
  shows "Lcm (( * ) c ` A) = normalize c * Lcm A"
eberlm@63359
  1039
proof (cases "c = 0")
eberlm@63359
  1040
  case True
nipkow@67399
  1041
  with assms have "( * ) c ` A = {0}"
wenzelm@63489
  1042
    by auto
wenzelm@63489
  1043
  with True show ?thesis by auto
eberlm@63359
  1044
next
eberlm@63359
  1045
  case [simp]: False
wenzelm@63489
  1046
  from assms obtain x where x: "x \<in> A"
wenzelm@63489
  1047
    by blast
wenzelm@63489
  1048
  have "c dvd c * x"
wenzelm@63489
  1049
    by simp
nipkow@67399
  1050
  also from x have "c * x dvd Lcm (( * ) c ` A)"
wenzelm@63489
  1051
    by (intro dvd_Lcm) auto
nipkow@67399
  1052
  finally have dvd: "c dvd Lcm (( * ) c ` A)" .
nipkow@67399
  1053
nipkow@67399
  1054
  have "Lcm A dvd Lcm (( * ) c ` A) div c"
eberlm@63359
  1055
    by (intro Lcm_least dvd_mult_imp_div)
wenzelm@63489
  1056
      (auto intro!: Lcm_least dvd_Lcm simp: mult.commute[of _ c])
nipkow@67399
  1057
  then have "c * Lcm A dvd Lcm (( * ) c ` A)"
eberlm@63359
  1058
    by (subst (asm) dvd_div_iff_mult) (auto intro!: Lcm_least simp: mult_ac dvd)
eberlm@63359
  1059
  also have "c * Lcm A = (normalize c * Lcm A) * unit_factor c"
eberlm@63359
  1060
    by (subst unit_factor_mult_normalize [symmetric]) (simp only: mult_ac)
nipkow@67399
  1061
  also have "\<dots> dvd Lcm (( * ) c ` A) \<longleftrightarrow> normalize c * Lcm A dvd Lcm (( * ) c ` A)"
eberlm@63359
  1062
    by (simp add: mult_unit_dvd_iff)
nipkow@67399
  1063
  finally have "normalize c * Lcm A dvd Lcm (( * ) c ` A)" .
nipkow@67399
  1064
  moreover have "Lcm (( * ) c ` A) dvd normalize c * Lcm A"
eberlm@63359
  1065
    by (intro Lcm_least) (auto intro: mult_dvd_mono dvd_Lcm)
nipkow@67399
  1066
  ultimately have "normalize (normalize c * Lcm A) = normalize (Lcm (( * ) c ` A))"
eberlm@63359
  1067
    by (rule associatedI)
wenzelm@63489
  1068
  then show ?thesis
wenzelm@63489
  1069
    by (simp add: normalize_mult)
eberlm@62429
  1070
qed
eberlm@62429
  1071
wenzelm@63489
  1072
lemma Lcm_no_units: "Lcm A = Lcm (A - {a. is_unit a})"
wenzelm@63489
  1073
proof -
wenzelm@63489
  1074
  have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A"
wenzelm@63489
  1075
    by blast
wenzelm@63489
  1076
  then have "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
wenzelm@63489
  1077
    by (simp add: Lcm_Un [symmetric])
wenzelm@63489
  1078
  also have "Lcm {a\<in>A. is_unit a} = 1"
wenzelm@63489
  1079
    by simp
wenzelm@63489
  1080
  finally show ?thesis
wenzelm@63489
  1081
    by simp
wenzelm@63489
  1082
qed
wenzelm@63489
  1083
wenzelm@63489
  1084
lemma Lcm_0_iff': "Lcm A = 0 \<longleftrightarrow> (\<nexists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
eberlm@62429
  1085
  by (metis Lcm_least dvd_0_left dvd_Lcm)
eberlm@62429
  1086
wenzelm@63489
  1087
lemma Lcm_no_multiple: "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not> a dvd m)) \<Longrightarrow> Lcm A = 0"
eberlm@62429
  1088
  by (auto simp: Lcm_0_iff')
eberlm@62429
  1089
wenzelm@63489
  1090
lemma Lcm_singleton [simp]: "Lcm {a} = normalize a"
eberlm@62429
  1091
  by simp
eberlm@62429
  1092
wenzelm@63489
  1093
lemma Lcm_2 [simp]: "Lcm {a, b} = lcm a b"
eberlm@62429
  1094
  by simp
eberlm@62429
  1095
wenzelm@63489
  1096
lemma Gcd_1: "1 \<in> A \<Longrightarrow> Gcd A = 1"
eberlm@62429
  1097
  by (auto intro!: Gcd_eq_1_I)
eberlm@62429
  1098
eberlm@62429
  1099
lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
eberlm@62429
  1100
  by simp
eberlm@62429
  1101
wenzelm@63489
  1102
lemma Gcd_2 [simp]: "Gcd {a, b} = gcd a b"
eberlm@62429
  1103
  by simp
eberlm@62429
  1104
haftmann@62350
  1105
end
haftmann@62345
  1106
wenzelm@65552
  1107
haftmann@64850
  1108
subsection \<open>An aside: GCD and LCM on finite sets for incomplete gcd rings\<close>
haftmann@64850
  1109
haftmann@64850
  1110
context semiring_gcd
haftmann@64850
  1111
begin
haftmann@64850
  1112
haftmann@64850
  1113
sublocale Gcd_fin: bounded_quasi_semilattice_set gcd 0 1 normalize
haftmann@64850
  1114
defines
haftmann@64850
  1115
  Gcd_fin ("Gcd\<^sub>f\<^sub>i\<^sub>n _" [900] 900) = "Gcd_fin.F :: 'a set \<Rightarrow> 'a" ..
haftmann@64850
  1116
haftmann@64850
  1117
abbreviation gcd_list :: "'a list \<Rightarrow> 'a"
haftmann@64850
  1118
  where "gcd_list xs \<equiv> Gcd\<^sub>f\<^sub>i\<^sub>n (set xs)"
haftmann@64850
  1119
haftmann@64850
  1120
sublocale Lcm_fin: bounded_quasi_semilattice_set lcm 1 0 normalize
haftmann@64850
  1121
defines
haftmann@64850
  1122
  Lcm_fin ("Lcm\<^sub>f\<^sub>i\<^sub>n _" [900] 900) = Lcm_fin.F ..
haftmann@64850
  1123
haftmann@64850
  1124
abbreviation lcm_list :: "'a list \<Rightarrow> 'a"
haftmann@64850
  1125
  where "lcm_list xs \<equiv> Lcm\<^sub>f\<^sub>i\<^sub>n (set xs)"
wenzelm@65552
  1126
haftmann@64850
  1127
lemma Gcd_fin_dvd:
haftmann@64850
  1128
  "a \<in> A \<Longrightarrow> Gcd\<^sub>f\<^sub>i\<^sub>n A dvd a"
wenzelm@65552
  1129
  by (induct A rule: infinite_finite_induct)
haftmann@64850
  1130
    (auto intro: dvd_trans)
haftmann@64850
  1131
haftmann@64850
  1132
lemma dvd_Lcm_fin:
haftmann@64850
  1133
  "a \<in> A \<Longrightarrow> a dvd Lcm\<^sub>f\<^sub>i\<^sub>n A"
wenzelm@65552
  1134
  by (induct A rule: infinite_finite_induct)
haftmann@64850
  1135
    (auto intro: dvd_trans)
haftmann@64850
  1136
haftmann@64850
  1137
lemma Gcd_fin_greatest:
haftmann@64850
  1138
  "a dvd Gcd\<^sub>f\<^sub>i\<^sub>n A" if "finite A" and "\<And>b. b \<in> A \<Longrightarrow> a dvd b"
haftmann@64850
  1139
  using that by (induct A) simp_all
haftmann@64850
  1140
haftmann@64850
  1141
lemma Lcm_fin_least:
haftmann@64850
  1142
  "Lcm\<^sub>f\<^sub>i\<^sub>n A dvd a" if "finite A" and "\<And>b. b \<in> A \<Longrightarrow> b dvd a"
haftmann@64850
  1143
  using that by (induct A) simp_all
haftmann@64850
  1144
haftmann@64850
  1145
lemma gcd_list_greatest:
haftmann@64850
  1146
  "a dvd gcd_list bs" if "\<And>b. b \<in> set bs \<Longrightarrow> a dvd b"
haftmann@64850
  1147
  by (rule Gcd_fin_greatest) (simp_all add: that)
haftmann@64850
  1148
haftmann@64850
  1149
lemma lcm_list_least:
haftmann@64850
  1150
  "lcm_list bs dvd a" if "\<And>b. b \<in> set bs \<Longrightarrow> b dvd a"
haftmann@64850
  1151
  by (rule Lcm_fin_least) (simp_all add: that)
haftmann@64850
  1152
haftmann@64850
  1153
lemma dvd_Gcd_fin_iff:
haftmann@64850
  1154
  "b dvd Gcd\<^sub>f\<^sub>i\<^sub>n A \<longleftrightarrow> (\<forall>a\<in>A. b dvd a)" if "finite A"
haftmann@64850
  1155
  using that by (auto intro: Gcd_fin_greatest Gcd_fin_dvd dvd_trans [of b "Gcd\<^sub>f\<^sub>i\<^sub>n A"])
haftmann@64850
  1156
haftmann@64850
  1157
lemma dvd_gcd_list_iff:
haftmann@64850
  1158
  "b dvd gcd_list xs \<longleftrightarrow> (\<forall>a\<in>set xs. b dvd a)"
haftmann@64850
  1159
  by (simp add: dvd_Gcd_fin_iff)
wenzelm@65552
  1160
haftmann@64850
  1161
lemma Lcm_fin_dvd_iff:
haftmann@64850
  1162
  "Lcm\<^sub>f\<^sub>i\<^sub>n A dvd b  \<longleftrightarrow> (\<forall>a\<in>A. a dvd b)" if "finite A"
haftmann@64850
  1163
  using that by (auto intro: Lcm_fin_least dvd_Lcm_fin dvd_trans [of _ "Lcm\<^sub>f\<^sub>i\<^sub>n A" b])
haftmann@64850
  1164
haftmann@64850
  1165
lemma lcm_list_dvd_iff:
haftmann@64850
  1166
  "lcm_list xs dvd b  \<longleftrightarrow> (\<forall>a\<in>set xs. a dvd b)"
haftmann@64850
  1167
  by (simp add: Lcm_fin_dvd_iff)
haftmann@64850
  1168
haftmann@64850
  1169
lemma Gcd_fin_mult:
haftmann@64850
  1170
  "Gcd\<^sub>f\<^sub>i\<^sub>n (image (times b) A) = normalize b * Gcd\<^sub>f\<^sub>i\<^sub>n A" if "finite A"
haftmann@64850
  1171
using that proof induct
haftmann@64850
  1172
  case empty
haftmann@64850
  1173
  then show ?case
haftmann@64850
  1174
    by simp
haftmann@64850
  1175
next
haftmann@64850
  1176
  case (insert a A)
haftmann@64850
  1177
  have "gcd (b * a) (b * Gcd\<^sub>f\<^sub>i\<^sub>n A) = gcd (b * a) (normalize (b * Gcd\<^sub>f\<^sub>i\<^sub>n A))"
haftmann@64850
  1178
    by simp
haftmann@64850
  1179
  also have "\<dots> = gcd (b * a) (normalize b * Gcd\<^sub>f\<^sub>i\<^sub>n A)"
haftmann@64850
  1180
    by (simp add: normalize_mult)
haftmann@64850
  1181
  finally show ?case
haftmann@64850
  1182
    using insert by (simp add: gcd_mult_distrib')
haftmann@64850
  1183
qed
haftmann@64850
  1184
haftmann@64850
  1185
lemma Lcm_fin_mult:
haftmann@64850
  1186
  "Lcm\<^sub>f\<^sub>i\<^sub>n (image (times b) A) = normalize b * Lcm\<^sub>f\<^sub>i\<^sub>n A" if "A \<noteq> {}"
haftmann@64850
  1187
proof (cases "b = 0")
haftmann@64850
  1188
  case True
haftmann@64850
  1189
  moreover from that have "times 0 ` A = {0}"
haftmann@64850
  1190
    by auto
haftmann@64850
  1191
  ultimately show ?thesis
haftmann@64850
  1192
    by simp
haftmann@64850
  1193
next
haftmann@64850
  1194
  case False
haftmann@64850
  1195
  show ?thesis proof (cases "finite A")
haftmann@64850
  1196
    case False
haftmann@66936
  1197
    moreover have "inj_on (times b) A"
haftmann@66936
  1198
      using \<open>b \<noteq> 0\<close> by (rule inj_on_mult)
haftmann@64850
  1199
    ultimately have "infinite (times b ` A)"
haftmann@64850
  1200
      by (simp add: finite_image_iff)
haftmann@64850
  1201
    with False show ?thesis
haftmann@64850
  1202
      by simp
haftmann@64850
  1203
  next
haftmann@64850
  1204
    case True
haftmann@64850
  1205
    then show ?thesis using that proof (induct A rule: finite_ne_induct)
haftmann@64850
  1206
      case (singleton a)
haftmann@64850
  1207
      then show ?case
haftmann@64850
  1208
        by (simp add: normalize_mult)
haftmann@64850
  1209
    next
haftmann@64850
  1210
      case (insert a A)
haftmann@64850
  1211
      have "lcm (b * a) (b * Lcm\<^sub>f\<^sub>i\<^sub>n A) = lcm (b * a) (normalize (b * Lcm\<^sub>f\<^sub>i\<^sub>n A))"
haftmann@64850
  1212
        by simp
haftmann@64850
  1213
      also have "\<dots> = lcm (b * a) (normalize b * Lcm\<^sub>f\<^sub>i\<^sub>n A)"
haftmann@64850
  1214
        by (simp add: normalize_mult)
haftmann@64850
  1215
      finally show ?case
haftmann@64850
  1216
        using insert by (simp add: lcm_mult_distrib')
haftmann@64850
  1217
    qed
haftmann@64850
  1218
  qed
haftmann@64850
  1219
qed
haftmann@64850
  1220
haftmann@65811
  1221
lemma unit_factor_Gcd_fin:
haftmann@65811
  1222
  "unit_factor (Gcd\<^sub>f\<^sub>i\<^sub>n A) = of_bool (Gcd\<^sub>f\<^sub>i\<^sub>n A \<noteq> 0)"
haftmann@65811
  1223
  by (rule normalize_idem_imp_unit_factor_eq) simp
haftmann@65811
  1224
haftmann@65811
  1225
lemma unit_factor_Lcm_fin:
haftmann@65811
  1226
  "unit_factor (Lcm\<^sub>f\<^sub>i\<^sub>n A) = of_bool (Lcm\<^sub>f\<^sub>i\<^sub>n A \<noteq> 0)"
haftmann@65811
  1227
  by (rule normalize_idem_imp_unit_factor_eq) simp
haftmann@65811
  1228
haftmann@65811
  1229
lemma is_unit_Gcd_fin_iff [simp]:
haftmann@65811
  1230
  "is_unit (Gcd\<^sub>f\<^sub>i\<^sub>n A) \<longleftrightarrow> Gcd\<^sub>f\<^sub>i\<^sub>n A = 1"
haftmann@65811
  1231
  by (rule normalize_idem_imp_is_unit_iff) simp
haftmann@65811
  1232
haftmann@65811
  1233
lemma is_unit_Lcm_fin_iff [simp]:
haftmann@65811
  1234
  "is_unit (Lcm\<^sub>f\<^sub>i\<^sub>n A) \<longleftrightarrow> Lcm\<^sub>f\<^sub>i\<^sub>n A = 1"
haftmann@65811
  1235
  by (rule normalize_idem_imp_is_unit_iff) simp
haftmann@65811
  1236
 
haftmann@65811
  1237
lemma Gcd_fin_0_iff:
haftmann@65811
  1238
  "Gcd\<^sub>f\<^sub>i\<^sub>n A = 0 \<longleftrightarrow> A \<subseteq> {0} \<and> finite A"
haftmann@65811
  1239
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@65811
  1240
haftmann@65811
  1241
lemma Lcm_fin_0_iff:
haftmann@65811
  1242
  "Lcm\<^sub>f\<^sub>i\<^sub>n A = 0 \<longleftrightarrow> 0 \<in> A" if "finite A"
haftmann@65811
  1243
  using that by (induct A) (auto simp add: lcm_eq_0_iff)
haftmann@65811
  1244
haftmann@65811
  1245
lemma Lcm_fin_1_iff:
haftmann@65811
  1246
  "Lcm\<^sub>f\<^sub>i\<^sub>n A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a) \<and> finite A"
haftmann@65811
  1247
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@65811
  1248
haftmann@64850
  1249
end
haftmann@64850
  1250
haftmann@64850
  1251
context semiring_Gcd
haftmann@64850
  1252
begin
haftmann@64850
  1253
haftmann@64850
  1254
lemma Gcd_fin_eq_Gcd [simp]:
haftmann@64850
  1255
  "Gcd\<^sub>f\<^sub>i\<^sub>n A = Gcd A" if "finite A" for A :: "'a set"
haftmann@64850
  1256
  using that by induct simp_all
haftmann@64850
  1257
haftmann@64850
  1258
lemma Gcd_set_eq_fold [code_unfold]:
haftmann@64850
  1259
  "Gcd (set xs) = fold gcd xs 0"
haftmann@64850
  1260
  by (simp add: Gcd_fin.set_eq_fold [symmetric])
haftmann@64850
  1261
haftmann@64850
  1262
lemma Lcm_fin_eq_Lcm [simp]:
haftmann@64850
  1263
  "Lcm\<^sub>f\<^sub>i\<^sub>n A = Lcm A" if "finite A" for A :: "'a set"
haftmann@64850
  1264
  using that by induct simp_all
haftmann@64850
  1265
haftmann@64850
  1266
lemma Lcm_set_eq_fold [code_unfold]:
haftmann@64850
  1267
  "Lcm (set xs) = fold lcm xs 1"
haftmann@64850
  1268
  by (simp add: Lcm_fin.set_eq_fold [symmetric])
haftmann@64850
  1269
haftmann@64850
  1270
end
wenzelm@63489
  1271
haftmann@67051
  1272
haftmann@67051
  1273
subsection \<open>Coprimality\<close>
haftmann@67051
  1274
haftmann@67051
  1275
context semiring_gcd
haftmann@67051
  1276
begin
haftmann@67051
  1277
haftmann@67051
  1278
lemma coprime_imp_gcd_eq_1 [simp]:
haftmann@67051
  1279
  "gcd a b = 1" if "coprime a b"
haftmann@67051
  1280
proof -
haftmann@67051
  1281
  define t r s where "t = gcd a b" and "r = a div t" and "s = b div t"
haftmann@67051
  1282
  then have "a = t * r" and "b = t * s"
haftmann@67051
  1283
    by simp_all
haftmann@67051
  1284
  with that have "coprime (t * r) (t * s)"
haftmann@67051
  1285
    by simp
haftmann@67051
  1286
  then show ?thesis
haftmann@67051
  1287
    by (simp add: t_def)
haftmann@67051
  1288
qed
haftmann@67051
  1289
haftmann@68270
  1290
lemma gcd_eq_1_imp_coprime [dest!]:
haftmann@67051
  1291
  "coprime a b" if "gcd a b = 1"
haftmann@67051
  1292
proof (rule coprimeI)
haftmann@67051
  1293
  fix c
haftmann@67051
  1294
  assume "c dvd a" and "c dvd b"
haftmann@67051
  1295
  then have "c dvd gcd a b"
haftmann@67051
  1296
    by (rule gcd_greatest)
haftmann@67051
  1297
  with that show "is_unit c"
haftmann@67051
  1298
    by simp
haftmann@67051
  1299
qed
haftmann@67051
  1300
haftmann@67051
  1301
lemma coprime_iff_gcd_eq_1 [presburger, code]:
haftmann@67051
  1302
  "coprime a b \<longleftrightarrow> gcd a b = 1"
haftmann@67051
  1303
  by rule (simp_all add: gcd_eq_1_imp_coprime)
haftmann@67051
  1304
haftmann@67051
  1305
lemma is_unit_gcd [simp]:
haftmann@67051
  1306
  "is_unit (gcd a b) \<longleftrightarrow> coprime a b"
haftmann@67051
  1307
  by (simp add: coprime_iff_gcd_eq_1)
haftmann@67051
  1308
haftmann@67051
  1309
lemma coprime_add_one_left [simp]: "coprime (a + 1) a"
haftmann@67051
  1310
  by (simp add: gcd_eq_1_imp_coprime ac_simps)
haftmann@67051
  1311
haftmann@67051
  1312
lemma coprime_add_one_right [simp]: "coprime a (a + 1)"
haftmann@67051
  1313
  using coprime_add_one_left [of a] by (simp add: ac_simps)
haftmann@67051
  1314
haftmann@67051
  1315
lemma coprime_mult_left_iff [simp]:
haftmann@67051
  1316
  "coprime (a * b) c \<longleftrightarrow> coprime a c \<and> coprime b c"
haftmann@67051
  1317
proof
haftmann@67051
  1318
  assume "coprime (a * b) c"
haftmann@67051
  1319
  with coprime_common_divisor [of "a * b" c]
haftmann@67051
  1320
  have *: "is_unit d" if "d dvd a * b" and "d dvd c" for d
haftmann@67051
  1321
    using that by blast
haftmann@67051
  1322
  have "coprime a c"
haftmann@67051
  1323
    by (rule coprimeI, rule *) simp_all
haftmann@67051
  1324
  moreover have "coprime b c"
haftmann@67051
  1325
    by (rule coprimeI, rule *) simp_all
haftmann@67051
  1326
  ultimately show "coprime a c \<and> coprime b c" ..
haftmann@67051
  1327
next
haftmann@67051
  1328
  assume "coprime a c \<and> coprime b c"
haftmann@67051
  1329
  then have "coprime a c" "coprime b c"
haftmann@67051
  1330
    by simp_all
haftmann@67051
  1331
  show "coprime (a * b) c"
haftmann@67051
  1332
  proof (rule coprimeI)
haftmann@67051
  1333
    fix d
haftmann@67051
  1334
    assume "d dvd a * b"
haftmann@67051
  1335
    then obtain r s where d: "d = r * s" "r dvd a" "s dvd b"
haftmann@67051
  1336
      by (rule dvd_productE)
haftmann@67051
  1337
    assume "d dvd c"
haftmann@67051
  1338
    with d have "r * s dvd c"
haftmann@67051
  1339
      by simp
haftmann@67051
  1340
    then have "r dvd c" "s dvd c"
haftmann@67051
  1341
      by (auto intro: dvd_mult_left dvd_mult_right)
haftmann@67051
  1342
    from \<open>coprime a c\<close> \<open>r dvd a\<close> \<open>r dvd c\<close>
haftmann@67051
  1343
    have "is_unit r"
haftmann@67051
  1344
      by (rule coprime_common_divisor)
haftmann@67051
  1345
    moreover from \<open>coprime b c\<close> \<open>s dvd b\<close> \<open>s dvd c\<close>
haftmann@67051
  1346
    have "is_unit s"
haftmann@67051
  1347
      by (rule coprime_common_divisor)
haftmann@67051
  1348
    ultimately show "is_unit d"
haftmann@67051
  1349
      by (simp add: d is_unit_mult_iff)
haftmann@67051
  1350
  qed
haftmann@67051
  1351
qed
haftmann@67051
  1352
haftmann@67051
  1353
lemma coprime_mult_right_iff [simp]:
haftmann@67051
  1354
  "coprime c (a * b) \<longleftrightarrow> coprime c a \<and> coprime c b"
haftmann@67051
  1355
  using coprime_mult_left_iff [of a b c] by (simp add: ac_simps)
haftmann@67051
  1356
haftmann@67051
  1357
lemma coprime_power_left_iff [simp]:
haftmann@67051
  1358
  "coprime (a ^ n) b \<longleftrightarrow> coprime a b \<or> n = 0"
haftmann@67051
  1359
proof (cases "n = 0")
haftmann@67051
  1360
  case True
haftmann@67051
  1361
  then show ?thesis
haftmann@67051
  1362
    by simp
haftmann@67051
  1363
next
haftmann@67051
  1364
  case False
haftmann@67051
  1365
  then have "n > 0"
haftmann@67051
  1366
    by simp
haftmann@67051
  1367
  then show ?thesis
haftmann@67051
  1368
    by (induction n rule: nat_induct_non_zero) simp_all
haftmann@67051
  1369
qed
haftmann@67051
  1370
haftmann@67051
  1371
lemma coprime_power_right_iff [simp]:
haftmann@67051
  1372
  "coprime a (b ^ n) \<longleftrightarrow> coprime a b \<or> n = 0"
haftmann@67051
  1373
  using coprime_power_left_iff [of b n a] by (simp add: ac_simps)
haftmann@67051
  1374
haftmann@67051
  1375
lemma prod_coprime_left:
haftmann@67051
  1376
  "coprime (\<Prod>i\<in>A. f i) a" if "\<And>i. i \<in> A \<Longrightarrow> coprime (f i) a"
haftmann@67051
  1377
  using that by (induct A rule: infinite_finite_induct) simp_all
haftmann@67051
  1378
haftmann@67051
  1379
lemma prod_coprime_right:
haftmann@67051
  1380
  "coprime a (\<Prod>i\<in>A. f i)" if "\<And>i. i \<in> A \<Longrightarrow> coprime a (f i)"
haftmann@67051
  1381
  using that prod_coprime_left [of A f a] by (simp add: ac_simps)
haftmann@67051
  1382
haftmann@67051
  1383
lemma prod_list_coprime_left:
haftmann@67051
  1384
  "coprime (prod_list xs) a" if "\<And>x. x \<in> set xs \<Longrightarrow> coprime x a"
haftmann@67051
  1385
  using that by (induct xs) simp_all
haftmann@67051
  1386
haftmann@67051
  1387
lemma prod_list_coprime_right:
haftmann@67051
  1388
  "coprime a (prod_list xs)" if "\<And>x. x \<in> set xs \<Longrightarrow> coprime a x"
haftmann@67051
  1389
  using that prod_list_coprime_left [of xs a] by (simp add: ac_simps)
haftmann@67051
  1390
haftmann@67051
  1391
lemma coprime_dvd_mult_left_iff:
haftmann@67051
  1392
  "a dvd b * c \<longleftrightarrow> a dvd b" if "coprime a c"
haftmann@67051
  1393
proof
haftmann@67051
  1394
  assume "a dvd b"
haftmann@67051
  1395
  then show "a dvd b * c"
haftmann@67051
  1396
    by simp
haftmann@67051
  1397
next
haftmann@67051
  1398
  assume "a dvd b * c"
haftmann@67051
  1399
  show "a dvd b"
haftmann@67051
  1400
  proof (cases "b = 0")
haftmann@67051
  1401
    case True
haftmann@67051
  1402
    then show ?thesis
haftmann@67051
  1403
      by simp
haftmann@67051
  1404
  next
haftmann@67051
  1405
    case False
haftmann@67051
  1406
    then have unit: "is_unit (unit_factor b)"
haftmann@67051
  1407
      by simp
haftmann@67051
  1408
    from \<open>coprime a c\<close> mult_gcd_left [of b a c]
haftmann@67051
  1409
    have "gcd (b * a) (b * c) * unit_factor b = b"
haftmann@67051
  1410
      by (simp add: ac_simps)
haftmann@67051
  1411
    moreover from \<open>a dvd b * c\<close>
haftmann@67051
  1412
    have "a dvd gcd (b * a) (b * c) * unit_factor b"
haftmann@67051
  1413
      by (simp add: dvd_mult_unit_iff unit)
haftmann@67051
  1414
    ultimately show ?thesis
haftmann@67051
  1415
      by simp
haftmann@67051
  1416
  qed
haftmann@67051
  1417
qed
haftmann@67051
  1418
haftmann@67051
  1419
lemma coprime_dvd_mult_right_iff:
haftmann@67051
  1420
  "a dvd c * b \<longleftrightarrow> a dvd b" if "coprime a c"
haftmann@67051
  1421
  using that coprime_dvd_mult_left_iff [of a c b] by (simp add: ac_simps)
haftmann@67051
  1422
haftmann@67051
  1423
lemma divides_mult:
haftmann@67051
  1424
  "a * b dvd c" if "a dvd c" and "b dvd c" and "coprime a b"
haftmann@67051
  1425
proof -
haftmann@67051
  1426
  from \<open>b dvd c\<close> obtain b' where "c = b * b'" ..
haftmann@67051
  1427
  with \<open>a dvd c\<close> have "a dvd b' * b"
haftmann@67051
  1428
    by (simp add: ac_simps)
haftmann@67051
  1429
  with \<open>coprime a b\<close> have "a dvd b'"
haftmann@67051
  1430
    by (simp add: coprime_dvd_mult_left_iff)
haftmann@67051
  1431
  then obtain a' where "b' = a * a'" ..
haftmann@67051
  1432
  with \<open>c = b * b'\<close> have "c = (a * b) * a'"
haftmann@67051
  1433
    by (simp add: ac_simps)
haftmann@67051
  1434
  then show ?thesis ..
haftmann@67051
  1435
qed
haftmann@67051
  1436
haftmann@67051
  1437
lemma div_gcd_coprime:
haftmann@67051
  1438
  assumes "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@67051
  1439
  shows "coprime (a div gcd a b) (b div gcd a b)"
haftmann@67051
  1440
proof -
haftmann@67051
  1441
  let ?g = "gcd a b"
haftmann@67051
  1442
  let ?a' = "a div ?g"
haftmann@67051
  1443
  let ?b' = "b div ?g"
haftmann@67051
  1444
  let ?g' = "gcd ?a' ?b'"
haftmann@67051
  1445
  have dvdg: "?g dvd a" "?g dvd b"
haftmann@67051
  1446
    by simp_all
haftmann@67051
  1447
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'"
haftmann@67051
  1448
    by simp_all
haftmann@67051
  1449
  from dvdg dvdg' obtain ka kb ka' kb' where
haftmann@67051
  1450
    kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
haftmann@67051
  1451
    unfolding dvd_def by blast
haftmann@67051
  1452
  from this [symmetric] have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'"
haftmann@67051
  1453
    by (simp_all add: mult.assoc mult.left_commute [of "gcd a b"])
haftmann@67051
  1454
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
haftmann@67051
  1455
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)] dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
haftmann@67051
  1456
  have "?g \<noteq> 0"
haftmann@67051
  1457
    using assms by simp
haftmann@67051
  1458
  moreover from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
haftmann@67051
  1459
  ultimately show ?thesis
haftmann@67051
  1460
    using dvd_times_left_cancel_iff [of "gcd a b" _ 1]
haftmann@67051
  1461
    by simp (simp only: coprime_iff_gcd_eq_1)
haftmann@67051
  1462
qed
haftmann@67051
  1463
haftmann@67051
  1464
lemma gcd_coprime:
haftmann@67051
  1465
  assumes c: "gcd a b \<noteq> 0"
haftmann@67051
  1466
    and a: "a = a' * gcd a b"
haftmann@67051
  1467
    and b: "b = b' * gcd a b"
haftmann@67051
  1468
  shows "coprime a' b'"
haftmann@67051
  1469
proof -
haftmann@67051
  1470
  from c have "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@67051
  1471
    by simp
haftmann@67051
  1472
  with div_gcd_coprime have "coprime (a div gcd a b) (b div gcd a b)" .
haftmann@67051
  1473
  also from assms have "a div gcd a b = a'"
haftmann@67051
  1474
    using dvd_div_eq_mult local.gcd_dvd1 by blast
haftmann@67051
  1475
  also from assms have "b div gcd a b = b'"
haftmann@67051
  1476
    using dvd_div_eq_mult local.gcd_dvd1 by blast
haftmann@67051
  1477
  finally show ?thesis .
haftmann@67051
  1478
qed
haftmann@67051
  1479
haftmann@67051
  1480
lemma gcd_coprime_exists:
haftmann@67051
  1481
  assumes "gcd a b \<noteq> 0"
haftmann@67051
  1482
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
haftmann@67051
  1483
  apply (rule_tac x = "a div gcd a b" in exI)
haftmann@67051
  1484
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@67051
  1485
  using assms
haftmann@67051
  1486
  apply (auto intro: div_gcd_coprime)
haftmann@67051
  1487
  done
haftmann@67051
  1488
haftmann@67051
  1489
lemma pow_divides_pow_iff [simp]:
haftmann@67051
  1490
  "a ^ n dvd b ^ n \<longleftrightarrow> a dvd b" if "n > 0"
haftmann@67051
  1491
proof (cases "gcd a b = 0")
haftmann@67051
  1492
  case True
haftmann@67051
  1493
  then show ?thesis
haftmann@67051
  1494
    by simp
haftmann@67051
  1495
next
haftmann@67051
  1496
  case False
haftmann@67051
  1497
  show ?thesis
haftmann@67051
  1498
  proof
haftmann@67051
  1499
    let ?d = "gcd a b"
haftmann@67051
  1500
    from \<open>n > 0\<close> obtain m where m: "n = Suc m"
haftmann@67051
  1501
      by (cases n) simp_all
haftmann@67051
  1502
    from False have zn: "?d ^ n \<noteq> 0"
haftmann@67051
  1503
      by (rule power_not_zero)
haftmann@67051
  1504
    from gcd_coprime_exists [OF False]
haftmann@67051
  1505
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "coprime a' b'"
haftmann@67051
  1506
      by blast
haftmann@67051
  1507
    assume "a ^ n dvd b ^ n"
haftmann@67051
  1508
    then have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
haftmann@67051
  1509
      by (simp add: ab'(1,2)[symmetric])
haftmann@67051
  1510
    then have "?d^n * a'^n dvd ?d^n * b'^n"
haftmann@67051
  1511
      by (simp only: power_mult_distrib ac_simps)
haftmann@67051
  1512
    with zn have "a' ^ n dvd b' ^ n"
haftmann@67051
  1513
      by simp
haftmann@67051
  1514
    then have "a' dvd b' ^ n"
haftmann@67051
  1515
      using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
haftmann@67051
  1516
    then have "a' dvd b' ^ m * b'"
haftmann@67051
  1517
      by (simp add: m ac_simps)
haftmann@67051
  1518
    moreover have "coprime a' (b' ^ n)"
haftmann@67051
  1519
      using \<open>coprime a' b'\<close> by simp
haftmann@67051
  1520
    then have "a' dvd b'"
haftmann@67051
  1521
      using \<open>a' dvd b' ^ n\<close> coprime_dvd_mult_left_iff dvd_mult by blast
haftmann@67051
  1522
    then have "a' * ?d dvd b' * ?d"
haftmann@67051
  1523
      by (rule mult_dvd_mono) simp
haftmann@67051
  1524
    with ab'(1,2) show "a dvd b"
haftmann@67051
  1525
      by simp
haftmann@67051
  1526
  next
haftmann@67051
  1527
    assume "a dvd b"
haftmann@67051
  1528
    with \<open>n > 0\<close> show "a ^ n dvd b ^ n"
haftmann@67051
  1529
      by (induction rule: nat_induct_non_zero)
haftmann@67051
  1530
        (simp_all add: mult_dvd_mono)
haftmann@67051
  1531
  qed
haftmann@67051
  1532
qed
haftmann@67051
  1533
haftmann@67051
  1534
lemma coprime_crossproduct:
haftmann@67051
  1535
  fixes a b c d :: 'a
haftmann@67051
  1536
  assumes "coprime a d" and "coprime b c"
haftmann@67051
  1537
  shows "normalize a * normalize c = normalize b * normalize d \<longleftrightarrow>
haftmann@67051
  1538
    normalize a = normalize b \<and> normalize c = normalize d"
haftmann@67051
  1539
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@67051
  1540
proof
haftmann@67051
  1541
  assume ?rhs
haftmann@67051
  1542
  then show ?lhs by simp
haftmann@67051
  1543
next
haftmann@67051
  1544
  assume ?lhs
haftmann@67051
  1545
  from \<open>?lhs\<close> have "normalize a dvd normalize b * normalize d"
haftmann@67051
  1546
    by (auto intro: dvdI dest: sym)
haftmann@67051
  1547
  with \<open>coprime a d\<close> have "a dvd b"
haftmann@67051
  1548
    by (simp add: coprime_dvd_mult_left_iff normalize_mult [symmetric])
haftmann@67051
  1549
  from \<open>?lhs\<close> have "normalize b dvd normalize a * normalize c"
haftmann@67051
  1550
    by (auto intro: dvdI dest: sym)
haftmann@67051
  1551
  with \<open>coprime b c\<close> have "b dvd a"
haftmann@67051
  1552
    by (simp add: coprime_dvd_mult_left_iff normalize_mult [symmetric])
haftmann@67051
  1553
  from \<open>?lhs\<close> have "normalize c dvd normalize d * normalize b"
haftmann@67051
  1554
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@67051
  1555
  with \<open>coprime b c\<close> have "c dvd d"
haftmann@67051
  1556
    by (simp add: coprime_dvd_mult_left_iff coprime_commute normalize_mult [symmetric])
haftmann@67051
  1557
  from \<open>?lhs\<close> have "normalize d dvd normalize c * normalize a"
haftmann@67051
  1558
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@67051
  1559
  with \<open>coprime a d\<close> have "d dvd c"
haftmann@67051
  1560
    by (simp add: coprime_dvd_mult_left_iff coprime_commute normalize_mult [symmetric])
haftmann@67051
  1561
  from \<open>a dvd b\<close> \<open>b dvd a\<close> have "normalize a = normalize b"
haftmann@67051
  1562
    by (rule associatedI)
haftmann@67051
  1563
  moreover from \<open>c dvd d\<close> \<open>d dvd c\<close> have "normalize c = normalize d"
haftmann@67051
  1564
    by (rule associatedI)
haftmann@67051
  1565
  ultimately show ?rhs ..
haftmann@67051
  1566
qed
haftmann@67051
  1567
haftmann@67051
  1568
lemma coprime_crossproduct':
haftmann@67051
  1569
  fixes a b c d
haftmann@67051
  1570
  assumes "b \<noteq> 0"
haftmann@67051
  1571
  assumes unit_factors: "unit_factor b = unit_factor d"
haftmann@67051
  1572
  assumes coprime: "coprime a b" "coprime c d"
haftmann@67051
  1573
  shows "a * d = b * c \<longleftrightarrow> a = c \<and> b = d"
haftmann@67051
  1574
proof safe
haftmann@67051
  1575
  assume eq: "a * d = b * c"
haftmann@67051
  1576
  hence "normalize a * normalize d = normalize c * normalize b"
haftmann@67051
  1577
    by (simp only: normalize_mult [symmetric] mult_ac)
haftmann@67051
  1578
  with coprime have "normalize b = normalize d"
haftmann@67051
  1579
    by (subst (asm) coprime_crossproduct) simp_all
haftmann@67051
  1580
  from this and unit_factors show "b = d"
haftmann@67051
  1581
    by (rule normalize_unit_factor_eqI)
haftmann@67051
  1582
  from eq have "a * d = c * d" by (simp only: \<open>b = d\<close> mult_ac)
haftmann@67051
  1583
  with \<open>b \<noteq> 0\<close> \<open>b = d\<close> show "a = c" by simp
haftmann@67051
  1584
qed (simp_all add: mult_ac)
haftmann@67051
  1585
haftmann@67051
  1586
lemma gcd_mult_left_left_cancel:
haftmann@67051
  1587
  "gcd (c * a) b = gcd a b" if "coprime b c"
haftmann@67051
  1588
proof -
haftmann@67051
  1589
  have "coprime (gcd b (a * c)) c"
haftmann@67051
  1590
    by (rule coprimeI) (auto intro: that coprime_common_divisor)
haftmann@67051
  1591
  then have "gcd b (a * c) dvd a"
haftmann@67051
  1592
    using coprime_dvd_mult_left_iff [of "gcd b (a * c)" c a]
haftmann@67051
  1593
    by simp
haftmann@67051
  1594
  then show ?thesis
haftmann@67051
  1595
    by (auto intro: associated_eqI simp add: ac_simps)
haftmann@67051
  1596
qed
haftmann@67051
  1597
haftmann@67051
  1598
lemma gcd_mult_left_right_cancel:
haftmann@67051
  1599
  "gcd (a * c) b = gcd a b" if "coprime b c"
haftmann@67051
  1600
  using that gcd_mult_left_left_cancel [of b c a]
haftmann@67051
  1601
  by (simp add: ac_simps)
haftmann@67051
  1602
haftmann@67051
  1603
lemma gcd_mult_right_left_cancel:
haftmann@67051
  1604
  "gcd a (c * b) = gcd a b" if "coprime a c"
haftmann@67051
  1605
  using that gcd_mult_left_left_cancel [of a c b]
haftmann@67051
  1606
  by (simp add: ac_simps)
haftmann@67051
  1607
haftmann@67051
  1608
lemma gcd_mult_right_right_cancel:
haftmann@67051
  1609
  "gcd a (b * c) = gcd a b" if "coprime a c"
haftmann@67051
  1610
  using that gcd_mult_right_left_cancel [of a c b]
haftmann@67051
  1611
  by (simp add: ac_simps)
haftmann@67051
  1612
haftmann@67051
  1613
lemma gcd_exp [simp]:
haftmann@67051
  1614
  "gcd (a ^ n) (b ^ n) = gcd a b ^ n"
haftmann@67051
  1615
proof (cases "a = 0 \<and> b = 0 \<or> n = 0")
haftmann@67051
  1616
  case True
haftmann@67051
  1617
  then show ?thesis
haftmann@67051
  1618
    by (cases n) simp_all
haftmann@67051
  1619
next
haftmann@67051
  1620
  case False
haftmann@67051
  1621
  then have "coprime (a div gcd a b) (b div gcd a b)" and "n > 0"
haftmann@67051
  1622
    by (auto intro: div_gcd_coprime)
haftmann@67051
  1623
  then have "coprime ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
haftmann@67051
  1624
    by simp
haftmann@67051
  1625
  then have "1 = gcd ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
haftmann@67051
  1626
    by simp
haftmann@67051
  1627
  then have "gcd a b ^ n = gcd a b ^ n * \<dots>"
haftmann@67051
  1628
    by simp
haftmann@67051
  1629
  also note gcd_mult_distrib
haftmann@67051
  1630
  also have "unit_factor (gcd a b ^ n) = 1"
haftmann@67051
  1631
    using False by (auto simp add: unit_factor_power unit_factor_gcd)
haftmann@67051
  1632
  also have "(gcd a b) ^ n * (a div gcd a b) ^ n = a ^ n"
haftmann@67051
  1633
    by (simp add: ac_simps div_power dvd_power_same)
haftmann@67051
  1634
  also have "(gcd a b) ^ n * (b div gcd a b) ^ n = b ^ n"
haftmann@67051
  1635
    by (simp add: ac_simps div_power dvd_power_same)
haftmann@67051
  1636
  finally show ?thesis by simp
haftmann@67051
  1637
qed
haftmann@67051
  1638
haftmann@67051
  1639
lemma division_decomp:
haftmann@67051
  1640
  assumes "a dvd b * c"
haftmann@67051
  1641
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
haftmann@67051
  1642
proof (cases "gcd a b = 0")
haftmann@67051
  1643
  case True
haftmann@67051
  1644
  then have "a = 0 \<and> b = 0"
haftmann@67051
  1645
    by simp
haftmann@67051
  1646
  then have "a = 0 * c \<and> 0 dvd b \<and> c dvd c"
haftmann@67051
  1647
    by simp
haftmann@67051
  1648
  then show ?thesis by blast
haftmann@67051
  1649
next
haftmann@67051
  1650
  case False
haftmann@67051
  1651
  let ?d = "gcd a b"
haftmann@67051
  1652
  from gcd_coprime_exists [OF False]
haftmann@67051
  1653
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "coprime a' b'"
haftmann@67051
  1654
    by blast
haftmann@67051
  1655
  from ab'(1) have "a' dvd a" ..
haftmann@67051
  1656
  with assms have "a' dvd b * c"
haftmann@67051
  1657
    using dvd_trans [of a' a "b * c"] by simp
haftmann@67051
  1658
  from assms ab'(1,2) have "a' * ?d dvd (b' * ?d) * c"
haftmann@67051
  1659
    by simp
haftmann@67051
  1660
  then have "?d * a' dvd ?d * (b' * c)"
haftmann@67051
  1661
    by (simp add: mult_ac)
haftmann@67051
  1662
  with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c"
haftmann@67051
  1663
    by simp
haftmann@67051
  1664
  then have "a' dvd c"
haftmann@67051
  1665
    using \<open>coprime a' b'\<close> by (simp add: coprime_dvd_mult_right_iff)
haftmann@67051
  1666
  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c"
haftmann@67051
  1667
    by (simp add: ac_simps)
haftmann@67051
  1668
  then show ?thesis by blast
haftmann@67051
  1669
qed
haftmann@67051
  1670
haftmann@67051
  1671
lemma lcm_coprime: "coprime a b \<Longrightarrow> lcm a b = normalize (a * b)"
haftmann@67051
  1672
  by (subst lcm_gcd) simp
haftmann@67051
  1673
haftmann@67051
  1674
end
haftmann@67051
  1675
haftmann@67051
  1676
context ring_gcd
haftmann@67051
  1677
begin
haftmann@67051
  1678
haftmann@67051
  1679
lemma coprime_minus_left_iff [simp]:
haftmann@67051
  1680
  "coprime (- a) b \<longleftrightarrow> coprime a b"
haftmann@67051
  1681
  by (rule; rule coprimeI) (auto intro: coprime_common_divisor)
haftmann@67051
  1682
haftmann@67051
  1683
lemma coprime_minus_right_iff [simp]:
haftmann@67051
  1684
  "coprime a (- b) \<longleftrightarrow> coprime a b"
haftmann@67051
  1685
  using coprime_minus_left_iff [of b a] by (simp add: ac_simps)
haftmann@67051
  1686
haftmann@67051
  1687
lemma coprime_diff_one_left [simp]: "coprime (a - 1) a"
haftmann@67051
  1688
  using coprime_add_one_right [of "a - 1"] by simp
haftmann@67051
  1689
haftmann@67051
  1690
lemma coprime_doff_one_right [simp]: "coprime a (a - 1)"
haftmann@67051
  1691
  using coprime_diff_one_left [of a] by (simp add: ac_simps)
haftmann@67051
  1692
haftmann@67051
  1693
end
haftmann@67051
  1694
haftmann@67051
  1695
context semiring_Gcd
haftmann@67051
  1696
begin
haftmann@67051
  1697
haftmann@67051
  1698
lemma Lcm_coprime:
haftmann@67051
  1699
  assumes "finite A"
haftmann@67051
  1700
    and "A \<noteq> {}"
haftmann@67051
  1701
    and "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> coprime a b"
haftmann@67051
  1702
  shows "Lcm A = normalize (\<Prod>A)"
haftmann@67051
  1703
  using assms
haftmann@67051
  1704
proof (induct rule: finite_ne_induct)
haftmann@67051
  1705
  case singleton
haftmann@67051
  1706
  then show ?case by simp
haftmann@67051
  1707
next
haftmann@67051
  1708
  case (insert a A)
haftmann@67051
  1709
  have "Lcm (insert a A) = lcm a (Lcm A)"
haftmann@67051
  1710
    by simp
haftmann@67051
  1711
  also from insert have "Lcm A = normalize (\<Prod>A)"
haftmann@67051
  1712
    by blast
haftmann@67051
  1713
  also have "lcm a \<dots> = lcm a (\<Prod>A)"
haftmann@67051
  1714
    by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
haftmann@67051
  1715
  also from insert have "coprime a (\<Prod>A)"
haftmann@67051
  1716
    by (subst coprime_commute, intro prod_coprime_left) auto
haftmann@67051
  1717
  with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"
haftmann@67051
  1718
    by (simp add: lcm_coprime)
haftmann@67051
  1719
  finally show ?case .
haftmann@67051
  1720
qed
haftmann@67051
  1721
haftmann@67051
  1722
lemma Lcm_coprime':
haftmann@67051
  1723
  "card A \<noteq> 0 \<Longrightarrow>
haftmann@67051
  1724
    (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> coprime a b) \<Longrightarrow>
haftmann@67051
  1725
    Lcm A = normalize (\<Prod>A)"
haftmann@67051
  1726
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
haftmann@67051
  1727
haftmann@67051
  1728
end
haftmann@67051
  1729
haftmann@67051
  1730
haftmann@62345
  1731
subsection \<open>GCD and LCM on @{typ nat} and @{typ int}\<close>
haftmann@59008
  1732
huffman@31706
  1733
instantiation nat :: gcd
huffman@31706
  1734
begin
wenzelm@21256
  1735
haftmann@62345
  1736
fun gcd_nat  :: "nat \<Rightarrow> nat \<Rightarrow> nat"
wenzelm@63489
  1737
  where "gcd_nat x y = (if y = 0 then x else gcd y (x mod y))"
huffman@31706
  1738
haftmann@62345
  1739
definition lcm_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
wenzelm@63489
  1740
  where "lcm_nat x y = x * y div (gcd x y)"
wenzelm@63489
  1741
wenzelm@63489
  1742
instance ..
huffman@31706
  1743
huffman@31706
  1744
end
huffman@31706
  1745
huffman@31706
  1746
instantiation int :: gcd
huffman@31706
  1747
begin
wenzelm@21256
  1748
haftmann@62345
  1749
definition gcd_int  :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
  1750
  where "gcd_int x y = int (gcd (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
  1751
haftmann@62345
  1752
definition lcm_int :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
  1753
  where "lcm_int x y = int (lcm (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
  1754
wenzelm@61944
  1755
instance ..
huffman@31706
  1756
huffman@31706
  1757
end
haftmann@23687
  1758
haftmann@67118
  1759
lemma gcd_int_int_eq [simp]:
haftmann@67118
  1760
  "gcd (int m) (int n) = int (gcd m n)"
haftmann@67118
  1761
  by (simp add: gcd_int_def)
haftmann@67118
  1762
haftmann@67118
  1763
lemma gcd_nat_abs_left_eq [simp]:
haftmann@67118
  1764
  "gcd (nat \<bar>k\<bar>) n = nat (gcd k (int n))"
haftmann@67118
  1765
  by (simp add: gcd_int_def)
haftmann@67118
  1766
haftmann@67118
  1767
lemma gcd_nat_abs_right_eq [simp]:
haftmann@67118
  1768
  "gcd n (nat \<bar>k\<bar>) = nat (gcd (int n) k)"
haftmann@67118
  1769
  by (simp add: gcd_int_def)
haftmann@67118
  1770
haftmann@67118
  1771
lemma abs_gcd_int [simp]:
haftmann@67118
  1772
  "\<bar>gcd x y\<bar> = gcd x y"
haftmann@67118
  1773
  for x y :: int
haftmann@67118
  1774
  by (simp only: gcd_int_def)
haftmann@67118
  1775
haftmann@67118
  1776
lemma gcd_abs1_int [simp]:
haftmann@67118
  1777
  "gcd \<bar>x\<bar> y = gcd x y"
haftmann@67118
  1778
  for x y :: int
haftmann@67118
  1779
  by (simp only: gcd_int_def) simp
haftmann@67118
  1780
haftmann@67118
  1781
lemma gcd_abs2_int [simp]:
haftmann@67118
  1782
  "gcd x \<bar>y\<bar> = gcd x y"
haftmann@67118
  1783
  for x y :: int
haftmann@67118
  1784
  by (simp only: gcd_int_def) simp
haftmann@67118
  1785
haftmann@67118
  1786
lemma lcm_int_int_eq [simp]:
haftmann@67118
  1787
  "lcm (int m) (int n) = int (lcm m n)"
haftmann@67118
  1788
  by (simp add: lcm_int_def)
haftmann@67118
  1789
haftmann@67118
  1790
lemma lcm_nat_abs_left_eq [simp]:
haftmann@67118
  1791
  "lcm (nat \<bar>k\<bar>) n = nat (lcm k (int n))"
haftmann@67118
  1792
  by (simp add: lcm_int_def)
haftmann@67118
  1793
haftmann@67118
  1794
lemma lcm_nat_abs_right_eq [simp]:
haftmann@67118
  1795
  "lcm n (nat \<bar>k\<bar>) = nat (lcm (int n) k)"
haftmann@67118
  1796
  by (simp add: lcm_int_def)
haftmann@67118
  1797
haftmann@67118
  1798
lemma lcm_abs1_int [simp]:
haftmann@67118
  1799
  "lcm \<bar>x\<bar> y = lcm x y"
haftmann@67118
  1800
  for x y :: int
haftmann@67118
  1801
  by (simp only: lcm_int_def) simp
haftmann@67118
  1802
haftmann@67118
  1803
lemma lcm_abs2_int [simp]:
haftmann@67118
  1804
  "lcm x \<bar>y\<bar> = lcm x y"
haftmann@67118
  1805
  for x y :: int
haftmann@67118
  1806
  by (simp only: lcm_int_def) simp
haftmann@67118
  1807
haftmann@67118
  1808
lemma abs_lcm_int [simp]: "\<bar>lcm i j\<bar> = lcm i j"
haftmann@67118
  1809
  for i j :: int
haftmann@67118
  1810
  by (simp only: lcm_int_def)
haftmann@67118
  1811
nipkow@31952
  1812
lemma gcd_nat_induct:
haftmann@23687
  1813
  fixes m n :: nat
haftmann@23687
  1814
  assumes "\<And>m. P m 0"
haftmann@23687
  1815
    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
haftmann@23687
  1816
  shows "P m n"
huffman@31706
  1817
  apply (rule gcd_nat.induct)
huffman@31706
  1818
  apply (case_tac "y = 0")
wenzelm@63489
  1819
  using assms
wenzelm@63489
  1820
   apply simp_all
wenzelm@63489
  1821
  done
wenzelm@63489
  1822
wenzelm@63489
  1823
lemma gcd_neg1_int [simp]: "gcd (- x) y = gcd x y"
wenzelm@63489
  1824
  for x y :: int
haftmann@67118
  1825
  by (simp only: gcd_int_def) simp
huffman@31706
  1826
wenzelm@63489
  1827
lemma gcd_neg2_int [simp]: "gcd x (- y) = gcd x y"
wenzelm@63489
  1828
  for x y :: int
haftmann@67118
  1829
  by (simp only: gcd_int_def) simp
huffman@31706
  1830
nipkow@31952
  1831
lemma gcd_cases_int:
wenzelm@63489
  1832
  fixes x y :: int
wenzelm@63489
  1833
  assumes "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> P (gcd x y)"
wenzelm@63489
  1834
    and "x \<ge> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> P (gcd x (- y))"
wenzelm@63489
  1835
    and "x \<le> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> P (gcd (- x) y)"
wenzelm@63489
  1836
    and "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> P (gcd (- x) (- y))"
huffman@31706
  1837
  shows "P (gcd x y)"
wenzelm@63489
  1838
  using assms by auto arith
wenzelm@21256
  1839
nipkow@31952
  1840
lemma gcd_ge_0_int [simp]: "gcd (x::int) y >= 0"
wenzelm@63489
  1841
  for x y :: int
huffman@31706
  1842
  by (simp add: gcd_int_def)
huffman@31706
  1843
wenzelm@63489
  1844
lemma lcm_neg1_int: "lcm (- x) y = lcm x y"
wenzelm@63489
  1845
  for x y :: int
haftmann@67118
  1846
  by (simp only: lcm_int_def) simp
huffman@31706
  1847
wenzelm@63489
  1848
lemma lcm_neg2_int: "lcm x (- y) = lcm x y"
wenzelm@63489
  1849
  for x y :: int
haftmann@67118
  1850
  by (simp only: lcm_int_def) simp
nipkow@31814
  1851
nipkow@31952
  1852
lemma lcm_cases_int:
wenzelm@63489
  1853
  fixes x y :: int
wenzelm@63489
  1854
  assumes "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> P (lcm x y)"
wenzelm@63489
  1855
    and "x \<ge> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> P (lcm x (- y))"
wenzelm@63489
  1856
    and "x \<le> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> P (lcm (- x) y)"
wenzelm@63489
  1857
    and "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> P (lcm (- x) (- y))"
huffman@31706
  1858
  shows "P (lcm x y)"
wenzelm@41550
  1859
  using assms by (auto simp add: lcm_neg1_int lcm_neg2_int) arith
huffman@31706
  1860
wenzelm@63489
  1861
lemma lcm_ge_0_int [simp]: "lcm x y \<ge> 0"
wenzelm@63489
  1862
  for x y :: int
haftmann@67118
  1863
  by (simp only: lcm_int_def)
huffman@31706
  1864
wenzelm@63489
  1865
lemma gcd_0_nat: "gcd x 0 = x"
wenzelm@63489
  1866
  for x :: nat
haftmann@23687
  1867
  by simp
haftmann@23687
  1868
wenzelm@63489
  1869
lemma gcd_0_int [simp]: "gcd x 0 = \<bar>x\<bar>"
wenzelm@63489
  1870
  for x :: int
wenzelm@63489
  1871
  by (auto simp: gcd_int_def)
wenzelm@63489
  1872
wenzelm@63489
  1873
lemma gcd_0_left_nat: "gcd 0 x = x"
wenzelm@63489
  1874
  for x :: nat
haftmann@23687
  1875
  by simp
haftmann@23687
  1876
wenzelm@63489
  1877
lemma gcd_0_left_int [simp]: "gcd 0 x = \<bar>x\<bar>"
wenzelm@63489
  1878
  for x :: int
haftmann@67118
  1879
  by (auto simp: gcd_int_def)
wenzelm@63489
  1880
wenzelm@63489
  1881
lemma gcd_red_nat: "gcd x y = gcd y (x mod y)"
wenzelm@63489
  1882
  for x y :: nat
wenzelm@63489
  1883
  by (cases "y = 0") auto
wenzelm@63489
  1884
wenzelm@63489
  1885
wenzelm@63489
  1886
text \<open>Weaker, but useful for the simplifier.\<close>
wenzelm@63489
  1887
wenzelm@63489
  1888
lemma gcd_non_0_nat: "y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
wenzelm@63489
  1889
  for x y :: nat
wenzelm@21263
  1890
  by simp
wenzelm@21256
  1891
wenzelm@63489
  1892
lemma gcd_1_nat [simp]: "gcd m 1 = 1"
wenzelm@63489
  1893
  for m :: nat
haftmann@60690
  1894
  by simp
huffman@31706
  1895
wenzelm@63489
  1896
lemma gcd_Suc_0 [simp]: "gcd m (Suc 0) = Suc 0"
wenzelm@63489
  1897
  for m :: nat
wenzelm@63489
  1898
  by simp
wenzelm@63489
  1899
wenzelm@63489
  1900
lemma gcd_1_int [simp]: "gcd m 1 = 1"
wenzelm@63489
  1901
  for m :: int
huffman@31706
  1902
  by (simp add: gcd_int_def)
huffman@30082
  1903
wenzelm@63489
  1904
lemma gcd_idem_nat: "gcd x x = x"
wenzelm@63489
  1905
  for x :: nat
wenzelm@63489
  1906
  by simp
wenzelm@63489
  1907
wenzelm@63489
  1908
lemma gcd_idem_int: "gcd x x = \<bar>x\<bar>"
wenzelm@63489
  1909
  for x :: int
wenzelm@63489
  1910
  by (auto simp add: gcd_int_def)
huffman@31706
  1911
huffman@31706
  1912
declare gcd_nat.simps [simp del]
wenzelm@21256
  1913
wenzelm@60758
  1914
text \<open>
wenzelm@63489
  1915
  \<^medskip> @{term "gcd m n"} divides \<open>m\<close> and \<open>n\<close>.
wenzelm@63489
  1916
  The conjunctions don't seem provable separately.
wenzelm@60758
  1917
\<close>
wenzelm@21256
  1918
haftmann@59008
  1919
instance nat :: semiring_gcd
haftmann@59008
  1920
proof
haftmann@59008
  1921
  fix m n :: nat
haftmann@59008
  1922
  show "gcd m n dvd m" and "gcd m n dvd n"
haftmann@59008
  1923
  proof (induct m n rule: gcd_nat_induct)
haftmann@59008
  1924
    fix m n :: nat
wenzelm@63489
  1925
    assume "gcd n (m mod n) dvd m mod n"
wenzelm@63489
  1926
      and "gcd n (m mod n) dvd n"
haftmann@59008
  1927
    then have "gcd n (m mod n) dvd m"
haftmann@59008
  1928
      by (rule dvd_mod_imp_dvd)
haftmann@59008
  1929
    moreover assume "0 < n"
haftmann@59008
  1930
    ultimately show "gcd m n dvd m"
haftmann@59008
  1931
      by (simp add: gcd_non_0_nat)
haftmann@59008
  1932
  qed (simp_all add: gcd_0_nat gcd_non_0_nat)
haftmann@59008
  1933
next
haftmann@59008
  1934
  fix m n k :: nat
haftmann@59008
  1935
  assume "k dvd m" and "k dvd n"
haftmann@59008
  1936
  then show "k dvd gcd m n"
haftmann@59008
  1937
    by (induct m n rule: gcd_nat_induct) (simp_all add: gcd_non_0_nat dvd_mod gcd_0_nat)
haftmann@60686
  1938
qed (simp_all add: lcm_nat_def)
lp15@59667
  1939
haftmann@59008
  1940
instance int :: ring_gcd
haftmann@67118
  1941
proof
haftmann@67118
  1942
  fix k l r :: int
haftmann@67118
  1943
  show "gcd k l dvd k" "gcd k l dvd l"
haftmann@67118
  1944
    using gcd_dvd1 [of "nat \<bar>k\<bar>" "nat \<bar>l\<bar>"]
haftmann@67118
  1945
      gcd_dvd2 [of "nat \<bar>k\<bar>" "nat \<bar>l\<bar>"]
haftmann@67118
  1946
    by simp_all
haftmann@67118
  1947
  show "lcm k l = normalize (k * l) div gcd k l"
haftmann@67118
  1948
    using lcm_gcd [of "nat \<bar>k\<bar>" "nat \<bar>l\<bar>"]
haftmann@67118
  1949
    by (simp add: nat_eq_iff of_nat_div abs_mult)
haftmann@67118
  1950
  assume "r dvd k" "r dvd l"
haftmann@67118
  1951
  then show "r dvd gcd k l"
haftmann@67118
  1952
    using gcd_greatest [of "nat \<bar>r\<bar>" "nat \<bar>k\<bar>" "nat \<bar>l\<bar>"]
haftmann@67118
  1953
    by simp
haftmann@67118
  1954
qed simp
wenzelm@63489
  1955
wenzelm@63489
  1956
lemma gcd_le1_nat [simp]: "a \<noteq> 0 \<Longrightarrow> gcd a b \<le> a"
wenzelm@63489
  1957
  for a b :: nat
wenzelm@63489
  1958
  by (rule dvd_imp_le) auto
wenzelm@63489
  1959
wenzelm@63489
  1960
lemma gcd_le2_nat [simp]: "b \<noteq> 0 \<Longrightarrow> gcd a b \<le> b"
wenzelm@63489
  1961
  for a b :: nat
wenzelm@63489
  1962
  by (rule dvd_imp_le) auto
wenzelm@63489
  1963
wenzelm@63489
  1964
lemma gcd_le1_int [simp]: "a > 0 \<Longrightarrow> gcd a b \<le> a"
wenzelm@63489
  1965
  for a b :: int
wenzelm@63489
  1966
  by (rule zdvd_imp_le) auto
wenzelm@63489
  1967
wenzelm@63489
  1968
lemma gcd_le2_int [simp]: "b > 0 \<Longrightarrow> gcd a b \<le> b"
wenzelm@63489
  1969
  for a b :: int
wenzelm@63489
  1970
  by (rule zdvd_imp_le) auto
wenzelm@63489
  1971
wenzelm@63489
  1972
lemma gcd_pos_nat [simp]: "gcd m n > 0 \<longleftrightarrow> m \<noteq> 0 \<or> n \<noteq> 0"
wenzelm@63489
  1973
  for m n :: nat
wenzelm@63489
  1974
  using gcd_eq_0_iff [of m n] by arith
wenzelm@63489
  1975
wenzelm@63489
  1976
lemma gcd_pos_int [simp]: "gcd m n > 0 \<longleftrightarrow> m \<noteq> 0 \<or> n \<noteq> 0"
wenzelm@63489
  1977
  for m n :: int
wenzelm@63489
  1978
  using gcd_eq_0_iff [of m n] gcd_ge_0_int [of m n] by arith
wenzelm@63489
  1979
wenzelm@63489
  1980
lemma gcd_unique_nat: "d dvd a \<and> d dvd b \<and> (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
wenzelm@63489
  1981
  for d a :: nat
huffman@31706
  1982
  apply auto
nipkow@33657
  1983
  apply (rule dvd_antisym)
wenzelm@63489
  1984
   apply (erule (1) gcd_greatest)
huffman@31706
  1985
  apply auto
wenzelm@63489
  1986
  done
wenzelm@63489
  1987
wenzelm@63489
  1988
lemma gcd_unique_int:
wenzelm@63489
  1989
  "d \<ge> 0 \<and> d dvd a \<and> d dvd b \<and> (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
wenzelm@63489
  1990
  for d a :: int
wenzelm@63489
  1991
  apply (cases "d = 0")
wenzelm@63489
  1992
   apply simp
wenzelm@63489
  1993
  apply (rule iffI)
wenzelm@63489
  1994
   apply (rule zdvd_antisym_nonneg)
wenzelm@63489
  1995
      apply (auto intro: gcd_greatest)
wenzelm@63489
  1996
  done
huffman@30082
  1997
haftmann@61913
  1998
interpretation gcd_nat:
haftmann@62344
  1999
  semilattice_neutr_order gcd "0::nat" Rings.dvd "\<lambda>m n. m dvd n \<and> m \<noteq> n"
haftmann@62344
  2000
  by standard (auto simp add: gcd_unique_nat [symmetric] intro: dvd_antisym dvd_trans)
nipkow@31798
  2001
wenzelm@63489
  2002
lemma gcd_proj1_if_dvd_int [simp]: "x dvd y \<Longrightarrow> gcd x y = \<bar>x\<bar>"
wenzelm@63489
  2003
  for x y :: int
haftmann@67118
  2004
  by (metis abs_dvd_iff gcd_0_left_int gcd_unique_int)
nipkow@31798
  2005
wenzelm@63489
  2006
lemma gcd_proj2_if_dvd_int [simp]: "y dvd x \<Longrightarrow> gcd x y = \<bar>y\<bar>"
wenzelm@63489
  2007
  for x y :: int
haftmann@62344
  2008
  by (metis gcd_proj1_if_dvd_int gcd.commute)
nipkow@31798
  2009
wenzelm@63489
  2010
wenzelm@63489
  2011
text \<open>\<^medskip> Multiplication laws.\<close>
wenzelm@63489
  2012
wenzelm@63489
  2013
lemma gcd_mult_distrib_nat: "k * gcd m n = gcd (k * m) (k * n)"
wenzelm@63489
  2014
  for k m n :: nat
wenzelm@63489
  2015
  \<comment> \<open>@{cite \<open>page 27\<close> davenport92}\<close>
nipkow@31952
  2016
  apply (induct m n rule: gcd_nat_induct)
wenzelm@63489
  2017
   apply simp
wenzelm@63489
  2018
  apply (cases "k = 0")
wenzelm@63489
  2019
   apply (simp_all add: gcd_non_0_nat)
wenzelm@63489
  2020
  done
wenzelm@63489
  2021
wenzelm@63489
  2022
lemma gcd_mult_distrib_int: "\<bar>k\<bar> * gcd m n = gcd (k * m) (k * n)"
wenzelm@63489
  2023
  for k m n :: int
haftmann@67118
  2024
  using gcd_mult_distrib' [of k m n] by simp
wenzelm@21256
  2025
wenzelm@63489
  2026
text \<open>\medskip Addition laws.\<close>
wenzelm@63489
  2027
wenzelm@63489
  2028
(* TODO: add the other variations? *)
wenzelm@63489
  2029
wenzelm@63489
  2030
lemma gcd_diff1_nat: "m \<ge> n \<Longrightarrow> gcd (m - n) n = gcd m n"
wenzelm@63489
  2031
  for m n :: nat
eberlm@62429
  2032
  by (subst gcd_add1 [symmetric]) auto
huffman@31706
  2033
wenzelm@63489
  2034
lemma gcd_diff2_nat: "n \<ge> m \<Longrightarrow> gcd (n - m) n = gcd m n"
wenzelm@63489
  2035
  for m n :: nat
haftmann@62344
  2036
  apply (subst gcd.commute)
nipkow@31952
  2037
  apply (subst gcd_diff1_nat [symmetric])
wenzelm@63489
  2038
   apply auto
haftmann@62344
  2039
  apply (subst gcd.commute)
nipkow@31952
  2040
  apply (subst gcd_diff1_nat)
wenzelm@63489
  2041
   apply assumption
haftmann@62344
  2042
  apply (rule gcd.commute)
haftmann@62344
  2043
  done
huffman@31706
  2044
wenzelm@63489
  2045
lemma gcd_non_0_int: "y > 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
wenzelm@63489
  2046
  for x y :: int
huffman@31706
  2047
  apply (frule_tac b = y and a = x in pos_mod_sign)
haftmann@66816
  2048
  apply (simp del: Euclidean_Division.pos_mod_sign add: gcd_int_def abs_if nat_mod_distrib)
wenzelm@63489
  2049
  apply (auto simp add: gcd_non_0_nat nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
huffman@31706
  2050
  apply (frule_tac a = x in pos_mod_bound)
haftmann@62344
  2051
  apply (subst (1 2) gcd.commute)
haftmann@66816
  2052
  apply (simp del: Euclidean_Division.pos_mod_bound add: nat_diff_distrib gcd_diff2_nat nat_le_eq_zle)
haftmann@62344
  2053
  done
wenzelm@21256
  2054
wenzelm@63489
  2055
lemma gcd_red_int: "gcd x y = gcd y (x mod y)"
wenzelm@63489
  2056
  for x y :: int
wenzelm@63489
  2057
  apply (cases "y = 0")
wenzelm@63489
  2058
   apply force
wenzelm@63489
  2059
  apply (cases "y > 0")
wenzelm@63489
  2060
   apply (subst gcd_non_0_int, auto)
wenzelm@63489
  2061
  apply (insert gcd_non_0_int [of "- y" "- x"])
huffman@35216
  2062
  apply auto
wenzelm@63489
  2063
  done
wenzelm@63489
  2064
wenzelm@63489
  2065
(* TODO: differences, and all variations of addition rules
huffman@31706
  2066
    as simplification rules for nat and int *)
huffman@31706
  2067
wenzelm@63489
  2068
(* TODO: add the three variations of these, and for ints? *)
wenzelm@63489
  2069
wenzelm@63489
  2070
lemma finite_divisors_nat [simp]: (* FIXME move *)
haftmann@62353
  2071
  fixes m :: nat
wenzelm@63489
  2072
  assumes "m > 0"
haftmann@62353
  2073
  shows "finite {d. d dvd m}"
nipkow@31734
  2074
proof-
haftmann@62353
  2075
  from assms have "{d. d dvd m} \<subseteq> {d. d \<le> m}"
haftmann@62353
  2076
    by (auto dest: dvd_imp_le)
haftmann@62353
  2077
  then show ?thesis
haftmann@62353
  2078
    using finite_Collect_le_nat by (rule finite_subset)
nipkow@31734
  2079
qed
nipkow@31734
  2080
haftmann@62353
  2081
lemma finite_divisors_int [simp]:
haftmann@62353
  2082
  fixes i :: int
haftmann@62353
  2083
  assumes "i \<noteq> 0"
haftmann@62353
  2084
  shows "finite {d. d dvd i}"
haftmann@62353
  2085
proof -
haftmann@62353
  2086
  have "{d. \<bar>d\<bar> \<le> \<bar>i\<bar>} = {- \<bar>i\<bar>..\<bar>i\<bar>}"
haftmann@62353
  2087
    by (auto simp: abs_if)
wenzelm@63489
  2088
  then have "finite {d. \<bar>d\<bar> \<le> \<bar>i\<bar>}"
haftmann@62353
  2089
    by simp
wenzelm@63489
  2090
  from finite_subset [OF _ this] show ?thesis
wenzelm@63489
  2091
    using assms by (simp add: dvd_imp_le_int subset_iff)
nipkow@31734
  2092
qed
nipkow@31734
  2093
wenzelm@63489
  2094
lemma Max_divisors_self_nat [simp]: "n \<noteq> 0 \<Longrightarrow> Max {d::nat. d dvd n} = n"
wenzelm@63489
  2095
  apply (rule antisym)
wenzelm@63489
  2096
   apply (fastforce intro: Max_le_iff[THEN iffD2] simp: dvd_imp_le)
wenzelm@63489
  2097
  apply simp
wenzelm@63489
  2098
  done
wenzelm@63489
  2099
wenzelm@63489
  2100
lemma Max_divisors_self_int [simp]: "n \<noteq> 0 \<Longrightarrow> Max {d::int. d dvd n} = \<bar>n\<bar>"
wenzelm@63489
  2101
  apply (rule antisym)
wenzelm@63489
  2102
   apply (rule Max_le_iff [THEN iffD2])
wenzelm@63489
  2103
     apply (auto intro: abs_le_D1 dvd_imp_le_int)
wenzelm@63489
  2104
  done
wenzelm@63489
  2105
wenzelm@63489
  2106
lemma gcd_is_Max_divisors_nat: "m > 0 \<Longrightarrow> n > 0 \<Longrightarrow> gcd m n = Max {d. d dvd m \<and> d dvd n}"
wenzelm@63489
  2107
  for m n :: nat
wenzelm@63489
  2108
  apply (rule Max_eqI[THEN sym])
wenzelm@63489
  2109
    apply (metis finite_Collect_conjI finite_divisors_nat)
wenzelm@63489
  2110
   apply simp
wenzelm@63489
  2111
   apply (metis Suc_diff_1 Suc_neq_Zero dvd_imp_le gcd_greatest_iff gcd_pos_nat)
wenzelm@63489
  2112
  apply simp
wenzelm@63489
  2113
  done
wenzelm@63489
  2114
wenzelm@63489
  2115
lemma gcd_is_Max_divisors_int: "m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> gcd m n = Max {d. d dvd m \<and> d dvd n}"
wenzelm@63489
  2116
  for m n :: int
wenzelm@63489
  2117
  apply (rule Max_eqI[THEN sym])
wenzelm@63489
  2118
    apply (metis finite_Collect_conjI finite_divisors_int)
wenzelm@63489
  2119
   apply simp
wenzelm@63489
  2120
   apply (metis gcd_greatest_iff gcd_pos_int zdvd_imp_le)
wenzelm@63489
  2121
  apply simp
wenzelm@63489
  2122
  done
wenzelm@63489
  2123
wenzelm@63489
  2124
lemma gcd_code_int [code]: "gcd k l = \<bar>if l = 0 then k else gcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
wenzelm@63489
  2125
  for k l :: int
haftmann@67118
  2126
  using gcd_red_int [of "\<bar>k\<bar>" "\<bar>l\<bar>"] by simp
haftmann@34030
  2127
haftmann@67051
  2128
lemma coprime_Suc_left_nat [simp]:
haftmann@67051
  2129
  "coprime (Suc n) n"
haftmann@67051
  2130
  using coprime_add_one_left [of n] by simp
haftmann@67051
  2131
haftmann@67051
  2132
lemma coprime_Suc_right_nat [simp]:
haftmann@67051
  2133
  "coprime n (Suc n)"
haftmann@67051
  2134
  using coprime_Suc_left_nat [of n] by (simp add: ac_simps)
haftmann@67051
  2135
haftmann@67051
  2136
lemma coprime_diff_one_left_nat [simp]:
haftmann@67051
  2137
  "coprime (n - 1) n" if "n > 0" for n :: nat
haftmann@67051
  2138
  using that coprime_Suc_right_nat [of "n - 1"] by simp
haftmann@67051
  2139
haftmann@67051
  2140
lemma coprime_diff_one_right_nat [simp]:
haftmann@67051
  2141
  "coprime n (n - 1)" if "n > 0" for n :: nat
haftmann@67051
  2142
  using that coprime_diff_one_left_nat [of n] by (simp add: ac_simps)
haftmann@67051
  2143
haftmann@67051
  2144
lemma coprime_crossproduct_nat:
haftmann@67051
  2145
  fixes a b c d :: nat
haftmann@67051
  2146
  assumes "coprime a d" and "coprime b c"
haftmann@67051
  2147
  shows "a * c = b * d \<longleftrightarrow> a = b \<and> c = d"
haftmann@67051
  2148
  using assms coprime_crossproduct [of a d b c] by simp
haftmann@67051
  2149
haftmann@67051
  2150
lemma coprime_crossproduct_int:
haftmann@67051
  2151
  fixes a b c d :: int
haftmann@67051
  2152
  assumes "coprime a d" and "coprime b c"
haftmann@67051
  2153
  shows "\<bar>a\<bar> * \<bar>c\<bar> = \<bar>b\<bar> * \<bar>d\<bar> \<longleftrightarrow> \<bar>a\<bar> = \<bar>b\<bar> \<and> \<bar>c\<bar> = \<bar>d\<bar>"
haftmann@67051
  2154
  using assms coprime_crossproduct [of a d b c] by simp
huffman@31706
  2155
huffman@31706
  2156
wenzelm@60758
  2157
subsection \<open>Bezout's theorem\<close>
huffman@31706
  2158
wenzelm@63489
  2159
text \<open>
wenzelm@63489
  2160
  Function \<open>bezw\<close> returns a pair of witnesses to Bezout's theorem --
wenzelm@63489
  2161
  see the theorems that follow the definition.
wenzelm@63489
  2162
\<close>
wenzelm@63489
  2163
wenzelm@63489
  2164
fun bezw :: "nat \<Rightarrow> nat \<Rightarrow> int * int"
wenzelm@63489
  2165
  where "bezw x y =
wenzelm@63489
  2166
    (if y = 0 then (1, 0)
wenzelm@63489
  2167
     else
huffman@31706
  2168
      (snd (bezw y (x mod y)),
huffman@31706
  2169
       fst (bezw y (x mod y)) - snd (bezw y (x mod y)) * int(x div y)))"
huffman@31706
  2170
wenzelm@63489
  2171
lemma bezw_0 [simp]: "bezw x 0 = (1, 0)"
wenzelm@63489
  2172
  by simp
wenzelm@63489
  2173
wenzelm@63489
  2174
lemma bezw_non_0:
wenzelm@63489
  2175
  "y > 0 \<Longrightarrow> bezw x y =
wenzelm@63489
  2176
    (snd (bezw y (x mod y)), fst (bezw y (x mod y)) - snd (bezw y (x mod y)) * int(x div y))"
huffman@31706
  2177
  by simp
huffman@31706
  2178
huffman@31706
  2179
declare bezw.simps [simp del]
huffman@31706
  2180
wenzelm@63489
  2181
lemma bezw_aux: "fst (bezw x y) * int x + snd (bezw x y) * int y = int (gcd x y)"
nipkow@31952
  2182
proof (induct x y rule: gcd_nat_induct)
huffman@31706
  2183
  fix m :: nat
huffman@31706
  2184
  show "fst (bezw m 0) * int m + snd (bezw m 0) * int 0 = int (gcd m 0)"
huffman@31706
  2185
    by auto
wenzelm@63489
  2186
next
wenzelm@63489
  2187
  fix m n :: nat
wenzelm@63489
  2188
  assume ngt0: "n > 0"
wenzelm@63489
  2189
    and ih: "fst (bezw n (m mod n)) * int n + snd (bezw n (m mod n)) * int (m mod n) =
wenzelm@63489
  2190
      int (gcd n (m mod n))"
wenzelm@63489
  2191
  then show "fst (bezw m n) * int m + snd (bezw m n) * int n = int (gcd m n)"
wenzelm@63489
  2192
    apply (simp add: bezw_non_0 gcd_non_0_nat)
wenzelm@63489
  2193
    apply (erule subst)
wenzelm@63489
  2194
    apply (simp add: field_simps)
haftmann@64242
  2195
    apply (subst div_mult_mod_eq [of m n, symmetric])
wenzelm@63489
  2196
      (* applying simp here undoes the last substitution! what is procedure cancel_div_mod? *)
wenzelm@63489
  2197
    apply (simp only: NO_MATCH_def field_simps of_nat_add of_nat_mult)
wenzelm@63489
  2198
    done
huffman@31706
  2199
qed
huffman@31706
  2200
wenzelm@63489
  2201
lemma bezout_int: "\<exists>u v. u * x + v * y = gcd x y"
wenzelm@63489
  2202
  for x y :: int
huffman@31706
  2203
proof -
wenzelm@63489
  2204
  have aux: "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> \<exists>u v. u * x + v * y = gcd x y" for x y :: int
huffman@31706
  2205
    apply (rule_tac x = "fst (bezw (nat x) (nat y))" in exI)
huffman@31706
  2206
    apply (rule_tac x = "snd (bezw (nat x) (nat y))" in exI)
huffman@31706
  2207
    apply (unfold gcd_int_def)
huffman@31706
  2208
    apply simp
huffman@31706
  2209
    apply (subst bezw_aux [symmetric])
huffman@31706
  2210
    apply auto
huffman@31706
  2211
    done
wenzelm@63489
  2212
  consider "x \<ge> 0" "y \<ge> 0" | "x \<ge> 0" "y \<le> 0" | "x \<le> 0" "y \<ge> 0" | "x \<le> 0" "y \<le> 0"
wenzelm@63489
  2213
    by atomize_elim auto
wenzelm@63489
  2214
  then show ?thesis
wenzelm@63489
  2215
  proof cases
wenzelm@63489
  2216
    case 1
wenzelm@63489
  2217
    then show ?thesis by (rule aux)
wenzelm@63489
  2218
  next
wenzelm@63489
  2219
    case 2
wenzelm@63489
  2220
    then show ?thesis
wenzelm@63489
  2221
      apply -
wenzelm@63489
  2222
      apply (insert aux [of x "-y"])
wenzelm@63489
  2223
      apply auto
wenzelm@63489
  2224
      apply (rule_tac x = u in exI)
wenzelm@63489
  2225
      apply (rule_tac x = "-v" in exI)
wenzelm@63489
  2226
      apply (subst gcd_neg2_int [symmetric])
wenzelm@63489
  2227
      apply auto
wenzelm@63489
  2228
      done
wenzelm@63489
  2229
  next
wenzelm@63489
  2230
    case 3
wenzelm@63489
  2231
    then show ?thesis
wenzelm@63489
  2232
      apply -
wenzelm@63489
  2233
      apply (insert aux [of "-x" y])
wenzelm@63489
  2234
      apply auto
wenzelm@63489
  2235
      apply (rule_tac x = "-u" in exI)
wenzelm@63489
  2236
      apply (rule_tac x = v in exI)
wenzelm@63489
  2237
      apply (subst gcd_neg1_int [symmetric])
wenzelm@63489
  2238
      apply auto
wenzelm@63489
  2239
      done
wenzelm@63489
  2240
  next
wenzelm@63489
  2241
    case 4
wenzelm@63489
  2242
    then show ?thesis
wenzelm@63489
  2243
      apply -
wenzelm@63489
  2244
      apply (insert aux [of "-x" "-y"])
wenzelm@63489
  2245
      apply auto
wenzelm@63489
  2246
      apply (rule_tac x = "-u" in exI)
wenzelm@63489
  2247
      apply (rule_tac x = "-v" in exI)
wenzelm@63489
  2248
      apply (subst gcd_neg1_int [symmetric])
wenzelm@63489
  2249
      apply (subst gcd_neg2_int [symmetric])
wenzelm@63489
  2250
      apply auto
wenzelm@63489
  2251
      done
wenzelm@63489
  2252
  qed
huffman@31706
  2253
qed
huffman@31706
  2254
wenzelm@63489
  2255
wenzelm@63489
  2256
text \<open>Versions of Bezout for \<open>nat\<close>, by Amine Chaieb.\<close>
huffman@31706
  2257
huffman@31706
  2258
lemma ind_euclid:
wenzelm@63489
  2259
  fixes P :: "nat \<Rightarrow> nat \<Rightarrow> bool"
wenzelm@63489
  2260
  assumes c: " \<forall>a b. P a b \<longleftrightarrow> P b a"
wenzelm@63489
  2261
    and z: "\<forall>a. P a 0"
wenzelm@63489
  2262
    and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)"
chaieb@27669
  2263
  shows "P a b"
wenzelm@63489
  2264
proof (induct "a + b" arbitrary: a b rule: less_induct)
berghofe@34915
  2265
  case less
wenzelm@63489
  2266
  consider (eq) "a = b" | (lt) "a < b" "a + b - a < a + b" | "b = 0" | "b + a - b < a + b"
wenzelm@63489
  2267
    by arith
wenzelm@63489
  2268
  show ?case
wenzelm@63489
  2269
  proof (cases a b rule: linorder_cases)
wenzelm@63489
  2270
    case equal
wenzelm@63489
  2271
    with add [rule_format, OF z [rule_format, of a]] show ?thesis by simp
wenzelm@63489
  2272
  next
wenzelm@63489
  2273
    case lt: less
wenzelm@63489
  2274
    then consider "a = 0" | "a + b - a < a + b" by arith
wenzelm@63489
  2275
    then show ?thesis
wenzelm@63489
  2276
    proof cases
wenzelm@63489
  2277
      case 1
wenzelm@63489
  2278
      with z c show ?thesis by blast
wenzelm@63489
  2279
    next
wenzelm@63489
  2280
      case 2
wenzelm@63489
  2281
      also have *: "a + b - a = a + (b - a)" using lt by arith
berghofe@34915
  2282
      finally have "a + (b - a) < a + b" .
wenzelm@63489
  2283
      then have "P a (a + (b - a))" by (rule add [rule_format, OF less])
wenzelm@63489
  2284
      then show ?thesis by (simp add: *[symmetric])
wenzelm@63489
  2285
    qed
wenzelm@63489
  2286
  next
wenzelm@63489
  2287
    case gt: greater
wenzelm@63489
  2288
    then consider "b = 0" | "b + a - b < a + b" by arith
wenzelm@63489
  2289
    then show ?thesis
wenzelm@63489
  2290
    proof cases
wenzelm@63489
  2291
      case 1
wenzelm@63489
  2292
      with z c show ?thesis by blast
wenzelm@63489
  2293
    next
wenzelm@63489
  2294
      case 2
wenzelm@63489
  2295
      also have *: "b + a - b = b + (a - b)" using gt by arith
berghofe@34915
  2296
      finally have "b + (a - b) < a + b" .
wenzelm@63489
  2297
      then have "P b (b + (a - b))" by (rule add [rule_format, OF less])
wenzelm@63489
  2298
      then have "P b a" by (simp add: *[symmetric])
wenzelm@63489
  2299
      with c show ?thesis by blast
wenzelm@63489
  2300
    qed
wenzelm@63489
  2301
  qed
chaieb@27669
  2302
qed
chaieb@27669
  2303
nipkow@31952
  2304
lemma bezout_lemma_nat:
huffman@31706
  2305
  assumes ex: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  2306
    (a * x = b * y + d \<or> b * x = a * y + d)"
huffman@31706
  2307
  shows "\<exists>d x y. d dvd a \<and> d dvd a + b \<and>
huffman@31706
  2308
    (a * x = (a + b) * y + d \<or> (a + b) * x = a * y + d)"
huffman@31706
  2309
  using ex
huffman@31706
  2310
  apply clarsimp
wenzelm@63489
  2311
  apply (rule_tac x="d" in exI)
wenzelm@63489
  2312
  apply simp
wenzelm@63489
  2313
  apply (case_tac "a * x = b * y + d")
wenzelm@63489
  2314
   apply simp_all
wenzelm@63489
  2315
   apply (rule_tac x="x + y" in exI)
wenzelm@63489
  2316
   apply (rule_tac x="y" in exI)
wenzelm@63489
  2317
   apply algebra
huffman@31706
  2318
  apply (rule_tac x="x" in exI)
huffman@31706
  2319
  apply (rule_tac x="x + y" in exI)
huffman@31706
  2320
  apply algebra
wenzelm@63489
  2321
  done
chaieb@27669
  2322
nipkow@31952
  2323
lemma bezout_add_nat: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  2324
    (a * x = b * y + d \<or> b * x = a * y + d)"
wenzelm@63489
  2325
  apply (induct a b rule: ind_euclid)
wenzelm@63489
  2326
    apply blast
wenzelm@63489
  2327
   apply clarify
wenzelm@63489
  2328
   apply (rule_tac x="a" in exI)
wenzelm@63489
  2329
   apply simp
huffman@31706
  2330
  apply clarsimp
huffman@31706
  2331
  apply (rule_tac x="d" in exI)
wenzelm@63489
  2332
  apply (case_tac "a * x = b * y + d")
wenzelm@63489
  2333
   apply simp_all
wenzelm@63489
  2334
   apply (rule_tac x="x+y" in exI)
wenzelm@63489
  2335
   apply (rule_tac x="y" in exI)
wenzelm@63489
  2336
   apply algebra
huffman@31706
  2337
  apply (rule_tac x="x" in exI)
huffman@31706
  2338
  apply (rule_tac x="x+y" in exI)
huffman@31706
  2339
  apply algebra
wenzelm@63489
  2340
  done
chaieb@27669
  2341
nipkow@31952
  2342
lemma bezout1_nat: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  2343
    (a * x - b * y = d \<or> b * x - a * y = d)"
nipkow@31952
  2344
  using bezout_add_nat[of a b]
huffman@31706
  2345
  apply clarsimp
wenzelm@63489
  2346
  apply (rule_tac x="d" in exI)
wenzelm@63489
  2347
  apply simp
huffman@31706
  2348
  apply (rule_tac x="x" in exI)
huffman@31706
  2349
  apply (rule_tac x="y" in exI)
huffman@31706
  2350
  apply auto
wenzelm@63489
  2351
  done
wenzelm@63489
  2352
wenzelm@63489
  2353
lemma bezout_add_strong_nat:
wenzelm@63489
  2354
  fixes a b :: nat
wenzelm@63489
  2355
  assumes a: "a \<noteq> 0"
chaieb@27669
  2356
  shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
wenzelm@63489
  2357
proof -
wenzelm@63489
  2358
  consider d x y where "d dvd a" "d dvd b" "a * x = b * y + d"
wenzelm@63489
  2359
    | d x y where "d dvd a" "d dvd b" "b * x = a * y + d"
wenzelm@63489
  2360
    using bezout_add_nat [of a b] by blast
wenzelm@63489
  2361
  then show ?thesis
wenzelm@63489
  2362
  proof cases
wenzelm@63489
  2363
    case 1
wenzelm@63489
  2364
    then show ?thesis by blast
wenzelm@63489
  2365
  next
wenzelm@63489
  2366
    case H: 2
wenzelm@63489
  2367
    show ?thesis
wenzelm@63489
  2368
    proof (cases "b = 0")
wenzelm@63489
  2369
      case True
wenzelm@63489
  2370
      with H show ?thesis by simp
wenzelm@63489
  2371
    next
wenzelm@63489
  2372
      case False
wenzelm@63489
  2373
      then have bp: "b > 0" by simp
wenzelm@63489
  2374
      with dvd_imp_le [OF H(2)] consider "d = b" | "d < b"
wenzelm@63489
  2375
        by atomize_elim auto
wenzelm@63489
  2376
      then show ?thesis
wenzelm@63489
  2377
      proof cases
wenzelm@63489
  2378
        case 1
wenzelm@63489
  2379
        with a H show ?thesis
wenzelm@63489
  2380
          apply simp
wenzelm@63489
  2381
          apply (rule exI[where x = b])
wenzelm@63489
  2382
          apply simp
wenzelm@63489
  2383
          apply (rule exI[where x = b])
wenzelm@63489
  2384
          apply (rule exI[where x = "a - 1"])
wenzelm@63489
  2385
          apply (simp add: diff_mult_distrib2)
wenzelm@63489
  2386
          done
wenzelm@63489
  2387
      next
wenzelm@63489
  2388
        case 2
wenzelm@63489
  2389
        show ?thesis
wenzelm@63489
  2390
        proof (cases "x = 0")
wenzelm@63489
  2391
          case True
wenzelm@63489
  2392
          with a H show ?thesis by simp
wenzelm@63489
  2393
        next
wenzelm@63489
  2394
          case x0: False
wenzelm@63489
  2395
          then have xp: "x > 0" by simp
wenzelm@63489
  2396
          from \<open>d < b\<close> have "d \<le> b - 1" by simp
wenzelm@63489
  2397
          then have "d * b \<le> b * (b - 1)" by simp
wenzelm@63489
  2398
          with xp mult_mono[of "1" "x" "d * b" "b * (b - 1)"]
wenzelm@63489
  2399
          have dble: "d * b \<le> x * b * (b - 1)" using bp by simp
wenzelm@63489
  2400
          from H(3) have "d + (b - 1) * (b * x) = d + (b - 1) * (a * y + d)"
huffman@31706
  2401
            by simp
wenzelm@63489
  2402
          then have "d + (b - 1) * a * y + (b - 1) * d = d + (b - 1) * b * x"
haftmann@57512
  2403
            by (simp only: mult.assoc distrib_left)
wenzelm@63489
  2404
          then have "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x * b * (b - 1)"
huffman@31706
  2405
            by algebra
wenzelm@63489
  2406
          then have "a * ((b - 1) * y) = d + x * b * (b - 1) - d * b"
wenzelm@63489
  2407
            using bp by simp
wenzelm@63489
  2408
          then have "a * ((b - 1) * y) = d + (x * b * (b - 1) - d * b)"
wenzelm@32960
  2409
            by (simp only: diff_add_assoc[OF dble, of d, symmetric])
wenzelm@63489
  2410
          then have "a * ((b - 1) * y) = b * (x * (b - 1) - d) + d"
haftmann@59008
  2411
            by (simp only: diff_mult_distrib2 ac_simps)
wenzelm@63489
  2412
          with H(1,2) show ?thesis
wenzelm@32960
  2413
            apply -
wenzelm@63489
  2414
            apply (rule exI [where x = d])
wenzelm@63489
  2415
            apply simp
wenzelm@63489
  2416
            apply (rule exI [where x = "(b - 1) * y"])
wenzelm@63489
  2417
            apply (rule exI [where x = "x * (b - 1) - d"])
wenzelm@63489
  2418
            apply simp
wenzelm@63489
  2419
            done
wenzelm@63489
  2420
        qed
wenzelm@63489
  2421
      qed
wenzelm@63489
  2422
    qed
wenzelm@63489
  2423
  qed
chaieb@27669
  2424
qed
chaieb@27669
  2425
wenzelm@63489
  2426
lemma bezout_nat:
wenzelm@63489
  2427
  fixes a :: nat
wenzelm@63489
  2428
  assumes a: "a \<noteq> 0"
chaieb@27669
  2429
  shows "\<exists>x y. a * x = b * y + gcd a b"
wenzelm@63489
  2430
proof -
wenzelm@63489
  2431
  obtain d x y where d: "d dvd a" "d dvd b" and eq: "a * x = b * y + d"
wenzelm@63489
  2432
    using bezout_add_strong_nat [OF a, of b] by blast
wenzelm@63489
  2433
  from d have "d dvd gcd a b"
wenzelm@63489
  2434
    by simp
wenzelm@63489
  2435
  then obtain k where k: "gcd a b = d * k"
wenzelm@63489
  2436
    unfolding dvd_def by blast
wenzelm@63489
  2437
  from eq have "a * x * k = (b * y + d) * k"
wenzelm@63489
  2438
    by auto
wenzelm@63489
  2439
  then have "a * (x * k) = b * (y * k) + gcd a b"
wenzelm@63489
  2440
    by (algebra add: k)
wenzelm@63489
  2441
  then show ?thesis
wenzelm@63489
  2442
    by blast
chaieb@27669
  2443
qed
chaieb@27669
  2444
huffman@31706
  2445
wenzelm@63489
  2446
subsection \<open>LCM properties on @{typ nat} and @{typ int}\<close>
wenzelm@63489
  2447
wenzelm@63489
  2448
lemma lcm_altdef_int [code]: "lcm a b = \<bar>a\<bar> * \<bar>b\<bar> div gcd a b"
wenzelm@63489
  2449
  for a b :: int
haftmann@67118
  2450
  by (simp add: abs_mult lcm_gcd)
haftmann@67118
  2451
  
wenzelm@63489
  2452
lemma prod_gcd_lcm_nat: "m * n = gcd m n * lcm m n"
wenzelm@63489
  2453
  for m n :: nat
huffman@31706
  2454
  unfolding lcm_nat_def
eberlm@62429
  2455
  by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
huffman@31706
  2456
wenzelm@63489
  2457
lemma prod_gcd_lcm_int: "\<bar>m\<bar> * \<bar>n\<bar> = gcd m n * lcm m n"
wenzelm@63489
  2458
  for m n :: int
huffman@31706
  2459
  unfolding lcm_int_def gcd_int_def
haftmann@62348
  2460
  apply (subst of_nat_mult [symmetric])
nipkow@31952
  2461
  apply (subst prod_gcd_lcm_nat [symmetric])
huffman@31706
  2462
  apply (subst nat_abs_mult_distrib [symmetric])
wenzelm@63489
  2463
  apply (simp add: abs_mult)
wenzelm@63489
  2464
  done
wenzelm@63489
  2465
wenzelm@63489
  2466
lemma lcm_pos_nat: "m > 0 \<Longrightarrow> n > 0 \<Longrightarrow> lcm m n > 0"
wenzelm@63489
  2467
  for m n :: nat
haftmann@67118
  2468
  using lcm_eq_0_iff [of m n] by auto
wenzelm@63489
  2469
wenzelm@63489
  2470
lemma lcm_pos_int: "m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> lcm m n > 0"
wenzelm@63489
  2471
  for m n :: int
haftmann@67118
  2472
  by (simp add: less_le lcm_eq_0_iff)
haftmann@23687
  2473
wenzelm@63489
  2474
lemma dvd_pos_nat: "n > 0 \<Longrightarrow> m dvd n \<Longrightarrow> m > 0"  (* FIXME move *)
wenzelm@63489
  2475
  for m n :: nat
wenzelm@63489
  2476
  by (cases m) auto
wenzelm@63489
  2477
wenzelm@63489
  2478
lemma lcm_unique_nat:
wenzelm@63489
  2479
  "a dvd d \<and> b dvd d \<and> (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
wenzelm@63489
  2480
  for a b d :: nat
haftmann@62344
  2481
  by (auto intro: dvd_antisym lcm_least)
chaieb@27568
  2482
wenzelm@63489
  2483
lemma lcm_unique_int:
wenzelm@63489
  2484
  "d \<ge> 0 \<and> a dvd d \<and> b dvd d \<and> (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
wenzelm@63489
  2485
  for a b d :: int
haftmann@62344
  2486
  using lcm_least zdvd_antisym_nonneg by auto
haftmann@34973
  2487
wenzelm@63489
  2488
lemma lcm_proj2_if_dvd_nat [simp]: "x dvd y \<Longrightarrow> lcm x y = y"
wenzelm@63489
  2489
  for x y :: nat
huffman@31706
  2490
  apply (rule sym)
nipkow@31952
  2491
  apply (subst lcm_unique_nat [symmetric])
huffman@31706
  2492
  apply auto
wenzelm@63489
  2493
  done
wenzelm@63489
  2494
wenzelm@63489
  2495
lemma lcm_proj2_if_dvd_int [simp]: "x dvd y \<Longrightarrow> lcm x y = \<bar>y\<bar>"
wenzelm@63489
  2496
  for x y :: int
huffman@31706
  2497
  apply (rule sym)
nipkow@31952
  2498
  apply (subst lcm_unique_int [symmetric])
huffman@31706
  2499
  apply auto
wenzelm@63489
  2500
  done
wenzelm@63489
  2501
wenzelm@63489
  2502
lemma lcm_proj1_if_dvd_nat [simp]: "x dvd y \<Longrightarrow> lcm y x = y"
wenzelm@63489
  2503
  for x y :: nat
wenzelm@63489
  2504
  by (subst lcm.commute) (erule lcm_proj2_if_dvd_nat)
wenzelm@63489
  2505
wenzelm@63489
  2506
lemma lcm_proj1_if_dvd_int [simp]: "x dvd y \<Longrightarrow> lcm y x = \<bar>y\<bar>"
wenzelm@63489
  2507
  for x y :: int
wenzelm@63489
  2508
  by (subst lcm.commute) (erule lcm_proj2_if_dvd_int)
wenzelm@63489
  2509
wenzelm@63489
  2510
lemma lcm_proj1_iff_nat [simp]: "lcm m n = m \<longleftrightarrow> n dvd m"
wenzelm@63489
  2511
  for m n :: nat
wenzelm@63489
  2512
  by (metis lcm_proj1_if_dvd_nat lcm_unique_nat)
wenzelm@63489
  2513
wenzelm@63489
  2514
lemma lcm_proj2_iff_nat [simp]: "lcm m n = n \<longleftrightarrow> m dvd n"
wenzelm@63489
  2515
  for m n :: nat
wenzelm@63489
  2516
  by (metis lcm_proj2_if_dvd_nat lcm_unique_nat)
wenzelm@63489
  2517
wenzelm@63489
  2518
lemma lcm_proj1_iff_int [simp]: "lcm m n = \<bar>m\<bar> \<longleftrightarrow> n dvd m"
wenzelm@63489
  2519
  for m n :: int
wenzelm@63489
  2520
  by (metis dvd_abs_iff lcm_proj1_if_dvd_int lcm_unique_int)
wenzelm@63489
  2521
wenzelm@63489
  2522
lemma lcm_proj2_iff_int [simp]: "lcm m n = \<bar>n\<bar> \<longleftrightarrow> m dvd n"
wenzelm@63489
  2523
  for m n :: int
wenzelm@63489
  2524
  by (metis dvd_abs_iff lcm_proj2_if_dvd_int lcm_unique_int)
wenzelm@63489
  2525
wenzelm@63489
  2526
lemma lcm_1_iff_nat [simp]: "lcm m n = Suc 0 \<longleftrightarrow> m = Suc 0 \<and> n = Suc 0"
wenzelm@63489
  2527
  for m n :: nat
haftmann@62353
  2528
  using lcm_eq_1_iff [of m n] by simp
wenzelm@63489
  2529
wenzelm@63489
  2530
lemma lcm_1_iff_int [simp]: "lcm m n = 1 \<longleftrightarrow> (m = 1 \<or> m = -1) \<and> (n = 1 \<or> n = -1)"
wenzelm@63489
  2531
  for m n :: int
haftmann@61913
  2532
  by auto
nipkow@31995
  2533
haftmann@34030
  2534
haftmann@62345
  2535
subsection \<open>The complete divisibility lattice on @{typ nat} and @{typ int}\<close>
nipkow@32112
  2536
wenzelm@63489
  2537
text \<open>
wenzelm@63489
  2538
  Lifting \<open>gcd\<close> and \<open>lcm\<close> to sets (\<open>Gcd\<close> / \<open>Lcm\<close>).
wenzelm@63489
  2539
  \<open>Gcd\<close> is defined via \<open>Lcm\<close> to facilitate the proof that we have a complete lattice.
wenzelm@60758
  2540
\<close>
huffman@45264
  2541
haftmann@62345
  2542
instantiation nat :: semiring_Gcd
nipkow@32112
  2543
begin
nipkow@32112
  2544
haftmann@62344
  2545
interpretation semilattice_neutr_set lcm "1::nat"
haftmann@62344
  2546
  by standard simp_all
haftmann@54867
  2547
wenzelm@63489
  2548
definition "Lcm M = (if finite M then F M else 0)" for M :: "nat set"
wenzelm@63489
  2549
wenzelm@63489
  2550
lemma Lcm_nat_empty: "Lcm {} = (1::nat)"
haftmann@60690
  2551
  by (simp add: Lcm_nat_def del: One_nat_def)
haftmann@51489
  2552
wenzelm@63489
  2553
lemma Lcm_nat_insert: "Lcm (insert n M) = lcm n (Lcm M)" for n :: nat
haftmann@61929
  2554
  by (cases "finite M") (auto simp add: Lcm_nat_def simp del: One_nat_def)
haftmann@61929
  2555
wenzelm@63489
  2556
lemma Lcm_nat_infinite: "infinite M \<Longrightarrow> Lcm M = 0" for M :: "nat set"
haftmann@61929
  2557
  by (simp add: Lcm_nat_def)
haftmann@61929
  2558
haftmann@61929
  2559
lemma dvd_Lcm_nat [simp]:
haftmann@61929
  2560
  fixes M :: "nat set"
haftmann@61929
  2561
  assumes "m \<in> M"
haftmann@61929
  2562
  shows "m dvd Lcm M"
haftmann@61929
  2563
proof -
wenzelm@63489
  2564
  from assms have "insert m M = M"
wenzelm@63489
  2565
    by auto
haftmann@61929
  2566
  moreover have "m dvd Lcm (insert m M)"
haftmann@61929
  2567
    by (simp add: Lcm_nat_insert)
wenzelm@63489
  2568
  ultimately show ?thesis
wenzelm@63489
  2569
    by simp
haftmann@61929
  2570
qed
haftmann@61929
  2571
haftmann@61929
  2572
lemma Lcm_dvd_nat [simp]:
haftmann@61929
  2573
  fixes M :: "nat set"
haftmann@61929
  2574
  assumes "\<forall>m\<in>M. m dvd n"
haftmann@61929
  2575
  shows "Lcm M dvd n"
haftmann@62353
  2576
proof (cases "n > 0")
wenzelm@63489
  2577
  case False
wenzelm@63489
  2578
  then show ?thesis by simp
haftmann@61929
  2579
next
haftmann@62353
  2580
  case True
wenzelm@63489
  2581
  then have "finite {d. d dvd n}"
wenzelm@63489
  2582
    by (rule finite_divisors_nat)
wenzelm@63489
  2583
  moreover have "M \<subseteq> {d. d dvd n}"
wenzelm@63489
  2584
    using assms by fast
wenzelm@63489
  2585
  ultimately have "finite M"
wenzelm@63489
  2586
    by (rule rev_finite_subset)
wenzelm@63489
  2587
  then show ?thesis
wenzelm@63489
  2588
    using assms by (induct M) (simp_all add: Lcm_nat_empty Lcm_nat_insert)
haftmann@61929
  2589
qed
nipkow@32112
  2590
wenzelm@63489
  2591
definition "Gcd M = Lcm {d. \<forall>m\<in>M. d dvd m}" for M :: "nat set"
wenzelm@63489
  2592
wenzelm@63489
  2593
instance
wenzelm@63489
  2594
proof
wenzelm@63489
  2595
  fix N :: "nat set"
wenzelm@63489
  2596
  fix n :: nat
wenzelm@63489
  2597
  show "Gcd N dvd n" if "n \<in> N"
wenzelm@63489
  2598
    using that by (induct N rule: infinite_finite_induct) (auto simp add: Gcd_nat_def)
wenzelm@63489
  2599
  show "n dvd Gcd N" if "\<And>m. m \<in> N \<Longrightarrow> n dvd m"
wenzelm@63489
  2600
    using that by (induct N rule: infinite_finite_induct) (auto simp add: Gcd_nat_def)
wenzelm@63489
  2601
  show "n dvd Lcm N" if "n \<in> N"
wenzelm@63489
  2602
    using that by (induct N rule: infinite_finite_induct) auto
wenzelm@63489
  2603
  show "Lcm N dvd n" if "\<And>m. m \<in> N \<Longrightarrow> m dvd n"
wenzelm@63489
  2604
    using that by (induct N rule: infinite_finite_induct) auto
wenzelm@63489
  2605
  show "normalize (Gcd N) = Gcd N" and "normalize (Lcm N) = Lcm N"
wenzelm@63489
  2606
    by simp_all
wenzelm@63489
  2607
qed
nipkow@32112
  2608
haftmann@62345
  2609
end
haftmann@61913
  2610
wenzelm@63489
  2611
lemma Gcd_nat_eq_one: "1 \<in> N \<Longrightarrow> Gcd N = 1"
wenzelm@63489
  2612
  for N :: "nat set"
haftmann@62346
  2613
  by (rule Gcd_eq_1_I) auto
haftmann@62346
  2614
wenzelm@63489
  2615
wenzelm@63489
  2616
text \<open>Alternative characterizations of Gcd:\<close>
nipkow@32112
  2617
haftmann@62353
  2618
lemma Gcd_eq_Max:
haftmann@62353
  2619
  fixes M :: "nat set"
haftmann@62353
  2620
  assumes "finite (M::nat set)" and "M \<noteq> {}" and "0 \<notin> M"
haftmann@62353
  2621
  shows "Gcd M = Max (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  2622
proof (rule antisym)
haftmann@62353
  2623
  from assms obtain m where "m \<in> M" and "m > 0"
haftmann@62353
  2624
    by auto
haftmann@62353
  2625
  from \<open>m > 0\<close> have "finite {d. d dvd m}"
haftmann@62353
  2626
    by (blast intro: finite_divisors_nat)
haftmann@62353
  2627
  with \<open>m \<in> M\<close> have fin: "finite (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  2628
    by blast
haftmann@62353
  2629
  from fin show "Gcd M \<le> Max (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  2630
    by (auto intro: Max_ge Gcd_dvd)
haftmann@62353
  2631
  from fin show "Max (\<Inter>m\<in>M. {d. d dvd m}) \<le> Gcd M"
haftmann@62353
  2632
    apply (rule Max.boundedI)
wenzelm@63489
  2633
     apply auto
haftmann@62353
  2634
    apply (meson Gcd_dvd Gcd_greatest \<open>0 < m\<close> \<open>m \<in> M\<close> dvd_imp_le dvd_pos_nat)
haftmann@62353
  2635
    done
haftmann@62353
  2636
qed
nipkow@32112
  2637
wenzelm@63489
  2638
lemma Gcd_remove0_nat: "finite M \<Longrightarrow> Gcd M = Gcd (M - {0})"
wenzelm@63489
  2639
  for M :: "nat set"
wenzelm@63489
  2640
  apply (induct pred: finite)
wenzelm@63489
  2641
   apply simp
wenzelm@63489
  2642
  apply (case_tac "x = 0")
wenzelm@63489
  2643
   apply simp
wenzelm@63489
  2644
  apply (subgoal_tac "insert x F - {0} = insert x (F - {0})")
wenzelm@63489
  2645
   apply simp
wenzelm@63489
  2646
  apply blast
wenzelm@63489
  2647
  done
nipkow@32112
  2648
nipkow@32112
  2649
lemma Lcm_in_lcm_closed_set_nat:
wenzelm@63489
  2650
  "finite M \<Longrightarrow> M \<noteq> {} \<Longrightarrow> \<forall>m n. m \<in> M \<longrightarrow> n \<in> M \<longrightarrow> lcm m n \<in> M \<Longrightarrow> Lcm M \<in> M"
wenzelm@63489
  2651
  for M :: "nat set"
wenzelm@63489
  2652
  apply (induct rule: finite_linorder_min_induct)
wenzelm@63489
  2653
   apply simp
nipkow@32112
  2654
  apply simp
wenzelm@63489
  2655
  apply (subgoal_tac "\<forall>m n. m \<in> A \<longrightarrow> n \<in> A \<longrightarrow> lcm m n \<in> A")
wenzelm@63489
  2656
   apply simp
wenzelm@63489
  2657
   apply(case_tac "A = {}")
wenzelm@63489
  2658
    apply simp
wenzelm@63489
  2659
   apply simp
wenzelm@63489
  2660
  apply (metis lcm_pos_nat lcm_unique_nat linorder_neq_iff nat_dvd_not_less not_less0)
wenzelm@63489
  2661
  done
nipkow@32112
  2662
nipkow@32112
  2663
lemma Lcm_eq_Max_nat:
wenzelm@63489
  2664
  "finite M \<Longrightarrow> M \<noteq> {} \<Longrightarrow> 0 \<notin> M \<Longrightarrow> \<forall>m n. m \<in> M \<longrightarrow> n \<in> M \<longrightarrow> lcm m n \<in> M \<Longrightarrow> Lcm M = Max M"
wenzelm@63489
  2665
  for M :: "nat set"
wenzelm@63489
  2666
  apply (rule antisym)
wenzelm@63489
  2667
   apply (rule Max_ge)
wenzelm@63489
  2668
    apply assumption
wenzelm@63489
  2669
   apply (erule (2) Lcm_in_lcm_closed_set_nat)
wenzelm@63489
  2670
  apply (auto simp add: not_le Lcm_0_iff dvd_imp_le leD le_neq_trans)
wenzelm@63489
  2671
  done
nipkow@32112
  2672
nipkow@34222
  2673
lemma mult_inj_if_coprime_nat:
wenzelm@63489
  2674
  "inj_on f A \<Longrightarrow> inj_on g B \<Longrightarrow> \<forall>a\<in>A. \<forall>b\<in>B. coprime (f a) (g b) \<Longrightarrow>
wenzelm@63489
  2675
    inj_on (\<lambda>(a, b). f a * g b) (A \<times> B)"
wenzelm@63489
  2676
  for f :: "'a \<Rightarrow> nat" and g :: "'b \<Rightarrow> nat"
haftmann@61913
  2677
  by (auto simp add: inj_on_def coprime_crossproduct_nat simp del: One_nat_def)
nipkow@34222
  2678
wenzelm@63489
  2679
wenzelm@63489
  2680
subsubsection \<open>Setwise GCD and LCM for integers\<close>
huffman@45264
  2681
haftmann@67118
  2682
instantiation int :: Gcd
huffman@45264
  2683
begin
huffman@45264
  2684
haftmann@67118
  2685
definition Gcd_int :: "int set \<Rightarrow> int"
haftmann@67118
  2686
  where "Gcd K = int (GCD k\<in>K. (nat \<circ> abs) k)"
haftmann@67118
  2687
haftmann@67118
  2688
definition Lcm_int :: "int set \<Rightarrow> int"
haftmann@67118
  2689
  where "Lcm K = int (LCM k\<in>K. (nat \<circ> abs) k)"
haftmann@67118
  2690
haftmann@67118
  2691
instance ..
haftmann@62345
  2692
haftmann@62345
  2693
end
haftmann@62345
  2694
haftmann@67118
  2695
lemma Gcd_int_eq [simp]:
haftmann@67118
  2696
  "(GCD n\<in>N. int n) = int (Gcd N)"