
wenzelm@38159: (*  Title:      HOL/Old_Number_Theory/Gauss.thy
wenzelm@38159:     Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871: *)
paulson@13871: 
wenzelm@58889: section {* Gauss' Lemma *}
paulson@13871: 
haftmann@27556: theory Gauss
haftmann@27556: imports Euler
haftmann@27556: begin
paulson@13871: 
paulson@13871: locale GAUSS =
paulson@13871:   fixes p :: "int"
paulson@13871:   fixes a :: "int"
paulson@13871: 
nipkow@16663:   assumes p_prime: "zprime p"
paulson@13871:   assumes p_g_2: "2 < p"
paulson@13871:   assumes p_a_relprime: "~[a = 0](mod p)"
paulson@13871:   assumes a_nonzero:    "0 < a"
wenzelm@21233: begin
paulson@13871: 
wenzelm@38159: definition "A = {(x::int). 0 < x & x \<le> ((p - 1) div 2)}"
wenzelm@38159: definition "B = (%x. x * a) ` A"
wenzelm@38159: definition "C = StandardRes p ` B"
wenzelm@38159: definition "D = C \<inter> {x. x \<le> ((p - 1) div 2)}"
wenzelm@38159: definition "E = C \<inter> {x. ((p - 1) div 2) < x}"
wenzelm@38159: definition "F = (%x. (p - x)) ` E"
wenzelm@21233: 
paulson@13871: 
paulson@13871: subsection {* Basic properties of p *}
paulson@13871: 
wenzelm@21233: lemma p_odd: "p \<in> zOdd"
paulson@13871:   by (auto simp add: p_prime p_g_2 zprime_zOdd_eq_grt_2)
paulson@13871: 
wenzelm@21233: lemma p_g_0: "0 < p"
wenzelm@18369:   using p_g_2 by auto
paulson@13871: 
wenzelm@21233: lemma int_nat: "int (nat ((p - 1) div 2)) = (p - 1) div 2"
wenzelm@26289:   using ListMem.insert p_g_2 by (auto simp add: pos_imp_zdiv_nonneg_iff)
paulson@13871: 
wenzelm@21233: lemma p_minus_one_l: "(p - 1) div 2 < p"
wenzelm@18369: proof -
wenzelm@18369:   have "(p - 1) div 2 \<le> (p - 1) div 1"
wenzelm@18369:     by (rule zdiv_mono2) (auto simp add: p_g_0)
wenzelm@18369:   also have "\<dots> = p - 1" by simp
wenzelm@18369:   finally show ?thesis by simp
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma p_eq: "p = (2 * (p - 1) div 2) + 1"
nipkow@30034:   using div_mult_self1_is_id [of 2 "p - 1"] by auto
paulson@13871: 
wenzelm@21233: 
wenzelm@21288: lemma (in -) zodd_imp_zdiv_eq: "x \<in> zOdd ==> 2 * (x - 1) div 2 = 2 * ((x - 1) div 2)"
paulson@13871:   apply (frule odd_minus_one_even)
paulson@13871:   apply (simp add: zEven_def)
paulson@13871:   apply (subgoal_tac "2 \<noteq> 0")
nipkow@30034:   apply (frule_tac b = "2 :: int" and a = "x - 1" in div_mult_self1_is_id)
wenzelm@18369:   apply (auto simp add: even_div_2_prop2)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: 
wenzelm@21233: lemma p_eq2: "p = (2 * ((p - 1) div 2)) + 1"
paulson@13871:   apply (insert p_eq p_prime p_g_2 zprime_zOdd_eq_grt_2 [of p], auto)
wenzelm@18369:   apply (frule zodd_imp_zdiv_eq, auto)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: 
paulson@13871: subsection {* Basic Properties of the Gauss Sets *}
paulson@13871: 
wenzelm@21233: lemma finite_A: "finite (A)"
bulwahn@46756: by (auto simp add: A_def)
paulson@13871: 
wenzelm@21233: lemma finite_B: "finite (B)"
nipkow@40786: by (auto simp add: B_def finite_A)
paulson@13871: 
wenzelm@21233: lemma finite_C: "finite (C)"
nipkow@40786: by (auto simp add: C_def finite_B)
paulson@13871: 
wenzelm@21233: lemma finite_D: "finite (D)"
wenzelm@41541: by (auto simp add: D_def finite_C)
paulson@13871: 
wenzelm@21233: lemma finite_E: "finite (E)"
wenzelm@41541: by (auto simp add: E_def finite_C)
paulson@13871: 
wenzelm@21233: lemma finite_F: "finite (F)"
nipkow@40786: by (auto simp add: F_def finite_E)
paulson@13871: 
wenzelm@21233: lemma C_eq: "C = D \<union> E"
nipkow@40786: by (auto simp add: C_def D_def E_def)
paulson@13871: 
wenzelm@21233: lemma A_card_eq: "card A = nat ((p - 1) div 2)"
wenzelm@18369:   apply (auto simp add: A_def)
paulson@13871:   apply (insert int_nat)
paulson@13871:   apply (erule subst)
wenzelm@18369:   apply (auto simp add: card_bdd_int_set_l_le)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma inj_on_xa_A: "inj_on (%x. x * a) A"
wenzelm@18369:   using a_nonzero by (simp add: A_def inj_on_def)
paulson@13871: 
wenzelm@21233: lemma A_res: "ResSet p A"
wenzelm@18369:   apply (auto simp add: A_def ResSet_def)
wenzelm@18369:   apply (rule_tac m = p in zcong_less_eq)
wenzelm@18369:   apply (insert p_g_2, auto)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma B_res: "ResSet p B"
paulson@13871:   apply (insert p_g_2 p_a_relprime p_minus_one_l)
wenzelm@18369:   apply (auto simp add: B_def)
paulson@13871:   apply (rule ResSet_image)
wenzelm@18369:   apply (auto simp add: A_res)
paulson@13871:   apply (auto simp add: A_def)
wenzelm@18369: proof -
wenzelm@18369:   fix x fix y
wenzelm@18369:   assume a: "[x * a = y * a] (mod p)"
wenzelm@18369:   assume b: "0 < x"
wenzelm@18369:   assume c: "x \<le> (p - 1) div 2"
wenzelm@18369:   assume d: "0 < y"
wenzelm@18369:   assume e: "y \<le> (p - 1) div 2"
wenzelm@18369:   from a p_a_relprime p_prime a_nonzero zcong_cancel [of p a x y]
wenzelm@18369:   have "[x = y](mod p)"
wenzelm@18369:     by (simp add: zprime_imp_zrelprime zcong_def p_g_0 order_le_less)
wenzelm@18369:   with zcong_less_eq [of x y p] p_minus_one_l
wenzelm@18369:       order_le_less_trans [of x "(p - 1) div 2" p]
wenzelm@18369:       order_le_less_trans [of y "(p - 1) div 2" p] show "x = y"
wenzelm@41541:     by (simp add: b c d e p_minus_one_l p_g_0)
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma SR_B_inj: "inj_on (StandardRes p) B"
wenzelm@41541:   apply (auto simp add: B_def StandardRes_def inj_on_def A_def)
wenzelm@18369: proof -
wenzelm@18369:   fix x fix y
wenzelm@18369:   assume a: "x * a mod p = y * a mod p"
wenzelm@18369:   assume b: "0 < x"
wenzelm@18369:   assume c: "x \<le> (p - 1) div 2"
wenzelm@18369:   assume d: "0 < y"
wenzelm@18369:   assume e: "y \<le> (p - 1) div 2"
wenzelm@18369:   assume f: "x \<noteq> y"
wenzelm@18369:   from a have "[x * a = y * a](mod p)"
wenzelm@18369:     by (simp add: zcong_zmod_eq p_g_0)
wenzelm@18369:   with p_a_relprime p_prime a_nonzero zcong_cancel [of p a x y]
wenzelm@18369:   have "[x = y](mod p)"
wenzelm@18369:     by (simp add: zprime_imp_zrelprime zcong_def p_g_0 order_le_less)
wenzelm@18369:   with zcong_less_eq [of x y p] p_minus_one_l
wenzelm@18369:     order_le_less_trans [of x "(p - 1) div 2" p]
wenzelm@18369:     order_le_less_trans [of y "(p - 1) div 2" p] have "x = y"
wenzelm@41541:     by (simp add: b c d e p_minus_one_l p_g_0)
wenzelm@18369:   then have False
wenzelm@18369:     by (simp add: f)
wenzelm@18369:   then show "a = 0"
wenzelm@18369:     by simp
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma inj_on_pminusx_E: "inj_on (%x. p - x) E"
paulson@13871:   apply (auto simp add: E_def C_def B_def A_def)
wenzelm@18369:   apply (rule_tac g = "%x. -1 * (x - p)" in inj_on_inverseI)
wenzelm@18369:   apply auto
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma A_ncong_p: "x \<in> A ==> ~[x = 0](mod p)"
paulson@13871:   apply (auto simp add: A_def)
paulson@13871:   apply (frule_tac m = p in zcong_not_zero)
paulson@13871:   apply (insert p_minus_one_l)
wenzelm@18369:   apply auto
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma A_greater_zero: "x \<in> A ==> 0 < x"
paulson@13871:   by (auto simp add: A_def)
paulson@13871: 
wenzelm@21233: lemma B_ncong_p: "x \<in> B ==> ~[x = 0](mod p)"
paulson@13871:   apply (auto simp add: B_def)
wenzelm@18369:   apply (frule A_ncong_p)
paulson@13871:   apply (insert p_a_relprime p_prime a_nonzero)
thomas@57492:   apply (frule_tac a = xa and b = a in zcong_zprime_prod_zero_contra)
wenzelm@18369:   apply (auto simp add: A_greater_zero)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma B_greater_zero: "x \<in> B ==> 0 < x"
nipkow@56544:   using a_nonzero by (auto simp add: B_def A_greater_zero)
paulson@13871: 
wenzelm@21233: lemma C_ncong_p: "x \<in> C ==>  ~[x = 0](mod p)"
paulson@13871:   apply (auto simp add: C_def)
paulson@13871:   apply (frule B_ncong_p)
thomas@57492:   apply (subgoal_tac "[xa = StandardRes p xa](mod p)")
wenzelm@18369:   defer apply (simp add: StandardRes_prop1)
thomas@57492:   apply (frule_tac a = xa and b = "StandardRes p xa" and c = 0 in zcong_trans)
wenzelm@18369:   apply auto
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma C_greater_zero: "y \<in> C ==> 0 < y"
paulson@13871:   apply (auto simp add: C_def)
wenzelm@18369: proof -
wenzelm@18369:   fix x
wenzelm@18369:   assume a: "x \<in> B"
wenzelm@18369:   from p_g_0 have "0 \<le> StandardRes p x"
wenzelm@18369:     by (simp add: StandardRes_lbound)
wenzelm@18369:   moreover have "~[x = 0] (mod p)"
wenzelm@18369:     by (simp add: a B_ncong_p)
wenzelm@18369:   then have "StandardRes p x \<noteq> 0"
wenzelm@18369:     by (simp add: StandardRes_prop3)
wenzelm@18369:   ultimately show "0 < StandardRes p x"
wenzelm@18369:     by (simp add: order_le_less)
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma D_ncong_p: "x \<in> D ==> ~[x = 0](mod p)"
paulson@13871:   by (auto simp add: D_def C_ncong_p)
paulson@13871: 
wenzelm@21233: lemma E_ncong_p: "x \<in> E ==> ~[x = 0](mod p)"
paulson@13871:   by (auto simp add: E_def C_ncong_p)
paulson@13871: 
wenzelm@21233: lemma F_ncong_p: "x \<in> F ==> ~[x = 0](mod p)"
wenzelm@18369:   apply (auto simp add: F_def)
wenzelm@18369: proof -
wenzelm@18369:   fix x assume a: "x \<in> E" assume b: "[p - x = 0] (mod p)"
wenzelm@18369:   from E_ncong_p have "~[x = 0] (mod p)"
wenzelm@18369:     by (simp add: a)
wenzelm@18369:   moreover from a have "0 < x"
wenzelm@18369:     by (simp add: a E_def C_greater_zero)
wenzelm@18369:   moreover from a have "x < p"
wenzelm@18369:     by (auto simp add: E_def C_def p_g_0 StandardRes_ubound)
wenzelm@18369:   ultimately have "~[p - x = 0] (mod p)"
wenzelm@18369:     by (simp add: zcong_not_zero)
wenzelm@18369:   from this show False by (simp add: b)
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma F_subset: "F \<subseteq> {x. 0 < x & x \<le> ((p - 1) div 2)}"
wenzelm@18369:   apply (auto simp add: F_def E_def)
paulson@13871:   apply (insert p_g_0)
paulson@13871:   apply (frule_tac x = xa in StandardRes_ubound)
paulson@13871:   apply (frule_tac x = x in StandardRes_ubound)
paulson@13871:   apply (subgoal_tac "xa = StandardRes p xa")
paulson@13871:   apply (auto simp add: C_def StandardRes_prop2 StandardRes_prop1)
wenzelm@18369: proof -
wenzelm@18369:   from zodd_imp_zdiv_eq p_prime p_g_2 zprime_zOdd_eq_grt_2 have
wenzelm@18369:     "2 * (p - 1) div 2 = 2 * ((p - 1) div 2)"
wenzelm@18369:     by simp
wenzelm@18369:   with p_eq2 show " !!x. [| (p - 1) div 2 < StandardRes p x; x \<in> B |]
wenzelm@18369:       ==> p - StandardRes p x \<le> (p - 1) div 2"
wenzelm@18369:     by simp
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma D_subset: "D \<subseteq> {x. 0 < x & x \<le> ((p - 1) div 2)}"
paulson@13871:   by (auto simp add: D_def C_greater_zero)
paulson@13871: 
wenzelm@21233: lemma F_eq: "F = {x. \<exists>y \<in> A. ( x = p - (StandardRes p (y*a)) & (p - 1) div 2 < StandardRes p (y*a))}"
paulson@13871:   by (auto simp add: F_def E_def D_def C_def B_def A_def)
paulson@13871: 
wenzelm@21233: lemma D_eq: "D = {x. \<exists>y \<in> A. ( x = StandardRes p (y*a) & StandardRes p (y*a) \<le> (p - 1) div 2)}"
paulson@13871:   by (auto simp add: D_def C_def B_def A_def)
paulson@13871: 
wenzelm@21233: lemma D_leq: "x \<in> D ==> x \<le> (p - 1) div 2"
paulson@13871:   by (auto simp add: D_eq)
paulson@13871: 
wenzelm@21233: lemma F_ge: "x \<in> F ==> x \<le> (p - 1) div 2"
paulson@13871:   apply (auto simp add: F_eq A_def)
wenzelm@18369: proof -
wenzelm@18369:   fix y
wenzelm@18369:   assume "(p - 1) div 2 < StandardRes p (y * a)"
wenzelm@18369:   then have "p - StandardRes p (y * a) < p - ((p - 1) div 2)"
wenzelm@18369:     by arith
wenzelm@18369:   also from p_eq2 have "... = 2 * ((p - 1) div 2) + 1 - ((p - 1) div 2)"
wenzelm@18369:     by auto
wenzelm@18369:   also have "2 * ((p - 1) div 2) + 1 - (p - 1) div 2 = (p - 1) div 2 + 1"
wenzelm@18369:     by arith
wenzelm@18369:   finally show "p - StandardRes p (y * a) \<le> (p - 1) div 2"
wenzelm@18369:     using zless_add1_eq [of "p - StandardRes p (y * a)" "(p - 1) div 2"] by auto
wenzelm@18369: qed
paulson@13871: 
haftmann@27556: lemma all_A_relprime: "\<forall>x \<in> A. zgcd x p = 1"
wenzelm@18369:   using p_prime p_minus_one_l by (auto simp add: A_def zless_zprime_imp_zrelprime)
paulson@13871: 
haftmann@27556: lemma A_prod_relprime: "zgcd (setprod id A) p = 1"
nipkow@30837: by(rule all_relprime_prod_relprime[OF finite_A all_A_relprime])
paulson@13871: 
wenzelm@21233: 
paulson@13871: subsection {* Relationships Between Gauss Sets *}
paulson@13871: 
wenzelm@21233: lemma B_card_eq_A: "card B = card A"
wenzelm@18369:   using finite_A by (simp add: finite_A B_def inj_on_xa_A card_image)
paulson@13871: 
wenzelm@21233: lemma B_card_eq: "card B = nat ((p - 1) div 2)"
wenzelm@18369:   by (simp add: B_card_eq_A A_card_eq)
paulson@13871: 
wenzelm@21233: lemma F_card_eq_E: "card F = card E"
wenzelm@18369:   using finite_E by (simp add: F_def inj_on_pminusx_E card_image)
paulson@13871: 
wenzelm@21233: lemma C_card_eq_B: "card C = card B"
paulson@13871:   apply (insert finite_B)
wenzelm@18369:   apply (subgoal_tac "inj_on (StandardRes p) B")
paulson@13871:   apply (simp add: B_def C_def card_image)
paulson@13871:   apply (rule StandardRes_inj_on_ResSet)
wenzelm@18369:   apply (simp add: B_res)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma D_E_disj: "D \<inter> E = {}"
paulson@13871:   by (auto simp add: D_def E_def)
paulson@13871: 
wenzelm@21233: lemma C_card_eq_D_plus_E: "card C = card D + card E"
paulson@13871:   by (auto simp add: C_eq card_Un_disjoint D_E_disj finite_D finite_E)
paulson@13871: 
wenzelm@21233: lemma C_prod_eq_D_times_E: "setprod id E * setprod id D = setprod id C"
paulson@13871:   apply (insert D_E_disj finite_D finite_E C_eq)
haftmann@57418:   apply (frule setprod.union_disjoint [of D E id])
wenzelm@18369:   apply auto
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma C_B_zcong_prod: "[setprod id C = setprod id B] (mod p)"
paulson@13871:   apply (auto simp add: C_def)
wenzelm@18369:   apply (insert finite_B SR_B_inj)
haftmann@57418:   apply (frule setprod.reindex [of "StandardRes p" B id])
haftmann@57418:   apply auto
nipkow@15392:   apply (rule setprod_same_function_zcong)
wenzelm@18369:   apply (auto simp add: StandardRes_prop1 zcong_sym p_g_0)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma F_Un_D_subset: "(F \<union> D) \<subseteq> A"
paulson@13871:   apply (rule Un_least)
wenzelm@18369:   apply (auto simp add: A_def F_subset D_subset)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma F_D_disj: "(F \<inter> D) = {}"
paulson@13871:   apply (simp add: F_eq D_eq)
paulson@13871:   apply (auto simp add: F_eq D_eq)
wenzelm@18369: proof -
wenzelm@18369:   fix y fix ya
wenzelm@18369:   assume "p - StandardRes p (y * a) = StandardRes p (ya * a)"
wenzelm@18369:   then have "p = StandardRes p (y * a) + StandardRes p (ya * a)"
wenzelm@18369:     by arith
wenzelm@18369:   moreover have "p dvd p"
wenzelm@18369:     by auto
wenzelm@18369:   ultimately have "p dvd (StandardRes p (y * a) + StandardRes p (ya * a))"
wenzelm@18369:     by auto
wenzelm@18369:   then have a: "[StandardRes p (y * a) + StandardRes p (ya * a) = 0] (mod p)"
wenzelm@18369:     by (auto simp add: zcong_def)
wenzelm@18369:   have "[y * a = StandardRes p (y * a)] (mod p)"
wenzelm@18369:     by (simp only: zcong_sym StandardRes_prop1)
wenzelm@18369:   moreover have "[ya * a = StandardRes p (ya * a)] (mod p)"
wenzelm@18369:     by (simp only: zcong_sym StandardRes_prop1)
wenzelm@18369:   ultimately have "[y * a + ya * a =
wenzelm@18369:     StandardRes p (y * a) + StandardRes p (ya * a)] (mod p)"
wenzelm@18369:     by (rule zcong_zadd)
wenzelm@18369:   with a have "[y * a + ya * a = 0] (mod p)"
wenzelm@18369:     apply (elim zcong_trans)
wenzelm@18369:     by (simp only: zcong_refl)
wenzelm@18369:   also have "y * a + ya * a = a * (y + ya)"
haftmann@57512:     by (simp add: distrib_left mult.commute)
wenzelm@18369:   finally have "[a * (y + ya) = 0] (mod p)" .
wenzelm@18369:   with p_prime a_nonzero zcong_zprime_prod_zero [of p a "y + ya"]
wenzelm@18369:     p_a_relprime
wenzelm@18369:   have a: "[y + ya = 0] (mod p)"
wenzelm@18369:     by auto
wenzelm@18369:   assume b: "y \<in> A" and c: "ya: A"
wenzelm@18369:   with A_def have "0 < y + ya"
wenzelm@18369:     by auto
wenzelm@18369:   moreover from b c A_def have "y + ya \<le> (p - 1) div 2 + (p - 1) div 2"
wenzelm@18369:     by auto
wenzelm@18369:   moreover from b c p_eq2 A_def have "y + ya < p"
wenzelm@18369:     by auto
wenzelm@18369:   ultimately show False
wenzelm@18369:     apply simp
wenzelm@18369:     apply (frule_tac m = p in zcong_not_zero)
wenzelm@18369:     apply (auto simp add: a)
wenzelm@18369:     done
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma F_Un_D_card: "card (F \<union> D) = nat ((p - 1) div 2)"
wenzelm@18369: proof -
wenzelm@18369:   have "card (F \<union> D) = card E + card D"
wenzelm@18369:     by (auto simp add: finite_F finite_D F_D_disj
wenzelm@18369:       card_Un_disjoint F_card_eq_E)
wenzelm@18369:   then have "card (F \<union> D) = card C"
wenzelm@18369:     by (simp add: C_card_eq_D_plus_E)
wenzelm@18369:   from this show "card (F \<union> D) = nat ((p - 1) div 2)"
wenzelm@18369:     by (simp add: C_card_eq_B B_card_eq)
wenzelm@18369: qed
paulson@13871: 
wenzelm@21233: lemma F_Un_D_eq_A: "F \<union> D = A"
wenzelm@18369:   using finite_A F_Un_D_subset A_card_eq F_Un_D_card by (auto simp add: card_seteq)
paulson@13871: 
wenzelm@21233: lemma prod_D_F_eq_prod_A:
wenzelm@18369:     "(setprod id D) * (setprod id F) = setprod id A"
paulson@13871:   apply (insert F_D_disj finite_D finite_F)
haftmann@57418:   apply (frule setprod.union_disjoint [of F D id])
wenzelm@18369:   apply (auto simp add: F_Un_D_eq_A)
wenzelm@18369:   done
paulson@13871: 
wenzelm@21233: lemma prod_F_zcong:
wenzelm@18369:   "[setprod id F = ((-1) ^ (card E)) * (setprod id E)] (mod p)"
wenzelm@18369: proof -
wenzelm@18369:   have "setprod id F = setprod id (op - p ` E)"
wenzelm@18369:     by (auto simp add: F_def)
wenzelm@18369:   then have "setprod id F = setprod (op - p) E"
wenzelm@18369:     apply simp
wenzelm@18369:     apply (insert finite_E inj_on_pminusx_E)
haftmann@57418:     apply (frule setprod.reindex [of "minus p" E id])
haftmann@57418:     apply auto
wenzelm@18369:     done
wenzelm@18369:   then have one:
wenzelm@18369:     "[setprod id F = setprod (StandardRes p o (op - p)) E] (mod p)"
wenzelm@18369:     apply simp
nipkow@30837:     apply (insert p_g_0 finite_E StandardRes_prod)
nipkow@30837:     by (auto)
wenzelm@18369:   moreover have a: "\<forall>x \<in> E. [p - x = 0 - x] (mod p)"
wenzelm@18369:     apply clarify
wenzelm@18369:     apply (insert zcong_id [of p])
wenzelm@18369:     apply (rule_tac a = p and m = p and c = x and d = x in zcong_zdiff, auto)
wenzelm@18369:     done
wenzelm@18369:   moreover have b: "\<forall>x \<in> E. [StandardRes p (p - x) = p - x](mod p)"
wenzelm@18369:     apply clarify
wenzelm@18369:     apply (simp add: StandardRes_prop1 zcong_sym)
wenzelm@18369:     done
wenzelm@18369:   moreover have "\<forall>x \<in> E. [StandardRes p (p - x) = - x](mod p)"
wenzelm@18369:     apply clarify
wenzelm@18369:     apply (insert a b)
wenzelm@18369:     apply (rule_tac b = "p - x" in zcong_trans, auto)
wenzelm@18369:     done
wenzelm@18369:   ultimately have c:
wenzelm@18369:     "[setprod (StandardRes p o (op - p)) E = setprod (uminus) E](mod p)"
wenzelm@18369:     apply simp
nipkow@30837:     using finite_E p_g_0
nipkow@30837:       setprod_same_function_zcong [of E "StandardRes p o (op - p)" uminus p]
nipkow@30837:     by auto
wenzelm@18369:   then have two: "[setprod id F = setprod (uminus) E](mod p)"
wenzelm@18369:     apply (insert one c)
wenzelm@18369:     apply (rule zcong_trans [of "setprod id F"
nipkow@15392:                                "setprod (StandardRes p o op - p) E" p
wenzelm@18369:                                "setprod uminus E"], auto)
wenzelm@18369:     done
wenzelm@18369:   also have "setprod uminus E = (setprod id E) * (-1)^(card E)"
berghofe@22274:     using finite_E by (induct set: finite) auto
wenzelm@18369:   then have "setprod uminus E = (-1) ^ (card E) * (setprod id E)"
haftmann@57512:     by (simp add: mult.commute)
wenzelm@18369:   with two show ?thesis
wenzelm@18369:     by simp
nipkow@15392: qed
paulson@13871: 
wenzelm@21233: 
paulson@13871: subsection {* Gauss' Lemma *}
paulson@13871: 
haftmann@58410: lemma aux: "setprod id A * (- 1) ^ card E * a ^ card A * (- 1) ^ card E = setprod id A * a ^ card A"
paulson@13871:   by (auto simp add: finite_E neg_one_special)
paulson@13871: 
wenzelm@21233: theorem pre_gauss_lemma:
wenzelm@18369:   "[a ^ nat((p - 1) div 2) = (-1) ^ (card E)] (mod p)"
wenzelm@18369: proof -
wenzelm@18369:   have "[setprod id A = setprod id F * setprod id D](mod p)"
haftmann@57512:     by (auto simp add: prod_D_F_eq_prod_A mult.commute cong del:setprod.cong)
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) * setprod id E) *
wenzelm@18369:       setprod id D] (mod p)"
wenzelm@18369:     apply (rule zcong_trans)
haftmann@57418:     apply (auto simp add: prod_F_zcong zcong_scalar cong del: setprod.cong)
wenzelm@18369:     done
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) * setprod id C)] (mod p)"
wenzelm@18369:     apply (rule zcong_trans)
wenzelm@18369:     apply (insert C_prod_eq_D_times_E, erule subst)
haftmann@57512:     apply (subst mult.assoc, auto)
wenzelm@18369:     done
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) * setprod id B)] (mod p)"
wenzelm@18369:     apply (rule zcong_trans)
haftmann@57418:     apply (simp add: C_B_zcong_prod zcong_scalar2 cong del:setprod.cong)
wenzelm@18369:     done
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) *
wenzelm@18369:     (setprod id ((%x. x * a) ` A)))] (mod p)"
wenzelm@18369:     by (simp add: B_def)
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) * (setprod (%x. x * a) A))]
wenzelm@18369:     (mod p)"
haftmann@57418:     by (simp add:finite_A inj_on_xa_A setprod.reindex cong del:setprod.cong)
wenzelm@18369:   moreover have "setprod (%x. x * a) A =
wenzelm@18369:     setprod (%x. a) A * setprod id A"
berghofe@22274:     using finite_A by (induct set: finite) auto
wenzelm@18369:   ultimately have "[setprod id A = ((-1)^(card E) * (setprod (%x. a) A *
wenzelm@18369:     setprod id A))] (mod p)"
wenzelm@18369:     by simp
wenzelm@18369:   then have "[setprod id A = ((-1)^(card E) * a^(card A) *
wenzelm@18369:       setprod id A)](mod p)"
wenzelm@18369:     apply (rule zcong_trans)
haftmann@57512:     apply (simp add: zcong_scalar2 zcong_scalar finite_A setprod_constant mult.assoc)
wenzelm@18369:     done
wenzelm@18369:   then have a: "[setprod id A * (-1)^(card E) =
wenzelm@18369:       ((-1)^(card E) * a^(card A) * setprod id A * (-1)^(card E))](mod p)"
wenzelm@18369:     by (rule zcong_scalar)
wenzelm@18369:   then have "[setprod id A * (-1)^(card E) = setprod id A *
wenzelm@18369:       (-1)^(card E) * a^(card A) * (-1)^(card E)](mod p)"
wenzelm@18369:     apply (rule zcong_trans)
haftmann@57512:     apply (simp add: a mult.commute mult.left_commute)
wenzelm@18369:     done
wenzelm@18369:   then have "[setprod id A * (-1)^(card E) = setprod id A *
wenzelm@18369:       a^(card A)](mod p)"
wenzelm@18369:     apply (rule zcong_trans)
haftmann@57418:     apply (simp add: aux cong del:setprod.cong)
wenzelm@18369:     done
haftmann@58410:   with this zcong_cancel2 [of p "setprod id A" "(- 1) ^ card E" "a ^ card A"]
haftmann@58410:       p_g_0 A_prod_relprime have "[(- 1) ^ card E = a ^ card A](mod p)"
wenzelm@18369:     by (simp add: order_less_imp_le)
wenzelm@18369:   from this show ?thesis
wenzelm@18369:     by (simp add: A_card_eq zcong_sym)
nipkow@15392: qed
paulson@13871: 
wenzelm@21233: theorem gauss_lemma: "(Legendre a p) = (-1) ^ (card E)"
nipkow@15392: proof -
paulson@13871:   from Euler_Criterion p_prime p_g_2 have
wenzelm@18369:       "[(Legendre a p) = a^(nat (((p) - 1) div 2))] (mod p)"
paulson@13871:     by auto
nipkow@15392:   moreover note pre_gauss_lemma
nipkow@15392:   ultimately have "[(Legendre a p) = (-1) ^ (card E)] (mod p)"
paulson@13871:     by (rule zcong_trans)
nipkow@15392:   moreover from p_a_relprime have "(Legendre a p) = 1 | (Legendre a p) = (-1)"
paulson@13871:     by (auto simp add: Legendre_def)
nipkow@15392:   moreover have "(-1::int) ^ (card E) = 1 | (-1::int) ^ (card E) = -1"
paulson@13871:     by (rule neg_one_power)
nipkow@15392:   ultimately show ?thesis
paulson@13871:     by (auto simp add: p_g_2 one_not_neg_one_mod_m zcong_sym)
nipkow@15392: qed
paulson@13871: 
avigad@16775: end
wenzelm@21233: 
wenzelm@21233: end




