1 (* Title: HOL/NumberTheory/WilsonRuss.thy
3 Author: Thomas M. Rasmussen
4 Copyright 2000 University of Cambridge
6 Changes by Jeremy Avigad, 2003/02/21:
7 repaired proof of prime_g_5
10 header {* Wilson's Theorem according to Russinoff *}
12 theory WilsonRuss = EulerFermat:
15 Wilson's Theorem following quite closely Russinoff's approach
16 using Boyer-Moore (using finite sets instead of lists, though).
19 subsection {* Definitions and lemmas *}
22 inv :: "int => int => int"
23 wset :: "int * int => int set"
26 inv_def: "inv p a == (a^(nat (p - 2))) mod p"
29 "measure ((\<lambda>(a, p). nat a) :: int * int => nat)"
32 let ws = wset (a - 1, p)
33 in (if a \<in> ws then ws else insert a (insert (inv p a) ws)) else {})"
36 text {* \medskip @{term [source] inv} *}
38 lemma inv_is_inv_aux: "1 < m ==> Suc (nat (m - 2)) = nat (m - 1)"
39 by (subst int_int_eq [symmetric], auto)
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)
55 "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> a \<noteq> inv p a"
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)
66 "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 0"
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")
74 apply (subst zdvd_zminus_iff [symmetric], auto)
78 "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
80 apply (cut_tac a = a and p = p in inv_is_inv)
83 apply (subgoal_tac "a = 1")
84 apply (rule_tac [2] zcong_zless_imp_eq, auto)
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)
98 lemma inv_not_p_minus_1:
99 "p \<in> zprime \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
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)
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)
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)
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)
135 lemma zcong_zpower_zmult:
136 "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
138 apply (auto simp add: zpower_zadd_distrib)
139 apply (subgoal_tac "zcong (x^y * x^(y * n)) (1 * 1) p")
140 apply (rule_tac [2] zcong_zmult, simp_all)
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)
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)
163 text {* \medskip @{term wset} *}
165 declare wset.simps [simp del]
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"
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)
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)
189 lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset (a, p)"
190 apply (subst wset.simps)
191 apply (unfold Let_def, simp)
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)
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)
208 apply (rule inv_g_1, auto)
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)
220 apply (rule inv_less_p_minus_1, auto)
223 lemma wset_mem [rule_format]:
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 (subgoal_tac "b = a")
228 apply (rule_tac [2] zle_anti_sym)
229 apply (rule_tac [4] wset_subset)
230 apply (simp (no_asm_simp))
234 lemma wset_mem_inv_mem [rule_format]:
235 "p \<in> zprime --> 5 \<le> p --> a < p - 1 --> b \<in> wset (a, p)
236 --> inv p b \<in> wset (a, p)"
237 apply (induct a p rule: wset_induct, auto)
238 apply (case_tac "b = a")
239 apply (subst wset.simps)
240 apply (unfold Let_def)
241 apply (rule_tac [3] wset_subset, auto)
242 apply (case_tac "b = inv p a")
243 apply (simp (no_asm_simp))
244 apply (subst inv_inv)
245 apply (subgoal_tac [6] "b = a \<or> b = inv p a")
246 apply (rule_tac [7] wset_mem_imp_or, auto)
249 lemma wset_inv_mem_mem:
250 "p \<in> zprime \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
251 \<Longrightarrow> inv p b \<in> wset (a, p) \<Longrightarrow> b \<in> wset (a, p)"
252 apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
253 apply (rule_tac [2] wset_mem_inv_mem)
254 apply (rule inv_inv, simp_all)
257 lemma wset_fin: "finite (wset (a, p))"
258 apply (induct a p rule: wset_induct)
260 apply (subst wset.simps)
261 apply (unfold Let_def, auto)
264 lemma wset_zcong_prod_1 [rule_format]:
266 5 \<le> p --> a < p - 1 --> [setprod (wset (a, p)) = 1] (mod p)"
267 apply (induct a p rule: wset_induct)
269 apply (subst wset.simps)
270 apply (unfold Let_def, auto)
271 apply (subst setprod_insert)
272 apply (tactic {* stac (thm "setprod_insert") 3 *})
273 apply (subgoal_tac [5]
274 "zcong (a * inv p a * setprod (wset (a - 1, p))) (1 * 1) p")
276 apply (simp add: zmult_assoc)
277 apply (rule_tac [5] zcong_zmult)
278 apply (rule_tac [5] inv_is_inv)
279 apply (tactic "Clarify_tac 4")
280 apply (subgoal_tac [4] "a \<in> wset (a - 1, p)")
281 apply (rule_tac [5] wset_inv_mem_mem)
282 apply (simp_all add: wset_fin)
283 apply (rule inv_distinct, auto)
286 lemma d22set_eq_wset: "p \<in> zprime ==> d22set (p - 2) = wset (p - 2, p)"
288 apply (erule wset_mem)
289 apply (rule_tac [2] d22set_g_1)
290 apply (rule_tac [3] d22set_le)
291 apply (rule_tac [4] d22set_mem)
292 apply (erule_tac [4] wset_g_1)
294 apply (subst zle_add1_eq_le [symmetric])
295 apply (subgoal_tac "p - 2 + 1 = p - 1")
296 apply (simp (no_asm_simp))
297 apply (erule wset_less, auto)
301 subsection {* Wilson *}
303 lemma prime_g_5: "p \<in> zprime \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
304 apply (unfold zprime_def dvd_def)
305 apply (case_tac "p = 4", auto)
309 apply (simp (no_asm))
310 apply (rule_tac x = 2 in exI)
312 apply (rule_tac x = 2 in exI, auto)
316 "p \<in> zprime ==> [zfact (p - 1) = -1] (mod p)"
317 apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
318 apply (rule_tac [2] zcong_zmult)
319 apply (simp only: zprime_def)
320 apply (subst zfact.simps)
321 apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
322 apply (simp only: zcong_def)
323 apply (simp (no_asm_simp))
324 apply (case_tac "p = 2")
325 apply (simp add: zfact.simps)
326 apply (case_tac "p = 3")
327 apply (simp add: zfact.simps)
328 apply (subgoal_tac "5 \<le> p")
329 apply (erule_tac [2] prime_g_5)
330 apply (subst d22set_prod_zfact [symmetric])
331 apply (subst d22set_eq_wset)
332 apply (rule_tac [2] wset_zcong_prod_1, auto)