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