src/HOL/GCD.thy
author haftmann
Wed Feb 17 21:51:57 2016 +0100 (2016-02-17)
changeset 62350 66a381d3f88f
parent 62349 7c23469b5118
child 62353 7f927120b5a2
permissions -rw-r--r--
more sophisticated GCD syntax
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 dvd_Gcd: \<comment> \<open>FIXME remove\<close>
haftmann@62345
   589
  "\<forall>b\<in>A. a dvd b \<Longrightarrow> a dvd Gcd A"
haftmann@62345
   590
  by (blast intro: Gcd_greatest)
haftmann@62345
   591
haftmann@62345
   592
lemma Gcd_set [code_unfold]:
haftmann@62345
   593
  "Gcd (set as) = foldr gcd as 0"
haftmann@62345
   594
  by (induct as) simp_all
haftmann@62345
   595
haftmann@60686
   596
lemma Lcm_set [code_unfold]:
haftmann@60686
   597
  "Lcm (set as) = foldr lcm as 1"
haftmann@60686
   598
  by (induct as) simp_all
haftmann@59008
   599
haftmann@62345
   600
lemma Gcd_image_normalize [simp]:
haftmann@62345
   601
  "Gcd (normalize ` A) = Gcd A"
haftmann@62345
   602
proof -
haftmann@62345
   603
  have "Gcd (normalize ` A) dvd a" if "a \<in> A" for a
haftmann@62345
   604
  proof -
haftmann@62345
   605
    from that obtain B where "A = insert a B" by blast
haftmann@62350
   606
    moreover have "gcd (normalize a) (Gcd (normalize ` B)) dvd normalize a"
haftmann@62345
   607
      by (rule gcd_dvd1)
haftmann@62345
   608
    ultimately show "Gcd (normalize ` A) dvd a"
haftmann@62345
   609
      by simp
haftmann@62345
   610
  qed
haftmann@62345
   611
  then have "Gcd (normalize ` A) dvd Gcd A" and "Gcd A dvd Gcd (normalize ` A)"
haftmann@62345
   612
    by (auto intro!: Gcd_greatest intro: Gcd_dvd)
haftmann@62345
   613
  then show ?thesis
haftmann@62345
   614
    by (auto intro: associated_eqI)
haftmann@62345
   615
qed
haftmann@62345
   616
haftmann@62346
   617
lemma Gcd_eqI:
haftmann@62346
   618
  assumes "normalize a = a"
haftmann@62346
   619
  assumes "\<And>b. b \<in> A \<Longrightarrow> a dvd b"
haftmann@62346
   620
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> c dvd b) \<Longrightarrow> c dvd a"
haftmann@62346
   621
  shows "Gcd A = a"
haftmann@62346
   622
  using assms by (blast intro: associated_eqI Gcd_greatest Gcd_dvd normalize_Gcd)
haftmann@62346
   623
haftmann@62346
   624
lemma Lcm_eqI:
haftmann@62346
   625
  assumes "normalize a = a"
haftmann@62346
   626
  assumes "\<And>b. b \<in> A \<Longrightarrow> b dvd a"
haftmann@62346
   627
    and "\<And>c. (\<And>b. b \<in> A \<Longrightarrow> b dvd c) \<Longrightarrow> a dvd c"
haftmann@62346
   628
  shows "Lcm A = a"
haftmann@62346
   629
  using assms by (blast intro: associated_eqI Lcm_least dvd_Lcm normalize_Lcm)
haftmann@62346
   630
haftmann@62350
   631
end
haftmann@62345
   632
haftmann@62345
   633
subsection \<open>GCD and LCM on @{typ nat} and @{typ int}\<close>
haftmann@59008
   634
huffman@31706
   635
instantiation nat :: gcd
huffman@31706
   636
begin
wenzelm@21256
   637
haftmann@62345
   638
fun gcd_nat  :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@62345
   639
where "gcd_nat x y =
haftmann@62345
   640
  (if y = 0 then x else gcd y (x mod y))"
huffman@31706
   641
haftmann@62345
   642
definition lcm_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
huffman@31706
   643
where
huffman@31706
   644
  "lcm_nat x y = x * y div (gcd x y)"
huffman@31706
   645
huffman@31706
   646
instance proof qed
huffman@31706
   647
huffman@31706
   648
end
huffman@31706
   649
huffman@31706
   650
instantiation int :: gcd
huffman@31706
   651
begin
wenzelm@21256
   652
haftmann@62345
   653
definition gcd_int  :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
   654
  where "gcd_int x y = int (gcd (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
   655
haftmann@62345
   656
definition lcm_int :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@62345
   657
  where "lcm_int x y = int (lcm (nat \<bar>x\<bar>) (nat \<bar>y\<bar>))"
haftmann@23687
   658
wenzelm@61944
   659
instance ..
huffman@31706
   660
huffman@31706
   661
end
haftmann@23687
   662
haftmann@62345
   663
text \<open>Transfer setup\<close>
huffman@31706
   664
huffman@31706
   665
lemma transfer_nat_int_gcd:
huffman@31706
   666
  "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> gcd (nat x) (nat y) = nat (gcd x y)"
huffman@31706
   667
  "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> lcm (nat x) (nat y) = nat (lcm x y)"
haftmann@32479
   668
  unfolding gcd_int_def lcm_int_def
huffman@31706
   669
  by auto
haftmann@23687
   670
huffman@31706
   671
lemma transfer_nat_int_gcd_closures:
huffman@31706
   672
  "x >= (0::int) \<Longrightarrow> y >= 0 \<Longrightarrow> gcd x y >= 0"
huffman@31706
   673
  "x >= (0::int) \<Longrightarrow> y >= 0 \<Longrightarrow> lcm x y >= 0"
huffman@31706
   674
  by (auto simp add: gcd_int_def lcm_int_def)
huffman@31706
   675
haftmann@35644
   676
declare transfer_morphism_nat_int[transfer add return:
huffman@31706
   677
    transfer_nat_int_gcd transfer_nat_int_gcd_closures]
huffman@31706
   678
huffman@31706
   679
lemma transfer_int_nat_gcd:
huffman@31706
   680
  "gcd (int x) (int y) = int (gcd x y)"
huffman@31706
   681
  "lcm (int x) (int y) = int (lcm x y)"
haftmann@32479
   682
  by (unfold gcd_int_def lcm_int_def, auto)
huffman@31706
   683
huffman@31706
   684
lemma transfer_int_nat_gcd_closures:
huffman@31706
   685
  "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> gcd x y >= 0"
huffman@31706
   686
  "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> lcm x y >= 0"
huffman@31706
   687
  by (auto simp add: gcd_int_def lcm_int_def)
huffman@31706
   688
haftmann@35644
   689
declare transfer_morphism_int_nat[transfer add return:
huffman@31706
   690
    transfer_int_nat_gcd transfer_int_nat_gcd_closures]
huffman@31706
   691
nipkow@31952
   692
lemma gcd_nat_induct:
haftmann@23687
   693
  fixes m n :: nat
haftmann@23687
   694
  assumes "\<And>m. P m 0"
haftmann@23687
   695
    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
haftmann@23687
   696
  shows "P m n"
huffman@31706
   697
  apply (rule gcd_nat.induct)
huffman@31706
   698
  apply (case_tac "y = 0")
huffman@31706
   699
  using assms apply simp_all
huffman@31706
   700
done
huffman@31706
   701
huffman@31706
   702
(* specific to int *)
huffman@31706
   703
haftmann@62346
   704
lemma gcd_eq_int_iff:
haftmann@62346
   705
  "gcd k l = int n \<longleftrightarrow> gcd (nat \<bar>k\<bar>) (nat \<bar>l\<bar>) = n"
haftmann@62346
   706
  by (simp add: gcd_int_def)
haftmann@62346
   707
haftmann@62346
   708
lemma lcm_eq_int_iff:
haftmann@62346
   709
  "lcm k l = int n \<longleftrightarrow> lcm (nat \<bar>k\<bar>) (nat \<bar>l\<bar>) = n"
haftmann@62346
   710
  by (simp add: lcm_int_def)
haftmann@62346
   711
nipkow@31952
   712
lemma gcd_neg1_int [simp]: "gcd (-x::int) y = gcd x y"
huffman@31706
   713
  by (simp add: gcd_int_def)
huffman@31706
   714
nipkow@31952
   715
lemma gcd_neg2_int [simp]: "gcd (x::int) (-y) = gcd x y"
huffman@31706
   716
  by (simp add: gcd_int_def)
huffman@31706
   717
wenzelm@61944
   718
lemma abs_gcd_int[simp]: "\<bar>gcd (x::int) y\<bar> = gcd x y"
nipkow@31813
   719
by(simp add: gcd_int_def)
nipkow@31813
   720
wenzelm@61944
   721
lemma gcd_abs_int: "gcd (x::int) y = gcd \<bar>x\<bar> \<bar>y\<bar>"
nipkow@31813
   722
by (simp add: gcd_int_def)
nipkow@31813
   723
wenzelm@61944
   724
lemma gcd_abs1_int[simp]: "gcd \<bar>x\<bar> (y::int) = gcd x y"
nipkow@31952
   725
by (metis abs_idempotent gcd_abs_int)
nipkow@31813
   726
wenzelm@61944
   727
lemma gcd_abs2_int[simp]: "gcd x \<bar>y::int\<bar> = gcd x y"
nipkow@31952
   728
by (metis abs_idempotent gcd_abs_int)
huffman@31706
   729
nipkow@31952
   730
lemma gcd_cases_int:
huffman@31706
   731
  fixes x :: int and y
huffman@31706
   732
  assumes "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (gcd x y)"
huffman@31706
   733
      and "x >= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (gcd x (-y))"
huffman@31706
   734
      and "x <= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (gcd (-x) y)"
huffman@31706
   735
      and "x <= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (gcd (-x) (-y))"
huffman@31706
   736
  shows "P (gcd x y)"
wenzelm@61944
   737
  by (insert assms, auto, arith)
wenzelm@21256
   738
nipkow@31952
   739
lemma gcd_ge_0_int [simp]: "gcd (x::int) y >= 0"
huffman@31706
   740
  by (simp add: gcd_int_def)
huffman@31706
   741
nipkow@31952
   742
lemma lcm_neg1_int: "lcm (-x::int) y = lcm x y"
huffman@31706
   743
  by (simp add: lcm_int_def)
huffman@31706
   744
nipkow@31952
   745
lemma lcm_neg2_int: "lcm (x::int) (-y) = lcm x y"
huffman@31706
   746
  by (simp add: lcm_int_def)
huffman@31706
   747
wenzelm@61944
   748
lemma lcm_abs_int: "lcm (x::int) y = lcm \<bar>x\<bar> \<bar>y\<bar>"
huffman@31706
   749
  by (simp add: lcm_int_def)
wenzelm@21256
   750
wenzelm@61944
   751
lemma abs_lcm_int [simp]: "\<bar>lcm i j::int\<bar> = lcm i j"
wenzelm@61944
   752
  by (simp add:lcm_int_def)
nipkow@31814
   753
wenzelm@61944
   754
lemma lcm_abs1_int[simp]: "lcm \<bar>x\<bar> (y::int) = lcm x y"
wenzelm@61944
   755
  by (metis abs_idempotent lcm_int_def)
nipkow@31814
   756
wenzelm@61944
   757
lemma lcm_abs2_int[simp]: "lcm x \<bar>y::int\<bar> = lcm x y"
wenzelm@61944
   758
  by (metis abs_idempotent lcm_int_def)
nipkow@31814
   759
nipkow@31952
   760
lemma lcm_cases_int:
huffman@31706
   761
  fixes x :: int and y
huffman@31706
   762
  assumes "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (lcm x y)"
huffman@31706
   763
      and "x >= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (lcm x (-y))"
huffman@31706
   764
      and "x <= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> P (lcm (-x) y)"
huffman@31706
   765
      and "x <= 0 \<Longrightarrow> y <= 0 \<Longrightarrow> P (lcm (-x) (-y))"
huffman@31706
   766
  shows "P (lcm x y)"
wenzelm@41550
   767
  using assms by (auto simp add: lcm_neg1_int lcm_neg2_int) arith
huffman@31706
   768
nipkow@31952
   769
lemma lcm_ge_0_int [simp]: "lcm (x::int) y >= 0"
huffman@31706
   770
  by (simp add: lcm_int_def)
huffman@31706
   771
haftmann@54867
   772
lemma gcd_0_nat: "gcd (x::nat) 0 = x"
haftmann@23687
   773
  by simp
haftmann@23687
   774
wenzelm@61944
   775
lemma gcd_0_int [simp]: "gcd (x::int) 0 = \<bar>x\<bar>"
huffman@31706
   776
  by (unfold gcd_int_def, auto)
huffman@31706
   777
haftmann@54867
   778
lemma gcd_0_left_nat: "gcd 0 (x::nat) = x"
haftmann@23687
   779
  by simp
haftmann@23687
   780
wenzelm@61944
   781
lemma gcd_0_left_int [simp]: "gcd 0 (x::int) = \<bar>x\<bar>"
huffman@31706
   782
  by (unfold gcd_int_def, auto)
huffman@31706
   783
nipkow@31952
   784
lemma gcd_red_nat: "gcd (x::nat) y = gcd y (x mod y)"
huffman@31706
   785
  by (case_tac "y = 0", auto)
huffman@31706
   786
huffman@31706
   787
(* weaker, but useful for the simplifier *)
huffman@31706
   788
haftmann@62345
   789
lemma gcd_non_0_nat: "y \<noteq> (0::nat) \<Longrightarrow> gcd (x::nat) y = gcd y (x mod y)"
huffman@31706
   790
  by simp
huffman@31706
   791
nipkow@31952
   792
lemma gcd_1_nat [simp]: "gcd (m::nat) 1 = 1"
wenzelm@21263
   793
  by simp
wenzelm@21256
   794
nipkow@31952
   795
lemma gcd_Suc_0 [simp]: "gcd (m::nat) (Suc 0) = Suc 0"
haftmann@60690
   796
  by simp
huffman@31706
   797
nipkow@31952
   798
lemma gcd_1_int [simp]: "gcd (m::int) 1 = 1"
huffman@31706
   799
  by (simp add: gcd_int_def)
huffman@30082
   800
nipkow@31952
   801
lemma gcd_idem_nat: "gcd (x::nat) x = x"
nipkow@31798
   802
by simp
huffman@31706
   803
wenzelm@61944
   804
lemma gcd_idem_int: "gcd (x::int) x = \<bar>x\<bar>"
nipkow@31813
   805
by (auto simp add: gcd_int_def)
huffman@31706
   806
huffman@31706
   807
declare gcd_nat.simps [simp del]
wenzelm@21256
   808
wenzelm@60758
   809
text \<open>
wenzelm@61799
   810
  \medskip @{term "gcd m n"} divides \<open>m\<close> and \<open>n\<close>.  The
wenzelm@21256
   811
  conjunctions don't seem provable separately.
wenzelm@60758
   812
\<close>
wenzelm@21256
   813
haftmann@59008
   814
instance nat :: semiring_gcd
haftmann@59008
   815
proof
haftmann@59008
   816
  fix m n :: nat
haftmann@59008
   817
  show "gcd m n dvd m" and "gcd m n dvd n"
haftmann@59008
   818
  proof (induct m n rule: gcd_nat_induct)
haftmann@59008
   819
    fix m n :: nat
haftmann@59008
   820
    assume "gcd n (m mod n) dvd m mod n" and "gcd n (m mod n) dvd n"
haftmann@59008
   821
    then have "gcd n (m mod n) dvd m"
haftmann@59008
   822
      by (rule dvd_mod_imp_dvd)
haftmann@59008
   823
    moreover assume "0 < n"
haftmann@59008
   824
    ultimately show "gcd m n dvd m"
haftmann@59008
   825
      by (simp add: gcd_non_0_nat)
haftmann@59008
   826
  qed (simp_all add: gcd_0_nat gcd_non_0_nat)
haftmann@59008
   827
next
haftmann@59008
   828
  fix m n k :: nat
haftmann@59008
   829
  assume "k dvd m" and "k dvd n"
haftmann@59008
   830
  then show "k dvd gcd m n"
haftmann@59008
   831
    by (induct m n rule: gcd_nat_induct) (simp_all add: gcd_non_0_nat dvd_mod gcd_0_nat)
haftmann@60686
   832
qed (simp_all add: lcm_nat_def)
lp15@59667
   833
haftmann@59008
   834
instance int :: ring_gcd
haftmann@60686
   835
  by standard
haftmann@60686
   836
    (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
   837
nipkow@31952
   838
lemma gcd_le1_nat [simp]: "a \<noteq> 0 \<Longrightarrow> gcd (a::nat) b \<le> a"
huffman@31706
   839
  by (rule dvd_imp_le, auto)
huffman@31706
   840
nipkow@31952
   841
lemma gcd_le2_nat [simp]: "b \<noteq> 0 \<Longrightarrow> gcd (a::nat) b \<le> b"
huffman@31706
   842
  by (rule dvd_imp_le, auto)
huffman@31706
   843
nipkow@31952
   844
lemma gcd_le1_int [simp]: "a > 0 \<Longrightarrow> gcd (a::int) b \<le> a"
huffman@31706
   845
  by (rule zdvd_imp_le, auto)
wenzelm@21256
   846
nipkow@31952
   847
lemma gcd_le2_int [simp]: "b > 0 \<Longrightarrow> gcd (a::int) b \<le> b"
huffman@31706
   848
  by (rule zdvd_imp_le, auto)
huffman@31706
   849
nipkow@31952
   850
lemma gcd_pos_nat [simp]: "(gcd (m::nat) n > 0) = (m ~= 0 | n ~= 0)"
haftmann@62344
   851
  by (insert gcd_eq_0_iff [of m n], arith)
wenzelm@21256
   852
nipkow@31952
   853
lemma gcd_pos_int [simp]: "(gcd (m::int) n > 0) = (m ~= 0 | n ~= 0)"
haftmann@62344
   854
  by (insert gcd_eq_0_iff [of m n], insert gcd_ge_0_int [of m n], arith)
huffman@31706
   855
nipkow@31952
   856
lemma gcd_unique_nat: "(d::nat) dvd a \<and> d dvd b \<and>
huffman@31706
   857
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
huffman@31706
   858
  apply auto
nipkow@33657
   859
  apply (rule dvd_antisym)
haftmann@59008
   860
  apply (erule (1) gcd_greatest)
huffman@31706
   861
  apply auto
huffman@31706
   862
done
wenzelm@21256
   863
nipkow@31952
   864
lemma gcd_unique_int: "d >= 0 & (d::int) dvd a \<and> d dvd b \<and>
huffman@31706
   865
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
nipkow@33657
   866
apply (case_tac "d = 0")
nipkow@33657
   867
 apply simp
nipkow@33657
   868
apply (rule iffI)
nipkow@33657
   869
 apply (rule zdvd_antisym_nonneg)
haftmann@59008
   870
 apply (auto intro: gcd_greatest)
huffman@31706
   871
done
huffman@30082
   872
haftmann@61913
   873
interpretation gcd_nat:
haftmann@62344
   874
  semilattice_neutr_order gcd "0::nat" Rings.dvd "\<lambda>m n. m dvd n \<and> m \<noteq> n"
haftmann@62344
   875
  by standard (auto simp add: gcd_unique_nat [symmetric] intro: dvd_antisym dvd_trans)
nipkow@31798
   876
wenzelm@61944
   877
lemma gcd_proj1_if_dvd_int [simp]: "x dvd y \<Longrightarrow> gcd (x::int) y = \<bar>x\<bar>"
haftmann@54867
   878
  by (metis abs_dvd_iff gcd_0_left_int gcd_abs_int gcd_unique_int)
nipkow@31798
   879
wenzelm@61944
   880
lemma gcd_proj2_if_dvd_int [simp]: "y dvd x \<Longrightarrow> gcd (x::int) y = \<bar>y\<bar>"
haftmann@62344
   881
  by (metis gcd_proj1_if_dvd_int gcd.commute)
nipkow@31798
   882
wenzelm@60758
   883
text \<open>
wenzelm@21256
   884
  \medskip Multiplication laws
wenzelm@60758
   885
\<close>
wenzelm@21256
   886
nipkow@31952
   887
lemma gcd_mult_distrib_nat: "(k::nat) * gcd m n = gcd (k * m) (k * n)"
wenzelm@61799
   888
    \<comment> \<open>@{cite \<open>page 27\<close> davenport92}\<close>
nipkow@31952
   889
  apply (induct m n rule: gcd_nat_induct)
huffman@31706
   890
  apply simp
wenzelm@21256
   891
  apply (case_tac "k = 0")
huffman@45270
   892
  apply (simp_all add: gcd_non_0_nat)
huffman@31706
   893
done
wenzelm@21256
   894
wenzelm@61944
   895
lemma gcd_mult_distrib_int: "\<bar>k::int\<bar> * gcd m n = gcd (k * m) (k * n)"
nipkow@31952
   896
  apply (subst (1 2) gcd_abs_int)
nipkow@31813
   897
  apply (subst (1 2) abs_mult)
nipkow@31952
   898
  apply (rule gcd_mult_distrib_nat [transferred])
huffman@31706
   899
  apply auto
huffman@31706
   900
done
wenzelm@21256
   901
haftmann@60690
   902
context semiring_gcd
haftmann@60690
   903
begin
wenzelm@21256
   904
haftmann@60690
   905
lemma coprime_dvd_mult:
haftmann@60690
   906
  assumes "coprime a b" and "a dvd c * b"
haftmann@60690
   907
  shows "a dvd c"
haftmann@60690
   908
proof (cases "c = 0")
haftmann@60690
   909
  case True then show ?thesis by simp
haftmann@60690
   910
next
haftmann@60690
   911
  case False
haftmann@60690
   912
  then have unit: "is_unit (unit_factor c)" by simp
haftmann@60690
   913
  from \<open>coprime a b\<close> mult_gcd_left [of c a b]
haftmann@60690
   914
  have "gcd (c * a) (c * b) * unit_factor c = c"
haftmann@60690
   915
    by (simp add: ac_simps)
haftmann@60690
   916
  moreover from \<open>a dvd c * b\<close> have "a dvd gcd (c * a) (c * b) * unit_factor c"
haftmann@60690
   917
    by (simp add: dvd_mult_unit_iff unit)
haftmann@60690
   918
  ultimately show ?thesis by simp
haftmann@60690
   919
qed
haftmann@60690
   920
haftmann@62344
   921
lemma coprime_dvd_mult_iff:
haftmann@62344
   922
  assumes "coprime a c"
haftmann@62344
   923
  shows "a dvd b * c \<longleftrightarrow> a dvd b"
haftmann@62344
   924
  using assms by (auto intro: coprime_dvd_mult)
haftmann@60690
   925
haftmann@60690
   926
lemma gcd_mult_cancel:
haftmann@60690
   927
  "coprime c b \<Longrightarrow> gcd (c * a) b = gcd a b"
haftmann@60690
   928
  apply (rule associated_eqI)
haftmann@59008
   929
  apply (rule gcd_greatest)
haftmann@60690
   930
  apply (rule_tac b = c in coprime_dvd_mult)
haftmann@60690
   931
  apply (simp add: gcd.assoc)
haftmann@60690
   932
  apply (simp_all add: ac_simps)
haftmann@60690
   933
  done
wenzelm@21256
   934
haftmann@62344
   935
lemma coprime_crossproduct:
haftmann@62344
   936
  fixes a b c d
haftmann@35368
   937
  assumes "coprime a d" and "coprime b c"
haftmann@62344
   938
  shows "normalize a * normalize c = normalize b * normalize d
haftmann@62344
   939
    \<longleftrightarrow> normalize a = normalize b \<and> normalize c = normalize d" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@35368
   940
proof
haftmann@35368
   941
  assume ?rhs then show ?lhs by simp
haftmann@35368
   942
next
haftmann@35368
   943
  assume ?lhs
haftmann@62344
   944
  from \<open>?lhs\<close> have "normalize a dvd normalize b * normalize d"
haftmann@62344
   945
    by (auto intro: dvdI dest: sym)
haftmann@62344
   946
  with \<open>coprime a d\<close> have "a dvd b"
haftmann@62344
   947
    by (simp add: coprime_dvd_mult_iff normalize_mult [symmetric])
haftmann@62344
   948
  from \<open>?lhs\<close> have "normalize b dvd normalize a * normalize c"
haftmann@62344
   949
    by (auto intro: dvdI dest: sym)
haftmann@62344
   950
  with \<open>coprime b c\<close> have "b dvd a"
haftmann@62344
   951
    by (simp add: coprime_dvd_mult_iff normalize_mult [symmetric])
haftmann@62344
   952
  from \<open>?lhs\<close> have "normalize c dvd normalize d * normalize b"
haftmann@62344
   953
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@62344
   954
  with \<open>coprime b c\<close> have "c dvd d"
haftmann@62344
   955
    by (simp add: coprime_dvd_mult_iff gcd.commute normalize_mult [symmetric])
haftmann@62344
   956
  from \<open>?lhs\<close> have "normalize d dvd normalize c * normalize a"
haftmann@62344
   957
    by (auto intro: dvdI dest: sym simp add: mult.commute)
haftmann@62344
   958
  with \<open>coprime a d\<close> have "d dvd c"
haftmann@62344
   959
    by (simp add: coprime_dvd_mult_iff gcd.commute normalize_mult [symmetric])
haftmann@62344
   960
  from \<open>a dvd b\<close> \<open>b dvd a\<close> have "normalize a = normalize b"
haftmann@62344
   961
    by (rule associatedI)
haftmann@62344
   962
  moreover from \<open>c dvd d\<close> \<open>d dvd c\<close> have "normalize c = normalize d"
haftmann@62344
   963
    by (rule associatedI)
haftmann@35368
   964
  ultimately show ?rhs ..
haftmann@35368
   965
qed
haftmann@35368
   966
haftmann@62344
   967
end
haftmann@62344
   968
haftmann@62344
   969
lemma coprime_crossproduct_nat:
haftmann@62344
   970
  fixes a b c d :: nat
haftmann@62344
   971
  assumes "coprime a d" and "coprime b c"
haftmann@62344
   972
  shows "a * c = b * d \<longleftrightarrow> a = b \<and> c = d"
haftmann@62344
   973
  using assms coprime_crossproduct [of a d b c] by simp
haftmann@62344
   974
haftmann@35368
   975
lemma coprime_crossproduct_int:
haftmann@35368
   976
  fixes a b c d :: int
haftmann@35368
   977
  assumes "coprime a d" and "coprime b c"
haftmann@35368
   978
  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
   979
  using assms coprime_crossproduct [of a d b c] by simp
haftmann@35368
   980
wenzelm@60758
   981
text \<open>\medskip Addition laws\<close>
wenzelm@21256
   982
nipkow@31952
   983
lemma gcd_add1_nat [simp]: "gcd ((m::nat) + n) n = gcd m n"
huffman@31706
   984
  apply (case_tac "n = 0")
nipkow@31952
   985
  apply (simp_all add: gcd_non_0_nat)
haftmann@62344
   986
  done
huffman@31706
   987
nipkow@31952
   988
lemma gcd_add2_nat [simp]: "gcd (m::nat) (m + n) = gcd m n"
haftmann@62344
   989
  apply (subst (1 2) gcd.commute)
haftmann@57512
   990
  apply (subst add.commute)
huffman@31706
   991
  apply simp
haftmann@62344
   992
  done
huffman@31706
   993
huffman@31706
   994
(* to do: add the other variations? *)
huffman@31706
   995
nipkow@31952
   996
lemma gcd_diff1_nat: "(m::nat) >= n \<Longrightarrow> gcd (m - n) n = gcd m n"
haftmann@62344
   997
  by (subst gcd_add1_nat [symmetric]) auto
huffman@31706
   998
nipkow@31952
   999
lemma gcd_diff2_nat: "(n::nat) >= m \<Longrightarrow> gcd (n - m) n = gcd m n"
haftmann@62344
  1000
  apply (subst gcd.commute)
nipkow@31952
  1001
  apply (subst gcd_diff1_nat [symmetric])
huffman@31706
  1002
  apply auto
haftmann@62344
  1003
  apply (subst gcd.commute)
nipkow@31952
  1004
  apply (subst gcd_diff1_nat)
huffman@31706
  1005
  apply assumption
haftmann@62344
  1006
  apply (rule gcd.commute)
haftmann@62344
  1007
  done
huffman@31706
  1008
nipkow@31952
  1009
lemma gcd_non_0_int: "(y::int) > 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
huffman@31706
  1010
  apply (frule_tac b = y and a = x in pos_mod_sign)
huffman@31706
  1011
  apply (simp del: pos_mod_sign add: gcd_int_def abs_if nat_mod_distrib)
nipkow@31952
  1012
  apply (auto simp add: gcd_non_0_nat nat_mod_distrib [symmetric]
huffman@31706
  1013
    zmod_zminus1_eq_if)
huffman@31706
  1014
  apply (frule_tac a = x in pos_mod_bound)
haftmann@62344
  1015
  apply (subst (1 2) gcd.commute)
nipkow@31952
  1016
  apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2_nat
huffman@31706
  1017
    nat_le_eq_zle)
haftmann@62344
  1018
  done
wenzelm@21256
  1019
nipkow@31952
  1020
lemma gcd_red_int: "gcd (x::int) y = gcd y (x mod y)"
huffman@31706
  1021
  apply (case_tac "y = 0")
huffman@31706
  1022
  apply force
huffman@31706
  1023
  apply (case_tac "y > 0")
nipkow@31952
  1024
  apply (subst gcd_non_0_int, auto)
nipkow@31952
  1025
  apply (insert gcd_non_0_int [of "-y" "-x"])
huffman@35216
  1026
  apply auto
huffman@31706
  1027
done
huffman@31706
  1028
nipkow@31952
  1029
lemma gcd_add1_int [simp]: "gcd ((m::int) + n) n = gcd m n"
haftmann@57512
  1030
by (metis gcd_red_int mod_add_self1 add.commute)
huffman@31706
  1031
nipkow@31952
  1032
lemma gcd_add2_int [simp]: "gcd m ((m::int) + n) = gcd m n"
haftmann@62344
  1033
by (metis gcd_add1_int gcd.commute add.commute)
wenzelm@21256
  1034
nipkow@31952
  1035
lemma gcd_add_mult_nat: "gcd (m::nat) (k * m + n) = gcd m n"
haftmann@62344
  1036
by (metis mod_mult_self3 gcd.commute gcd_red_nat)
wenzelm@21256
  1037
nipkow@31952
  1038
lemma gcd_add_mult_int: "gcd (m::int) (k * m + n) = gcd m n"
haftmann@62344
  1039
by (metis gcd.commute gcd_red_int mod_mult_self1 add.commute)
nipkow@31798
  1040
wenzelm@21256
  1041
huffman@31706
  1042
(* to do: differences, and all variations of addition rules
huffman@31706
  1043
    as simplification rules for nat and int *)
huffman@31706
  1044
haftmann@60689
  1045
lemma gcd_dvd_prod_nat: "gcd (m::nat) n dvd k * n"
haftmann@23687
  1046
  using mult_dvd_mono [of 1] by auto
chaieb@22027
  1047
huffman@31706
  1048
(* to do: add the three variations of these, and for ints? *)
huffman@31706
  1049
nipkow@31992
  1050
lemma finite_divisors_nat[simp]:
nipkow@31992
  1051
  assumes "(m::nat) ~= 0" shows "finite{d. d dvd m}"
nipkow@31734
  1052
proof-
wenzelm@60512
  1053
  have "finite{d. d <= m}"
wenzelm@60512
  1054
    by (blast intro: bounded_nat_set_is_finite)
nipkow@31734
  1055
  from finite_subset[OF _ this] show ?thesis using assms
wenzelm@60512
  1056
    by (metis Collect_mono dvd_imp_le neq0_conv)
nipkow@31734
  1057
qed
nipkow@31734
  1058
nipkow@31995
  1059
lemma finite_divisors_int[simp]:
nipkow@31734
  1060
  assumes "(i::int) ~= 0" shows "finite{d. d dvd i}"
nipkow@31734
  1061
proof-
wenzelm@61944
  1062
  have "{d. \<bar>d\<bar> <= \<bar>i\<bar>} = {- \<bar>i\<bar> .. \<bar>i\<bar>}" by(auto simp:abs_if)
wenzelm@61944
  1063
  hence "finite {d. \<bar>d\<bar> <= \<bar>i\<bar>}" by simp
nipkow@31734
  1064
  from finite_subset[OF _ this] show ?thesis using assms
wenzelm@60512
  1065
    by (simp add: dvd_imp_le_int subset_iff)
nipkow@31734
  1066
qed
nipkow@31734
  1067
nipkow@31995
  1068
lemma Max_divisors_self_nat[simp]: "n\<noteq>0 \<Longrightarrow> Max{d::nat. d dvd n} = n"
nipkow@31995
  1069
apply(rule antisym)
nipkow@44890
  1070
 apply (fastforce intro: Max_le_iff[THEN iffD2] simp: dvd_imp_le)
nipkow@31995
  1071
apply simp
nipkow@31995
  1072
done
nipkow@31995
  1073
wenzelm@61944
  1074
lemma Max_divisors_self_int[simp]: "n\<noteq>0 \<Longrightarrow> Max{d::int. d dvd n} = \<bar>n\<bar>"
nipkow@31995
  1075
apply(rule antisym)
haftmann@44278
  1076
 apply(rule Max_le_iff [THEN iffD2])
haftmann@44278
  1077
  apply (auto intro: abs_le_D1 dvd_imp_le_int)
nipkow@31995
  1078
done
nipkow@31995
  1079
nipkow@31734
  1080
lemma gcd_is_Max_divisors_nat:
nipkow@31734
  1081
  "m ~= 0 \<Longrightarrow> n ~= 0 \<Longrightarrow> gcd (m::nat) n = (Max {d. d dvd m & d dvd n})"
nipkow@31734
  1082
apply(rule Max_eqI[THEN sym])
nipkow@31995
  1083
  apply (metis finite_Collect_conjI finite_divisors_nat)
nipkow@31734
  1084
 apply simp
haftmann@62344
  1085
 apply(metis Suc_diff_1 Suc_neq_Zero dvd_imp_le gcd_greatest_iff gcd_pos_nat)
nipkow@31734
  1086
apply simp
nipkow@31734
  1087
done
nipkow@31734
  1088
nipkow@31734
  1089
lemma gcd_is_Max_divisors_int:
nipkow@31734
  1090
  "m ~= 0 ==> n ~= 0 ==> gcd (m::int) n = (Max {d. d dvd m & d dvd n})"
nipkow@31734
  1091
apply(rule Max_eqI[THEN sym])
nipkow@31995
  1092
  apply (metis finite_Collect_conjI finite_divisors_int)
nipkow@31734
  1093
 apply simp
haftmann@62344
  1094
 apply (metis gcd_greatest_iff gcd_pos_int zdvd_imp_le)
nipkow@31734
  1095
apply simp
nipkow@31734
  1096
done
nipkow@31734
  1097
haftmann@34030
  1098
lemma gcd_code_int [code]:
haftmann@34030
  1099
  "gcd k l = \<bar>if l = (0::int) then k else gcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
haftmann@34030
  1100
  by (simp add: gcd_int_def nat_mod_distrib gcd_non_0_nat)
haftmann@34030
  1101
chaieb@22027
  1102
wenzelm@60758
  1103
subsection \<open>Coprimality\<close>
huffman@31706
  1104
haftmann@60690
  1105
context semiring_gcd
haftmann@60690
  1106
begin
haftmann@60690
  1107
haftmann@60690
  1108
lemma div_gcd_coprime:
haftmann@60690
  1109
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
huffman@31706
  1110
  shows "coprime (a div gcd a b) (b div gcd a b)"
wenzelm@22367
  1111
proof -
haftmann@27556
  1112
  let ?g = "gcd a b"
chaieb@22027
  1113
  let ?a' = "a div ?g"
chaieb@22027
  1114
  let ?b' = "b div ?g"
haftmann@27556
  1115
  let ?g' = "gcd ?a' ?b'"
chaieb@22027
  1116
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
  1117
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
wenzelm@22367
  1118
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
  1119
      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
chaieb@22027
  1120
    unfolding dvd_def by blast
haftmann@58834
  1121
  from this [symmetric] have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'"
haftmann@58834
  1122
    by (simp_all add: mult.assoc mult.left_commute [of "gcd a b"])
wenzelm@22367
  1123
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
  1124
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
  1125
      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
huffman@35216
  1126
  have "?g \<noteq> 0" using nz by simp
haftmann@60690
  1127
  moreover from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
haftmann@60690
  1128
  thm dvd_mult_cancel_left
haftmann@60690
  1129
  ultimately show ?thesis using dvd_times_left_cancel_iff [of "gcd a b" _ 1] by simp
chaieb@22027
  1130
qed
chaieb@22027
  1131
haftmann@62345
  1132
lemma coprime:
haftmann@62345
  1133
  "coprime a b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62345
  1134
proof
haftmann@62345
  1135
  assume ?P then show ?Q by auto
haftmann@62345
  1136
next
haftmann@62345
  1137
  assume ?Q
haftmann@62345
  1138
  then have "is_unit (gcd a b) \<longleftrightarrow> gcd a b dvd a \<and> gcd a b dvd b"
haftmann@62345
  1139
    by blast
haftmann@62345
  1140
  then have "is_unit (gcd a b)"
haftmann@62345
  1141
    by simp
haftmann@62345
  1142
  then show ?P
haftmann@62345
  1143
    by simp
haftmann@62345
  1144
qed
haftmann@62345
  1145
haftmann@60690
  1146
end
haftmann@60690
  1147
haftmann@62345
  1148
lemma coprime_nat:
haftmann@62345
  1149
  "coprime (a::nat) b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
haftmann@62345
  1150
  using coprime [of a b] by simp
huffman@31706
  1151
nipkow@31952
  1152
lemma coprime_Suc_0_nat:
haftmann@62345
  1153
  "coprime (a::nat) b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = Suc 0)"
haftmann@60690
  1154
  using coprime_nat by simp
huffman@31706
  1155
haftmann@62345
  1156
lemma coprime_int:
haftmann@62345
  1157
  "coprime (a::int) b \<longleftrightarrow> (\<forall>d. d \<ge> 0 \<and> d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
nipkow@31952
  1158
  using gcd_unique_int [of 1 a b]
huffman@31706
  1159
  apply clarsimp
huffman@31706
  1160
  apply (erule subst)
huffman@31706
  1161
  apply (rule iffI)
huffman@31706
  1162
  apply force
lp15@61649
  1163
  using abs_dvd_iff abs_ge_zero apply blast
wenzelm@59807
  1164
  done
huffman@31706
  1165
nipkow@31952
  1166
lemma gcd_coprime_nat:
huffman@31706
  1167
  assumes z: "gcd (a::nat) b \<noteq> 0" and a: "a = a' * gcd a b" and
huffman@31706
  1168
    b: "b = b' * gcd a b"
huffman@31706
  1169
  shows    "coprime a' b'"
huffman@31706
  1170
huffman@31706
  1171
  apply (subgoal_tac "a' = a div gcd a b")
huffman@31706
  1172
  apply (erule ssubst)
huffman@31706
  1173
  apply (subgoal_tac "b' = b div gcd a b")
huffman@31706
  1174
  apply (erule ssubst)
haftmann@62344
  1175
  apply (rule div_gcd_coprime)
wenzelm@41550
  1176
  using z apply force
huffman@31706
  1177
  apply (subst (1) b)
huffman@31706
  1178
  using z apply force
huffman@31706
  1179
  apply (subst (1) a)
huffman@31706
  1180
  using z apply force
wenzelm@41550
  1181
  done
huffman@31706
  1182
nipkow@31952
  1183
lemma gcd_coprime_int:
huffman@31706
  1184
  assumes z: "gcd (a::int) b \<noteq> 0" and a: "a = a' * gcd a b" and
huffman@31706
  1185
    b: "b = b' * gcd a b"
huffman@31706
  1186
  shows    "coprime a' b'"
huffman@31706
  1187
  apply (subgoal_tac "a' = a div gcd a b")
huffman@31706
  1188
  apply (erule ssubst)
huffman@31706
  1189
  apply (subgoal_tac "b' = b div gcd a b")
huffman@31706
  1190
  apply (erule ssubst)
haftmann@62344
  1191
  apply (rule div_gcd_coprime)
wenzelm@41550
  1192
  using z apply force
huffman@31706
  1193
  apply (subst (1) b)
huffman@31706
  1194
  using z apply force
huffman@31706
  1195
  apply (subst (1) a)
huffman@31706
  1196
  using z apply force
wenzelm@41550
  1197
  done
huffman@31706
  1198
haftmann@60690
  1199
context semiring_gcd
haftmann@60690
  1200
begin
huffman@31706
  1201
haftmann@60690
  1202
lemma coprime_mult:
haftmann@60690
  1203
  assumes da: "coprime d a" and db: "coprime d b"
haftmann@60690
  1204
  shows "coprime d (a * b)"
haftmann@60690
  1205
  apply (subst gcd.commute)
haftmann@60690
  1206
  using da apply (subst gcd_mult_cancel)
haftmann@60690
  1207
  apply (subst gcd.commute, assumption)
haftmann@60690
  1208
  apply (subst gcd.commute, rule db)
haftmann@60690
  1209
  done
huffman@31706
  1210
haftmann@60690
  1211
end
haftmann@60690
  1212
nipkow@31952
  1213
lemma coprime_lmult_nat:
huffman@31706
  1214
  assumes dab: "coprime (d::nat) (a * b)" shows "coprime d a"
huffman@31706
  1215
proof -
huffman@31706
  1216
  have "gcd d a dvd gcd d (a * b)"
haftmann@59008
  1217
    by (rule gcd_greatest, auto)
huffman@31706
  1218
  with dab show ?thesis
huffman@31706
  1219
    by auto
huffman@31706
  1220
qed
huffman@31706
  1221
nipkow@31952
  1222
lemma coprime_lmult_int:
nipkow@31798
  1223
  assumes "coprime (d::int) (a * b)" shows "coprime d a"
huffman@31706
  1224
proof -
huffman@31706
  1225
  have "gcd d a dvd gcd d (a * b)"
haftmann@59008
  1226
    by (rule gcd_greatest, auto)
nipkow@31798
  1227
  with assms show ?thesis
huffman@31706
  1228
    by auto
huffman@31706
  1229
qed
huffman@31706
  1230
nipkow@31952
  1231
lemma coprime_rmult_nat:
nipkow@31798
  1232
  assumes "coprime (d::nat) (a * b)" shows "coprime d b"
huffman@31706
  1233
proof -
huffman@31706
  1234
  have "gcd d b dvd gcd d (a * b)"
haftmann@59008
  1235
    by (rule gcd_greatest, auto intro: dvd_mult)
nipkow@31798
  1236
  with assms show ?thesis
huffman@31706
  1237
    by auto
huffman@31706
  1238
qed
huffman@31706
  1239
nipkow@31952
  1240
lemma coprime_rmult_int:
huffman@31706
  1241
  assumes dab: "coprime (d::int) (a * b)" shows "coprime d b"
huffman@31706
  1242
proof -
huffman@31706
  1243
  have "gcd d b dvd gcd d (a * b)"
haftmann@59008
  1244
    by (rule gcd_greatest, auto intro: dvd_mult)
huffman@31706
  1245
  with dab show ?thesis
huffman@31706
  1246
    by auto
huffman@31706
  1247
qed
huffman@31706
  1248
nipkow@31952
  1249
lemma coprime_mul_eq_nat: "coprime (d::nat) (a * b) \<longleftrightarrow>
huffman@31706
  1250
    coprime d a \<and>  coprime d b"
nipkow@31952
  1251
  using coprime_rmult_nat[of d a b] coprime_lmult_nat[of d a b]
haftmann@62344
  1252
    coprime_mult [of d a b]
huffman@31706
  1253
  by blast
huffman@31706
  1254
nipkow@31952
  1255
lemma coprime_mul_eq_int: "coprime (d::int) (a * b) \<longleftrightarrow>
huffman@31706
  1256
    coprime d a \<and>  coprime d b"
nipkow@31952
  1257
  using coprime_rmult_int[of d a b] coprime_lmult_int[of d a b]
haftmann@62344
  1258
    coprime_mult [of d a b]
huffman@31706
  1259
  by blast
huffman@31706
  1260
noschinl@52397
  1261
lemma coprime_power_int:
noschinl@52397
  1262
  assumes "0 < n" shows "coprime (a :: int) (b ^ n) \<longleftrightarrow> coprime a b"
noschinl@52397
  1263
  using assms
noschinl@52397
  1264
proof (induct n)
noschinl@52397
  1265
  case (Suc n) then show ?case
noschinl@52397
  1266
    by (cases n) (simp_all add: coprime_mul_eq_int)
noschinl@52397
  1267
qed simp
noschinl@52397
  1268
nipkow@31952
  1269
lemma gcd_coprime_exists_nat:
huffman@31706
  1270
    assumes nz: "gcd (a::nat) b \<noteq> 0"
huffman@31706
  1271
    shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
huffman@31706
  1272
  apply (rule_tac x = "a div gcd a b" in exI)
huffman@31706
  1273
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@62344
  1274
  using nz apply (auto simp add: div_gcd_coprime dvd_div_mult)
huffman@31706
  1275
done
huffman@31706
  1276
nipkow@31952
  1277
lemma gcd_coprime_exists_int:
huffman@31706
  1278
    assumes nz: "gcd (a::int) b \<noteq> 0"
huffman@31706
  1279
    shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
huffman@31706
  1280
  apply (rule_tac x = "a div gcd a b" in exI)
huffman@31706
  1281
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@62344
  1282
  using nz apply (auto simp add: div_gcd_coprime)
huffman@31706
  1283
done
huffman@31706
  1284
nipkow@31952
  1285
lemma coprime_exp_nat: "coprime (d::nat) a \<Longrightarrow> coprime d (a^n)"
haftmann@62344
  1286
  by (induct n) (simp_all add: coprime_mult)
huffman@31706
  1287
nipkow@31952
  1288
lemma coprime_exp_int: "coprime (d::int) a \<Longrightarrow> coprime d (a^n)"
haftmann@62344
  1289
  by (induct n) (simp_all add: coprime_mult)
huffman@31706
  1290
haftmann@60690
  1291
context semiring_gcd
haftmann@60690
  1292
begin
haftmann@60690
  1293
haftmann@60690
  1294
lemma coprime_exp_left:
haftmann@60690
  1295
  assumes "coprime a b"
haftmann@60690
  1296
  shows "coprime (a ^ n) b"
haftmann@60690
  1297
  using assms by (induct n) (simp_all add: gcd_mult_cancel)
haftmann@60690
  1298
haftmann@60690
  1299
lemma coprime_exp2:
haftmann@60690
  1300
  assumes "coprime a b"
haftmann@60690
  1301
  shows "coprime (a ^ n) (b ^ m)"
haftmann@60690
  1302
proof (rule coprime_exp_left)
haftmann@60690
  1303
  from assms show "coprime a (b ^ m)"
haftmann@60690
  1304
    by (induct m) (simp_all add: gcd_mult_cancel gcd.commute [of a])
haftmann@60690
  1305
qed
haftmann@60690
  1306
haftmann@60690
  1307
end
haftmann@60690
  1308
haftmann@60690
  1309
lemma gcd_exp_nat:
haftmann@60690
  1310
  "gcd ((a :: nat) ^ n) (b ^ n) = gcd a b ^ n"
haftmann@60690
  1311
proof (cases "a = 0 \<and> b = 0")
haftmann@60690
  1312
  case True then show ?thesis by (cases "n > 0") (simp_all add: zero_power)
haftmann@60690
  1313
next
haftmann@60690
  1314
  case False
haftmann@60690
  1315
  then have "coprime (a div gcd a b) (b div gcd a b)"
haftmann@60690
  1316
    by (auto simp: div_gcd_coprime)
haftmann@60690
  1317
  then have "coprime ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
haftmann@60690
  1318
    by (simp add: coprime_exp2)
haftmann@60690
  1319
  then have "gcd ((a div gcd a b)^n * (gcd a b)^n)
huffman@31706
  1320
      ((b div gcd a b)^n * (gcd a b)^n) = (gcd a b)^n"
lp15@60162
  1321
    by (metis gcd_mult_distrib_nat mult.commute mult.right_neutral)
huffman@31706
  1322
  also have "(a div gcd a b)^n * (gcd a b)^n = a^n"
lp15@60162
  1323
    by (metis dvd_div_mult_self gcd_unique_nat power_mult_distrib)
huffman@31706
  1324
  also have "(b div gcd a b)^n * (gcd a b)^n = b^n"
lp15@60162
  1325
    by (metis dvd_div_mult_self gcd_unique_nat power_mult_distrib)
huffman@31706
  1326
  finally show ?thesis .
huffman@31706
  1327
qed
huffman@31706
  1328
nipkow@31952
  1329
lemma gcd_exp_int: "gcd ((a::int)^n) (b^n) = (gcd a b)^n"
nipkow@31952
  1330
  apply (subst (1 2) gcd_abs_int)
huffman@31706
  1331
  apply (subst (1 2) power_abs)
nipkow@31952
  1332
  apply (rule gcd_exp_nat [where n = n, transferred])
huffman@31706
  1333
  apply auto
huffman@31706
  1334
done
huffman@31706
  1335
nipkow@31952
  1336
lemma division_decomp_nat: assumes dc: "(a::nat) dvd b * c"
huffman@31706
  1337
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
huffman@31706
  1338
proof-
huffman@31706
  1339
  let ?g = "gcd a b"
huffman@31706
  1340
  {assume "?g = 0" with dc have ?thesis by auto}
huffman@31706
  1341
  moreover
huffman@31706
  1342
  {assume z: "?g \<noteq> 0"
nipkow@31952
  1343
    from gcd_coprime_exists_nat[OF z]
huffman@31706
  1344
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1345
      by blast
huffman@31706
  1346
    have thb: "?g dvd b" by auto
huffman@31706
  1347
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast
huffman@31706
  1348
    with dc have th0: "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
huffman@31706
  1349
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
haftmann@57512
  1350
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: mult.assoc)
huffman@31706
  1351
    with z have th_1: "a' dvd b' * c" by auto
haftmann@62344
  1352
    from coprime_dvd_mult [OF ab'(3)] th_1
haftmann@57512
  1353
    have thc: "a' dvd c" by (subst (asm) mult.commute, blast)
huffman@31706
  1354
    from ab' have "a = ?g*a'" by algebra
huffman@31706
  1355
    with thb thc have ?thesis by blast }
huffman@31706
  1356
  ultimately show ?thesis by blast
huffman@31706
  1357
qed
huffman@31706
  1358
nipkow@31952
  1359
lemma division_decomp_int: assumes dc: "(a::int) dvd b * c"
huffman@31706
  1360
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
huffman@31706
  1361
proof-
huffman@31706
  1362
  let ?g = "gcd a b"
huffman@31706
  1363
  {assume "?g = 0" with dc have ?thesis by auto}
huffman@31706
  1364
  moreover
huffman@31706
  1365
  {assume z: "?g \<noteq> 0"
nipkow@31952
  1366
    from gcd_coprime_exists_int[OF z]
huffman@31706
  1367
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1368
      by blast
huffman@31706
  1369
    have thb: "?g dvd b" by auto
huffman@31706
  1370
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast
huffman@31706
  1371
    with dc have th0: "a' dvd b*c"
huffman@31706
  1372
      using dvd_trans[of a' a "b*c"] by simp
huffman@31706
  1373
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
haftmann@60690
  1374
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: ac_simps)
huffman@31706
  1375
    with z have th_1: "a' dvd b' * c" by auto
haftmann@62344
  1376
    from coprime_dvd_mult [OF ab'(3)] th_1
haftmann@57512
  1377
    have thc: "a' dvd c" by (subst (asm) mult.commute, blast)
huffman@31706
  1378
    from ab' have "a = ?g*a'" by algebra
huffman@31706
  1379
    with thb thc have ?thesis by blast }
huffman@31706
  1380
  ultimately show ?thesis by blast
chaieb@27669
  1381
qed
chaieb@27669
  1382
nipkow@31952
  1383
lemma pow_divides_pow_nat:
huffman@31706
  1384
  assumes ab: "(a::nat) ^ n dvd b ^n" and n:"n \<noteq> 0"
huffman@31706
  1385
  shows "a dvd b"
huffman@31706
  1386
proof-
huffman@31706
  1387
  let ?g = "gcd a b"
huffman@31706
  1388
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
huffman@31706
  1389
  {assume "?g = 0" with ab n have ?thesis by auto }
huffman@31706
  1390
  moreover
huffman@31706
  1391
  {assume z: "?g \<noteq> 0"
huffman@35216
  1392
    hence zn: "?g ^ n \<noteq> 0" using n by simp
nipkow@31952
  1393
    from gcd_coprime_exists_nat[OF z]
huffman@31706
  1394
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1395
      by blast
huffman@31706
  1396
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n"
huffman@31706
  1397
      by (simp add: ab'(1,2)[symmetric])
huffman@31706
  1398
    hence "?g^n*a'^n dvd ?g^n *b'^n"
haftmann@57512
  1399
      by (simp only: power_mult_distrib mult.commute)
haftmann@58787
  1400
    then have th0: "a'^n dvd b'^n"
haftmann@58787
  1401
      using zn by auto
huffman@31706
  1402
    have "a' dvd a'^n" by (simp add: m)
huffman@31706
  1403
    with th0 have "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by simp
haftmann@57512
  1404
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult.commute)
haftmann@62344
  1405
    from coprime_dvd_mult [OF coprime_exp_nat [OF ab'(3), of m]] th1
haftmann@57512
  1406
    have "a' dvd b'" by (subst (asm) mult.commute, blast)
huffman@31706
  1407
    hence "a'*?g dvd b'*?g" by simp
huffman@31706
  1408
    with ab'(1,2)  have ?thesis by simp }
huffman@31706
  1409
  ultimately show ?thesis by blast
huffman@31706
  1410
qed
huffman@31706
  1411
nipkow@31952
  1412
lemma pow_divides_pow_int:
huffman@31706
  1413
  assumes ab: "(a::int) ^ n dvd b ^n" and n:"n \<noteq> 0"
huffman@31706
  1414
  shows "a dvd b"
chaieb@27669
  1415
proof-
huffman@31706
  1416
  let ?g = "gcd a b"
huffman@31706
  1417
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
huffman@31706
  1418
  {assume "?g = 0" with ab n have ?thesis by auto }
huffman@31706
  1419
  moreover
huffman@31706
  1420
  {assume z: "?g \<noteq> 0"
huffman@35216
  1421
    hence zn: "?g ^ n \<noteq> 0" using n by simp
nipkow@31952
  1422
    from gcd_coprime_exists_int[OF z]
huffman@31706
  1423
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'"
huffman@31706
  1424
      by blast
huffman@31706
  1425
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n"
huffman@31706
  1426
      by (simp add: ab'(1,2)[symmetric])
huffman@31706
  1427
    hence "?g^n*a'^n dvd ?g^n *b'^n"
haftmann@57512
  1428
      by (simp only: power_mult_distrib mult.commute)
huffman@31706
  1429
    with zn z n have th0:"a'^n dvd b'^n" by auto
huffman@31706
  1430
    have "a' dvd a'^n" by (simp add: m)
huffman@31706
  1431
    with th0 have "a' dvd b'^n"
huffman@31706
  1432
      using dvd_trans[of a' "a'^n" "b'^n"] by simp
haftmann@60596
  1433
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult.commute)
haftmann@62344
  1434
    from coprime_dvd_mult [OF coprime_exp_int [OF ab'(3), of m]] th1
haftmann@57512
  1435
    have "a' dvd b'" by (subst (asm) mult.commute, blast)
huffman@31706
  1436
    hence "a'*?g dvd b'*?g" by simp
huffman@31706
  1437
    with ab'(1,2)  have ?thesis by simp }
huffman@31706
  1438
  ultimately show ?thesis by blast
huffman@31706
  1439
qed
huffman@31706
  1440
nipkow@31952
  1441
lemma pow_divides_eq_nat [simp]: "n ~= 0 \<Longrightarrow> ((a::nat)^n dvd b^n) = (a dvd b)"
nipkow@31952
  1442
  by (auto intro: pow_divides_pow_nat dvd_power_same)
huffman@31706
  1443
nipkow@31952
  1444
lemma pow_divides_eq_int [simp]: "n ~= 0 \<Longrightarrow> ((a::int)^n dvd b^n) = (a dvd b)"
nipkow@31952
  1445
  by (auto intro: pow_divides_pow_int dvd_power_same)
huffman@31706
  1446
nipkow@31952
  1447
lemma divides_mult_nat:
huffman@31706
  1448
  assumes mr: "(m::nat) dvd r" and nr: "n dvd r" and mn:"coprime m n"
huffman@31706
  1449
  shows "m * n dvd r"
huffman@31706
  1450
proof-
huffman@31706
  1451
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
huffman@31706
  1452
    unfolding dvd_def by blast
haftmann@57512
  1453
  from mr n' have "m dvd n'*n" by (simp add: mult.commute)
haftmann@62344
  1454
  hence "m dvd n'" using coprime_dvd_mult_iff [OF mn] by simp
huffman@31706
  1455
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
huffman@31706
  1456
  from n' k show ?thesis unfolding dvd_def by auto
huffman@31706
  1457
qed
huffman@31706
  1458
nipkow@31952
  1459
lemma divides_mult_int:
huffman@31706
  1460
  assumes mr: "(m::int) dvd r" and nr: "n dvd r" and mn:"coprime m n"
huffman@31706
  1461
  shows "m * n dvd r"
huffman@31706
  1462
proof-
huffman@31706
  1463
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
huffman@31706
  1464
    unfolding dvd_def by blast
haftmann@57512
  1465
  from mr n' have "m dvd n'*n" by (simp add: mult.commute)
haftmann@62344
  1466
  hence "m dvd n'" using coprime_dvd_mult_iff [OF mn] by simp
huffman@31706
  1467
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
huffman@31706
  1468
  from n' k show ?thesis unfolding dvd_def by auto
chaieb@27669
  1469
qed
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_0_nat [simp]: "lcm (m::nat) 0 = 0"
huffman@31706
  1777
  unfolding lcm_nat_def by simp
huffman@31706
  1778
nipkow@31952
  1779
lemma lcm_0_int [simp]: "lcm (m::int) 0 = 0"
huffman@31706
  1780
  unfolding lcm_int_def by simp
huffman@31706
  1781
nipkow@31952
  1782
lemma lcm_0_left_nat [simp]: "lcm (0::nat) n = 0"
huffman@31706
  1783
  unfolding lcm_nat_def by simp
chaieb@27669
  1784
nipkow@31952
  1785
lemma lcm_0_left_int [simp]: "lcm (0::int) n = 0"
huffman@31706
  1786
  unfolding lcm_int_def by simp
huffman@31706
  1787
nipkow@31952
  1788
lemma lcm_pos_nat:
nipkow@31798
  1789
  "(m::nat) > 0 \<Longrightarrow> n>0 \<Longrightarrow> lcm m n > 0"
nipkow@31952
  1790
by (metis gr0I mult_is_0 prod_gcd_lcm_nat)
chaieb@27669
  1791
nipkow@31952
  1792
lemma lcm_pos_int:
nipkow@31798
  1793
  "(m::int) ~= 0 \<Longrightarrow> n ~= 0 \<Longrightarrow> lcm m n > 0"
nipkow@31952
  1794
  apply (subst lcm_abs_int)
nipkow@31952
  1795
  apply (rule lcm_pos_nat [transferred])
nipkow@31798
  1796
  apply auto
haftmann@62344
  1797
  done
haftmann@23687
  1798
nipkow@31952
  1799
lemma dvd_pos_nat:
haftmann@23687
  1800
  fixes n m :: nat
haftmann@23687
  1801
  assumes "n > 0" and "m dvd n"
haftmann@23687
  1802
  shows "m > 0"
haftmann@62344
  1803
  using assms by (cases m) auto
nipkow@31729
  1804
nipkow@31952
  1805
lemma lcm_unique_nat: "(a::nat) dvd d \<and> b dvd d \<and>
huffman@31706
  1806
    (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@62344
  1807
  by (auto intro: dvd_antisym lcm_least)
chaieb@27568
  1808
nipkow@31952
  1809
lemma lcm_unique_int: "d >= 0 \<and> (a::int) dvd d \<and> b dvd d \<and>
huffman@31706
  1810
    (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@62344
  1811
  using lcm_least zdvd_antisym_nonneg by auto
haftmann@34973
  1812
nipkow@31798
  1813
lemma lcm_proj2_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> lcm x y = y"
huffman@31706
  1814
  apply (rule sym)
nipkow@31952
  1815
  apply (subst lcm_unique_nat [symmetric])
huffman@31706
  1816
  apply auto
huffman@31706
  1817
done
huffman@31706
  1818
wenzelm@61944
  1819
lemma lcm_proj2_if_dvd_int [simp]: "(x::int) dvd y \<Longrightarrow> lcm x y = \<bar>y\<bar>"
huffman@31706
  1820
  apply (rule sym)
nipkow@31952
  1821
  apply (subst lcm_unique_int [symmetric])
huffman@31706
  1822
  apply auto
huffman@31706
  1823
done
huffman@31706
  1824
nipkow@31798
  1825
lemma lcm_proj1_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> lcm y x = y"
haftmann@62344
  1826
by (subst lcm.commute, erule lcm_proj2_if_dvd_nat)
huffman@31706
  1827
wenzelm@61944
  1828
lemma lcm_proj1_if_dvd_int [simp]: "(x::int) dvd y \<Longrightarrow> lcm y x = \<bar>y\<bar>"
haftmann@62344
  1829
by (subst lcm.commute, erule lcm_proj2_if_dvd_int)
huffman@31706
  1830
nipkow@31992
  1831
lemma lcm_proj1_iff_nat[simp]: "lcm m n = (m::nat) \<longleftrightarrow> n dvd m"
nipkow@31992
  1832
by (metis lcm_proj1_if_dvd_nat lcm_unique_nat)
nipkow@31992
  1833
nipkow@31992
  1834
lemma lcm_proj2_iff_nat[simp]: "lcm m n = (n::nat) \<longleftrightarrow> m dvd n"
nipkow@31992
  1835
by (metis lcm_proj2_if_dvd_nat lcm_unique_nat)
nipkow@31992
  1836
wenzelm@61944
  1837
lemma lcm_proj1_iff_int[simp]: "lcm m n = \<bar>m::int\<bar> \<longleftrightarrow> n dvd m"
nipkow@31992
  1838
by (metis dvd_abs_iff lcm_proj1_if_dvd_int lcm_unique_int)
nipkow@31992
  1839
wenzelm@61944
  1840
lemma lcm_proj2_iff_int[simp]: "lcm m n = \<bar>n::int\<bar> \<longleftrightarrow> m dvd n"
nipkow@31992
  1841
by (metis dvd_abs_iff lcm_proj2_if_dvd_int lcm_unique_int)
chaieb@27568
  1842
haftmann@61913
  1843
lemma (in semiring_gcd) comp_fun_idem_gcd:
haftmann@61913
  1844
  "comp_fun_idem gcd"
haftmann@61913
  1845
  by standard (simp_all add: fun_eq_iff ac_simps)
haftmann@61913
  1846
haftmann@61913
  1847
lemma (in semiring_gcd) comp_fun_idem_lcm:
haftmann@61913
  1848
  "comp_fun_idem lcm"
haftmann@61913
  1849
  by standard (simp_all add: fun_eq_iff ac_simps)
haftmann@61913
  1850
haftmann@61913
  1851
lemma lcm_1_iff_nat [simp]: "lcm (m::nat) n = 1 \<longleftrightarrow> m=1 \<and> n=1"
haftmann@61913
  1852
  by (simp only: lcm_eq_1_iff) simp
haftmann@61913
  1853
  
haftmann@61913
  1854
lemma lcm_1_iff_int [simp]: "lcm (m::int) n = 1 \<longleftrightarrow> (m=1 \<or> m = -1) \<and> (n=1 \<or> n = -1)"
haftmann@61913
  1855
  by auto
nipkow@31995
  1856
haftmann@34030
  1857
haftmann@62345
  1858
subsection \<open>The complete divisibility lattice on @{typ nat} and @{typ int}\<close>
nipkow@32112
  1859
wenzelm@60758
  1860
text\<open>Lifting gcd and lcm to sets (Gcd/Lcm).
huffman@45264
  1861
Gcd is defined via Lcm to facilitate the proof that we have a complete lattice.
wenzelm@60758
  1862
\<close>
huffman@45264
  1863
haftmann@62345
  1864
instantiation nat :: semiring_Gcd
nipkow@32112
  1865
begin
nipkow@32112
  1866
haftmann@62344
  1867
interpretation semilattice_neutr_set lcm "1::nat"
haftmann@62344
  1868
  by standard simp_all
haftmann@54867
  1869
haftmann@61929
  1870
definition
haftmann@61929
  1871
  "Lcm (M::nat set) = (if finite M then F M else 0)"
haftmann@51489
  1872
haftmann@51489
  1873
lemma Lcm_nat_empty:
haftmann@51489
  1874
  "Lcm {} = (1::nat)"
haftmann@60690
  1875
  by (simp add: Lcm_nat_def del: One_nat_def)
haftmann@51489
  1876
haftmann@51489
  1877
lemma Lcm_nat_insert:
haftmann@51489
  1878
  "Lcm (insert n M) = lcm (n::nat) (Lcm M)"
haftmann@61929
  1879
  by (cases "finite M") (auto simp add: Lcm_nat_def simp del: One_nat_def)
haftmann@61929
  1880
haftmann@61929
  1881
lemma Lcm_nat_infinite:
haftmann@61929
  1882
  "infinite M \<Longrightarrow> Lcm M = (0::nat)"
haftmann@61929
  1883
  by (simp add: Lcm_nat_def)
haftmann@61929
  1884
haftmann@61929
  1885
lemma dvd_Lcm_nat [simp]:
haftmann@61929
  1886
  fixes M :: "nat set"
haftmann@61929
  1887
  assumes "m \<in> M"
haftmann@61929
  1888
  shows "m dvd Lcm M"
haftmann@61929
  1889
proof -
haftmann@61929
  1890
  from assms have "insert m M = M" by auto
haftmann@61929
  1891
  moreover have "m dvd Lcm (insert m M)"
haftmann@61929
  1892
    by (simp add: Lcm_nat_insert)
haftmann@61929
  1893
  ultimately show ?thesis by simp
haftmann@61929
  1894
qed
haftmann@61929
  1895
haftmann@61929
  1896
lemma Lcm_dvd_nat [simp]:
haftmann@61929
  1897
  fixes M :: "nat set"
haftmann@61929
  1898
  assumes "\<forall>m\<in>M. m dvd n"
haftmann@61929
  1899
  shows "Lcm M dvd n"
haftmann@61929
  1900
proof (cases "n = 0")
haftmann@61929
  1901
  case True then show ?thesis by simp
haftmann@61929
  1902
next
haftmann@61929
  1903
  case False
haftmann@61929
  1904
  then have "finite {d. d dvd n}" by (rule finite_divisors_nat)
haftmann@61929
  1905
  moreover have "M \<subseteq> {d. d dvd n}" using assms by fast
haftmann@61929
  1906
  ultimately have "finite M" by (rule rev_finite_subset)
haftmann@61929
  1907
  then show ?thesis using assms by (induct M) (simp_all add: Lcm_nat_empty Lcm_nat_insert)
haftmann@61929
  1908
qed
nipkow@32112
  1909
huffman@45264
  1910
definition
huffman@45264
  1911
  "Gcd (M::nat set) = Lcm {d. \<forall>m\<in>M. d dvd m}"
nipkow@32112
  1912
haftmann@62345
  1913
instance proof
haftmann@61929
  1914
  show "Gcd N dvd n" if "n \<in> N" for N and n :: nat
haftmann@61929
  1915
  using that by (induct N rule: infinite_finite_induct)
haftmann@61929
  1916
    (auto simp add: Gcd_nat_def)
haftmann@61929
  1917
  show "n dvd Gcd N" if "\<And>m. m \<in> N \<Longrightarrow> n dvd m" for N and n :: nat
haftmann@61929
  1918
  using that by (induct N rule: infinite_finite_induct)
haftmann@61929
  1919
    (auto simp add: Gcd_nat_def)
haftmann@62345
  1920
  show "n dvd Lcm N" if "n \<in> N" for N and n ::nat
haftmann@62345
  1921
  using that by (induct N rule: infinite_finite_induct)
haftmann@62345
  1922
    auto
haftmann@62345
  1923
  show "Lcm N dvd n" if "\<And>m. m \<in> N \<Longrightarrow> m dvd n" for N and n ::nat
haftmann@62345
  1924
  using that by (induct N rule: infinite_finite_induct)
haftmann@62345
  1925
    auto
haftmann@62345
  1926
qed simp_all
nipkow@32112
  1927
haftmann@62345
  1928
end
haftmann@61913
  1929
haftmann@62346
  1930
lemma Gcd_nat_eq_one:
haftmann@62346
  1931
  "1 \<in> N \<Longrightarrow> Gcd N = (1::nat)"
haftmann@62346
  1932
  by (rule Gcd_eq_1_I) auto
haftmann@62346
  1933
wenzelm@60758
  1934
text\<open>Alternative characterizations of Gcd:\<close>
nipkow@32112
  1935
nipkow@32112
  1936
lemma Gcd_eq_Max: "finite(M::nat set) \<Longrightarrow> M \<noteq> {} \<Longrightarrow> 0 \<notin> M \<Longrightarrow> Gcd M = Max(\<Inter>m\<in>M. {d. d dvd m})"
nipkow@32112
  1937
apply(rule antisym)
nipkow@32112
  1938
 apply(rule Max_ge)
nipkow@32112
  1939
  apply (metis all_not_in_conv finite_divisors_nat finite_INT)
haftmann@60596
  1940
 apply (simp add: Gcd_dvd)
nipkow@32112
  1941
apply (rule Max_le_iff[THEN iffD2])
nipkow@32112
  1942
  apply (metis all_not_in_conv finite_divisors_nat finite_INT)
nipkow@44890
  1943
 apply fastforce
nipkow@32112
  1944
apply clarsimp
haftmann@60596
  1945
apply (metis Gcd_dvd Max_in dvd_0_left dvd_Gcd dvd_imp_le linorder_antisym_conv3 not_less0)
nipkow@32112
  1946
done
nipkow@32112
  1947
nipkow@32112
  1948
lemma Gcd_remove0_nat: "finite M \<Longrightarrow> Gcd M = Gcd (M - {0::nat})"
nipkow@32112
  1949
apply(induct pred:finite)
nipkow@32112
  1950
 apply simp
nipkow@32112
  1951
apply(case_tac "x=0")
nipkow@32112
  1952
 apply simp
nipkow@32112
  1953
apply(subgoal_tac "insert x F - {0} = insert x (F - {0})")
nipkow@32112
  1954
 apply simp
nipkow@32112
  1955
apply blast
nipkow@32112
  1956
done
nipkow@32112
  1957
nipkow@32112
  1958
lemma Lcm_in_lcm_closed_set_nat:
nipkow@32112
  1959
  "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
  1960
apply(induct rule:finite_linorder_min_induct)
nipkow@32112
  1961
 apply simp
nipkow@32112
  1962
apply simp
nipkow@32112
  1963
apply(subgoal_tac "ALL m n :: nat. m:A \<longrightarrow> n:A \<longrightarrow> lcm m n : A")
nipkow@32112
  1964
 apply simp
nipkow@32112
  1965
 apply(case_tac "A={}")
nipkow@32112
  1966
  apply simp
nipkow@32112
  1967
 apply simp
nipkow@32112
  1968
apply (metis lcm_pos_nat lcm_unique_nat linorder_neq_iff nat_dvd_not_less not_less0)
nipkow@32112
  1969
done
nipkow@32112
  1970
nipkow@32112
  1971
lemma Lcm_eq_Max_nat:
nipkow@32112
  1972
  "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
  1973
apply(rule antisym)
nipkow@32112
  1974
 apply(rule Max_ge, assumption)
nipkow@32112
  1975
 apply(erule (2) Lcm_in_lcm_closed_set_nat)
haftmann@62345
  1976
apply (auto simp add: not_le Lcm_0_iff dvd_imp_le leD le_neq_trans)
nipkow@32112
  1977
done
nipkow@32112
  1978
nipkow@34222
  1979
lemma mult_inj_if_coprime_nat:
nipkow@34222
  1980
  "inj_on f A \<Longrightarrow> inj_on g B \<Longrightarrow> ALL a:A. ALL b:B. coprime (f a) (g b)
nipkow@34222
  1981
   \<Longrightarrow> inj_on (%(a,b). f a * g b::nat) (A \<times> B)"
haftmann@61913
  1982
  by (auto simp add: inj_on_def coprime_crossproduct_nat simp del: One_nat_def)
nipkow@34222
  1983
wenzelm@60758
  1984
text\<open>Nitpick:\<close>
nipkow@34222
  1985
blanchet@41792
  1986
lemma gcd_eq_nitpick_gcd [nitpick_unfold]: "gcd x y = Nitpick.nat_gcd x y"
blanchet@41792
  1987
by (induct x y rule: nat_gcd.induct)
blanchet@41792
  1988
   (simp add: gcd_nat.simps Nitpick.nat_gcd.simps)
blanchet@33197
  1989
blanchet@41792
  1990
lemma lcm_eq_nitpick_lcm [nitpick_unfold]: "lcm x y = Nitpick.nat_lcm x y"
blanchet@33197
  1991
by (simp only: lcm_nat_def Nitpick.nat_lcm_def gcd_eq_nitpick_gcd)
blanchet@33197
  1992
haftmann@54867
  1993
wenzelm@60758
  1994
subsubsection \<open>Setwise gcd and lcm for integers\<close>
huffman@45264
  1995
haftmann@62345
  1996
instantiation int :: semiring_Gcd
huffman@45264
  1997
begin
huffman@45264
  1998
huffman@45264
  1999
definition
haftmann@62346
  2000
  "Lcm M = int (Lcm m\<in>M. (nat \<circ> abs) m)"
huffman@45264
  2001
huffman@45264
  2002
definition
haftmann@62346
  2003
  "Gcd M = int (Gcd m\<in>M. (nat \<circ> abs) m)"
huffman@45264
  2004
haftmann@62345
  2005
instance by standard
haftmann@62345
  2006
  (auto intro!: Gcd_dvd Gcd_greatest simp add: Gcd_int_def
haftmann@62345
  2007
    Lcm_int_def int_dvd_iff dvd_int_iff dvd_int_unfold_dvd_nat [symmetric])
haftmann@62345
  2008
haftmann@62345
  2009
end
haftmann@62345
  2010
haftmann@62346
  2011
lemma abs_Gcd [simp]:
haftmann@62346
  2012
  fixes K :: "int set"
haftmann@62346
  2013
  shows "\<bar>Gcd K\<bar> = Gcd K"
haftmann@62346
  2014
  using normalize_Gcd [of K] by simp
haftmann@62346
  2015
haftmann@62346
  2016
lemma abs_Lcm [simp]:
haftmann@62346
  2017
  fixes K :: "int set"
haftmann@62346
  2018
  shows "\<bar>Lcm K\<bar> = Lcm K"
haftmann@62346
  2019
  using normalize_Lcm [of K] by simp
haftmann@62346
  2020
haftmann@62346
  2021
lemma Gcm_eq_int_iff:
haftmann@62346
  2022
  "Gcd K = int n \<longleftrightarrow> Gcd ((nat \<circ> abs) ` K) = n"
haftmann@62346
  2023
  by (simp add: Gcd_int_def comp_def image_image)
haftmann@62346
  2024
haftmann@62346
  2025
lemma Lcm_eq_int_iff:
haftmann@62346
  2026
  "Lcm K = int n \<longleftrightarrow> Lcm ((nat \<circ> abs) ` K) = n"
haftmann@62346
  2027
  by (simp add: Lcm_int_def comp_def image_image)
haftmann@62346
  2028
haftmann@62345
  2029
haftmann@62345
  2030
subsection \<open>GCD and LCM on @{typ integer}\<close>
haftmann@62345
  2031
haftmann@62345
  2032
instantiation integer :: gcd
haftmann@62345
  2033
begin
haftmann@62345
  2034
haftmann@62345
  2035
context
haftmann@62345
  2036
  includes integer.lifting
haftmann@62345
  2037
begin
haftmann@62345
  2038
haftmann@62345
  2039
lift_definition gcd_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@62345
  2040
  is gcd .
haftmann@62345
  2041
lift_definition lcm_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@62345
  2042
  is lcm .
haftmann@62345
  2043
haftmann@62345
  2044
end
huffman@45264
  2045
instance ..
haftmann@60686
  2046
wenzelm@21256
  2047
end
huffman@45264
  2048
Andreas@61856
  2049
lifting_update integer.lifting
Andreas@61856
  2050
lifting_forget integer.lifting
Andreas@61856
  2051
haftmann@62345
  2052
context
haftmann@62345
  2053
  includes integer.lifting
haftmann@62345
  2054
begin
Andreas@61856
  2055
Andreas@61856
  2056
lemma gcd_code_integer [code]:
Andreas@61856
  2057
  "gcd k l = \<bar>if l = (0::integer) then k else gcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
haftmann@62345
  2058
  by transfer (fact gcd_code_int)
Andreas@61856
  2059
wenzelm@61944
  2060
lemma lcm_code_integer [code]: "lcm (a::integer) b = \<bar>a\<bar> * \<bar>b\<bar> div gcd a b"
haftmann@62345
  2061
  by transfer (fact lcm_altdef_int)
Andreas@61856
  2062
Andreas@61856
  2063
end
Andreas@61856
  2064
Andreas@61856
  2065
code_printing constant "gcd :: integer \<Rightarrow> _"
Andreas@61856
  2066
  \<rightharpoonup> (OCaml) "Big'_int.gcd'_big'_int"
Andreas@61856
  2067
  and (Haskell) "Prelude.gcd"
Andreas@61856
  2068
  and (Scala) "_.gcd'((_)')"
wenzelm@61975
  2069
  \<comment> \<open>There is no gcd operation in the SML standard library, so no code setup for SML\<close>
Andreas@61856
  2070
haftmann@62344
  2071
text \<open>Some code equations\<close>
haftmann@62344
  2072
haftmann@62344
  2073
lemma Lcm_set_nat [code, code_unfold]:
haftmann@62344
  2074
  "Lcm (set ns) = fold lcm ns (1::nat)"
haftmann@62344
  2075
  using Lcm_set [of ns] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2076
haftmann@62344
  2077
lemma Gcd_set_nat [code]:
haftmann@62344
  2078
  "Gcd (set ns) = fold gcd ns (0::nat)"
haftmann@62344
  2079
  using Gcd_set [of ns] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2080
haftmann@62344
  2081
lemma Lcm_set_int [code, code_unfold]:
haftmann@62344
  2082
  "Lcm (set xs) = fold lcm xs (1::int)"
haftmann@62344
  2083
  using Lcm_set [of xs] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2084
haftmann@62344
  2085
lemma Gcd_set_int [code]:
haftmann@62344
  2086
  "Gcd (set xs) = fold gcd xs (0::int)"
haftmann@62344
  2087
  using Gcd_set [of xs] by (simp_all add: fun_eq_iff ac_simps foldr_fold [symmetric])
haftmann@62344
  2088
haftmann@62344
  2089
text \<open>Fact aliasses\<close>
haftmann@62344
  2090
haftmann@62344
  2091
lemma lcm_0_iff_nat [simp]: "lcm (m::nat) n = 0 \<longleftrightarrow> m = 0 \<or> n= 0"
haftmann@62344
  2092
  by (fact lcm_eq_0_iff)
haftmann@62344
  2093
haftmann@62344
  2094
lemma lcm_0_iff_int [simp]: "lcm (m::int) n = 0 \<longleftrightarrow> m = 0 \<or> n = 0"
haftmann@62344
  2095
  by (fact lcm_eq_0_iff)
haftmann@62344
  2096
haftmann@62344
  2097
lemma dvd_lcm_I1_nat [simp]: "(k::nat) dvd m \<Longrightarrow> k dvd lcm m n"
haftmann@62344
  2098
  by (fact dvd_lcmI1)
haftmann@62344
  2099
haftmann@62344
  2100
lemma dvd_lcm_I2_nat [simp]: "(k::nat) dvd n \<Longrightarrow> k dvd lcm m n"
haftmann@62344
  2101
  by (fact dvd_lcmI2)
haftmann@62344
  2102
haftmann@62344
  2103
lemma dvd_lcm_I1_int [simp]: "(i::int) dvd m \<Longrightarrow> i dvd lcm m n"
haftmann@62344
  2104
  by (fact dvd_lcmI1)
haftmann@62344
  2105
haftmann@62344
  2106
lemma dvd_lcm_I2_int[simp]: "(i::int) dvd n \<Longrightarrow> i dvd lcm m n"
haftmann@62344
  2107
  by (fact dvd_lcmI2)
haftmann@62344
  2108
haftmann@62344
  2109
lemma coprime_dvd_mult_iff_nat: "coprime (k::nat) n \<Longrightarrow>
haftmann@62344
  2110
    (k dvd m * n) = (k dvd m)"
haftmann@62344
  2111
  by (fact coprime_dvd_mult_iff)
haftmann@62344
  2112
haftmann@62344
  2113
lemma coprime_dvd_mult_iff_int: "coprime (k::int) n \<Longrightarrow>
haftmann@62344
  2114
    (k dvd m * n) = (k dvd m)"
haftmann@62344
  2115
  by (fact coprime_dvd_mult_iff)
haftmann@62344
  2116
haftmann@62344
  2117
lemma coprime_exp2_nat [intro]: "coprime (a::nat) b \<Longrightarrow> coprime (a^n) (b^m)"
haftmann@62344
  2118
  by (fact coprime_exp2)
haftmann@62344
  2119
haftmann@62344
  2120
lemma coprime_exp2_int [intro]: "coprime (a::int) b \<Longrightarrow> coprime (a^n) (b^m)"
haftmann@62344
  2121
  by (fact coprime_exp2)
haftmann@62344
  2122
haftmann@62344
  2123
lemmas Gcd_dvd_nat [simp] = Gcd_dvd [where ?'a = nat]
haftmann@62344
  2124
lemmas Gcd_dvd_int [simp] = Gcd_dvd [where ?'a = int]
haftmann@62344
  2125
lemmas dvd_Gcd_nat [simp] = dvd_Gcd [where ?'a = nat]
haftmann@62344
  2126
lemmas dvd_Gcd_int [simp] = dvd_Gcd [where ?'a = int]
haftmann@62344
  2127
haftmann@62344
  2128
lemma dvd_Lcm_int [simp]:
haftmann@62344
  2129
  fixes M :: "int set" assumes "m \<in> M" shows "m dvd Lcm M"
haftmann@62344
  2130
  using assms by (fact dvd_Lcm)
haftmann@62344
  2131
haftmann@62344
  2132
lemma Lcm_empty_nat:
haftmann@62344
  2133
  "Lcm {} = (1::nat)"
haftmann@62344
  2134
  by (fact Lcm_empty)
haftmann@62344
  2135
haftmann@62344
  2136
lemma Lcm_empty_int:
haftmann@62344
  2137
  "Lcm {} = (1::int)"
haftmann@62344
  2138
  by (fact Lcm_empty)
haftmann@62344
  2139
haftmann@62344
  2140
lemma Lcm_insert_nat:
haftmann@62344
  2141
  "Lcm (insert (n::nat) N) = lcm n (Lcm N)"
haftmann@62344
  2142
  by (fact Lcm_insert)
haftmann@62344
  2143
haftmann@62344
  2144
lemma Lcm_insert_int:
haftmann@62344
  2145
  "Lcm (insert (n::int) N) = lcm n (Lcm N)"
haftmann@62344
  2146
  by (fact Lcm_insert)
haftmann@62344
  2147
haftmann@62344
  2148
lemma gcd_neg_numeral_1_int [simp]:
haftmann@62344
  2149
  "gcd (- numeral n :: int) x = gcd (numeral n) x"
haftmann@62344
  2150
  by (fact gcd_neg1_int)
haftmann@62344
  2151
haftmann@62344
  2152
lemma gcd_neg_numeral_2_int [simp]:
haftmann@62344
  2153
  "gcd x (- numeral n :: int) = gcd x (numeral n)"
haftmann@62344
  2154
  by (fact gcd_neg2_int)
haftmann@62344
  2155
haftmann@62344
  2156
lemma gcd_proj1_if_dvd_nat [simp]: "(x::nat) dvd y \<Longrightarrow> gcd x y = x"
haftmann@62344
  2157
  by (fact gcd_nat.absorb1)
haftmann@62344
  2158
haftmann@62344
  2159
lemma gcd_proj2_if_dvd_nat [simp]: "(y::nat) dvd x \<Longrightarrow> gcd x y = y"
haftmann@62344
  2160
  by (fact gcd_nat.absorb2)
haftmann@62344
  2161
haftmann@62344
  2162
lemma comp_fun_idem_gcd_nat: "comp_fun_idem (gcd :: nat\<Rightarrow>nat\<Rightarrow>nat)"
haftmann@62344
  2163
  by (fact comp_fun_idem_gcd)
haftmann@62344
  2164
haftmann@62344
  2165
lemma comp_fun_idem_gcd_int: "comp_fun_idem (gcd :: int\<Rightarrow>int\<Rightarrow>int)"
haftmann@62344
  2166
  by (fact comp_fun_idem_gcd)
haftmann@62344
  2167
haftmann@62344
  2168
lemma comp_fun_idem_lcm_nat: "comp_fun_idem (lcm :: nat\<Rightarrow>nat\<Rightarrow>nat)"
haftmann@62344
  2169
  by (fact comp_fun_idem_lcm)
haftmann@62344
  2170
haftmann@62344
  2171
lemma comp_fun_idem_lcm_int: "comp_fun_idem (lcm :: int\<Rightarrow>int\<Rightarrow>int)"
haftmann@62344
  2172
  by (fact comp_fun_idem_lcm)
haftmann@62344
  2173
haftmann@62345
  2174
lemma Lcm_eq_0 [simp]:
haftmann@62345
  2175
  "finite (M::nat set) \<Longrightarrow> 0 \<in> M \<Longrightarrow> Lcm M = 0"
haftmann@62345
  2176
  by (rule Lcm_eq_0_I)
haftmann@62345
  2177
haftmann@62345
  2178
lemma Lcm0_iff [simp]:
haftmann@62345
  2179
  fixes M :: "nat set"
haftmann@62345
  2180
  assumes "finite M" and "M \<noteq> {}"
haftmann@62345
  2181
  shows "Lcm M = 0 \<longleftrightarrow> 0 \<in> M"
haftmann@62345
  2182
  using assms by (simp add: Lcm_0_iff)
haftmann@62345
  2183
haftmann@62345
  2184
lemma Lcm_dvd_int [simp]:
haftmann@62345
  2185
  fixes M :: "int set"
haftmann@62345
  2186
  assumes "\<forall>m\<in>M. m dvd n" shows "Lcm M dvd n"
haftmann@62345
  2187
  using assms by (auto intro: Lcm_least)
haftmann@62345
  2188
haftmann@62345
  2189
lemma dvd_gcd_D1_nat: "k dvd gcd m n \<Longrightarrow> (k::nat) dvd m"
haftmann@62345
  2190
  by (fact dvd_gcdD1)
haftmann@62345
  2191
haftmann@62345
  2192
lemma dvd_gcd_D2_nat: "k dvd gcd m n \<Longrightarrow> (k::nat) dvd n"
haftmann@62345
  2193
  by (fact dvd_gcdD2)
haftmann@62345
  2194
haftmann@62345
  2195
lemma dvd_gcd_D1_int: "i dvd gcd m n \<Longrightarrow> (i::int) dvd m"
haftmann@62345
  2196
  by (fact dvd_gcdD1)
haftmann@62345
  2197
haftmann@62345
  2198
lemma dvd_gcd_D2_int: "i dvd gcd m n \<Longrightarrow> (i::int) dvd n"
haftmann@62345
  2199
  by (fact dvd_gcdD2)
haftmann@62345
  2200
Andreas@61856
  2201
end