src/HOL/Number_Theory/Residues.thy
changeset 65416 f707dbcf11e3
parent 65066 c64d778a593a
child 65465 067210a08a22
equal deleted inserted replaced
65415:8cd54b18b68b 65416:f707dbcf11e3
     6 *)
     6 *)
     7 
     7 
     8 section \<open>Residue rings\<close>
     8 section \<open>Residue rings\<close>
     9 
     9 
    10 theory Residues
    10 theory Residues
    11 imports Cong MiscAlgebra
    11 imports
       
    12   Cong
       
    13   "~~/src/HOL/Algebra/More_Group"
       
    14   "~~/src/HOL/Algebra/More_Ring"
       
    15   "~~/src/HOL/Algebra/More_Finite_Product"
       
    16   "~~/src/HOL/Algebra/Multiplicative_Group"
    12 begin
    17 begin
    13 
    18 
    14 definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool" where
    19 definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool" where
    15   "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))"
    20   "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))"
    16 
    21 
   115   apply (simp add: add.commute mod_add_right_eq)
   120   apply (simp add: add.commute mod_add_right_eq)
   116   apply (metis add.right_neutral minus_add_cancel mod_add_right_eq mod_pos_pos_trivial)
   121   apply (metis add.right_neutral minus_add_cancel mod_add_right_eq mod_pos_pos_trivial)
   117   done
   122   done
   118 
   123 
   119 lemma finite [iff]: "finite (carrier R)"
   124 lemma finite [iff]: "finite (carrier R)"
   120   by (subst res_carrier_eq) auto
   125   by (simp add: res_carrier_eq)
   121 
   126 
   122 lemma finite_Units [iff]: "finite (Units R)"
   127 lemma finite_Units [iff]: "finite (Units R)"
   123   by (subst res_units_eq) auto
   128   by (simp add: finite_ring_finite_units)
   124 
   129 
   125 text \<open>
   130 text \<open>
   126   The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
   131   The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
   127   residue classes. The following lemmas show that this mapping
   132   residue classes. The following lemmas show that this mapping
   128   respects addition and multiplication on the integers.
   133   respects addition and multiplication on the integers.
   284   unfolding phi_def by (auto simp add: card_eq_0_iff)
   289   unfolding phi_def by (auto simp add: card_eq_0_iff)
   285 
   290 
   286 lemma phi_one [simp]: "phi 1 = 0"
   291 lemma phi_one [simp]: "phi 1 = 0"
   287   by (auto simp add: phi_def card_eq_0_iff)
   292   by (auto simp add: phi_def card_eq_0_iff)
   288 
   293 
       
   294 lemma phi_leq: "phi x \<le> nat x - 1"
       
   295 proof -
       
   296   have "phi x \<le> card {1..x - 1}"
       
   297     unfolding phi_def by (rule card_mono) auto
       
   298   then show ?thesis by simp
       
   299 qed
       
   300 
       
   301 lemma phi_nonzero:
       
   302   "phi x > 0" if "2 \<le> x"
       
   303 proof -
       
   304   have "finite {y. 0 < y \<and> y < x}"
       
   305     by simp
       
   306   then have "finite {y. 0 < y \<and> y < x \<and> coprime y x}"
       
   307     by (auto intro: rev_finite_subset)
       
   308   moreover have "1 \<in> {y. 0 < y \<and> y < x \<and> coprime y x}"
       
   309     using that by simp
       
   310   ultimately have "card {y. 0 < y \<and> y < x \<and> coprime y x} \<noteq> 0"
       
   311     by auto
       
   312   then show ?thesis
       
   313     by (simp add: phi_def)
       
   314 qed
       
   315 
   289 lemma (in residues) phi_eq: "phi m = card (Units R)"
   316 lemma (in residues) phi_eq: "phi m = card (Units R)"
   290   by (simp add: phi_def res_units_eq)
   317   by (simp add: phi_def res_units_eq)
   291 
   318 
   292 lemma (in residues) euler_theorem1:
   319 lemma (in residues) euler_theorem1:
   293   assumes a: "gcd a m = 1"
   320   assumes a: "gcd a m = 1"
   411   then show ?thesis
   438   then show ?thesis
   412     using assms prime_ge_2_nat
   439     using assms prime_ge_2_nat
   413     by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
   440     by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
   414 qed
   441 qed
   415 
   442 
       
   443 text {*
       
   444   This result can be transferred to the multiplicative group of
       
   445   $\mathbb{Z}/p\mathbb{Z}$ for $p$ prime. *}
       
   446 
       
   447 lemma mod_nat_int_pow_eq:
       
   448   fixes n :: nat and p a :: int
       
   449   assumes "a \<ge> 0" "p \<ge> 0"
       
   450   shows "(nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)"
       
   451   using assms
       
   452   by (simp add: int_one_le_iff_zero_less nat_mod_distrib order_less_imp_le nat_power_eq[symmetric])
       
   453 
       
   454 theorem residue_prime_mult_group_has_gen :
       
   455  fixes p :: nat
       
   456  assumes prime_p : "prime p"
       
   457  shows "\<exists>a \<in> {1 .. p - 1}. {1 .. p - 1} = {a^i mod p|i . i \<in> UNIV}"
       
   458 proof -
       
   459   have "p\<ge>2" using prime_gt_1_nat[OF prime_p] by simp
       
   460   interpret R:residues_prime "p" "residue_ring p" unfolding residues_prime_def
       
   461     by (simp add: prime_p)
       
   462   have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} =  {1 .. int p - 1}"
       
   463     by (auto simp add: R.zero_cong R.res_carrier_eq)
       
   464   obtain a where a:"a \<in> {1 .. int p - 1}"
       
   465          and a_gen:"{1 .. int p - 1} = {a(^)\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}"
       
   466     apply atomize_elim using field.finite_field_mult_group_has_gen[OF R.is_field]
       
   467     by (auto simp add: car[symmetric] carrier_mult_of)
       
   468   { fix x fix i :: nat assume x: "x \<in> {1 .. int p - 1}"
       
   469     hence "x (^)\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)" using R.pow_cong[of x i] by auto}
       
   470   note * = this
       
   471   have **:"nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R")
       
   472   proof
       
   473     { fix n assume n: "n \<in> ?L"
       
   474       then have "n \<in> ?R" using `p\<ge>2` by force
       
   475     } thus "?L \<subseteq> ?R" by blast
       
   476     { fix n assume n: "n \<in> ?R"
       
   477       then have "n \<in> ?L" using `p\<ge>2` Set_Interval.transfer_nat_int_set_functions(2) by fastforce
       
   478     } thus "?R \<subseteq> ?L" by blast
       
   479   qed
       
   480   have "nat ` {a^i mod (int p) | i::nat. i \<in> UNIV} = {nat a^i mod p | i . i \<in> UNIV}" (is "?L = ?R")
       
   481   proof
       
   482     { fix x assume x: "x \<in> ?L"
       
   483       then obtain i where i:"x = nat (a^i mod (int p))" by blast
       
   484       hence "x = nat a ^ i mod p" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
       
   485       hence "x \<in> ?R" using i by blast
       
   486     } thus "?L \<subseteq> ?R" by blast
       
   487     { fix x assume x: "x \<in> ?R"
       
   488       then obtain i where i:"x = nat a^i mod p" by blast
       
   489       hence "x \<in> ?L" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
       
   490     } thus "?R \<subseteq> ?L" by blast
       
   491   qed
       
   492   hence "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}"
       
   493     using * a a_gen ** by presburger
       
   494   moreover
       
   495   have "nat a \<in> {1 .. p - 1}" using a by force
       
   496   ultimately show ?thesis ..
       
   497 qed
       
   498 
   416 end
   499 end