src/HOL/Number_Theory/Residues.thy
changeset 41541 1fa4725c4656
parent 36350 bc7982c54e37
child 41959 b460124855b8
equal deleted inserted replaced
41540:414a68d72279 41541:1fa4725c4656
     1 (*  Title:      HOL/Library/Residues.thy
     1 (*  Title:      HOL/Library/Residues.thy
     2     ID:         
       
     3     Author:     Jeremy Avigad
     2     Author:     Jeremy Avigad
     4 
     3 
     5     An algebraic treatment of residue rings, and resulting proofs of
     4 An algebraic treatment of residue rings, and resulting proofs of
     6     Euler's theorem and Wilson's theorem. 
     5 Euler's theorem and Wilson's theorem. 
     7 *)
     6 *)
     8 
     7 
     9 header {* Residue rings *}
     8 header {* Residue rings *}
    10 
     9 
    11 theory Residues
    10 theory Residues
    12 imports
    11 imports
    13    UniqueFactorization
    12   UniqueFactorization
    14    Binomial
    13   Binomial
    15    MiscAlgebra
    14   MiscAlgebra
    16 begin
    15 begin
    17 
    16 
    18 
    17 
    19 (*
    18 (*
    20  
    19  
    39 
    38 
    40 lemma abelian_group: "abelian_group R"
    39 lemma abelian_group: "abelian_group R"
    41   apply (insert m_gt_one)
    40   apply (insert m_gt_one)
    42   apply (rule abelian_groupI)
    41   apply (rule abelian_groupI)
    43   apply (unfold R_def residue_ring_def)
    42   apply (unfold R_def residue_ring_def)
    44   apply (auto simp add: mod_pos_pos_trivial mod_add_right_eq [symmetric]
    43   apply (auto simp add: mod_add_right_eq [symmetric] add_ac)
    45     add_ac)
       
    46   apply (case_tac "x = 0")
    44   apply (case_tac "x = 0")
    47   apply force
    45   apply force
    48   apply (subgoal_tac "(x + (m - x)) mod m = 0")
    46   apply (subgoal_tac "(x + (m - x)) mod m = 0")
    49   apply (erule bexI)
    47   apply (erule bexI)
    50   apply auto
    48   apply auto
    51 done
    49   done
    52 
    50 
    53 lemma comm_monoid: "comm_monoid R"
    51 lemma comm_monoid: "comm_monoid R"
    54   apply (insert m_gt_one)
    52   apply (insert m_gt_one)
    55   apply (unfold R_def residue_ring_def)
    53   apply (unfold R_def residue_ring_def)
    56   apply (rule comm_monoidI)
    54   apply (rule comm_monoidI)
    57   apply auto
    55   apply auto
    58   apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
    56   apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
    59   apply (erule ssubst)
    57   apply (erule ssubst)
    60   apply (subst zmod_zmult1_eq [symmetric])+
    58   apply (subst zmod_zmult1_eq [symmetric])+
    61   apply (simp_all only: mult_ac)
    59   apply (simp_all only: mult_ac)
    62 done
    60   done
    63 
    61 
    64 lemma cring: "cring R"
    62 lemma cring: "cring R"
    65   apply (rule cringI)
    63   apply (rule cringI)
    66   apply (rule abelian_group)
    64   apply (rule abelian_group)
    67   apply (rule comm_monoid)
    65   apply (rule comm_monoid)
    68   apply (unfold R_def residue_ring_def, auto)
    66   apply (unfold R_def residue_ring_def, auto)
    69   apply (subst mod_add_eq [symmetric])
    67   apply (subst mod_add_eq [symmetric])
    70   apply (subst mult_commute)
    68   apply (subst mult_commute)
    71   apply (subst zmod_zmult1_eq [symmetric])
    69   apply (subst zmod_zmult1_eq [symmetric])
    72   apply (simp add: field_simps)
    70   apply (simp add: field_simps)
    73 done
    71   done
    74 
    72 
    75 end
    73 end
    76 
    74 
    77 sublocale residues < cring
    75 sublocale residues < cring
    78   by (rule cring)
    76   by (rule cring)
    79 
    77 
    80 
    78 
    81 context residues begin
    79 context residues
       
    80 begin
    82 
    81 
    83 (* These lemmas translate back and forth between internal and 
    82 (* These lemmas translate back and forth between internal and 
    84    external concepts *)
    83    external concepts *)
    85 
    84 
    86 lemma res_carrier_eq: "carrier R = {0..m - 1}"
    85 lemma res_carrier_eq: "carrier R = {0..m - 1}"
    94 
    93 
    95 lemma res_zero_eq: "\<zero> = 0"
    94 lemma res_zero_eq: "\<zero> = 0"
    96   by (unfold R_def residue_ring_def, auto)
    95   by (unfold R_def residue_ring_def, auto)
    97 
    96 
    98 lemma res_one_eq: "\<one> = 1"
    97 lemma res_one_eq: "\<one> = 1"
    99   by (unfold R_def residue_ring_def units_of_def residue_ring_def, auto)
    98   by (unfold R_def residue_ring_def units_of_def, auto)
   100 
    99 
   101 lemma res_units_eq: "Units R = { x. 0 < x & x < m & coprime x m}"
   100 lemma res_units_eq: "Units R = { x. 0 < x & x < m & coprime x m}"
   102   apply (insert m_gt_one)
   101   apply (insert m_gt_one)
   103   apply (unfold Units_def R_def residue_ring_def)
   102   apply (unfold Units_def R_def residue_ring_def)
   104   apply auto
   103   apply auto
   108   apply (subgoal_tac "x ~= 0")
   107   apply (subgoal_tac "x ~= 0")
   109   apply auto
   108   apply auto
   110   apply (subst (asm) coprime_iff_invertible'_int)
   109   apply (subst (asm) coprime_iff_invertible'_int)
   111   apply (rule m_gt_one)
   110   apply (rule m_gt_one)
   112   apply (auto simp add: cong_int_def mult_commute)
   111   apply (auto simp add: cong_int_def mult_commute)
   113 done
   112   done
   114 
   113 
   115 lemma res_neg_eq: "\<ominus> x = (- x) mod m"
   114 lemma res_neg_eq: "\<ominus> x = (- x) mod m"
   116   apply (insert m_gt_one)
   115   apply (insert m_gt_one)
   117   apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
   116   apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
   118   apply auto
   117   apply auto
   124   apply auto
   123   apply auto
   125   apply (subgoal_tac "y mod m = - x mod m")
   124   apply (subgoal_tac "y mod m = - x mod m")
   126   apply simp
   125   apply simp
   127   apply (subst zmod_eq_dvd_iff)
   126   apply (subst zmod_eq_dvd_iff)
   128   apply auto
   127   apply auto
   129 done
   128   done
   130 
   129 
   131 lemma finite [iff]: "finite(carrier R)"
   130 lemma finite [iff]: "finite(carrier R)"
   132   by (subst res_carrier_eq, auto)
   131   by (subst res_carrier_eq, auto)
   133 
   132 
   134 lemma finite_Units [iff]: "finite(Units R)"
   133 lemma finite_Units [iff]: "finite(Units R)"
   139    respects addition and multiplication on the integers. *)
   138    respects addition and multiplication on the integers. *)
   140 
   139 
   141 lemma mod_in_carrier [iff]: "a mod m : carrier R"
   140 lemma mod_in_carrier [iff]: "a mod m : carrier R"
   142   apply (unfold res_carrier_eq)
   141   apply (unfold res_carrier_eq)
   143   apply (insert m_gt_one, auto)
   142   apply (insert m_gt_one, auto)
   144 done
   143   done
   145 
   144 
   146 lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
   145 lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
   147   by (unfold R_def residue_ring_def, auto, arith)
   146   by (unfold R_def residue_ring_def, auto, arith)
   148 
   147 
   149 lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
   148 lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
   151   apply (subst zmod_zmult1_eq [symmetric])
   150   apply (subst zmod_zmult1_eq [symmetric])
   152   apply (subst mult_commute)
   151   apply (subst mult_commute)
   153   apply (subst zmod_zmult1_eq [symmetric])
   152   apply (subst zmod_zmult1_eq [symmetric])
   154   apply (subst mult_commute)
   153   apply (subst mult_commute)
   155   apply auto
   154   apply auto
   156 done  
   155   done
   157 
   156 
   158 lemma zero_cong: "\<zero> = 0"
   157 lemma zero_cong: "\<zero> = 0"
   159   apply (unfold R_def residue_ring_def, auto)
   158   apply (unfold R_def residue_ring_def, auto)
   160 done
   159   done
   161 
   160 
   162 lemma one_cong: "\<one> = 1 mod m"
   161 lemma one_cong: "\<one> = 1 mod m"
   163   apply (insert m_gt_one)
   162   apply (insert m_gt_one)
   164   apply (unfold R_def residue_ring_def, auto)
   163   apply (unfold R_def residue_ring_def, auto)
   165 done
   164   done
   166 
   165 
   167 (* revise algebra library to use 1? *)
   166 (* revise algebra library to use 1? *)
   168 lemma pow_cong: "(x mod m) (^) n = x^n mod m"
   167 lemma pow_cong: "(x mod m) (^) n = x^n mod m"
   169   apply (insert m_gt_one)
   168   apply (insert m_gt_one)
   170   apply (induct n)
   169   apply (induct n)
   171   apply (auto simp add: nat_pow_def one_cong One_nat_def)
   170   apply (auto simp add: nat_pow_def one_cong)
   172   apply (subst mult_commute)
   171   apply (subst mult_commute)
   173   apply (rule mult_cong)
   172   apply (rule mult_cong)
   174 done
   173   done
   175 
   174 
   176 lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
   175 lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
   177   apply (rule sym)
   176   apply (rule sym)
   178   apply (rule sum_zero_eq_neg)
   177   apply (rule sum_zero_eq_neg)
   179   apply auto
   178   apply auto
   180   apply (subst add_cong)
   179   apply (subst add_cong)
   181   apply (subst zero_cong)
   180   apply (subst zero_cong)
   182   apply auto
   181   apply auto
   183 done
   182   done
   184 
   183 
   185 lemma (in residues) prod_cong: 
   184 lemma (in residues) prod_cong: 
   186   "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
   185   "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
   187   apply (induct set: finite)
   186   apply (induct set: finite)
   188   apply (auto simp: one_cong mult_cong)
   187   apply (auto simp: one_cong mult_cong)
   189 done
   188   done
   190 
   189 
   191 lemma (in residues) sum_cong:
   190 lemma (in residues) sum_cong:
   192   "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
   191   "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
   193   apply (induct set: finite)
   192   apply (induct set: finite)
   194   apply (auto simp: zero_cong add_cong)
   193   apply (auto simp: zero_cong add_cong)
   195 done
   194   done
   196 
   195 
   197 lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow> 
   196 lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow> 
   198     a mod m : Units R"
   197     a mod m : Units R"
   199   apply (subst res_units_eq, auto)
   198   apply (subst res_units_eq, auto)
   200   apply (insert pos_mod_sign [of m a])
   199   apply (insert pos_mod_sign [of m a])
   201   apply (subgoal_tac "a mod m ~= 0")
   200   apply (subgoal_tac "a mod m ~= 0")
   202   apply arith
   201   apply arith
   203   apply auto
   202   apply auto
   204   apply (subst (asm) gcd_red_int)
   203   apply (subst (asm) gcd_red_int)
   205   apply (subst gcd_commute_int, assumption)
   204   apply (subst gcd_commute_int, assumption)
   206 done
   205   done
   207 
   206 
   208 lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))" 
   207 lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))" 
   209   unfolding cong_int_def by auto
   208   unfolding cong_int_def by auto
   210 
   209 
   211 (* Simplifying with these will translate a ring equation in R to a 
   210 (* Simplifying with these will translate a ring equation in R to a 
   225   apply (subgoal_tac "-1 mod m = m - 1")
   224   apply (subgoal_tac "-1 mod m = m - 1")
   226   apply force
   225   apply force
   227   apply (subst mod_add_self2 [symmetric])
   226   apply (subst mod_add_self2 [symmetric])
   228   apply (subst mod_pos_pos_trivial)
   227   apply (subst mod_pos_pos_trivial)
   229   apply auto
   228   apply auto
   230 done
   229   done
   231 
   230 
   232 end
   231 end
   233 
   232 
   234 
   233 
   235 (* prime residues *)
   234 (* prime residues *)
   240   defines "R == residue_ring p"
   239   defines "R == residue_ring p"
   241 
   240 
   242 sublocale residues_prime < residues p
   241 sublocale residues_prime < residues p
   243   apply (unfold R_def residues_def)
   242   apply (unfold R_def residues_def)
   244   using p_prime apply auto
   243   using p_prime apply auto
   245 done
   244   done
   246 
   245 
   247 context residues_prime begin
   246 context residues_prime begin
   248 
   247 
   249 lemma is_field: "field R"
   248 lemma is_field: "field R"
   250   apply (rule cring.field_intro2)
   249   apply (rule cring.field_intro2)
   257   apply (rule prime_imp_coprime_int)
   256   apply (rule prime_imp_coprime_int)
   258   apply (rule p_prime)
   257   apply (rule p_prime)
   259   apply (rule notI)
   258   apply (rule notI)
   260   apply (frule zdvd_imp_le)
   259   apply (frule zdvd_imp_le)
   261   apply auto
   260   apply auto
   262 done
   261   done
   263 
   262 
   264 lemma res_prime_units_eq: "Units R = {1..p - 1}"
   263 lemma res_prime_units_eq: "Units R = {1..p - 1}"
   265   apply (subst res_units_eq)
   264   apply (subst res_units_eq)
   266   apply auto
   265   apply auto
   267   apply (subst gcd_commute_int)
   266   apply (subst gcd_commute_int)
   268   apply (rule prime_imp_coprime_int)
   267   apply (rule prime_imp_coprime_int)
   269   apply (rule p_prime)
   268   apply (rule p_prime)
   270   apply (rule zdvd_not_zless)
   269   apply (rule zdvd_not_zless)
   271   apply auto
   270   apply auto
   272 done
   271   done
   273 
   272 
   274 end
   273 end
   275 
   274 
   276 sublocale residues_prime < field
   275 sublocale residues_prime < field
   277   by (rule is_field)
   276   by (rule is_field)
   293   apply (subst phi_def)
   292   apply (subst phi_def)
   294 (* Auto hangs here. Once again, where is the simplification rule 
   293 (* Auto hangs here. Once again, where is the simplification rule 
   295    1 == Suc 0 coming from? *)
   294    1 == Suc 0 coming from? *)
   296   apply (auto simp add: card_eq_0_iff)
   295   apply (auto simp add: card_eq_0_iff)
   297 (* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
   296 (* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
   298 done
   297   done
   299 
   298 
   300 lemma phi_one [simp]: "phi 1 = 0"
   299 lemma phi_one [simp]: "phi 1 = 0"
   301   apply (auto simp add: phi_def card_eq_0_iff)
   300   apply (auto simp add: phi_def card_eq_0_iff)
   302 done
   301   done
   303 
   302 
   304 lemma (in residues) phi_eq: "phi m = card(Units R)"
   303 lemma (in residues) phi_eq: "phi m = card(Units R)"
   305   by (simp add: phi_def res_units_eq)
   304   by (simp add: phi_def res_units_eq)
   306 
   305 
   307 lemma (in residues) euler_theorem1: 
   306 lemma (in residues) euler_theorem1: 
   340 proof (cases)
   339 proof (cases)
   341   assume "m = 0 | m = 1"
   340   assume "m = 0 | m = 1"
   342   thus ?thesis by auto
   341   thus ?thesis by auto
   343 next
   342 next
   344   assume "~(m = 0 | m = 1)"
   343   assume "~(m = 0 | m = 1)"
   345   with prems show ?thesis
   344   with assms show ?thesis
   346     by (intro residues.euler_theorem1, unfold residues_def, auto)
   345     by (intro residues.euler_theorem1, unfold residues_def, auto)
   347 qed
   346 qed
   348 
   347 
   349 lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
   348 lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
   350   apply (subst phi_eq)
   349   apply (subst phi_eq)
   351   apply (subst res_prime_units_eq)
   350   apply (subst res_prime_units_eq)
   352   apply auto
   351   apply auto
   353 done
   352   done
   354 
   353 
   355 lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
   354 lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
   356   apply (rule residues_prime.phi_prime)
   355   apply (rule residues_prime.phi_prime)
   357   apply (erule residues_prime.intro)
   356   apply (erule residues_prime.intro)
   358 done
   357   done
   359 
   358 
   360 lemma fermat_theorem:
   359 lemma fermat_theorem:
   361   assumes "prime p" and "~ (p dvd a)"
   360   assumes "prime p" and "~ (p dvd a)"
   362   shows "[a^(nat p - 1) = 1] (mod p)"
   361   shows "[a^(nat p - 1) = 1] (mod p)"
   363 proof -
   362 proof -
   364   from prems have "[a^phi p = 1] (mod p)"
   363   from assms have "[a^phi p = 1] (mod p)"
   365     apply (intro euler_theorem)
   364     apply (intro euler_theorem)
   366     (* auto should get this next part. matching across
   365     (* auto should get this next part. matching across
   367        substitutions is needed. *)
   366        substitutions is needed. *)
   368     apply (frule prime_gt_1_int, arith)
   367     apply (frule prime_gt_1_int, arith)
   369     apply (subst gcd_commute_int, erule prime_imp_coprime_int, assumption)
   368     apply (subst gcd_commute_int, erule prime_imp_coprime_int, assumption)
   370     done
   369     done
   371   also have "phi p = nat p - 1"
   370   also have "phi p = nat p - 1"
   372     by (rule phi_prime, rule prems)
   371     by (rule phi_prime, rule assms)
   373   finally show ?thesis .
   372   finally show ?thesis .
   374 qed
   373 qed
   375 
   374 
   376 
   375 
   377 subsection {* Wilson's theorem *}
   376 subsection {* Wilson's theorem *}
   378 
   377 
   379 lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow> 
   378 lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow> 
   380   {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}" 
   379     {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}" 
   381   apply auto
   380   apply auto
   382   apply (erule notE)
   381   apply (erule notE)
   383   apply (erule inv_eq_imp_eq)
   382   apply (erule inv_eq_imp_eq)
   384   apply auto
   383   apply auto
   385   apply (erule notE)
   384   apply (erule notE)
   386   apply (erule inv_eq_imp_eq)
   385   apply (erule inv_eq_imp_eq)
   387   apply auto
   386   apply auto
   388 done
   387   done
   389 
   388 
   390 lemma (in residues_prime) wilson_theorem1:
   389 lemma (in residues_prime) wilson_theorem1:
   391   assumes a: "p > 2"
   390   assumes a: "p > 2"
   392   shows "[fact (p - 1) = - 1] (mod p)"
   391   shows "[fact (p - 1) = - 1] (mod p)"
   393 proof -
   392 proof -
   409     apply auto
   408     apply auto
   410     apply (frule one_eq_neg_one)
   409     apply (frule one_eq_neg_one)
   411     apply (insert a, force)
   410     apply (insert a, force)
   412     done
   411     done
   413   also have "(\<Otimes>i:(Union ?InversePairs). i) = 
   412   also have "(\<Otimes>i:(Union ?InversePairs). i) = 
   414       (\<Otimes> A: ?InversePairs. (\<Otimes> y:A. y))"
   413       (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
   415     apply (subst finprod_Union_disjoint)
   414     apply (subst finprod_Union_disjoint)
   416     apply force
   415     apply force
   417     apply force
   416     apply force
   418     apply clarify
   417     apply clarify
   419     apply (rule inv_pair_lemma)
   418     apply (rule inv_pair_lemma)
   439     apply (rule prod_cong)
   438     apply (rule prod_cong)
   440     apply auto
   439     apply auto
   441     done
   440     done
   442   also have "\<dots> = fact (p - 1) mod p"
   441   also have "\<dots> = fact (p - 1) mod p"
   443     apply (subst fact_altdef_int)
   442     apply (subst fact_altdef_int)
   444     apply (insert prems, force)
   443     apply (insert assms, force)
   445     apply (subst res_prime_units_eq, rule refl)
   444     apply (subst res_prime_units_eq, rule refl)
   446     done
   445     done
   447   finally have "fact (p - 1) mod p = \<ominus> \<one>".
   446   finally have "fact (p - 1) mod p = \<ominus> \<one>".
   448   thus ?thesis
   447   thus ?thesis
   449     by (simp add: res_to_cong_simps)
   448     by (simp add: res_to_cong_simps)