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