src/HOL/Old_Number_Theory/WilsonRuss.thy
author wenzelm
Sat Oct 10 16:26:23 2015 +0200 (2015-10-10)
changeset 61382 efac889fccbc
parent 59498 50b60f501b05
child 61649 268d88ec9087
permissions -rw-r--r--
isabelle update_cartouches;
     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 (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 mod_mult_right_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   done
    64 
    65 lemma inv_not_1:
    66     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
    67   apply safe
    68   apply (cut_tac a = a and p = p in inv_is_inv)
    69      prefer 4
    70      apply simp
    71      apply (subgoal_tac "a = 1")
    72       apply (rule_tac [2] zcong_zless_imp_eq, auto)
    73   done
    74 
    75 lemma inv_not_p_minus_1_aux:
    76     "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
    77   apply (unfold zcong_def)
    78   apply (simp add: diff_diff_eq diff_diff_eq2 right_diff_distrib)
    79   apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
    80    apply (simp add: algebra_simps)
    81   apply (subst dvd_minus_iff)
    82   apply (subst zdvd_reduce)
    83   apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
    84    apply (subst zdvd_reduce, auto)
    85   done
    86 
    87 lemma inv_not_p_minus_1:
    88     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
    89   apply safe
    90   apply (cut_tac a = a and p = p in inv_is_inv, auto)
    91   apply (simp add: inv_not_p_minus_1_aux)
    92   apply (subgoal_tac "a = p - 1")
    93    apply (rule_tac [2] zcong_zless_imp_eq, auto)
    94   done
    95 
    96 lemma inv_g_1:
    97     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
    98   apply (case_tac "0\<le> inv p a")
    99    apply (subgoal_tac "inv p a \<noteq> 1")
   100     apply (subgoal_tac "inv p a \<noteq> 0")
   101      apply (subst order_less_le)
   102      apply (subst zle_add1_eq_le [symmetric])
   103      apply (subst order_less_le)
   104      apply (rule_tac [2] inv_not_0)
   105        apply (rule_tac [5] inv_not_1, auto)
   106   apply (unfold inv_def zprime_def, simp)
   107   done
   108 
   109 lemma inv_less_p_minus_1:
   110     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
   111   apply (case_tac "inv p a < p")
   112    apply (subst order_less_le)
   113    apply (simp add: inv_not_p_minus_1, auto)
   114   apply (unfold inv_def zprime_def, simp)
   115   done
   116 
   117 lemma inv_inv_aux: "5 \<le> p ==>
   118     nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
   119   apply (subst int_int_eq [symmetric])
   120   apply (simp add: of_nat_mult)
   121   apply (simp add: left_diff_distrib right_diff_distrib)
   122   done
   123 
   124 lemma zcong_zpower_zmult:
   125     "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
   126   apply (induct z)
   127    apply (auto simp add: power_add)
   128   apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
   129    apply (rule_tac [2] zcong_zmult, simp_all)
   130   done
   131 
   132 lemma inv_inv: "zprime p \<Longrightarrow>
   133     5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
   134   apply (unfold inv_def)
   135   apply (subst power_mod)
   136   apply (subst zpower_zpower)
   137   apply (rule zcong_zless_imp_eq)
   138       prefer 5
   139       apply (subst zcong_zmod)
   140       apply (subst mod_mod_trivial)
   141       apply (subst zcong_zmod [symmetric])
   142       apply (subst inv_inv_aux)
   143        apply (subgoal_tac [2]
   144          "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
   145         apply (rule_tac [3] zcong_zmult)
   146          apply (rule_tac [4] zcong_zpower_zmult)
   147          apply (erule_tac [4] Little_Fermat)
   148          apply (rule_tac [4] zdvd_not_zless, simp_all)
   149   done
   150 
   151 
   152 text \<open>\medskip @{term wset}\<close>
   153 
   154 declare wset.simps [simp del]
   155 
   156 lemma wset_induct:
   157   assumes "!!a p. P {} a p"
   158     and "!!a p. 1 < (a::int) \<Longrightarrow>
   159       P (wset (a - 1) p) (a - 1) p ==> P (wset a p) a p"
   160   shows "P (wset u v) u v"
   161   apply (rule wset.induct)
   162   apply (case_tac "1 < a")
   163    apply (rule assms)
   164     apply (simp_all add: wset.simps assms)
   165   done
   166 
   167 lemma wset_mem_imp_or [rule_format]:
   168   "1 < a \<Longrightarrow> b \<notin> wset (a - 1) p
   169     ==> b \<in> wset a p --> b = a \<or> b = inv p a"
   170   apply (subst wset.simps)
   171   apply (unfold Let_def, simp)
   172   done
   173 
   174 lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset a p"
   175   apply (subst wset.simps)
   176   apply (unfold Let_def, simp)
   177   done
   178 
   179 lemma wset_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1) p ==> b \<in> wset a p"
   180   apply (subst wset.simps)
   181   apply (unfold Let_def, auto)
   182   done
   183 
   184 lemma wset_g_1 [rule_format]:
   185     "zprime p --> a < p - 1 --> b \<in> wset a p --> 1 < b"
   186   apply (induct a p rule: wset_induct, auto)
   187   apply (case_tac "b = a")
   188    apply (case_tac [2] "b = inv p a")
   189     apply (subgoal_tac [3] "b = a \<or> b = inv p a")
   190      apply (rule_tac [4] wset_mem_imp_or)
   191        prefer 2
   192        apply simp
   193        apply (rule inv_g_1, auto)
   194   done
   195 
   196 lemma wset_less [rule_format]:
   197     "zprime p --> a < p - 1 --> b \<in> wset a p --> b < p - 1"
   198   apply (induct a p rule: wset_induct, auto)
   199   apply (case_tac "b = a")
   200    apply (case_tac [2] "b = inv p a")
   201     apply (subgoal_tac [3] "b = a \<or> b = inv p a")
   202      apply (rule_tac [4] wset_mem_imp_or)
   203        prefer 2
   204        apply simp
   205        apply (rule inv_less_p_minus_1, auto)
   206   done
   207 
   208 lemma wset_mem [rule_format]:
   209   "zprime p -->
   210     a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset a p"
   211   apply (induct a p rule: wset.induct, auto)
   212   apply (rule_tac wset_subset)
   213   apply (simp (no_asm_simp))
   214   apply auto
   215   done
   216 
   217 lemma wset_mem_inv_mem [rule_format]:
   218   "zprime p --> 5 \<le> p --> a < p - 1 --> b \<in> wset a p
   219     --> inv p b \<in> wset a p"
   220   apply (induct a p rule: wset_induct, auto)
   221    apply (case_tac "b = a")
   222     apply (subst wset.simps)
   223     apply (unfold Let_def)
   224     apply (rule_tac [3] wset_subset, auto)
   225   apply (case_tac "b = inv p a")
   226    apply (simp (no_asm_simp))
   227    apply (subst inv_inv)
   228        apply (subgoal_tac [6] "b = a \<or> b = inv p a")
   229         apply (rule_tac [7] wset_mem_imp_or, auto)
   230   done
   231 
   232 lemma wset_inv_mem_mem:
   233   "zprime p \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
   234     \<Longrightarrow> inv p b \<in> wset a p \<Longrightarrow> b \<in> wset a p"
   235   apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
   236    apply (rule_tac [2] wset_mem_inv_mem)
   237       apply (rule inv_inv, simp_all)
   238   done
   239 
   240 lemma wset_fin: "finite (wset a p)"
   241   apply (induct a p rule: wset_induct)
   242    prefer 2
   243    apply (subst wset.simps)
   244    apply (unfold Let_def, auto)
   245   done
   246 
   247 lemma wset_zcong_prod_1 [rule_format]:
   248   "zprime p -->
   249     5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset a p. x) = 1] (mod p)"
   250   apply (induct a p rule: wset_induct)
   251    prefer 2
   252    apply (subst wset.simps)
   253    apply (auto, unfold Let_def, auto)
   254   apply (subst setprod.insert)
   255     apply (tactic \<open>stac @{context} @{thm setprod.insert} 3\<close>)
   256       apply (subgoal_tac [5]
   257         "zcong (a * inv p a * (\<Prod>x\<in>wset (a - 1) p. x)) (1 * 1) p")
   258        prefer 5
   259        apply (simp add: mult.assoc)
   260       apply (rule_tac [5] zcong_zmult)
   261        apply (rule_tac [5] inv_is_inv)
   262          apply (tactic "clarify_tac @{context} 4")
   263          apply (subgoal_tac [4] "a \<in> wset (a - 1) p")
   264           apply (rule_tac [5] wset_inv_mem_mem)
   265                apply (simp_all add: wset_fin)
   266   apply (rule inv_distinct, auto)
   267   done
   268 
   269 lemma d22set_eq_wset: "zprime p ==> d22set (p - 2) = wset (p - 2) p"
   270   apply safe
   271    apply (erule wset_mem)
   272      apply (rule_tac [2] d22set_g_1)
   273      apply (rule_tac [3] d22set_le)
   274      apply (rule_tac [4] d22set_mem)
   275       apply (erule_tac [4] wset_g_1)
   276        prefer 6
   277        apply (subst zle_add1_eq_le [symmetric])
   278        apply (subgoal_tac "p - 2 + 1 = p - 1")
   279         apply (simp (no_asm_simp))
   280         apply (erule wset_less, auto)
   281   done
   282 
   283 
   284 subsection \<open>Wilson\<close>
   285 
   286 lemma prime_g_5: "zprime p \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
   287   apply (unfold zprime_def dvd_def)
   288   apply (case_tac "p = 4", auto)
   289    apply (rule notE)
   290     prefer 2
   291     apply assumption
   292    apply (simp (no_asm))
   293    apply (rule_tac x = 2 in exI)
   294    apply (safe, arith)
   295      apply (rule_tac x = 2 in exI, auto)
   296   done
   297 
   298 theorem Wilson_Russ:
   299     "zprime p ==> [zfact (p - 1) = -1] (mod p)"
   300   apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
   301    apply (rule_tac [2] zcong_zmult)
   302     apply (simp only: zprime_def)
   303     apply (subst zfact.simps)
   304     apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
   305    apply (simp only: zcong_def)
   306    apply (simp (no_asm_simp))
   307   apply (case_tac "p = 2")
   308    apply (simp add: zfact.simps)
   309   apply (case_tac "p = 3")
   310    apply (simp add: zfact.simps)
   311   apply (subgoal_tac "5 \<le> p")
   312    apply (erule_tac [2] prime_g_5)
   313     apply (subst d22set_prod_zfact [symmetric])
   314     apply (subst d22set_eq_wset)
   315      apply (rule_tac [2] wset_zcong_prod_1, auto)
   316   done
   317 
   318 end