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