src/HOL/Library/GCD.thy
author haftmann
Mon Jul 14 11:04:42 2008 +0200 (2008-07-14)
changeset 27556 292098f2efdf
parent 27487 c8a6ce181805
child 27568 9949dc7a24de
permissions -rw-r--r--
unified curried gcd, lcm, zgcd, zlcm
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
haftmann@27556
    42
fun gcd  :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@27556
    43
  "gcd m n = (if n = 0 then m else gcd n (m mod n))"
wenzelm@21256
    44
haftmann@27556
    45
thm gcd.induct
haftmann@27556
    46
haftmann@27556
    47
lemma gcd_induct [case_names "0" rec]:
haftmann@23687
    48
  fixes m n :: nat
haftmann@23687
    49
  assumes "\<And>m. P m 0"
haftmann@23687
    50
    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
haftmann@23687
    51
  shows "P m n"
haftmann@27556
    52
proof (induct m n rule: gcd.induct)
haftmann@27556
    53
  case (1 m n) with assms show ?case by (cases "n = 0") simp_all
haftmann@27556
    54
qed
wenzelm@21256
    55
haftmann@27556
    56
lemma gcd_0 [simp]: "gcd m 0 = m"
wenzelm@21263
    57
  by simp
wenzelm@21256
    58
haftmann@27556
    59
lemma gcd_0_left [simp]: "gcd 0 m = m"
haftmann@23687
    60
  by simp
haftmann@23687
    61
haftmann@27556
    62
lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
haftmann@23687
    63
  by simp
haftmann@23687
    64
haftmann@27556
    65
lemma gcd_1 [simp]: "gcd m (Suc 0) = 1"
wenzelm@21263
    66
  by simp
wenzelm@21256
    67
wenzelm@21256
    68
declare gcd.simps [simp del]
wenzelm@21256
    69
wenzelm@21256
    70
text {*
haftmann@27556
    71
  \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
wenzelm@21256
    72
  conjunctions don't seem provable separately.
wenzelm@21256
    73
*}
wenzelm@21256
    74
haftmann@27556
    75
lemma gcd_dvd1 [iff]: "gcd m n dvd m"
haftmann@27556
    76
  and gcd_dvd2 [iff]: "gcd m n dvd n"
wenzelm@21256
    77
  apply (induct m n rule: gcd_induct)
wenzelm@21263
    78
     apply (simp_all add: gcd_non_0)
wenzelm@21256
    79
  apply (blast dest: dvd_mod_imp_dvd)
wenzelm@21256
    80
  done
wenzelm@21256
    81
wenzelm@21256
    82
text {*
wenzelm@21256
    83
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
wenzelm@21256
    84
  naturals, if @{term k} divides @{term m} and @{term k} divides
haftmann@27556
    85
  @{term n} then @{term k} divides @{term "gcd m n"}.
wenzelm@21256
    86
*}
wenzelm@21256
    87
haftmann@27556
    88
lemma gcd_greatest: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd gcd m n"
wenzelm@21263
    89
  by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
wenzelm@21256
    90
wenzelm@21256
    91
text {*
wenzelm@21256
    92
  \medskip Function gcd yields the Greatest Common Divisor.
wenzelm@21256
    93
*}
wenzelm@21256
    94
haftmann@27556
    95
lemma is_gcd: "is_gcd m n (gcd m n) "
haftmann@23687
    96
  by (simp add: is_gcd_def gcd_greatest)
wenzelm@21256
    97
wenzelm@21256
    98
haftmann@23687
    99
subsection {* Derived laws for GCD *}
wenzelm@21256
   100
haftmann@27556
   101
lemma gcd_greatest_iff [iff]: "k dvd gcd m n \<longleftrightarrow> k dvd m \<and> k dvd n"
haftmann@23687
   102
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@23687
   103
haftmann@27556
   104
lemma gcd_zero: "gcd m n = 0 \<longleftrightarrow> m = 0 \<and> n = 0"
haftmann@23687
   105
  by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
wenzelm@21256
   106
haftmann@27556
   107
lemma gcd_commute: "gcd m n = gcd n m"
wenzelm@21256
   108
  apply (rule is_gcd_unique)
wenzelm@21256
   109
   apply (rule is_gcd)
wenzelm@21256
   110
  apply (subst is_gcd_commute)
wenzelm@21256
   111
  apply (simp add: is_gcd)
wenzelm@21256
   112
  done
wenzelm@21256
   113
haftmann@27556
   114
lemma gcd_assoc: "gcd (gcd k m) n = gcd k (gcd m n)"
wenzelm@21256
   115
  apply (rule is_gcd_unique)
wenzelm@21256
   116
   apply (rule is_gcd)
wenzelm@21256
   117
  apply (simp add: is_gcd_def)
wenzelm@21256
   118
  apply (blast intro: dvd_trans)
wenzelm@21256
   119
  done
wenzelm@21256
   120
haftmann@27556
   121
lemma gcd_1_left [simp]: "gcd (Suc 0) m = 1"
haftmann@23687
   122
  by (simp add: gcd_commute)
wenzelm@21256
   123
wenzelm@21256
   124
text {*
wenzelm@21256
   125
  \medskip Multiplication laws
wenzelm@21256
   126
*}
wenzelm@21256
   127
haftmann@27556
   128
lemma gcd_mult_distrib2: "k * gcd m n = gcd (k * m) (k * n)"
wenzelm@21256
   129
    -- {* \cite[page 27]{davenport92} *}
wenzelm@21256
   130
  apply (induct m n rule: gcd_induct)
wenzelm@21256
   131
   apply simp
wenzelm@21256
   132
  apply (case_tac "k = 0")
wenzelm@21256
   133
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
wenzelm@21256
   134
  done
wenzelm@21256
   135
haftmann@27556
   136
lemma gcd_mult [simp]: "gcd k (k * n) = k"
wenzelm@21256
   137
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
wenzelm@21256
   138
  done
wenzelm@21256
   139
haftmann@27556
   140
lemma gcd_self [simp]: "gcd k k = k"
wenzelm@21256
   141
  apply (rule gcd_mult [of k 1, simplified])
wenzelm@21256
   142
  done
wenzelm@21256
   143
haftmann@27556
   144
lemma relprime_dvd_mult: "gcd k n = 1 ==> k dvd m * n ==> k dvd m"
wenzelm@21256
   145
  apply (insert gcd_mult_distrib2 [of m k n])
wenzelm@21256
   146
  apply simp
wenzelm@21256
   147
  apply (erule_tac t = m in ssubst)
wenzelm@21256
   148
  apply simp
wenzelm@21256
   149
  done
wenzelm@21256
   150
haftmann@27556
   151
lemma relprime_dvd_mult_iff: "gcd k n = 1 ==> (k dvd m * n) = (k dvd m)"
wenzelm@21256
   152
  apply (blast intro: relprime_dvd_mult dvd_trans)
wenzelm@21256
   153
  done
wenzelm@21256
   154
haftmann@27556
   155
lemma gcd_mult_cancel: "gcd k n = 1 ==> gcd (k * m) n = gcd m n"
wenzelm@21256
   156
  apply (rule dvd_anti_sym)
wenzelm@21256
   157
   apply (rule gcd_greatest)
wenzelm@21256
   158
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   159
     apply (simp add: gcd_assoc)
wenzelm@21256
   160
     apply (simp add: gcd_commute)
wenzelm@21256
   161
    apply (simp_all add: mult_commute)
wenzelm@21256
   162
  apply (blast intro: dvd_trans)
wenzelm@21256
   163
  done
wenzelm@21256
   164
wenzelm@21256
   165
wenzelm@21256
   166
text {* \medskip Addition laws *}
wenzelm@21256
   167
haftmann@27556
   168
lemma gcd_add1 [simp]: "gcd (m + n) n = gcd m n"
wenzelm@21256
   169
  apply (case_tac "n = 0")
wenzelm@21256
   170
   apply (simp_all add: gcd_non_0)
wenzelm@21256
   171
  done
wenzelm@21256
   172
haftmann@27556
   173
lemma gcd_add2 [simp]: "gcd m (m + n) = gcd m n"
wenzelm@21256
   174
proof -
haftmann@27556
   175
  have "gcd m (m + n) = gcd (m + n) m" by (rule gcd_commute)
haftmann@27556
   176
  also have "... = gcd (n + m) m" by (simp add: add_commute)
haftmann@27556
   177
  also have "... = gcd n m" by simp
haftmann@27556
   178
  also have  "... = gcd m n" by (rule gcd_commute)
wenzelm@21256
   179
  finally show ?thesis .
wenzelm@21256
   180
qed
wenzelm@21256
   181
haftmann@27556
   182
lemma gcd_add2' [simp]: "gcd m (n + m) = gcd m n"
wenzelm@21256
   183
  apply (subst add_commute)
wenzelm@21256
   184
  apply (rule gcd_add2)
wenzelm@21256
   185
  done
wenzelm@21256
   186
haftmann@27556
   187
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
wenzelm@21263
   188
  by (induct k) (simp_all add: add_assoc)
wenzelm@21256
   189
haftmann@27556
   190
lemma gcd_dvd_prod: "gcd m n dvd m * n"
haftmann@23687
   191
  using mult_dvd_mono [of 1] by auto
chaieb@22027
   192
wenzelm@22367
   193
text {*
wenzelm@22367
   194
  \medskip Division by gcd yields rrelatively primes.
wenzelm@22367
   195
*}
chaieb@22027
   196
chaieb@22027
   197
lemma div_gcd_relprime:
wenzelm@22367
   198
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@27556
   199
  shows "gcd (a div gcd a b) (b div gcd a b) = 1"
wenzelm@22367
   200
proof -
haftmann@27556
   201
  let ?g = "gcd a b"
chaieb@22027
   202
  let ?a' = "a div ?g"
chaieb@22027
   203
  let ?b' = "b div ?g"
haftmann@27556
   204
  let ?g' = "gcd ?a' ?b'"
chaieb@22027
   205
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
   206
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
wenzelm@22367
   207
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   208
      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
chaieb@22027
   209
    unfolding dvd_def by blast
wenzelm@22367
   210
  then have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   211
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   212
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   213
      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   214
  have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
wenzelm@22367
   215
  then have gp: "?g > 0" by simp
wenzelm@22367
   216
  from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   217
  with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
chaieb@22027
   218
qed
chaieb@22027
   219
haftmann@23687
   220
subsection {* LCM defined by GCD *}
wenzelm@22367
   221
haftmann@23687
   222
definition
haftmann@27556
   223
  lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@23687
   224
where
haftmann@27556
   225
  lcm_prim_def: "lcm m n = m * n div gcd m n"
haftmann@23687
   226
haftmann@23687
   227
lemma lcm_def:
haftmann@27556
   228
  "lcm m n = m * n div gcd m n"
wenzelm@26304
   229
  unfolding lcm_prim_def by simp
haftmann@23687
   230
haftmann@23687
   231
lemma prod_gcd_lcm:
haftmann@27556
   232
  "m * n = gcd m n * lcm m n"
haftmann@23687
   233
  unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
haftmann@23687
   234
haftmann@27556
   235
lemma lcm_0 [simp]: "lcm m 0 = 0"
haftmann@23687
   236
  unfolding lcm_def by simp
haftmann@23687
   237
haftmann@27556
   238
lemma lcm_1 [simp]: "lcm m 1 = m"
haftmann@23687
   239
  unfolding lcm_def by simp
haftmann@23687
   240
haftmann@27556
   241
lemma lcm_0_left [simp]: "lcm 0 n = 0"
haftmann@23687
   242
  unfolding lcm_def by simp
haftmann@23687
   243
haftmann@27556
   244
lemma lcm_1_left [simp]: "lcm 1 m = m"
haftmann@23687
   245
  unfolding lcm_def by simp
haftmann@23687
   246
haftmann@23687
   247
lemma dvd_pos:
haftmann@23687
   248
  fixes n m :: nat
haftmann@23687
   249
  assumes "n > 0" and "m dvd n"
haftmann@23687
   250
  shows "m > 0"
haftmann@23687
   251
using assms by (cases m) auto
haftmann@23687
   252
haftmann@23951
   253
lemma lcm_least:
haftmann@23687
   254
  assumes "m dvd k" and "n dvd k"
haftmann@27556
   255
  shows "lcm m n dvd k"
haftmann@23687
   256
proof (cases k)
haftmann@23687
   257
  case 0 then show ?thesis by auto
haftmann@23687
   258
next
haftmann@23687
   259
  case (Suc _) then have pos_k: "k > 0" by auto
haftmann@23687
   260
  from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
haftmann@27556
   261
  with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
haftmann@23687
   262
  from assms obtain p where k_m: "k = m * p" using dvd_def by blast
haftmann@23687
   263
  from assms obtain q where k_n: "k = n * q" using dvd_def by blast
haftmann@23687
   264
  from pos_k k_m have pos_p: "p > 0" by auto
haftmann@23687
   265
  from pos_k k_n have pos_q: "q > 0" by auto
haftmann@27556
   266
  have "k * k * gcd q p = k * gcd (k * q) (k * p)"
haftmann@23687
   267
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   268
  also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
haftmann@23687
   269
    by (simp add: k_m [symmetric] k_n [symmetric])
haftmann@27556
   270
  also have "\<dots> = k * p * q * gcd m n"
haftmann@23687
   271
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   272
  finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
haftmann@23687
   273
    by (simp only: k_m [symmetric] k_n [symmetric])
haftmann@27556
   274
  then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
haftmann@23687
   275
    by (simp add: mult_ac)
haftmann@27556
   276
  with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
haftmann@23687
   277
    by simp
haftmann@23687
   278
  with prod_gcd_lcm [of m n]
haftmann@27556
   279
  have "lcm m n * gcd q p * gcd m n = k * gcd m n"
haftmann@23687
   280
    by (simp add: mult_ac)
haftmann@27556
   281
  with pos_gcd have "lcm m n * gcd q p = k" by simp
haftmann@23687
   282
  then show ?thesis using dvd_def by auto
haftmann@23687
   283
qed
haftmann@23687
   284
haftmann@23687
   285
lemma lcm_dvd1 [iff]:
haftmann@27556
   286
  "m dvd lcm m n"
haftmann@23687
   287
proof (cases m)
haftmann@23687
   288
  case 0 then show ?thesis by simp
haftmann@23687
   289
next
haftmann@23687
   290
  case (Suc _)
haftmann@23687
   291
  then have mpos: "m > 0" by simp
haftmann@23687
   292
  show ?thesis
haftmann@23687
   293
  proof (cases n)
haftmann@23687
   294
    case 0 then show ?thesis by simp
haftmann@23687
   295
  next
haftmann@23687
   296
    case (Suc _)
haftmann@23687
   297
    then have npos: "n > 0" by simp
haftmann@27556
   298
    have "gcd m n dvd n" by simp
haftmann@27556
   299
    then obtain k where "n = gcd m n * k" using dvd_def by auto
haftmann@27556
   300
    then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: mult_ac)
haftmann@23687
   301
    also have "\<dots> = m * k" using mpos npos gcd_zero by simp
haftmann@23687
   302
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   303
  qed
haftmann@23687
   304
qed
haftmann@23687
   305
haftmann@23687
   306
lemma lcm_dvd2 [iff]: 
haftmann@27556
   307
  "n dvd lcm m n"
haftmann@23687
   308
proof (cases n)
haftmann@23687
   309
  case 0 then show ?thesis by simp
haftmann@23687
   310
next
haftmann@23687
   311
  case (Suc _)
haftmann@23687
   312
  then have npos: "n > 0" by simp
haftmann@23687
   313
  show ?thesis
haftmann@23687
   314
  proof (cases m)
haftmann@23687
   315
    case 0 then show ?thesis by simp
haftmann@23687
   316
  next
haftmann@23687
   317
    case (Suc _)
haftmann@23687
   318
    then have mpos: "m > 0" by simp
haftmann@27556
   319
    have "gcd m n dvd m" by simp
haftmann@27556
   320
    then obtain k where "m = gcd m n * k" using dvd_def by auto
haftmann@27556
   321
    then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: mult_ac)
haftmann@23687
   322
    also have "\<dots> = n * k" using mpos npos gcd_zero by simp
haftmann@23687
   323
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   324
  qed
haftmann@23687
   325
qed
haftmann@23687
   326
haftmann@23687
   327
haftmann@23687
   328
subsection {* GCD and LCM on integers *}
wenzelm@22367
   329
wenzelm@22367
   330
definition
haftmann@27556
   331
  zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
haftmann@27556
   332
  "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
wenzelm@22367
   333
haftmann@27556
   334
lemma zgcd_dvd1 [simp]: "zgcd i j dvd i"
haftmann@27556
   335
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   336
haftmann@27556
   337
lemma zgcd_dvd2 [simp]: "zgcd i j dvd j"
haftmann@27556
   338
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   339
haftmann@27556
   340
lemma zgcd_pos: "zgcd i j \<ge> 0"
haftmann@27556
   341
  by (simp add: zgcd_def)
wenzelm@22367
   342
haftmann@27556
   343
lemma zgcd0 [simp]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
haftmann@27556
   344
  by (simp add: zgcd_def gcd_zero) arith
chaieb@22027
   345
haftmann@27556
   346
lemma zgcd_commute: "zgcd i j = zgcd j i"
haftmann@27556
   347
  unfolding zgcd_def by (simp add: gcd_commute)
wenzelm@22367
   348
haftmann@27556
   349
lemma zgcd_neg1 [simp]: "zgcd (- i) j = zgcd i j"
haftmann@27556
   350
  unfolding zgcd_def by simp
wenzelm@22367
   351
haftmann@27556
   352
lemma zgcd_neg2 [simp]: "zgcd i (- j) = zgcd i j"
haftmann@27556
   353
  unfolding zgcd_def by simp
wenzelm@22367
   354
haftmann@27556
   355
lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
haftmann@27556
   356
  unfolding zgcd_def
wenzelm@22367
   357
proof -
haftmann@27556
   358
  assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
haftmann@27556
   359
  then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
wenzelm@22367
   360
  from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
chaieb@22027
   361
  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
wenzelm@22367
   362
    unfolding dvd_def
wenzelm@22367
   363
    by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
wenzelm@22367
   364
  from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
chaieb@22027
   365
    unfolding dvd_def by blast
chaieb@22027
   366
  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
huffman@23431
   367
  then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
chaieb@22027
   368
  then show ?thesis
wenzelm@22367
   369
    apply (subst zdvd_abs1 [symmetric])
wenzelm@22367
   370
    apply (subst zdvd_abs2 [symmetric])
chaieb@22027
   371
    apply (unfold dvd_def)
wenzelm@22367
   372
    apply (rule_tac x = "int h'" in exI, simp)
chaieb@22027
   373
    done
chaieb@22027
   374
qed
chaieb@22027
   375
haftmann@27556
   376
lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
wenzelm@22367
   377
haftmann@27556
   378
lemma zgcd_greatest:
wenzelm@22367
   379
  assumes "k dvd m" and "k dvd n"
haftmann@27556
   380
  shows "k dvd zgcd m n"
wenzelm@22367
   381
proof -
chaieb@22027
   382
  let ?k' = "nat \<bar>k\<bar>"
chaieb@22027
   383
  let ?m' = "nat \<bar>m\<bar>"
chaieb@22027
   384
  let ?n' = "nat \<bar>n\<bar>"
wenzelm@22367
   385
  from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
chaieb@22027
   386
    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
haftmann@27556
   387
  from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
haftmann@27556
   388
    unfolding zgcd_def by (simp only: zdvd_int)
haftmann@27556
   389
  then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
haftmann@27556
   390
  then show "k dvd zgcd m n" by (simp add: zdvd_abs1)
chaieb@22027
   391
qed
chaieb@22027
   392
haftmann@27556
   393
lemma div_zgcd_relprime:
wenzelm@22367
   394
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@27556
   395
  shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
wenzelm@22367
   396
proof -
chaieb@25112
   397
  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
haftmann@27556
   398
  let ?g = "zgcd a b"
chaieb@22027
   399
  let ?a' = "a div ?g"
chaieb@22027
   400
  let ?b' = "b div ?g"
haftmann@27556
   401
  let ?g' = "zgcd ?a' ?b'"
haftmann@27556
   402
  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: zgcd_dvd1 zgcd_dvd2)
haftmann@27556
   403
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: zgcd_dvd1 zgcd_dvd2)
wenzelm@22367
   404
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   405
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
chaieb@22027
   406
    unfolding dvd_def by blast
wenzelm@22367
   407
  then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   408
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   409
    by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   410
      zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   411
  have "?g \<noteq> 0" using nz by simp
haftmann@27556
   412
  then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
haftmann@27556
   413
  from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   414
  with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
haftmann@27556
   415
  with zgcd_pos show "?g' = 1" by simp
chaieb@22027
   416
qed
chaieb@22027
   417
haftmann@27556
   418
definition zlcm :: "int \<Rightarrow> int \<Rightarrow> int" where
haftmann@27556
   419
  "zlcm i j = int (lcm (nat (abs i)) (nat (abs j)))"
chaieb@23244
   420
haftmann@27556
   421
lemma dvd_zlcm_self1[simp]: "i dvd zlcm i j"
haftmann@27556
   422
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   423
haftmann@27556
   424
lemma dvd_zlcm_self2[simp]: "j dvd zlcm i j"
haftmann@27556
   425
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   426
chaieb@23244
   427
haftmann@27556
   428
lemma dvd_imp_dvd_zlcm1:
haftmann@27556
   429
  assumes "k dvd i" shows "k dvd (zlcm i j)"
nipkow@23983
   430
proof -
nipkow@23983
   431
  have "nat(abs k) dvd nat(abs i)" using `k dvd i`
chaieb@23994
   432
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   433
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   434
qed
nipkow@23983
   435
haftmann@27556
   436
lemma dvd_imp_dvd_zlcm2:
haftmann@27556
   437
  assumes "k dvd j" shows "k dvd (zlcm i j)"
nipkow@23983
   438
proof -
nipkow@23983
   439
  have "nat(abs k) dvd nat(abs j)" using `k dvd j`
chaieb@23994
   440
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   441
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   442
qed
nipkow@23983
   443
chaieb@23994
   444
chaieb@23244
   445
lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
chaieb@23244
   446
by (case_tac "d <0", simp_all)
chaieb@23244
   447
chaieb@23244
   448
lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
chaieb@23244
   449
by (case_tac "d<0", simp_all)
chaieb@23244
   450
chaieb@23244
   451
(* lcm a b is positive for positive a and b *)
chaieb@23244
   452
chaieb@23244
   453
lemma lcm_pos: 
chaieb@23244
   454
  assumes mpos: "m > 0"
haftmann@27556
   455
  and npos: "n > 0"
haftmann@27556
   456
  shows "lcm m n > 0"
chaieb@23244
   457
proof(rule ccontr, simp add: lcm_def gcd_zero)
haftmann@27556
   458
assume h:"m * n div gcd m n = 0"
haftmann@27556
   459
from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
haftmann@27556
   460
hence gcdp: "gcd m n > 0" by simp
chaieb@23244
   461
with h
haftmann@27556
   462
have "m*n < gcd m n"
haftmann@27556
   463
  by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
chaieb@23244
   464
moreover 
haftmann@27556
   465
have "gcd m n dvd m" by simp
haftmann@27556
   466
 with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
haftmann@27556
   467
 with npos have t1:"gcd m n*n \<le> m*n" by simp
haftmann@27556
   468
 have "gcd m n \<le> gcd m n*n" using npos by simp
haftmann@27556
   469
 with t1 have "gcd m n \<le> m*n" by arith
chaieb@23244
   470
ultimately show "False" by simp
chaieb@23244
   471
qed
chaieb@23244
   472
haftmann@27556
   473
lemma zlcm_pos: 
nipkow@23983
   474
  assumes anz: "a \<noteq> 0"
nipkow@23983
   475
  and bnz: "b \<noteq> 0" 
haftmann@27556
   476
  shows "0 < zlcm a b"
chaieb@23244
   477
proof-
chaieb@23244
   478
  let ?na = "nat (abs a)"
chaieb@23244
   479
  let ?nb = "nat (abs b)"
nipkow@23983
   480
  have nap: "?na >0" using anz by simp
nipkow@23983
   481
  have nbp: "?nb >0" using bnz by simp
haftmann@27556
   482
  have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
haftmann@27556
   483
  thus ?thesis by (simp add: zlcm_def)
chaieb@23244
   484
qed
chaieb@23244
   485
wenzelm@21256
   486
end