src/HOL/Number_Theory/Residues.thy
 author paulson Mon Feb 03 00:22:48 2014 +0000 (2014-02-03) changeset 55262 16724746ad89 parent 55261 ad3604df6bc6 child 55352 1d2852dfc4a7 permissions -rw-r--r--
fixed indentation
```     1 (*  Title:      HOL/Number_Theory/Residues.thy
```
```     2     Author:     Jeremy Avigad
```
```     3
```
```     4 An algebraic treatment of residue rings, and resulting proofs of
```
```     5 Euler's theorem and Wilson's theorem.
```
```     6 *)
```
```     7
```
```     8 header {* Residue rings *}
```
```     9
```
```    10 theory Residues
```
```    11 imports
```
```    12   UniqueFactorization
```
```    13   Binomial
```
```    14   MiscAlgebra
```
```    15 begin
```
```    16
```
```    17 (*
```
```    18
```
```    19   A locale for residue rings
```
```    20
```
```    21 *)
```
```    22
```
```    23 definition residue_ring :: "int => int ring" where
```
```    24   "residue_ring m == (|
```
```    25     carrier =       {0..m - 1},
```
```    26     mult =          (%x y. (x * y) mod m),
```
```    27     one =           1,
```
```    28     zero =          0,
```
```    29     add =           (%x y. (x + y) mod m) |)"
```
```    30
```
```    31 locale residues =
```
```    32   fixes m :: int and R (structure)
```
```    33   assumes m_gt_one: "m > 1"
```
```    34   defines "R == residue_ring m"
```
```    35
```
```    36 context residues
```
```    37 begin
```
```    38
```
```    39 lemma abelian_group: "abelian_group R"
```
```    40   apply (insert m_gt_one)
```
```    41   apply (rule abelian_groupI)
```
```    42   apply (unfold R_def residue_ring_def)
```
```    43   apply (auto simp add: mod_add_right_eq [symmetric] add_ac)
```
```    44   apply (case_tac "x = 0")
```
```    45   apply force
```
```    46   apply (subgoal_tac "(x + (m - x)) mod m = 0")
```
```    47   apply (erule bexI)
```
```    48   apply auto
```
```    49   done
```
```    50
```
```    51 lemma comm_monoid: "comm_monoid R"
```
```    52   apply (insert m_gt_one)
```
```    53   apply (unfold R_def residue_ring_def)
```
```    54   apply (rule comm_monoidI)
```
```    55   apply auto
```
```    56   apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
```
```    57   apply (erule ssubst)
```
```    58   apply (subst mod_mult_right_eq [symmetric])+
```
```    59   apply (simp_all only: mult_ac)
```
```    60   done
```
```    61
```
```    62 lemma cring: "cring R"
```
```    63   apply (rule cringI)
```
```    64   apply (rule abelian_group)
```
```    65   apply (rule comm_monoid)
```
```    66   apply (unfold R_def residue_ring_def, auto)
```
```    67   apply (subst mod_add_eq [symmetric])
```
```    68   apply (subst mult_commute)
```
```    69   apply (subst mod_mult_right_eq [symmetric])
```
```    70   apply (simp add: field_simps)
```
```    71   done
```
```    72
```
```    73 end
```
```    74
```
```    75 sublocale residues < cring
```
```    76   by (rule cring)
```
```    77
```
```    78
```
```    79 context residues
```
```    80 begin
```
```    81
```
```    82 (* These lemmas translate back and forth between internal and
```
```    83    external concepts *)
```
```    84
```
```    85 lemma res_carrier_eq: "carrier R = {0..m - 1}"
```
```    86   unfolding R_def residue_ring_def by auto
```
```    87
```
```    88 lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
```
```    89   unfolding R_def residue_ring_def by auto
```
```    90
```
```    91 lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
```
```    92   unfolding R_def residue_ring_def by auto
```
```    93
```
```    94 lemma res_zero_eq: "\<zero> = 0"
```
```    95   unfolding R_def residue_ring_def by auto
```
```    96
```
```    97 lemma res_one_eq: "\<one> = 1"
```
```    98   unfolding R_def residue_ring_def units_of_def by auto
```
```    99
```
```   100 lemma res_units_eq: "Units R = { x. 0 < x & x < m & coprime x m}"
```
```   101   apply (insert m_gt_one)
```
```   102   apply (unfold Units_def R_def residue_ring_def)
```
```   103   apply auto
```
```   104   apply (subgoal_tac "x ~= 0")
```
```   105   apply auto
```
```   106   apply (rule invertible_coprime_int)
```
```   107   apply (subgoal_tac "x ~= 0")
```
```   108   apply auto
```
```   109   apply (subst (asm) coprime_iff_invertible'_int)
```
```   110   apply arith
```
```   111   apply (auto simp add: cong_int_def mult_commute)
```
```   112   done
```
```   113
```
```   114 lemma res_neg_eq: "\<ominus> x = (- x) mod m"
```
```   115   apply (insert m_gt_one)
```
```   116   apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
```
```   117   apply auto
```
```   118   apply (rule the_equality)
```
```   119   apply auto
```
```   120   apply (subst mod_add_right_eq [symmetric])
```
```   121   apply auto
```
```   122   apply (subst mod_add_left_eq [symmetric])
```
```   123   apply auto
```
```   124   apply (subgoal_tac "y mod m = - x mod m")
```
```   125   apply simp
```
```   126   apply (subst zmod_eq_dvd_iff)
```
```   127   apply auto
```
```   128   done
```
```   129
```
```   130 lemma finite [iff]: "finite (carrier R)"
```
```   131   by (subst res_carrier_eq, auto)
```
```   132
```
```   133 lemma finite_Units [iff]: "finite (Units R)"
```
```   134   by (subst res_units_eq) auto
```
```   135
```
```   136 (* The function a -> a mod m maps the integers to the
```
```   137    residue classes. The following lemmas show that this mapping
```
```   138    respects addition and multiplication on the integers. *)
```
```   139
```
```   140 lemma mod_in_carrier [iff]: "a mod m : carrier R"
```
```   141   apply (unfold res_carrier_eq)
```
```   142   apply (insert m_gt_one, auto)
```
```   143   done
```
```   144
```
```   145 lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
```
```   146   unfolding R_def residue_ring_def
```
```   147   apply auto
```
```   148   apply presburger
```
```   149   done
```
```   150
```
```   151 lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
```
```   152   apply (unfold R_def residue_ring_def, auto)
```
```   153   apply (subst mod_mult_right_eq [symmetric])
```
```   154   apply (subst mult_commute)
```
```   155   apply (subst mod_mult_right_eq [symmetric])
```
```   156   apply (subst mult_commute)
```
```   157   apply auto
```
```   158   done
```
```   159
```
```   160 lemma zero_cong: "\<zero> = 0"
```
```   161   unfolding R_def residue_ring_def by auto
```
```   162
```
```   163 lemma one_cong: "\<one> = 1 mod m"
```
```   164   using m_gt_one unfolding R_def residue_ring_def by auto
```
```   165
```
```   166 (* revise algebra library to use 1? *)
```
```   167 lemma pow_cong: "(x mod m) (^) n = x^n mod m"
```
```   168   apply (insert m_gt_one)
```
```   169   apply (induct n)
```
```   170   apply (auto simp add: nat_pow_def one_cong)
```
```   171   apply (subst mult_commute)
```
```   172   apply (rule mult_cong)
```
```   173   done
```
```   174
```
```   175 lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
```
```   176   apply (rule sym)
```
```   177   apply (rule sum_zero_eq_neg)
```
```   178   apply auto
```
```   179   apply (subst add_cong)
```
```   180   apply (subst zero_cong)
```
```   181   apply auto
```
```   182   done
```
```   183
```
```   184 lemma (in residues) prod_cong:
```
```   185     "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
```
```   186   apply (induct set: finite)
```
```   187   apply (auto simp: one_cong mult_cong)
```
```   188   done
```
```   189
```
```   190 lemma (in residues) sum_cong:
```
```   191     "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
```
```   192   apply (induct set: finite)
```
```   193   apply (auto simp: zero_cong add_cong)
```
```   194   done
```
```   195
```
```   196 lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow>
```
```   197     a mod m : Units R"
```
```   198   apply (subst res_units_eq, auto)
```
```   199   apply (insert pos_mod_sign [of m a])
```
```   200   apply (subgoal_tac "a mod m ~= 0")
```
```   201   apply arith
```
```   202   apply auto
```
```   203   apply (subst (asm) gcd_red_int)
```
```   204   apply (subst gcd_commute_int, assumption)
```
```   205   done
```
```   206
```
```   207 lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))"
```
```   208   unfolding cong_int_def by auto
```
```   209
```
```   210 (* Simplifying with these will translate a ring equation in R to a
```
```   211    congruence. *)
```
```   212
```
```   213 lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
```
```   214     prod_cong sum_cong neg_cong res_eq_to_cong
```
```   215
```
```   216 (* Other useful facts about the residue ring *)
```
```   217
```
```   218 lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
```
```   219   apply (simp add: res_one_eq res_neg_eq)
```
```   220   apply (insert m_gt_one)
```
```   221   apply (subgoal_tac "~(m > 2)")
```
```   222   apply arith
```
```   223   apply (rule notI)
```
```   224   apply (subgoal_tac "-1 mod m = m - 1")
```
```   225   apply force
```
```   226   apply (subst mod_add_self2 [symmetric])
```
```   227   apply (subst mod_pos_pos_trivial)
```
```   228   apply auto
```
```   229   done
```
```   230
```
```   231 end
```
```   232
```
```   233
```
```   234 (* prime residues *)
```
```   235
```
```   236 locale residues_prime =
```
```   237   fixes p and R (structure)
```
```   238   assumes p_prime [intro]: "prime p"
```
```   239   defines "R == residue_ring p"
```
```   240
```
```   241 sublocale residues_prime < residues p
```
```   242   apply (unfold R_def residues_def)
```
```   243   using p_prime apply auto
```
```   244   apply (metis (full_types) int_1 of_nat_less_iff prime_gt_1_nat)
```
```   245   done
```
```   246
```
```   247 context residues_prime
```
```   248 begin
```
```   249
```
```   250 lemma is_field: "field R"
```
```   251   apply (rule cring.field_intro2)
```
```   252   apply (rule cring)
```
```   253   apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq)
```
```   254   apply (rule classical)
```
```   255   apply (erule notE)
```
```   256   apply (subst gcd_commute_int)
```
```   257   apply (rule prime_imp_coprime_int)
```
```   258   apply (rule p_prime)
```
```   259   apply (rule notI)
```
```   260   apply (frule zdvd_imp_le)
```
```   261   apply auto
```
```   262   done
```
```   263
```
```   264 lemma res_prime_units_eq: "Units R = {1..p - 1}"
```
```   265   apply (subst res_units_eq)
```
```   266   apply auto
```
```   267   apply (subst gcd_commute_int)
```
```   268   apply (rule prime_imp_coprime_int)
```
```   269   apply (rule p_prime)
```
```   270   apply (rule zdvd_not_zless)
```
```   271   apply auto
```
```   272   done
```
```   273
```
```   274 end
```
```   275
```
```   276 sublocale residues_prime < field
```
```   277   by (rule is_field)
```
```   278
```
```   279
```
```   280 (*
```
```   281   Test cases: Euler's theorem and Wilson's theorem.
```
```   282 *)
```
```   283
```
```   284
```
```   285 subsection{* Euler's theorem *}
```
```   286
```
```   287 (* the definition of the phi function *)
```
```   288
```
```   289 definition phi :: "int => nat"
```
```   290   where "phi m = card({ x. 0 < x & x < m & gcd x m = 1})"
```
```   291
```
```   292 lemma phi_def_nat: "phi m = card({ x. 0 < x & x < nat m & gcd x (nat m) = 1})"
```
```   293   apply (simp add: phi_def)
```
```   294   apply (rule bij_betw_same_card [of nat])
```
```   295   apply (auto simp add: inj_on_def bij_betw_def image_def)
```
```   296   apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
```
```   297   apply (metis One_nat_def int_0 int_1 int_less_0_conv int_nat_eq nat_int transfer_int_nat_gcd(1) zless_int)
```
```   298   done
```
```   299
```
```   300 lemma prime_phi:
```
```   301   assumes  "2 \<le> p" "phi p = p - 1" shows "prime p"
```
```   302 proof -
```
```   303   have "{x. 0 < x \<and> x < p \<and> coprime x p} = {1..p - 1}"
```
```   304     using assms unfolding phi_def_nat
```
```   305     by (intro card_seteq) fastforce+
```
```   306   then have cop: "\<And>x. x \<in> {1::nat..p - 1} \<Longrightarrow> coprime x p"
```
```   307     by blast
```
```   308   { fix x::nat assume *: "1 < x" "x < p" and "x dvd p"
```
```   309     have "coprime x p"
```
```   310       apply (rule cop)
```
```   311       using * apply auto
```
```   312       done
```
```   313     with `x dvd p` `1 < x` have "False" by auto }
```
```   314   then show ?thesis
```
```   315     using `2 \<le> p`
```
```   316     by (simp add: prime_def)
```
```   317        (metis One_nat_def dvd_pos_nat nat_dvd_not_less nat_neq_iff not_gr0 not_numeral_le_zero one_dvd)
```
```   318 qed
```
```   319
```
```   320 lemma phi_zero [simp]: "phi 0 = 0"
```
```   321   apply (subst phi_def)
```
```   322 (* Auto hangs here. Once again, where is the simplification rule
```
```   323    1 == Suc 0 coming from? *)
```
```   324   apply (auto simp add: card_eq_0_iff)
```
```   325 (* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
```
```   326   done
```
```   327
```
```   328 lemma phi_one [simp]: "phi 1 = 0"
```
```   329   by (auto simp add: phi_def card_eq_0_iff)
```
```   330
```
```   331 lemma (in residues) phi_eq: "phi m = card(Units R)"
```
```   332   by (simp add: phi_def res_units_eq)
```
```   333
```
```   334 lemma (in residues) euler_theorem1:
```
```   335   assumes a: "gcd a m = 1"
```
```   336   shows "[a^phi m = 1] (mod m)"
```
```   337 proof -
```
```   338   from a m_gt_one have [simp]: "a mod m : Units R"
```
```   339     by (intro mod_in_res_units)
```
```   340   from phi_eq have "(a mod m) (^) (phi m) = (a mod m) (^) (card (Units R))"
```
```   341     by simp
```
```   342   also have "\<dots> = \<one>"
```
```   343     by (intro units_power_order_eq_one, auto)
```
```   344   finally show ?thesis
```
```   345     by (simp add: res_to_cong_simps)
```
```   346 qed
```
```   347
```
```   348 (* In fact, there is a two line proof!
```
```   349
```
```   350 lemma (in residues) euler_theorem1:
```
```   351   assumes a: "gcd a m = 1"
```
```   352   shows "[a^phi m = 1] (mod m)"
```
```   353 proof -
```
```   354   have "(a mod m) (^) (phi m) = \<one>"
```
```   355     by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
```
```   356   then show ?thesis
```
```   357     by (simp add: res_to_cong_simps)
```
```   358 qed
```
```   359
```
```   360 *)
```
```   361
```
```   362 (* outside the locale, we can relax the restriction m > 1 *)
```
```   363
```
```   364 lemma euler_theorem:
```
```   365   assumes "m >= 0" and "gcd a m = 1"
```
```   366   shows "[a^phi m = 1] (mod m)"
```
```   367 proof (cases)
```
```   368   assume "m = 0 | m = 1"
```
```   369   then show ?thesis by auto
```
```   370 next
```
```   371   assume "~(m = 0 | m = 1)"
```
```   372   with assms show ?thesis
```
```   373     by (intro residues.euler_theorem1, unfold residues_def, auto)
```
```   374 qed
```
```   375
```
```   376 lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
```
```   377   apply (subst phi_eq)
```
```   378   apply (subst res_prime_units_eq)
```
```   379   apply auto
```
```   380   done
```
```   381
```
```   382 lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
```
```   383   apply (rule residues_prime.phi_prime)
```
```   384   apply (erule residues_prime.intro)
```
```   385   done
```
```   386
```
```   387 lemma fermat_theorem:
```
```   388   fixes a::int
```
```   389   assumes "prime p" and "~ (p dvd a)"
```
```   390   shows "[a^(p - 1) = 1] (mod p)"
```
```   391 proof -
```
```   392   from assms have "[a^phi p = 1] (mod p)"
```
```   393     apply (intro euler_theorem)
```
```   394     apply (metis of_nat_0_le_iff)
```
```   395     apply (metis gcd_int.commute prime_imp_coprime_int)
```
```   396     done
```
```   397   also have "phi p = nat p - 1"
```
```   398     by (rule phi_prime, rule assms)
```
```   399   finally show ?thesis
```
```   400     by (metis nat_int)
```
```   401 qed
```
```   402
```
```   403 lemma fermat_theorem_nat:
```
```   404   assumes "prime p" and "~ (p dvd a)"
```
```   405   shows "[a^(p - 1) = 1] (mod p)"
```
```   406 using fermat_theorem [of p a] assms
```
```   407 by (metis int_1 of_nat_power transfer_int_nat_cong zdvd_int)
```
```   408
```
```   409
```
```   410 subsection {* Wilson's theorem *}
```
```   411
```
```   412 lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
```
```   413     {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}"
```
```   414   apply auto
```
```   415   apply (erule notE)
```
```   416   apply (erule inv_eq_imp_eq)
```
```   417   apply auto
```
```   418   apply (erule notE)
```
```   419   apply (erule inv_eq_imp_eq)
```
```   420   apply auto
```
```   421   done
```
```   422
```
```   423 lemma (in residues_prime) wilson_theorem1:
```
```   424   assumes a: "p > 2"
```
```   425   shows "[fact (p - 1) = - 1] (mod p)"
```
```   426 proof -
```
```   427   let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}"
```
```   428   have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
```
```   429     by auto
```
```   430   have "(\<Otimes>i: Units R. i) =
```
```   431     (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
```
```   432     apply (subst UR)
```
```   433     apply (subst finprod_Un_disjoint)
```
```   434     apply (auto intro:funcsetI)
```
```   435     apply (drule sym, subst (asm) inv_eq_one_eq)
```
```   436     apply auto
```
```   437     apply (drule sym, subst (asm) inv_eq_neg_one_eq)
```
```   438     apply auto
```
```   439     done
```
```   440   also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
```
```   441     apply (subst finprod_insert)
```
```   442     apply auto
```
```   443     apply (frule one_eq_neg_one)
```
```   444     apply (insert a, force)
```
```   445     done
```
```   446   also have "(\<Otimes>i:(Union ?InversePairs). i) =
```
```   447       (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
```
```   448     apply (subst finprod_Union_disjoint)
```
```   449     apply force
```
```   450     apply force
```
```   451     apply clarify
```
```   452     apply (rule inv_pair_lemma)
```
```   453     apply auto
```
```   454     done
```
```   455   also have "\<dots> = \<one>"
```
```   456     apply (rule finprod_one)
```
```   457     apply auto
```
```   458     apply (subst finprod_insert)
```
```   459     apply auto
```
```   460     apply (frule inv_eq_self)
```
```   461     apply (auto)
```
```   462     done
```
```   463   finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
```
```   464     by simp
```
```   465   also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
```
```   466     apply (rule finprod_cong')
```
```   467     apply (auto)
```
```   468     apply (subst (asm) res_prime_units_eq)
```
```   469     apply auto
```
```   470     done
```
```   471   also have "\<dots> = (PROD i: Units R. i) mod p"
```
```   472     apply (rule prod_cong)
```
```   473     apply auto
```
```   474     done
```
```   475   also have "\<dots> = fact (p - 1) mod p"
```
```   476     apply (subst fact_altdef_nat)
```
```   477     apply (insert assms)
```
```   478     apply (subst res_prime_units_eq)
```
```   479     apply (simp add: int_setprod zmod_int setprod_int_eq)
```
```   480     done
```
```   481   finally have "fact (p - 1) mod p = \<ominus> \<one>".
```
```   482   then show ?thesis
```
```   483     by (metis Divides.transfer_int_nat_functions(2) cong_int_def res_neg_eq res_one_eq)
```
```   484 qed
```
```   485
```
```   486 lemma wilson_theorem: "prime p \<Longrightarrow> [fact (p - 1) = - 1] (mod p)"
```
```   487   apply (frule prime_gt_1_nat)
```
```   488   apply (case_tac "p = 2")
```
```   489   apply (subst fact_altdef_nat, simp)
```
```   490   apply (subst cong_int_def)
```
```   491   apply simp
```
```   492   apply (rule residues_prime.wilson_theorem1)
```
```   493   apply (rule residues_prime.intro)
```
```   494   apply auto
```
```   495   done
```
```   496
```
```   497 end
```