src/HOL/Library/GCD.thy
author wenzelm
Tue Feb 27 00:33:49 2007 +0100 (2007-02-27)
changeset 22367 6860f09242bf
parent 22027 e4a08629c4bd
child 23244 1630951f0512
permissions -rw-r--r--
tuned document;
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
wenzelm@21256
    10
imports Main
wenzelm@21256
    11
begin
wenzelm@21256
    12
wenzelm@21256
    13
text {*
wenzelm@21256
    14
  See \cite{davenport92}.
wenzelm@21256
    15
  \bigskip
wenzelm@21256
    16
*}
wenzelm@21256
    17
wenzelm@21256
    18
consts
wenzelm@21256
    19
  gcd  :: "nat \<times> nat => nat"  -- {* Euclid's algorithm *}
wenzelm@21256
    20
wenzelm@21256
    21
recdef gcd  "measure ((\<lambda>(m, n). n) :: nat \<times> nat => nat)"
wenzelm@21256
    22
  "gcd (m, n) = (if n = 0 then m else gcd (n, m mod n))"
wenzelm@21256
    23
wenzelm@21263
    24
definition
wenzelm@21404
    25
  is_gcd :: "nat => nat => nat => bool" where -- {* @{term gcd} as a relation *}
wenzelm@21263
    26
  "is_gcd p m n = (p dvd m \<and> p dvd n \<and>
wenzelm@21263
    27
    (\<forall>d. d dvd m \<and> d dvd n --> d dvd p))"
wenzelm@21256
    28
wenzelm@21256
    29
wenzelm@21256
    30
lemma gcd_induct:
wenzelm@21256
    31
  "(!!m. P m 0) ==>
wenzelm@21256
    32
    (!!m n. 0 < n ==> P n (m mod n) ==> P m n)
wenzelm@21256
    33
  ==> P (m::nat) (n::nat)"
wenzelm@21256
    34
  apply (induct m n rule: gcd.induct)
wenzelm@21256
    35
  apply (case_tac "n = 0")
wenzelm@21256
    36
   apply simp_all
wenzelm@21256
    37
  done
wenzelm@21256
    38
wenzelm@21256
    39
wenzelm@21256
    40
lemma gcd_0 [simp]: "gcd (m, 0) = m"
wenzelm@21263
    41
  by simp
wenzelm@21256
    42
wenzelm@21256
    43
lemma gcd_non_0: "0 < n ==> gcd (m, n) = gcd (n, m mod n)"
wenzelm@21263
    44
  by simp
wenzelm@21256
    45
wenzelm@21256
    46
declare gcd.simps [simp del]
wenzelm@21256
    47
wenzelm@21256
    48
lemma gcd_1 [simp]: "gcd (m, Suc 0) = 1"
wenzelm@21263
    49
  by (simp add: gcd_non_0)
wenzelm@21256
    50
wenzelm@21256
    51
text {*
wenzelm@21256
    52
  \medskip @{term "gcd (m, n)"} divides @{text m} and @{text n}.  The
wenzelm@21256
    53
  conjunctions don't seem provable separately.
wenzelm@21256
    54
*}
wenzelm@21256
    55
wenzelm@21256
    56
lemma gcd_dvd1 [iff]: "gcd (m, n) dvd m"
wenzelm@21256
    57
  and gcd_dvd2 [iff]: "gcd (m, n) dvd n"
wenzelm@21256
    58
  apply (induct m n rule: gcd_induct)
wenzelm@21263
    59
     apply (simp_all add: gcd_non_0)
wenzelm@21256
    60
  apply (blast dest: dvd_mod_imp_dvd)
wenzelm@21256
    61
  done
wenzelm@21256
    62
wenzelm@21256
    63
text {*
wenzelm@21256
    64
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
wenzelm@21256
    65
  naturals, if @{term k} divides @{term m} and @{term k} divides
wenzelm@21256
    66
  @{term n} then @{term k} divides @{term "gcd (m, n)"}.
wenzelm@21256
    67
*}
wenzelm@21256
    68
wenzelm@21256
    69
lemma gcd_greatest: "k dvd m ==> k dvd n ==> k dvd gcd (m, n)"
wenzelm@21263
    70
  by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
wenzelm@21256
    71
wenzelm@21256
    72
lemma gcd_greatest_iff [iff]: "(k dvd gcd (m, n)) = (k dvd m \<and> k dvd n)"
wenzelm@21263
    73
  by (blast intro!: gcd_greatest intro: dvd_trans)
wenzelm@21256
    74
wenzelm@21256
    75
lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)"
wenzelm@21263
    76
  by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
wenzelm@21256
    77
wenzelm@21256
    78
wenzelm@21256
    79
text {*
wenzelm@21256
    80
  \medskip Function gcd yields the Greatest Common Divisor.
wenzelm@21256
    81
*}
wenzelm@21256
    82
wenzelm@21256
    83
lemma is_gcd: "is_gcd (gcd (m, n)) m n"
wenzelm@21256
    84
  apply (simp add: is_gcd_def gcd_greatest)
wenzelm@21256
    85
  done
wenzelm@21256
    86
wenzelm@21256
    87
text {*
wenzelm@21256
    88
  \medskip Uniqueness of GCDs.
wenzelm@21256
    89
*}
wenzelm@21256
    90
wenzelm@21256
    91
lemma is_gcd_unique: "is_gcd m a b ==> is_gcd n a b ==> m = n"
wenzelm@21256
    92
  apply (simp add: is_gcd_def)
wenzelm@21256
    93
  apply (blast intro: dvd_anti_sym)
wenzelm@21256
    94
  done
wenzelm@21256
    95
wenzelm@21256
    96
lemma is_gcd_dvd: "is_gcd m a b ==> k dvd a ==> k dvd b ==> k dvd m"
wenzelm@21256
    97
  apply (auto simp add: is_gcd_def)
wenzelm@21256
    98
  done
wenzelm@21256
    99
wenzelm@21256
   100
wenzelm@21256
   101
text {*
wenzelm@21256
   102
  \medskip Commutativity
wenzelm@21256
   103
*}
wenzelm@21256
   104
wenzelm@21256
   105
lemma is_gcd_commute: "is_gcd k m n = is_gcd k n m"
wenzelm@21256
   106
  apply (auto simp add: is_gcd_def)
wenzelm@21256
   107
  done
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_0_left [simp]: "gcd (0, m) = m"
wenzelm@21256
   124
  apply (simp add: gcd_commute [of 0])
wenzelm@21256
   125
  done
wenzelm@21256
   126
wenzelm@21256
   127
lemma gcd_1_left [simp]: "gcd (Suc 0, m) = 1"
wenzelm@21256
   128
  apply (simp add: gcd_commute [of "Suc 0"])
wenzelm@21256
   129
  done
wenzelm@21256
   130
wenzelm@21256
   131
wenzelm@21256
   132
text {*
wenzelm@21256
   133
  \medskip Multiplication laws
wenzelm@21256
   134
*}
wenzelm@21256
   135
wenzelm@21256
   136
lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)"
wenzelm@21256
   137
    -- {* \cite[page 27]{davenport92} *}
wenzelm@21256
   138
  apply (induct m n rule: gcd_induct)
wenzelm@21256
   139
   apply simp
wenzelm@21256
   140
  apply (case_tac "k = 0")
wenzelm@21256
   141
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
wenzelm@21256
   142
  done
wenzelm@21256
   143
wenzelm@21256
   144
lemma gcd_mult [simp]: "gcd (k, k * n) = k"
wenzelm@21256
   145
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
wenzelm@21256
   146
  done
wenzelm@21256
   147
wenzelm@21256
   148
lemma gcd_self [simp]: "gcd (k, k) = k"
wenzelm@21256
   149
  apply (rule gcd_mult [of k 1, simplified])
wenzelm@21256
   150
  done
wenzelm@21256
   151
wenzelm@21256
   152
lemma relprime_dvd_mult: "gcd (k, n) = 1 ==> k dvd m * n ==> k dvd m"
wenzelm@21256
   153
  apply (insert gcd_mult_distrib2 [of m k n])
wenzelm@21256
   154
  apply simp
wenzelm@21256
   155
  apply (erule_tac t = m in ssubst)
wenzelm@21256
   156
  apply simp
wenzelm@21256
   157
  done
wenzelm@21256
   158
wenzelm@21256
   159
lemma relprime_dvd_mult_iff: "gcd (k, n) = 1 ==> (k dvd m * n) = (k dvd m)"
wenzelm@21256
   160
  apply (blast intro: relprime_dvd_mult dvd_trans)
wenzelm@21256
   161
  done
wenzelm@21256
   162
wenzelm@21256
   163
lemma gcd_mult_cancel: "gcd (k, n) = 1 ==> gcd (k * m, n) = gcd (m, n)"
wenzelm@21256
   164
  apply (rule dvd_anti_sym)
wenzelm@21256
   165
   apply (rule gcd_greatest)
wenzelm@21256
   166
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   167
     apply (simp add: gcd_assoc)
wenzelm@21256
   168
     apply (simp add: gcd_commute)
wenzelm@21256
   169
    apply (simp_all add: mult_commute)
wenzelm@21256
   170
  apply (blast intro: dvd_trans)
wenzelm@21256
   171
  done
wenzelm@21256
   172
wenzelm@21256
   173
wenzelm@21256
   174
text {* \medskip Addition laws *}
wenzelm@21256
   175
wenzelm@21256
   176
lemma gcd_add1 [simp]: "gcd (m + n, n) = gcd (m, n)"
wenzelm@21256
   177
  apply (case_tac "n = 0")
wenzelm@21256
   178
   apply (simp_all add: gcd_non_0)
wenzelm@21256
   179
  done
wenzelm@21256
   180
wenzelm@21256
   181
lemma gcd_add2 [simp]: "gcd (m, m + n) = gcd (m, n)"
wenzelm@21256
   182
proof -
wenzelm@22367
   183
  have "gcd (m, m + n) = gcd (m + n, m)" by (rule gcd_commute)
wenzelm@21256
   184
  also have "... = gcd (n + m, m)" by (simp add: add_commute)
wenzelm@21256
   185
  also have "... = gcd (n, m)" by simp
wenzelm@22367
   186
  also have  "... = gcd (m, n)" by (rule gcd_commute)
wenzelm@21256
   187
  finally show ?thesis .
wenzelm@21256
   188
qed
wenzelm@21256
   189
wenzelm@21256
   190
lemma gcd_add2' [simp]: "gcd (m, n + m) = gcd (m, n)"
wenzelm@21256
   191
  apply (subst add_commute)
wenzelm@21256
   192
  apply (rule gcd_add2)
wenzelm@21256
   193
  done
wenzelm@21256
   194
wenzelm@21256
   195
lemma gcd_add_mult: "gcd (m, k * m + n) = gcd (m, n)"
wenzelm@21263
   196
  by (induct k) (simp_all add: add_assoc)
wenzelm@21256
   197
chaieb@22027
   198
wenzelm@22367
   199
text {*
wenzelm@22367
   200
  \medskip Division by gcd yields rrelatively primes.
wenzelm@22367
   201
*}
chaieb@22027
   202
chaieb@22027
   203
lemma div_gcd_relprime:
wenzelm@22367
   204
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
chaieb@22027
   205
  shows "gcd (a div gcd(a,b), b div gcd(a,b)) = 1"
wenzelm@22367
   206
proof -
wenzelm@22367
   207
  let ?g = "gcd (a, b)"
chaieb@22027
   208
  let ?a' = "a div ?g"
chaieb@22027
   209
  let ?b' = "b div ?g"
chaieb@22027
   210
  let ?g' = "gcd (?a', ?b')"
chaieb@22027
   211
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
   212
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
wenzelm@22367
   213
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   214
      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
chaieb@22027
   215
    unfolding dvd_def by blast
wenzelm@22367
   216
  then have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   217
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   218
    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   219
      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   220
  have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
wenzelm@22367
   221
  then have gp: "?g > 0" by simp
wenzelm@22367
   222
  from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   223
  with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
chaieb@22027
   224
qed
chaieb@22027
   225
wenzelm@22367
   226
wenzelm@22367
   227
text {*
wenzelm@22367
   228
  \medskip Gcd on integers.
wenzelm@22367
   229
*}
wenzelm@22367
   230
wenzelm@22367
   231
definition
wenzelm@22367
   232
  igcd :: "int \<Rightarrow> int \<Rightarrow> int" where
wenzelm@22367
   233
  "igcd i j = int (gcd (nat (abs i), nat (abs j)))"
wenzelm@22367
   234
wenzelm@22367
   235
lemma igcd_dvd1 [simp]: "igcd i j dvd i"
chaieb@22027
   236
  by (simp add: igcd_def int_dvd_iff)
chaieb@22027
   237
wenzelm@22367
   238
lemma igcd_dvd2 [simp]: "igcd i j dvd j"
wenzelm@22367
   239
  by (simp add: igcd_def int_dvd_iff)
chaieb@22027
   240
chaieb@22027
   241
lemma igcd_pos: "igcd i j \<ge> 0"
wenzelm@22367
   242
  by (simp add: igcd_def)
wenzelm@22367
   243
wenzelm@22367
   244
lemma igcd0 [simp]: "(igcd i j = 0) = (i = 0 \<and> j = 0)"
wenzelm@22367
   245
  by (simp add: igcd_def gcd_zero) arith
chaieb@22027
   246
chaieb@22027
   247
lemma igcd_commute: "igcd i j = igcd j i"
chaieb@22027
   248
  unfolding igcd_def by (simp add: gcd_commute)
wenzelm@22367
   249
wenzelm@22367
   250
lemma igcd_neg1 [simp]: "igcd (- i) j = igcd i j"
chaieb@22027
   251
  unfolding igcd_def by simp
wenzelm@22367
   252
wenzelm@22367
   253
lemma igcd_neg2 [simp]: "igcd i (- j) = igcd i j"
chaieb@22027
   254
  unfolding igcd_def by simp
wenzelm@22367
   255
chaieb@22027
   256
lemma zrelprime_dvd_mult: "igcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
chaieb@22027
   257
  unfolding igcd_def
wenzelm@22367
   258
proof -
wenzelm@22367
   259
  assume "int (gcd (nat \<bar>i\<bar>, nat \<bar>j\<bar>)) = 1" "i dvd k * j"
wenzelm@22367
   260
  then have g: "gcd (nat \<bar>i\<bar>, nat \<bar>j\<bar>) = 1" by simp
wenzelm@22367
   261
  from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
chaieb@22027
   262
  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
wenzelm@22367
   263
    unfolding dvd_def
wenzelm@22367
   264
    by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
wenzelm@22367
   265
  from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
chaieb@22027
   266
    unfolding dvd_def by blast
chaieb@22027
   267
  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
wenzelm@22367
   268
  then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
chaieb@22027
   269
  then show ?thesis
wenzelm@22367
   270
    apply (subst zdvd_abs1 [symmetric])
wenzelm@22367
   271
    apply (subst zdvd_abs2 [symmetric])
chaieb@22027
   272
    apply (unfold dvd_def)
wenzelm@22367
   273
    apply (rule_tac x = "int h'" in exI, simp)
chaieb@22027
   274
    done
chaieb@22027
   275
qed
chaieb@22027
   276
chaieb@22027
   277
lemma int_nat_abs: "int (nat (abs x)) = abs x"  by arith
wenzelm@22367
   278
wenzelm@22367
   279
lemma igcd_greatest:
wenzelm@22367
   280
  assumes "k dvd m" and "k dvd n"
wenzelm@22367
   281
  shows "k dvd igcd m n"
wenzelm@22367
   282
proof -
chaieb@22027
   283
  let ?k' = "nat \<bar>k\<bar>"
chaieb@22027
   284
  let ?m' = "nat \<bar>m\<bar>"
chaieb@22027
   285
  let ?n' = "nat \<bar>n\<bar>"
wenzelm@22367
   286
  from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
chaieb@22027
   287
    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
wenzelm@22367
   288
  from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd igcd m n"
chaieb@22027
   289
    unfolding igcd_def by (simp only: zdvd_int)
wenzelm@22367
   290
  then have "\<bar>k\<bar> dvd igcd m n" by (simp only: int_nat_abs)
wenzelm@22367
   291
  then show "k dvd igcd m n" by (simp add: zdvd_abs1)
chaieb@22027
   292
qed
chaieb@22027
   293
chaieb@22027
   294
lemma div_igcd_relprime:
wenzelm@22367
   295
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
chaieb@22027
   296
  shows "igcd (a div (igcd a b)) (b div (igcd a b)) = 1"
wenzelm@22367
   297
proof -
chaieb@22027
   298
  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by simp
chaieb@22027
   299
  let ?g = "igcd a b"
chaieb@22027
   300
  let ?a' = "a div ?g"
chaieb@22027
   301
  let ?b' = "b div ?g"
chaieb@22027
   302
  let ?g' = "igcd ?a' ?b'"
chaieb@22027
   303
  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: igcd_dvd1 igcd_dvd2)
chaieb@22027
   304
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: igcd_dvd1 igcd_dvd2)
wenzelm@22367
   305
  from dvdg dvdg' obtain ka kb ka' kb' where
wenzelm@22367
   306
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
chaieb@22027
   307
    unfolding dvd_def by blast
wenzelm@22367
   308
  then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
wenzelm@22367
   309
  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
wenzelm@22367
   310
    by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
wenzelm@22367
   311
      zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
chaieb@22027
   312
  have "?g \<noteq> 0" using nz by simp
wenzelm@22367
   313
  then have gp: "?g \<noteq> 0" using igcd_pos[where i="a" and j="b"] by arith
wenzelm@22367
   314
  from igcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
wenzelm@22367
   315
  with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
chaieb@22027
   316
  with igcd_pos show "?g' = 1" by simp
chaieb@22027
   317
qed
chaieb@22027
   318
wenzelm@21256
   319
end