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