src/HOL/Old_Number_Theory/WilsonRuss.thy
changeset 32479 521cc9bf2958
parent 30042 31039ee583fa
child 32960 69916a850301
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Old_Number_Theory/WilsonRuss.thy	Tue Sep 01 15:39:33 2009 +0200
     1.3 @@ -0,0 +1,327 @@
     1.4 +(*  Author:     Thomas M. Rasmussen
     1.5 +    Copyright   2000  University of Cambridge
     1.6 +*)
     1.7 +
     1.8 +header {* Wilson's Theorem according to Russinoff *}
     1.9 +
    1.10 +theory WilsonRuss imports EulerFermat begin
    1.11 +
    1.12 +text {*
    1.13 +  Wilson's Theorem following quite closely Russinoff's approach
    1.14 +  using Boyer-Moore (using finite sets instead of lists, though).
    1.15 +*}
    1.16 +
    1.17 +subsection {* Definitions and lemmas *}
    1.18 +
    1.19 +definition
    1.20 +  inv :: "int => int => int" where
    1.21 +  "inv p a = (a^(nat (p - 2))) mod p"
    1.22 +
    1.23 +consts
    1.24 +  wset :: "int * int => int set"
    1.25 +
    1.26 +recdef wset
    1.27 +  "measure ((\<lambda>(a, p). nat a) :: int * int => nat)"
    1.28 +  "wset (a, p) =
    1.29 +    (if 1 < a then
    1.30 +      let ws = wset (a - 1, p)
    1.31 +      in (if a \<in> ws then ws else insert a (insert (inv p a) ws)) else {})"
    1.32 +
    1.33 +
    1.34 +text {* \medskip @{term [source] inv} *}
    1.35 +
    1.36 +lemma inv_is_inv_aux: "1 < m ==> Suc (nat (m - 2)) = nat (m - 1)"
    1.37 +by (subst int_int_eq [symmetric], auto)
    1.38 +
    1.39 +lemma inv_is_inv:
    1.40 +    "zprime p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> [a * inv p a = 1] (mod p)"
    1.41 +  apply (unfold inv_def)
    1.42 +  apply (subst zcong_zmod)
    1.43 +  apply (subst zmod_zmult1_eq [symmetric])
    1.44 +  apply (subst zcong_zmod [symmetric])
    1.45 +  apply (subst power_Suc [symmetric])
    1.46 +  apply (subst inv_is_inv_aux)
    1.47 +   apply (erule_tac [2] Little_Fermat)
    1.48 +   apply (erule_tac [2] zdvd_not_zless)
    1.49 +   apply (unfold zprime_def, auto)
    1.50 +  done
    1.51 +
    1.52 +lemma inv_distinct:
    1.53 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> a \<noteq> inv p a"
    1.54 +  apply safe
    1.55 +  apply (cut_tac a = a and p = p in zcong_square)
    1.56 +     apply (cut_tac [3] a = a and p = p in inv_is_inv, auto)
    1.57 +   apply (subgoal_tac "a = 1")
    1.58 +    apply (rule_tac [2] m = p in zcong_zless_imp_eq)
    1.59 +        apply (subgoal_tac [7] "a = p - 1")
    1.60 +         apply (rule_tac [8] m = p in zcong_zless_imp_eq, auto)
    1.61 +  done
    1.62 +
    1.63 +lemma inv_not_0:
    1.64 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 0"
    1.65 +  apply safe
    1.66 +  apply (cut_tac a = a and p = p in inv_is_inv)
    1.67 +     apply (unfold zcong_def, auto)
    1.68 +  apply (subgoal_tac "\<not> p dvd 1")
    1.69 +   apply (rule_tac [2] zdvd_not_zless)
    1.70 +    apply (subgoal_tac "p dvd 1")
    1.71 +     prefer 2
    1.72 +     apply (subst dvd_minus_iff [symmetric], auto)
    1.73 +  done
    1.74 +
    1.75 +lemma inv_not_1:
    1.76 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
    1.77 +  apply safe
    1.78 +  apply (cut_tac a = a and p = p in inv_is_inv)
    1.79 +     prefer 4
    1.80 +     apply simp
    1.81 +     apply (subgoal_tac "a = 1")
    1.82 +      apply (rule_tac [2] zcong_zless_imp_eq, auto)
    1.83 +  done
    1.84 +
    1.85 +lemma inv_not_p_minus_1_aux:
    1.86 +    "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
    1.87 +  apply (unfold zcong_def)
    1.88 +  apply (simp add: OrderedGroup.diff_diff_eq diff_diff_eq2 zdiff_zmult_distrib2)
    1.89 +  apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
    1.90 +   apply (simp add: mult_commute)
    1.91 +  apply (subst dvd_minus_iff)
    1.92 +  apply (subst zdvd_reduce)
    1.93 +  apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
    1.94 +   apply (subst zdvd_reduce, auto)
    1.95 +  done
    1.96 +
    1.97 +lemma inv_not_p_minus_1:
    1.98 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
    1.99 +  apply safe
   1.100 +  apply (cut_tac a = a and p = p in inv_is_inv, auto)
   1.101 +  apply (simp add: inv_not_p_minus_1_aux)
   1.102 +  apply (subgoal_tac "a = p - 1")
   1.103 +   apply (rule_tac [2] zcong_zless_imp_eq, auto)
   1.104 +  done
   1.105 +
   1.106 +lemma inv_g_1:
   1.107 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
   1.108 +  apply (case_tac "0\<le> inv p a")
   1.109 +   apply (subgoal_tac "inv p a \<noteq> 1")
   1.110 +    apply (subgoal_tac "inv p a \<noteq> 0")
   1.111 +     apply (subst order_less_le)
   1.112 +     apply (subst zle_add1_eq_le [symmetric])
   1.113 +     apply (subst order_less_le)
   1.114 +     apply (rule_tac [2] inv_not_0)
   1.115 +       apply (rule_tac [5] inv_not_1, auto)
   1.116 +  apply (unfold inv_def zprime_def, simp)
   1.117 +  done
   1.118 +
   1.119 +lemma inv_less_p_minus_1:
   1.120 +    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
   1.121 +  apply (case_tac "inv p a < p")
   1.122 +   apply (subst order_less_le)
   1.123 +   apply (simp add: inv_not_p_minus_1, auto)
   1.124 +  apply (unfold inv_def zprime_def, simp)
   1.125 +  done
   1.126 +
   1.127 +lemma inv_inv_aux: "5 \<le> p ==>
   1.128 +    nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
   1.129 +  apply (subst int_int_eq [symmetric])
   1.130 +  apply (simp add: zmult_int [symmetric])
   1.131 +  apply (simp add: zdiff_zmult_distrib zdiff_zmult_distrib2)
   1.132 +  done
   1.133 +
   1.134 +lemma zcong_zpower_zmult:
   1.135 +    "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
   1.136 +  apply (induct z)
   1.137 +   apply (auto simp add: zpower_zadd_distrib)
   1.138 +  apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
   1.139 +   apply (rule_tac [2] zcong_zmult, simp_all)
   1.140 +  done
   1.141 +
   1.142 +lemma inv_inv: "zprime p \<Longrightarrow>
   1.143 +    5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
   1.144 +  apply (unfold inv_def)
   1.145 +  apply (subst zpower_zmod)
   1.146 +  apply (subst zpower_zpower)
   1.147 +  apply (rule zcong_zless_imp_eq)
   1.148 +      prefer 5
   1.149 +      apply (subst zcong_zmod)
   1.150 +      apply (subst mod_mod_trivial)
   1.151 +      apply (subst zcong_zmod [symmetric])
   1.152 +      apply (subst inv_inv_aux)
   1.153 +       apply (subgoal_tac [2]
   1.154 +	 "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
   1.155 +        apply (rule_tac [3] zcong_zmult)
   1.156 +         apply (rule_tac [4] zcong_zpower_zmult)
   1.157 +         apply (erule_tac [4] Little_Fermat)
   1.158 +         apply (rule_tac [4] zdvd_not_zless, simp_all)
   1.159 +  done
   1.160 +
   1.161 +
   1.162 +text {* \medskip @{term wset} *}
   1.163 +
   1.164 +declare wset.simps [simp del]
   1.165 +
   1.166 +lemma wset_induct:
   1.167 +  assumes "!!a p. P {} a p"
   1.168 +    and "!!a p. 1 < (a::int) \<Longrightarrow>
   1.169 +      P (wset (a - 1, p)) (a - 1) p ==> P (wset (a, p)) a p"
   1.170 +  shows "P (wset (u, v)) u v"
   1.171 +  apply (rule wset.induct, safe)
   1.172 +   prefer 2
   1.173 +   apply (case_tac "1 < a")
   1.174 +    apply (rule prems)
   1.175 +     apply simp_all
   1.176 +   apply (simp_all add: wset.simps prems)
   1.177 +  done
   1.178 +
   1.179 +lemma wset_mem_imp_or [rule_format]:
   1.180 +  "1 < a \<Longrightarrow> b \<notin> wset (a - 1, p)
   1.181 +    ==> b \<in> wset (a, p) --> b = a \<or> b = inv p a"
   1.182 +  apply (subst wset.simps)
   1.183 +  apply (unfold Let_def, simp)
   1.184 +  done
   1.185 +
   1.186 +lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset (a, p)"
   1.187 +  apply (subst wset.simps)
   1.188 +  apply (unfold Let_def, simp)
   1.189 +  done
   1.190 +
   1.191 +lemma wset_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1, p) ==> b \<in> wset (a, p)"
   1.192 +  apply (subst wset.simps)
   1.193 +  apply (unfold Let_def, auto)
   1.194 +  done
   1.195 +
   1.196 +lemma wset_g_1 [rule_format]:
   1.197 +    "zprime p --> a < p - 1 --> b \<in> wset (a, p) --> 1 < b"
   1.198 +  apply (induct a p rule: wset_induct, auto)
   1.199 +  apply (case_tac "b = a")
   1.200 +   apply (case_tac [2] "b = inv p a")
   1.201 +    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
   1.202 +     apply (rule_tac [4] wset_mem_imp_or)
   1.203 +       prefer 2
   1.204 +       apply simp
   1.205 +       apply (rule inv_g_1, auto)
   1.206 +  done
   1.207 +
   1.208 +lemma wset_less [rule_format]:
   1.209 +    "zprime p --> a < p - 1 --> b \<in> wset (a, p) --> b < p - 1"
   1.210 +  apply (induct a p rule: wset_induct, auto)
   1.211 +  apply (case_tac "b = a")
   1.212 +   apply (case_tac [2] "b = inv p a")
   1.213 +    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
   1.214 +     apply (rule_tac [4] wset_mem_imp_or)
   1.215 +       prefer 2
   1.216 +       apply simp
   1.217 +       apply (rule inv_less_p_minus_1, auto)
   1.218 +  done
   1.219 +
   1.220 +lemma wset_mem [rule_format]:
   1.221 +  "zprime p -->
   1.222 +    a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset (a, p)"
   1.223 +  apply (induct a p rule: wset.induct, auto)
   1.224 +  apply (rule_tac wset_subset)
   1.225 +  apply (simp (no_asm_simp))
   1.226 +  apply auto
   1.227 +  done
   1.228 +
   1.229 +lemma wset_mem_inv_mem [rule_format]:
   1.230 +  "zprime p --> 5 \<le> p --> a < p - 1 --> b \<in> wset (a, p)
   1.231 +    --> inv p b \<in> wset (a, p)"
   1.232 +  apply (induct a p rule: wset_induct, auto)
   1.233 +   apply (case_tac "b = a")
   1.234 +    apply (subst wset.simps)
   1.235 +    apply (unfold Let_def)
   1.236 +    apply (rule_tac [3] wset_subset, auto)
   1.237 +  apply (case_tac "b = inv p a")
   1.238 +   apply (simp (no_asm_simp))
   1.239 +   apply (subst inv_inv)
   1.240 +       apply (subgoal_tac [6] "b = a \<or> b = inv p a")
   1.241 +        apply (rule_tac [7] wset_mem_imp_or, auto)
   1.242 +  done
   1.243 +
   1.244 +lemma wset_inv_mem_mem:
   1.245 +  "zprime p \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
   1.246 +    \<Longrightarrow> inv p b \<in> wset (a, p) \<Longrightarrow> b \<in> wset (a, p)"
   1.247 +  apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
   1.248 +   apply (rule_tac [2] wset_mem_inv_mem)
   1.249 +      apply (rule inv_inv, simp_all)
   1.250 +  done
   1.251 +
   1.252 +lemma wset_fin: "finite (wset (a, p))"
   1.253 +  apply (induct a p rule: wset_induct)
   1.254 +   prefer 2
   1.255 +   apply (subst wset.simps)
   1.256 +   apply (unfold Let_def, auto)
   1.257 +  done
   1.258 +
   1.259 +lemma wset_zcong_prod_1 [rule_format]:
   1.260 +  "zprime p -->
   1.261 +    5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset(a, p). x) = 1] (mod p)"
   1.262 +  apply (induct a p rule: wset_induct)
   1.263 +   prefer 2
   1.264 +   apply (subst wset.simps)
   1.265 +   apply (unfold Let_def, auto)
   1.266 +  apply (subst setprod_insert)
   1.267 +    apply (tactic {* stac (thm "setprod_insert") 3 *})
   1.268 +      apply (subgoal_tac [5]
   1.269 +	"zcong (a * inv p a * (\<Prod>x\<in> wset(a - 1, p). x)) (1 * 1) p")
   1.270 +       prefer 5
   1.271 +       apply (simp add: zmult_assoc)
   1.272 +      apply (rule_tac [5] zcong_zmult)
   1.273 +       apply (rule_tac [5] inv_is_inv)
   1.274 +         apply (tactic "clarify_tac @{claset} 4")
   1.275 +         apply (subgoal_tac [4] "a \<in> wset (a - 1, p)")
   1.276 +          apply (rule_tac [5] wset_inv_mem_mem)
   1.277 +               apply (simp_all add: wset_fin)
   1.278 +  apply (rule inv_distinct, auto)
   1.279 +  done
   1.280 +
   1.281 +lemma d22set_eq_wset: "zprime p ==> d22set (p - 2) = wset (p - 2, p)"
   1.282 +  apply safe
   1.283 +   apply (erule wset_mem)
   1.284 +     apply (rule_tac [2] d22set_g_1)
   1.285 +     apply (rule_tac [3] d22set_le)
   1.286 +     apply (rule_tac [4] d22set_mem)
   1.287 +      apply (erule_tac [4] wset_g_1)
   1.288 +       prefer 6
   1.289 +       apply (subst zle_add1_eq_le [symmetric])
   1.290 +       apply (subgoal_tac "p - 2 + 1 = p - 1")
   1.291 +        apply (simp (no_asm_simp))
   1.292 +        apply (erule wset_less, auto)
   1.293 +  done
   1.294 +
   1.295 +
   1.296 +subsection {* Wilson *}
   1.297 +
   1.298 +lemma prime_g_5: "zprime p \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
   1.299 +  apply (unfold zprime_def dvd_def)
   1.300 +  apply (case_tac "p = 4", auto)
   1.301 +   apply (rule notE)
   1.302 +    prefer 2
   1.303 +    apply assumption
   1.304 +   apply (simp (no_asm))
   1.305 +   apply (rule_tac x = 2 in exI)
   1.306 +   apply (safe, arith)
   1.307 +     apply (rule_tac x = 2 in exI, auto)
   1.308 +  done
   1.309 +
   1.310 +theorem Wilson_Russ:
   1.311 +    "zprime p ==> [zfact (p - 1) = -1] (mod p)"
   1.312 +  apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
   1.313 +   apply (rule_tac [2] zcong_zmult)
   1.314 +    apply (simp only: zprime_def)
   1.315 +    apply (subst zfact.simps)
   1.316 +    apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
   1.317 +   apply (simp only: zcong_def)
   1.318 +   apply (simp (no_asm_simp))
   1.319 +  apply (case_tac "p = 2")
   1.320 +   apply (simp add: zfact.simps)
   1.321 +  apply (case_tac "p = 3")
   1.322 +   apply (simp add: zfact.simps)
   1.323 +  apply (subgoal_tac "5 \<le> p")
   1.324 +   apply (erule_tac [2] prime_g_5)
   1.325 +    apply (subst d22set_prod_zfact [symmetric])
   1.326 +    apply (subst d22set_eq_wset)
   1.327 +     apply (rule_tac [2] wset_zcong_prod_1, auto)
   1.328 +  done
   1.329 +
   1.330 +end