src/HOL/NumberTheory/WilsonRuss.thy
 author obua Mon Apr 10 16:00:34 2006 +0200 (2006-04-10) changeset 19404 9bf2cdc9e8e8 parent 18369 694ea14ab4f2 child 19670 2e4a143c73c5 permissions -rw-r--r--
Moved stuff from Ring_and_Field to Matrix
```     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 consts
```
```    19   inv :: "int => int => int"
```
```    20   wset :: "int * int => int set"
```
```    21
```
```    22 defs
```
```    23   inv_def: "inv p a == (a^(nat (p - 2))) mod p"
```
```    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: "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
```
```    85   apply (unfold zcong_def)
```
```    86   apply (simp add: OrderedGroup.diff_diff_eq diff_diff_eq2 zdiff_zmult_distrib2)
```
```    87   apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
```
```    88    apply (simp add: mult_commute)
```
```    89   apply (subst zdvd_zminus_iff)
```
```    90   apply (subst zdvd_reduce)
```
```    91   apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
```
```    92    apply (subst zdvd_reduce, auto)
```
```    93   done
```
```    94
```
```    95 lemma inv_not_p_minus_1:
```
```    96     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
```
```    97   apply safe
```
```    98   apply (cut_tac a = a and p = p in inv_is_inv, auto)
```
```    99   apply (simp add: inv_not_p_minus_1_aux)
```
```   100   apply (subgoal_tac "a = p - 1")
```
```   101    apply (rule_tac [2] zcong_zless_imp_eq, auto)
```
```   102   done
```
```   103
```
```   104 lemma inv_g_1:
```
```   105     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
```
```   106   apply (case_tac "0\<le> inv p a")
```
```   107    apply (subgoal_tac "inv p a \<noteq> 1")
```
```   108     apply (subgoal_tac "inv p a \<noteq> 0")
```
```   109      apply (subst order_less_le)
```
```   110      apply (subst zle_add1_eq_le [symmetric])
```
```   111      apply (subst order_less_le)
```
```   112      apply (rule_tac [2] inv_not_0)
```
```   113        apply (rule_tac [5] inv_not_1, auto)
```
```   114   apply (unfold inv_def zprime_def, simp)
```
```   115   done
```
```   116
```
```   117 lemma inv_less_p_minus_1:
```
```   118     "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
```
```   119   apply (case_tac "inv p a < p")
```
```   120    apply (subst order_less_le)
```
```   121    apply (simp add: inv_not_p_minus_1, auto)
```
```   122   apply (unfold inv_def zprime_def, simp)
```
```   123   done
```
```   124
```
```   125 lemma inv_inv_aux: "5 \<le> p ==>
```
```   126     nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
```
```   127   apply (subst int_int_eq [symmetric])
```
```   128   apply (simp add: zmult_int [symmetric])
```
```   129   apply (simp add: zdiff_zmult_distrib zdiff_zmult_distrib2)
```
```   130   done
```
```   131
```
```   132 lemma zcong_zpower_zmult:
```
```   133     "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
```
```   134   apply (induct z)
```
```   135    apply (auto simp add: zpower_zadd_distrib)
```
```   136   apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
```
```   137    apply (rule_tac [2] zcong_zmult, simp_all)
```
```   138   done
```
```   139
```
```   140 lemma inv_inv: "zprime p \<Longrightarrow>
```
```   141     5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
```
```   142   apply (unfold inv_def)
```
```   143   apply (subst zpower_zmod)
```
```   144   apply (subst zpower_zpower)
```
```   145   apply (rule zcong_zless_imp_eq)
```
```   146       prefer 5
```
```   147       apply (subst zcong_zmod)
```
```   148       apply (subst mod_mod_trivial)
```
```   149       apply (subst zcong_zmod [symmetric])
```
```   150       apply (subst inv_inv_aux)
```
```   151        apply (subgoal_tac [2]
```
```   152 	 "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
```
```   153         apply (rule_tac [3] zcong_zmult)
```
```   154          apply (rule_tac [4] zcong_zpower_zmult)
```
```   155          apply (erule_tac [4] Little_Fermat)
```
```   156          apply (rule_tac [4] zdvd_not_zless, simp_all)
```
```   157   done
```
```   158
```
```   159
```
```   160 text {* \medskip @{term wset} *}
```
```   161
```
```   162 declare wset.simps [simp del]
```
```   163
```
```   164 lemma wset_induct:
```
```   165   assumes "!!a p. P {} a p"
```
```   166     and "!!a p. 1 < (a::int) \<Longrightarrow> 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 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
```