1 (* Title: WilsonBij.thy |
1 (* Title: HOL/NumberTheory/WilsonBij.thy |
2 ID: $Id$ |
2 ID: $Id$ |
3 Author: Thomas M. Rasmussen |
3 Author: Thomas M. Rasmussen |
4 Copyright 2000 University of Cambridge |
4 Copyright 2000 University of Cambridge |
5 *) |
5 *) |
6 |
6 |
7 WilsonBij = BijectionRel + IntFact + |
7 header {* Wilson's Theorem using a more abstract approach *} |
8 |
8 |
9 consts |
9 theory WilsonBij = BijectionRel + IntFact: |
10 reciR :: "int => [int,int] => bool" |
10 |
11 inv :: "[int,int] => int" |
11 text {* |
12 |
12 Wilson's Theorem using a more ``abstract'' approach based on |
13 defs |
13 bijections between sets. Does not use Fermat's Little Theorem |
14 reciR_def "reciR p == (%a b. zcong (a*b) #1 p & |
14 (unlike Russinoff). |
15 #1<a & a<p-#1 & #1<b & b<p-#1)" |
15 *} |
16 inv_def "inv p a == (if p:zprime & #0<a & a<p then |
16 |
17 (@x. #0<=x & x<p & zcong (a*x) #1 p) |
17 |
18 else #0)" |
18 subsection {* Definitions and lemmas *} |
|
19 |
|
20 constdefs |
|
21 reciR :: "int => int => int => bool" |
|
22 "reciR p == |
|
23 \<lambda>a b. zcong (a * b) #1 p \<and> #1 < a \<and> a < p - #1 \<and> #1 < b \<and> b < p - #1" |
|
24 inv :: "int => int => int" |
|
25 "inv p a == |
|
26 if p \<in> zprime \<and> #0 < a \<and> a < p then |
|
27 (SOME x. #0 \<le> x \<and> x < p \<and> zcong (a * x) #1 p) |
|
28 else #0" |
|
29 |
|
30 |
|
31 text {* \medskip Inverse *} |
|
32 |
|
33 lemma inv_correct: |
|
34 "p \<in> zprime ==> #0 < a ==> a < p |
|
35 ==> #0 \<le> inv p a \<and> inv p a < p \<and> [a * inv p a = #1] (mod p)" |
|
36 apply (unfold inv_def) |
|
37 apply (simp (no_asm_simp)) |
|
38 apply (rule zcong_lineq_unique [THEN ex1_implies_ex, THEN someI_ex]) |
|
39 apply (erule_tac [2] zless_zprime_imp_zrelprime) |
|
40 apply (unfold zprime_def) |
|
41 apply auto |
|
42 done |
|
43 |
|
44 lemmas inv_ge = inv_correct [THEN conjunct1, standard] |
|
45 lemmas inv_less = inv_correct [THEN conjunct2, THEN conjunct1, standard] |
|
46 lemmas inv_is_inv = inv_correct [THEN conjunct2, THEN conjunct2, standard] |
|
47 |
|
48 lemma inv_not_0: |
|
49 "p \<in> zprime ==> #1 < a ==> a < p - #1 ==> inv p a \<noteq> #0" |
|
50 -- {* same as @{text WilsonRuss} *} |
|
51 apply safe |
|
52 apply (cut_tac a = a and p = p in inv_is_inv) |
|
53 apply (unfold zcong_def) |
|
54 apply auto |
|
55 apply (subgoal_tac "\<not> p dvd #1") |
|
56 apply (rule_tac [2] zdvd_not_zless) |
|
57 apply (subgoal_tac "p dvd #1") |
|
58 prefer 2 |
|
59 apply (subst zdvd_zminus_iff [symmetric]) |
|
60 apply auto |
|
61 done |
|
62 |
|
63 lemma inv_not_1: |
|
64 "p \<in> zprime ==> #1 < a ==> a < p - #1 ==> inv p a \<noteq> #1" |
|
65 -- {* same as @{text WilsonRuss} *} |
|
66 apply safe |
|
67 apply (cut_tac a = a and p = p in inv_is_inv) |
|
68 prefer 4 |
|
69 apply simp |
|
70 apply (subgoal_tac "a = #1") |
|
71 apply (rule_tac [2] zcong_zless_imp_eq) |
|
72 apply auto |
|
73 done |
|
74 |
|
75 lemma aux: "[a * (p - #1) = #1] (mod p) = [a = p - #1] (mod p)" |
|
76 -- {* same as @{text WilsonRuss} *} |
|
77 apply (unfold zcong_def) |
|
78 apply (simp add: zdiff_zdiff_eq zdiff_zdiff_eq2 zdiff_zmult_distrib2) |
|
79 apply (rule_tac s = "p dvd -((a + #1) + (p * -a))" in trans) |
|
80 apply (simp add: zmult_commute zminus_zdiff_eq) |
|
81 apply (subst zdvd_zminus_iff) |
|
82 apply (subst zdvd_reduce) |
|
83 apply (rule_tac s = "p dvd (a + #1) + (p * -#1)" in trans) |
|
84 apply (subst zdvd_reduce) |
|
85 apply auto |
|
86 done |
|
87 |
|
88 lemma inv_not_p_minus_1: |
|
89 "p \<in> zprime ==> #1 < a ==> a < p - #1 ==> inv p a \<noteq> p - #1" |
|
90 -- {* same as @{text WilsonRuss} *} |
|
91 apply safe |
|
92 apply (cut_tac a = a and p = p in inv_is_inv) |
|
93 apply auto |
|
94 apply (simp add: aux) |
|
95 apply (subgoal_tac "a = p - #1") |
|
96 apply (rule_tac [2] zcong_zless_imp_eq) |
|
97 apply auto |
|
98 done |
|
99 |
|
100 text {* |
|
101 Below is slightly different as we don't expand @{term [source] inv} |
|
102 but use ``@{text correct}'' theorems. |
|
103 *} |
|
104 |
|
105 lemma inv_g_1: "p \<in> zprime ==> #1 < a ==> a < p - #1 ==> #1 < inv p a" |
|
106 apply (subgoal_tac "inv p a \<noteq> #1") |
|
107 apply (subgoal_tac "inv p a \<noteq> #0") |
|
108 apply (subst order_less_le) |
|
109 apply (subst zle_add1_eq_le [symmetric]) |
|
110 apply (subst order_less_le) |
|
111 apply (rule_tac [2] inv_not_0) |
|
112 apply (rule_tac [5] inv_not_1) |
|
113 apply auto |
|
114 apply (rule inv_ge) |
|
115 apply auto |
|
116 done |
|
117 |
|
118 lemma inv_less_p_minus_1: |
|
119 "p \<in> zprime ==> #1 < a ==> a < p - #1 ==> inv p a < p - #1" |
|
120 -- {* ditto *} |
|
121 apply (subst order_less_le) |
|
122 apply (simp add: inv_not_p_minus_1 inv_less) |
|
123 done |
|
124 |
|
125 |
|
126 text {* \medskip Bijection *} |
|
127 |
|
128 lemma aux1: "#1 < x ==> #0 \<le> (x::int)" |
|
129 apply auto |
|
130 done |
|
131 |
|
132 lemma aux2: "#1 < x ==> #0 < (x::int)" |
|
133 apply auto |
|
134 done |
|
135 |
|
136 lemma aux3: "x \<le> p - #2 ==> x < (p::int)" |
|
137 apply auto |
|
138 done |
|
139 |
|
140 lemma aux4: "x \<le> p - #2 ==> x < (p::int)-#1" |
|
141 apply auto |
|
142 done |
|
143 |
|
144 lemma inv_inj: "p \<in> zprime ==> inj_on (inv p) (d22set (p - #2))" |
|
145 apply (unfold inj_on_def) |
|
146 apply auto |
|
147 apply (rule zcong_zless_imp_eq) |
|
148 apply (tactic {* stac (thm "zcong_cancel" RS sym) 5 *}) |
|
149 apply (rule_tac [7] zcong_trans) |
|
150 apply (tactic {* stac (thm "zcong_sym") 8 *}) |
|
151 apply (erule_tac [7] inv_is_inv) |
|
152 apply (tactic "Asm_simp_tac 9") |
|
153 apply (erule_tac [9] inv_is_inv) |
|
154 apply (rule_tac [6] zless_zprime_imp_zrelprime) |
|
155 apply (rule_tac [8] inv_less) |
|
156 apply (rule_tac [7] inv_g_1 [THEN aux2]) |
|
157 apply (unfold zprime_def) |
|
158 apply (auto intro: d22set_g_1 d22set_le |
|
159 aux1 aux2 aux3 aux4) |
|
160 done |
|
161 |
|
162 lemma inv_d22set_d22set: |
|
163 "p \<in> zprime ==> inv p ` d22set (p - #2) = d22set (p - #2)" |
|
164 apply (rule endo_inj_surj) |
|
165 apply (rule d22set_fin) |
|
166 apply (erule_tac [2] inv_inj) |
|
167 apply auto |
|
168 apply (rule d22set_mem) |
|
169 apply (erule inv_g_1) |
|
170 apply (subgoal_tac [3] "inv p xa < p - #1") |
|
171 apply (erule_tac [4] inv_less_p_minus_1) |
|
172 apply (auto intro: d22set_g_1 d22set_le aux4) |
|
173 done |
|
174 |
|
175 lemma d22set_d22set_bij: |
|
176 "p \<in> zprime ==> (d22set (p - #2), d22set (p - #2)) \<in> bijR (reciR p)" |
|
177 apply (unfold reciR_def) |
|
178 apply (rule_tac s = "(d22set (p - #2), inv p ` d22set (p - #2))" in subst) |
|
179 apply (simp add: inv_d22set_d22set) |
|
180 apply (rule inj_func_bijR) |
|
181 apply (rule_tac [3] d22set_fin) |
|
182 apply (erule_tac [2] inv_inj) |
|
183 apply auto |
|
184 apply (erule inv_is_inv) |
|
185 apply (erule_tac [5] inv_g_1) |
|
186 apply (erule_tac [7] inv_less_p_minus_1) |
|
187 apply (auto intro: d22set_g_1 d22set_le aux2 aux3 aux4) |
|
188 done |
|
189 |
|
190 lemma reciP_bijP: "p \<in> zprime ==> bijP (reciR p) (d22set (p - #2))" |
|
191 apply (unfold reciR_def bijP_def) |
|
192 apply auto |
|
193 apply (rule d22set_mem) |
|
194 apply auto |
|
195 done |
|
196 |
|
197 lemma reciP_uniq: "p \<in> zprime ==> uniqP (reciR p)" |
|
198 apply (unfold reciR_def uniqP_def) |
|
199 apply auto |
|
200 apply (rule zcong_zless_imp_eq) |
|
201 apply (tactic {* stac (thm "zcong_cancel2" RS sym) 5 *}) |
|
202 apply (rule_tac [7] zcong_trans) |
|
203 apply (tactic {* stac (thm "zcong_sym") 8 *}) |
|
204 apply (rule_tac [6] zless_zprime_imp_zrelprime) |
|
205 apply auto |
|
206 apply (rule zcong_zless_imp_eq) |
|
207 apply (tactic {* stac (thm "zcong_cancel" RS sym) 5 *}) |
|
208 apply (rule_tac [7] zcong_trans) |
|
209 apply (tactic {* stac (thm "zcong_sym") 8 *}) |
|
210 apply (rule_tac [6] zless_zprime_imp_zrelprime) |
|
211 apply auto |
|
212 done |
|
213 |
|
214 lemma reciP_sym: "p \<in> zprime ==> symP (reciR p)" |
|
215 apply (unfold reciR_def symP_def) |
|
216 apply (simp add: zmult_commute) |
|
217 apply auto |
|
218 done |
|
219 |
|
220 lemma bijER_d22set: "p \<in> zprime ==> d22set (p - #2) \<in> bijER (reciR p)" |
|
221 apply (rule bijR_bijER) |
|
222 apply (erule d22set_d22set_bij) |
|
223 apply (erule reciP_bijP) |
|
224 apply (erule reciP_uniq) |
|
225 apply (erule reciP_sym) |
|
226 done |
|
227 |
|
228 |
|
229 subsection {* Wilson *} |
|
230 |
|
231 lemma bijER_zcong_prod_1: |
|
232 "p \<in> zprime ==> A \<in> bijER (reciR p) ==> [setprod A = #1] (mod p)" |
|
233 apply (unfold reciR_def) |
|
234 apply (erule bijER.induct) |
|
235 apply (subgoal_tac [2] "a = #1 \<or> a = p - #1") |
|
236 apply (rule_tac [3] zcong_square_zless) |
|
237 apply auto |
|
238 apply (subst setprod_insert) |
|
239 prefer 3 |
|
240 apply (subst setprod_insert) |
|
241 apply (auto simp add: fin_bijER) |
|
242 apply (subgoal_tac "zcong ((a * b) * setprod A) (#1 * #1) p") |
|
243 apply (simp add: zmult_assoc) |
|
244 apply (rule zcong_zmult) |
|
245 apply auto |
|
246 done |
|
247 |
|
248 theorem Wilson_Bij: "p \<in> zprime ==> [zfact (p - #1) = #-1] (mod p)" |
|
249 apply (subgoal_tac "zcong ((p - #1) * zfact (p - #2)) (#-1 * #1) p") |
|
250 apply (rule_tac [2] zcong_zmult) |
|
251 apply (simp add: zprime_def) |
|
252 apply (subst zfact.simps) |
|
253 apply (rule_tac t = "p - #1 - #1" and s = "p - #2" in subst) |
|
254 apply auto |
|
255 apply (simp add: zcong_def) |
|
256 apply (subst d22set_prod_zfact [symmetric]) |
|
257 apply (rule bijER_zcong_prod_1) |
|
258 apply (rule_tac [2] bijER_d22set) |
|
259 apply auto |
|
260 done |
19 |
261 |
20 end |
262 end |