src/HOL/Library/GCD.thy
author chaieb
Mon Jul 21 13:37:05 2008 +0200 (2008-07-21)
changeset 27669 4b1642284dd7
parent 27651 16a26996c30e
child 27676 55676111ed69
permissions -rw-r--r--
Tuned and simplified proofs; Rules added to presburger's and algebra's context; moved Bezout theorems from Primes.thy
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
chaieb@27669
    55
lemma gcd_0 [simp, algebra]: "gcd m 0 = m"
wenzelm@21263
    56
  by simp
wenzelm@21256
    57
chaieb@27669
    58
lemma gcd_0_left [simp,algebra]: "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
chaieb@27669
    64
lemma gcd_1 [simp, algebra]: "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
chaieb@27669
    74
lemma gcd_dvd1 [iff, algebra]: "gcd m n dvd m"
chaieb@27669
    75
  and gcd_dvd2 [iff, algebra]: "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
chaieb@27669
   100
lemma gcd_greatest_iff [iff, algebra]: "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
chaieb@27669
   103
lemma gcd_zero[algebra]: "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
chaieb@27669
   120
lemma gcd_1_left [simp, algebra]: "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
chaieb@27669
   135
lemma gcd_mult [simp, algebra]: "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
chaieb@27669
   139
lemma gcd_self [simp, algebra]: "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)"
haftmann@27651
   151
  by (auto intro: relprime_dvd_mult dvd_mult2)
wenzelm@21256
   152
haftmann@27556
   153
lemma gcd_mult_cancel: "gcd k n = 1 ==> gcd (k * m) n = gcd m n"
wenzelm@21256
   154
  apply (rule dvd_anti_sym)
wenzelm@21256
   155
   apply (rule gcd_greatest)
wenzelm@21256
   156
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   157
     apply (simp add: gcd_assoc)
wenzelm@21256
   158
     apply (simp add: gcd_commute)
wenzelm@21256
   159
    apply (simp_all add: mult_commute)
haftmann@27651
   160
  apply (blast intro: dvd_mult)
wenzelm@21256
   161
  done
wenzelm@21256
   162
wenzelm@21256
   163
wenzelm@21256
   164
text {* \medskip Addition laws *}
wenzelm@21256
   165
chaieb@27669
   166
lemma gcd_add1 [simp,algebra]: "gcd (m + n) n = gcd m n"
wenzelm@21256
   167
  apply (case_tac "n = 0")
wenzelm@21256
   168
   apply (simp_all add: gcd_non_0)
haftmann@27651
   169
  apply (simp add: mod_add_self2)
wenzelm@21256
   170
  done
wenzelm@21256
   171
chaieb@27669
   172
lemma gcd_add2 [simp, algebra]: "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
chaieb@27669
   181
lemma gcd_add2' [simp, algebra]: "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
chaieb@27669
   186
lemma gcd_add_mult[algebra]: "gcd m (k * m + n) = gcd m n"
wenzelm@21263
   187
  by (induct k) (simp_all add: add_assoc)
wenzelm@21256
   188
chaieb@27669
   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
chaieb@27669
   219
chaieb@27669
   220
lemma gcd_unique: "d dvd a\<and>d dvd b \<and> (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
chaieb@27669
   221
proof(auto)
chaieb@27669
   222
  assume H: "d dvd a" "d dvd b" "\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d"
chaieb@27669
   223
  from H(3)[rule_format] gcd_dvd1[of a b] gcd_dvd2[of a b] 
chaieb@27669
   224
  have th: "gcd a b dvd d" by blast
chaieb@27669
   225
  from dvd_anti_sym[OF th gcd_greatest[OF H(1,2)]]  show "d = gcd a b" by blast 
chaieb@27669
   226
qed
chaieb@27669
   227
chaieb@27669
   228
lemma gcd_eq: assumes H: "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd u \<and> d dvd v"
chaieb@27669
   229
  shows "gcd x y = gcd u v"
chaieb@27669
   230
proof-
chaieb@27669
   231
  from H have "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd gcd u v" by simp
chaieb@27669
   232
  with gcd_unique[of "gcd u v" x y]  show ?thesis by auto
chaieb@27669
   233
qed
chaieb@27669
   234
chaieb@27669
   235
lemma ind_euclid: 
chaieb@27669
   236
  assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0" 
chaieb@27669
   237
  and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)" 
chaieb@27669
   238
  shows "P a b"
chaieb@27669
   239
proof(induct n\<equiv>"a+b" arbitrary: a b rule: nat_less_induct)
chaieb@27669
   240
  fix n a b
chaieb@27669
   241
  assume H: "\<forall>m < n. \<forall>a b. m = a + b \<longrightarrow> P a b" "n = a + b"
chaieb@27669
   242
  have "a = b \<or> a < b \<or> b < a" by arith
chaieb@27669
   243
  moreover {assume eq: "a= b"
chaieb@27669
   244
    from add[rule_format, OF z[rule_format, of a]] have "P a b" using eq by simp}
chaieb@27669
   245
  moreover
chaieb@27669
   246
  {assume lt: "a < b"
chaieb@27669
   247
    hence "a + b - a < n \<or> a = 0"  using H(2) by arith
chaieb@27669
   248
    moreover
chaieb@27669
   249
    {assume "a =0" with z c have "P a b" by blast }
chaieb@27669
   250
    moreover
chaieb@27669
   251
    {assume ab: "a + b - a < n"
chaieb@27669
   252
      have th0: "a + b - a = a + (b - a)" using lt by arith
chaieb@27669
   253
      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
chaieb@27669
   254
      have "P a b" by (simp add: th0[symmetric])}
chaieb@27669
   255
    ultimately have "P a b" by blast}
chaieb@27669
   256
  moreover
chaieb@27669
   257
  {assume lt: "a > b"
chaieb@27669
   258
    hence "b + a - b < n \<or> b = 0"  using H(2) by arith
chaieb@27669
   259
    moreover
chaieb@27669
   260
    {assume "b =0" with z c have "P a b" by blast }
chaieb@27669
   261
    moreover
chaieb@27669
   262
    {assume ab: "b + a - b < n"
chaieb@27669
   263
      have th0: "b + a - b = b + (a - b)" using lt by arith
chaieb@27669
   264
      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
chaieb@27669
   265
      have "P b a" by (simp add: th0[symmetric])
chaieb@27669
   266
      hence "P a b" using c by blast }
chaieb@27669
   267
    ultimately have "P a b" by blast}
chaieb@27669
   268
ultimately  show "P a b" by blast
chaieb@27669
   269
qed
chaieb@27669
   270
chaieb@27669
   271
lemma bezout_lemma: 
chaieb@27669
   272
  assumes ex: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x = b * y + d \<or> b * x = a * y + d)"
chaieb@27669
   273
  shows "\<exists>d x y. d dvd a \<and> d dvd a + b \<and> (a * x = (a + b) * y + d \<or> (a + b) * x = a * y + d)"
chaieb@27669
   274
using ex
chaieb@27669
   275
apply clarsimp
chaieb@27669
   276
apply (rule_tac x="d" in exI, simp add: dvd_add)
chaieb@27669
   277
apply (case_tac "a * x = b * y + d" , simp_all)
chaieb@27669
   278
apply (rule_tac x="x + y" in exI)
chaieb@27669
   279
apply (rule_tac x="y" in exI)
chaieb@27669
   280
apply algebra
chaieb@27669
   281
apply (rule_tac x="x" in exI)
chaieb@27669
   282
apply (rule_tac x="x + y" in exI)
chaieb@27669
   283
apply algebra
chaieb@27669
   284
done
chaieb@27669
   285
chaieb@27669
   286
lemma bezout_add: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x = b * y + d \<or> b * x = a * y + d)"
chaieb@27669
   287
apply(induct a b rule: ind_euclid)
chaieb@27669
   288
apply blast
chaieb@27669
   289
apply clarify
chaieb@27669
   290
apply (rule_tac x="a" in exI, simp add: dvd_add)
chaieb@27669
   291
apply clarsimp
chaieb@27669
   292
apply (rule_tac x="d" in exI)
chaieb@27669
   293
apply (case_tac "a * x = b * y + d", simp_all add: dvd_add)
chaieb@27669
   294
apply (rule_tac x="x+y" in exI)
chaieb@27669
   295
apply (rule_tac x="y" in exI)
chaieb@27669
   296
apply algebra
chaieb@27669
   297
apply (rule_tac x="x" in exI)
chaieb@27669
   298
apply (rule_tac x="x+y" in exI)
chaieb@27669
   299
apply algebra
chaieb@27669
   300
done
chaieb@27669
   301
chaieb@27669
   302
lemma bezout: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x - b * y = d \<or> b * x - a * y = d)"
chaieb@27669
   303
using bezout_add[of a b]
chaieb@27669
   304
apply clarsimp
chaieb@27669
   305
apply (rule_tac x="d" in exI, simp)
chaieb@27669
   306
apply (rule_tac x="x" in exI)
chaieb@27669
   307
apply (rule_tac x="y" in exI)
chaieb@27669
   308
apply auto
chaieb@27669
   309
done
chaieb@27669
   310
chaieb@27669
   311
chaieb@27669
   312
text {* We can get a stronger version with a nonzeroness assumption. *}
chaieb@27669
   313
lemma divides_le: "m dvd n ==> m <= n \<or> n = (0::nat)" by (auto simp add: dvd_def)
chaieb@27669
   314
chaieb@27669
   315
lemma bezout_add_strong: assumes nz: "a \<noteq> (0::nat)"
chaieb@27669
   316
  shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
chaieb@27669
   317
proof-
chaieb@27669
   318
  from nz have ap: "a > 0" by simp
chaieb@27669
   319
 from bezout_add[of a b] 
chaieb@27669
   320
 have "(\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d) \<or> (\<exists>d x y. d dvd a \<and> d dvd b \<and> b * x = a * y + d)" by blast
chaieb@27669
   321
 moreover
chaieb@27669
   322
 {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
chaieb@27669
   323
   from H have ?thesis by blast }
chaieb@27669
   324
 moreover
chaieb@27669
   325
 {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
chaieb@27669
   326
   {assume b0: "b = 0" with H  have ?thesis by simp}
chaieb@27669
   327
   moreover 
chaieb@27669
   328
   {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
chaieb@27669
   329
     from divides_le[OF H(2)] b have "d < b \<or> d = b" using le_less by blast
chaieb@27669
   330
     moreover
chaieb@27669
   331
     {assume db: "d=b"
chaieb@27669
   332
       from prems have ?thesis apply simp
chaieb@27669
   333
	 apply (rule exI[where x = b], simp)
chaieb@27669
   334
	 apply (rule exI[where x = b])
chaieb@27669
   335
	by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
chaieb@27669
   336
    moreover
chaieb@27669
   337
    {assume db: "d < b" 
chaieb@27669
   338
	{assume "x=0" hence ?thesis  using prems by simp }
chaieb@27669
   339
	moreover
chaieb@27669
   340
	{assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
chaieb@27669
   341
	  
chaieb@27669
   342
	  from db have "d \<le> b - 1" by simp
chaieb@27669
   343
	  hence "d*b \<le> b*(b - 1)" by simp
chaieb@27669
   344
	  with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
chaieb@27669
   345
	  have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
chaieb@27669
   346
	  from H (3) have "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)" by algebra
chaieb@27669
   347
	  hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
chaieb@27669
   348
	  hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)" 
chaieb@27669
   349
	    by (simp only: diff_add_assoc[OF dble, of d, symmetric])
chaieb@27669
   350
	  hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
chaieb@27669
   351
	    by (simp only: diff_mult_distrib2 add_commute mult_ac)
chaieb@27669
   352
	  hence ?thesis using H(1,2)
chaieb@27669
   353
	    apply -
chaieb@27669
   354
	    apply (rule exI[where x=d], simp)
chaieb@27669
   355
	    apply (rule exI[where x="(b - 1) * y"])
chaieb@27669
   356
	    by (rule exI[where x="x*(b - 1) - d"], simp)}
chaieb@27669
   357
	ultimately have ?thesis by blast}
chaieb@27669
   358
    ultimately have ?thesis by blast}
chaieb@27669
   359
  ultimately have ?thesis by blast}
chaieb@27669
   360
 ultimately show ?thesis by blast
chaieb@27669
   361
qed
chaieb@27669
   362
chaieb@27669
   363
chaieb@27669
   364
lemma bezout_gcd: "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
chaieb@27669
   365
proof-
chaieb@27669
   366
  let ?g = "gcd a b"
chaieb@27669
   367
  from bezout[of a b] obtain d x y where d: "d dvd a" "d dvd b" "a * x - b * y = d \<or> b * x - a * y = d" by blast
chaieb@27669
   368
  from d(1,2) have "d dvd ?g" by simp
chaieb@27669
   369
  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
chaieb@27669
   370
  from d(3) have "(a * x - b * y)*k = d*k \<or> (b * x - a * y)*k = d*k" by blast 
chaieb@27669
   371
  hence "a * x * k - b * y*k = d*k \<or> b * x * k - a * y*k = d*k" 
chaieb@27669
   372
    by (algebra add: diff_mult_distrib)
chaieb@27669
   373
  hence "a * (x * k) - b * (y*k) = ?g \<or> b * (x * k) - a * (y*k) = ?g" 
chaieb@27669
   374
    by (simp add: k mult_assoc)
chaieb@27669
   375
  thus ?thesis by blast
chaieb@27669
   376
qed
chaieb@27669
   377
chaieb@27669
   378
lemma bezout_gcd_strong: assumes a: "a \<noteq> 0" 
chaieb@27669
   379
  shows "\<exists>x y. a * x = b * y + gcd a b"
chaieb@27669
   380
proof-
chaieb@27669
   381
  let ?g = "gcd a b"
chaieb@27669
   382
  from bezout_add_strong[OF a, of b]
chaieb@27669
   383
  obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
chaieb@27669
   384
  from d(1,2) have "d dvd ?g" by simp
chaieb@27669
   385
  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
chaieb@27669
   386
  from d(3) have "a * x * k = (b * y + d) *k " by algebra
chaieb@27669
   387
  hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
chaieb@27669
   388
  thus ?thesis by blast
chaieb@27669
   389
qed
chaieb@27669
   390
chaieb@27669
   391
lemma gcd_mult_distrib: "gcd(a * c) (b * c) = c * gcd a b"
chaieb@27669
   392
by(simp add: gcd_mult_distrib2 mult_commute)
chaieb@27669
   393
chaieb@27669
   394
lemma gcd_bezout: "(\<exists>x y. a * x - b * y = d \<or> b * x - a * y = d) \<longleftrightarrow> gcd a b dvd d"
chaieb@27669
   395
  (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@27669
   396
proof-
chaieb@27669
   397
  let ?g = "gcd a b"
chaieb@27669
   398
  {assume H: ?rhs then obtain k where k: "d = ?g*k" unfolding dvd_def by blast
chaieb@27669
   399
    from bezout_gcd[of a b] obtain x y where xy: "a * x - b * y = ?g \<or> b * x - a * y = ?g"
chaieb@27669
   400
      by blast
chaieb@27669
   401
    hence "(a * x - b * y)*k = ?g*k \<or> (b * x - a * y)*k = ?g*k" by auto
chaieb@27669
   402
    hence "a * x*k - b * y*k = ?g*k \<or> b * x * k - a * y*k = ?g*k" 
chaieb@27669
   403
      by (simp only: diff_mult_distrib)
chaieb@27669
   404
    hence "a * (x*k) - b * (y*k) = d \<or> b * (x * k) - a * (y*k) = d"
chaieb@27669
   405
      by (simp add: k[symmetric] mult_assoc)
chaieb@27669
   406
    hence ?lhs by blast}
chaieb@27669
   407
  moreover
chaieb@27669
   408
  {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
chaieb@27669
   409
    have dv: "?g dvd a*x" "?g dvd b * y" "?g dvd b*x" "?g dvd a * y"
chaieb@27669
   410
      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
chaieb@27669
   411
    from dvd_diff[OF dv(1,2)] dvd_diff[OF dv(3,4)] H
chaieb@27669
   412
    have ?rhs by auto}
chaieb@27669
   413
  ultimately show ?thesis by blast
chaieb@27669
   414
qed
chaieb@27669
   415
chaieb@27669
   416
lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
chaieb@27669
   417
proof-
chaieb@27669
   418
  let ?g = "gcd a b"
chaieb@27669
   419
    have dv: "?g dvd a*x" "?g dvd b * y" 
chaieb@27669
   420
      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
chaieb@27669
   421
    from dvd_add[OF dv] H
chaieb@27669
   422
    show ?thesis by auto
chaieb@27669
   423
qed
chaieb@27669
   424
chaieb@27669
   425
lemma gcd_mult': "gcd b (a * b) = b"
chaieb@27669
   426
by (simp add: gcd_mult mult_commute[of a b]) 
chaieb@27669
   427
chaieb@27669
   428
lemma gcd_add: "gcd(a + b) b = gcd a b" 
chaieb@27669
   429
  "gcd(b + a) b = gcd a b" "gcd a (a + b) = gcd a b" "gcd a (b + a) = gcd a b"
chaieb@27669
   430
apply (simp_all add: gcd_add1)
chaieb@27669
   431
by (simp add: gcd_commute gcd_add1)
chaieb@27669
   432
chaieb@27669
   433
lemma gcd_sub: "b <= a ==> gcd(a - b) b = gcd a b" "a <= b ==> gcd a (b - a) = gcd a b"
chaieb@27669
   434
proof-
chaieb@27669
   435
  {fix a b assume H: "b \<le> (a::nat)"
chaieb@27669
   436
    hence th: "a - b + b = a" by arith
chaieb@27669
   437
    from gcd_add(1)[of "a - b" b] th  have "gcd(a - b) b = gcd a b" by simp}
chaieb@27669
   438
  note th = this
chaieb@27669
   439
{
chaieb@27669
   440
  assume ab: "b \<le> a"
chaieb@27669
   441
  from th[OF ab] show "gcd (a - b)  b = gcd a b" by blast
chaieb@27669
   442
next
chaieb@27669
   443
  assume ab: "a \<le> b"
chaieb@27669
   444
  from th[OF ab] show "gcd a (b - a) = gcd a b" 
chaieb@27669
   445
    by (simp add: gcd_commute)}
chaieb@27669
   446
qed
chaieb@27669
   447
chaieb@27669
   448
haftmann@23687
   449
subsection {* LCM defined by GCD *}
wenzelm@22367
   450
chaieb@27669
   451
haftmann@23687
   452
definition
haftmann@27556
   453
  lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@23687
   454
where
chaieb@27568
   455
  lcm_def: "lcm m n = m * n div gcd m n"
haftmann@23687
   456
haftmann@23687
   457
lemma prod_gcd_lcm:
haftmann@27556
   458
  "m * n = gcd m n * lcm m n"
haftmann@23687
   459
  unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
haftmann@23687
   460
haftmann@27556
   461
lemma lcm_0 [simp]: "lcm m 0 = 0"
haftmann@23687
   462
  unfolding lcm_def by simp
haftmann@23687
   463
haftmann@27556
   464
lemma lcm_1 [simp]: "lcm m 1 = m"
haftmann@23687
   465
  unfolding lcm_def by simp
haftmann@23687
   466
haftmann@27556
   467
lemma lcm_0_left [simp]: "lcm 0 n = 0"
haftmann@23687
   468
  unfolding lcm_def by simp
haftmann@23687
   469
haftmann@27556
   470
lemma lcm_1_left [simp]: "lcm 1 m = m"
haftmann@23687
   471
  unfolding lcm_def by simp
haftmann@23687
   472
haftmann@23687
   473
lemma dvd_pos:
haftmann@23687
   474
  fixes n m :: nat
haftmann@23687
   475
  assumes "n > 0" and "m dvd n"
haftmann@23687
   476
  shows "m > 0"
haftmann@23687
   477
using assms by (cases m) auto
haftmann@23687
   478
haftmann@23951
   479
lemma lcm_least:
haftmann@23687
   480
  assumes "m dvd k" and "n dvd k"
haftmann@27556
   481
  shows "lcm m n dvd k"
haftmann@23687
   482
proof (cases k)
haftmann@23687
   483
  case 0 then show ?thesis by auto
haftmann@23687
   484
next
haftmann@23687
   485
  case (Suc _) then have pos_k: "k > 0" by auto
haftmann@23687
   486
  from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
haftmann@27556
   487
  with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
haftmann@23687
   488
  from assms obtain p where k_m: "k = m * p" using dvd_def by blast
haftmann@23687
   489
  from assms obtain q where k_n: "k = n * q" using dvd_def by blast
haftmann@23687
   490
  from pos_k k_m have pos_p: "p > 0" by auto
haftmann@23687
   491
  from pos_k k_n have pos_q: "q > 0" by auto
haftmann@27556
   492
  have "k * k * gcd q p = k * gcd (k * q) (k * p)"
haftmann@23687
   493
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   494
  also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
haftmann@23687
   495
    by (simp add: k_m [symmetric] k_n [symmetric])
haftmann@27556
   496
  also have "\<dots> = k * p * q * gcd m n"
haftmann@23687
   497
    by (simp add: mult_ac gcd_mult_distrib2)
haftmann@27556
   498
  finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
haftmann@23687
   499
    by (simp only: k_m [symmetric] k_n [symmetric])
haftmann@27556
   500
  then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
haftmann@23687
   501
    by (simp add: mult_ac)
haftmann@27556
   502
  with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
haftmann@23687
   503
    by simp
haftmann@23687
   504
  with prod_gcd_lcm [of m n]
haftmann@27556
   505
  have "lcm m n * gcd q p * gcd m n = k * gcd m n"
haftmann@23687
   506
    by (simp add: mult_ac)
haftmann@27556
   507
  with pos_gcd have "lcm m n * gcd q p = k" by simp
haftmann@23687
   508
  then show ?thesis using dvd_def by auto
haftmann@23687
   509
qed
haftmann@23687
   510
haftmann@23687
   511
lemma lcm_dvd1 [iff]:
haftmann@27556
   512
  "m dvd lcm m n"
haftmann@23687
   513
proof (cases m)
haftmann@23687
   514
  case 0 then show ?thesis by simp
haftmann@23687
   515
next
haftmann@23687
   516
  case (Suc _)
haftmann@23687
   517
  then have mpos: "m > 0" by simp
haftmann@23687
   518
  show ?thesis
haftmann@23687
   519
  proof (cases n)
haftmann@23687
   520
    case 0 then show ?thesis by simp
haftmann@23687
   521
  next
haftmann@23687
   522
    case (Suc _)
haftmann@23687
   523
    then have npos: "n > 0" by simp
haftmann@27556
   524
    have "gcd m n dvd n" by simp
haftmann@27556
   525
    then obtain k where "n = gcd m n * k" using dvd_def by auto
haftmann@27556
   526
    then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: mult_ac)
haftmann@23687
   527
    also have "\<dots> = m * k" using mpos npos gcd_zero by simp
haftmann@23687
   528
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   529
  qed
haftmann@23687
   530
qed
haftmann@23687
   531
haftmann@23687
   532
lemma lcm_dvd2 [iff]: 
haftmann@27556
   533
  "n dvd lcm m n"
haftmann@23687
   534
proof (cases n)
haftmann@23687
   535
  case 0 then show ?thesis by simp
haftmann@23687
   536
next
haftmann@23687
   537
  case (Suc _)
haftmann@23687
   538
  then have npos: "n > 0" by simp
haftmann@23687
   539
  show ?thesis
haftmann@23687
   540
  proof (cases m)
haftmann@23687
   541
    case 0 then show ?thesis by simp
haftmann@23687
   542
  next
haftmann@23687
   543
    case (Suc _)
haftmann@23687
   544
    then have mpos: "m > 0" by simp
haftmann@27556
   545
    have "gcd m n dvd m" by simp
haftmann@27556
   546
    then obtain k where "m = gcd m n * k" using dvd_def by auto
haftmann@27556
   547
    then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: mult_ac)
haftmann@23687
   548
    also have "\<dots> = n * k" using mpos npos gcd_zero by simp
haftmann@23687
   549
    finally show ?thesis by (simp add: lcm_def)
haftmann@23687
   550
  qed
haftmann@23687
   551
qed
haftmann@23687
   552
chaieb@27568
   553
lemma gcd_add1_eq: "gcd (m + k) k = gcd (m + k) m"
chaieb@27568
   554
  by (simp add: gcd_commute)
chaieb@27568
   555
chaieb@27568
   556
lemma gcd_diff2: "m \<le> n ==> gcd n (n - m) = gcd n m"
chaieb@27568
   557
  apply (subgoal_tac "n = m + (n - m)")
chaieb@27669
   558
  apply (erule ssubst, rule gcd_add1_eq, simp)  
chaieb@27568
   559
  done
chaieb@27568
   560
haftmann@23687
   561
haftmann@23687
   562
subsection {* GCD and LCM on integers *}
wenzelm@22367
   563
wenzelm@22367
   564
definition
haftmann@27556
   565
  zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
haftmann@27556
   566
  "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
wenzelm@22367
   567
chaieb@27669
   568
lemma zgcd_zdvd1 [iff,simp, algebra]: "zgcd i j dvd i"
haftmann@27556
   569
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   570
chaieb@27669
   571
lemma zgcd_zdvd2 [iff,simp, algebra]: "zgcd i j dvd j"
haftmann@27556
   572
  by (simp add: zgcd_def int_dvd_iff)
chaieb@22027
   573
haftmann@27556
   574
lemma zgcd_pos: "zgcd i j \<ge> 0"
haftmann@27556
   575
  by (simp add: zgcd_def)
wenzelm@22367
   576
chaieb@27669
   577
lemma zgcd0 [simp,algebra]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
haftmann@27556
   578
  by (simp add: zgcd_def gcd_zero) arith
chaieb@22027
   579
haftmann@27556
   580
lemma zgcd_commute: "zgcd i j = zgcd j i"
haftmann@27556
   581
  unfolding zgcd_def by (simp add: gcd_commute)
wenzelm@22367
   582
chaieb@27669
   583
lemma zgcd_zminus [simp, algebra]: "zgcd (- i) j = zgcd i j"
haftmann@27556
   584
  unfolding zgcd_def by simp
wenzelm@22367
   585
chaieb@27669
   586
lemma zgcd_zminus2 [simp, algebra]: "zgcd i (- j) = zgcd i j"
haftmann@27556
   587
  unfolding zgcd_def by simp
wenzelm@22367
   588
chaieb@27669
   589
  (* should be solved by algebra*)
haftmann@27556
   590
lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
haftmann@27556
   591
  unfolding zgcd_def
wenzelm@22367
   592
proof -
haftmann@27556
   593
  assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
haftmann@27556
   594
  then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
wenzelm@22367
   595
  from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
chaieb@22027
   596
  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
wenzelm@22367
   597
    unfolding dvd_def
wenzelm@22367
   598
    by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
wenzelm@22367
   599
  from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
chaieb@22027
   600
    unfolding dvd_def by blast
chaieb@22027
   601
  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
huffman@23431
   602
  then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
chaieb@22027
   603
  then show ?thesis
wenzelm@22367
   604
    apply (subst zdvd_abs1 [symmetric])
wenzelm@22367
   605
    apply (subst zdvd_abs2 [symmetric])
chaieb@22027
   606
    apply (unfold dvd_def)
wenzelm@22367
   607
    apply (rule_tac x = "int h'" in exI, simp)
chaieb@22027
   608
    done
chaieb@22027
   609
qed
chaieb@22027
   610
haftmann@27556
   611
lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
wenzelm@22367
   612
haftmann@27556
   613
lemma zgcd_greatest:
wenzelm@22367
   614
  assumes "k dvd m" and "k dvd n"
haftmann@27556
   615
  shows "k dvd zgcd m n"
wenzelm@22367
   616
proof -
chaieb@22027
   617
  let ?k' = "nat \<bar>k\<bar>"
chaieb@22027
   618
  let ?m' = "nat \<bar>m\<bar>"
chaieb@22027
   619
  let ?n' = "nat \<bar>n\<bar>"
wenzelm@22367
   620
  from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
chaieb@22027
   621
    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
haftmann@27556
   622
  from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
haftmann@27556
   623
    unfolding zgcd_def by (simp only: zdvd_int)
haftmann@27556
   624
  then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
haftmann@27556
   625
  then show "k dvd zgcd m n" by (simp add: zdvd_abs1)
chaieb@22027
   626
qed
chaieb@22027
   627
haftmann@27556
   628
lemma div_zgcd_relprime:
wenzelm@22367
   629
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@27556
   630
  shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
wenzelm@22367
   631
proof -
chaieb@25112
   632
  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
haftmann@27556
   633
  let ?g = "zgcd a b"
chaieb@22027
   634
  let ?a' = "a div ?g"
chaieb@22027
   635
  let ?b' = "b div ?g"
haftmann@27556
   636
  let ?g' = "zgcd ?a' ?b'"
chaieb@27568
   637
  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
chaieb@27568
   638
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
wenzelm@22367
   639
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   640
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
chaieb@22027
   641
    unfolding dvd_def by blast
wenzelm@22367
   642
  then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   643
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   644
    by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   645
      zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   646
  have "?g \<noteq> 0" using nz by simp
haftmann@27556
   647
  then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
haftmann@27556
   648
  from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   649
  with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
haftmann@27556
   650
  with zgcd_pos show "?g' = 1" by simp
chaieb@22027
   651
qed
chaieb@22027
   652
chaieb@27669
   653
lemma zgcd_0 [simp, algebra]: "zgcd m 0 = abs m"
chaieb@27568
   654
  by (simp add: zgcd_def abs_if)
chaieb@27568
   655
chaieb@27669
   656
lemma zgcd_0_left [simp, algebra]: "zgcd 0 m = abs m"
chaieb@27568
   657
  by (simp add: zgcd_def abs_if)
chaieb@27568
   658
chaieb@27568
   659
lemma zgcd_non_0: "0 < n ==> zgcd m n = zgcd n (m mod n)"
chaieb@27568
   660
  apply (frule_tac b = n and a = m in pos_mod_sign)
chaieb@27568
   661
  apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
chaieb@27568
   662
  apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
chaieb@27568
   663
  apply (frule_tac a = m in pos_mod_bound)
chaieb@27568
   664
  apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
chaieb@27568
   665
  done
chaieb@27568
   666
chaieb@27568
   667
lemma zgcd_eq: "zgcd m n = zgcd n (m mod n)"
chaieb@27568
   668
  apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
chaieb@27568
   669
  apply (auto simp add: linorder_neq_iff zgcd_non_0)
chaieb@27568
   670
  apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
chaieb@27568
   671
  done
chaieb@27568
   672
chaieb@27669
   673
lemma zgcd_1 [simp, algebra]: "zgcd m 1 = 1"
chaieb@27568
   674
  by (simp add: zgcd_def abs_if)
chaieb@27568
   675
chaieb@27669
   676
lemma zgcd_0_1_iff [simp, algebra]: "zgcd 0 m = 1 \<longleftrightarrow> \<bar>m\<bar> = 1"
chaieb@27568
   677
  by (simp add: zgcd_def abs_if)
chaieb@27568
   678
chaieb@27669
   679
lemma zgcd_greatest_iff[algebra]: "k dvd zgcd m n = (k dvd m \<and> k dvd n)"
chaieb@27568
   680
  by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
chaieb@27568
   681
chaieb@27669
   682
lemma zgcd_1_left [simp, algebra]: "zgcd 1 m = 1"
chaieb@27568
   683
  by (simp add: zgcd_def gcd_1_left)
chaieb@27568
   684
chaieb@27568
   685
lemma zgcd_assoc: "zgcd (zgcd k m) n = zgcd k (zgcd m n)"
chaieb@27568
   686
  by (simp add: zgcd_def gcd_assoc)
chaieb@27568
   687
chaieb@27568
   688
lemma zgcd_left_commute: "zgcd k (zgcd m n) = zgcd m (zgcd k n)"
chaieb@27568
   689
  apply (rule zgcd_commute [THEN trans])
chaieb@27568
   690
  apply (rule zgcd_assoc [THEN trans])
chaieb@27568
   691
  apply (rule zgcd_commute [THEN arg_cong])
chaieb@27568
   692
  done
chaieb@27568
   693
chaieb@27568
   694
lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
chaieb@27568
   695
  -- {* addition is an AC-operator *}
chaieb@27568
   696
chaieb@27568
   697
lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd m n = zgcd (k * m) (k * n)"
chaieb@27568
   698
  by (simp del: minus_mult_right [symmetric]
chaieb@27568
   699
      add: minus_mult_right nat_mult_distrib zgcd_def abs_if
chaieb@27568
   700
          mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
chaieb@27568
   701
chaieb@27568
   702
lemma zgcd_zmult_distrib2_abs: "zgcd (k * m) (k * n) = abs k * zgcd m n"
chaieb@27568
   703
  by (simp add: abs_if zgcd_zmult_distrib2)
chaieb@27568
   704
chaieb@27568
   705
lemma zgcd_self [simp]: "0 \<le> m ==> zgcd m m = m"
chaieb@27568
   706
  by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   707
chaieb@27568
   708
lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd k (k * n) = k"
chaieb@27568
   709
  by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   710
chaieb@27568
   711
lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n) k = k"
chaieb@27568
   712
  by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
chaieb@27568
   713
chaieb@27568
   714
chaieb@27568
   715
definition "zlcm i j = int (lcm(nat(abs i)) (nat(abs j)))"
chaieb@23244
   716
chaieb@27669
   717
lemma dvd_zlcm_self1[simp, algebra]: "i dvd zlcm i j"
haftmann@27556
   718
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   719
chaieb@27669
   720
lemma dvd_zlcm_self2[simp, algebra]: "j dvd zlcm i j"
haftmann@27556
   721
by(simp add:zlcm_def dvd_int_iff)
nipkow@23983
   722
chaieb@23244
   723
haftmann@27556
   724
lemma dvd_imp_dvd_zlcm1:
haftmann@27556
   725
  assumes "k dvd i" shows "k dvd (zlcm i j)"
nipkow@23983
   726
proof -
nipkow@23983
   727
  have "nat(abs k) dvd nat(abs i)" using `k dvd i`
chaieb@23994
   728
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   729
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   730
qed
nipkow@23983
   731
haftmann@27556
   732
lemma dvd_imp_dvd_zlcm2:
haftmann@27556
   733
  assumes "k dvd j" shows "k dvd (zlcm i j)"
nipkow@23983
   734
proof -
nipkow@23983
   735
  have "nat(abs k) dvd nat(abs j)" using `k dvd j`
chaieb@23994
   736
    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
haftmann@27556
   737
  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
nipkow@23983
   738
qed
nipkow@23983
   739
chaieb@23994
   740
chaieb@23244
   741
lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
chaieb@23244
   742
by (case_tac "d <0", simp_all)
chaieb@23244
   743
chaieb@23244
   744
lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
chaieb@23244
   745
by (case_tac "d<0", simp_all)
chaieb@23244
   746
chaieb@23244
   747
(* lcm a b is positive for positive a and b *)
chaieb@23244
   748
chaieb@23244
   749
lemma lcm_pos: 
chaieb@23244
   750
  assumes mpos: "m > 0"
chaieb@27568
   751
  and npos: "n>0"
haftmann@27556
   752
  shows "lcm m n > 0"
chaieb@23244
   753
proof(rule ccontr, simp add: lcm_def gcd_zero)
chaieb@27568
   754
assume h:"m*n div gcd m n = 0"
haftmann@27556
   755
from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
haftmann@27556
   756
hence gcdp: "gcd m n > 0" by simp
chaieb@23244
   757
with h
haftmann@27556
   758
have "m*n < gcd m n"
haftmann@27556
   759
  by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
chaieb@23244
   760
moreover 
haftmann@27556
   761
have "gcd m n dvd m" by simp
haftmann@27556
   762
 with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
chaieb@27568
   763
 with npos have t1:"gcd m n *n \<le> m*n" by simp
haftmann@27556
   764
 have "gcd m n \<le> gcd m n*n" using npos by simp
haftmann@27556
   765
 with t1 have "gcd m n \<le> m*n" by arith
chaieb@23244
   766
ultimately show "False" by simp
chaieb@23244
   767
qed
chaieb@23244
   768
haftmann@27556
   769
lemma zlcm_pos: 
nipkow@23983
   770
  assumes anz: "a \<noteq> 0"
nipkow@23983
   771
  and bnz: "b \<noteq> 0" 
haftmann@27556
   772
  shows "0 < zlcm a b"
chaieb@23244
   773
proof-
chaieb@23244
   774
  let ?na = "nat (abs a)"
chaieb@23244
   775
  let ?nb = "nat (abs b)"
nipkow@23983
   776
  have nap: "?na >0" using anz by simp
nipkow@23983
   777
  have nbp: "?nb >0" using bnz by simp
haftmann@27556
   778
  have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
haftmann@27556
   779
  thus ?thesis by (simp add: zlcm_def)
chaieb@23244
   780
qed
chaieb@23244
   781
haftmann@27651
   782
lemma zgcd_code [code func]:
haftmann@27651
   783
  "zgcd k l = \<bar>if l = 0 then k else zgcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
haftmann@27651
   784
  by (simp add: zgcd_def gcd.simps [of "nat \<bar>k\<bar>"] nat_mod_distrib)
haftmann@27651
   785
wenzelm@21256
   786
end