src/HOL/GCD.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62353 7f927120b5a2
child 62429 25271ff79171
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
haftmann@32479
     1
(*  Authors:    Christophe Tabacznyj, Lawrence C. Paulson, Amine Chaieb,
nipkow@31798
     2
                Thomas M. Rasmussen, Jeremy Avigad, Tobias Nipkow
huffman@31706
     3
huffman@31706
     4
haftmann@32479
     5
This file deals with the functions gcd and lcm.  Definitions and
haftmann@32479
     6
lemmas are proved uniformly for the natural numbers and integers.
huffman@31706
     7
huffman@31706
     8
This file combines and revises a number of prior developments.
huffman@31706
     9
huffman@31706
    10
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
wenzelm@58623
    11
and Lawrence C. Paulson, based on @{cite davenport92}. They introduced
huffman@31706
    12
gcd, lcm, and prime for the natural numbers.
huffman@31706
    13
huffman@31706
    14
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
huffman@31706
    15
extended gcd, lcm, primes to the integers. Amine Chaieb provided
huffman@31706
    16
another extension of the notions to the integers, and added a number
huffman@31706
    17
of results to "Primes" and "GCD". IntPrimes also defined and developed
huffman@31706
    18
the congruence relations on the integers. The notion was extended to
berghofe@34915
    19
the natural numbers by Chaieb.
huffman@31706
    20
avigad@32036
    21
Jeremy Avigad combined all of these, made everything uniform for the
avigad@32036
    22
natural numbers and the integers, and added a number of new theorems.
avigad@32036
    23
nipkow@31798
    24
Tobias Nipkow cleaned up a lot.
wenzelm@21256
    25
*)
wenzelm@21256
    26
huffman@31706
    27
wenzelm@60758
    28
section \<open>Greatest common divisor and least common multiple\<close>
wenzelm@21256
    29
wenzelm@21256
    30
theory GCD
lp15@59667
    31
imports Main
huffman@31706
    32
begin
huffman@31706
    33
haftmann@62345
    34
subsection \<open>Abstract GCD and LCM\<close>
huffman@31706
    35
nipkow@31992
    36
class gcd = zero + one + dvd +
wenzelm@41550
    37
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@41550
    38
    and lcm :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@21256
    39
begin
wenzelm@21256
    40
haftmann@60686
    41
abbreviation coprime :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@60686
    42
  where "coprime x y \<equiv> gcd x y = 1"
huffman@31706
    43
huffman@31706
    44
end
huffman@31706
    45
haftmann@60686
    46
class Gcd = gcd +
haftmann@62350
    47
  fixes Gcd :: "'a set \<Rightarrow> 'a" ("Gcd")
haftmann@62350
    48
    and Lcm :: "'a set \<Rightarrow> 'a" ("Lcm")
haftmann@62350
    49
begin
haftmann@62350
    50
haftmann@62350
    51
abbreviation GCD :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
haftmann@62350
    52
where
haftmann@62350
    53
  "GCD A f \<equiv> Gcd (f ` A)"
haftmann@62350
    54
haftmann@62350
    55
abbreviation LCM :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
haftmann@62350
    56
where
haftmann@62350
    57
  "LCM A f \<equiv> Lcm (f ` A)"
haftmann@62350
    58
haftmann@62350
    59
end
haftmann@62350
    60
haftmann@62350
    61
syntax
haftmann@62350
    62
  "_GCD1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3Gcd _./ _)" [0, 10] 10)
haftmann@62350
    63
  "_GCD"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3Gcd _\<in>_./ _)" [0, 0, 10] 10)
haftmann@62350
    64
  "_LCM1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3Lcm _./ _)" [0, 10] 10)
haftmann@62350
    65
  "_LCM"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3Lcm _\<in>_./ _)" [0, 0, 10] 10)
haftmann@62350
    66
haftmann@62350
    67
translations
haftmann@62350
    68
  "Gcd x y. B"   \<rightleftharpoons> "Gcd x. Gcd y. B"
haftmann@62350
    69
  "Gcd x. B"     \<rightleftharpoons> "CONST GCD CONST UNIV (\<lambda>x. B)"
haftmann@62350
    70
  "Gcd x. B"     \<rightleftharpoons> "Gcd x \<in> CONST UNIV. B"
haftmann@62350
    71
  "Gcd x\<in>A. B"   \<rightleftharpoons> "CONST GCD A (\<lambda>x. B)"
haftmann@62350
    72
  "Lcm x y. B"   \<rightleftharpoons> "Lcm x. Lcm y. B"
haftmann@62350
    73
  "Lcm x. B"     \<rightleftharpoons> "CONST LCM CONST UNIV (\<lambda>x. B)"
haftmann@62350
    74
  "Lcm x. B"     \<rightleftharpoons> "Lcm x \<in> CONST UNIV. B"
haftmann@62350
    75
  "Lcm x\<in>A. B"   \<rightleftharpoons> "CONST LCM A (\<lambda>x. B)"
haftmann@62350
    76
haftmann@62350
    77
print_translation \<open>
haftmann@62350
    78
  [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax GCD} @{syntax_const "_GCD"},
haftmann@62350
    79
    Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax LCM} @{syntax_const "_LCM"}]
haftmann@62350
    80
\<close> \<comment> \<open>to avoid eta-contraction of body\<close>
haftmann@60686
    81
haftmann@60686
    82
class semiring_gcd = normalization_semidom + gcd +
haftmann@59008
    83
  assumes gcd_dvd1 [iff]: "gcd a b dvd a"
wenzelm@59977
    84
    and gcd_dvd2 [iff]: "gcd a b dvd b"
wenzelm@59977
    85
    and gcd_greatest: "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> c dvd gcd a b"
haftmann@60686
    86
    and normalize_gcd [simp]: "normalize (gcd a b) = gcd a b"
haftmann@60686
    87
    and lcm_gcd: "lcm a b = normalize (a * b) div gcd a b"
haftmann@60686
    88
begin    
haftmann@60686
    89
haftmann@60689
    90
lemma gcd_greatest_iff [simp]:
haftmann@60686
    91
  "a dvd gcd b c \<longleftrightarrow> a dvd b \<and> a dvd c"
haftmann@60686
    92
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@60686
    93
haftmann@60689
    94
lemma gcd_dvdI1:
haftmann@60689
    95
  "a dvd c \<Longrightarrow> gcd a b dvd c"
haftmann@60689
    96
  by (rule dvd_trans) (rule gcd_dvd1)
haftmann@60689
    97
haftmann@60689
    98
lemma gcd_dvdI2:
haftmann@60689
    99
  "b dvd c \<Longrightarrow> gcd a b dvd c"
haftmann@60689
   100
  by (rule dvd_trans) (rule gcd_dvd2)
haftmann@60689
   101
haftmann@62345
   102
lemma dvd_gcdD1:
haftmann@62345
   103
  "a dvd gcd b c \<Longrightarrow> a dvd b"
haftmann@62345
   104
  using gcd_dvd1 [of b c] by (blast intro: dvd_trans)
haftmann@62345
   105
haftmann@62345
   106
lemma dvd_gcdD2:
haftmann@62345
   107
  "a dvd gcd b c \<Longrightarrow> a dvd c"
haftmann@62345
   108
  using gcd_dvd2 [of b c] by (blast intro: dvd_trans)
haftmann@62345
   109
haftmann@60686
   110
lemma gcd_0_left [simp]:
haftmann@60686
   111
  "gcd 0 a = normalize a"
haftmann@60688
   112
  by (rule associated_eqI) simp_all
haftmann@60686
   113
haftmann@60686
   114
lemma gcd_0_right [simp]:
haftmann@60686
   115
  "gcd a 0 = normalize a"
haftmann@60688
   116
  by (rule associated_eqI) simp_all
haftmann@60686
   117
  
haftmann@60686
   118
lemma gcd_eq_0_iff [simp]:
haftmann@60686
   119
  "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0" (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   120
proof
haftmann@60686
   121
  assume ?P then have "0 dvd gcd a b" by simp
haftmann@60686
   122
  then have "0 dvd a" and "0 dvd b" by (blast intro: dvd_trans)+
haftmann@60686
   123
  then show ?Q by simp
haftmann@60686
   124
next
haftmann@60686
   125
  assume ?Q then show ?P by simp
haftmann@60686
   126
qed
haftmann@60686
   127
haftmann@60686
   128
lemma unit_factor_gcd:
haftmann@60686
   129
  "unit_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)"
haftmann@60686
   130
proof (cases "gcd a b = 0")
haftmann@60686
   131
  case True then show ?thesis by simp
haftmann@60686
   132
next
haftmann@60686
   133
  case False
haftmann@60686
   134
  have "unit_factor (gcd a b) * normalize (gcd a b) = gcd a b"
haftmann@60686
   135
    by (rule unit_factor_mult_normalize)
haftmann@60686
   136
  then have "unit_factor (gcd a b) * gcd a b = gcd a b"
haftmann@60686
   137
    by simp
haftmann@60686
   138
  then have "unit_factor (gcd a b) * gcd a b div gcd a b = gcd a b div gcd a b"
haftmann@60686
   139
    by simp
haftmann@60686
   140
  with False show ?thesis by simp
haftmann@60686
   141
qed
haftmann@60686
   142
haftmann@60690
   143
lemma is_unit_gcd [simp]:
haftmann@60690
   144
  "is_unit (gcd a b) \<longleftrightarrow> coprime a b"
haftmann@60690
   145
  by (cases "a = 0 \<and> b = 0") (auto simp add: unit_factor_gcd dest: is_unit_unit_factor)
haftmann@60690
   146
wenzelm@61605
   147
sublocale gcd: abel_semigroup gcd
haftmann@60686
   148
proof
haftmann@60686
   149
  fix a b c
haftmann@60686
   150
  show "gcd a b = gcd b a"
haftmann@60688
   151
    by (rule associated_eqI) simp_all
haftmann@60686
   152
  from gcd_dvd1 have "gcd (gcd a b) c dvd a"
haftmann@60686
   153
    by (rule dvd_trans) simp
haftmann@60686
   154
  moreover from gcd_dvd1 have "gcd (gcd a b) c dvd b"
haftmann@60686
   155
    by (rule dvd_trans) simp
haftmann@60686
   156
  ultimately have P1: "gcd (gcd a b) c dvd gcd a (gcd b c)"
haftmann@60686
   157
    by (auto intro!: gcd_greatest)
haftmann@60686
   158
  from gcd_dvd2 have "gcd a (gcd b c) dvd b"
haftmann@60686
   159
    by (rule dvd_trans) simp
haftmann@60686
   160
  moreover from gcd_dvd2 have "gcd a (gcd b c) dvd c"
haftmann@60686
   161
    by (rule dvd_trans) simp
haftmann@60686
   162
  ultimately have P2: "gcd a (gcd b c) dvd gcd (gcd a b) c"
haftmann@60686
   163
    by (auto intro!: gcd_greatest)
haftmann@60688
   164
  from P1 P2 show "gcd (gcd a b) c = gcd a (gcd b c)"
haftmann@60688
   165
    by (rule associated_eqI) simp_all
haftmann@60686
   166
qed
haftmann@60686
   167
haftmann@60686
   168
lemma gcd_self [simp]:
haftmann@60686
   169
  "gcd a a = normalize a"
haftmann@60686
   170
proof -
haftmann@60686
   171
  have "a dvd gcd a a"
haftmann@60686
   172
    by (rule gcd_greatest) simp_all
haftmann@60686
   173
  then show ?thesis
haftmann@60688
   174
    by (auto intro: associated_eqI)
haftmann@60686
   175
qed
haftmann@61913
   176
haftmann@61913
   177
lemma gcd_left_idem [simp]:
haftmann@61913
   178
  "gcd a (gcd a b) = gcd a b"
haftmann@61913
   179
  by (auto intro: associated_eqI)
haftmann@61913
   180
haftmann@61913
   181
lemma gcd_right_idem [simp]:
haftmann@61913
   182
  "gcd (gcd a b) b = gcd a b"
haftmann@61913
   183
  unfolding gcd.commute [of a] gcd.commute [of "gcd b a"] by simp
haftmann@61913
   184
haftmann@60686
   185
lemma coprime_1_left [simp]:
haftmann@60686
   186
  "coprime 1 a"
haftmann@60688
   187
  by (rule associated_eqI) simp_all
haftmann@60686
   188
haftmann@60686
   189
lemma coprime_1_right [simp]:
haftmann@60686
   190
  "coprime a 1"
haftmann@60686
   191
  using coprime_1_left [of a] by (simp add: ac_simps)
haftmann@60686
   192
haftmann@60686
   193
lemma gcd_mult_left:
haftmann@60686
   194
  "gcd (c * a) (c * b) = normalize c * gcd a b"
haftmann@60686
   195
proof (cases "c = 0")
haftmann@60686
   196
  case True then show ?thesis by simp
haftmann@60686
   197
next
haftmann@60686
   198
  case False
haftmann@60686
   199
  then have "c * gcd a b dvd gcd (c * a) (c * b)"
haftmann@60686
   200
    by (auto intro: gcd_greatest)
haftmann@60686
   201
  moreover from calculation False have "gcd (c * a) (c * b) dvd c * gcd a b"
haftmann@60686
   202
    by (metis div_dvd_iff_mult dvd_mult_left gcd_dvd1 gcd_dvd2 gcd_greatest mult_commute)
haftmann@60686
   203
  ultimately have "normalize (gcd (c * a) (c * b)) = normalize (c * gcd a b)"
haftmann@60688
   204
    by (auto intro: associated_eqI)
haftmann@60686
   205
  then show ?thesis by (simp add: normalize_mult)
haftmann@60686
   206
qed
haftmann@60686
   207
haftmann@60686
   208
lemma gcd_mult_right:
haftmann@60686
   209
  "gcd (a * c) (b * c) = gcd b a * normalize c"
haftmann@60686
   210
  using gcd_mult_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   211
haftmann@60686
   212
lemma mult_gcd_left:
haftmann@60686
   213
  "c * gcd a b = unit_factor c * gcd (c * a) (c * b)"
haftmann@60686
   214
  by (simp add: gcd_mult_left mult.assoc [symmetric])
haftmann@60686
   215
haftmann@60686
   216
lemma mult_gcd_right:
haftmann@60686
   217
  "gcd a b * c = gcd (a * c) (b * c) * unit_factor c"
haftmann@60686
   218
  using mult_gcd_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   219
haftmann@60689
   220
lemma dvd_lcm1 [iff]:
haftmann@60686
   221
  "a dvd lcm a b"
haftmann@60686
   222
proof -
haftmann@60686
   223
  have "normalize (a * b) div gcd a b = normalize a * (normalize b div gcd a b)"
haftmann@60686
   224
    by (simp add: lcm_gcd normalize_mult div_mult_swap)
haftmann@60686
   225
  then show ?thesis
haftmann@60686
   226
    by (simp add: lcm_gcd)
haftmann@60686
   227
qed
haftmann@60686
   228
  
haftmann@60689
   229
lemma dvd_lcm2 [iff]:
haftmann@60686
   230
  "b dvd lcm a b"
haftmann@60686
   231
proof -
haftmann@60686
   232
  have "normalize (a * b) div gcd a b = normalize b * (normalize a div gcd a b)"
haftmann@60686
   233
    by (simp add: lcm_gcd normalize_mult div_mult_swap ac_simps)
haftmann@60686
   234
  then show ?thesis
haftmann@60686
   235
    by (simp add: lcm_gcd)
haftmann@60686
   236
qed
haftmann@60686
   237
haftmann@60689
   238
lemma dvd_lcmI1:
haftmann@60689
   239
  "a dvd b \<Longrightarrow> a dvd lcm b c"
haftmann@60689
   240
  by (rule dvd_trans) (assumption, blast) 
haftmann@60689
   241
haftmann@60689
   242
lemma dvd_lcmI2:
haftmann@60689
   243
  "a dvd c \<Longrightarrow> a dvd lcm b c"
haftmann@60689
   244
  by (rule dvd_trans) (assumption, blast)
haftmann@60689
   245
haftmann@62345
   246
lemma lcm_dvdD1:
haftmann@62345
   247
  "lcm a b dvd c \<Longrightarrow> a dvd c"
haftmann@62345
   248
  using dvd_lcm1 [of a b] by (blast intro: dvd_trans)
haftmann@62345
   249
haftmann@62345
   250
lemma lcm_dvdD2:
haftmann@62345
   251
  "lcm a b dvd c \<Longrightarrow> b dvd c"
haftmann@62345
   252
  using dvd_lcm2 [of a b] by (blast intro: dvd_trans)
haftmann@62345
   253
haftmann@60686
   254
lemma lcm_least:
haftmann@60686
   255
  assumes "a dvd c" and "b dvd c"
haftmann@60686
   256
  shows "lcm a b dvd c"
haftmann@60686
   257
proof (cases "c = 0")
haftmann@60686
   258
  case True then show ?thesis by simp
haftmann@60686
   259
next
haftmann@60686
   260
  case False then have U: "is_unit (unit_factor c)" by simp
haftmann@60686
   261
  show ?thesis
haftmann@60686
   262
  proof (cases "gcd a b = 0")
haftmann@60686
   263
    case True with assms show ?thesis by simp
haftmann@60686
   264
  next
haftmann@60686
   265
    case False then have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
haftmann@60686
   266
    with \<open>c \<noteq> 0\<close> assms have "a * b dvd a * c" "a * b dvd c * b"
haftmann@60686
   267
      by (simp_all add: mult_dvd_mono)
haftmann@60686
   268
    then have "normalize (a * b) dvd gcd (a * c) (b * c)"
haftmann@60686
   269
      by (auto intro: gcd_greatest simp add: ac_simps)
haftmann@60686
   270
    then have "normalize (a * b) dvd gcd (a * c) (b * c) * unit_factor c"
haftmann@60686
   271
      using U by (simp add: dvd_mult_unit_iff)
haftmann@60686
   272
    then have "normalize (a * b) dvd gcd a b * c"
haftmann@60686
   273
      by (simp add: mult_gcd_right [of a b c])
haftmann@60686
   274
    then have "normalize (a * b) div gcd a b dvd c"
haftmann@60686
   275
      using False by (simp add: div_dvd_iff_mult ac_simps)
haftmann@60686
   276
    then show ?thesis by (simp add: lcm_gcd)
haftmann@60686
   277
  qed
haftmann@60686
   278
qed
haftmann@60686
   279
haftmann@60689
   280
lemma lcm_least_iff [simp]:
haftmann@60686
   281
  "lcm a b dvd c \<longleftrightarrow> a dvd c \<and> b dvd c"
haftmann@60686
   282
  by (blast intro!: lcm_least intro: dvd_trans)
haftmann@60686
   283
haftmann@60686
   284
lemma normalize_lcm [simp]:
haftmann@60686
   285
  "normalize (lcm a b) = lcm a b"
haftmann@60686
   286
  by (simp add: lcm_gcd dvd_normalize_div)
haftmann@60686
   287
haftmann@60686
   288
lemma lcm_0_left [simp]:
haftmann@60686
   289
  "lcm 0 a = 0"
haftmann@60686
   290
  by (simp add: lcm_gcd)
haftmann@60686
   291
  
haftmann@60686
   292
lemma lcm_0_right [simp]:
haftmann@60686
   293
  "lcm a 0 = 0"
haftmann@60686
   294
  by (simp add: lcm_gcd)
haftmann@60686
   295
haftmann@60686
   296
lemma lcm_eq_0_iff:
haftmann@60686
   297
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0" (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   298
proof
haftmann@60686
   299
  assume ?P then have "0 dvd lcm a b" by simp
haftmann@60686
   300
  then have "0 dvd normalize (a * b) div gcd a b"
haftmann@60686
   301
    by (simp add: lcm_gcd)
haftmann@60686
   302
  then have "0 * gcd a b dvd normalize (a * b)"
haftmann@60686
   303
    using dvd_div_iff_mult [of "gcd a b" _ 0] by (cases "gcd a b = 0") simp_all
haftmann@60686
   304
  then have "normalize (a * b) = 0"
haftmann@60686
   305
    by simp
haftmann@60686
   306
  then show ?Q by simp
haftmann@60686
   307
next
haftmann@60686
   308
  assume ?Q then show ?P by auto
haftmann@60686
   309
qed
haftmann@60686
   310
haftmann@61913
   311
lemma lcm_eq_1_iff [simp]:
haftmann@61913
   312
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@61913
   313
  by (auto intro: associated_eqI)
haftmann@61913
   314
haftmann@60686
   315
lemma unit_factor_lcm :
haftmann@60686
   316
  "unit_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
haftmann@60686
   317
  by (simp add: unit_factor_gcd dvd_unit_factor_div lcm_gcd)
haftmann@60686
   318
wenzelm@61605
   319
sublocale lcm: abel_semigroup lcm
haftmann@60686
   320
proof
haftmann@60686
   321
  fix a b c
haftmann@60686
   322
  show "lcm a b = lcm b a"
haftmann@60686
   323
    by (simp add: lcm_gcd ac_simps normalize_mult dvd_normalize_div)
haftmann@60688
   324
  have "lcm (lcm a b) c dvd lcm a (lcm b c)"
haftmann@60688
   325
    and "lcm a (lcm b c) dvd lcm (lcm a b) c"
haftmann@60688
   326
    by (auto intro: lcm_least
haftmann@60686
   327
      dvd_trans [of b "lcm b c" "lcm a (lcm b c)"]
haftmann@60686
   328
      dvd_trans [of c "lcm b c" "lcm a (lcm b c)"]
haftmann@60686
   329
      dvd_trans [of a "lcm a b" "lcm (lcm a b) c"]
haftmann@60686
   330
      dvd_trans [of b "lcm a b" "lcm (lcm a b) c"])
haftmann@60686
   331
  then show "lcm (lcm a b) c = lcm a (lcm b c)"
haftmann@60688
   332
    by (rule associated_eqI) simp_all
haftmann@60686
   333
qed
haftmann@60686
   334
haftmann@60686
   335
lemma lcm_self [simp]:
haftmann@60686
   336
  "lcm a a = normalize a"
haftmann@60686
   337
proof -
haftmann@60686
   338
  have "lcm a a dvd a"
haftmann@60686
   339
    by (rule lcm_least) simp_all
haftmann@60686
   340
  then show ?thesis
haftmann@60688
   341
    by (auto intro: associated_eqI)
haftmann@60686
   342
qed
haftmann@60686
   343
haftmann@61913
   344
lemma lcm_left_idem [simp]:
haftmann@61913
   345
  "lcm a (lcm a b) = lcm a b"
haftmann@61913
   346
  by (auto intro: associated_eqI)
haftmann@61913
   347
haftmann@61913
   348
lemma lcm_right_idem [simp]:
haftmann@61913
   349
  "lcm (lcm a b) b = lcm a b"
haftmann@61913
   350
  unfolding lcm.commute [of a] lcm.commute [of "lcm b a"] by simp
haftmann@61913
   351
haftmann@60686
   352
lemma gcd_mult_lcm [simp]:
haftmann@60686
   353
  "gcd a b * lcm a b = normalize a * normalize b"
haftmann@60686
   354
  by (simp add: lcm_gcd normalize_mult)
haftmann@60686
   355
haftmann@60686
   356
lemma lcm_mult_gcd [simp]:
haftmann@60686
   357
  "lcm a b * gcd a b = normalize a * normalize b"
haftmann@60686
   358
  using gcd_mult_lcm [of a b] by (simp add: ac_simps) 
haftmann@60686
   359
haftmann@60686
   360
lemma gcd_lcm:
haftmann@60686
   361
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@60686
   362
  shows "gcd a b = normalize (a * b) div lcm a b"
haftmann@60686
   363
proof -
haftmann@60686
   364
  from assms have "lcm a b \<noteq> 0"
haftmann@60686
   365
    by (simp add: lcm_eq_0_iff)
haftmann@60686
   366
  have "gcd a b * lcm a b = normalize a * normalize b" by simp
haftmann@60686
   367
  then have "gcd a b * lcm a b div lcm a b = normalize (a * b) div lcm a b"
haftmann@60686
   368
    by (simp_all add: normalize_mult)
haftmann@60686
   369
  with \<open>lcm a b \<noteq> 0\<close> show ?thesis
haftmann@60686
   370
    using nonzero_mult_divide_cancel_right [of "lcm a b" "gcd a b"] by simp
haftmann@60686
   371
qed
haftmann@60686
   372
haftmann@60686
   373
lemma lcm_1_left [simp]:
haftmann@60686
   374
  "lcm 1 a = normalize a"
haftmann@60686
   375
  by (simp add: lcm_gcd)
haftmann@60686
   376
haftmann@60686
   377
lemma lcm_1_right [simp]:
haftmann@60686
   378
  "lcm a 1 = normalize a"
haftmann@60686
   379
  by (simp add: lcm_gcd)
haftmann@60686
   380
  
haftmann@60686
   381
lemma lcm_mult_left:
haftmann@60686
   382
  "lcm (c * a) (c * b) = normalize c * lcm a b"
haftmann@60686
   383
  by (cases "c = 0")
haftmann@60686
   384
    (simp_all add: gcd_mult_right lcm_gcd div_mult_swap normalize_mult ac_simps,
haftmann@60686
   385
      simp add: dvd_div_mult2_eq mult.left_commute [of "normalize c", symmetric])
haftmann@60686
   386
haftmann@60686
   387
lemma lcm_mult_right:
haftmann@60686
   388
  "lcm (a * c) (b * c) = lcm b a * normalize c"
haftmann@60686
   389
  using lcm_mult_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   390
haftmann@60686
   391
lemma mult_lcm_left:
haftmann@60686
   392
  "c * lcm a b = unit_factor c * lcm (c * a) (c * b)"
haftmann@60686
   393
  by (simp add: lcm_mult_left mult.assoc [symmetric])
haftmann@60686
   394
haftmann@60686
   395
lemma mult_lcm_right:
haftmann@60686
   396
  "lcm a b * c = lcm (a * c) (b * c) * unit_factor c"
haftmann@60686
   397
  using mult_lcm_left [of c a b] by (simp add: ac_simps)
haftmann@60686
   398
  
haftmann@60686
   399
end
haftmann@60686
   400
haftmann@62345
   401
class ring_gcd = comm_ring_1 + semiring_gcd
haftmann@62345
   402
haftmann@60686
   403
class semiring_Gcd = semiring_gcd + Gcd +
haftmann@60686
   404
  assumes Gcd_dvd: "a \<in> A \<Longrightarrow> Gcd A dvd a"
haftmann@60686
   405
    and Gcd_greatest: "(\<And>b. b \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> a dvd Gcd A"
haftmann@60686
   406
    and normalize_Gcd [simp]: "normalize (Gcd A) = Gcd A"
haftmann@62345
   407
  assumes dvd_Lcm: "a \<in> A \<Longrightarrow> a dvd Lcm A"
haftmann@62345
   408
    and Lcm_least: "(\<And>b. b \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> Lcm A dvd a"
haftmann@62345
   409
    and normalize_Lcm [simp]: "normalize (Lcm A) = Lcm A"
haftmann@60686
   410
begin
haftmann@60686
   411
haftmann@62345
   412
lemma Lcm_Gcd:
haftmann@62345
   413
  "Lcm A = Gcd {b. \<forall>a\<in>A. a dvd b}"
haftmann@62345
   414
  by (rule associated_eqI) (auto intro: Gcd_dvd dvd_Lcm Gcd_greatest Lcm_least)
haftmann@62345
   415
haftmann@62345
   416
lemma Gcd_Lcm:
haftmann@62345
   417
  "Gcd A = Lcm {b. \<forall>a\<in>A. b dvd a}"
haftmann@62345
   418
  by (rule associated_eqI) (auto intro: Gcd_dvd dvd_Lcm Gcd_greatest Lcm_least)
haftmann@62345
   419
haftmann@60686
   420
lemma Gcd_empty [simp]:
haftmann@60686
   421
  "Gcd {} = 0"
haftmann@60686
   422
  by (rule dvd_0_left, rule Gcd_greatest) simp
haftmann@60686
   423
haftmann@62345
   424
lemma Lcm_empty [simp]:
haftmann@62345
   425
  "Lcm {} = 1"
haftmann@62345
   426
  by (auto intro: associated_eqI Lcm_least)
haftmann@62345
   427
haftmann@62345
   428
lemma Gcd_insert [simp]:
haftmann@62345
   429
  "Gcd (insert a A) = gcd a (Gcd A)"
haftmann@62345
   430
proof -
haftmann@62345
   431
  have "Gcd (insert a A) dvd gcd a (Gcd A)"
haftmann@62345
   432
    by (auto intro: Gcd_dvd Gcd_greatest)
haftmann@62345
   433
  moreover have "gcd a (Gcd A) dvd Gcd (insert a A)"
haftmann@62345
   434
  proof (rule Gcd_greatest)
haftmann@62345
   435
    fix b
haftmann@62345
   436
    assume "b \<in> insert a A"
haftmann@62345
   437
    then show "gcd a (Gcd A) dvd b"
haftmann@62345
   438
    proof
haftmann@62345
   439
      assume "b = a" then show ?thesis by simp
haftmann@62345
   440
    next
haftmann@62345
   441
      assume "b \<in> A"
haftmann@62345
   442
      then have "Gcd A dvd b" by (rule Gcd_dvd)
haftmann@62345
   443
      moreover have "gcd a (Gcd A) dvd Gcd A" by simp
haftmann@62345
   444
      ultimately show ?thesis by (blast intro: dvd_trans)
haftmann@62345
   445
    qed
haftmann@62345
   446
  qed
haftmann@62345
   447
  ultimately show ?thesis
haftmann@62345
   448
    by (auto intro: associated_eqI)
haftmann@62345
   449
qed
haftmann@62345
   450
haftmann@62345
   451
lemma Lcm_insert [simp]:
haftmann@62345
   452
  "Lcm (insert a A) = lcm a (Lcm A)"
haftmann@62345
   453
proof (rule sym)
haftmann@62345
   454
  have "lcm a (Lcm A) dvd Lcm (insert a A)"
haftmann@62345
   455
    by (auto intro: dvd_Lcm Lcm_least)
haftmann@62345
   456
  moreover have "Lcm (insert a A) dvd lcm a (Lcm A)"
haftmann@62345
   457
  proof (rule Lcm_least)
haftmann@62345
   458
    fix b
haftmann@62345
   459
    assume "b \<in> insert a A"
haftmann@62345
   460
    then show "b dvd lcm a (Lcm A)"
haftmann@62345
   461
    proof
haftmann@62345
   462
      assume "b = a" then show ?thesis by simp
haftmann@62345
   463
    next
haftmann@62345
   464
      assume "b \<in> A"
haftmann@62345
   465
      then have "b dvd Lcm A" by (rule dvd_Lcm)
haftmann@62345
   466
      moreover have "Lcm A dvd lcm a (Lcm A)" by simp
haftmann@62345
   467
      ultimately show ?thesis by (blast intro: dvd_trans)
haftmann@62345
   468
    qed
haftmann@62345
   469
  qed
haftmann@62345
   470
  ultimately show "lcm a (Lcm A) = Lcm (insert a A)"
haftmann@62345
   471
    by (rule associated_eqI) (simp_all add: lcm_eq_0_iff)
haftmann@62345
   472
qed
haftmann@62345
   473
haftmann@60686
   474
lemma Gcd_0_iff [simp]:
haftmann@60687
   475
  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}" (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   476
proof
haftmann@60686
   477
  assume ?P
haftmann@60686
   478
  show ?Q
haftmann@60686
   479
  proof
haftmann@60686
   480
    fix a
haftmann@60686
   481
    assume "a \<in> A"
haftmann@60686
   482
    then have "Gcd A dvd a" by (rule Gcd_dvd)
haftmann@60687
   483
    with \<open>?P\<close> have "a = 0" by simp
haftmann@60687
   484
    then show "a \<in> {0}" by simp
haftmann@60686
   485
  qed
haftmann@60686
   486
next
haftmann@60686
   487
  assume ?Q
haftmann@60686
   488
  have "0 dvd Gcd A"
haftmann@60686
   489
  proof (rule Gcd_greatest)
haftmann@60686
   490
    fix a
haftmann@60686
   491
    assume "a \<in> A"
haftmann@60687
   492
    with \<open>?Q\<close> have "a = 0" by auto
haftmann@60686
   493
    then show "0 dvd a" by simp
haftmann@60686
   494
  qed
haftmann@60686
   495
  then show ?P by simp
haftmann@60686
   496
qed
haftmann@60686
   497
haftmann@60686
   498
lemma Lcm_1_iff [simp]:
haftmann@60686
   499
  "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)" (is "?P \<longleftrightarrow> ?Q")
haftmann@60686
   500
proof
haftmann@60686
   501
  assume ?P
haftmann@60686
   502
  show ?Q
haftmann@60686
   503
  proof
haftmann@60686
   504
    fix a
haftmann@60686
   505
    assume "a \<in> A"
haftmann@60686
   506
    then have "a dvd Lcm A"
haftmann@60686
   507
      by (rule dvd_Lcm)
haftmann@60686
   508
    with \<open>?P\<close> show "is_unit a"
haftmann@60686
   509
      by simp
haftmann@60686
   510
  qed
haftmann@60686
   511
next
haftmann@60686
   512
  assume ?Q
haftmann@60686
   513
  then have "is_unit (Lcm A)"
haftmann@60686
   514
    by (blast intro: Lcm_least)
haftmann@60686
   515
  then have "normalize (Lcm A) = 1"
haftmann@60686
   516
    by (rule is_unit_normalize)
haftmann@60686
   517
  then show ?P
haftmann@60686
   518
    by simp
haftmann@60686
   519
qed
haftmann@60686
   520
haftmann@62345
   521
lemma unit_factor_Gcd:
haftmann@62345
   522
  "unit_factor (Gcd A) = (if \<forall>a\<in>A. a = 0 then 0 else 1)"
haftmann@62345
   523
proof (cases "Gcd A = 0")
haftmann@62345
   524
  case True then show ?thesis by auto
haftmann@62345
   525
next
haftmann@62345
   526
  case False
haftmann@62345
   527
  from unit_factor_mult_normalize
haftmann@62345
   528
  have "unit_factor (Gcd A) * normalize (Gcd A) = Gcd A" .
haftmann@62345
   529
  then have "unit_factor (Gcd A) * Gcd A = Gcd A" by simp
haftmann@62345
   530
  then have "unit_factor (Gcd A) * Gcd A div Gcd A = Gcd A div Gcd A" by simp
haftmann@62345
   531
  with False have "unit_factor (Gcd A) = 1" by simp
haftmann@62345
   532
  with False show ?thesis by auto
haftmann@62345
   533
qed
haftmann@62345
   534
haftmann@62345
   535
lemma unit_factor_Lcm:
haftmann@62345
   536
  "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
haftmann@62345
   537
proof (cases "Lcm A = 0")
haftmann@62345
   538
  case True then show ?thesis by simp
haftmann@62345
   539
next
haftmann@62345
   540
  case False
haftmann@62345
   541
  with unit_factor_normalize have "unit_factor (normalize (Lcm A)) = 1"
haftmann@62345
   542
    by blast
haftmann@62345
   543
  with False show ?thesis
haftmann@62345
   544
    by simp
haftmann@62345
   545
qed
haftmann@62345
   546
haftmann@62345
   547
lemma Gcd_eq_1_I:
haftmann@62345
   548
  assumes "is_unit a" and "a \<in> A"
haftmann@62345
   549
  shows "Gcd A = 1"
haftmann@62345
   550
proof -
haftmann@62345
   551
  from assms have "is_unit (Gcd A)"
haftmann@62345
   552
    by (blast intro: Gcd_dvd dvd_unit_imp_unit)
haftmann@62345
   553
  then have "normalize (Gcd A) = 1"
haftmann@62345
   554
    by (rule is_unit_normalize)
haftmann@62345
   555
  then show ?thesis
haftmann@62345
   556
    by simp
haftmann@62345
   557
qed
haftmann@62345
   558
haftmann@60686
   559
lemma Lcm_eq_0_I:
haftmann@60686
   560
  assumes "0 \<in> A"
haftmann@60686
   561
  shows "Lcm A = 0"
haftmann@60686
   562
proof -
haftmann@60686
   563
  from assms have "0 dvd Lcm A"
haftmann@60686
   564
    by (rule dvd_Lcm)
haftmann@60686
   565
  then show ?thesis
haftmann@60686
   566
    by simp
haftmann@60686
   567
qed
haftmann@60686
   568
haftmann@62345
   569
lemma Gcd_UNIV [simp]:
haftmann@62345
   570
  "Gcd UNIV = 1"
haftmann@62345
   571
  using dvd_refl by (rule Gcd_eq_1_I) simp
haftmann@62345
   572
haftmann@61929
   573
lemma Lcm_UNIV [simp]:
haftmann@61929
   574
  "Lcm UNIV = 0"
haftmann@61929
   575
  by (rule Lcm_eq_0_I) simp
haftmann@60686
   576
haftmann@61929
   577
lemma Lcm_0_iff:
haftmann@61929
   578
  assumes "finite A"
haftmann@61929
   579
  shows "Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
haftmann@61929
   580
proof (cases "A = {}")
haftmann@61929
   581
  case True then show ?thesis by simp
haftmann@61929
   582
next
haftmann@61929
   583
  case False with assms show ?thesis
haftmann@61929
   584
    by (induct A rule: finite_ne_induct)
haftmann@61929
   585
      (auto simp add: lcm_eq_0_iff)
haftmann@60686
   586
qed
haftmann@61929
   587
haftmann@62345
   588
lemma Gcd_set [code_unfold]:
haftmann@62345
   589
  "Gcd (set as) = foldr gcd as 0"
haftmann@62345
   590
  by (induct as) simp_all
haftmann@62345
   591
haftmann@60686
   592
lemma Lcm_set [code_unfold]:
haftmann@60686
   593
  "Lcm (set as) = foldr lcm as 1"
haftmann@60686
   594
  by (induct as) simp_all
haftmann@59008
   595
haftmann@62345
   596
lemma Gcd_image_normalize [simp]:
haftmann@62345
   597
  "Gcd (normalize ` A) = Gcd A"
haftmann@62345
   598
proof -
haftmann@62345
   599
  have "Gcd (normalize ` A) dvd a" if "a \<in> A" for a
haftmann@62345
   600
  proof -
haftmann@62345
   601
    from that obtain B where "A = insert a B" by blast
haftmann@62350
   602
    moreover have "gcd (normalize a) (Gcd (normalize ` B)) dvd normalize a"
haftmann@62345
   603
      by (rule gcd_dvd1)
haftmann@62345
   604
    ultimately show "Gcd (normalize ` A) dvd a"
haftmann@62345
   605
      by simp
haftmann@62345
   606
  qed
haftmann@62345
   607
  then have "Gcd (normalize ` A) dvd Gcd A" and "Gcd A dvd Gcd (normalize ` A)"
haftmann@62345
   608
    by (auto intro!: Gcd_greatest intro: Gcd_dvd)
haftmann@62345
   609
  then show ?thesis
haftmann@62345
   610
    by (auto intro: associated_eqI)
haftmann@62345
   611
qed
haftmann@62345
   612
haftmann@62346
   613
lemma Gcd_eqI:
haftmann@62346
   614
  assumes "normalize a = a"
haftmann@62346
   615
  assumes "\<And>b. b \<in> A \<Longrightarrow> a dvd b"
haftmann@62346
   616
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> c dvd b) \<Longrightarrow> c dvd a"
haftmann@62346
   617
  shows "Gcd A = a"
haftmann@62346
   618
  using assms by (blast intro: associated_eqI Gcd_greatest Gcd_dvd normalize_Gcd)
haftmann@62346
   619
haftmann@62346
   620
lemma Lcm_eqI:
haftmann@62346
   621
  assumes "normalize a = a"
haftmann@62346
   622
  assumes "\<And>b. b \<in> A \<Longrightarrow> b dvd a"
haftmann@62346
   623
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> b dvd c) \<Longrightarrow> a dvd c"
haftmann@62346
   624
  shows "Lcm A = a"
haftmann@62346
   625
  using assms by (blast intro: associated_eqI Lcm_least dvd_Lcm normalize_Lcm)
haftmann@62346
   626
haftmann@62350
   627
end
haftmann@62345
   628
haftmann@62345
   629
subsection \<open>GCD and LCM on @{typ nat} and @{typ int}\<close>
haftmann@59008
   630
huffman@31706
   631
instantiation nat :: gcd
huffman@31706
   632
begin
wenzelm@21256
   633
haftmann@62345
   634
fun gcd_nat  :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@62345
   635
where "gcd_nat x y =
haftmann@62345
   636
  (if y = 0 then x else gcd y (x mod y))"
huffman@31706
   637
haftmann@62345
   638
definition lcm_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
huffman@31706
   639
where
huffman@31706
   640
  "lcm_nat x y = x * y div (gcd x y)"
huffman@31706
   641
huffman@31706
   642
instance proof qed
huffman@31706
   643
huffman@31706
   644
end
huffman@31706
   645
huffman@31706
   646
instantiation int :: gcd
huffman@31706
   647
begin
wenzelm@21256
   648
haftmann@62345
   649
definition gcd_int  :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
   650
  where "gcd_int x y = int (gcd (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
   651
haftmann@62345
   652
definition lcm_int :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
   653
  where "lcm_int x y = int (lcm (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
   654
wenzelm@61944
   655
instance ..
huffman@31706
   656
huffman@31706
   657
end
haftmann@23687
   658
haftmann@62345
   659
text \<open>Transfer setup\<close>
huffman@31706
   660
huffman@31706
   661
lemma transfer_nat_int_gcd:
huffman@31706
   662
  "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> gcd (nat x) (nat y) = nat (gcd x y)"
huffman@31706
   663
  "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> lcm (nat x) (nat y) = nat (lcm x y)"
haftmann@32479
   664
  unfolding gcd_int_def lcm_int_def
huffman@31706
   665
  by auto
haftmann@23687
   666
huffman@31706
   667
lemma transfer_nat_int_gcd_closures:
huffman@31706
   668
  "x >= (0::int) \<Longrightarrow> y >= 0 \<Longrightarrow> gcd x y >= 0"
huffman@31706
   669
  "x >= (0::int) \<Longrightarrow> y >= 0 \<Longrightarrow> lcm x y >= 0"
huffman@31706
   670
  by (auto simp add: gcd_int_def lcm_int_def)
huffman@31706
   671
haftmann@35644
   672
declare transfer_morphism_nat_int[transfer add return:
huffman@31706
   673
    transfer_nat_int_gcd transfer_nat_int_gcd_closures]
huffman@31706
   674
huffman@31706
   675
lemma transfer_int_nat_gcd:
huffman@31706
   676
  "gcd (int x) (int y) = int (gcd x y)"
huffman@31706
   677
  "lcm (int x) (int y) = int (lcm x y)"
haftmann@32479
   678
  by (unfold gcd_int_def lcm_int_def, auto)
huffman@31706
   679
huffman@31706
   680
lemma transfer_int_nat_gcd_closures:
huffman@31706
   681
  "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> gcd x y >= 0"
huffman@31706
   682
  "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> lcm x y >= 0"
huffman@31706
   683
  by (auto simp add: gcd_int_def lcm_int_def)
huffman@31706
   684
haftmann@35644
   685
declare transfer_morphism_int_nat[transfer add return:
huffman@31706
   686
    transfer_int_nat_gcd transfer_int_nat_gcd_closures]
huffman@31706
   687
nipkow@31952
   688
lemma gcd_nat_induct:
haftmann@23687
   689
  fixes m n :: nat
haftmann@23687
   690
  assumes "\<And>m. P m 0"
haftmann@23687
   691
    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
haftmann@23687
   692
  shows "P m n"
huffman@31706
   693
  apply (rule gcd_nat.induct)
huffman@31706
   694
  apply (case_tac "y = 0")
huffman@31706
   695
  using assms apply simp_all
huffman@31706
   696
done
huffman@31706
   697
huffman@31706
   698
(* specific to int *)
huffman@31706
   699
haftmann@62346
   700
lemma gcd_eq_int_iff:
haftmann@62346
   701
  "gcd k l = int n \<longleftrightarrow> gcd (nat \<bar>k\<bar>) (nat \<bar>l\<bar>) = n"
haftmann@62346
   702
  by (simp add: gcd_int_def)
haftmann@62346
   703
haftmann@62346
   704
lemma lcm_eq_int_iff:
haftmann@62346
   705
  "lcm k l = int n \<longleftrightarrow> lcm (nat \<bar>k\<bar>) (nat \<bar>l\<bar>) = n"
haftmann@62346
   706
  by (simp add: lcm_int_def)
haftmann@62346
   707
nipkow@31952
   708
lemma gcd_neg1_int [simp]: "gcd (-x::int) y = gcd x y"
huffman@31706
   709
  by (simp add: gcd_int_def)
huffman@31706
   710
nipkow@31952
   711
lemma gcd_neg2_int [simp]: "gcd (x::int) (-y) = gcd x y"
huffman@31706
   712
  by (simp add: gcd_int_def)
huffman@31706
   713
haftmann@62353
   714
lemma abs_gcd_int [simp]: "\<bar>gcd (x::int) y\<bar> = gcd x y"
nipkow@31813
   715
by(simp add: gcd_int_def)
nipkow@31813
   716
wenzelm@61944
   717
lemma gcd_abs_int: "gcd (x::int) y = gcd \<bar>x\<bar> \<bar>y\<bar>"
nipkow@31813
   718
by (simp add: gcd_int_def)
nipkow@31813
   719
haftmann@62353
   720
lemma gcd_abs1_int [simp]: "gcd \<bar>x\<bar> (y::int) = gcd x y"
nipkow@31952
   721
by (metis abs_idempotent gcd_abs_int)
nipkow@31813
   722
haftmann@62353
   723
lemma gcd_abs2_int [simp]: "gcd x \<bar>y::int\<bar> = gcd x y"
nipkow@31952
   724
by (metis abs_idempotent gcd_abs_int)
huffman@31706
   725
nipkow@31952
   726
lemma gcd_cases_int:
huffman@31706
   727
  fixes x :: int and y
huffman@31706
   728
  assumes "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (gcd x y)"
huffman@31706
   729
      and "x >= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (gcd x (-y))"
huffman@31706
   730
      and "x <= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (gcd (-x) y)"
huffman@31706
   731
      and "x <= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (gcd (-x) (-y))"
huffman@31706
   732
  shows "P (gcd x y)"
wenzelm@61944
   733
  by (insert assms, auto, arith)
wenzelm@21256
   734
nipkow@31952
   735
lemma gcd_ge_0_int [simp]: "gcd (x::int) y >= 0"
huffman@31706
   736
  by (simp add: gcd_int_def)
huffman@31706
   737
nipkow@31952
   738
lemma lcm_neg1_int: "lcm (-x::int) y = lcm x y"
huffman@31706
   739
  by (simp add: lcm_int_def)
huffman@31706
   740
nipkow@31952
   741
lemma lcm_neg2_int: "lcm (x::int) (-y) = lcm x y"
huffman@31706
   742
  by (simp add: lcm_int_def)
huffman@31706
   743
wenzelm@61944
   744
lemma lcm_abs_int: "lcm (x::int) y = lcm \<bar>x\<bar> \<bar>y\<bar>"
huffman@31706
   745
  by (simp add: lcm_int_def)
wenzelm@21256
   746
wenzelm@61944
   747
lemma abs_lcm_int [simp]: "\<bar>lcm i j::int\<bar> = lcm i j"
wenzelm@61944
   748
  by (simp add:lcm_int_def)
nipkow@31814
   749
haftmann@62353
   750
lemma lcm_abs1_int [simp]: "lcm \<bar>x\<bar> (y::int) = lcm x y"
wenzelm@61944
   751
  by (metis abs_idempotent lcm_int_def)
nipkow@31814
   752
haftmann@62353
   753
lemma lcm_abs2_int [simp]: "lcm x \<bar>y::int\<bar> = lcm x y"
wenzelm@61944
   754
  by (metis abs_idempotent lcm_int_def)
nipkow@31814
   755
nipkow@31952
   756
lemma lcm_cases_int:
huffman@31706
   757
  fixes x :: int and y
huffman@31706
   758
  assumes "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (lcm x y)"
huffman@31706
   759
      and "x >= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (lcm x (-y))"
huffman@31706
   760
      and "x <= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (lcm (-x) y)"
huffman@31706
   761
      and "x <= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (lcm (-x) (-y))"
huffman@31706
   762
  shows "P (lcm x y)"
wenzelm@41550
   763
  using assms by (auto simp add: lcm_neg1_int lcm_neg2_int) arith
huffman@31706
   764
nipkow@31952
   765
lemma lcm_ge_0_int [simp]: "lcm (x::int) y >= 0"
huffman@31706
   766
  by (simp add: lcm_int_def)
huffman@31706
   767
haftmann@54867
   768
lemma gcd_0_nat: "gcd (x::nat) 0 = x"
haftmann@23687
   769
  by simp
haftmann@23687
   770
wenzelm@61944
   771
lemma gcd_0_int [simp]: "gcd (x::int) 0 = \<bar>x\<bar>"
huffman@31706
   772
  by (unfold gcd_int_def, auto)
huffman@31706
   773
haftmann@54867
   774
lemma gcd_0_left_nat: "gcd 0 (x::nat) = x"
haftmann@23687
   775
  by simp
haftmann@23687
   776
wenzelm@61944
   777
lemma gcd_0_left_int [simp]: "gcd 0 (x::int) = \<bar>x\<bar>"
huffman@31706
   778
  by (unfold gcd_int_def, auto)
huffman@31706
   779
nipkow@31952
   780
lemma gcd_red_nat: "gcd (x::nat) y = gcd y (x mod y)"
huffman@31706
   781
  by (case_tac "y = 0", auto)
huffman@31706
   782
huffman@31706
   783
(* weaker, but useful for the simplifier *)
huffman@31706
   784
haftmann@62345
   785
lemma gcd_non_0_nat: "y \<noteq> (0::nat) \<Longrightarrow> gcd (x::nat) y = gcd y (x mod y)"
huffman@31706
   786
  by simp
huffman@31706
   787
nipkow@31952
   788
lemma gcd_1_nat [simp]: "gcd (m::nat) 1 = 1"
wenzelm@21263
   789
  by simp
wenzelm@21256
   790
nipkow@31952
   791
lemma gcd_Suc_0 [simp]: "gcd (m::nat) (Suc 0) = Suc 0"
haftmann@60690
   792
  by simp
huffman@31706
   793
nipkow@31952
   794
lemma gcd_1_int [simp]: "gcd (m::int) 1 = 1"
huffman@31706
   795
  by (simp add: gcd_int_def)
huffman@30082
   796
nipkow@31952
   797
lemma gcd_idem_nat: "gcd (x::nat) x = x"
nipkow@31798
   798
by simp
huffman@31706
   799
wenzelm@61944
   800
lemma gcd_idem_int: "gcd (x::int) x = \<bar>x\<bar>"
nipkow@31813
   801
by (auto simp add: gcd_int_def)
huffman@31706
   802
huffman@31706
   803
declare gcd_nat.simps [simp del]
wenzelm@21256
   804
wenzelm@60758
   805
text \<open>
wenzelm@61799
   806
  \medskip @{term "gcd m n"} divides \<open>m\<close> and \<open>n\<close>.  The
wenzelm@21256
   807
  conjunctions don't seem provable separately.
wenzelm@60758
   808
\<close>
wenzelm@21256
   809
haftmann@59008
   810
instance nat :: semiring_gcd
haftmann@59008
   811
proof
haftmann@59008
   812
  fix m n :: nat
haftmann@59008
   813
  show "gcd m n dvd m" and "gcd m n dvd n"
haftmann@59008
   814
  proof (induct m n rule: gcd_nat_induct)
haftmann@59008
   815
    fix m n :: nat
haftmann@59008
   816
    assume "gcd n (m mod n) dvd m mod n" and "gcd n (m mod n) dvd n"
haftmann@59008
   817
    then have "gcd n (m mod n) dvd m"
haftmann@59008
   818
      by (rule dvd_mod_imp_dvd)
haftmann@59008
   819
    moreover assume "0 < n"
haftmann@59008
   820
    ultimately show "gcd m n dvd m"
haftmann@59008
   821
      by (simp add: gcd_non_0_nat)
haftmann@59008
   822
  qed (simp_all add: gcd_0_nat gcd_non_0_nat)
haftmann@59008
   823
next
haftmann@59008
   824
  fix m n k :: nat
haftmann@59008
   825
  assume "k dvd m" and "k dvd n"
haftmann@59008
   826
  then show "k dvd gcd m n"
haftmann@59008
   827
    by (induct m n rule: gcd_nat_induct) (simp_all add: gcd_non_0_nat dvd_mod gcd_0_nat)
haftmann@60686
   828
qed (simp_all add: lcm_nat_def)
lp15@59667
   829
haftmann@59008
   830
instance int :: ring_gcd
haftmann@60686
   831
  by standard
haftmann@60686
   832
    (simp_all add: dvd_int_unfold_dvd_nat gcd_int_def lcm_int_def zdiv_int nat_abs_mult_distrib [symmetric] lcm_gcd gcd_greatest)
lp15@59667
   833
nipkow@31952
   834
lemma gcd_le1_nat [simp]: "a \<noteq> 0 \<Longrightarrow> gcd (a::nat) b \<le> a"
huffman@31706
   835
  by (rule dvd_imp_le, auto)
huffman@31706
   836
nipkow@31952
   837
lemma gcd_le2_nat [simp]: "b \<noteq> 0 \<Longrightarrow> gcd (a::nat) b \<le> b"
huffman@31706
   838
  by (rule dvd_imp_le, auto)
huffman@31706
   839
nipkow@31952
   840
lemma gcd_le1_int [simp]: "a > 0 \<Longrightarrow> gcd (a::int) b \<le> a"
huffman@31706
   841
  by (rule zdvd_imp_le, auto)
wenzelm@21256
   842
nipkow@31952
   843
lemma gcd_le2_int [simp]: "b > 0 \<Longrightarrow> gcd (a::int) b \<le> b"
huffman@31706
   844
  by (rule zdvd_imp_le, auto)
huffman@31706
   845
nipkow@31952
   846
lemma gcd_pos_nat [simp]: "(gcd (m::nat) n > 0) = (m ~= 0 | n ~= 0)"
haftmann@62344
   847
  by (insert gcd_eq_0_iff [of m n], arith)
wenzelm@21256
   848
nipkow@31952
   849
lemma gcd_pos_int [simp]: "(gcd (m::int) n > 0) = (m ~= 0 | n ~= 0)"
haftmann@62344
   850
  by (insert gcd_eq_0_iff [of m n], insert gcd_ge_0_int [of m n], arith)
huffman@31706
   851
nipkow@31952
   852
lemma gcd_unique_nat: "(d::nat) dvd a \<and> d dvd b \<and>
huffman@31706
   853
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
huffman@31706
   854
  apply auto
nipkow@33657
   855
  apply (rule dvd_antisym)
haftmann@59008
   856
  apply (erule (1) gcd_greatest)
huffman@31706
   857
  apply auto
huffman@31706
   858
done
wenzelm@21256
   859
nipkow@31952
   860
lemma gcd_unique_int: "d >= 0 & (d::int) dvd a \<and> d dvd b \<and>
huffman@31706
   861
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
nipkow@33657
   862
apply (case_tac "d = 0")
nipkow@33657
   863
 apply simp
nipkow@33657
   864
apply (rule iffI)
nipkow@33657
   865
 apply (rule zdvd_antisym_nonneg)
haftmann@59008
   866
 apply (auto intro: gcd_greatest)
huffman@31706
   867
done
huffman@30082
   868
haftmann@61913
   869
interpretation gcd_nat:
haftmann@62344
   870
  semilattice_neutr_order gcd "0::nat" Rings.dvd "\<lambda>m n. m dvd n \<and> m \<noteq> n"
haftmann@62344
   871
  by standard (auto simp add: gcd_unique_nat [symmetric] intro: dvd_antisym dvd_trans)
nipkow@31798
   872
wenzelm@61944
   873
lemma gcd_proj1_if_dvd_int [simp]: "x dvd y \<Longrightarrow> gcd (x::int) y = \<bar>x\<bar>"
haftmann@54867
   874
  by (metis abs_dvd_iff gcd_0_left_int gcd_abs_int gcd_unique_int)
nipkow@31798
   875
wenzelm@61944
   876
lemma gcd_proj2_if_dvd_int [simp]: "y dvd x \<Longrightarrow> gcd (x::int) y = \<bar>y\<bar>"
haftmann@62344
   877
  by (metis gcd_proj1_if_dvd_int gcd.commute)
nipkow@31798
   878
wenzelm@60758
   879
text \<open>
wenzelm@21256
   880
  \medskip Multiplication laws
wenzelm@60758
   881
\<close>
wenzelm@21256
   882
nipkow@31952
   883
lemma gcd_mult_distrib_nat: "(k::nat) * gcd m n = gcd (k * m) (k * n)"
wenzelm@61799
   884
    \<comment> \<open>@{cite \<open>page 27\<close> davenport92}\<close>
nipkow@31952
   885
  apply (induct m n rule: gcd_nat_induct)
huffman@31706
   886
  apply simp
wenzelm@21256
   887
  apply (case_tac "k = 0")
huffman@45270
   888
  apply (simp_all add: gcd_non_0_nat)
huffman@31706
   889
done
wenzelm@21256
   890
wenzelm@61944
   891
lemma gcd_mult_distrib_int: "\<bar>k::int\<bar> * gcd m n = gcd (k * m) (k * n)"
nipkow@31952
   892
  apply (subst (1 2) gcd_abs_int)
nipkow@31813
   893
  apply (subst (1 2) abs_mult)
nipkow@31952
   894
  apply (rule gcd_mult_distrib_nat [transferred])
huffman@31706
   895
  apply auto
huffman@31706
   896
done
wenzelm@21256
   897
haftmann@60690
   898
context semiring_gcd
haftmann@60690
   899
begin
wenzelm@21256
   900
haftmann@60690
   901
lemma coprime_dvd_mult:
haftmann@60690
   902
  assumes "coprime a b" and "a dvd c * b"
haftmann@60690
   903
  shows "a dvd c"
haftmann@60690
   904
proof (cases "c = 0")
haftmann@60690
   905
  case True then show ?thesis by simp
haftmann@60690
   906
next
haftmann@60690
   907
  case False
haftmann@60690
   908
  then have unit: "is_unit (unit_factor c)" by simp
haftmann@60690
   909
  from \<open>coprime a b\<close> mult_gcd_left [of c a b]
haftmann@60690
   910
  have "gcd (c * a) (c * b) * unit_factor c = c"
haftmann@60690
   911
    by (simp add: ac_simps)
haftmann@60690
   912
  moreover from \<open>a dvd c * b\<close> have "a dvd gcd (c * a) (c * b) * unit_factor c"
haftmann@60690
   913
    by (simp add: dvd_mult_unit_iff unit)
haftmann@60690
   914
  ultimately show ?thesis by simp
haftmann@60690
   915
qed
haftmann@60690
   916
haftmann@62344
   917
lemma coprime_dvd_mult_iff:
haftmann@62344
   918
  assumes "coprime a c"
haftmann@62344
   919
  shows "a dvd b * c \<longleftrightarrow> a dvd b"
haftmann@62344
   920
  using assms by (auto intro: coprime_dvd_mult)
haftmann@60690
   921
haftmann@60690
   922
lemma gcd_mult_cancel:
haftmann@60690
   923
  "coprime c b \<Longrightarrow> gcd (c * a) b = gcd a b"
haftmann@60690
   924
  apply (rule associated_eqI)
haftmann@59008
   925
  apply (rule gcd_greatest)
haftmann@60690
   926
  apply (rule_tac b = c in coprime_dvd_mult)
haftmann@60690
   927
  apply (simp add: gcd.assoc)
haftmann@60690
   928
  apply (simp_all add: ac_simps)
haftmann@60690
   929
  done
wenzelm@21256
   930
haftmann@62344
   931
lemma coprime_crossproduct:
haftmann@62344
   932
  fixes a b c d
haftmann@35368
   933
  assumes "coprime a d" and "coprime b c"
haftmann@62344
   934
  shows "normalize a * normalize c = normalize b * normalize d
haftmann@62344
   935
    \<longleftrightarrow> normalize a = normalize b \<and> normalize c = normalize d" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@35368
   936
proof
haftmann@35368
   937
  assume ?rhs then show ?lhs by simp
haftmann@35368
   938
next
haftmann@35368
   939
  assume ?lhs
haftmann@62344
   940
  from \<open>?lhs\<close> have "normalize a dvd normalize b * normalize d"
haftmann@62344
   941
    by (auto intro: dvdI dest: sym)
haftmann@62344
   942
  with \<open>coprime a d\<close> have "a dvd b"
haftmann@62344
   943
    by (simp add: coprime_dvd_mult_iff normalize_mult [symmetric])
haftmann@62344
   944
  from \<open>?lhs\<close> have "normalize b dvd normalize a * normalize c"
haftmann@62344
   945
    by (auto intro: dvdI dest: sym)
haftmann@62344
   946
  with \<open>coprime b c\<close> have "b dvd a"
haftmann@62344
   947
    by (simp add: coprime_dvd_mult_iff normalize_mult [symmetric])
haftmann@62344
   948
  from \<open>?lhs\<close> have "normalize c dvd normalize d * normalize b"
haftmann@62344
   949
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@62344
   950
  with \<open>coprime b c\<close> have "c dvd d"
haftmann@62344
   951
    by (simp add: coprime_dvd_mult_iff gcd.commute normalize_mult [symmetric])
haftmann@62344
   952
  from \<open>?lhs\<close> have "normalize d dvd normalize c * normalize a"
haftmann@62344
   953
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@62344
   954
  with \<open>coprime a d\<close> have "d dvd c"
haftmann@62344
   955
    by (simp add: coprime_dvd_mult_iff gcd.commute normalize_mult [symmetric])
haftmann@62344
   956
  from \<open>a dvd b\<close> \<open>b dvd a\<close> have "normalize a = normalize b"
haftmann@62344
   957
    by (rule associatedI)
haftmann@62344
   958
  moreover from \<open>c dvd d\<close> \<open>d dvd c\<close> have "normalize c = normalize d"
haftmann@62344
   959
    by (rule associatedI)
haftmann@35368
   960
  ultimately show ?rhs ..
haftmann@35368
   961
qed
haftmann@35368
   962
haftmann@62344
   963
end
haftmann@62344
   964
haftmann@62344
   965
lemma coprime_crossproduct_nat:
haftmann@62344
   966
  fixes a b c d :: nat
haftmann@62344
   967
  assumes "coprime a d" and "coprime b c"
haftmann@62344
   968
  shows "a * c = b * d \<longleftrightarrow> a = b \<and> c = d"
haftmann@62344
   969
  using assms coprime_crossproduct [of a d b c] by simp
haftmann@62344
   970
haftmann@35368
   971
lemma coprime_crossproduct_int:
haftmann@35368
   972
  fixes a b c d :: int
haftmann@35368
   973
  assumes "coprime a d" and "coprime b c"
haftmann@35368
   974
  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@62344
   975
  using assms coprime_crossproduct [of a d b c] by simp
haftmann@35368
   976
wenzelm@60758
   977
text \<open>\medskip Addition laws\<close>
wenzelm@21256
   978
nipkow@31952
   979
lemma gcd_add1_nat [simp]: "gcd ((m::nat) + n) n = gcd m n"
huffman@31706
   980
  apply (case_tac "n = 0")
nipkow@31952
   981
  apply (simp_all add: gcd_non_0_nat)
haftmann@62344
   982
  done
huffman@31706
   983
nipkow@31952
   984
lemma gcd_add2_nat [simp]: "gcd (m::nat) (m + n) = gcd m n"
haftmann@62344
   985
  apply (subst (1 2) gcd.commute)
haftmann@57512
   986
  apply (subst add.commute)
huffman@31706
   987
  apply simp
haftmann@62344
   988
  done
huffman@31706
   989
huffman@31706
   990
(* to do: add the other variations? *)
huffman@31706
   991
nipkow@31952
   992
lemma gcd_diff1_nat: "(m::nat) >= n \<Longrightarrow> gcd (m - n) n = gcd m n"
haftmann@62344
   993
  by (subst gcd_add1_nat [symmetric]) auto
huffman@31706
   994
nipkow@31952
   995
lemma gcd_diff2_nat: "(n::nat) >= m \<Longrightarrow> gcd (n - m) n = gcd m n"
haftmann@62344
   996
  apply (subst gcd.commute)
nipkow@31952
   997
  apply (subst gcd_diff1_nat [symmetric])
huffman@31706
   998
  apply auto
haftmann@62344
   999
  apply (subst gcd.commute)
nipkow@31952
  1000
  apply (subst gcd_diff1_nat)
huffman@31706
  1001
  apply assumption
haftmann@62344
  1002
  apply (rule gcd.commute)
haftmann@62344
  1003
  done
huffman@31706
  1004
nipkow@31952
  1005
lemma gcd_non_0_int: "(y::int) > 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
huffman@31706
  1006
  apply (frule_tac b = y and a = x in pos_mod_sign)
huffman@31706
  1007
  apply (simp del: pos_mod_sign add: gcd_int_def abs_if nat_mod_distrib)
nipkow@31952
  1008
  apply (auto simp add: gcd_non_0_nat nat_mod_distrib [symmetric]
huffman@31706
  1009
    zmod_zminus1_eq_if)
huffman@31706
  1010
  apply (frule_tac a = x in pos_mod_bound)
haftmann@62344
  1011
  apply (subst (1 2) gcd.commute)
nipkow@31952
  1012
  apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2_nat
huffman@31706
  1013
    nat_le_eq_zle)
haftmann@62344
  1014
  done
wenzelm@21256
  1015
nipkow@31952
  1016
lemma gcd_red_int: "gcd (x::int) y = gcd y (x mod y)"
huffman@31706
  1017
  apply (case_tac "y = 0")
huffman@31706
  1018
  apply force
huffman@31706
  1019
  apply (case_tac "y > 0")
nipkow@31952
  1020
  apply (subst gcd_non_0_int, auto)
nipkow@31952
  1021
  apply (insert gcd_non_0_int [of "-y" "-x"])
huffman@35216
  1022
  apply auto
huffman@31706
  1023
done
huffman@31706
  1024
nipkow@31952
  1025
lemma gcd_add1_int [simp]: "gcd ((m::int) + n) n = gcd m n"
haftmann@57512
  1026
by (metis gcd_red_int mod_add_self1 add.commute)
huffman@31706
  1027
nipkow@31952
  1028
lemma gcd_add2_int [simp]: "gcd m ((m::int) + n) = gcd m n"
haftmann@62344
  1029
by (metis gcd_add1_int gcd.commute add.commute)
wenzelm@21256
  1030
nipkow@31952
  1031
lemma gcd_add_mult_nat: "gcd (m::nat) (k * m + n) = gcd m n"
haftmann@62344
  1032
by (metis mod_mult_self3 gcd.commute gcd_red_nat)
wenzelm@21256
  1033
nipkow@31952
  1034
lemma gcd_add_mult_int: "gcd (m::int) (k * m + n) = gcd m n"
haftmann@62344
  1035
by (metis gcd.commute gcd_red_int mod_mult_self1 add.commute)
nipkow@31798
  1036
wenzelm@21256
  1037
huffman@31706
  1038
(* to do: differences, and all variations of addition rules
huffman@31706
  1039
    as simplification rules for nat and int *)
huffman@31706
  1040
haftmann@60689
  1041
lemma gcd_dvd_prod_nat: "gcd (m::nat) n dvd k * n"
haftmann@23687
  1042
  using mult_dvd_mono [of 1] by auto
chaieb@22027
  1043
huffman@31706
  1044
(* to do: add the three variations of these, and for ints? *)
huffman@31706
  1045
haftmann@62353
  1046
lemma finite_divisors_nat [simp]: -- \<open>FIXME move\<close>
haftmann@62353
  1047
  fixes m :: nat
haftmann@62353
  1048
  assumes "m > 0" 
haftmann@62353
  1049
  shows "finite {d. d dvd m}"
nipkow@31734
  1050
proof-
haftmann@62353
  1051
  from assms have "{d. d dvd m} \<subseteq> {d. d \<le> m}"
haftmann@62353
  1052
    by (auto dest: dvd_imp_le)
haftmann@62353
  1053
  then show ?thesis
haftmann@62353
  1054
    using finite_Collect_le_nat by (rule finite_subset)
nipkow@31734
  1055
qed
nipkow@31734
  1056
haftmann@62353
  1057
lemma finite_divisors_int [simp]:
haftmann@62353
  1058
  fixes i :: int
haftmann@62353
  1059
  assumes "i \<noteq> 0"
haftmann@62353
  1060
  shows "finite {d. d dvd i}"
haftmann@62353
  1061
proof -
haftmann@62353
  1062
  have "{d. \<bar>d\<bar> \<le> \<bar>i\<bar>} = {- \<bar>i\<bar>..\<bar>i\<bar>}"
haftmann@62353
  1063
    by (auto simp: abs_if)
haftmann@62353
  1064
  then have "finite {d. \<bar>d\<bar> <= \<bar>i\<bar>}"
haftmann@62353
  1065
    by simp
haftmann@62353
  1066
  from finite_subset [OF _ this] show ?thesis using assms
wenzelm@60512
  1067
    by (simp add: dvd_imp_le_int subset_iff)
nipkow@31734
  1068
qed
nipkow@31734
  1069
haftmann@62353
  1070
lemma Max_divisors_self_nat [simp]: "n\<noteq>0 \<Longrightarrow> Max{d::nat. d dvd n} = n"
nipkow@31995
  1071
apply(rule antisym)
nipkow@44890
  1072
 apply (fastforce intro: Max_le_iff[THEN iffD2] simp: dvd_imp_le)
nipkow@31995
  1073
apply simp
nipkow@31995
  1074
done
nipkow@31995
  1075
haftmann@62353
  1076
lemma Max_divisors_self_int [simp]: "n\<noteq>0 \<Longrightarrow> Max{d::int. d dvd n} = \<bar>n\<bar>"
nipkow@31995
  1077
apply(rule antisym)
haftmann@44278
  1078
 apply(rule Max_le_iff [THEN iffD2])
haftmann@44278
  1079
  apply (auto intro: abs_le_D1 dvd_imp_le_int)
nipkow@31995
  1080
done
nipkow@31995
  1081
nipkow@31734
  1082
lemma gcd_is_Max_divisors_nat:
haftmann@62353
  1083
  "m > 0 \<Longrightarrow> n > 0 \<Longrightarrow> gcd (m::nat) n = Max {d. d dvd m \<and> d dvd n}"
nipkow@31734
  1084
apply(rule Max_eqI[THEN sym])
nipkow@31995
  1085
  apply (metis finite_Collect_conjI finite_divisors_nat)
nipkow@31734
  1086
 apply simp
haftmann@62344
  1087
 apply(metis Suc_diff_1 Suc_neq_Zero dvd_imp_le gcd_greatest_iff gcd_pos_nat)
nipkow@31734
  1088
apply simp
nipkow@31734
  1089
done
nipkow@31734
  1090
nipkow@31734
  1091
lemma gcd_is_Max_divisors_int:
nipkow@31734
  1092
  "m ~= 0 ==> n ~= 0 ==> gcd (m::int) n = (Max {d. d dvd m & d dvd n})"
nipkow@31734
  1093
apply(rule Max_eqI[THEN sym])
nipkow@31995
  1094
  apply (metis finite_Collect_conjI finite_divisors_int)
nipkow@31734
  1095
 apply simp
haftmann@62344
  1096
 apply (metis gcd_greatest_iff gcd_pos_int zdvd_imp_le)
nipkow@31734
  1097
apply simp
nipkow@31734
  1098
done
nipkow@31734
  1099
haftmann@34030
  1100
lemma gcd_code_int [code]:
haftmann@34030
  1101
  "gcd k l = \<bar>if l = (0::int) then k else gcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
haftmann@34030
  1102
  by (simp add: gcd_int_def nat_mod_distrib gcd_non_0_nat)
haftmann@34030
  1103
chaieb@22027
  1104
wenzelm@60758
  1105
subsection \<open>Coprimality\<close>
huffman@31706
  1106
haftmann@60690
  1107
context semiring_gcd
haftmann@60690
  1108
begin
haftmann@60690
  1109
haftmann@60690
  1110
lemma div_gcd_coprime:
haftmann@60690
  1111
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
huffman@31706
  1112
  shows "coprime (a div gcd a b) (b div gcd a b)"
wenzelm@22367
  1113
proof -
haftmann@27556
  1114
  let ?g = "gcd a b"
chaieb@22027
  1115
  let ?a' = "a div ?g"
chaieb@22027
  1116
  let ?b' = "b div ?g"
haftmann@27556
  1117
  let ?g' = "gcd ?a' ?b'"
chaieb@22027
  1118
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
  1119
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
wenzelm@22367
  1120
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
  1121
      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
chaieb@22027
  1122
    unfolding dvd_def by blast
haftmann@58834
  1123
  from this [symmetric] have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'"
haftmann@58834
  1124
    by (simp_all add: mult.assoc mult.left_commute [of "gcd a b"])
wenzelm@22367
  1125
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
  1126
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
  1127
      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
huffman@35216
  1128
  have "?g \<noteq> 0" using nz by simp
haftmann@60690
  1129
  moreover from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
haftmann@60690
  1130
  thm dvd_mult_cancel_left
haftmann@60690
  1131
  ultimately show ?thesis using dvd_times_left_cancel_iff [of "gcd a b" _ 1] by simp
chaieb@22027
  1132
qed
chaieb@22027
  1133
haftmann@62345
  1134
lemma coprime:
haftmann@62345
  1135
  "coprime a b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62345
  1136
proof
haftmann@62345
  1137
  assume ?P then show ?Q by auto
haftmann@62345
  1138
next
haftmann@62345
  1139
  assume ?Q
haftmann@62345
  1140
  then have "is_unit (gcd a b) \<longleftrightarrow> gcd a b dvd a \<and> gcd a b dvd b"
haftmann@62345
  1141
    by blast
haftmann@62345
  1142
  then have "is_unit (gcd a b)"
haftmann@62345
  1143
    by simp
haftmann@62345
  1144
  then show ?P
haftmann@62345
  1145
    by simp
haftmann@62345
  1146
qed
haftmann@62345
  1147
haftmann@60690
  1148
end
haftmann@60690
  1149
haftmann@62345
  1150
lemma coprime_nat:
haftmann@62345
  1151
  "coprime (a::nat) b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
haftmann@62345
  1152
  using coprime [of a b] by simp
huffman@31706
  1153
nipkow@31952
  1154
lemma coprime_Suc_0_nat:
haftmann@62345
  1155
  "coprime (a::nat) b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = Suc 0)"
haftmann@60690
  1156
  using coprime_nat by simp
huffman@31706
  1157
haftmann@62345
  1158
lemma coprime_int:
haftmann@62345
  1159
  "coprime (a::int) b \<longleftrightarrow> (\<forall>d. d \<ge> 0 \<and> d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
nipkow@31952
  1160
  using gcd_unique_int [of 1 a b]
huffman@31706
  1161
  apply clarsimp
huffman@31706
  1162
  apply (erule subst)
huffman@31706
  1163
  apply (rule iffI)
huffman@31706
  1164
  apply force
lp15@61649
  1165
  using abs_dvd_iff abs_ge_zero apply blast
wenzelm@59807
  1166
  done
huffman@31706
  1167
nipkow@31952
  1168
lemma gcd_coprime_nat:
huffman@31706
  1169
  assumes z: "gcd (a::nat) b \<noteq> 0" and a: "a = a' * gcd a b" and
huffman@31706
  1170
    b: "b = b' * gcd a b"
huffman@31706
  1171
  shows    "coprime a' b'"
huffman@31706
  1172
huffman@31706
  1173
  apply (subgoal_tac "a' = a div gcd a b")
huffman@31706
  1174
  apply (erule ssubst)
huffman@31706
  1175
  apply (subgoal_tac "b' = b div gcd a b")
huffman@31706
  1176
  apply (erule ssubst)
haftmann@62344
  1177
  apply (rule div_gcd_coprime)
wenzelm@41550
  1178
  using z apply force
huffman@31706
  1179
  apply (subst (1) b)
huffman@31706
  1180
  using z apply force
huffman@31706
  1181
  apply (subst (1) a)
huffman@31706
  1182
  using z apply force
wenzelm@41550
  1183
  done
huffman@31706
  1184
nipkow@31952
  1185
lemma gcd_coprime_int:
huffman@31706
  1186
  assumes z: "gcd (a::int) b \<noteq> 0" and a: "a = a' * gcd a b" and
huffman@31706
  1187
    b: "b = b' * gcd a b"
huffman@31706
  1188
  shows    "coprime a' b'"
huffman@31706
  1189
  apply (subgoal_tac "a' = a div gcd a b")
huffman@31706
  1190
  apply (erule ssubst)
huffman@31706
  1191
  apply (subgoal_tac "b' = b div gcd a b")
huffman@31706
  1192
  apply (erule ssubst)
haftmann@62344
  1193
  apply (rule div_gcd_coprime)
wenzelm@41550
  1194
  using z apply force
huffman@31706
  1195
  apply (subst (1) b)
huffman@31706
  1196
  using z apply force
huffman@31706
  1197
  apply (subst (1) a)
huffman@31706
  1198
  using z apply force
wenzelm@41550
  1199
  done
huffman@31706
  1200
haftmann@60690
  1201
context semiring_gcd
haftmann@60690
  1202
begin
huffman@31706
  1203
haftmann@60690
  1204
lemma coprime_mult:
haftmann@60690
  1205
  assumes da: "coprime d a" and db: "coprime d b"
haftmann@60690
  1206
  shows "coprime d (a * b)"
haftmann@60690
  1207
  apply (subst gcd.commute)
haftmann@60690
  1208
  using da apply (subst gcd_mult_cancel)
haftmann@60690
  1209
  apply (subst gcd.commute, assumption)
haftmann@60690
  1210
  apply (subst gcd.commute, rule db)
haftmann@60690
  1211
  done
huffman@31706
  1212
haftmann@60690
  1213
end
haftmann@60690
  1214
nipkow@31952
  1215
lemma coprime_lmult_nat:
huffman@31706
  1216
  assumes dab: "coprime (d::nat) (a * b)" shows "coprime d a"
huffman@31706
  1217
proof -
huffman@31706
  1218
  have "gcd d a dvd gcd d (a * b)"
haftmann@59008
  1219
    by (rule gcd_greatest, auto)
huffman@31706
  1220
  with dab show ?thesis
huffman@31706
  1221
    by auto
huffman@31706
  1222
qed
huffman@31706
  1223
nipkow@31952
  1224
lemma coprime_lmult_int:
nipkow@31798
  1225
  assumes "coprime (d::int) (a * b)" shows "coprime d a"
huffman@31706
  1226
proof -
huffman@31706
  1227
  have "gcd d a dvd gcd d (a * b)"
haftmann@59008
  1228
    by (rule gcd_greatest, auto)
nipkow@31798
  1229
  with assms show ?thesis
huffman@31706
  1230
    by auto
huffman@31706
  1231
qed
huffman@31706
  1232
nipkow@31952
  1233
lemma coprime_rmult_nat:
nipkow@31798
  1234
  assumes "coprime (d::nat) (a * b)" shows "coprime d b"
huffman@31706
  1235
proof -
huffman@31706
  1236
  have "gcd d b dvd gcd d (a * b)"
haftmann@59008
  1237
    by (rule gcd_greatest, auto intro: dvd_mult)
nipkow@31798
  1238
  with assms show ?thesis
huffman@31706
  1239
    by auto
huffman@31706
  1240
qed
huffman@31706
  1241
nipkow@31952
  1242
lemma coprime_rmult_int:
huffman@31706
  1243
  assumes dab: "coprime (d::int) (a * b)" shows "coprime d b"
huffman@31706
  1244
proof -
huffman@31706
  1245
  have "gcd d b dvd gcd d (a * b)"
haftmann@59008
  1246
    by (rule gcd_greatest, auto intro: dvd_mult)
huffman@31706
  1247
  with dab show ?thesis
huffman@31706
  1248
    by auto
huffman@31706
  1249
qed
huffman@31706
  1250
nipkow@31952
  1251
lemma coprime_mul_eq_nat: "coprime (d::nat) (a * b) \<longleftrightarrow>
huffman@31706
  1252
    coprime d a \<and>  coprime d b"
nipkow@31952
  1253
  using coprime_rmult_nat[of d a b] coprime_lmult_nat[of d a b]
haftmann@62344
  1254
    coprime_mult [of d a b]
huffman@31706
  1255
  by blast
huffman@31706
  1256
nipkow@31952
  1257
lemma coprime_mul_eq_int: "coprime (d::int) (a * b) \<longleftrightarrow>
huffman@31706
  1258
    coprime d a \<and>  coprime d b"
nipkow@31952
  1259
  using coprime_rmult_int[of d a b] coprime_lmult_int[of d a b]
haftmann@62344
  1260
    coprime_mult [of d a b]
huffman@31706
  1261
  by blast
huffman@31706
  1262
noschinl@52397
  1263
lemma coprime_power_int:
noschinl@52397
  1264
  assumes "0 < n" shows "coprime (a :: int) (b ^ n) \<longleftrightarrow> coprime a b"
noschinl@52397
  1265
  using assms
noschinl@52397
  1266
proof (induct n)
noschinl@52397
  1267
  case (Suc n) then show ?case
noschinl@52397
  1268
    by (cases n) (simp_all add: coprime_mul_eq_int)
noschinl@52397
  1269
qed simp
noschinl@52397
  1270
nipkow@31952
  1271
lemma gcd_coprime_exists_nat:
huffman@31706
  1272
    assumes nz: "gcd (a::nat) b \<noteq> 0"
huffman@31706
  1273
    shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
huffman@31706
  1274
  apply (rule_tac x = "a div gcd a b" in exI)
huffman@31706
  1275
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@62344
  1276
  using nz apply (auto simp add: div_gcd_coprime dvd_div_mult)
huffman@31706
  1277
done
huffman@31706
  1278
nipkow@31952
  1279
lemma gcd_coprime_exists_int:
huffman@31706
  1280
    assumes nz: "gcd (a::int) b \<noteq> 0"
huffman@31706
  1281
    shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
huffman@31706
  1282
  apply (rule_tac x = "a div gcd a b" in exI)
huffman@31706
  1283
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@62344
  1284
  using nz apply (auto simp add: div_gcd_coprime)
huffman@31706
  1285
done
huffman@31706
  1286
nipkow@31952
  1287
lemma coprime_exp_nat: "coprime (d::nat) a \<Longrightarrow> coprime d (a^n)"
haftmann@62344
  1288
  by (induct n) (simp_all add: coprime_mult)
huffman@31706
  1289
nipkow@31952
  1290
lemma coprime_exp_int: "coprime (d::int) a \<Longrightarrow> coprime d (a^n)"
haftmann@62344
  1291
  by (induct n) (simp_all add: coprime_mult)
huffman@31706
  1292
haftmann@60690
  1293
context semiring_gcd
haftmann@60690
  1294
begin
haftmann@60690
  1295
haftmann@60690
  1296
lemma coprime_exp_left:
haftmann@60690
  1297
  assumes "coprime a b"
haftmann@60690
  1298
  shows "coprime (a ^ n) b"
haftmann@60690
  1299
  using assms by (induct n) (simp_all add: gcd_mult_cancel)
haftmann@60690
  1300
haftmann@60690
  1301
lemma coprime_exp2:
haftmann@60690
  1302
  assumes "coprime a b"
haftmann@60690
  1303
  shows "coprime (a ^ n) (b ^ m)"
haftmann@60690
  1304
proof (rule coprime_exp_left)
haftmann@60690
  1305
  from assms show "coprime a (b ^ m)"
haftmann@60690
  1306
    by (induct m) (simp_all add: gcd_mult_cancel gcd.commute [of a])
haftmann@60690
  1307
qed
haftmann@60690
  1308
haftmann@60690
  1309
end
haftmann@60690
  1310
haftmann@60690
  1311
lemma gcd_exp_nat:
haftmann@60690
  1312
  "gcd ((a :: nat) ^ n) (b ^ n) = gcd a b ^ n"
haftmann@60690
  1313
proof (cases "a = 0 \<and> b = 0")
haftmann@60690
  1314
  case True then show ?thesis by (cases "n > 0") (simp_all add: zero_power)
haftmann@60690
  1315
next
haftmann@60690
  1316
  case False
haftmann@60690
  1317
  then have "coprime (a div gcd a b) (b div gcd a b)"
haftmann@60690
  1318
    by (auto simp: div_gcd_coprime)
haftmann@60690
  1319
  then have "coprime ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
haftmann@60690
  1320
    by (simp add: coprime_exp2)
haftmann@60690
  1321
  then have "gcd ((a div gcd a b)^n * (gcd a b)^n)
huffman@31706
  1322
      ((b div gcd a b)^n * (gcd a b)^n) = (gcd a b)^n"
lp15@60162
  1323
    by (metis gcd_mult_distrib_nat mult.commute mult.right_neutral)
huffman@31706
  1324
  also have "(a div gcd a b)^n * (gcd a b)^n = a^n"
lp15@60162
  1325
    by (metis dvd_div_mult_self gcd_unique_nat power_mult_distrib)
huffman@31706
  1326
  also have "(b div gcd a b)^n * (gcd a b)^n = b^n"
lp15@60162
  1327
    by (metis dvd_div_mult_self gcd_unique_nat power_mult_distrib)
huffman@31706
  1328
  finally show ?thesis .
huffman@31706
  1329
qed
huffman@31706
  1330
nipkow@31952
  1331
lemma gcd_exp_int: "gcd ((a::int)^n) (b^n) = (gcd a b)^n"
nipkow@31952
  1332
  apply (subst (1 2) gcd_abs_int)
huffman@31706
  1333
  apply (subst (1 2) power_abs)
nipkow@31952
  1334
  apply (rule gcd_exp_nat [where n = n, transferred])
huffman@31706
  1335
  apply auto
huffman@31706
  1336
done
huffman@31706
  1337
nipkow@31952
  1338
lemma division_decomp_nat: assumes dc: "(a::nat) dvd b * c"
huffman@31706
  1339
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
huffman@31706
  1340
proof-
huffman@31706
  1341
  let ?g = "gcd a b"
huffman@31706
  1342
  {assume "?g = 0" with dc have ?thesis by auto}
huffman@31706
  1343
  moreover
huffman@31706
  1344
  {assume z: "?g \<noteq> 0"
nipkow@31952
  1345
    from gcd_coprime_exists_nat[OF z]
huffman@31706
  1346
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1347
      by blast
huffman@31706
  1348
    have thb: "?g dvd b" by auto
huffman@31706
  1349
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast
huffman@31706
  1350
    with dc have th0: "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
huffman@31706
  1351
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
haftmann@57512
  1352
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: mult.assoc)
huffman@31706
  1353
    with z have th_1: "a' dvd b' * c" by auto
haftmann@62344
  1354
    from coprime_dvd_mult [OF ab'(3)] th_1
haftmann@57512
  1355
    have thc: "a' dvd c" by (subst (asm) mult.commute, blast)
huffman@31706
  1356
    from ab' have "a = ?g*a'" by algebra
huffman@31706
  1357
    with thb thc have ?thesis by blast }
huffman@31706
  1358
  ultimately show ?thesis by blast
huffman@31706
  1359
qed
huffman@31706
  1360
nipkow@31952
  1361
lemma division_decomp_int: assumes dc: "(a::int) dvd b * c"
huffman@31706
  1362
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
huffman@31706
  1363
proof-
huffman@31706
  1364
  let ?g = "gcd a b"
huffman@31706
  1365
  {assume "?g = 0" with dc have ?thesis by auto}
huffman@31706
  1366
  moreover
huffman@31706
  1367
  {assume z: "?g \<noteq> 0"
nipkow@31952
  1368
    from gcd_coprime_exists_int[OF z]
huffman@31706
  1369
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1370
      by blast
huffman@31706
  1371
    have thb: "?g dvd b" by auto
huffman@31706
  1372
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast
huffman@31706
  1373
    with dc have th0: "a' dvd b*c"
huffman@31706
  1374
      using dvd_trans[of a' a "b*c"] by simp
huffman@31706
  1375
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
haftmann@60690
  1376
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: ac_simps)
huffman@31706
  1377
    with z have th_1: "a' dvd b' * c" by auto
haftmann@62344
  1378
    from coprime_dvd_mult [OF ab'(3)] th_1
haftmann@57512
  1379
    have thc: "a' dvd c" by (subst (asm) mult.commute, blast)
huffman@31706
  1380
    from ab' have "a = ?g*a'" by algebra
huffman@31706
  1381
    with thb thc have ?thesis by blast }
huffman@31706
  1382
  ultimately show ?thesis by blast
chaieb@27669
  1383
qed
chaieb@27669
  1384
nipkow@31952
  1385
lemma pow_divides_pow_nat:
huffman@31706
  1386
  assumes ab: "(a::nat) ^ n dvd b ^n" and n:"n \<noteq> 0"
huffman@31706
  1387
  shows "a dvd b"
huffman@31706
  1388
proof-
huffman@31706
  1389
  let ?g = "gcd a b"
huffman@31706
  1390
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
huffman@31706
  1391
  {assume "?g = 0" with ab n have ?thesis by auto }
huffman@31706
  1392
  moreover
huffman@31706
  1393
  {assume z: "?g \<noteq> 0"
huffman@35216
  1394
    hence zn: "?g ^ n \<noteq> 0" using n by simp
nipkow@31952
  1395
    from gcd_coprime_exists_nat[OF z]
huffman@31706
  1396
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1397
      by blast
huffman@31706
  1398
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n"
huffman@31706
  1399
      by (simp add: ab'(1,2)[symmetric])
huffman@31706
  1400
    hence "?g^n*a'^n dvd ?g^n *b'^n"
haftmann@57512
  1401
      by (simp only: power_mult_distrib mult.commute)
haftmann@58787
  1402
    then have th0: "a'^n dvd b'^n"
haftmann@58787
  1403
      using zn by auto
huffman@31706
  1404
    have "a' dvd a'^n" by (simp add: m)
huffman@31706
  1405
    with th0 have "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by simp
haftmann@57512
  1406
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult.commute)
haftmann@62344
  1407
    from coprime_dvd_mult [OF coprime_exp_nat [OF ab'(3), of m]] th1
haftmann@57512
  1408
    have "a' dvd b'" by (subst (asm) mult.commute, blast)
huffman@31706
  1409
    hence "a'*?g dvd b'*?g" by simp
huffman@31706
  1410
    with ab'(1,2)  have ?thesis by simp }
huffman@31706
  1411
  ultimately show ?thesis by blast
huffman@31706
  1412
qed
huffman@31706
  1413
nipkow@31952
  1414
lemma pow_divides_pow_int:
huffman@31706
  1415
  assumes ab: "(a::int) ^ n dvd b ^n" and n:"n \<noteq> 0"
huffman@31706
  1416
  shows "a dvd b"
chaieb@27669
  1417
proof-
huffman@31706
  1418
  let ?g = "gcd a b"
huffman@31706
  1419
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
huffman@31706
  1420
  {assume "?g = 0" with ab n have ?thesis by auto }
huffman@31706
  1421
  moreover
huffman@31706
  1422
  {assume z: "?g \<noteq> 0"
huffman@35216
  1423
    hence zn: "?g ^ n \<noteq> 0" using n by simp
nipkow@31952
  1424
    from gcd_coprime_exists_int[OF z]
huffman@31706
  1425
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1426
      by blast
huffman@31706
  1427
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n"
huffman@31706
  1428
      by (simp add: ab'(1,2)[symmetric])
huffman@31706
  1429
    hence "?g^n*a'^n dvd ?g^n *b'^n"
haftmann@57512
  1430
      by (simp only: power_mult_distrib mult.commute)
huffman@31706
  1431
    with zn z n have th0:"a'^n dvd b'^n" by auto
huffman@31706
  1432
    have "a' dvd a'^n" by (simp add: m)
huffman@31706
  1433
    with th0 have "a' dvd b'^n"
huffman@31706
  1434
      using dvd_trans[of a' "a'^n" "b'^n"] by simp
haftmann@60596
  1435
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult.commute)
haftmann@62344
  1436
    from coprime_dvd_mult [OF coprime_exp_int [OF ab'(3), of m]] th1
haftmann@57512
  1437
    have "a' dvd b'" by (subst (asm) mult.commute, blast)
huffman@31706
  1438
    hence "a'*?g dvd b'*?g" by simp
huffman@31706
  1439
    with ab'(1,2)  have ?thesis by simp }
huffman@31706
  1440
  ultimately show ?thesis by blast
huffman@31706
  1441
qed
huffman@31706
  1442
haftmann@62353
  1443
lemma pow_divides_eq_nat [simp]:
haftmann@62353
  1444
  "n > 0 \<Longrightarrow> (a::nat) ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
nipkow@31952
  1445
  by (auto intro: pow_divides_pow_nat dvd_power_same)
huffman@31706
  1446
haftmann@62353
  1447
lemma pow_divides_eq_int [simp]:
haftmann@62353
  1448
  "n ~= 0 \<Longrightarrow> (a::int) ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
nipkow@31952
  1449
  by (auto intro: pow_divides_pow_int dvd_power_same)
huffman@31706
  1450
haftmann@62353
  1451
context semiring_gcd
haftmann@62353
  1452
begin
haftmann@62353
  1453
haftmann@62353
  1454
lemma divides_mult:
haftmann@62353
  1455
  assumes "a dvd c" and nr: "b dvd c" and "coprime a b"
haftmann@62353
  1456
  shows "a * b dvd c"
huffman@31706
  1457
proof-
haftmann@62353
  1458
  from \<open>b dvd c\<close> obtain b' where"c = b * b'" ..
haftmann@62353
  1459
  with \<open>a dvd c\<close> have "a dvd b' * b"
haftmann@62353
  1460
    by (simp add: ac_simps)
haftmann@62353
  1461
  with \<open>coprime a b\<close> have "a dvd b'"
haftmann@62353
  1462
    by (simp add: coprime_dvd_mult_iff)
haftmann@62353
  1463
  then obtain a' where "b' = a * a'" ..
haftmann@62353
  1464
  with \<open>c = b * b'\<close> have "c = (a * b) * a'"
haftmann@62353
  1465
    by (simp add: ac_simps)
haftmann@62353
  1466
  then show ?thesis ..
huffman@31706
  1467
qed
huffman@31706
  1468
haftmann@62353
  1469
end
chaieb@27669
  1470
nipkow@31952
  1471
lemma coprime_plus_one_nat [simp]: "coprime ((n::nat) + 1) n"
haftmann@60690
  1472
  by (simp add: gcd.commute del: One_nat_def)
huffman@31706
  1473
nipkow@31952
  1474
lemma coprime_Suc_nat [simp]: "coprime (Suc n) n"
haftmann@60690
  1475
  using coprime_plus_one_nat by simp
huffman@31706
  1476
nipkow@31952
  1477
lemma coprime_plus_one_int [simp]: "coprime ((n::int) + 1) n"
haftmann@60686
  1478
  by (simp add: gcd.commute)
huffman@31706
  1479
nipkow@31952
  1480
lemma coprime_minus_one_nat: "(n::nat) \<noteq> 0 \<Longrightarrow> coprime (n - 1) n"
nipkow@31952
  1481
  using coprime_plus_one_nat [of "n - 1"]
haftmann@62344
  1482
    gcd.commute [of "n - 1" n] by auto
huffman@31706
  1483
nipkow@31952
  1484
lemma coprime_minus_one_int: "coprime ((n::int) - 1) n"
nipkow@31952
  1485
  using coprime_plus_one_int [of "n - 1"]
haftmann@62344
  1486
    gcd.commute [of "n - 1" n] by auto
huffman@31706
  1487
haftmann@62344
  1488
lemma setprod_coprime_nat:
haftmann@62344
  1489
  fixes x :: nat
haftmann@62344
  1490
  shows "(\<And>i. i \<in> A \<Longrightarrow> coprime (f i) x) \<Longrightarrow> coprime (\<Prod>i\<in>A. f i) x"
haftmann@62344
  1491
  by (induct A rule: infinite_finite_induct)
haftmann@62344
  1492
    (auto simp add: gcd_mult_cancel One_nat_def [symmetric] simp del: One_nat_def)
huffman@31706
  1493
haftmann@62344
  1494
lemma setprod_coprime_int:
haftmann@62344
  1495
  fixes x :: int
haftmann@62344
  1496
  shows "(\<And>i. i \<in> A \<Longrightarrow> coprime (f i) x) \<Longrightarrow> coprime (\<Prod>i\<in>A. f i) x"
haftmann@62344
  1497
  by (induct A rule: infinite_finite_induct)
haftmann@62344
  1498
    (auto simp add: gcd_mult_cancel)
huffman@31706
  1499
lp15@60162
  1500
lemma coprime_common_divisor_nat: 
haftmann@60686
  1501
  "coprime (a::nat) b \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> x = 1"
haftmann@62344
  1502
  by (metis gcd_greatest_iff nat_dvd_1_iff_1)
huffman@31706
  1503
lp15@60162
  1504
lemma coprime_common_divisor_int:
wenzelm@61944
  1505
  "coprime (a::int) b \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> \<bar>x\<bar> = 1"
haftmann@60686
  1506
  using gcd_greatest_iff [of x a b] by auto
huffman@31706
  1507
lp15@60162
  1508
lemma coprime_divisors_nat:
lp15@60162
  1509
    "(d::int) dvd a \<Longrightarrow> e dvd b \<Longrightarrow> coprime a b \<Longrightarrow> coprime d e"
lp15@60162
  1510
  by (meson coprime_int dvd_trans gcd_dvd1 gcd_dvd2 gcd_ge_0_int)
huffman@31706
  1511
nipkow@31952
  1512
lemma invertible_coprime_nat: "(x::nat) * y mod m = 1 \<Longrightarrow> coprime x m"
haftmann@62344
  1513
by (metis coprime_lmult_nat gcd_1_nat gcd.commute gcd_red_nat)
huffman@31706
  1514
nipkow@31952
  1515
lemma invertible_coprime_int: "(x::int) * y mod m = 1 \<Longrightarrow> coprime x m"
haftmann@62344
  1516
by (metis coprime_lmult_int gcd_1_int gcd.commute gcd_red_int)
huffman@31706
  1517
huffman@31706
  1518
wenzelm@60758
  1519
subsection \<open>Bezout's theorem\<close>
huffman@31706
  1520
huffman@31706
  1521
(* Function bezw returns a pair of witnesses to Bezout's theorem --
huffman@31706
  1522
   see the theorems that follow the definition. *)
huffman@31706
  1523
fun
huffman@31706
  1524
  bezw  :: "nat \<Rightarrow> nat \<Rightarrow> int * int"
huffman@31706
  1525
where
huffman@31706
  1526
  "bezw x y =
huffman@31706
  1527
  (if y = 0 then (1, 0) else
huffman@31706
  1528
      (snd (bezw y (x mod y)),
huffman@31706
  1529
       fst (bezw y (x mod y)) - snd (bezw y (x mod y)) * int(x div y)))"
huffman@31706
  1530
huffman@31706
  1531
lemma bezw_0 [simp]: "bezw x 0 = (1, 0)" by simp
huffman@31706
  1532
huffman@31706
  1533
lemma bezw_non_0: "y > 0 \<Longrightarrow> bezw x y = (snd (bezw y (x mod y)),
huffman@31706
  1534
       fst (bezw y (x mod y)) - snd (bezw y (x mod y)) * int(x div y))"
huffman@31706
  1535
  by simp
huffman@31706
  1536
huffman@31706
  1537
declare bezw.simps [simp del]
huffman@31706
  1538
huffman@31706
  1539
lemma bezw_aux [rule_format]:
huffman@31706
  1540
    "fst (bezw x y) * int x + snd (bezw x y) * int y = int (gcd x y)"
nipkow@31952
  1541
proof (induct x y rule: gcd_nat_induct)
huffman@31706
  1542
  fix m :: nat
huffman@31706
  1543
  show "fst (bezw m 0) * int m + snd (bezw m 0) * int 0 = int (gcd m 0)"
huffman@31706
  1544
    by auto
huffman@31706
  1545
  next fix m :: nat and n
huffman@31706
  1546
    assume ngt0: "n > 0" and
huffman@31706
  1547
      ih: "fst (bezw n (m mod n)) * int n +
huffman@31706
  1548
        snd (bezw n (m mod n)) * int (m mod n) =
huffman@31706
  1549
        int (gcd n (m mod n))"
huffman@31706
  1550
    thus "fst (bezw m n) * int m + snd (bezw m n) * int n = int (gcd m n)"
nipkow@31952
  1551
      apply (simp add: bezw_non_0 gcd_non_0_nat)
huffman@31706
  1552
      apply (erule subst)
haftmann@36350
  1553
      apply (simp add: field_simps)
huffman@31706
  1554
      apply (subst mod_div_equality [of m n, symmetric])
huffman@31706
  1555
      (* applying simp here undoes the last substitution!
huffman@31706
  1556
         what is procedure cancel_div_mod? *)
hoelzl@58776
  1557
      apply (simp only: NO_MATCH_def field_simps of_nat_add of_nat_mult)
huffman@31706
  1558
      done
huffman@31706
  1559
qed
huffman@31706
  1560
nipkow@31952
  1561
lemma bezout_int:
huffman@31706
  1562
  fixes x y
huffman@31706
  1563
  shows "EX u v. u * (x::int) + v * y = gcd x y"
huffman@31706
  1564
proof -
huffman@31706
  1565
  have bezout_aux: "!!x y. x \<ge> (0::int) \<Longrightarrow> y \<ge> 0 \<Longrightarrow>
huffman@31706
  1566
      EX u v. u * x + v * y = gcd x y"
huffman@31706
  1567
    apply (rule_tac x = "fst (bezw (nat x) (nat y))" in exI)
huffman@31706
  1568
    apply (rule_tac x = "snd (bezw (nat x) (nat y))" in exI)
huffman@31706
  1569
    apply (unfold gcd_int_def)
huffman@31706
  1570
    apply simp
huffman@31706
  1571
    apply (subst bezw_aux [symmetric])
huffman@31706
  1572
    apply auto
huffman@31706
  1573
    done
huffman@31706
  1574
  have "(x \<ge> 0 \<and> y \<ge> 0) | (x \<ge> 0 \<and> y \<le> 0) | (x \<le> 0 \<and> y \<ge> 0) |
huffman@31706
  1575
      (x \<le> 0 \<and> y \<le> 0)"
huffman@31706
  1576
    by auto
huffman@31706
  1577
  moreover have "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> ?thesis"
huffman@31706
  1578
    by (erule (1) bezout_aux)
huffman@31706
  1579
  moreover have "x >= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> ?thesis"
huffman@31706
  1580
    apply (insert bezout_aux [of x "-y"])
huffman@31706
  1581
    apply auto
huffman@31706
  1582
    apply (rule_tac x = u in exI)
huffman@31706
  1583
    apply (rule_tac x = "-v" in exI)
nipkow@31952
  1584
    apply (subst gcd_neg2_int [symmetric])
huffman@31706
  1585
    apply auto
huffman@31706
  1586
    done
huffman@31706
  1587
  moreover have "x <= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> ?thesis"
huffman@31706
  1588
    apply (insert bezout_aux [of "-x" y])
huffman@31706
  1589
    apply auto
huffman@31706
  1590
    apply (rule_tac x = "-u" in exI)
huffman@31706
  1591
    apply (rule_tac x = v in exI)
nipkow@31952
  1592
    apply (subst gcd_neg1_int [symmetric])
huffman@31706
  1593
    apply auto
huffman@31706
  1594
    done
huffman@31706
  1595
  moreover have "x <= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> ?thesis"
huffman@31706
  1596
    apply (insert bezout_aux [of "-x" "-y"])
huffman@31706
  1597
    apply auto
huffman@31706
  1598
    apply (rule_tac x = "-u" in exI)
huffman@31706
  1599
    apply (rule_tac x = "-v" in exI)
nipkow@31952
  1600
    apply (subst gcd_neg1_int [symmetric])
nipkow@31952
  1601
    apply (subst gcd_neg2_int [symmetric])
huffman@31706
  1602
    apply auto
huffman@31706
  1603
    done
huffman@31706
  1604
  ultimately show ?thesis by blast
huffman@31706
  1605
qed
huffman@31706
  1606
wenzelm@60758
  1607
text \<open>versions of Bezout for nat, by Amine Chaieb\<close>
huffman@31706
  1608
huffman@31706
  1609
lemma ind_euclid:
huffman@31706
  1610
  assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0"
huffman@31706
  1611
  and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)"
chaieb@27669
  1612
  shows "P a b"
berghofe@34915
  1613
proof(induct "a + b" arbitrary: a b rule: less_induct)
berghofe@34915
  1614
  case less
chaieb@27669
  1615
  have "a = b \<or> a < b \<or> b < a" by arith
chaieb@27669
  1616
  moreover {assume eq: "a= b"
huffman@31706
  1617
    from add[rule_format, OF z[rule_format, of a]] have "P a b" using eq
huffman@31706
  1618
    by simp}
chaieb@27669
  1619
  moreover
chaieb@27669
  1620
  {assume lt: "a < b"
berghofe@34915
  1621
    hence "a + b - a < a + b \<or> a = 0" by arith
chaieb@27669
  1622
    moreover
chaieb@27669
  1623
    {assume "a =0" with z c have "P a b" by blast }
chaieb@27669
  1624
    moreover
berghofe@34915
  1625
    {assume "a + b - a < a + b"
berghofe@34915
  1626
      also have th0: "a + b - a = a + (b - a)" using lt by arith
berghofe@34915
  1627
      finally have "a + (b - a) < a + b" .
berghofe@34915
  1628
      then have "P a (a + (b - a))" by (rule add[rule_format, OF less])
berghofe@34915
  1629
      then have "P a b" by (simp add: th0[symmetric])}
chaieb@27669
  1630
    ultimately have "P a b" by blast}
chaieb@27669
  1631
  moreover
chaieb@27669
  1632
  {assume lt: "a > b"
berghofe@34915
  1633
    hence "b + a - b < a + b \<or> b = 0" by arith
chaieb@27669
  1634
    moreover
chaieb@27669
  1635
    {assume "b =0" with z c have "P a b" by blast }
chaieb@27669
  1636
    moreover
berghofe@34915
  1637
    {assume "b + a - b < a + b"
berghofe@34915
  1638
      also have th0: "b + a - b = b + (a - b)" using lt by arith
berghofe@34915
  1639
      finally have "b + (a - b) < a + b" .
berghofe@34915
  1640
      then have "P b (b + (a - b))" by (rule add[rule_format, OF less])
berghofe@34915
  1641
      then have "P b a" by (simp add: th0[symmetric])
chaieb@27669
  1642
      hence "P a b" using c by blast }
chaieb@27669
  1643
    ultimately have "P a b" by blast}
chaieb@27669
  1644
ultimately  show "P a b" by blast
chaieb@27669
  1645
qed
chaieb@27669
  1646
nipkow@31952
  1647
lemma bezout_lemma_nat:
huffman@31706
  1648
  assumes ex: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  1649
    (a * x = b * y + d \<or> b * x = a * y + d)"
huffman@31706
  1650
  shows "\<exists>d x y. d dvd a \<and> d dvd a + b \<and>
huffman@31706
  1651
    (a * x = (a + b) * y + d \<or> (a + b) * x = a * y + d)"
huffman@31706
  1652
  using ex
huffman@31706
  1653
  apply clarsimp
huffman@35216
  1654
  apply (rule_tac x="d" in exI, simp)
huffman@31706
  1655
  apply (case_tac "a * x = b * y + d" , simp_all)
huffman@31706
  1656
  apply (rule_tac x="x + y" in exI)
huffman@31706
  1657
  apply (rule_tac x="y" in exI)
huffman@31706
  1658
  apply algebra
huffman@31706
  1659
  apply (rule_tac x="x" in exI)
huffman@31706
  1660
  apply (rule_tac x="x + y" in exI)
huffman@31706
  1661
  apply algebra
chaieb@27669
  1662
done
chaieb@27669
  1663
nipkow@31952
  1664
lemma bezout_add_nat: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  1665
    (a * x = b * y + d \<or> b * x = a * y + d)"
huffman@31706
  1666
  apply(induct a b rule: ind_euclid)
huffman@31706
  1667
  apply blast
huffman@31706
  1668
  apply clarify
huffman@35216
  1669
  apply (rule_tac x="a" in exI, simp)
huffman@31706
  1670
  apply clarsimp
huffman@31706
  1671
  apply (rule_tac x="d" in exI)
huffman@35216
  1672
  apply (case_tac "a * x = b * y + d", simp_all)
huffman@31706
  1673
  apply (rule_tac x="x+y" in exI)
huffman@31706
  1674
  apply (rule_tac x="y" in exI)
huffman@31706
  1675
  apply algebra
huffman@31706
  1676
  apply (rule_tac x="x" in exI)
huffman@31706
  1677
  apply (rule_tac x="x+y" in exI)
huffman@31706
  1678
  apply algebra
chaieb@27669
  1679
done
chaieb@27669
  1680
nipkow@31952
  1681
lemma bezout1_nat: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and>
huffman@31706
  1682
    (a * x - b * y = d \<or> b * x - a * y = d)"
nipkow@31952
  1683
  using bezout_add_nat[of a b]
huffman@31706
  1684
  apply clarsimp
huffman@31706
  1685
  apply (rule_tac x="d" in exI, simp)
huffman@31706
  1686
  apply (rule_tac x="x" in exI)
huffman@31706
  1687
  apply (rule_tac x="y" in exI)
huffman@31706
  1688
  apply auto
chaieb@27669
  1689
done
chaieb@27669
  1690
nipkow@31952
  1691
lemma bezout_add_strong_nat: assumes nz: "a \<noteq> (0::nat)"
chaieb@27669
  1692
  shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
chaieb@27669
  1693
proof-
huffman@31706
  1694
 from nz have ap: "a > 0" by simp
nipkow@31952
  1695
 from bezout_add_nat[of a b]
huffman@31706
  1696
 have "(\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d) \<or>
huffman@31706
  1697
   (\<exists>d x y. d dvd a \<and> d dvd b \<and> b * x = a * y + d)" by blast
chaieb@27669
  1698
 moreover
huffman@31706
  1699
    {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
huffman@31706
  1700
     from H have ?thesis by blast }
chaieb@27669
  1701
 moreover
chaieb@27669
  1702
 {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
chaieb@27669
  1703
   {assume b0: "b = 0" with H  have ?thesis by simp}
huffman@31706
  1704
   moreover
chaieb@27669
  1705
   {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
huffman@31706
  1706
     from b dvd_imp_le [OF H(2)] have "d < b \<or> d = b"
huffman@31706
  1707
       by auto
chaieb@27669
  1708
     moreover
chaieb@27669
  1709
     {assume db: "d=b"
wenzelm@41550
  1710
       with nz H have ?thesis apply simp
wenzelm@32960
  1711
         apply (rule exI[where x = b], simp)
wenzelm@32960
  1712
         apply (rule exI[where x = b])
wenzelm@32960
  1713
        by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
chaieb@27669
  1714
    moreover
huffman@31706
  1715
    {assume db: "d < b"
wenzelm@41550
  1716
        {assume "x=0" hence ?thesis using nz H by simp }
wenzelm@32960
  1717
        moreover
wenzelm@32960
  1718
        {assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
wenzelm@32960
  1719
          from db have "d \<le> b - 1" by simp
wenzelm@32960
  1720
          hence "d*b \<le> b*(b - 1)" by simp
wenzelm@32960
  1721
          with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
wenzelm@32960
  1722
          have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
wenzelm@32960
  1723
          from H (3) have "d + (b - 1) * (b*x) = d + (b - 1) * (a*y + d)"
huffman@31706
  1724
            by simp
wenzelm@32960
  1725
          hence "d + (b - 1) * a * y + (b - 1) * d = d + (b - 1) * b * x"
haftmann@57512
  1726
            by (simp only: mult.assoc distrib_left)
wenzelm@32960
  1727
          hence "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)"
huffman@31706
  1728
            by algebra
wenzelm@32960
  1729
          hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
wenzelm@32960
  1730
          hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)"
wenzelm@32960
  1731
            by (simp only: diff_add_assoc[OF dble, of d, symmetric])
wenzelm@32960
  1732
          hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
haftmann@59008
  1733
            by (simp only: diff_mult_distrib2 ac_simps)
wenzelm@32960
  1734
          hence ?thesis using H(1,2)
wenzelm@32960
  1735
            apply -
wenzelm@32960
  1736
            apply (rule exI[where x=d], simp)
wenzelm@32960
  1737
            apply (rule exI[where x="(b - 1) * y"])
wenzelm@32960
  1738
            by (rule exI[where x="x*(b - 1) - d"], simp)}
wenzelm@32960
  1739
        ultimately have ?thesis by blast}
chaieb@27669
  1740
    ultimately have ?thesis by blast}
chaieb@27669
  1741
  ultimately have ?thesis by blast}
chaieb@27669
  1742
 ultimately show ?thesis by blast
chaieb@27669
  1743
qed
chaieb@27669
  1744
nipkow@31952
  1745
lemma bezout_nat: assumes a: "(a::nat) \<noteq> 0"
chaieb@27669
  1746
  shows "\<exists>x y. a * x = b * y + gcd a b"
chaieb@27669
  1747
proof-
chaieb@27669
  1748
  let ?g = "gcd a b"
nipkow@31952
  1749
  from bezout_add_strong_nat[OF a, of b]
chaieb@27669
  1750
  obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
chaieb@27669
  1751
  from d(1,2) have "d dvd ?g" by simp
chaieb@27669
  1752
  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
huffman@31706
  1753
  from d(3) have "a * x * k = (b * y + d) *k " by auto
chaieb@27669
  1754
  hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
chaieb@27669
  1755
  thus ?thesis by blast
chaieb@27669
  1756
qed
chaieb@27669
  1757
huffman@31706
  1758
haftmann@62345
  1759
subsection \<open>LCM properties  on @{typ nat} and @{typ int}\<close>
huffman@31706
  1760
wenzelm@61944
  1761
lemma lcm_altdef_int [code]: "lcm (a::int) b = \<bar>a\<bar> * \<bar>b\<bar> div gcd a b"
haftmann@62344
  1762
  by (simp add: lcm_int_def lcm_nat_def zdiv_int gcd_int_def)
huffman@31706
  1763
nipkow@31952
  1764
lemma prod_gcd_lcm_nat: "(m::nat) * n = gcd m n * lcm m n"
huffman@31706
  1765
  unfolding lcm_nat_def
nipkow@31952
  1766
  by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod_nat])
huffman@31706
  1767
wenzelm@61944
  1768
lemma prod_gcd_lcm_int: "\<bar>m::int\<bar> * \<bar>n\<bar> = gcd m n * lcm m n"
huffman@31706
  1769
  unfolding lcm_int_def gcd_int_def
haftmann@62348
  1770
  apply (subst of_nat_mult [symmetric])
nipkow@31952
  1771
  apply (subst prod_gcd_lcm_nat [symmetric])
huffman@31706
  1772
  apply (subst nat_abs_mult_distrib [symmetric])
huffman@31706
  1773
  apply (simp, simp add: abs_mult)
huffman@31706
  1774
done
huffman@31706
  1775
nipkow@31952
  1776
lemma lcm_pos_nat:
nipkow@31798
  1777
  "(m::nat) > 0 \<Longrightarrow> n>0 \<Longrightarrow> lcm m n > 0"
nipkow@31952
  1778
by (metis gr0I mult_is_0 prod_gcd_lcm_nat)
chaieb@27669
  1779
nipkow@31952
  1780
lemma lcm_pos_int:
nipkow@31798
  1781
  "(m::int) ~= 0 \<Longrightarrow> n ~= 0 \<Longrightarrow> lcm m n > 0"
nipkow@31952
  1782
  apply (subst lcm_abs_int)
nipkow@31952
  1783
  apply (rule lcm_pos_nat [transferred])
nipkow@31798
  1784
  apply auto
haftmann@62344
  1785
  done
haftmann@23687
  1786
haftmann@62353
  1787
lemma dvd_pos_nat: -- \<open>FIXME move\<close>
haftmann@23687
  1788
  fixes n m :: nat
haftmann@23687
  1789
  assumes "n > 0" and "m dvd n"
haftmann@23687
  1790
  shows "m > 0"
haftmann@62344
  1791
  using assms by (cases m) auto
nipkow@31729
  1792
nipkow@31952
  1793
lemma lcm_unique_nat: "(a::nat) dvd d \<and> b dvd d \<and>
huffman@31706
  1794
    (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@62344
  1795
  by (auto intro: dvd_antisym lcm_least)
chaieb@27568
  1796
nipkow@31952
  1797
lemma lcm_unique_int: "d >= 0 \<and> (a::int) dvd d \<and> b dvd d \<and>
huffman@31706
  1798
    (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@62344
  1799
  using lcm_least zdvd_antisym_nonneg by auto
haftmann@34973
  1800
nipkow@31798
  1801
lemma lcm_proj2_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> lcm x y = y"
huffman@31706
  1802
  apply (rule sym)
nipkow@31952
  1803
  apply (subst lcm_unique_nat [symmetric])
huffman@31706
  1804
  apply auto
huffman@31706
  1805
done
huffman@31706
  1806
wenzelm@61944
  1807
lemma lcm_proj2_if_dvd_int [simp]: "(x::int) dvd y \<Longrightarrow> lcm x y = \<bar>y\<bar>"
huffman@31706
  1808
  apply (rule sym)
nipkow@31952
  1809
  apply (subst lcm_unique_int [symmetric])
huffman@31706
  1810
  apply auto
huffman@31706
  1811
done
huffman@31706
  1812
nipkow@31798
  1813
lemma lcm_proj1_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> lcm y x = y"
haftmann@62344
  1814
by (subst lcm.commute, erule lcm_proj2_if_dvd_nat)
huffman@31706
  1815
wenzelm@61944
  1816
lemma lcm_proj1_if_dvd_int [simp]: "(x::int) dvd y \<Longrightarrow> lcm y x = \<bar>y\<bar>"
haftmann@62344
  1817
by (subst lcm.commute, erule lcm_proj2_if_dvd_int)
huffman@31706
  1818
haftmann@62353
  1819
lemma lcm_proj1_iff_nat [simp]: "lcm m n = (m::nat) \<longleftrightarrow> n dvd m"
nipkow@31992
  1820
by (metis lcm_proj1_if_dvd_nat lcm_unique_nat)
nipkow@31992
  1821
haftmann@62353
  1822
lemma lcm_proj2_iff_nat [simp]: "lcm m n = (n::nat) \<longleftrightarrow> m dvd n"
nipkow@31992
  1823
by (metis lcm_proj2_if_dvd_nat lcm_unique_nat)
nipkow@31992
  1824
haftmann@62353
  1825
lemma lcm_proj1_iff_int [simp]: "lcm m n = \<bar>m::int\<bar> \<longleftrightarrow> n dvd m"
nipkow@31992
  1826
by (metis dvd_abs_iff lcm_proj1_if_dvd_int lcm_unique_int)
nipkow@31992
  1827
haftmann@62353
  1828
lemma lcm_proj2_iff_int [simp]: "lcm m n = \<bar>n::int\<bar> \<longleftrightarrow> m dvd n"
nipkow@31992
  1829
by (metis dvd_abs_iff lcm_proj2_if_dvd_int lcm_unique_int)
chaieb@27568
  1830
haftmann@61913
  1831
lemma (in semiring_gcd) comp_fun_idem_gcd:
haftmann@61913
  1832
  "comp_fun_idem gcd"
haftmann@61913
  1833
  by standard (simp_all add: fun_eq_iff ac_simps)
haftmann@61913
  1834
haftmann@61913
  1835
lemma (in semiring_gcd) comp_fun_idem_lcm:
haftmann@61913
  1836
  "comp_fun_idem lcm"
haftmann@61913
  1837
  by standard (simp_all add: fun_eq_iff ac_simps)
haftmann@61913
  1838
haftmann@62353
  1839
lemma lcm_1_iff_nat [simp]:
haftmann@62353
  1840
  "lcm (m::nat) n = Suc 0 \<longleftrightarrow> m = Suc 0 \<and> n = Suc 0"
haftmann@62353
  1841
  using lcm_eq_1_iff [of m n] by simp
haftmann@61913
  1842
  
haftmann@62353
  1843
lemma lcm_1_iff_int [simp]:
haftmann@62353
  1844
  "lcm (m::int) n = 1 \<longleftrightarrow> (m=1 \<or> m = -1) \<and> (n=1 \<or> n = -1)"
haftmann@61913
  1845
  by auto
nipkow@31995
  1846
haftmann@34030
  1847
haftmann@62345
  1848
subsection \<open>The complete divisibility lattice on @{typ nat} and @{typ int}\<close>
nipkow@32112
  1849
wenzelm@60758
  1850
text\<open>Lifting gcd and lcm to sets (Gcd/Lcm).
huffman@45264
  1851
Gcd is defined via Lcm to facilitate the proof that we have a complete lattice.
wenzelm@60758
  1852
\<close>
huffman@45264
  1853
haftmann@62345
  1854
instantiation nat :: semiring_Gcd
nipkow@32112
  1855
begin
nipkow@32112
  1856
haftmann@62344
  1857
interpretation semilattice_neutr_set lcm "1::nat"
haftmann@62344
  1858
  by standard simp_all
haftmann@54867
  1859
haftmann@61929
  1860
definition
haftmann@61929
  1861
  "Lcm (M::nat set) = (if finite M then F M else 0)"
haftmann@51489
  1862
haftmann@51489
  1863
lemma Lcm_nat_empty:
haftmann@51489
  1864
  "Lcm {} = (1::nat)"
haftmann@60690
  1865
  by (simp add: Lcm_nat_def del: One_nat_def)
haftmann@51489
  1866
haftmann@51489
  1867
lemma Lcm_nat_insert:
haftmann@51489
  1868
  "Lcm (insert n M) = lcm (n::nat) (Lcm M)"
haftmann@61929
  1869
  by (cases "finite M") (auto simp add: Lcm_nat_def simp del: One_nat_def)
haftmann@61929
  1870
haftmann@61929
  1871
lemma Lcm_nat_infinite:
haftmann@61929
  1872
  "infinite M \<Longrightarrow> Lcm M = (0::nat)"
haftmann@61929
  1873
  by (simp add: Lcm_nat_def)
haftmann@61929
  1874
haftmann@61929
  1875
lemma dvd_Lcm_nat [simp]:
haftmann@61929
  1876
  fixes M :: "nat set"
haftmann@61929
  1877
  assumes "m \<in> M"
haftmann@61929
  1878
  shows "m dvd Lcm M"
haftmann@61929
  1879
proof -
haftmann@61929
  1880
  from assms have "insert m M = M" by auto
haftmann@61929
  1881
  moreover have "m dvd Lcm (insert m M)"
haftmann@61929
  1882
    by (simp add: Lcm_nat_insert)
haftmann@61929
  1883
  ultimately show ?thesis by simp
haftmann@61929
  1884
qed
haftmann@61929
  1885
haftmann@61929
  1886
lemma Lcm_dvd_nat [simp]:
haftmann@61929
  1887
  fixes M :: "nat set"
haftmann@61929
  1888
  assumes "\<forall>m\<in>M. m dvd n"
haftmann@61929
  1889
  shows "Lcm M dvd n"
haftmann@62353
  1890
proof (cases "n > 0")
haftmann@62353
  1891
  case False then show ?thesis by simp
haftmann@61929
  1892
next
haftmann@62353
  1893
  case True
haftmann@61929
  1894
  then have "finite {d. d dvd n}" by (rule finite_divisors_nat)
haftmann@61929
  1895
  moreover have "M \<subseteq> {d. d dvd n}" using assms by fast
haftmann@61929
  1896
  ultimately have "finite M" by (rule rev_finite_subset)
haftmann@62353
  1897
  then show ?thesis using assms
haftmann@62353
  1898
    by (induct M) (simp_all add: Lcm_nat_empty Lcm_nat_insert)
haftmann@61929
  1899
qed
nipkow@32112
  1900
huffman@45264
  1901
definition
huffman@45264
  1902
  "Gcd (M::nat set) = Lcm {d. \<forall>m\<in>M. d dvd m}"
nipkow@32112
  1903
haftmann@62345
  1904
instance proof
haftmann@61929
  1905
  show "Gcd N dvd n" if "n \<in> N" for N and n :: nat
haftmann@61929
  1906
  using that by (induct N rule: infinite_finite_induct)
haftmann@61929
  1907
    (auto simp add: Gcd_nat_def)
haftmann@61929
  1908
  show "n dvd Gcd N" if "\<And>m. m \<in> N \<Longrightarrow> n dvd m" for N and n :: nat
haftmann@61929
  1909
  using that by (induct N rule: infinite_finite_induct)
haftmann@61929
  1910
    (auto simp add: Gcd_nat_def)
haftmann@62345
  1911
  show "n dvd Lcm N" if "n \<in> N" for N and n ::nat
haftmann@62345
  1912
  using that by (induct N rule: infinite_finite_induct)
haftmann@62345
  1913
    auto
haftmann@62345
  1914
  show "Lcm N dvd n" if "\<And>m. m \<in> N \<Longrightarrow> m dvd n" for N and n ::nat
haftmann@62345
  1915
  using that by (induct N rule: infinite_finite_induct)
haftmann@62345
  1916
    auto
haftmann@62345
  1917
qed simp_all
nipkow@32112
  1918
haftmann@62345
  1919
end
haftmann@61913
  1920
haftmann@62346
  1921
lemma Gcd_nat_eq_one:
haftmann@62346
  1922
  "1 \<in> N \<Longrightarrow> Gcd N = (1::nat)"
haftmann@62346
  1923
  by (rule Gcd_eq_1_I) auto
haftmann@62346
  1924
wenzelm@60758
  1925
text\<open>Alternative characterizations of Gcd:\<close>
nipkow@32112
  1926
haftmann@62353
  1927
lemma Gcd_eq_Max:
haftmann@62353
  1928
  fixes M :: "nat set"
haftmann@62353
  1929
  assumes "finite (M::nat set)" and "M \<noteq> {}" and "0 \<notin> M"
haftmann@62353
  1930
  shows "Gcd M = Max (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  1931
proof (rule antisym)
haftmann@62353
  1932
  from assms obtain m where "m \<in> M" and "m > 0"
haftmann@62353
  1933
    by auto
haftmann@62353
  1934
  from \<open>m > 0\<close> have "finite {d. d dvd m}"
haftmann@62353
  1935
    by (blast intro: finite_divisors_nat)
haftmann@62353
  1936
  with \<open>m \<in> M\<close> have fin: "finite (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  1937
    by blast
haftmann@62353
  1938
  from fin show "Gcd M \<le> Max (\<Inter>m\<in>M. {d. d dvd m})"
haftmann@62353
  1939
    by (auto intro: Max_ge Gcd_dvd)
haftmann@62353
  1940
  from fin show "Max (\<Inter>m\<in>M. {d. d dvd m}) \<le> Gcd M"
haftmann@62353
  1941
    apply (rule Max.boundedI)
haftmann@62353
  1942
    apply auto
haftmann@62353
  1943
    apply (meson Gcd_dvd Gcd_greatest \<open>0 < m\<close> \<open>m \<in> M\<close> dvd_imp_le dvd_pos_nat)
haftmann@62353
  1944
    done
haftmann@62353
  1945
qed
nipkow@32112
  1946
nipkow@32112
  1947
lemma Gcd_remove0_nat: "finite M \<Longrightarrow> Gcd M = Gcd (M - {0::nat})"
nipkow@32112
  1948
apply(induct pred:finite)
nipkow@32112
  1949
 apply simp
nipkow@32112
  1950
apply(case_tac "x=0")
nipkow@32112
  1951
 apply simp
nipkow@32112
  1952
apply(subgoal_tac "insert x F - {0} = insert x (F - {0})")
nipkow@32112
  1953
 apply simp
nipkow@32112
  1954
apply blast
nipkow@32112
  1955
done
nipkow@32112
  1956
nipkow@32112
  1957
lemma Lcm_in_lcm_closed_set_nat:
nipkow@32112
  1958
  "finite M \<Longrightarrow> M \<noteq> {} \<Longrightarrow> ALL m n :: nat. m:M \<longrightarrow> n:M \<longrightarrow> lcm m n : M \<Longrightarrow> Lcm M : M"
nipkow@32112
  1959
apply(induct rule:finite_linorder_min_induct)
nipkow@32112
  1960
 apply simp
nipkow@32112
  1961
apply simp
nipkow@32112
  1962
apply(subgoal_tac "ALL m n :: nat. m:A \<longrightarrow> n:A \<longrightarrow> lcm m n : A")
nipkow@32112
  1963
 apply simp
nipkow@32112
  1964
 apply(case_tac "A={}")
nipkow@32112
  1965
  apply simp
nipkow@32112
  1966
 apply simp
nipkow@32112
  1967
apply (metis lcm_pos_nat lcm_unique_nat linorder_neq_iff nat_dvd_not_less not_less0)
nipkow@32112
  1968
done
nipkow@32112
  1969
nipkow@32112
  1970
lemma Lcm_eq_Max_nat:
nipkow@32112
  1971
  "finite M \<Longrightarrow> M \<noteq> {} \<Longrightarrow> 0 \<notin> M \<Longrightarrow> ALL m n :: nat. m:M \<longrightarrow> n:M \<longrightarrow> lcm m n : M \<Longrightarrow> Lcm M = Max M"
nipkow@32112
  1972
apply(rule antisym)
nipkow@32112
  1973
 apply(rule Max_ge, assumption)
nipkow@32112
  1974
 apply(erule (2) Lcm_in_lcm_closed_set_nat)
haftmann@62345
  1975
apply (auto simp add: not_le Lcm_0_iff dvd_imp_le leD le_neq_trans)
nipkow@32112
  1976
done
nipkow@32112
  1977
nipkow@34222
  1978
lemma mult_inj_if_coprime_nat:
nipkow@34222
  1979
  "inj_on f A \<Longrightarrow> inj_on g B \<Longrightarrow> ALL a:A. ALL b:B. coprime (f a) (g b)
nipkow@34222
  1980
   \<Longrightarrow> inj_on (%(a,b). f a * g b::nat) (A \<times> B)"
haftmann@61913
  1981
  by (auto simp add: inj_on_def coprime_crossproduct_nat simp del: One_nat_def)
nipkow@34222
  1982
wenzelm@60758
  1983
text\<open>Nitpick:\<close>
nipkow@34222
  1984
blanchet@41792
  1985
lemma gcd_eq_nitpick_gcd [nitpick_unfold]: "gcd x y = Nitpick.nat_gcd x y"
blanchet@41792
  1986
by (induct x y rule: nat_gcd.induct)
blanchet@41792
  1987
   (simp add: gcd_nat.simps Nitpick.nat_gcd.simps)
blanchet@33197
  1988
blanchet@41792
  1989
lemma lcm_eq_nitpick_lcm [nitpick_unfold]: "lcm x y = Nitpick.nat_lcm x y"
blanchet@33197
  1990
by (simp only: lcm_nat_def Nitpick.nat_lcm_def gcd_eq_nitpick_gcd)
blanchet@33197
  1991
haftmann@54867
  1992
wenzelm@60758
  1993
subsubsection \<open>Setwise gcd and lcm for integers\<close>
huffman@45264
  1994
haftmann@62345
  1995
instantiation int :: semiring_Gcd
huffman@45264
  1996
begin
huffman@45264
  1997
huffman@45264
  1998
definition
haftmann@62346
  1999
  "Lcm M = int (Lcm m\<in>M. (nat \<circ> abs) m)"
huffman@45264
  2000
huffman@45264
  2001
definition
haftmann@62346
  2002
  "Gcd M = int (Gcd m\<in>M. (nat \<circ> abs) m)"
huffman@45264
  2003
haftmann@62345
  2004
instance by standard
haftmann@62345
  2005
  (auto intro!: Gcd_dvd Gcd_greatest simp add: Gcd_int_def
haftmann@62345
  2006
    Lcm_int_def int_dvd_iff dvd_int_iff dvd_int_unfold_dvd_nat [symmetric])
haftmann@62345
  2007
haftmann@62345
  2008
end
haftmann@62345
  2009
haftmann@62346
  2010
lemma abs_Gcd [simp]:
haftmann@62346
  2011
  fixes K :: "int set"
haftmann@62346
  2012
  shows "\<bar>Gcd K\<bar> = Gcd K"
haftmann@62346
  2013
  using normalize_Gcd [of K] by simp
haftmann@62346
  2014
haftmann@62346
  2015
lemma abs_Lcm [simp]:
haftmann@62346
  2016
  fixes K :: "int set"
haftmann@62346
  2017
  shows "\<bar>Lcm K\<bar> = Lcm K"
haftmann@62346
  2018
  using normalize_Lcm [of K] by simp
haftmann@62346
  2019
haftmann@62346
  2020
lemma Gcm_eq_int_iff:
haftmann@62346
  2021
  "Gcd K = int n \<longleftrightarrow> Gcd ((nat \<circ> abs) ` K) = n"
haftmann@62346
  2022
  by (simp add: Gcd_int_def comp_def image_image)
haftmann@62346
  2023
haftmann@62346
  2024
lemma Lcm_eq_int_iff:
haftmann@62346
  2025
  "Lcm K = int n \<longleftrightarrow> Lcm ((nat \<circ> abs) ` K) = n"
haftmann@62346
  2026
  by (simp add: Lcm_int_def comp_def image_image)
haftmann@62346
  2027
haftmann@62345
  2028
haftmann@62345
  2029
subsection \<open>GCD and LCM on @{typ integer}\<close>
haftmann@62345
  2030
haftmann@62345
  2031
instantiation integer :: gcd
haftmann@62345
  2032
begin
haftmann@62345
  2033
haftmann@62345
  2034
context
haftmann@62345
  2035
  includes integer.lifting
haftmann@62345
  2036
begin
haftmann@62345
  2037
haftmann@62345
  2038
lift_definition gcd_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@62345
  2039
  is gcd .
haftmann@62345
  2040
lift_definition lcm_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@62345
  2041
  is lcm .
haftmann@62345
  2042
haftmann@62345
  2043
end
huffman@45264
  2044
instance ..
haftmann@60686
  2045
wenzelm@21256
  2046
end
huffman@45264
  2047
Andreas@61856
  2048
lifting_update integer.lifting
Andreas@61856
  2049
lifting_forget integer.lifting
Andreas@61856
  2050
haftmann@62345
  2051
context
haftmann@62345
  2052
  includes integer.lifting
haftmann@62345
  2053
begin
Andreas@61856
  2054
Andreas@61856
  2055
lemma gcd_code_integer [code]:
Andreas@61856
  2056
  "gcd k l = \<bar>if l = (0::integer) then k else gcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
haftmann@62345
  2057
  by transfer (fact gcd_code_int)
Andreas@61856
  2058
wenzelm@61944
  2059
lemma lcm_code_integer [code]: "lcm (a::integer) b = \<bar>a\<bar> * \<bar>b\<bar> div gcd a b"
haftmann@62345
  2060
  by transfer (fact lcm_altdef_int)
Andreas@61856
  2061
Andreas@61856
  2062
end
Andreas@61856
  2063
Andreas@61856
  2064
code_printing constant "gcd :: integer \<Rightarrow> _"
Andreas@61856
  2065
  \<rightharpoonup> (OCaml) "Big'_int.gcd'_big'_int"
Andreas@61856
  2066
  and (Haskell) "Prelude.gcd"
Andreas@61856
  2067
  and (Scala) "_.gcd'((_)')"
wenzelm@61975
  2068
  \<comment> \<open>There is no gcd operation in the SML standard library, so no code setup for SML\<close>
Andreas@61856
  2069
haftmann@62344
  2070
text \<open>Some code equations\<close>
haftmann@62344
  2071
haftmann@62344
  2072
lemma Lcm_set_nat [code, code_unfold]:
haftmann@62344
  2073
  "Lcm (set ns) = fold lcm ns (1::nat)"
haftmann@62344
  2074
  using Lcm_set [of ns] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2075
haftmann@62344
  2076
lemma Gcd_set_nat [code]:
haftmann@62344
  2077
  "Gcd (set ns) = fold gcd ns (0::nat)"
haftmann@62344
  2078
  using Gcd_set [of ns] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2079
haftmann@62344
  2080
lemma Lcm_set_int [code, code_unfold]:
haftmann@62344
  2081
  "Lcm (set xs) = fold lcm xs (1::int)"
haftmann@62344
  2082
  using Lcm_set [of xs] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2083
haftmann@62344
  2084
lemma Gcd_set_int [code]:
haftmann@62344
  2085
  "Gcd (set xs) = fold gcd xs (0::int)"
haftmann@62344
  2086
  using Gcd_set [of xs] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2087
haftmann@62344
  2088
text \<open>Fact aliasses\<close>
haftmann@62344
  2089
haftmann@62353
  2090
lemma lcm_0_iff_nat [simp]: "lcm (m::nat) n = 0 \<longleftrightarrow> m = 0 \<or> n = 0"
haftmann@62344
  2091
  by (fact lcm_eq_0_iff)
haftmann@62344
  2092
haftmann@62344
  2093
lemma lcm_0_iff_int [simp]: "lcm (m::int) n = 0 \<longleftrightarrow> m = 0 \<or> n = 0"
haftmann@62344
  2094
  by (fact lcm_eq_0_iff)
haftmann@62344
  2095
haftmann@62344
  2096
lemma dvd_lcm_I1_nat [simp]: "(k::nat) dvd m \<Longrightarrow> k dvd lcm m n"
haftmann@62344
  2097
  by (fact dvd_lcmI1)
haftmann@62344
  2098
haftmann@62344
  2099
lemma dvd_lcm_I2_nat [simp]: "(k::nat) dvd n \<Longrightarrow> k dvd lcm m n"
haftmann@62344
  2100
  by (fact dvd_lcmI2)
haftmann@62344
  2101
haftmann@62344
  2102
lemma dvd_lcm_I1_int [simp]: "(i::int) dvd m \<Longrightarrow> i dvd lcm m n"
haftmann@62344
  2103
  by (fact dvd_lcmI1)
haftmann@62344
  2104
haftmann@62353
  2105
lemma dvd_lcm_I2_int [simp]: "(i::int) dvd n \<Longrightarrow> i dvd lcm m n"
haftmann@62344
  2106
  by (fact dvd_lcmI2)
haftmann@62344
  2107
haftmann@62344
  2108
lemma coprime_exp2_nat [intro]: "coprime (a::nat) b \<Longrightarrow> coprime (a^n) (b^m)"
haftmann@62344
  2109
  by (fact coprime_exp2)
haftmann@62344
  2110
haftmann@62344
  2111
lemma coprime_exp2_int [intro]: "coprime (a::int) b \<Longrightarrow> coprime (a^n) (b^m)"
haftmann@62344
  2112
  by (fact coprime_exp2)
haftmann@62344
  2113
haftmann@62344
  2114
lemmas Gcd_dvd_nat [simp] = Gcd_dvd [where ?'a = nat]
haftmann@62344
  2115
lemmas Gcd_dvd_int [simp] = Gcd_dvd [where ?'a = int]
haftmann@62353
  2116
lemmas Gcd_greatest_nat [simp] = Gcd_greatest [where ?'a = nat]
haftmann@62353
  2117
lemmas Gcd_greatest_int [simp] = Gcd_greatest [where ?'a = int]
haftmann@62344
  2118
haftmann@62344
  2119
lemma dvd_Lcm_int [simp]:
haftmann@62344
  2120
  fixes M :: "int set" assumes "m \<in> M" shows "m dvd Lcm M"
haftmann@62344
  2121
  using assms by (fact dvd_Lcm)
haftmann@62344
  2122
haftmann@62344
  2123
lemma gcd_neg_numeral_1_int [simp]:
haftmann@62344
  2124
  "gcd (- numeral n :: int) x = gcd (numeral n) x"
haftmann@62344
  2125
  by (fact gcd_neg1_int)
haftmann@62344
  2126
haftmann@62344
  2127
lemma gcd_neg_numeral_2_int [simp]:
haftmann@62344
  2128
  "gcd x (- numeral n :: int) = gcd x (numeral n)"
haftmann@62344
  2129
  by (fact gcd_neg2_int)
haftmann@62344
  2130
haftmann@62344
  2131
lemma gcd_proj1_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> gcd x y = x"
haftmann@62344
  2132
  by (fact gcd_nat.absorb1)
haftmann@62344
  2133
haftmann@62344
  2134
lemma gcd_proj2_if_dvd_nat [simp]: "(y::nat) dvd x \<Longrightarrow> gcd x y = y"
haftmann@62344
  2135
  by (fact gcd_nat.absorb2)
haftmann@62344
  2136
haftmann@62353
  2137
lemmas Lcm_eq_0_I_nat [simp] = Lcm_eq_0_I [where ?'a = nat]
haftmann@62353
  2138
lemmas Lcm_0_iff_nat [simp] = Lcm_0_iff [where ?'a = nat]
haftmann@62353
  2139
lemmas Lcm_least_int [simp] = Lcm_least [where ?'a = int]
haftmann@62345
  2140
Andreas@61856
  2141
end