src/HOL/Library/Primes.thy
author haftmann
Fri Jul 18 18:25:53 2008 +0200 (2008-07-18)
changeset 27651 16a26996c30e
parent 27567 e3fe9a327c63
child 27670 3b5425dead98
permissions -rw-r--r--
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
wenzelm@11368
     1
(*  Title:      HOL/Library/Primes.thy
paulson@11363
     2
    ID:         $Id$
haftmann@27106
     3
    Author:     Amine Chaieb, Christophe Tabacznyj and Lawrence C Paulson
paulson@11363
     4
    Copyright   1996  University of Cambridge
paulson@11363
     5
*)
paulson@11363
     6
nipkow@16762
     7
header {* Primality on nat *}
paulson@11363
     8
nipkow@15131
     9
theory Primes
haftmann@27487
    10
imports Plain "~~/src/HOL/ATP_Linkup" GCD Parity
nipkow@15131
    11
begin
paulson@11363
    12
wenzelm@19086
    13
definition
wenzelm@21404
    14
  coprime :: "nat => nat => bool" where
chaieb@27567
    15
  "coprime m n \<longleftrightarrow> gcd m n = 1"
paulson@11363
    16
wenzelm@21404
    17
definition
wenzelm@21404
    18
  prime :: "nat \<Rightarrow> bool" where
haftmann@27106
    19
  [code func del]: "prime p \<longleftrightarrow> (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
paulson@11363
    20
paulson@11363
    21
nipkow@16762
    22
lemma two_is_prime: "prime 2"
nipkow@16762
    23
  apply (auto simp add: prime_def)
nipkow@16762
    24
  apply (case_tac m)
nipkow@16762
    25
   apply (auto dest!: dvd_imp_le)
paulson@11363
    26
  done
paulson@11363
    27
haftmann@27556
    28
lemma prime_imp_relprime: "prime p ==> \<not> p dvd n ==> gcd p n = 1"
paulson@11363
    29
  apply (auto simp add: prime_def)
paulson@23839
    30
  apply (metis One_nat_def gcd_dvd1 gcd_dvd2)
paulson@11363
    31
  done
paulson@11363
    32
paulson@11363
    33
text {*
paulson@11363
    34
  This theorem leads immediately to a proof of the uniqueness of
paulson@11363
    35
  factorization.  If @{term p} divides a product of primes then it is
paulson@11363
    36
  one of those primes.
paulson@11363
    37
*}
paulson@11363
    38
nipkow@16663
    39
lemma prime_dvd_mult: "prime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
wenzelm@12739
    40
  by (blast intro: relprime_dvd_mult prime_imp_relprime)
paulson@11363
    41
nipkow@16663
    42
lemma prime_dvd_square: "prime p ==> p dvd m^Suc (Suc 0) ==> p dvd m"
wenzelm@12739
    43
  by (auto dest: prime_dvd_mult)
wenzelm@12739
    44
nipkow@16663
    45
lemma prime_dvd_power_two: "prime p ==> p dvd m\<twosuperior> ==> p dvd m"
paulson@14353
    46
  by (rule prime_dvd_square) (simp_all add: power2_eq_square)
wenzelm@11368
    47
chaieb@26125
    48
chaieb@26125
    49
lemma exp_eq_1:"(x::nat)^n = 1 \<longleftrightarrow> x = 1 \<or> n = 0" by (induct n, auto)
chaieb@26125
    50
lemma exp_mono_lt: "(x::nat) ^ (Suc n) < y ^ (Suc n) \<longleftrightarrow> x < y"
chaieb@26125
    51
  using power_less_imp_less_base[of x "Suc n" y] power_strict_mono[of x y "Suc n"]
chaieb@26125
    52
    by auto
chaieb@26125
    53
lemma exp_mono_le: "(x::nat) ^ (Suc n) \<le> y ^ (Suc n) \<longleftrightarrow> x \<le> y"
chaieb@26125
    54
  by (simp only: linorder_not_less[symmetric] exp_mono_lt)
chaieb@26125
    55
chaieb@26125
    56
lemma exp_mono_eq: "(x::nat) ^ Suc n = y ^ Suc n \<longleftrightarrow> x = y"
chaieb@26125
    57
using power_inject_base[of x n y] by auto
chaieb@26125
    58
chaieb@26125
    59
chaieb@26125
    60
lemma even_square: assumes e: "even (n::nat)" shows "\<exists>x. n ^ 2 = 4*x"
chaieb@26125
    61
proof-
chaieb@26125
    62
  from e have "2 dvd n" by presburger
chaieb@26125
    63
  then obtain k where k: "n = 2*k" using dvd_def by auto
chaieb@26125
    64
  hence "n^2 = 4* (k^2)" by (simp add: power2_eq_square)
chaieb@26125
    65
  thus ?thesis by blast
chaieb@26125
    66
qed
chaieb@26125
    67
chaieb@26125
    68
lemma odd_square: assumes e: "odd (n::nat)" shows "\<exists>x. n ^ 2 = 4*x + 1"
chaieb@26125
    69
proof-
chaieb@26125
    70
  from e have np: "n > 0" by presburger
chaieb@26125
    71
  from e have "2 dvd (n - 1)" by presburger
chaieb@26125
    72
  then obtain k where "n - 1 = 2*k" using dvd_def by auto
chaieb@26125
    73
  hence k: "n = 2*k + 1"  using e by presburger 
chaieb@26125
    74
  hence "n^2 = 4* (k^2 + k) + 1" by algebra   
chaieb@26125
    75
  thus ?thesis by blast
chaieb@26125
    76
qed
chaieb@26125
    77
chaieb@26125
    78
lemma diff_square: "(x::nat)^2 - y^2 = (x+y)*(x - y)" 
chaieb@26125
    79
proof-
chaieb@26125
    80
  have "x \<le> y \<or> y \<le> x" by (rule nat_le_linear)
chaieb@26125
    81
  moreover
chaieb@26125
    82
  {assume le: "x \<le> y"
chaieb@26125
    83
    hence "x ^2 \<le> y^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
chaieb@26125
    84
    with le have ?thesis by simp }
chaieb@26125
    85
  moreover
chaieb@26125
    86
  {assume le: "y \<le> x"
chaieb@26125
    87
    hence le2: "y ^2 \<le> x^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
chaieb@26125
    88
    from le have "\<exists>z. y + z = x" by presburger
chaieb@26125
    89
    then obtain z where z: "x = y + z" by blast 
chaieb@26125
    90
    from le2 have "\<exists>z. x^2 = y^2 + z" by presburger
chaieb@26125
    91
    then obtain z2 where z2: "x^2 = y^2 + z2"  by blast
chaieb@26125
    92
    from z z2 have ?thesis apply simp by algebra }
chaieb@26125
    93
  ultimately show ?thesis by blast  
chaieb@26125
    94
qed
chaieb@26125
    95
wenzelm@26144
    96
text {* Elementary theory of divisibility *}
chaieb@26125
    97
lemma divides_ge: "(a::nat) dvd b \<Longrightarrow> b = 0 \<or> a \<le> b" unfolding dvd_def by auto
chaieb@26125
    98
lemma divides_antisym: "(x::nat) dvd y \<and> y dvd x \<longleftrightarrow> x = y"
chaieb@26125
    99
  using dvd_anti_sym[of x y] by auto
chaieb@26125
   100
chaieb@26125
   101
lemma divides_add_revr: assumes da: "(d::nat) dvd a" and dab:"d dvd (a + b)"
chaieb@26125
   102
  shows "d dvd b"
chaieb@26125
   103
proof-
chaieb@26125
   104
  from da obtain k where k:"a = d*k" by (auto simp add: dvd_def)
chaieb@26125
   105
  from dab obtain k' where k': "a + b = d*k'" by (auto simp add: dvd_def)
chaieb@26125
   106
  from k k' have "b = d *(k' - k)" by (simp add : diff_mult_distrib2)
chaieb@26125
   107
  thus ?thesis unfolding dvd_def by blast
chaieb@26125
   108
qed
chaieb@26125
   109
chaieb@26125
   110
declare nat_mult_dvd_cancel_disj[presburger]
chaieb@26125
   111
lemma nat_mult_dvd_cancel_disj'[presburger]: 
chaieb@26125
   112
  "(m\<Colon>nat)*k dvd n*k \<longleftrightarrow> k = 0 \<or> m dvd n" unfolding mult_commute[of m k] mult_commute[of n k] by presburger 
chaieb@26125
   113
chaieb@26125
   114
lemma divides_mul_l: "(a::nat) dvd b ==> (c * a) dvd (c * b)"
chaieb@26125
   115
  by presburger
chaieb@26125
   116
chaieb@26125
   117
lemma divides_mul_r: "(a::nat) dvd b ==> (a * c) dvd (b * c)" by presburger
chaieb@26125
   118
lemma divides_cases: "(n::nat) dvd m ==> m = 0 \<or> m = n \<or> 2 * n <= m" 
chaieb@26125
   119
  by (auto simp add: dvd_def)
chaieb@26125
   120
lemma divides_le: "m dvd n ==> m <= n \<or> n = (0::nat)" by (auto simp add: dvd_def)
chaieb@26125
   121
chaieb@26125
   122
lemma divides_div_not: "(x::nat) = (q * n) + r \<Longrightarrow> 0 < r \<Longrightarrow> r < n ==> ~(n dvd x)"
chaieb@26125
   123
proof(auto simp add: dvd_def)
chaieb@26125
   124
  fix k assume H: "0 < r" "r < n" "q * n + r = n * k"
chaieb@26125
   125
  from H(3) have r: "r = n* (k -q)" by(simp add: diff_mult_distrib2 mult_commute)
chaieb@26125
   126
  {assume "k - q = 0" with r H(1) have False by simp}
chaieb@26125
   127
  moreover
chaieb@26125
   128
  {assume "k - q \<noteq> 0" with r have "r \<ge> n" by auto
chaieb@26125
   129
    with H(2) have False by simp}
chaieb@26125
   130
  ultimately show False by blast
chaieb@26125
   131
qed
chaieb@26125
   132
lemma divides_exp: "(x::nat) dvd y ==> x ^ n dvd y ^ n"
chaieb@26125
   133
  by (auto simp add: power_mult_distrib dvd_def)
chaieb@26125
   134
chaieb@26125
   135
lemma divides_exp2: "n \<noteq> 0 \<Longrightarrow> (x::nat) ^ n dvd y \<Longrightarrow> x dvd y" 
chaieb@26125
   136
  by (induct n ,auto simp add: dvd_def)
chaieb@26125
   137
chaieb@26125
   138
fun fact :: "nat \<Rightarrow> nat" where
chaieb@26125
   139
  "fact 0 = 1"
chaieb@26125
   140
| "fact (Suc n) = Suc n * fact n"	
chaieb@26125
   141
chaieb@26125
   142
lemma fact_lt: "0 < fact n" by(induct n, simp_all)
chaieb@26125
   143
lemma fact_le: "fact n \<ge> 1" using fact_lt[of n] by simp 
chaieb@26125
   144
lemma fact_mono: assumes le: "m \<le> n" shows "fact m \<le> fact n"
chaieb@26125
   145
proof-
chaieb@26125
   146
  from le have "\<exists>i. n = m+i" by presburger
chaieb@26125
   147
  then obtain i where i: "n = m+i" by blast 
chaieb@26125
   148
  have "fact m \<le> fact (m + i)"
chaieb@26125
   149
  proof(induct m)
chaieb@26125
   150
    case 0 thus ?case using fact_le[of i] by simp
chaieb@26125
   151
  next
chaieb@26125
   152
    case (Suc m)
chaieb@26125
   153
    have "fact (Suc m) = Suc m * fact m" by simp
chaieb@26125
   154
    have th1: "Suc m \<le> Suc (m + i)" by simp
chaieb@26125
   155
    from mult_le_mono[of "Suc m" "Suc (m+i)" "fact m" "fact (m+i)", OF th1 Suc.hyps]
chaieb@26125
   156
    show ?case by simp
chaieb@26125
   157
  qed
chaieb@26125
   158
  thus ?thesis using i by simp
chaieb@26125
   159
qed
chaieb@26125
   160
chaieb@26125
   161
lemma divides_fact: "1 <= p \<Longrightarrow> p <= n ==> p dvd fact n"
chaieb@26125
   162
proof(induct n arbitrary: p)
chaieb@26125
   163
  case 0 thus ?case by simp
chaieb@26125
   164
next
chaieb@26125
   165
  case (Suc n p)
chaieb@26125
   166
  from Suc.prems have "p = Suc n \<or> p \<le> n" by presburger 
chaieb@26125
   167
  moreover
chaieb@26125
   168
  {assume "p = Suc n" hence ?case  by (simp only: fact.simps dvd_triv_left)}
chaieb@26125
   169
  moreover
chaieb@26125
   170
  {assume "p \<le> n"
chaieb@26125
   171
    with Suc.prems(1) Suc.hyps have th: "p dvd fact n" by simp
chaieb@26125
   172
    from dvd_mult[OF th] have ?case by (simp only: fact.simps) }
chaieb@26125
   173
  ultimately show ?case by blast
chaieb@26125
   174
qed
chaieb@26125
   175
chaieb@26125
   176
declare dvd_triv_left[presburger]
chaieb@26125
   177
declare dvd_triv_right[presburger]
chaieb@26125
   178
lemma divides_rexp: 
chaieb@26125
   179
  "x dvd y \<Longrightarrow> (x::nat) dvd (y^(Suc n))" by (simp add: dvd_mult2[of x y])
chaieb@26125
   180
wenzelm@26144
   181
text {* The Bezout theorem is a bit ugly for N; it'd be easier for Z *}
chaieb@26125
   182
lemma ind_euclid: 
chaieb@26125
   183
  assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0" 
chaieb@26125
   184
  and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)" 
chaieb@26125
   185
  shows "P a b"
chaieb@26125
   186
proof(induct n\<equiv>"a+b" arbitrary: a b rule: nat_less_induct)
chaieb@26125
   187
  fix n a b
chaieb@26125
   188
  assume H: "\<forall>m < n. \<forall>a b. m = a + b \<longrightarrow> P a b" "n = a + b"
chaieb@26125
   189
  have "a = b \<or> a < b \<or> b < a" by arith
chaieb@26125
   190
  moreover {assume eq: "a= b"
chaieb@26125
   191
    from add[rule_format, OF z[rule_format, of a]] have "P a b" using eq by simp}
chaieb@26125
   192
  moreover
chaieb@26125
   193
  {assume lt: "a < b"
chaieb@26125
   194
    hence "a + b - a < n \<or> a = 0"  using H(2) by arith
chaieb@26125
   195
    moreover
chaieb@26125
   196
    {assume "a =0" with z c have "P a b" by blast }
chaieb@26125
   197
    moreover
chaieb@26125
   198
    {assume ab: "a + b - a < n"
chaieb@26125
   199
      have th0: "a + b - a = a + (b - a)" using lt by arith
chaieb@26125
   200
      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
chaieb@26125
   201
      have "P a b" by (simp add: th0[symmetric])}
chaieb@26125
   202
    ultimately have "P a b" by blast}
chaieb@26125
   203
  moreover
chaieb@26125
   204
  {assume lt: "a > b"
chaieb@26125
   205
    hence "b + a - b < n \<or> b = 0"  using H(2) by arith
chaieb@26125
   206
    moreover
chaieb@26125
   207
    {assume "b =0" with z c have "P a b" by blast }
chaieb@26125
   208
    moreover
chaieb@26125
   209
    {assume ab: "b + a - b < n"
chaieb@26125
   210
      have th0: "b + a - b = b + (a - b)" using lt by arith
chaieb@26125
   211
      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
chaieb@26125
   212
      have "P b a" by (simp add: th0[symmetric])
chaieb@26125
   213
      hence "P a b" using c by blast }
chaieb@26125
   214
    ultimately have "P a b" by blast}
chaieb@26125
   215
ultimately  show "P a b" by blast
chaieb@26125
   216
qed
chaieb@26125
   217
chaieb@26125
   218
lemma bezout_lemma: 
chaieb@26125
   219
  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@26125
   220
  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@26125
   221
using ex
chaieb@26125
   222
apply clarsimp
chaieb@26125
   223
apply (rule_tac x="d" in exI, simp add: dvd_add)
chaieb@26125
   224
apply (case_tac "a * x = b * y + d" , simp_all)
chaieb@26125
   225
apply (rule_tac x="x + y" in exI)
chaieb@26125
   226
apply (rule_tac x="y" in exI)
chaieb@26125
   227
apply algebra
chaieb@26125
   228
apply (rule_tac x="x" in exI)
chaieb@26125
   229
apply (rule_tac x="x + y" in exI)
chaieb@26125
   230
apply algebra
chaieb@26125
   231
done
chaieb@26125
   232
chaieb@26125
   233
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@26125
   234
apply(induct a b rule: ind_euclid)
chaieb@26125
   235
apply blast
chaieb@26125
   236
apply clarify
chaieb@26125
   237
apply (rule_tac x="a" in exI, simp add: dvd_add)
chaieb@26125
   238
apply clarsimp
chaieb@26125
   239
apply (rule_tac x="d" in exI)
chaieb@26125
   240
apply (case_tac "a * x = b * y + d", simp_all add: dvd_add)
chaieb@26125
   241
apply (rule_tac x="x+y" in exI)
chaieb@26125
   242
apply (rule_tac x="y" in exI)
chaieb@26125
   243
apply algebra
chaieb@26125
   244
apply (rule_tac x="x" in exI)
chaieb@26125
   245
apply (rule_tac x="x+y" in exI)
chaieb@26125
   246
apply algebra
chaieb@26125
   247
done
chaieb@26125
   248
chaieb@26125
   249
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@26125
   250
using bezout_add[of a b]
chaieb@26125
   251
apply clarsimp
chaieb@26125
   252
apply (rule_tac x="d" in exI, simp)
chaieb@26125
   253
apply (rule_tac x="x" in exI)
chaieb@26125
   254
apply (rule_tac x="y" in exI)
chaieb@26125
   255
apply auto
chaieb@26125
   256
done
chaieb@26125
   257
wenzelm@26144
   258
text {* We can get a stronger version with a nonzeroness assumption. *}
chaieb@26125
   259
chaieb@26125
   260
lemma bezout_add_strong: assumes nz: "a \<noteq> (0::nat)"
chaieb@26125
   261
  shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
chaieb@26125
   262
proof-
chaieb@26125
   263
  from nz have ap: "a > 0" by simp
chaieb@26125
   264
 from bezout_add[of a b] 
chaieb@26125
   265
 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@26125
   266
 moreover
chaieb@26125
   267
 {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
chaieb@26125
   268
   from H have ?thesis by blast }
chaieb@26125
   269
 moreover
chaieb@26125
   270
 {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
chaieb@26125
   271
   {assume b0: "b = 0" with H  have ?thesis by simp}
chaieb@26125
   272
   moreover 
chaieb@26125
   273
   {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
chaieb@26125
   274
     from divides_le[OF H(2)] b have "d < b \<or> d = b" using le_less by blast
chaieb@26125
   275
     moreover
chaieb@26125
   276
     {assume db: "d=b"
chaieb@26125
   277
       from prems have ?thesis apply simp
chaieb@26125
   278
	 apply (rule exI[where x = b], simp)
chaieb@26125
   279
	 apply (rule exI[where x = b])
chaieb@26125
   280
	by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
chaieb@26125
   281
    moreover
chaieb@26125
   282
    {assume db: "d < b" 
chaieb@26125
   283
	{assume "x=0" hence ?thesis  using prems by simp }
chaieb@26125
   284
	moreover
chaieb@26125
   285
	{assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
chaieb@26125
   286
	  
chaieb@26125
   287
	  from db have "d \<le> b - 1" by simp
chaieb@26125
   288
	  hence "d*b \<le> b*(b - 1)" by simp
chaieb@26125
   289
	  with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
chaieb@26125
   290
	  have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
chaieb@26125
   291
	  from H (3) have "d + (b - 1) * (b*x) = d + (b - 1) * (a*y + d)" by simp
chaieb@26125
   292
	  hence "d + (b - 1) * a * y + (b - 1) * d = d + (b - 1) * b * x"
chaieb@26125
   293
	    by (simp only: mult_assoc right_distrib)
chaieb@26125
   294
	  hence "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)" by algebra
chaieb@26125
   295
	  hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
chaieb@26125
   296
	  hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)"
chaieb@26125
   297
	    by (simp only: diff_add_assoc[OF dble, of d, symmetric])
chaieb@26125
   298
	  hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
chaieb@26125
   299
	    by (simp only: diff_mult_distrib2 add_commute mult_ac)
chaieb@26125
   300
	  hence ?thesis using H(1,2)
chaieb@26125
   301
	    apply -
chaieb@26125
   302
	    apply (rule exI[where x=d], simp)
chaieb@26125
   303
	    apply (rule exI[where x="(b - 1) * y"])
chaieb@26125
   304
	    by (rule exI[where x="x*(b - 1) - d"], simp)}
chaieb@26125
   305
	ultimately have ?thesis by blast}
chaieb@26125
   306
    ultimately have ?thesis by blast}
chaieb@26125
   307
  ultimately have ?thesis by blast}
chaieb@26125
   308
 ultimately show ?thesis by blast
chaieb@26125
   309
qed
chaieb@26125
   310
wenzelm@26144
   311
text {* Greatest common divisor. *}
haftmann@27556
   312
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@26125
   313
proof(auto)
chaieb@26125
   314
  assume H: "d dvd a" "d dvd b" "\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d"
chaieb@26125
   315
  from H(3)[rule_format] gcd_dvd1[of a b] gcd_dvd2[of a b] 
haftmann@27556
   316
  have th: "gcd a b dvd d" by blast
chaieb@27567
   317
  from dvd_anti_sym[OF th gcd_greatest[OF H(1,2)]]  show "d = gcd a b" by blast 
chaieb@26125
   318
qed
chaieb@26125
   319
chaieb@26125
   320
lemma gcd_eq: assumes H: "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd u \<and> d dvd v"
haftmann@27556
   321
  shows "gcd x y = gcd u v"
chaieb@26125
   322
proof-
haftmann@27556
   323
  from H have "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd gcd u v" by simp
haftmann@27556
   324
  with gcd_unique[of "gcd u v" x y]  show ?thesis by auto
chaieb@26125
   325
qed
chaieb@26125
   326
haftmann@27556
   327
lemma bezout_gcd: "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
chaieb@26125
   328
proof-
haftmann@27556
   329
  let ?g = "gcd a b"
chaieb@26125
   330
  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@26125
   331
  from d(1,2) have "d dvd ?g" by simp
chaieb@26125
   332
  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
chaieb@26125
   333
  from d(3) have "(a * x - b * y)*k = d*k \<or> (b * x - a * y)*k = d*k" by blast 
chaieb@26125
   334
  hence "a * x * k - b * y*k = d*k \<or> b * x * k - a * y*k = d*k"
chaieb@26125
   335
    by (simp only: diff_mult_distrib)
chaieb@26125
   336
  hence "a * (x * k) - b * (y*k) = ?g \<or> b * (x * k) - a * (y*k) = ?g"
chaieb@26125
   337
    by (simp add: k mult_assoc)
chaieb@26125
   338
  thus ?thesis by blast
chaieb@26125
   339
qed
chaieb@26125
   340
chaieb@26125
   341
lemma bezout_gcd_strong: assumes a: "a \<noteq> 0" 
haftmann@27556
   342
  shows "\<exists>x y. a * x = b * y + gcd a b"
chaieb@26125
   343
proof-
haftmann@27556
   344
  let ?g = "gcd a b"
chaieb@26125
   345
  from bezout_add_strong[OF a, of b]
chaieb@26125
   346
  obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
chaieb@26125
   347
  from d(1,2) have "d dvd ?g" by simp
chaieb@26125
   348
  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
chaieb@26125
   349
  from d(3) have "a * x * k = (b * y + d) *k " by auto 
chaieb@26125
   350
  hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
chaieb@26125
   351
  thus ?thesis by blast
chaieb@26125
   352
qed
chaieb@26125
   353
chaieb@27567
   354
lemma gcd_mult_distrib: "gcd(a * c) (b * c) = c * gcd a b"
chaieb@26125
   355
by(simp add: gcd_mult_distrib2 mult_commute)
chaieb@26125
   356
haftmann@27556
   357
lemma gcd_bezout: "(\<exists>x y. a * x - b * y = d \<or> b * x - a * y = d) \<longleftrightarrow> gcd a b dvd d"
chaieb@26125
   358
  (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@26125
   359
proof-
haftmann@27556
   360
  let ?g = "gcd a b"
chaieb@26125
   361
  {assume H: ?rhs then obtain k where k: "d = ?g*k" unfolding dvd_def by blast
chaieb@26125
   362
    from bezout_gcd[of a b] obtain x y where xy: "a * x - b * y = ?g \<or> b * x - a * y = ?g"
chaieb@26125
   363
      by blast
chaieb@26125
   364
    hence "(a * x - b * y)*k = ?g*k \<or> (b * x - a * y)*k = ?g*k" by auto
chaieb@26125
   365
    hence "a * x*k - b * y*k = ?g*k \<or> b * x * k - a * y*k = ?g*k" 
chaieb@26125
   366
      by (simp only: diff_mult_distrib)
chaieb@26125
   367
    hence "a * (x*k) - b * (y*k) = d \<or> b * (x * k) - a * (y*k) = d" 
chaieb@26125
   368
      by (simp add: k[symmetric] mult_assoc)
chaieb@26125
   369
    hence ?lhs by blast}
chaieb@26125
   370
  moreover
chaieb@26125
   371
  {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
chaieb@26125
   372
    have dv: "?g dvd a*x" "?g dvd b * y" "?g dvd b*x" "?g dvd a * y" 
chaieb@26125
   373
      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
chaieb@26125
   374
    from dvd_diff[OF dv(1,2)] dvd_diff[OF dv(3,4)] H
chaieb@26125
   375
    have ?rhs by auto}
chaieb@26125
   376
  ultimately show ?thesis by blast
chaieb@26125
   377
qed
chaieb@26125
   378
haftmann@27556
   379
lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
chaieb@26125
   380
proof-
haftmann@27556
   381
  let ?g = "gcd a b"
chaieb@26125
   382
    have dv: "?g dvd a*x" "?g dvd b * y" 
chaieb@26125
   383
      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
chaieb@26125
   384
    from dvd_add[OF dv] H
chaieb@26125
   385
    show ?thesis by auto
chaieb@26125
   386
qed
chaieb@26125
   387
haftmann@27556
   388
lemma gcd_mult': "gcd b (a * b) = b"
chaieb@26125
   389
by (simp add: gcd_mult mult_commute[of a b]) 
chaieb@26125
   390
chaieb@27567
   391
lemma gcd_add: "gcd(a + b) b = gcd a b" 
chaieb@27567
   392
  "gcd(b + a) b = gcd a b" "gcd a (a + b) = gcd a b" "gcd a (b + a) = gcd a b"
chaieb@26125
   393
apply (simp_all add: gcd_add1)
chaieb@26125
   394
by (simp add: gcd_commute gcd_add1)
chaieb@26125
   395
chaieb@27567
   396
lemma gcd_sub: "b <= a ==> gcd(a - b) b = gcd a b" "a <= b ==> gcd a (b - a) = gcd a b"
chaieb@26125
   397
proof-
chaieb@26125
   398
  {fix a b assume H: "b \<le> (a::nat)"
chaieb@26125
   399
    hence th: "a - b + b = a" by arith
chaieb@27567
   400
    from gcd_add(1)[of "a - b" b] th  have "gcd(a - b) b = gcd a b" by simp}
chaieb@26125
   401
  note th = this
chaieb@26125
   402
{
chaieb@26125
   403
  assume ab: "b \<le> a"
chaieb@27567
   404
  from th[OF ab] show "gcd (a - b)  b = gcd a b" by blast
chaieb@26125
   405
next
chaieb@26125
   406
  assume ab: "a \<le> b"
haftmann@27556
   407
  from th[OF ab] show "gcd a (b - a) = gcd a b" 
chaieb@26125
   408
    by (simp add: gcd_commute)}
chaieb@26125
   409
qed
chaieb@26125
   410
wenzelm@26144
   411
text {* Coprimality *}
chaieb@26125
   412
chaieb@26125
   413
lemma coprime: "coprime a b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
chaieb@26125
   414
using gcd_unique[of 1 a b, simplified] by (auto simp add: coprime_def)
chaieb@26125
   415
lemma coprime_commute: "coprime a b \<longleftrightarrow> coprime b a" by (simp add: coprime_def gcd_commute)
chaieb@26125
   416
chaieb@26125
   417
lemma coprime_bezout: "coprime a b \<longleftrightarrow> (\<exists>x y. a * x - b * y = 1 \<or> b * x - a * y = 1)"
chaieb@26125
   418
using coprime_def gcd_bezout by auto
chaieb@26125
   419
chaieb@26125
   420
lemma coprime_divprod: "d dvd a * b  \<Longrightarrow> coprime d a \<Longrightarrow> d dvd b"
chaieb@26125
   421
  using relprime_dvd_mult_iff[of d a b] by (auto simp add: coprime_def mult_commute)
chaieb@26125
   422
chaieb@26125
   423
lemma coprime_1[simp]: "coprime a 1" by (simp add: coprime_def)
chaieb@26125
   424
lemma coprime_1'[simp]: "coprime 1 a" by (simp add: coprime_def)
chaieb@26125
   425
lemma coprime_Suc0[simp]: "coprime a (Suc 0)" by (simp add: coprime_def)
chaieb@26125
   426
lemma coprime_Suc0'[simp]: "coprime (Suc 0) a" by (simp add: coprime_def)
chaieb@26125
   427
chaieb@26125
   428
lemma gcd_coprime: 
haftmann@27556
   429
  assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 
chaieb@26125
   430
  shows    "coprime a' b'"
chaieb@26125
   431
proof-
haftmann@27556
   432
  let ?g = "gcd a b"
chaieb@26125
   433
  {assume bz: "a = 0" from b bz z a have ?thesis by (simp add: gcd_zero coprime_def)}
chaieb@26125
   434
  moreover 
chaieb@26125
   435
  {assume az: "a\<noteq> 0" 
chaieb@26125
   436
    from z have z': "?g > 0" by simp
chaieb@26125
   437
    from bezout_gcd_strong[OF az, of b] 
chaieb@26125
   438
    obtain x y where xy: "a*x = b*y + ?g" by blast
chaieb@26125
   439
    from xy a b have "?g * a'*x = ?g * (b'*y + 1)" by (simp add: ring_simps)
chaieb@26125
   440
    hence "?g * (a'*x) = ?g * (b'*y + 1)" by (simp add: mult_assoc)
chaieb@26125
   441
    hence "a'*x = (b'*y + 1)"
chaieb@26125
   442
      by (simp only: nat_mult_eq_cancel1[OF z']) 
chaieb@26125
   443
    hence "a'*x - b'*y = 1" by simp
chaieb@26125
   444
    with coprime_bezout[of a' b'] have ?thesis by auto}
chaieb@26125
   445
  ultimately show ?thesis by blast
chaieb@26125
   446
qed
chaieb@26125
   447
lemma coprime_0: "coprime d 0 \<longleftrightarrow> d = 1" by (simp add: coprime_def)
chaieb@26125
   448
lemma coprime_mul: assumes da: "coprime d a" and db: "coprime d b"
chaieb@26125
   449
  shows "coprime d (a * b)"
chaieb@26125
   450
proof-
haftmann@27556
   451
  from da have th: "gcd a d = 1" by (simp add: coprime_def gcd_commute)
chaieb@27567
   452
  from gcd_mult_cancel[of a d b, OF th] db[unfolded coprime_def] have "gcd d (a*b) = 1"
chaieb@26125
   453
    by (simp add: gcd_commute)
chaieb@26125
   454
  thus ?thesis unfolding coprime_def .
chaieb@26125
   455
qed
chaieb@26125
   456
lemma coprime_lmul2: assumes dab: "coprime d (a * b)" shows "coprime d b"
chaieb@26125
   457
using prems unfolding coprime_bezout
chaieb@26125
   458
apply clarsimp
chaieb@26125
   459
apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
chaieb@26125
   460
apply (rule_tac x="x" in exI)
chaieb@26125
   461
apply (rule_tac x="a*y" in exI)
chaieb@26125
   462
apply (simp add: mult_ac)
chaieb@26125
   463
apply (rule_tac x="a*x" in exI)
chaieb@26125
   464
apply (rule_tac x="y" in exI)
chaieb@26125
   465
apply (simp add: mult_ac)
chaieb@26125
   466
done
chaieb@26125
   467
chaieb@26125
   468
lemma coprime_rmul2: "coprime d (a * b) \<Longrightarrow> coprime d a"
chaieb@26125
   469
unfolding coprime_bezout
chaieb@26125
   470
apply clarsimp
chaieb@26125
   471
apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
chaieb@26125
   472
apply (rule_tac x="x" in exI)
chaieb@26125
   473
apply (rule_tac x="b*y" in exI)
chaieb@26125
   474
apply (simp add: mult_ac)
chaieb@26125
   475
apply (rule_tac x="b*x" in exI)
chaieb@26125
   476
apply (rule_tac x="y" in exI)
chaieb@26125
   477
apply (simp add: mult_ac)
chaieb@26125
   478
done
chaieb@26125
   479
lemma coprime_mul_eq: "coprime d (a * b) \<longleftrightarrow> coprime d a \<and>  coprime d b"
chaieb@26125
   480
  using coprime_rmul2[of d a b] coprime_lmul2[of d a b] coprime_mul[of d a b] 
chaieb@26125
   481
  by blast
chaieb@26125
   482
chaieb@26125
   483
lemma gcd_coprime_exists:
haftmann@27556
   484
  assumes nz: "gcd a b \<noteq> 0" 
haftmann@27556
   485
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
chaieb@26125
   486
proof-
haftmann@27556
   487
  let ?g = "gcd a b"
chaieb@26125
   488
  from gcd_dvd1[of a b] gcd_dvd2[of a b] 
chaieb@26125
   489
  obtain a' b' where "a = ?g*a'"  "b = ?g*b'" unfolding dvd_def by blast
chaieb@26125
   490
  hence ab': "a = a'*?g" "b = b'*?g" by algebra+
chaieb@26125
   491
  from ab' gcd_coprime[OF nz ab'] show ?thesis by blast
chaieb@26125
   492
qed
chaieb@26125
   493
chaieb@26125
   494
lemma coprime_exp: "coprime d a ==> coprime d (a^n)" 
chaieb@26125
   495
  by(induct n, simp_all add: coprime_mul)
chaieb@26125
   496
chaieb@26125
   497
lemma coprime_exp_imp: "coprime a b ==> coprime (a ^n) (b ^n)"
chaieb@26125
   498
  by (induct n, simp_all add: coprime_mul_eq coprime_commute coprime_exp)
chaieb@26125
   499
lemma coprime_refl[simp]: "coprime n n \<longleftrightarrow> n = 1" by (simp add: coprime_def)
chaieb@26125
   500
lemma coprime_plus1[simp]: "coprime (n + 1) n"
chaieb@26125
   501
  apply (simp add: coprime_bezout)
chaieb@26125
   502
  apply (rule exI[where x=1])
chaieb@26125
   503
  apply (rule exI[where x=1])
chaieb@26125
   504
  apply simp
chaieb@26125
   505
  done
chaieb@26125
   506
lemma coprime_minus1: "n \<noteq> 0 ==> coprime (n - 1) n"
chaieb@26125
   507
  using coprime_plus1[of "n - 1"] coprime_commute[of "n - 1" n] by auto
chaieb@26125
   508
haftmann@27556
   509
lemma bezout_gcd_pow: "\<exists>x y. a ^n * x - b ^ n * y = gcd a b ^ n \<or> b ^ n * x - a ^ n * y = gcd a b ^ n"
chaieb@26125
   510
proof-
haftmann@27556
   511
  let ?g = "gcd a b"
chaieb@26125
   512
  {assume z: "?g = 0" hence ?thesis 
chaieb@26125
   513
      apply (cases n, simp)
chaieb@26125
   514
      apply arith
chaieb@26125
   515
      apply (simp only: z power_0_Suc)
chaieb@26125
   516
      apply (rule exI[where x=0])
chaieb@26125
   517
      apply (rule exI[where x=0])
chaieb@26125
   518
      by simp}
chaieb@26125
   519
  moreover
chaieb@26125
   520
  {assume z: "?g \<noteq> 0"
chaieb@26125
   521
    from gcd_dvd1[of a b] gcd_dvd2[of a b] obtain a' b' where
chaieb@26125
   522
      ab': "a = a'*?g" "b = b'*?g" unfolding dvd_def by (auto simp add: mult_ac)
chaieb@26125
   523
    hence ab'': "?g*a' = a" "?g * b' = b" by algebra+
chaieb@26125
   524
    from coprime_exp_imp[OF gcd_coprime[OF z ab'], unfolded coprime_bezout, of n]
chaieb@26125
   525
    obtain x y where "a'^n * x - b'^n * y = 1 \<or> b'^n * x - a'^n * y = 1"  by blast
chaieb@26125
   526
    hence "?g^n * (a'^n * x - b'^n * y) = ?g^n \<or> ?g^n*(b'^n * x - a'^n * y) = ?g^n"
chaieb@26125
   527
      using z by auto 
chaieb@26125
   528
    then have "a^n * x - b^n * y = ?g^n \<or> b^n * x - a^n * y = ?g^n"
chaieb@26125
   529
      using z ab'' by (simp only: power_mult_distrib[symmetric] 
chaieb@26125
   530
	diff_mult_distrib2 mult_assoc[symmetric])
chaieb@26125
   531
    hence  ?thesis by blast }
chaieb@26125
   532
  ultimately show ?thesis by blast
chaieb@26125
   533
qed
chaieb@27567
   534
chaieb@27567
   535
lemma gcd_exp: "gcd (a^n) (b^n) = gcd a b^n"
chaieb@26125
   536
proof-
haftmann@27556
   537
  let ?g = "gcd (a^n) (b^n)"
chaieb@27567
   538
  let ?gn = "gcd a b^n"
chaieb@26125
   539
  {fix e assume H: "e dvd a^n" "e dvd b^n"
chaieb@26125
   540
    from bezout_gcd_pow[of a n b] obtain x y 
chaieb@26125
   541
      where xy: "a ^ n * x - b ^ n * y = ?gn \<or> b ^ n * x - a ^ n * y = ?gn" by blast
chaieb@26125
   542
    from dvd_diff [OF dvd_mult2[OF H(1), of x] dvd_mult2[OF H(2), of y]]
chaieb@26125
   543
      dvd_diff [OF dvd_mult2[OF H(2), of x] dvd_mult2[OF H(1), of y]] xy
haftmann@27556
   544
    have "e dvd ?gn" by (cases "a ^ n * x - b ^ n * y = gcd a b ^ n", simp_all)}
chaieb@26125
   545
  hence th:  "\<forall>e. e dvd a^n \<and> e dvd b^n \<longrightarrow> e dvd ?gn" by blast
chaieb@26125
   546
  from divides_exp[OF gcd_dvd1[of a b], of n] divides_exp[OF gcd_dvd2[of a b], of n] th
chaieb@26125
   547
    gcd_unique have "?gn = ?g" by blast thus ?thesis by simp 
chaieb@26125
   548
qed
chaieb@26125
   549
chaieb@26125
   550
lemma coprime_exp2:  "coprime (a ^ Suc n) (b^ Suc n) \<longleftrightarrow> coprime a b"
chaieb@26125
   551
by (simp only: coprime_def gcd_exp exp_eq_1) simp
chaieb@26125
   552
chaieb@26125
   553
lemma division_decomp: assumes dc: "(a::nat) dvd b * c"
chaieb@26125
   554
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
chaieb@26125
   555
proof-
haftmann@27556
   556
  let ?g = "gcd a b"
chaieb@26125
   557
  {assume "?g = 0" with dc have ?thesis apply (simp add: gcd_zero)
chaieb@26125
   558
      apply (rule exI[where x="0"])
chaieb@26125
   559
      by (rule exI[where x="c"], simp)}
chaieb@26125
   560
  moreover
chaieb@26125
   561
  {assume z: "?g \<noteq> 0"
chaieb@26125
   562
    from gcd_coprime_exists[OF z]
chaieb@26125
   563
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
chaieb@26125
   564
    from gcd_dvd2[of a b] have thb: "?g dvd b" .
chaieb@26125
   565
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast  
chaieb@26125
   566
    with dc have th0: "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
chaieb@26125
   567
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
chaieb@26125
   568
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: mult_assoc)
chaieb@26125
   569
    with z have th_1: "a' dvd b'*c" by simp
chaieb@26125
   570
    from coprime_divprod[OF th_1 ab'(3)] have thc: "a' dvd c" . 
chaieb@26125
   571
    from ab' have "a = ?g*a'" by algebra
chaieb@26125
   572
    with thb thc have ?thesis by blast }
chaieb@26125
   573
  ultimately show ?thesis by blast
chaieb@26125
   574
qed
chaieb@26125
   575
chaieb@26125
   576
lemma nat_power_eq_0_iff: "(m::nat) ^ n = 0 \<longleftrightarrow> n \<noteq> 0 \<and> m = 0" by (induct n, auto)
chaieb@26125
   577
chaieb@26125
   578
lemma divides_rev: assumes ab: "(a::nat) ^ n dvd b ^n" and n:"n \<noteq> 0" shows "a dvd b"
chaieb@26125
   579
proof-
haftmann@27556
   580
  let ?g = "gcd a b"
chaieb@26125
   581
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
chaieb@26125
   582
  {assume "?g = 0" with ab n have ?thesis by (simp add: gcd_zero)}
chaieb@26125
   583
  moreover
chaieb@26125
   584
  {assume z: "?g \<noteq> 0"
chaieb@26125
   585
    hence zn: "?g ^ n \<noteq> 0" using n by (simp add: neq0_conv)
chaieb@26125
   586
    from gcd_coprime_exists[OF z] 
chaieb@26125
   587
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
chaieb@26125
   588
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n" by (simp add: ab'(1,2)[symmetric])
chaieb@26125
   589
    hence "?g^n*a'^n dvd ?g^n *b'^n" by (simp only: power_mult_distrib mult_commute)
chaieb@26125
   590
    with zn z n have th0:"a'^n dvd b'^n" by (auto simp add: nat_power_eq_0_iff)
chaieb@26125
   591
    have "a' dvd a'^n" by (simp add: m)
chaieb@26125
   592
    with th0 have "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by simp
chaieb@26125
   593
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult_commute)
chaieb@26125
   594
    from coprime_divprod[OF th1 coprime_exp[OF ab'(3), of m]]
chaieb@26125
   595
    have "a' dvd b'" .
chaieb@26125
   596
    hence "a'*?g dvd b'*?g" by simp
chaieb@26125
   597
    with ab'(1,2)  have ?thesis by simp }
chaieb@26125
   598
  ultimately show ?thesis by blast
chaieb@26125
   599
qed
chaieb@26125
   600
chaieb@26125
   601
lemma divides_mul: assumes mr: "m dvd r" and nr: "n dvd r" and mn:"coprime m n" 
chaieb@26125
   602
  shows "m * n dvd r"
chaieb@26125
   603
proof-
chaieb@26125
   604
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
chaieb@26125
   605
    unfolding dvd_def by blast
chaieb@26125
   606
  from mr n' have "m dvd n'*n" by (simp add: mult_commute)
chaieb@26125
   607
  hence "m dvd n'" using relprime_dvd_mult_iff[OF mn[unfolded coprime_def]] by simp
chaieb@26125
   608
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
chaieb@26125
   609
  from n' k show ?thesis unfolding dvd_def by auto
chaieb@26125
   610
qed
chaieb@26125
   611
wenzelm@26144
   612
wenzelm@26144
   613
text {* A binary form of the Chinese Remainder Theorem. *}
chaieb@26125
   614
chaieb@26125
   615
lemma chinese_remainder: assumes ab: "coprime a b" and a:"a \<noteq> 0" and b:"b \<noteq> 0"
chaieb@26125
   616
  shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
chaieb@26125
   617
proof-
chaieb@26125
   618
  from bezout_add_strong[OF a, of b] bezout_add_strong[OF b, of a]
chaieb@26125
   619
  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1" 
chaieb@26125
   620
    and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
chaieb@26125
   621
  from gcd_unique[of 1 a b, simplified ab[unfolded coprime_def], simplified] 
chaieb@26125
   622
    dxy1(1,2) dxy2(1,2) have d12: "d1 = 1" "d2 =1" by auto
chaieb@26125
   623
  let ?x = "v * a * x1 + u * b * x2"
chaieb@26125
   624
  let ?q1 = "v * x1 + u * y2"
chaieb@26125
   625
  let ?q2 = "v * y1 + u * x2"
chaieb@26125
   626
  from dxy2(3)[simplified d12] dxy1(3)[simplified d12] 
chaieb@26125
   627
  have "?x = u + ?q1 * a" "?x = v + ?q2 * b" by algebra+ 
chaieb@26125
   628
  thus ?thesis by blast
chaieb@26125
   629
qed
chaieb@26125
   630
wenzelm@26144
   631
text {* Primality *}
wenzelm@26144
   632
wenzelm@26144
   633
text {* A few useful theorems about primes *}
chaieb@26125
   634
chaieb@26125
   635
lemma prime_0[simp]: "~prime 0" by (simp add: prime_def)
chaieb@26125
   636
lemma prime_1[simp]: "~ prime 1"  by (simp add: prime_def)
chaieb@26125
   637
lemma prime_Suc0[simp]: "~ prime (Suc 0)"  by (simp add: prime_def)
chaieb@26125
   638
chaieb@26125
   639
lemma prime_ge_2: "prime p ==> p \<ge> 2" by (simp add: prime_def)
chaieb@26125
   640
lemma prime_factor: assumes n: "n \<noteq> 1" shows "\<exists> p. prime p \<and> p dvd n"
chaieb@26125
   641
using n
chaieb@26125
   642
proof(induct n rule: nat_less_induct)
chaieb@26125
   643
  fix n
chaieb@26125
   644
  assume H: "\<forall>m<n. m \<noteq> 1 \<longrightarrow> (\<exists>p. prime p \<and> p dvd m)" "n \<noteq> 1"
chaieb@26125
   645
  let ?ths = "\<exists>p. prime p \<and> p dvd n"
chaieb@26125
   646
  {assume "n=0" hence ?ths using two_is_prime by auto}
chaieb@26125
   647
  moreover
chaieb@26125
   648
  {assume nz: "n\<noteq>0" 
chaieb@26125
   649
    {assume "prime n" hence ?ths by - (rule exI[where x="n"], simp)}
chaieb@26125
   650
    moreover
chaieb@26125
   651
    {assume n: "\<not> prime n"
chaieb@26125
   652
      with nz H(2) 
chaieb@26125
   653
      obtain k where k:"k dvd n" "k \<noteq> 1" "k \<noteq> n" by (auto simp add: prime_def) 
chaieb@26125
   654
      from dvd_imp_le[OF k(1)] nz k(3) have kn: "k < n" by simp
chaieb@26125
   655
      from H(1)[rule_format, OF kn k(2)] obtain p where p: "prime p" "p dvd k" by blast
chaieb@26125
   656
      from dvd_trans[OF p(2) k(1)] p(1) have ?ths by blast}
chaieb@26125
   657
    ultimately have ?ths by blast}
chaieb@26125
   658
  ultimately show ?ths by blast
chaieb@26125
   659
qed
chaieb@26125
   660
chaieb@26125
   661
lemma prime_factor_lt: assumes p: "prime p" and n: "n \<noteq> 0" and npm:"n = p * m"
chaieb@26125
   662
  shows "m < n"
chaieb@26125
   663
proof-
chaieb@26125
   664
  {assume "m=0" with n have ?thesis by simp}
chaieb@26125
   665
  moreover
chaieb@26125
   666
  {assume m: "m \<noteq> 0"
chaieb@26125
   667
    from npm have mn: "m dvd n" unfolding dvd_def by auto
chaieb@26125
   668
    from npm m have "n \<noteq> m" using p by auto
chaieb@26125
   669
    with dvd_imp_le[OF mn] n have ?thesis by simp}
chaieb@26125
   670
  ultimately show ?thesis by blast
chaieb@26125
   671
qed
chaieb@26125
   672
chaieb@26125
   673
lemma euclid_bound: "\<exists>p. prime p \<and> n < p \<and>  p <= Suc (fact n)"
chaieb@26125
   674
proof-
chaieb@26125
   675
  have f1: "fact n + 1 \<noteq> 1" using fact_le[of n] by arith 
chaieb@26125
   676
  from prime_factor[OF f1] obtain p where p: "prime p" "p dvd fact n + 1" by blast
chaieb@26125
   677
  from dvd_imp_le[OF p(2)] have pfn: "p \<le> fact n + 1" by simp
chaieb@26125
   678
  {assume np: "p \<le> n"
chaieb@26125
   679
    from p(1) have p1: "p \<ge> 1" by (cases p, simp_all)
chaieb@26125
   680
    from divides_fact[OF p1 np] have pfn': "p dvd fact n" .
chaieb@26125
   681
    from divides_add_revr[OF pfn' p(2)] p(1) have False by simp}
chaieb@26125
   682
  hence "n < p" by arith
chaieb@26125
   683
  with p(1) pfn show ?thesis by auto
chaieb@26125
   684
qed
chaieb@26125
   685
chaieb@26125
   686
lemma euclid: "\<exists>p. prime p \<and> p > n" using euclid_bound by auto
chaieb@26125
   687
lemma primes_infinite: "\<not> (finite {p. prime p})"
chaieb@26125
   688
proof (auto simp add: finite_conv_nat_seg_image)
chaieb@26125
   689
  fix n f 
chaieb@26125
   690
  assume H: "Collect prime = f ` {i. i < (n::nat)}"
chaieb@26125
   691
  let ?P = "Collect prime"
chaieb@26125
   692
  let ?m = "Max ?P"
chaieb@26125
   693
  have P0: "?P \<noteq> {}" using two_is_prime by auto
chaieb@26125
   694
  from H have fP: "finite ?P" using finite_conv_nat_seg_image by blast 
haftmann@26757
   695
  from Max_in [OF fP P0] have "?m \<in> ?P" . 
haftmann@26757
   696
  from Max_ge [OF fP] have contr: "\<forall> p. prime p \<longrightarrow> p \<le> ?m" by blast
haftmann@26757
   697
  from euclid [of ?m] obtain q where q: "prime q" "q > ?m" by blast
chaieb@26125
   698
  with contr show False by auto
chaieb@26125
   699
qed
chaieb@26125
   700
chaieb@26125
   701
lemma coprime_prime: assumes ab: "coprime a b"
chaieb@26125
   702
  shows "~(prime p \<and> p dvd a \<and> p dvd b)"
chaieb@26125
   703
proof
chaieb@26125
   704
  assume "prime p \<and> p dvd a \<and> p dvd b"
chaieb@26125
   705
  thus False using ab gcd_greatest[of p a b] by (simp add: coprime_def)
chaieb@26125
   706
qed
chaieb@26125
   707
lemma coprime_prime_eq: "coprime a b \<longleftrightarrow> (\<forall>p. ~(prime p \<and> p dvd a \<and> p dvd b))" 
chaieb@26125
   708
  (is "?lhs = ?rhs")
chaieb@26125
   709
proof-
chaieb@26125
   710
  {assume "?lhs" with coprime_prime  have ?rhs by blast}
chaieb@26125
   711
  moreover
chaieb@26125
   712
  {assume r: "?rhs" and c: "\<not> ?lhs"
chaieb@26125
   713
    then obtain g where g: "g\<noteq>1" "g dvd a" "g dvd b" unfolding coprime_def by blast
chaieb@26125
   714
    from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
chaieb@26125
   715
    from dvd_trans [OF p(2) g(2)] dvd_trans [OF p(2) g(3)] 
chaieb@26125
   716
    have "p dvd a" "p dvd b" . with p(1) r have False by blast}
chaieb@26125
   717
  ultimately show ?thesis by blast
chaieb@26125
   718
qed
chaieb@26125
   719
chaieb@26125
   720
lemma prime_coprime: assumes p: "prime p" 
chaieb@26125
   721
  shows "n = 1 \<or> p dvd n \<or> coprime p n"
chaieb@26125
   722
using p prime_imp_relprime[of p n] by (auto simp add: coprime_def)
chaieb@26125
   723
chaieb@26125
   724
lemma prime_coprime_strong: "prime p \<Longrightarrow> p dvd n \<or> coprime p n"
chaieb@26125
   725
  using prime_coprime[of p n] by auto
chaieb@26125
   726
chaieb@26125
   727
declare  coprime_0[simp]
chaieb@26125
   728
chaieb@26125
   729
lemma coprime_0'[simp]: "coprime 0 d \<longleftrightarrow> d = 1" by (simp add: coprime_commute[of 0 d])
chaieb@26125
   730
lemma coprime_bezout_strong: assumes ab: "coprime a b" and b: "b \<noteq> 1"
chaieb@26125
   731
  shows "\<exists>x y. a * x = b * y + 1"
chaieb@26125
   732
proof-
chaieb@26125
   733
  from ab b have az: "a \<noteq> 0" by - (rule ccontr, auto)
chaieb@26125
   734
  from bezout_gcd_strong[OF az, of b] ab[unfolded coprime_def]
chaieb@26125
   735
  show ?thesis by auto
chaieb@26125
   736
qed
chaieb@26125
   737
chaieb@26125
   738
lemma bezout_prime: assumes p: "prime p"  and pa: "\<not> p dvd a"
chaieb@26125
   739
  shows "\<exists>x y. a*x = p*y + 1"
chaieb@26125
   740
proof-
chaieb@26125
   741
  from p have p1: "p \<noteq> 1" using prime_1 by blast 
chaieb@26125
   742
  from prime_coprime[OF p, of a] p1 pa have ap: "coprime a p" 
chaieb@26125
   743
    by (auto simp add: coprime_commute)
chaieb@26125
   744
  from coprime_bezout_strong[OF ap p1] show ?thesis . 
chaieb@26125
   745
qed
chaieb@26125
   746
lemma prime_divprod: assumes p: "prime p" and pab: "p dvd a*b"
chaieb@26125
   747
  shows "p dvd a \<or> p dvd b"
chaieb@26125
   748
proof-
chaieb@26125
   749
  {assume "a=1" hence ?thesis using pab by simp }
chaieb@26125
   750
  moreover
chaieb@26125
   751
  {assume "p dvd a" hence ?thesis by blast}
chaieb@26125
   752
  moreover
chaieb@26125
   753
  {assume pa: "coprime p a" from coprime_divprod[OF pab pa]  have ?thesis .. }
chaieb@26125
   754
  ultimately show ?thesis using prime_coprime[OF p, of a] by blast
chaieb@26125
   755
qed
chaieb@26125
   756
chaieb@26125
   757
lemma prime_divprod_eq: assumes p: "prime p"
chaieb@26125
   758
  shows "p dvd a*b \<longleftrightarrow> p dvd a \<or> p dvd b"
chaieb@26125
   759
using p prime_divprod dvd_mult dvd_mult2 by auto
chaieb@26125
   760
chaieb@26125
   761
lemma prime_divexp: assumes p:"prime p" and px: "p dvd x^n"
chaieb@26125
   762
  shows "p dvd x"
chaieb@26125
   763
using px
chaieb@26125
   764
proof(induct n)
chaieb@26125
   765
  case 0 thus ?case by simp
chaieb@26125
   766
next
chaieb@26125
   767
  case (Suc n) 
chaieb@26125
   768
  hence th: "p dvd x*x^n" by simp
chaieb@26125
   769
  {assume H: "p dvd x^n"
chaieb@26125
   770
    from Suc.hyps[OF H] have ?case .}
chaieb@26125
   771
  with prime_divprod[OF p th] show ?case by blast
chaieb@26125
   772
qed
chaieb@26125
   773
chaieb@26125
   774
lemma prime_divexp_n: "prime p \<Longrightarrow> p dvd x^n \<Longrightarrow> p^n dvd x^n"
chaieb@26125
   775
  using prime_divexp[of p x n] divides_exp[of p x n] by blast
chaieb@26125
   776
chaieb@26125
   777
lemma coprime_prime_dvd_ex: assumes xy: "\<not>coprime x y"
chaieb@26125
   778
  shows "\<exists>p. prime p \<and> p dvd x \<and> p dvd y"
chaieb@26125
   779
proof-
chaieb@26125
   780
  from xy[unfolded coprime_def] obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd y" 
chaieb@26125
   781
    by blast
chaieb@26125
   782
  from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
chaieb@26125
   783
  from g(2,3) dvd_trans[OF p(2)] p(1) show ?thesis by auto
chaieb@26125
   784
qed
chaieb@26125
   785
lemma coprime_sos: assumes xy: "coprime x y" 
chaieb@26125
   786
  shows "coprime (x * y) (x^2 + y^2)"
chaieb@26125
   787
proof-
chaieb@26125
   788
  {assume c: "\<not> coprime (x * y) (x^2 + y^2)"
chaieb@26125
   789
    from coprime_prime_dvd_ex[OF c] obtain p 
chaieb@26125
   790
      where p: "prime p" "p dvd x*y" "p dvd x^2 + y^2" by blast
chaieb@26125
   791
    {assume px: "p dvd x"
haftmann@27651
   792
      from dvd_mult[OF px, of x] p(3) 
haftmann@27651
   793
        obtain r s where "x * x = p * r" and "x^2 + y^2 = p * s"
haftmann@27651
   794
          by (auto elim!: dvdE)
haftmann@27651
   795
        then have "y^2 = p * (s - r)" 
haftmann@27651
   796
          by (auto simp add: power2_eq_square diff_mult_distrib2)
haftmann@27651
   797
        then have "p dvd y^2" ..
chaieb@26125
   798
      with prime_divexp[OF p(1), of y 2] have py: "p dvd y" .
chaieb@26125
   799
      from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
chaieb@26125
   800
      have False by simp }
chaieb@26125
   801
    moreover
chaieb@26125
   802
    {assume py: "p dvd y"
haftmann@27651
   803
      from dvd_mult[OF py, of y] p(3)
haftmann@27651
   804
        obtain r s where "y * y = p * r" and "x^2 + y^2 = p * s"
haftmann@27651
   805
          by (auto elim!: dvdE)
haftmann@27651
   806
        then have "x^2 = p * (s - r)" 
haftmann@27651
   807
          by (auto simp add: power2_eq_square diff_mult_distrib2)
haftmann@27651
   808
        then have "p dvd x^2" ..
chaieb@26125
   809
      with prime_divexp[OF p(1), of x 2] have px: "p dvd x" .
chaieb@26125
   810
      from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
chaieb@26125
   811
      have False by simp }
chaieb@26125
   812
    ultimately have False using prime_divprod[OF p(1,2)] by blast}
chaieb@26125
   813
  thus ?thesis by blast
chaieb@26125
   814
qed
chaieb@26125
   815
chaieb@26125
   816
lemma distinct_prime_coprime: "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
chaieb@26125
   817
  unfolding prime_def coprime_prime_eq by blast
chaieb@26125
   818
chaieb@26125
   819
lemma prime_coprime_lt: assumes p: "prime p" and x: "0 < x" and xp: "x < p"
chaieb@26125
   820
  shows "coprime x p"
chaieb@26125
   821
proof-
chaieb@26125
   822
  {assume c: "\<not> coprime x p"
chaieb@26125
   823
    then obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd p" unfolding coprime_def by blast
chaieb@26125
   824
  from dvd_imp_le[OF g(2)] x xp have gp: "g < p" by arith
chaieb@26125
   825
  from g(2) x have "g \<noteq> 0" by - (rule ccontr, simp)
chaieb@26125
   826
  with g gp p[unfolded prime_def] have False by blast}
chaieb@26125
   827
thus ?thesis by blast
chaieb@26125
   828
qed
chaieb@26125
   829
chaieb@26125
   830
lemma even_dvd[simp]: "even (n::nat) \<longleftrightarrow> 2 dvd n" by presburger
chaieb@26125
   831
lemma prime_odd: "prime p \<Longrightarrow> p = 2 \<or> odd p" unfolding prime_def by auto
chaieb@26125
   832
wenzelm@26144
   833
wenzelm@26144
   834
text {* One property of coprimality is easier to prove via prime factors. *}
chaieb@26125
   835
chaieb@26125
   836
lemma prime_divprod_pow: 
chaieb@26125
   837
  assumes p: "prime p" and ab: "coprime a b" and pab: "p^n dvd a * b"
chaieb@26125
   838
  shows "p^n dvd a \<or> p^n dvd b"
chaieb@26125
   839
proof-
chaieb@26125
   840
  {assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis 
chaieb@26125
   841
      apply (cases "n=0", simp_all)
chaieb@26125
   842
      apply (cases "a=1", simp_all) done}
chaieb@26125
   843
  moreover
chaieb@26125
   844
  {assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1" 
chaieb@26125
   845
    then obtain m where m: "n = Suc m" by (cases n, auto)
chaieb@26125
   846
    from divides_exp2[OF n pab] have pab': "p dvd a*b" .
chaieb@26125
   847
    from prime_divprod[OF p pab'] 
chaieb@26125
   848
    have "p dvd a \<or> p dvd b" .
chaieb@26125
   849
    moreover
chaieb@26125
   850
    {assume pa: "p dvd a"
chaieb@26125
   851
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
chaieb@26125
   852
      from coprime_prime[OF ab, of p] p pa have "\<not> p dvd b" by blast
chaieb@26125
   853
      with prime_coprime[OF p, of b] b 
chaieb@26125
   854
      have cpb: "coprime b p" using coprime_commute by blast 
chaieb@26125
   855
      from coprime_exp[OF cpb] have pnb: "coprime (p^n) b" 
chaieb@26125
   856
	by (simp add: coprime_commute)
chaieb@26125
   857
      from coprime_divprod[OF pnba pnb] have ?thesis by blast }
chaieb@26125
   858
    moreover
chaieb@26125
   859
    {assume pb: "p dvd b"
chaieb@26125
   860
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
chaieb@26125
   861
      from coprime_prime[OF ab, of p] p pb have "\<not> p dvd a" by blast
chaieb@26125
   862
      with prime_coprime[OF p, of a] a
chaieb@26125
   863
      have cpb: "coprime a p" using coprime_commute by blast 
chaieb@26125
   864
      from coprime_exp[OF cpb] have pnb: "coprime (p^n) a" 
chaieb@26125
   865
	by (simp add: coprime_commute)
chaieb@26125
   866
      from coprime_divprod[OF pab pnb] have ?thesis by blast }
chaieb@26125
   867
    ultimately have ?thesis by blast}
chaieb@26125
   868
  ultimately show ?thesis by blast
chaieb@26125
   869
qed
chaieb@26125
   870
chaieb@26125
   871
lemma nat_mult_eq_one: "(n::nat) * m = 1 \<longleftrightarrow> n = 1 \<and> m = 1" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@26125
   872
proof
chaieb@26125
   873
  assume H: "?lhs"
chaieb@26125
   874
  hence "n dvd 1" "m dvd 1" unfolding dvd_def by (auto simp add: mult_commute)
chaieb@26125
   875
  thus ?rhs by auto
chaieb@26125
   876
next
chaieb@26125
   877
  assume ?rhs then show ?lhs by auto
chaieb@26125
   878
qed
chaieb@26125
   879
  
chaieb@26125
   880
lemma power_Suc0[simp]: "Suc 0 ^ n = Suc 0" 
chaieb@26125
   881
  unfolding One_nat_def[symmetric] power_one ..
chaieb@26125
   882
lemma coprime_pow: assumes ab: "coprime a b" and abcn: "a * b = c ^n"
chaieb@26125
   883
  shows "\<exists>r s. a = r^n  \<and> b = s ^n"
chaieb@26125
   884
  using ab abcn
chaieb@26125
   885
proof(induct c arbitrary: a b rule: nat_less_induct)
chaieb@26125
   886
  fix c a b
chaieb@26125
   887
  assume H: "\<forall>m<c. \<forall>a b. coprime a b \<longrightarrow> a * b = m ^ n \<longrightarrow> (\<exists>r s. a = r ^ n \<and> b = s ^ n)" "coprime a b" "a * b = c ^ n" 
chaieb@26125
   888
  let ?ths = "\<exists>r s. a = r^n  \<and> b = s ^n"
chaieb@26125
   889
  {assume n: "n = 0"
chaieb@26125
   890
    with H(3) power_one have "a*b = 1" by simp
chaieb@26125
   891
    hence "a = 1 \<and> b = 1" by simp
chaieb@26125
   892
    hence ?ths 
chaieb@26125
   893
      apply -
chaieb@26125
   894
      apply (rule exI[where x=1])
chaieb@26125
   895
      apply (rule exI[where x=1])
chaieb@26125
   896
      using power_one[of  n]
chaieb@26125
   897
      by simp}
chaieb@26125
   898
  moreover
chaieb@26125
   899
  {assume n: "n \<noteq> 0" then obtain m where m: "n = Suc m" by (cases n, auto)
chaieb@26125
   900
    {assume c: "c = 0"
chaieb@26125
   901
      with H(3) m H(2) have ?ths apply simp 
chaieb@26125
   902
	apply (cases "a=0", simp_all) 
chaieb@26125
   903
	apply (rule exI[where x="0"], simp)
chaieb@26125
   904
	apply (rule exI[where x="0"], simp)
chaieb@26125
   905
	done}
chaieb@26125
   906
    moreover
chaieb@26125
   907
    {assume "c=1" with H(3) power_one have "a*b = 1" by simp 
chaieb@26125
   908
	hence "a = 1 \<and> b = 1" by simp
chaieb@26125
   909
	hence ?ths 
chaieb@26125
   910
      apply -
chaieb@26125
   911
      apply (rule exI[where x=1])
chaieb@26125
   912
      apply (rule exI[where x=1])
chaieb@26125
   913
      using power_one[of  n]
chaieb@26125
   914
      by simp}
chaieb@26125
   915
  moreover
chaieb@26125
   916
  {assume c: "c\<noteq>1" "c \<noteq> 0"
chaieb@26125
   917
    from prime_factor[OF c(1)] obtain p where p: "prime p" "p dvd c" by blast
chaieb@26125
   918
    from prime_divprod_pow[OF p(1) H(2), unfolded H(3), OF divides_exp[OF p(2), of n]] 
chaieb@26125
   919
    have pnab: "p ^ n dvd a \<or> p^n dvd b" . 
chaieb@26125
   920
    from p(2) obtain l where l: "c = p*l" unfolding dvd_def by blast
chaieb@26125
   921
    have pn0: "p^n \<noteq> 0" using n prime_ge_2 [OF p(1)] by (simp add: neq0_conv)
chaieb@26125
   922
    {assume pa: "p^n dvd a"
chaieb@26125
   923
      then obtain k where k: "a = p^n * k" unfolding dvd_def by blast
chaieb@26125
   924
      from l have "l dvd c" by auto
chaieb@26125
   925
      with dvd_imp_le[of l c] c have "l \<le> c" by auto
chaieb@26125
   926
      moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
chaieb@26125
   927
      ultimately have lc: "l < c" by arith
chaieb@26125
   928
      from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" b]]]
chaieb@26125
   929
      have kb: "coprime k b" by (simp add: coprime_commute) 
chaieb@26125
   930
      from H(3) l k pn0 have kbln: "k * b = l ^ n" 
chaieb@26125
   931
	by (auto simp add: power_mult_distrib)
chaieb@26125
   932
      from H(1)[rule_format, OF lc kb kbln]
chaieb@26125
   933
      obtain r s where rs: "k = r ^n" "b = s^n" by blast
chaieb@26125
   934
      from k rs(1) have "a = (p*r)^n" by (simp add: power_mult_distrib)
chaieb@26125
   935
      with rs(2) have ?ths by blast }
chaieb@26125
   936
    moreover
chaieb@26125
   937
    {assume pb: "p^n dvd b"
chaieb@26125
   938
      then obtain k where k: "b = p^n * k" unfolding dvd_def by blast
chaieb@26125
   939
      from l have "l dvd c" by auto
chaieb@26125
   940
      with dvd_imp_le[of l c] c have "l \<le> c" by auto
chaieb@26125
   941
      moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
chaieb@26125
   942
      ultimately have lc: "l < c" by arith
chaieb@26125
   943
      from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" a]]]
chaieb@26125
   944
      have kb: "coprime k a" by (simp add: coprime_commute) 
chaieb@26125
   945
      from H(3) l k pn0 n have kbln: "k * a = l ^ n" 
chaieb@26125
   946
	by (simp add: power_mult_distrib mult_commute)
chaieb@26125
   947
      from H(1)[rule_format, OF lc kb kbln]
chaieb@26125
   948
      obtain r s where rs: "k = r ^n" "a = s^n" by blast
chaieb@26125
   949
      from k rs(1) have "b = (p*r)^n" by (simp add: power_mult_distrib)
chaieb@26125
   950
      with rs(2) have ?ths by blast }
chaieb@26125
   951
    ultimately have ?ths using pnab by blast}
chaieb@26125
   952
  ultimately have ?ths by blast}
chaieb@26125
   953
ultimately show ?ths by blast
chaieb@26125
   954
qed
chaieb@26125
   955
wenzelm@26144
   956
text {* More useful lemmas. *}
chaieb@26125
   957
lemma prime_product: 
haftmann@27651
   958
  assumes "prime (p * q)"
haftmann@27651
   959
  shows "p = 1 \<or> q = 1"
haftmann@27651
   960
proof -
haftmann@27651
   961
  from assms have 
haftmann@27651
   962
    "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
haftmann@27651
   963
    unfolding prime_def by auto
haftmann@27651
   964
  from `1 < p * q` have "p \<noteq> 0" by (cases p) auto
haftmann@27651
   965
  then have Q: "p = p * q \<longleftrightarrow> q = 1" by auto
haftmann@27651
   966
  have "p dvd p * q" by simp
haftmann@27651
   967
  then have "p = 1 \<or> p = p * q" by (rule P)
haftmann@27651
   968
  then show ?thesis by (simp add: Q)
haftmann@27651
   969
qed
chaieb@26125
   970
chaieb@26125
   971
lemma prime_exp: "prime (p^n) \<longleftrightarrow> prime p \<and> n = 1"
chaieb@26125
   972
proof(induct n)
chaieb@26125
   973
  case 0 thus ?case by simp
chaieb@26125
   974
next
chaieb@26125
   975
  case (Suc n)
chaieb@26125
   976
  {assume "p = 0" hence ?case by simp}
chaieb@26125
   977
  moreover
chaieb@26125
   978
  {assume "p=1" hence ?case by simp}
chaieb@26125
   979
  moreover
chaieb@26125
   980
  {assume p: "p \<noteq> 0" "p\<noteq>1"
chaieb@26125
   981
    {assume pp: "prime (p^Suc n)"
chaieb@26125
   982
      hence "p = 1 \<or> p^n = 1" using prime_product[of p "p^n"] by simp
chaieb@26125
   983
      with p have n: "n = 0" 
chaieb@26125
   984
	by (simp only: exp_eq_1 ) simp
chaieb@26125
   985
      with pp have "prime p \<and> Suc n = 1" by simp}
chaieb@26125
   986
    moreover
chaieb@26125
   987
    {assume n: "prime p \<and> Suc n = 1" hence "prime (p^Suc n)" by simp}
chaieb@26125
   988
    ultimately have ?case by blast}
chaieb@26125
   989
  ultimately show ?case by blast
chaieb@26125
   990
qed
chaieb@26125
   991
chaieb@26125
   992
lemma prime_power_mult: 
chaieb@26125
   993
  assumes p: "prime p" and xy: "x * y = p ^ k"
chaieb@26125
   994
  shows "\<exists>i j. x = p ^i \<and> y = p^ j"
chaieb@26125
   995
  using xy
chaieb@26125
   996
proof(induct k arbitrary: x y)
chaieb@26125
   997
  case 0 thus ?case apply simp by (rule exI[where x="0"], simp)
chaieb@26125
   998
next
chaieb@26125
   999
  case (Suc k x y)
chaieb@26125
  1000
  from Suc.prems have pxy: "p dvd x*y" by auto
chaieb@26125
  1001
  from prime_divprod[OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
chaieb@26125
  1002
  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp) 
chaieb@26125
  1003
  {assume px: "p dvd x"
chaieb@26125
  1004
    then obtain d where d: "x = p*d" unfolding dvd_def by blast
chaieb@26125
  1005
    from Suc.prems d  have "p*d*y = p^Suc k" by simp
chaieb@26125
  1006
    hence th: "d*y = p^k" using p0 by simp
chaieb@26125
  1007
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
chaieb@26125
  1008
    with d have "x = p^Suc i" by simp 
chaieb@26125
  1009
    with ij(2) have ?case by blast}
chaieb@26125
  1010
  moreover 
chaieb@26125
  1011
  {assume px: "p dvd y"
chaieb@26125
  1012
    then obtain d where d: "y = p*d" unfolding dvd_def by blast
chaieb@26125
  1013
    from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult_commute)
chaieb@26125
  1014
    hence th: "d*x = p^k" using p0 by simp
chaieb@26125
  1015
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
chaieb@26125
  1016
    with d have "y = p^Suc i" by simp 
chaieb@26125
  1017
    with ij(2) have ?case by blast}
chaieb@26125
  1018
  ultimately show ?case  using pxyc by blast
chaieb@26125
  1019
qed
chaieb@26125
  1020
chaieb@26125
  1021
lemma prime_power_exp: assumes p: "prime p" and n:"n \<noteq> 0" 
chaieb@26125
  1022
  and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
chaieb@26125
  1023
  using n xn
chaieb@26125
  1024
proof(induct n arbitrary: k)
chaieb@26125
  1025
  case 0 thus ?case by simp
chaieb@26125
  1026
next
chaieb@26125
  1027
  case (Suc n k) hence th: "x*x^n = p^k" by simp
chaieb@26125
  1028
  {assume "n = 0" with prems have ?case apply simp 
chaieb@26125
  1029
      by (rule exI[where x="k"],simp)}
chaieb@26125
  1030
  moreover
chaieb@26125
  1031
  {assume n: "n \<noteq> 0"
chaieb@26125
  1032
    from prime_power_mult[OF p th] 
chaieb@26125
  1033
    obtain i j where ij: "x = p^i" "x^n = p^j"by blast
chaieb@26125
  1034
    from Suc.hyps[OF n ij(2)] have ?case .}
chaieb@26125
  1035
  ultimately show ?case by blast
chaieb@26125
  1036
qed
chaieb@26125
  1037
chaieb@26125
  1038
lemma divides_primepow: assumes p: "prime p" 
chaieb@26125
  1039
  shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
chaieb@26125
  1040
proof
chaieb@26125
  1041
  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k" 
chaieb@26125
  1042
    unfolding dvd_def  apply (auto simp add: mult_commute) by blast
chaieb@26125
  1043
  from prime_power_mult[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
chaieb@26125
  1044
  from prime_ge_2[OF p] have p1: "p > 1" by arith
chaieb@26125
  1045
  from e ij have "p^(i + j) = p^k" by (simp add: power_add)
chaieb@26125
  1046
  hence "i + j = k" using power_inject_exp[of p "i+j" k, OF p1] by simp 
chaieb@26125
  1047
  hence "i \<le> k" by arith
chaieb@26125
  1048
  with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
chaieb@26125
  1049
next
chaieb@26125
  1050
  {fix i assume H: "i \<le> k" "d = p^i"
chaieb@26125
  1051
    hence "\<exists>j. k = i + j" by arith
chaieb@26125
  1052
    then obtain j where j: "k = i + j" by blast
chaieb@26125
  1053
    hence "p^k = p^j*d" using H(2) by (simp add: power_add)
chaieb@26125
  1054
    hence "d dvd p^k" unfolding dvd_def by auto}
chaieb@26125
  1055
  thus "\<exists>i\<le>k. d = p ^ i \<Longrightarrow> d dvd p ^ k" by blast
chaieb@26125
  1056
qed
chaieb@26125
  1057
chaieb@26125
  1058
lemma coprime_divisors: "d dvd a \<Longrightarrow> e dvd b \<Longrightarrow> coprime a b \<Longrightarrow> coprime d e"
chaieb@26125
  1059
  by (auto simp add: dvd_def coprime)
chaieb@26125
  1060
chaieb@26159
  1061
declare power_Suc0[simp del]
chaieb@26159
  1062
declare even_dvd[simp del]
haftmann@26757
  1063
paulson@11363
  1064
end