src/HOL/Library/GCD.thy
author chaieb
Mon Jul 14 16:13:51 2008 +0200 (2008-07-14)
changeset 27568 9949dc7a24de
parent 27556 292098f2efdf
child 27651 16a26996c30e
permissions -rw-r--r--
Theorem names as in IntPrimes.thy, also several theorems moved from there
wenzelm@21256
     1
(*  Title:      HOL/GCD.thy
wenzelm@21256
     2
    ID:         $Id$
wenzelm@21256
     3
    Author:     Christophe Tabacznyj and Lawrence C Paulson
wenzelm@21256
     4
    Copyright   1996  University of Cambridge
wenzelm@21256
     5
*)
wenzelm@21256
     6
wenzelm@21256
     7
header {* The Greatest Common Divisor *}
wenzelm@21256
     8
wenzelm@21256
     9
theory GCD
haftmann@27487
    10
imports Plain "~~/src/HOL/Presburger"
wenzelm@21256
    11
begin
wenzelm@21256
    12
wenzelm@21256
    13
text {*
haftmann@23687
    14
  See \cite{davenport92}. \bigskip
wenzelm@21256
    15
*}
wenzelm@21256
    16
haftmann@23687
    17
subsection {* Specification of GCD on nats *}
wenzelm@21256
    18
wenzelm@21263
    19
definition
haftmann@23687
    20
  is_gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where -- {* @{term gcd} as a relation *}
haftmann@27556
    21
  [code func del]: "is_gcd m n p \<longleftrightarrow> p dvd m \<and> p dvd n \<and>
haftmann@23687
    22
    (\<forall>d. d dvd m \<longrightarrow> d dvd n \<longrightarrow> d dvd p)"
haftmann@23687
    23
haftmann@23687
    24
text {* Uniqueness *}
haftmann@23687
    25
haftmann@27556
    26
lemma is_gcd_unique: "is_gcd a b m \<Longrightarrow> is_gcd a b n \<Longrightarrow> m = n"
haftmann@23687
    27
  by (simp add: is_gcd_def) (blast intro: dvd_anti_sym)
haftmann@23687
    28
haftmann@23687
    29
text {* Connection to divides relation *}
wenzelm@21256
    30
haftmann@27556
    31
lemma is_gcd_dvd: "is_gcd a b m \<Longrightarrow> k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd m"
haftmann@23687
    32
  by (auto simp add: is_gcd_def)
haftmann@23687
    33
haftmann@23687
    34
text {* Commutativity *}
haftmann@23687
    35
haftmann@27556
    36
lemma is_gcd_commute: "is_gcd m n k = is_gcd n m k"
haftmann@23687
    37
  by (auto simp add: is_gcd_def)
haftmann@23687
    38
haftmann@23687
    39
haftmann@23687
    40
subsection {* GCD on nat by Euclid's algorithm *}
haftmann@23687
    41
chaieb@27568
    42
fun
chaieb@27568
    43
  gcd  :: "nat => nat => nat"
chaieb@27568
    44
where
haftmann@27556
    45
  "gcd m n = (if n = 0 then m else gcd n (m mod n))"
haftmann@27556
    46
lemma gcd_induct [case_names "0" rec]:
haftmann@23687
    47
  fixes m n :: nat
haftmann@23687
    48
  assumes "\<And>m. P m 0"
haftmann@23687
    49
    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
haftmann@23687
    50
  shows "P m n"
haftmann@27556
    51
proof (induct m n rule: gcd.induct)
haftmann@27556
    52
  case (1 m n) with assms show ?case by (cases "n = 0") simp_all
haftmann@27556
    53
qed
wenzelm@21256
    54
haftmann@27556
    55
lemma gcd_0 [simp]: "gcd m 0 = m"
wenzelm@21263
    56
  by simp
wenzelm@21256
    57
haftmann@27556
    58
lemma gcd_0_left [simp]: "gcd 0 m = m"
haftmann@23687
    59
  by simp
haftmann@23687
    60
haftmann@27556
    61
lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
haftmann@23687
    62
  by simp
haftmann@23687
    63
haftmann@27556
    64
lemma gcd_1 [simp]: "gcd m (Suc 0) = 1"
wenzelm@21263
    65
  by simp
wenzelm@21256
    66
wenzelm@21256
    67
declare gcd.simps [simp del]
wenzelm@21256
    68
wenzelm@21256
    69
text {*
haftmann@27556
    70
  \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
wenzelm@21256
    71
  conjunctions don't seem provable separately.
wenzelm@21256
    72
*}
wenzelm@21256
    73
haftmann@27556
    74
lemma gcd_dvd1 [iff]: "gcd m n dvd m"
haftmann@27556
    75
  and gcd_dvd2 [iff]: "gcd m n dvd n"
wenzelm@21256
    76
  apply (induct m n rule: gcd_induct)
wenzelm@21263
    77
     apply (simp_all add: gcd_non_0)
wenzelm@21256
    78
  apply (blast dest: dvd_mod_imp_dvd)
wenzelm@21256
    79
  done
wenzelm@21256
    80
wenzelm@21256
    81
text {*
wenzelm@21256
    82
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
wenzelm@21256
    83
  naturals, if @{term k} divides @{term m} and @{term k} divides
haftmann@27556
    84
  @{term n} then @{term k} divides @{term "gcd m n"}.
wenzelm@21256
    85
*}
wenzelm@21256
    86
haftmann@27556
    87
lemma gcd_greatest: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd gcd m n"
wenzelm@21263
    88
  by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
wenzelm@21256
    89
wenzelm@21256
    90
text {*
wenzelm@21256
    91
  \medskip Function gcd yields the Greatest Common Divisor.
wenzelm@21256
    92
*}
wenzelm@21256
    93
haftmann@27556
    94
lemma is_gcd: "is_gcd m n (gcd m n) "
haftmann@23687
    95
  by (simp add: is_gcd_def gcd_greatest)
wenzelm@21256
    96
wenzelm@21256
    97
haftmann@23687
    98
subsection {* Derived laws for GCD *}
wenzelm@21256
    99
haftmann@27556
   100
lemma gcd_greatest_iff [iff]: "k dvd gcd m n \<longleftrightarrow> k dvd m \<and> k dvd n"
haftmann@23687
   101
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@23687
   102
haftmann@27556
   103
lemma gcd_zero: "gcd m n = 0 \<longleftrightarrow> m = 0 \<and> n = 0"
haftmann@23687
   104
  by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
wenzelm@21256
   105
haftmann@27556
   106
lemma gcd_commute: "gcd m n = gcd n m"
wenzelm@21256
   107
  apply (rule is_gcd_unique)
wenzelm@21256
   108
   apply (rule is_gcd)
wenzelm@21256
   109
  apply (subst is_gcd_commute)
wenzelm@21256
   110
  apply (simp add: is_gcd)
wenzelm@21256
   111
  done
wenzelm@21256
   112
haftmann@27556
   113
lemma gcd_assoc: "gcd (gcd k m) n = gcd k (gcd m n)"
wenzelm@21256
   114
  apply (rule is_gcd_unique)
wenzelm@21256
   115
   apply (rule is_gcd)
wenzelm@21256
   116
  apply (simp add: is_gcd_def)
wenzelm@21256
   117
  apply (blast intro: dvd_trans)
wenzelm@21256
   118
  done
wenzelm@21256
   119
haftmann@27556
   120
lemma gcd_1_left [simp]: "gcd (Suc 0) m = 1"
haftmann@23687
   121
  by (simp add: gcd_commute)
wenzelm@21256
   122
wenzelm@21256
   123
text {*
wenzelm@21256
   124
  \medskip Multiplication laws
wenzelm@21256
   125
*}
wenzelm@21256
   126
haftmann@27556
   127
lemma gcd_mult_distrib2: "k * gcd m n = gcd (k * m) (k * n)"
wenzelm@21256
   128
    -- {* \cite[page 27]{davenport92} *}
wenzelm@21256
   129
  apply (induct m n rule: gcd_induct)
wenzelm@21256
   130
   apply simp
wenzelm@21256
   131
  apply (case_tac "k = 0")
wenzelm@21256
   132
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
wenzelm@21256
   133
  done
wenzelm@21256
   134
haftmann@27556
   135
lemma gcd_mult [simp]: "gcd k (k * n) = k"
wenzelm@21256
   136
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
wenzelm@21256
   137
  done
wenzelm@21256
   138
haftmann@27556
   139
lemma gcd_self [simp]: "gcd k k = k"
wenzelm@21256
   140
  apply (rule gcd_mult [of k 1, simplified])
wenzelm@21256
   141
  done
wenzelm@21256
   142
haftmann@27556
   143
lemma relprime_dvd_mult: "gcd k n = 1 ==> k dvd m * n ==> k dvd m"
wenzelm@21256
   144
  apply (insert gcd_mult_distrib2 [of m k n])
wenzelm@21256
   145
  apply simp
wenzelm@21256
   146
  apply (erule_tac t = m in ssubst)
wenzelm@21256
   147
  apply simp
wenzelm@21256
   148
  done
wenzelm@21256
   149
haftmann@27556
   150
lemma relprime_dvd_mult_iff: "gcd k n = 1 ==> (k dvd m * n) = (k dvd m)"
wenzelm@21256
   151
  apply (blast intro: relprime_dvd_mult dvd_trans)
wenzelm@21256
   152
  done
wenzelm@21256
   153
haftmann@27556
   154
lemma gcd_mult_cancel: "gcd k n = 1 ==> gcd (k * m) n = gcd m n"
wenzelm@21256
   155
  apply (rule dvd_anti_sym)
wenzelm@21256
   156
   apply (rule gcd_greatest)
wenzelm@21256
   157
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   158
     apply (simp add: gcd_assoc)
wenzelm@21256
   159
     apply (simp add: gcd_commute)
wenzelm@21256
   160
    apply (simp_all add: mult_commute)
wenzelm@21256
   161
  apply (blast intro: dvd_trans)
wenzelm@21256
   162
  done
wenzelm@21256
   163
wenzelm@21256
   164
wenzelm@21256
   165
text {* \medskip Addition laws *}
wenzelm@21256
   166
haftmann@27556
   167
lemma gcd_add1 [simp]: "gcd (m + n) n = gcd m n"
wenzelm@21256
   168
  apply (case_tac "n = 0")
wenzelm@21256
   169
   apply (simp_all add: gcd_non_0)
wenzelm@21256
   170
  done
wenzelm@21256
   171
haftmann@27556
   172
lemma gcd_add2 [simp]: "gcd m (m + n) = gcd m n"
wenzelm@21256
   173
proof -
haftmann@27556
   174
  have "gcd m (m + n) = gcd (m + n) m" by (rule gcd_commute)
haftmann@27556
   175
  also have "... = gcd (n + m) m" by (simp add: add_commute)
haftmann@27556
   176
  also have "... = gcd n m" by simp
haftmann@27556
   177
  also have  "... = gcd m n" by (rule gcd_commute)
wenzelm@21256
   178
  finally show ?thesis .
wenzelm@21256
   179
qed
wenzelm@21256
   180
haftmann@27556
   181
lemma gcd_add2' [simp]: "gcd m (n + m) = gcd m n"
wenzelm@21256
   182
  apply (subst add_commute)
wenzelm@21256
   183
  apply (rule gcd_add2)
wenzelm@21256
   184
  done
wenzelm@21256
   185
haftmann@27556
   186
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
wenzelm@21263
   187
  by (induct k) (simp_all add: add_assoc)
wenzelm@21256
   188
haftmann@27556
   189
lemma gcd_dvd_prod: "gcd m n dvd m * n"
haftmann@23687
   190
  using mult_dvd_mono [of 1] by auto
chaieb@22027
   191
wenzelm@22367
   192
text {*
wenzelm@22367
   193
  \medskip Division by gcd yields rrelatively primes.
wenzelm@22367
   194
*}
chaieb@22027
   195
chaieb@22027
   196
lemma div_gcd_relprime:
wenzelm@22367
   197
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@27556
   198
  shows "gcd (a div gcd a b) (b div gcd a b) = 1"
wenzelm@22367
   199
proof -
haftmann@27556
   200
  let ?g = "gcd a b"
chaieb@22027
   201
  let ?a' = "a div ?g"
chaieb@22027
   202
  let ?b' = "b div ?g"
haftmann@27556
   203
  let ?g' = "gcd ?a' ?b'"
chaieb@22027
   204
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
   205
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
wenzelm@22367
   206
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   207
      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
chaieb@22027
   208
    unfolding dvd_def by blast
wenzelm@22367
   209
  then have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   210
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   211
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   212
      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   213
  have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
wenzelm@22367
   214
  then have gp: "?g > 0" by simp
wenzelm@22367
   215
  from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   216
  with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
chaieb@22027
   217
qed
chaieb@22027
   218
haftmann@23687
   219
subsection {* LCM defined by GCD *}
wenzelm@22367
   220
haftmann@23687
   221
definition
haftmann@27556
   222
  lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@23687
   223
where
chaieb@27568
   224
  lcm_def: "lcm m n = m * n div gcd m n"
haftmann@23687
   225
haftmann@23687
   226
lemma prod_gcd_lcm:
haftmann@27556
   227
  "m * n = gcd m n * lcm m n"
haftmann@23687
   228
  unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
haftmann@23687
   229
haftmann@27556
   230
lemma lcm_0 [simp]: "lcm m 0 = 0"
haftmann@23687
   231
  unfolding lcm_def by simp
haftmann@23687
   232
haftmann@27556
   233
lemma lcm_1 [simp]: "lcm m 1 = m"
haftmann@23687
   234
  unfolding lcm_def by simp
haftmann@23687
   235
haftmann@27556
   236
lemma lcm_0_left [simp]: "lcm 0 n = 0"
haftmann@23687
   237
  unfolding lcm_def by simp
haftmann@23687
   238
haftmann@27556
   239
lemma lcm_1_left [simp]: "lcm 1 m = m"
haftmann@23687
   240
  unfolding lcm_def by simp
haftmann@23687
   241
haftmann@23687
   242
lemma dvd_pos:
haftmann@23687
   243
  fixes n m :: nat
haftmann@23687
   244
  assumes "n > 0" and "m dvd n"
haftmann@23687
   245
  shows "m > 0"
haftmann@23687
   246
using assms by (cases m) auto
haftmann@23687
   247
haftmann@23951
   248
lemma lcm_least:
haftmann@23687
   249
  assumes "m dvd k" and "n dvd k"
haftmann@27556
   250
  shows "lcm m n dvd k"
haftmann@23687
   251
proof (cases k)
haftmann@23687
   252
  case 0 then show ?thesis by auto
haftmann@23687
   253
next
haftmann@23687
   254
  case (Suc _) then have pos_k: "k > 0" by auto
haftmann@23687
   255
  from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
haftmann@27556
   256
  with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
haftmann@23687
   257
  from assms obtain p where k_m: "k = m * p" using dvd_def by blast
haftmann@23687
   258
  from assms obtain q where k_n: "k = n * q" using dvd_def by blast
haftmann@23687
   259
  from pos_k k_m have pos_p: "p > 0" by auto
haftmann@23687
   260
  from pos_k k_n have pos_q: "q > 0" by auto
haftmann@27556
   261
  have "k * k * gcd q p = k * gcd (k * q) (k * p)"
haftmann@23687
   262
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   263
  also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
haftmann@23687
   264
    by (simp add: k_m [symmetric] k_n [symmetric])
haftmann@27556
   265
  also have "\<dots> = k * p * q * gcd m n"
haftmann@23687
   266
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   267
  finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
haftmann@23687
   268
    by (simp only: k_m [symmetric] k_n [symmetric])
haftmann@27556
   269
  then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
haftmann@23687
   270
    by (simp add: mult_ac)
haftmann@27556
   271
  with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
haftmann@23687
   272
    by simp
haftmann@23687
   273
  with prod_gcd_lcm [of m n]
haftmann@27556
   274
  have "lcm m n * gcd q p * gcd m n = k * gcd m n"
haftmann@23687
   275
    by (simp add: mult_ac)
haftmann@27556
   276
  with pos_gcd have "lcm m n * gcd q p = k" by simp
haftmann@23687
   277
  then show ?thesis using dvd_def by auto
haftmann@23687
   278
qed
haftmann@23687
   279
haftmann@23687
   280
lemma lcm_dvd1 [iff]:
haftmann@27556
   281
  "m dvd lcm m n"
haftmann@23687
   282
proof (cases m)
haftmann@23687
   283
  case 0 then show ?thesis by simp
haftmann@23687
   284
next
haftmann@23687
   285
  case (Suc _)
haftmann@23687
   286
  then have mpos: "m > 0" by simp
haftmann@23687
   287
  show ?thesis
haftmann@23687
   288
  proof (cases n)
haftmann@23687
   289
    case 0 then show ?thesis by simp
haftmann@23687
   290
  next
haftmann@23687
   291
    case (Suc _)
haftmann@23687
   292
    then have npos: "n > 0" by simp
haftmann@27556
   293
    have "gcd m n dvd n" by simp
haftmann@27556
   294
    then obtain k where "n = gcd m n * k" using dvd_def by auto
haftmann@27556
   295
    then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: mult_ac)
haftmann@23687
   296
    also have "\<dots> = m * k" using mpos npos gcd_zero by simp
haftmann@23687
   297
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   298
  qed
haftmann@23687
   299
qed
haftmann@23687
   300
haftmann@23687
   301
lemma lcm_dvd2 [iff]: 
haftmann@27556
   302
  "n dvd lcm m n"
haftmann@23687
   303
proof (cases n)
haftmann@23687
   304
  case 0 then show ?thesis by simp
haftmann@23687
   305
next
haftmann@23687
   306
  case (Suc _)
haftmann@23687
   307
  then have npos: "n > 0" by simp
haftmann@23687
   308
  show ?thesis
haftmann@23687
   309
  proof (cases m)
haftmann@23687
   310
    case 0 then show ?thesis by simp
haftmann@23687
   311
  next
haftmann@23687
   312
    case (Suc _)
haftmann@23687
   313
    then have mpos: "m > 0" by simp
haftmann@27556
   314
    have "gcd m n dvd m" by simp
haftmann@27556
   315
    then obtain k where "m = gcd m n * k" using dvd_def by auto
haftmann@27556
   316
    then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: mult_ac)
haftmann@23687
   317
    also have "\<dots> = n * k" using mpos npos gcd_zero by simp
haftmann@23687
   318
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   319
  qed
haftmann@23687
   320
qed
haftmann@23687
   321
chaieb@27568
   322
lemma gcd_add1_eq: "gcd (m + k) k = gcd (m + k) m"
chaieb@27568
   323
  by (simp add: gcd_commute)
chaieb@27568
   324
chaieb@27568
   325
lemma gcd_diff2: "m \<le> n ==> gcd n (n - m) = gcd n m"
chaieb@27568
   326
  apply (subgoal_tac "n = m + (n - m)")
chaieb@27568
   327
   apply (erule ssubst, rule gcd_add1_eq, simp)
chaieb@27568
   328
  done
chaieb@27568
   329
haftmann@23687
   330
haftmann@23687
   331
subsection {* GCD and LCM on integers *}
wenzelm@22367
   332
wenzelm@22367
   333
definition
haftmann@27556
   334
  zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
haftmann@27556
   335
  "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
wenzelm@22367
   336
chaieb@27568
   337
lemma zgcd_zdvd1 [iff,simp]: "zgcd i j dvd i"
haftmann@27556
   338
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   339
chaieb@27568
   340
lemma zgcd_zdvd2 [iff,simp]: "zgcd i j dvd j"
haftmann@27556
   341
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   342
haftmann@27556
   343
lemma zgcd_pos: "zgcd i j \<ge> 0"
haftmann@27556
   344
  by (simp add: zgcd_def)
wenzelm@22367
   345
haftmann@27556
   346
lemma zgcd0 [simp]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
haftmann@27556
   347
  by (simp add: zgcd_def gcd_zero) arith
chaieb@22027
   348
haftmann@27556
   349
lemma zgcd_commute: "zgcd i j = zgcd j i"
haftmann@27556
   350
  unfolding zgcd_def by (simp add: gcd_commute)
wenzelm@22367
   351
chaieb@27568
   352
lemma zgcd_zminus [simp]: "zgcd (- i) j = zgcd i j"
haftmann@27556
   353
  unfolding zgcd_def by simp
wenzelm@22367
   354
chaieb@27568
   355
lemma zgcd_zminus2 [simp]: "zgcd i (- j) = zgcd i j"
haftmann@27556
   356
  unfolding zgcd_def by simp
wenzelm@22367
   357
haftmann@27556
   358
lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
haftmann@27556
   359
  unfolding zgcd_def
wenzelm@22367
   360
proof -
haftmann@27556
   361
  assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
haftmann@27556
   362
  then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
wenzelm@22367
   363
  from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
chaieb@22027
   364
  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
wenzelm@22367
   365
    unfolding dvd_def
wenzelm@22367
   366
    by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
wenzelm@22367
   367
  from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
chaieb@22027
   368
    unfolding dvd_def by blast
chaieb@22027
   369
  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
huffman@23431
   370
  then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
chaieb@22027
   371
  then show ?thesis
wenzelm@22367
   372
    apply (subst zdvd_abs1 [symmetric])
wenzelm@22367
   373
    apply (subst zdvd_abs2 [symmetric])
chaieb@22027
   374
    apply (unfold dvd_def)
wenzelm@22367
   375
    apply (rule_tac x = "int h'" in exI, simp)
chaieb@22027
   376
    done
chaieb@22027
   377
qed
chaieb@22027
   378
haftmann@27556
   379
lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
wenzelm@22367
   380
haftmann@27556
   381
lemma zgcd_greatest:
wenzelm@22367
   382
  assumes "k dvd m" and "k dvd n"
haftmann@27556
   383
  shows "k dvd zgcd m n"
wenzelm@22367
   384
proof -
chaieb@22027
   385
  let ?k' = "nat \<bar>k\<bar>"
chaieb@22027
   386
  let ?m' = "nat \<bar>m\<bar>"
chaieb@22027
   387
  let ?n' = "nat \<bar>n\<bar>"
wenzelm@22367
   388
  from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
chaieb@22027
   389
    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
haftmann@27556
   390
  from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
haftmann@27556
   391
    unfolding zgcd_def by (simp only: zdvd_int)
haftmann@27556
   392
  then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
haftmann@27556
   393
  then show "k dvd zgcd m n" by (simp add: zdvd_abs1)
chaieb@22027
   394
qed
chaieb@22027
   395
haftmann@27556
   396
lemma div_zgcd_relprime:
wenzelm@22367
   397
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@27556
   398
  shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
wenzelm@22367
   399
proof -
chaieb@25112
   400
  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
haftmann@27556
   401
  let ?g = "zgcd a b"
chaieb@22027
   402
  let ?a' = "a div ?g"
chaieb@22027
   403
  let ?b' = "b div ?g"
haftmann@27556
   404
  let ?g' = "zgcd ?a' ?b'"
chaieb@27568
   405
  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
chaieb@27568
   406
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
wenzelm@22367
   407
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   408
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
chaieb@22027
   409
    unfolding dvd_def by blast
wenzelm@22367
   410
  then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   411
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   412
    by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   413
      zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   414
  have "?g \<noteq> 0" using nz by simp
haftmann@27556
   415
  then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
haftmann@27556
   416
  from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   417
  with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
haftmann@27556
   418
  with zgcd_pos show "?g' = 1" by simp
chaieb@22027
   419
qed
chaieb@22027
   420
chaieb@27568
   421
    (* IntPrimes stuff *)
chaieb@27568
   422
chaieb@27568
   423
lemma zgcd_0 [simp]: "zgcd m 0 = abs m"
chaieb@27568
   424
  by (simp add: zgcd_def abs_if)
chaieb@27568
   425
chaieb@27568
   426
lemma zgcd_0_left [simp]: "zgcd 0 m = abs m"
chaieb@27568
   427
  by (simp add: zgcd_def abs_if)
chaieb@27568
   428
chaieb@27568
   429
lemma zgcd_non_0: "0 < n ==> zgcd m n = zgcd n (m mod n)"
chaieb@27568
   430
  apply (frule_tac b = n and a = m in pos_mod_sign)
chaieb@27568
   431
  apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
chaieb@27568
   432
  apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
chaieb@27568
   433
  apply (frule_tac a = m in pos_mod_bound)
chaieb@27568
   434
  apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
chaieb@27568
   435
  done
chaieb@27568
   436
chaieb@27568
   437
lemma zgcd_eq: "zgcd m n = zgcd n (m mod n)"
chaieb@27568
   438
  apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
chaieb@27568
   439
  apply (auto simp add: linorder_neq_iff zgcd_non_0)
chaieb@27568
   440
  apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
chaieb@27568
   441
  done
chaieb@27568
   442
chaieb@27568
   443
lemma zgcd_1 [simp]: "zgcd m 1 = 1"
chaieb@27568
   444
  by (simp add: zgcd_def abs_if)
chaieb@27568
   445
chaieb@27568
   446
lemma zgcd_0_1_iff [simp]: "zgcd 0 m = 1 \<longleftrightarrow> \<bar>m\<bar> = 1"
chaieb@27568
   447
  by (simp add: zgcd_def abs_if)
chaieb@27568
   448
chaieb@27568
   449
lemma zgcd_greatest_iff: "k dvd zgcd m n = (k dvd m \<and> k dvd n)"
chaieb@27568
   450
  by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
chaieb@27568
   451
chaieb@27568
   452
lemma zgcd_1_left [simp]: "zgcd 1 m = 1"
chaieb@27568
   453
  by (simp add: zgcd_def gcd_1_left)
chaieb@27568
   454
chaieb@27568
   455
lemma zgcd_assoc: "zgcd (zgcd k m) n = zgcd k (zgcd m n)"
chaieb@27568
   456
  by (simp add: zgcd_def gcd_assoc)
chaieb@27568
   457
chaieb@27568
   458
lemma zgcd_left_commute: "zgcd k (zgcd m n) = zgcd m (zgcd k n)"
chaieb@27568
   459
  apply (rule zgcd_commute [THEN trans])
chaieb@27568
   460
  apply (rule zgcd_assoc [THEN trans])
chaieb@27568
   461
  apply (rule zgcd_commute [THEN arg_cong])
chaieb@27568
   462
  done
chaieb@27568
   463
chaieb@27568
   464
lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
chaieb@27568
   465
  -- {* addition is an AC-operator *}
chaieb@27568
   466
chaieb@27568
   467
lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd m n = zgcd (k * m) (k * n)"
chaieb@27568
   468
  by (simp del: minus_mult_right [symmetric]
chaieb@27568
   469
      add: minus_mult_right nat_mult_distrib zgcd_def abs_if
chaieb@27568
   470
          mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
chaieb@27568
   471
chaieb@27568
   472
lemma zgcd_zmult_distrib2_abs: "zgcd (k * m) (k * n) = abs k * zgcd m n"
chaieb@27568
   473
  by (simp add: abs_if zgcd_zmult_distrib2)
chaieb@27568
   474
chaieb@27568
   475
lemma zgcd_self [simp]: "0 \<le> m ==> zgcd m m = m"
chaieb@27568
   476
  by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   477
chaieb@27568
   478
lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd k (k * n) = k"
chaieb@27568
   479
  by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   480
chaieb@27568
   481
lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n) k = k"
chaieb@27568
   482
  by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   483
chaieb@27568
   484
chaieb@27568
   485
definition "zlcm i j = int (lcm(nat(abs i)) (nat(abs j)))"
chaieb@23244
   486
haftmann@27556
   487
lemma dvd_zlcm_self1[simp]: "i dvd zlcm i j"
haftmann@27556
   488
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   489
haftmann@27556
   490
lemma dvd_zlcm_self2[simp]: "j dvd zlcm i j"
haftmann@27556
   491
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   492
chaieb@23244
   493
haftmann@27556
   494
lemma dvd_imp_dvd_zlcm1:
haftmann@27556
   495
  assumes "k dvd i" shows "k dvd (zlcm i j)"
nipkow@23983
   496
proof -
nipkow@23983
   497
  have "nat(abs k) dvd nat(abs i)" using `k dvd i`
chaieb@23994
   498
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   499
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   500
qed
nipkow@23983
   501
haftmann@27556
   502
lemma dvd_imp_dvd_zlcm2:
haftmann@27556
   503
  assumes "k dvd j" shows "k dvd (zlcm i j)"
nipkow@23983
   504
proof -
nipkow@23983
   505
  have "nat(abs k) dvd nat(abs j)" using `k dvd j`
chaieb@23994
   506
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   507
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   508
qed
nipkow@23983
   509
chaieb@23994
   510
chaieb@23244
   511
lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
chaieb@23244
   512
by (case_tac "d <0", simp_all)
chaieb@23244
   513
chaieb@23244
   514
lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
chaieb@23244
   515
by (case_tac "d<0", simp_all)
chaieb@23244
   516
chaieb@23244
   517
(* lcm a b is positive for positive a and b *)
chaieb@23244
   518
chaieb@23244
   519
lemma lcm_pos: 
chaieb@23244
   520
  assumes mpos: "m > 0"
chaieb@27568
   521
  and npos: "n>0"
haftmann@27556
   522
  shows "lcm m n > 0"
chaieb@23244
   523
proof(rule ccontr, simp add: lcm_def gcd_zero)
chaieb@27568
   524
assume h:"m*n div gcd m n = 0"
haftmann@27556
   525
from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
haftmann@27556
   526
hence gcdp: "gcd m n > 0" by simp
chaieb@23244
   527
with h
haftmann@27556
   528
have "m*n < gcd m n"
haftmann@27556
   529
  by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
chaieb@23244
   530
moreover 
haftmann@27556
   531
have "gcd m n dvd m" by simp
haftmann@27556
   532
 with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
chaieb@27568
   533
 with npos have t1:"gcd m n *n \<le> m*n" by simp
haftmann@27556
   534
 have "gcd m n \<le> gcd m n*n" using npos by simp
haftmann@27556
   535
 with t1 have "gcd m n \<le> m*n" by arith
chaieb@23244
   536
ultimately show "False" by simp
chaieb@23244
   537
qed
chaieb@23244
   538
haftmann@27556
   539
lemma zlcm_pos: 
nipkow@23983
   540
  assumes anz: "a \<noteq> 0"
nipkow@23983
   541
  and bnz: "b \<noteq> 0" 
haftmann@27556
   542
  shows "0 < zlcm a b"
chaieb@23244
   543
proof-
chaieb@23244
   544
  let ?na = "nat (abs a)"
chaieb@23244
   545
  let ?nb = "nat (abs b)"
nipkow@23983
   546
  have nap: "?na >0" using anz by simp
nipkow@23983
   547
  have nbp: "?nb >0" using bnz by simp
haftmann@27556
   548
  have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
haftmann@27556
   549
  thus ?thesis by (simp add: zlcm_def)
chaieb@23244
   550
qed
chaieb@23244
   551
wenzelm@21256
   552
end