src/HOL/Library/GCD.thy
author chaieb
Sat Jan 06 20:56:44 2007 +0100 (2007-01-06)
changeset 22027 e4a08629c4bd
parent 21404 eb85850d3eb7
child 22367 6860f09242bf
permissions -rw-r--r--
A few lemmas about relative primes when dividing trough gcd
Definition of gcd on integers and a few 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
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@21256
   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@21256
   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
  (* Division by gcd yields rrelatively primes *)
chaieb@22027
   199
chaieb@22027
   200
chaieb@22027
   201
lemma div_gcd_relprime:
chaieb@22027
   202
  assumes nz:"a\<noteq>0 \<or> b\<noteq>0"
chaieb@22027
   203
  shows "gcd (a div gcd(a,b), b div gcd(a,b)) = 1"
chaieb@22027
   204
proof-
chaieb@22027
   205
  let ?g = "gcd (a,b)"
chaieb@22027
   206
  let ?a' = "a div ?g"
chaieb@22027
   207
  let ?b' = "b div ?g"
chaieb@22027
   208
  let ?g' = "gcd (?a', ?b')"
chaieb@22027
   209
  have dvdg: "?g dvd a" "?g dvd b" by simp_all
chaieb@22027
   210
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
chaieb@22027
   211
  from dvdg dvdg' obtain ka kb ka' kb' where 
chaieb@22027
   212
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'" 
chaieb@22027
   213
    unfolding dvd_def by blast
chaieb@22027
   214
  hence	"?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
chaieb@22027
   215
  hence dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
chaieb@22027
   216
    by (auto simp add: dvd_mult_div_cancel[OF dvdg(1)] 
chaieb@22027
   217
      dvd_mult_div_cancel[OF dvdg(2)] dvd_def)
chaieb@22027
   218
  have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
chaieb@22027
   219
  hence gp: "?g > 0" by simp 
chaieb@22027
   220
  from gcd_greatest[OF dvdgg'] have "?g * ?g' dvd ?g" .
chaieb@22027
   221
  with dvd_mult_cancel1[OF gp] show "?g' = 1" by simp
chaieb@22027
   222
qed
chaieb@22027
   223
chaieb@22027
   224
  (* gcd on integers *)
chaieb@22027
   225
constdefs igcd:: "int \<Rightarrow> int \<Rightarrow> int"
chaieb@22027
   226
  "igcd i j \<equiv> int (gcd (nat (abs i),nat (abs j)))"
chaieb@22027
   227
lemma igcd_dvd1[simp]:"igcd i j dvd i"
chaieb@22027
   228
  by (simp add: igcd_def int_dvd_iff)
chaieb@22027
   229
chaieb@22027
   230
lemma igcd_dvd2[simp]:"igcd i j dvd j"
chaieb@22027
   231
by (simp add: igcd_def int_dvd_iff)
chaieb@22027
   232
chaieb@22027
   233
lemma igcd_pos: "igcd i j \<ge> 0"
chaieb@22027
   234
by (simp add: igcd_def)
chaieb@22027
   235
lemma igcd0[simp]: "(igcd i j = 0) = (i = 0 \<and> j = 0)"
chaieb@22027
   236
by (simp add: igcd_def gcd_zero) arith
chaieb@22027
   237
chaieb@22027
   238
lemma igcd_commute: "igcd i j = igcd j i"
chaieb@22027
   239
  unfolding igcd_def by (simp add: gcd_commute)
chaieb@22027
   240
lemma igcd_neg1[simp]: "igcd (- i) j = igcd i j"
chaieb@22027
   241
  unfolding igcd_def by simp
chaieb@22027
   242
lemma igcd_neg2[simp]: "igcd i (- j) = igcd i j"
chaieb@22027
   243
  unfolding igcd_def by simp
chaieb@22027
   244
lemma zrelprime_dvd_mult: "igcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
chaieb@22027
   245
  unfolding igcd_def
chaieb@22027
   246
proof-
chaieb@22027
   247
  assume H: "int (gcd (nat \<bar>i\<bar>, nat \<bar>j\<bar>)) = 1" "i dvd k * j"
chaieb@22027
   248
  hence g: "gcd (nat \<bar>i\<bar>, nat \<bar>j\<bar>) = 1" by simp
chaieb@22027
   249
  from H(2) obtain h where h:"k*j = i*h" unfolding dvd_def by blast  
chaieb@22027
   250
  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
chaieb@22027
   251
    unfolding dvd_def 
chaieb@22027
   252
    by (rule_tac x= "nat \<bar>h\<bar>" in exI,simp add: h nat_abs_mult_distrib[symmetric])
chaieb@22027
   253
  from relprime_dvd_mult[OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'" 
chaieb@22027
   254
    unfolding dvd_def by blast
chaieb@22027
   255
  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
chaieb@22027
   256
  hence "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
chaieb@22027
   257
  then show ?thesis
chaieb@22027
   258
    apply (subst zdvd_abs1[symmetric])
chaieb@22027
   259
    apply (subst zdvd_abs2[symmetric])
chaieb@22027
   260
    apply (unfold dvd_def)
chaieb@22027
   261
    apply (rule_tac x="int h'" in exI, simp)
chaieb@22027
   262
    done
chaieb@22027
   263
qed
chaieb@22027
   264
chaieb@22027
   265
lemma int_nat_abs: "int (nat (abs x)) = abs x"  by arith
chaieb@22027
   266
lemma igcd_greatest: assumes km:"k dvd m" and kn:"k dvd n"  shows "k dvd igcd m n"
chaieb@22027
   267
proof-
chaieb@22027
   268
  let ?k' = "nat \<bar>k\<bar>"
chaieb@22027
   269
  let ?m' = "nat \<bar>m\<bar>"
chaieb@22027
   270
  let ?n' = "nat \<bar>n\<bar>"
chaieb@22027
   271
  from km kn have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
chaieb@22027
   272
    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
chaieb@22027
   273
  from gcd_greatest[OF dvd'] have "int (nat \<bar>k\<bar>) dvd igcd m n"
chaieb@22027
   274
    unfolding igcd_def by (simp only: zdvd_int)
chaieb@22027
   275
  hence "\<bar>k\<bar> dvd igcd m n" by (simp only: int_nat_abs)
chaieb@22027
   276
  thus "k dvd igcd m n" by (simp add: zdvd_abs1)
chaieb@22027
   277
qed
chaieb@22027
   278
chaieb@22027
   279
lemma div_igcd_relprime:
chaieb@22027
   280
  assumes nz:"a\<noteq>0 \<or> b\<noteq>0"
chaieb@22027
   281
  shows "igcd (a div (igcd a b)) (b div (igcd a b)) = 1"
chaieb@22027
   282
proof-
chaieb@22027
   283
  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by simp
chaieb@22027
   284
  have th1: "(1::int) = int 1" by simp
chaieb@22027
   285
  let ?g = "igcd a b"
chaieb@22027
   286
  let ?a' = "a div ?g"
chaieb@22027
   287
  let ?b' = "b div ?g"
chaieb@22027
   288
  let ?g' = "igcd ?a' ?b'"
chaieb@22027
   289
  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: igcd_dvd1 igcd_dvd2)
chaieb@22027
   290
  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: igcd_dvd1 igcd_dvd2)
chaieb@22027
   291
  from dvdg dvdg' obtain ka kb ka' kb' where 
chaieb@22027
   292
   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'" 
chaieb@22027
   293
    unfolding dvd_def by blast
chaieb@22027
   294
  hence	"?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
chaieb@22027
   295
  hence dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
chaieb@22027
   296
    by (auto simp add: zdvd_mult_div_cancel[OF dvdg(1)] 
chaieb@22027
   297
      zdvd_mult_div_cancel[OF dvdg(2)] dvd_def)
chaieb@22027
   298
  have "?g \<noteq> 0" using nz by simp
chaieb@22027
   299
  hence gp: "?g \<noteq> 0" using igcd_pos[where i="a" and j="b"] by arith
chaieb@22027
   300
  from igcd_greatest[OF dvdgg'] have "?g * ?g' dvd ?g" .
chaieb@22027
   301
  with zdvd_mult_cancel1[OF gp] have "\<bar>?g'\<bar> = 1" by simp 
chaieb@22027
   302
  with igcd_pos show "?g' = 1" by simp
chaieb@22027
   303
qed
chaieb@22027
   304
wenzelm@21256
   305
end