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