src/HOL/NumberTheory/WilsonRuss.thy
 author paulson Tue Jun 28 15:27:45 2005 +0200 (2005-06-28) changeset 16587 b34c8aa657a5 parent 16417 9bc16273c2d4 child 16663 13e9c402308b permissions -rw-r--r--
Constant "If" is now local
```     1 (*  Title:      HOL/NumberTheory/WilsonRuss.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Thomas M. Rasmussen
```
```     4     Copyright   2000  University of Cambridge
```
```     5
```
```     6 Changes by Jeremy Avigad, 2003/02/21:
```
```     7     repaired proof of prime_g_5
```
```     8 *)
```
```     9
```
```    10 header {* Wilson's Theorem according to Russinoff *}
```
```    11
```
```    12 theory WilsonRuss imports EulerFermat begin
```
```    13
```
```    14 text {*
```
```    15   Wilson's Theorem following quite closely Russinoff's approach
```
```    16   using Boyer-Moore (using finite sets instead of lists, though).
```
```    17 *}
```
```    18
```
```    19 subsection {* Definitions and lemmas *}
```
```    20
```
```    21 consts
```
```    22   inv :: "int => int => int"
```
```    23   wset :: "int * int => int set"
```
```    24
```
```    25 defs
```
```    26   inv_def: "inv p a == (a^(nat (p - 2))) mod p"
```
```    27
```
```    28 recdef wset
```
```    29   "measure ((\<lambda>(a, p). nat a) :: int * int => nat)"
```
```    30   "wset (a, p) =
```
```    31     (if 1 < a then
```
```    32       let ws = wset (a - 1, p)
```
```    33       in (if a \<in> ws then ws else insert a (insert (inv p a) ws)) else {})"
```
```    34
```
```    35
```
```    36 text {* \medskip @{term [source] inv} *}
```
```    37
```
```    38 lemma inv_is_inv_aux: "1 < m ==> Suc (nat (m - 2)) = nat (m - 1)"
```
```    39 by (subst int_int_eq [symmetric], auto)
```
```    40
```
```    41 lemma inv_is_inv:
```
```    42     "p \<in> zprime \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> [a * inv p a = 1] (mod p)"
```
```    43   apply (unfold inv_def)
```
```    44   apply (subst zcong_zmod)
```
```    45   apply (subst zmod_zmult1_eq [symmetric])
```
```    46   apply (subst zcong_zmod [symmetric])
```
```    47   apply (subst power_Suc [symmetric])
```
```    48   apply (subst inv_is_inv_aux)
```
```    49    apply (erule_tac [2] Little_Fermat)
```
```    50    apply (erule_tac [2] zdvd_not_zless)
```
```    51    apply (unfold zprime_def, auto)
```
```    52   done
```
```    53
```
```    54 lemma inv_distinct:
```
```    55     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> a \<noteq> inv p a"
```
```    56   apply safe
```
```    57   apply (cut_tac a = a and p = p in zcong_square)
```
```    58      apply (cut_tac [3] a = a and p = p in inv_is_inv, auto)
```
```    59    apply (subgoal_tac "a = 1")
```
```    60     apply (rule_tac [2] m = p in zcong_zless_imp_eq)
```
```    61         apply (subgoal_tac [7] "a = p - 1")
```
```    62          apply (rule_tac [8] m = p in zcong_zless_imp_eq, auto)
```
```    63   done
```
```    64
```
```    65 lemma inv_not_0:
```
```    66     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 0"
```
```    67   apply safe
```
```    68   apply (cut_tac a = a and p = p in inv_is_inv)
```
```    69      apply (unfold zcong_def, auto)
```
```    70   apply (subgoal_tac "\<not> p dvd 1")
```
```    71    apply (rule_tac [2] zdvd_not_zless)
```
```    72     apply (subgoal_tac "p dvd 1")
```
```    73      prefer 2
```
```    74      apply (subst zdvd_zminus_iff [symmetric], auto)
```
```    75   done
```
```    76
```
```    77 lemma inv_not_1:
```
```    78     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
```
```    79   apply safe
```
```    80   apply (cut_tac a = a and p = p in inv_is_inv)
```
```    81      prefer 4
```
```    82      apply simp
```
```    83      apply (subgoal_tac "a = 1")
```
```    84       apply (rule_tac [2] zcong_zless_imp_eq, auto)
```
```    85   done
```
```    86
```
```    87 lemma inv_not_p_minus_1_aux: "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
```
```    88   apply (unfold zcong_def)
```
```    89   apply (simp add: OrderedGroup.diff_diff_eq diff_diff_eq2 zdiff_zmult_distrib2)
```
```    90   apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
```
```    91    apply (simp add: mult_commute)
```
```    92   apply (subst zdvd_zminus_iff)
```
```    93   apply (subst zdvd_reduce)
```
```    94   apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
```
```    95    apply (subst zdvd_reduce, auto)
```
```    96   done
```
```    97
```
```    98 lemma inv_not_p_minus_1:
```
```    99     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
```
```   100   apply safe
```
```   101   apply (cut_tac a = a and p = p in inv_is_inv, auto)
```
```   102   apply (simp add: inv_not_p_minus_1_aux)
```
```   103   apply (subgoal_tac "a = p - 1")
```
```   104    apply (rule_tac [2] zcong_zless_imp_eq, auto)
```
```   105   done
```
```   106
```
```   107 lemma inv_g_1:
```
```   108     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
```
```   109   apply (case_tac "0\<le> inv p a")
```
```   110    apply (subgoal_tac "inv p a \<noteq> 1")
```
```   111     apply (subgoal_tac "inv p a \<noteq> 0")
```
```   112      apply (subst order_less_le)
```
```   113      apply (subst zle_add1_eq_le [symmetric])
```
```   114      apply (subst order_less_le)
```
```   115      apply (rule_tac [2] inv_not_0)
```
```   116        apply (rule_tac [5] inv_not_1, auto)
```
```   117   apply (unfold inv_def zprime_def, simp)
```
```   118   done
```
```   119
```
```   120 lemma inv_less_p_minus_1:
```
```   121     "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
```
```   122   apply (case_tac "inv p a < p")
```
```   123    apply (subst order_less_le)
```
```   124    apply (simp add: inv_not_p_minus_1, auto)
```
```   125   apply (unfold inv_def zprime_def, simp)
```
```   126   done
```
```   127
```
```   128 lemma inv_inv_aux: "5 \<le> p ==>
```
```   129     nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
```
```   130   apply (subst int_int_eq [symmetric])
```
```   131   apply (simp add: zmult_int [symmetric])
```
```   132   apply (simp add: zdiff_zmult_distrib zdiff_zmult_distrib2)
```
```   133   done
```
```   134
```
```   135 lemma zcong_zpower_zmult:
```
```   136     "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
```
```   137   apply (induct z)
```
```   138    apply (auto simp add: zpower_zadd_distrib)
```
```   139   apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
```
```   140    apply (rule_tac [2] zcong_zmult, simp_all)
```
```   141   done
```
```   142
```
```   143 lemma inv_inv: "p \<in> zprime \<Longrightarrow>
```
```   144     5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
```
```   145   apply (unfold inv_def)
```
```   146   apply (subst zpower_zmod)
```
```   147   apply (subst zpower_zpower)
```
```   148   apply (rule zcong_zless_imp_eq)
```
```   149       prefer 5
```
```   150       apply (subst zcong_zmod)
```
```   151       apply (subst mod_mod_trivial)
```
```   152       apply (subst zcong_zmod [symmetric])
```
```   153       apply (subst inv_inv_aux)
```
```   154        apply (subgoal_tac [2]
```
```   155 	 "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
```
```   156         apply (rule_tac [3] zcong_zmult)
```
```   157          apply (rule_tac [4] zcong_zpower_zmult)
```
```   158          apply (erule_tac [4] Little_Fermat)
```
```   159          apply (rule_tac [4] zdvd_not_zless, simp_all)
```
```   160   done
```
```   161
```
```   162
```
```   163 text {* \medskip @{term wset} *}
```
```   164
```
```   165 declare wset.simps [simp del]
```
```   166
```
```   167 lemma wset_induct:
```
```   168   "(!!a p. P {} a p) \<Longrightarrow>
```
```   169     (!!a p. 1 < (a::int) \<Longrightarrow> P (wset (a - 1, p)) (a - 1) p
```
```   170       ==> P (wset (a, p)) a p)
```
```   171     ==> P (wset (u, v)) u v"
```
```   172 proof -
```
```   173   case rule_context
```
```   174   show ?thesis
```
```   175     apply (rule wset.induct, safe)
```
```   176      apply (case_tac [2] "1 < a")
```
```   177       apply (rule_tac [2] rule_context, simp_all)
```
```   178       apply (simp_all add: wset.simps rule_context)
```
```   179     done
```
```   180 qed
```
```   181
```
```   182 lemma wset_mem_imp_or [rule_format]:
```
```   183   "1 < a \<Longrightarrow> b \<notin> wset (a - 1, p)
```
```   184     ==> b \<in> wset (a, p) --> b = a \<or> b = inv p a"
```
```   185   apply (subst wset.simps)
```
```   186   apply (unfold Let_def, simp)
```
```   187   done
```
```   188
```
```   189 lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset (a, p)"
```
```   190   apply (subst wset.simps)
```
```   191   apply (unfold Let_def, simp)
```
```   192   done
```
```   193
```
```   194 lemma wset_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1, p) ==> b \<in> wset (a, p)"
```
```   195   apply (subst wset.simps)
```
```   196   apply (unfold Let_def, auto)
```
```   197   done
```
```   198
```
```   199 lemma wset_g_1 [rule_format]:
```
```   200     "p \<in> zprime --> a < p - 1 --> b \<in> wset (a, p) --> 1 < b"
```
```   201   apply (induct a p rule: wset_induct, auto)
```
```   202   apply (case_tac "b = a")
```
```   203    apply (case_tac [2] "b = inv p a")
```
```   204     apply (subgoal_tac [3] "b = a \<or> b = inv p a")
```
```   205      apply (rule_tac [4] wset_mem_imp_or)
```
```   206        prefer 2
```
```   207        apply simp
```
```   208        apply (rule inv_g_1, auto)
```
```   209   done
```
```   210
```
```   211 lemma wset_less [rule_format]:
```
```   212     "p \<in> zprime --> a < p - 1 --> b \<in> wset (a, p) --> b < p - 1"
```
```   213   apply (induct a p rule: wset_induct, auto)
```
```   214   apply (case_tac "b = a")
```
```   215    apply (case_tac [2] "b = inv p a")
```
```   216     apply (subgoal_tac [3] "b = a \<or> b = inv p a")
```
```   217      apply (rule_tac [4] wset_mem_imp_or)
```
```   218        prefer 2
```
```   219        apply simp
```
```   220        apply (rule inv_less_p_minus_1, auto)
```
```   221   done
```
```   222
```
```   223 lemma wset_mem [rule_format]:
```
```   224   "p \<in> zprime -->
```
```   225     a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset (a, p)"
```
```   226   apply (induct a p rule: wset.induct, auto)
```
```   227   apply (rule_tac wset_subset)
```
```   228   apply (simp (no_asm_simp))
```
```   229   apply auto
```
```   230   done
```
```   231
```
```   232 lemma wset_mem_inv_mem [rule_format]:
```
```   233   "p \<in> zprime --> 5 \<le> p --> a < p - 1 --> b \<in> wset (a, p)
```
```   234     --> inv p b \<in> wset (a, p)"
```
```   235   apply (induct a p rule: wset_induct, auto)
```
```   236    apply (case_tac "b = a")
```
```   237     apply (subst wset.simps)
```
```   238     apply (unfold Let_def)
```
```   239     apply (rule_tac [3] wset_subset, auto)
```
```   240   apply (case_tac "b = inv p a")
```
```   241    apply (simp (no_asm_simp))
```
```   242    apply (subst inv_inv)
```
```   243        apply (subgoal_tac [6] "b = a \<or> b = inv p a")
```
```   244         apply (rule_tac [7] wset_mem_imp_or, auto)
```
```   245   done
```
```   246
```
```   247 lemma wset_inv_mem_mem:
```
```   248   "p \<in> zprime \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
```
```   249     \<Longrightarrow> inv p b \<in> wset (a, p) \<Longrightarrow> b \<in> wset (a, p)"
```
```   250   apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
```
```   251    apply (rule_tac [2] wset_mem_inv_mem)
```
```   252       apply (rule inv_inv, simp_all)
```
```   253   done
```
```   254
```
```   255 lemma wset_fin: "finite (wset (a, p))"
```
```   256   apply (induct a p rule: wset_induct)
```
```   257    prefer 2
```
```   258    apply (subst wset.simps)
```
```   259    apply (unfold Let_def, auto)
```
```   260   done
```
```   261
```
```   262 lemma wset_zcong_prod_1 [rule_format]:
```
```   263   "p \<in> zprime -->
```
```   264     5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset(a, p). x) = 1] (mod p)"
```
```   265   apply (induct a p rule: wset_induct)
```
```   266    prefer 2
```
```   267    apply (subst wset.simps)
```
```   268    apply (unfold Let_def, auto)
```
```   269   apply (subst setprod_insert)
```
```   270     apply (tactic {* stac (thm "setprod_insert") 3 *})
```
```   271       apply (subgoal_tac [5]
```
```   272 	"zcong (a * inv p a * (\<Prod>x\<in> wset(a - 1, p). x)) (1 * 1) p")
```
```   273        prefer 5
```
```   274        apply (simp add: zmult_assoc)
```
```   275       apply (rule_tac [5] zcong_zmult)
```
```   276        apply (rule_tac [5] inv_is_inv)
```
```   277          apply (tactic "Clarify_tac 4")
```
```   278          apply (subgoal_tac [4] "a \<in> wset (a - 1, p)")
```
```   279           apply (rule_tac [5] wset_inv_mem_mem)
```
```   280                apply (simp_all add: wset_fin)
```
```   281   apply (rule inv_distinct, auto)
```
```   282   done
```
```   283
```
```   284 lemma d22set_eq_wset: "p \<in> zprime ==> d22set (p - 2) = wset (p - 2, p)"
```
```   285   apply safe
```
```   286    apply (erule wset_mem)
```
```   287      apply (rule_tac [2] d22set_g_1)
```
```   288      apply (rule_tac [3] d22set_le)
```
```   289      apply (rule_tac [4] d22set_mem)
```
```   290       apply (erule_tac [4] wset_g_1)
```
```   291        prefer 6
```
```   292        apply (subst zle_add1_eq_le [symmetric])
```
```   293        apply (subgoal_tac "p - 2 + 1 = p - 1")
```
```   294         apply (simp (no_asm_simp))
```
```   295         apply (erule wset_less, auto)
```
```   296   done
```
```   297
```
```   298
```
```   299 subsection {* Wilson *}
```
```   300
```
```   301 lemma prime_g_5: "p \<in> zprime \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
```
```   302   apply (unfold zprime_def dvd_def)
```
```   303   apply (case_tac "p = 4", auto)
```
```   304    apply (rule notE)
```
```   305     prefer 2
```
```   306     apply assumption
```
```   307    apply (simp (no_asm))
```
```   308    apply (rule_tac x = 2 in exI)
```
```   309    apply (safe, arith)
```
```   310      apply (rule_tac x = 2 in exI, auto)
```
```   311   done
```
```   312
```
```   313 theorem Wilson_Russ:
```
```   314     "p \<in> zprime ==> [zfact (p - 1) = -1] (mod p)"
```
```   315   apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
```
```   316    apply (rule_tac [2] zcong_zmult)
```
```   317     apply (simp only: zprime_def)
```
```   318     apply (subst zfact.simps)
```
```   319     apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
```
```   320    apply (simp only: zcong_def)
```
```   321    apply (simp (no_asm_simp))
```
```   322   apply (case_tac "p = 2")
```
```   323    apply (simp add: zfact.simps)
```
```   324   apply (case_tac "p = 3")
```
```   325    apply (simp add: zfact.simps)
```
```   326   apply (subgoal_tac "5 \<le> p")
```
```   327    apply (erule_tac [2] prime_g_5)
```
```   328     apply (subst d22set_prod_zfact [symmetric])
```
```   329     apply (subst d22set_eq_wset)
```
```   330      apply (rule_tac [2] wset_zcong_prod_1, auto)
```
```   331   done
```
```   332
```
```   333 end
```