src/HOL/Number_Theory/Quadratic_Reciprocity.thy
changeset 64318 1e92b5c35615
parent 64282 261d42f0bfac
child 64593 50c715579715
equal deleted inserted replaced
64317:029e6247210e 64318:1e92b5c35615
       
     1 (* Author: Jaime Mendizabal Roche *)
       
     2 
       
     3 theory Quadratic_Reciprocity
       
     4 imports Gauss
       
     5 begin
       
     6 
       
     7 text {* The proof is based on Gauss's fifth proof, which can be found at http://www.lehigh.edu/~shw2/q-recip/gauss5.pdf *}
       
     8 
       
     9 locale QR =
       
    10   fixes p :: "nat"
       
    11   fixes q :: "nat"
       
    12 
       
    13   assumes p_prime: "prime p"
       
    14   assumes p_ge_2: "2 < p"
       
    15   assumes q_prime: "prime q"
       
    16   assumes q_ge_2: "2 < q"
       
    17   assumes pq_neq: "p \<noteq> q"
       
    18 begin
       
    19 
       
    20 lemma odd_p: "odd p" using p_ge_2 p_prime prime_odd_nat by blast
       
    21 
       
    22 lemma p_ge_0: "0 < int p"
       
    23   using p_prime not_prime_0[where 'a = nat] by fastforce+
       
    24 
       
    25 lemma p_eq2: "int p = (2 * ((int p - 1) div 2)) + 1" using odd_p by simp
       
    26 
       
    27 lemma odd_q: "odd q" using q_ge_2 q_prime prime_odd_nat by blast
       
    28 
       
    29 lemma q_ge_0: "0 < int q" using q_prime not_prime_0[where 'a = nat] by fastforce+
       
    30 
       
    31 lemma q_eq2: "int q = (2 * ((int q - 1) div 2)) + 1" using odd_q by simp
       
    32 
       
    33 lemma pq_eq2: "int p * int q = (2 * ((int p * int q - 1) div 2)) + 1" using odd_p odd_q by simp
       
    34 
       
    35 lemma pq_coprime: "coprime p q"
       
    36   using pq_neq p_prime primes_coprime_nat q_prime by blast
       
    37 
       
    38 lemma pq_coprime_int: "coprime (int p) (int q)"
       
    39   using pq_coprime transfer_int_nat_gcd(1) by presburger
       
    40 
       
    41 lemma qp_ineq: "(int p * k \<le> (int p * int q - 1) div 2) = (k \<le> (int q - 1) div 2)"
       
    42 proof -
       
    43   have "(2 * int p * k \<le> int p * int q - 1) = (2 * k \<le> int q - 1)" using p_ge_0 by auto
       
    44   thus ?thesis by auto
       
    45 qed
       
    46 
       
    47 lemma QRqp: "QR q p" using QR_def QR_axioms by simp
       
    48 
       
    49 lemma pq_commute: "int p * int q = int q * int p" by simp
       
    50 
       
    51 lemma pq_ge_0: "int p * int q > 0" using p_ge_0 q_ge_0 mult_pos_pos by blast
       
    52 
       
    53 definition "r = ((p - 1) div 2)*((q - 1) div 2)"
       
    54 definition "m = card (GAUSS.E p q)"
       
    55 definition "n = card (GAUSS.E q p)"
       
    56 
       
    57 abbreviation "Res (k::int) \<equiv> {0 .. k - 1}"
       
    58 abbreviation "Res_ge_0 (k::int) \<equiv> {0 <.. k - 1}"
       
    59 abbreviation "Res_0 (k::int) \<equiv> {0::int}"
       
    60 abbreviation "Res_l (k::int) \<equiv> {0 <.. (k - 1) div 2}"
       
    61 abbreviation "Res_h (k::int) \<equiv> {(k - 1) div 2 <.. k - 1}"
       
    62 
       
    63 abbreviation "Sets_pq r0 r1 r2 \<equiv>
       
    64   {(x::int). x \<in> r0 (int p * int q) \<and> x mod p \<in> r1 (int p) \<and> x mod q \<in> r2 (int q)}"
       
    65 
       
    66 definition "A = Sets_pq Res_l Res_l Res_h"
       
    67 definition "B = Sets_pq Res_l Res_h Res_l"
       
    68 definition "C = Sets_pq Res_h Res_h Res_l"
       
    69 definition "D = Sets_pq Res_l Res_h Res_h"
       
    70 definition "E = Sets_pq Res_l Res_0 Res_h"
       
    71 definition "F = Sets_pq Res_l Res_h Res_0"
       
    72 
       
    73 definition "a = card A"
       
    74 definition "b = card B"
       
    75 definition "c = card C"
       
    76 definition "d = card D"
       
    77 definition "e = card E"
       
    78 definition "f = card F"
       
    79 
       
    80 lemma Gpq: "GAUSS p q" unfolding GAUSS_def
       
    81   using p_prime pq_neq p_ge_2 q_prime
       
    82   by (auto simp: cong_altdef_int zdvd_int [symmetric] dest: primes_dvd_imp_eq) 
       
    83 
       
    84 lemma Gqp: "GAUSS q p" using QRqp QR.Gpq by simp
       
    85 
       
    86 lemma QR_lemma_01: "(\<lambda>x. x mod q) ` E = GAUSS.E q p"
       
    87 proof
       
    88     {
       
    89       fix x
       
    90       assume a1: "x \<in> E"
       
    91       then obtain k where k: "x = int p * k" unfolding E_def by blast
       
    92       have "x \<in> Res_l (int p * int q)" using a1 E_def by blast
       
    93       hence "k \<in> GAUSS.A q" using Gqp GAUSS.A_def k qp_ineq by (simp add: zero_less_mult_iff)
       
    94       hence "x mod q \<in> GAUSS.E q p"
       
    95         using GAUSS.C_def[of q p] Gqp k GAUSS.B_def[of q p] a1 GAUSS.E_def[of q p]
       
    96         unfolding E_def by force
       
    97       hence "x \<in> E \<longrightarrow> x mod int q \<in> GAUSS.E q p" by auto
       
    98     }
       
    99     thus "(\<lambda>x. x mod int q) ` E \<subseteq> GAUSS.E q p" by auto
       
   100 next
       
   101   show "GAUSS.E q p \<subseteq> (\<lambda>x. x mod q) ` E"
       
   102   proof
       
   103     fix x
       
   104     assume a1: "x \<in> GAUSS.E q p"
       
   105     then obtain ka where ka: "ka \<in> GAUSS.A q" "x = (ka * p) mod q"
       
   106       using Gqp GAUSS.B_def GAUSS.C_def GAUSS.E_def by auto
       
   107     hence "ka * p \<in> Res_l (int p * int q)"
       
   108       using GAUSS.A_def Gqp p_ge_0 qp_ineq by (simp add: Groups.mult_ac(2))
       
   109     thus "x \<in> (\<lambda>x. x mod q) ` E" unfolding E_def using ka a1 Gqp GAUSS.E_def q_ge_0 by force
       
   110   qed
       
   111 qed
       
   112 
       
   113 lemma QR_lemma_02: "e= n"
       
   114 proof -
       
   115   {
       
   116     fix x y
       
   117     assume a: "x \<in> E" "y \<in> E" "x mod q = y mod q"
       
   118     obtain p_inv where p_inv: "[int p * p_inv = 1] (mod int q)"
       
   119       using pq_coprime_int cong_solve_coprime_int by blast
       
   120     obtain kx ky where k: "x = int p * kx" "y = int p * ky" using a E_def dvd_def[of p x] by blast
       
   121     hence "0 < x" "int p * kx \<le> (int p * int q - 1) div 2"
       
   122         "0 < y" "int p * ky \<le> (int p * int q - 1) div 2"
       
   123       using E_def a greaterThanAtMost_iff mem_Collect_eq by blast+
       
   124     hence "0 \<le> kx" "kx < q" "0 \<le> ky" "ky < q" using qp_ineq k by (simp add: zero_less_mult_iff)+
       
   125     moreover have "(p_inv * (p * kx)) mod q = (p_inv * (p * ky)) mod q"
       
   126       using a(3) mod_mult_cong k by blast
       
   127     hence "(p * p_inv * kx) mod q = (p * p_inv * ky) mod q" by (simp add:algebra_simps)
       
   128     hence "kx mod q = ky mod q"
       
   129       using p_inv mod_mult_cong[of "p * p_inv" "q" "1"] cong_int_def by auto
       
   130     hence "[kx = ky] (mod q)" using cong_int_def by blast
       
   131     ultimately have "x = y" using cong_less_imp_eq_int k by blast
       
   132   }
       
   133   hence "inj_on (\<lambda>x. x mod q) E" unfolding inj_on_def by auto
       
   134   thus ?thesis using QR_lemma_01 card_image e_def n_def by fastforce
       
   135 qed
       
   136 
       
   137 lemma QR_lemma_03: "f = m"
       
   138 proof -
       
   139   have "F = QR.E q p" unfolding F_def pq_commute using QRqp QR.E_def[of q p] by fastforce
       
   140   hence "f = QR.e q p" unfolding f_def using QRqp QR.e_def[of q p] by presburger
       
   141   thus ?thesis using QRqp QR.QR_lemma_02 m_def QRqp QR.n_def by presburger
       
   142 qed
       
   143 
       
   144 definition f_1 :: "int \<Rightarrow> int \<times> int" where
       
   145   "f_1 x = ((x mod p), (x mod q))"
       
   146 
       
   147 definition P_1 :: "int \<times> int \<Rightarrow> int \<Rightarrow> bool" where
       
   148   "P_1 res x \<longleftrightarrow> x mod p = fst res & x mod q = snd res & x \<in> Res (int p * int q)"
       
   149 
       
   150 definition g_1 :: "int \<times> int \<Rightarrow> int" where
       
   151   "g_1 res = (THE x. P_1 res x)"
       
   152 
       
   153 lemma P_1_lemma: assumes "0 \<le> fst res" "fst res < p" "0 \<le> snd res" "snd res < q"
       
   154   shows "\<exists>! x. P_1 res x"
       
   155 proof -
       
   156   obtain y k1 k2 where yk: "y = nat (fst res) + k1 * p" "y = nat (snd res) + k2 * q"
       
   157     using chinese_remainder[of p q] pq_coprime p_ge_0 q_ge_0 by fastforce
       
   158   have h1: "[y = fst res] (mod p)" "[y = snd res] (mod q)"
       
   159     using yk(1) assms(1) cong_iff_lin_int[of "fst res"] cong_sym_int apply simp
       
   160     using yk(2) assms(3) cong_iff_lin_int[of "snd res"] cong_sym_int by simp
       
   161   have "(y mod (int p * int q)) mod int p = fst res" "(y mod (int p * int q)) mod int q = snd res"
       
   162     using h1(1) mod_mod_cancel[of "int p"] assms(1) assms(2) cong_int_def apply simp
       
   163     using h1(2) mod_mod_cancel[of "int q"] assms(3) assms(4) cong_int_def by simp
       
   164   then obtain x where "P_1 res x" unfolding P_1_def
       
   165     using Divides.pos_mod_bound Divides.pos_mod_sign pq_ge_0 by fastforce
       
   166   moreover {
       
   167     fix a b
       
   168     assume a: "P_1 res a" "P_1 res b"
       
   169     hence "int p * int q dvd a - b"
       
   170       using divides_mult[of "int p" "a - b" "int q"] pq_coprime_int zmod_eq_dvd_iff[of a _ b]
       
   171       unfolding P_1_def by force
       
   172     hence "a = b" using dvd_imp_le_int[of "a - b"] a unfolding P_1_def by fastforce
       
   173   }
       
   174   ultimately show ?thesis by auto
       
   175 qed
       
   176 
       
   177 lemma g_1_lemma: assumes "0 \<le> fst res" "fst res < p" "0 \<le> snd res" "snd res < q"
       
   178   shows "P_1 res (g_1 res)" using assms P_1_lemma theI'[of "P_1 res"] g_1_def by presburger
       
   179 
       
   180 definition "BuC = Sets_pq Res_ge_0 Res_h Res_l"
       
   181 
       
   182 lemma QR_lemma_04: "card BuC = card ((Res_h p) \<times> (Res_l q))"
       
   183   using card_bij_eq[of f_1 "BuC" "(Res_h p) \<times> (Res_l q)" g_1]
       
   184 proof
       
   185   {
       
   186     fix x y
       
   187     assume a: "x \<in> BuC" "y \<in> BuC" "f_1 x = f_1 y"
       
   188     hence "int p * int q dvd x - y"
       
   189       using f_1_def pq_coprime_int divides_mult[of "int p" "x - y" "int q"] 
       
   190             zmod_eq_dvd_iff[of x _ y] by auto
       
   191     hence "x = y"
       
   192       using dvd_imp_le_int[of "x - y" "int p * int q"] a unfolding BuC_def by force
       
   193   }
       
   194   thus "inj_on f_1 BuC" unfolding inj_on_def by auto
       
   195 next
       
   196   {
       
   197     fix x y
       
   198     assume a: "x \<in> (Res_h p) \<times> (Res_l q)" "y \<in> (Res_h p) \<times> (Res_l q)" "g_1 x = g_1 y"
       
   199     hence "0 \<le> fst x" "fst x < p" "0 \<le> snd x" "snd x < q"
       
   200         "0 \<le> fst y" "fst y < p" "0 \<le> snd y" "snd y < q"
       
   201       using mem_Sigma_iff prod.collapse by fastforce+
       
   202     hence "x = y" using g_1_lemma[of x] g_1_lemma[of y] a P_1_def by fastforce
       
   203   }
       
   204   thus "inj_on g_1 ((Res_h p) \<times> (Res_l q))" unfolding inj_on_def by auto
       
   205 next
       
   206   show "g_1 ` ((Res_h p) \<times> (Res_l q)) \<subseteq> BuC"
       
   207   proof
       
   208     fix y
       
   209     assume "y \<in> g_1 ` ((Res_h p) \<times> (Res_l q))"
       
   210     then obtain x where x: "y = g_1 x" "x \<in> ((Res_h p) \<times> (Res_l q))" by blast
       
   211     hence "P_1 x y" using g_1_lemma by fastforce
       
   212     thus "y \<in> BuC" unfolding P_1_def BuC_def mem_Collect_eq using x SigmaE prod.sel by fastforce
       
   213   qed
       
   214 qed (auto simp: BuC_def finite_subset f_1_def)
       
   215 
       
   216 lemma QR_lemma_05: "card ((Res_h p) \<times> (Res_l q)) = r"
       
   217 proof -
       
   218   have "card (Res_l q) = (q - 1) div 2" "card (Res_h p) = (p - 1) div 2" using p_eq2 by force+
       
   219   thus ?thesis unfolding r_def using card_cartesian_product[of "Res_h p" "Res_l q"] by presburger
       
   220 qed
       
   221 
       
   222 lemma QR_lemma_06: "b + c = r"
       
   223 proof -
       
   224   have "B \<inter> C = {}" "finite B" "finite C" "B \<union> C = BuC" unfolding B_def C_def BuC_def by fastforce+
       
   225   thus ?thesis
       
   226     unfolding b_def c_def using card_empty card_Un_Int QR_lemma_04 QR_lemma_05 by fastforce
       
   227 qed
       
   228 
       
   229 definition f_2:: "int \<Rightarrow> int" where
       
   230   "f_2 x = (int p * int q) - x"
       
   231 
       
   232 lemma f_2_lemma_1: "\<And>x. f_2 (f_2 x) = x" unfolding f_2_def by simp
       
   233 
       
   234 lemma f_2_lemma_2: "[f_2 x = int p - x] (mod p)" unfolding f_2_def using cong_altdef_int by simp
       
   235 
       
   236 lemma f_2_lemma_3: "f_2 x \<in> S \<Longrightarrow> x \<in> f_2 ` S"
       
   237   using f_2_lemma_1[of x] image_eqI[of x f_2 "f_2 x" S] by presburger
       
   238 
       
   239 lemma QR_lemma_07: "f_2 ` Res_l (int p * int q) = Res_h (int p * int q)"
       
   240     "f_2 ` Res_h (int p * int q) = Res_l (int p * int q)"
       
   241 proof -
       
   242   have h1: "f_2 ` Res_l (int p * int q) \<subseteq> Res_h (int p * int q)" using f_2_def by force
       
   243   have h2: "f_2 ` Res_h (int p * int q) \<subseteq> Res_l (int p * int q)" using f_2_def pq_eq2 by fastforce
       
   244   have h3: "Res_h (int p * int q) \<subseteq> f_2 ` Res_l (int p * int q)" using h2 f_2_lemma_3 by blast
       
   245   have h4: "Res_l (int p * int q) \<subseteq> f_2 ` Res_h (int p * int q)" using h1 f_2_lemma_3 by blast
       
   246   show "f_2 ` Res_l (int p * int q) = Res_h (int p * int q)" using h1 h3 by blast
       
   247   show "f_2 ` Res_h (int p * int q) = Res_l (int p * int q)" using h2 h4 by blast
       
   248 qed
       
   249 
       
   250 lemma QR_lemma_08: "(f_2 x mod p \<in> Res_l p) = (x mod p \<in> Res_h p)"
       
   251     "(f_2 x mod p \<in> Res_h p) = (x mod p \<in> Res_l p)"
       
   252   using f_2_lemma_2[of x] cong_int_def[of "f_2 x" "p - x" p] minus_mod_self2[of x p]
       
   253   zmod_zminus1_eq_if[of x p] p_eq2 by auto
       
   254 
       
   255 lemma QR_lemma_09: "(f_2 x mod q \<in> Res_l q) = (x mod q \<in> Res_h q)"
       
   256     "(f_2 x mod q \<in> Res_h q) = (x mod q \<in> Res_l q)"
       
   257   using QRqp QR.QR_lemma_08 f_2_def QR.f_2_def pq_commute by auto+
       
   258 
       
   259 lemma QR_lemma_10: "a = c" unfolding a_def c_def apply (rule card_bij_eq[of f_2 A C f_2])
       
   260   unfolding A_def C_def
       
   261   using QR_lemma_07 QR_lemma_08 QR_lemma_09 apply ((simp add: inj_on_def f_2_def),blast)+
       
   262   by fastforce+
       
   263 
       
   264 definition "BuD = Sets_pq Res_l Res_h Res_ge_0"
       
   265 definition "BuDuF = Sets_pq Res_l Res_h Res"
       
   266 
       
   267 definition f_3 :: "int \<Rightarrow> int \<times> int" where
       
   268   "f_3 x = (x mod p, x div p + 1)"
       
   269 
       
   270 definition g_3 :: "int \<times> int \<Rightarrow> int" where
       
   271   "g_3 x = fst x + (snd x - 1) * p"
       
   272 
       
   273 lemma QR_lemma_11: "card BuDuF = card ((Res_h p) \<times> (Res_l q))"
       
   274   using card_bij_eq[of f_3 BuDuF "(Res_h p) \<times> (Res_l q)" g_3]
       
   275 proof
       
   276   show "f_3 ` BuDuF \<subseteq> (Res_h p) \<times> (Res_l q)"
       
   277   proof
       
   278     fix y
       
   279     assume "y \<in> f_3 ` BuDuF"
       
   280     then obtain x where x: "y = f_3 x" "x \<in> BuDuF" by blast
       
   281     hence "x \<le> int p * (int q - 1) div 2 + (int p - 1) div 2"
       
   282       unfolding BuDuF_def using p_eq2 int_distrib(4) by auto
       
   283     moreover have "(int p - 1) div 2 \<le> - 1 + x mod p" using x BuDuF_def by auto
       
   284     moreover have "int p * (int q - 1) div 2 = int p * ((int q - 1) div 2)"
       
   285       using zdiv_zmult1_eq odd_q by auto
       
   286     hence "p * (int q - 1) div 2 = p * ((int q + 1) div 2 - 1)" by fastforce
       
   287     ultimately have "x \<le> p * ((int q + 1) div 2 - 1) - 1 + x mod p" by linarith
       
   288     hence "x div p < (int q + 1) div 2 - 1"
       
   289       using mult.commute[of "int p" "x div p"] p_ge_0 div_mult_mod_eq[of x p]
       
   290         mult_less_cancel_left_pos[of p "x div p" "(int q + 1) div 2 - 1"] by linarith
       
   291     moreover have "0 < x div p + 1"
       
   292       using pos_imp_zdiv_neg_iff[of p x] p_ge_0 x mem_Collect_eq BuDuF_def by auto
       
   293     ultimately show "y \<in> (Res_h p) \<times> (Res_l q)" using x BuDuF_def f_3_def by auto
       
   294   qed
       
   295 next
       
   296   have h1: "\<And>x. x \<in> ((Res_h p) \<times> (Res_l q)) \<Longrightarrow> f_3 (g_3 x) = x"
       
   297   proof -
       
   298     fix x
       
   299     assume a: "x \<in> ((Res_h p) \<times> (Res_l q))"
       
   300     moreover have h: "(fst x + (snd x - 1) * int p) mod int p = fst x" using a by force
       
   301     ultimately have "(fst x + (snd x - 1) * int p) div int p + 1 = snd x"
       
   302       by (auto simp: semiring_numeral_div_class.div_less)
       
   303     with h show "f_3 (g_3 x) = x" unfolding f_3_def g_3_def by simp
       
   304   qed
       
   305   show "inj_on g_3 ((Res_h p) \<times> (Res_l q))" apply (rule inj_onI[of "(Res_h p) \<times> (Res_l q)" g_3])
       
   306   proof -
       
   307     fix x y
       
   308     assume "x \<in> ((Res_h p) \<times> (Res_l q))" "y \<in> ((Res_h p) \<times> (Res_l q))" "g_3 x = g_3 y"
       
   309     thus "x = y" using h1[of x] h1[of y] by presburger
       
   310   qed
       
   311 next
       
   312   show "g_3 ` ((Res_h p) \<times> (Res_l q)) \<subseteq> BuDuF"
       
   313   proof
       
   314     fix y
       
   315     assume "y \<in> g_3 ` ((Res_h p) \<times> (Res_l q))"
       
   316     then obtain x where x: "y = g_3 x" "x \<in> (Res_h p) \<times> (Res_l q)" by blast
       
   317     hence "snd x \<le> (int q - 1) div 2" by force
       
   318     moreover have "int p * ((int q - 1) div 2) = (int p * int q - int p) div 2"
       
   319       using int_distrib(4) zdiv_zmult1_eq[of "int p" "int q - 1" 2] odd_q by fastforce
       
   320     ultimately have "(snd x) * int p \<le> (int q * int p - int p) div 2"
       
   321       using mult_right_mono[of "snd x" "(int q - 1) div 2" p] mult.commute[of "(int q - 1) div 2" p]
       
   322         pq_commute by presburger
       
   323     hence "(snd x - 1) * int p \<le> (int q * int p - 1) div 2 - int p"
       
   324       using p_ge_0 int_distrib(3) by auto
       
   325     moreover have "fst x \<le> int p - 1" using x by force
       
   326     ultimately have "fst x + (snd x - 1) * int p \<le> (int p * int q - 1) div 2"
       
   327       using pq_commute by linarith
       
   328     moreover have "0 < fst x" "0 \<le> (snd x - 1) * p" using x(2) by fastforce+
       
   329     ultimately show "y \<in> BuDuF" unfolding BuDuF_def using q_ge_0 x g_3_def x(1) by auto
       
   330   qed
       
   331 next
       
   332   show "finite BuDuF" unfolding BuDuF_def by fastforce
       
   333 qed (simp add: inj_on_inverseI[of BuDuF g_3] f_3_def g_3_def QR_lemma_05)+
       
   334 
       
   335 lemma QR_lemma_12: "b + d + m = r"
       
   336 proof -
       
   337   have "B \<inter> D = {}" "finite B" "finite D" "B \<union> D = BuD" unfolding B_def D_def BuD_def by fastforce+
       
   338   hence "b + d = card BuD" unfolding b_def d_def using card_Un_Int by fastforce
       
   339   moreover have "BuD \<inter> F = {}" "finite BuD" "finite F" unfolding BuD_def F_def by fastforce+
       
   340   moreover have "BuD \<union> F = BuDuF" unfolding BuD_def F_def BuDuF_def
       
   341     using q_ge_0 ivl_disj_un_singleton(5)[of 0 "int q - 1"] by auto
       
   342   ultimately show ?thesis using QR_lemma_03 QR_lemma_05 QR_lemma_11 card_Un_disjoint[of BuD F]
       
   343     unfolding b_def d_def f_def by presburger
       
   344 qed
       
   345 
       
   346 lemma QR_lemma_13: "a + d + n = r"
       
   347 proof -
       
   348   have "A = QR.B q p" unfolding A_def pq_commute using QRqp QR.B_def[of q p] by blast
       
   349   hence "a = QR.b q p" using a_def QRqp QR.b_def[of q p] by presburger
       
   350   moreover have "D = QR.D q p" unfolding D_def pq_commute using QRqp QR.D_def[of q p] by blast
       
   351     hence "d = QR.d q p" using d_def  QRqp QR.d_def[of q p] by presburger
       
   352   moreover have "n = QR.m q p" using n_def QRqp QR.m_def[of q p] by presburger
       
   353   moreover have "r = QR.r q p" unfolding r_def using QRqp QR.r_def[of q p] by auto
       
   354   ultimately show ?thesis using QRqp QR.QR_lemma_12 by presburger
       
   355 qed
       
   356 
       
   357 lemma QR_lemma_14: "(-1::int) ^ (m + n) = (-1) ^ r"
       
   358 proof -
       
   359   have "m + n + 2 * d = r" using QR_lemma_06 QR_lemma_10 QR_lemma_12 QR_lemma_13 by auto
       
   360   thus ?thesis using power_add[of "-1::int" "m + n" "2 * d"] by fastforce
       
   361 qed
       
   362 
       
   363 lemma Quadratic_Reciprocity:
       
   364     "(Legendre p q) * (Legendre q p) = (-1::int) ^ ((p - 1) div 2 * ((q - 1) div 2))"
       
   365   using Gpq Gqp GAUSS.gauss_lemma power_add[of "-1::int" m n] QR_lemma_14
       
   366   unfolding r_def m_def n_def by auto
       
   367 
       
   368 end
       
   369 
       
   370 theorem Quadratic_Reciprocity: assumes "prime p" "2 < p" "prime q" "2 < q" "p \<noteq> q"
       
   371   shows "(Legendre p q) * (Legendre q p) = (-1::int) ^ ((p - 1) div 2 * ((q - 1) div 2))"
       
   372   using QR.Quadratic_Reciprocity QR_def assms by blast
       
   373 
       
   374 theorem Quadratic_Reciprocity_int: assumes "prime (nat p)" "2 < p" "prime (nat q)" "2 < q" "p \<noteq> q"
       
   375   shows "(Legendre p q) * (Legendre q p) = (-1::int) ^ (nat ((p - 1) div 2 * ((q - 1) div 2)))"
       
   376 proof -
       
   377   have "0 \<le> (p - 1) div 2" using assms by simp
       
   378   moreover have "(nat p - 1) div 2 = nat ((p - 1) div 2)" "(nat q - 1) div 2 = nat ((q - 1) div 2)"
       
   379     by fastforce+
       
   380   ultimately have "(nat p - 1) div 2 * ((nat q - 1) div 2) = nat ((p - 1) div 2 * ((q - 1) div 2))"
       
   381     using nat_mult_distrib by presburger
       
   382   moreover have "2 < nat p" "2 < nat q" "nat p \<noteq> nat q" "int (nat p) = p" "int (nat q) = q"
       
   383     using assms by linarith+
       
   384   ultimately show ?thesis using Quadratic_Reciprocity[of "nat p" "nat q"] assms by presburger
       
   385 qed
       
   386 
       
   387 end