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