1 (* Title: HOL/Old_Number_Theory/WilsonRuss.thy
2 Author: Thomas M. Rasmussen
3 Copyright 2000 University of Cambridge
6 header {* Wilson's Theorem according to Russinoff *}
13 Wilson's Theorem following quite closely Russinoff's approach
14 using Boyer-Moore (using finite sets instead of lists, though).
17 subsection {* Definitions and lemmas *}
19 definition inv :: "int => int => int"
20 where "inv p a = (a^(nat (p - 2))) mod p"
22 fun wset :: "int \<Rightarrow> int => int set" where
25 let ws = wset (a - 1) p
26 in (if a \<in> ws then ws else insert a (insert (inv p a) ws)) else {})"
29 text {* \medskip @{term [source] inv} *}
31 lemma inv_is_inv_aux: "1 < m ==> Suc (nat (m - 2)) = nat (m - 1)"
32 by (subst int_int_eq [symmetric]) auto
35 "zprime p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> [a * inv p a = 1] (mod p)"
36 apply (unfold inv_def)
37 apply (subst zcong_zmod)
38 apply (subst mod_mult_right_eq [symmetric])
39 apply (subst zcong_zmod [symmetric])
40 apply (subst power_Suc [symmetric])
41 apply (subst inv_is_inv_aux)
42 apply (erule_tac [2] Little_Fermat)
43 apply (erule_tac [2] zdvd_not_zless)
44 apply (unfold zprime_def, auto)
48 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> a \<noteq> inv p a"
50 apply (cut_tac a = a and p = p in zcong_square)
51 apply (cut_tac [3] a = a and p = p in inv_is_inv, auto)
52 apply (subgoal_tac "a = 1")
53 apply (rule_tac [2] m = p in zcong_zless_imp_eq)
54 apply (subgoal_tac [7] "a = p - 1")
55 apply (rule_tac [8] m = p in zcong_zless_imp_eq, auto)
59 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 0"
61 apply (cut_tac a = a and p = p in inv_is_inv)
62 apply (unfold zcong_def, auto)
63 apply (subgoal_tac "\<not> p dvd 1")
64 apply (rule_tac [2] zdvd_not_zless)
65 apply (subgoal_tac "p dvd 1")
67 apply (subst dvd_minus_iff [symmetric], auto)
71 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
73 apply (cut_tac a = a and p = p in inv_is_inv)
76 apply (subgoal_tac "a = 1")
77 apply (rule_tac [2] zcong_zless_imp_eq, auto)
80 lemma inv_not_p_minus_1_aux:
81 "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
82 apply (unfold zcong_def)
83 apply (simp add: diff_diff_eq diff_diff_eq2 right_diff_distrib)
84 apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
85 apply (simp add: algebra_simps)
86 apply (subst dvd_minus_iff)
87 apply (subst zdvd_reduce)
88 apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
89 apply (subst zdvd_reduce, auto)
92 lemma inv_not_p_minus_1:
93 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
95 apply (cut_tac a = a and p = p in inv_is_inv, auto)
96 apply (simp add: inv_not_p_minus_1_aux)
97 apply (subgoal_tac "a = p - 1")
98 apply (rule_tac [2] zcong_zless_imp_eq, auto)
102 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
103 apply (case_tac "0\<le> inv p a")
104 apply (subgoal_tac "inv p a \<noteq> 1")
105 apply (subgoal_tac "inv p a \<noteq> 0")
106 apply (subst order_less_le)
107 apply (subst zle_add1_eq_le [symmetric])
108 apply (subst order_less_le)
109 apply (rule_tac [2] inv_not_0)
110 apply (rule_tac [5] inv_not_1, auto)
111 apply (unfold inv_def zprime_def, simp)
114 lemma inv_less_p_minus_1:
115 "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
116 apply (case_tac "inv p a < p")
117 apply (subst order_less_le)
118 apply (simp add: inv_not_p_minus_1, auto)
119 apply (unfold inv_def zprime_def, simp)
122 lemma inv_inv_aux: "5 \<le> p ==>
123 nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
124 apply (subst int_int_eq [symmetric])
125 apply (simp add: of_nat_mult)
126 apply (simp add: left_diff_distrib right_diff_distrib)
129 lemma zcong_zpower_zmult:
130 "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
132 apply (auto simp add: power_add)
133 apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
134 apply (rule_tac [2] zcong_zmult, simp_all)
137 lemma inv_inv: "zprime p \<Longrightarrow>
138 5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
139 apply (unfold inv_def)
140 apply (subst power_mod)
141 apply (subst zpower_zpower)
142 apply (rule zcong_zless_imp_eq)
144 apply (subst zcong_zmod)
145 apply (subst mod_mod_trivial)
146 apply (subst zcong_zmod [symmetric])
147 apply (subst inv_inv_aux)
148 apply (subgoal_tac [2]
149 "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
150 apply (rule_tac [3] zcong_zmult)
151 apply (rule_tac [4] zcong_zpower_zmult)
152 apply (erule_tac [4] Little_Fermat)
153 apply (rule_tac [4] zdvd_not_zless, simp_all)
157 text {* \medskip @{term wset} *}
159 declare wset.simps [simp del]
162 assumes "!!a p. P {} a p"
163 and "!!a p. 1 < (a::int) \<Longrightarrow>
164 P (wset (a - 1) p) (a - 1) p ==> P (wset a p) a p"
165 shows "P (wset u v) u v"
166 apply (rule wset.induct)
167 apply (case_tac "1 < a")
169 apply (simp_all add: wset.simps assms)
172 lemma wset_mem_imp_or [rule_format]:
173 "1 < a \<Longrightarrow> b \<notin> wset (a - 1) p
174 ==> b \<in> wset a p --> b = a \<or> b = inv p a"
175 apply (subst wset.simps)
176 apply (unfold Let_def, simp)
179 lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset a p"
180 apply (subst wset.simps)
181 apply (unfold Let_def, simp)
184 lemma wset_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1) p ==> b \<in> wset a p"
185 apply (subst wset.simps)
186 apply (unfold Let_def, auto)
189 lemma wset_g_1 [rule_format]:
190 "zprime p --> a < p - 1 --> b \<in> wset a p --> 1 < b"
191 apply (induct a p rule: wset_induct, auto)
192 apply (case_tac "b = a")
193 apply (case_tac [2] "b = inv p a")
194 apply (subgoal_tac [3] "b = a \<or> b = inv p a")
195 apply (rule_tac [4] wset_mem_imp_or)
198 apply (rule inv_g_1, auto)
201 lemma wset_less [rule_format]:
202 "zprime p --> a < p - 1 --> b \<in> wset a p --> b < p - 1"
203 apply (induct a p rule: wset_induct, auto)
204 apply (case_tac "b = a")
205 apply (case_tac [2] "b = inv p a")
206 apply (subgoal_tac [3] "b = a \<or> b = inv p a")
207 apply (rule_tac [4] wset_mem_imp_or)
210 apply (rule inv_less_p_minus_1, auto)
213 lemma wset_mem [rule_format]:
215 a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset a p"
216 apply (induct a p rule: wset.induct, auto)
217 apply (rule_tac wset_subset)
218 apply (simp (no_asm_simp))
222 lemma wset_mem_inv_mem [rule_format]:
223 "zprime p --> 5 \<le> p --> a < p - 1 --> b \<in> wset a p
224 --> inv p b \<in> wset a p"
225 apply (induct a p rule: wset_induct, auto)
226 apply (case_tac "b = a")
227 apply (subst wset.simps)
228 apply (unfold Let_def)
229 apply (rule_tac [3] wset_subset, auto)
230 apply (case_tac "b = inv p a")
231 apply (simp (no_asm_simp))
232 apply (subst inv_inv)
233 apply (subgoal_tac [6] "b = a \<or> b = inv p a")
234 apply (rule_tac [7] wset_mem_imp_or, auto)
237 lemma wset_inv_mem_mem:
238 "zprime p \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
239 \<Longrightarrow> inv p b \<in> wset a p \<Longrightarrow> b \<in> wset a p"
240 apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
241 apply (rule_tac [2] wset_mem_inv_mem)
242 apply (rule inv_inv, simp_all)
245 lemma wset_fin: "finite (wset a p)"
246 apply (induct a p rule: wset_induct)
248 apply (subst wset.simps)
249 apply (unfold Let_def, auto)
252 lemma wset_zcong_prod_1 [rule_format]:
254 5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset a p. x) = 1] (mod p)"
255 apply (induct a p rule: wset_induct)
257 apply (subst wset.simps)
258 apply (auto, unfold Let_def, auto)
259 apply (subst setprod_insert)
260 apply (tactic {* stac @{thm setprod_insert} 3 *})
261 apply (subgoal_tac [5]
262 "zcong (a * inv p a * (\<Prod>x\<in>wset (a - 1) p. x)) (1 * 1) p")
264 apply (simp add: mult_assoc)
265 apply (rule_tac [5] zcong_zmult)
266 apply (rule_tac [5] inv_is_inv)
267 apply (tactic "clarify_tac @{context} 4")
268 apply (subgoal_tac [4] "a \<in> wset (a - 1) p")
269 apply (rule_tac [5] wset_inv_mem_mem)
270 apply (simp_all add: wset_fin)
271 apply (rule inv_distinct, auto)
274 lemma d22set_eq_wset: "zprime p ==> d22set (p - 2) = wset (p - 2) p"
276 apply (erule wset_mem)
277 apply (rule_tac [2] d22set_g_1)
278 apply (rule_tac [3] d22set_le)
279 apply (rule_tac [4] d22set_mem)
280 apply (erule_tac [4] wset_g_1)
282 apply (subst zle_add1_eq_le [symmetric])
283 apply (subgoal_tac "p - 2 + 1 = p - 1")
284 apply (simp (no_asm_simp))
285 apply (erule wset_less, auto)
289 subsection {* Wilson *}
291 lemma prime_g_5: "zprime p \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
292 apply (unfold zprime_def dvd_def)
293 apply (case_tac "p = 4", auto)
297 apply (simp (no_asm))
298 apply (rule_tac x = 2 in exI)
300 apply (rule_tac x = 2 in exI, auto)
304 "zprime p ==> [zfact (p - 1) = -1] (mod p)"
305 apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
306 apply (rule_tac [2] zcong_zmult)
307 apply (simp only: zprime_def)
308 apply (subst zfact.simps)
309 apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
310 apply (simp only: zcong_def)
311 apply (simp (no_asm_simp))
312 apply (case_tac "p = 2")
313 apply (simp add: zfact.simps)
314 apply (case_tac "p = 3")
315 apply (simp add: zfact.simps)
316 apply (subgoal_tac "5 \<le> p")
317 apply (erule_tac [2] prime_g_5)
318 apply (subst d22set_prod_zfact [symmetric])
319 apply (subst d22set_eq_wset)
320 apply (rule_tac [2] wset_zcong_prod_1, auto)