src/HOL/NumberTheory/WilsonBij.thy
author haftmann
Wed Sep 26 20:27:55 2007 +0200 (2007-09-26)
changeset 24728 e2b3a1065676
parent 23894 1a4167d761ac
child 30042 31039ee583fa
permissions -rw-r--r--
moved Finite_Set before Datatype
     1 (*  Title:      HOL/NumberTheory/WilsonBij.thy
     2     ID:         $Id$
     3     Author:     Thomas M. Rasmussen
     4     Copyright   2000  University of Cambridge
     5 *)
     6 
     7 header {* Wilson's Theorem using a more abstract approach *}
     8 
     9 theory WilsonBij imports BijectionRel IntFact begin
    10 
    11 text {*
    12   Wilson's Theorem using a more ``abstract'' approach based on
    13   bijections between sets.  Does not use Fermat's Little Theorem
    14   (unlike Russinoff).
    15 *}
    16 
    17 
    18 subsection {* Definitions and lemmas *}
    19 
    20 definition
    21   reciR :: "int => int => int => bool" where
    22   "reciR p = (\<lambda>a b. zcong (a * b) 1 p \<and> 1 < a \<and> a < p - 1 \<and> 1 < b \<and> b < p - 1)"
    23 
    24 definition
    25   inv :: "int => int => int" where
    26   "inv p a =
    27     (if zprime p \<and> 0 < a \<and> a < p then
    28       (SOME x. 0 \<le> x \<and> x < p \<and> zcong (a * x) 1 p)
    29      else 0)"
    30 
    31 
    32 text {* \medskip Inverse *}
    33 
    34 lemma inv_correct:
    35   "zprime p ==> 0 < a ==> a < p
    36     ==> 0 \<le> inv p a \<and> inv p a < p \<and> [a * inv p a = 1] (mod p)"
    37   apply (unfold inv_def)
    38   apply (simp (no_asm_simp))
    39   apply (rule zcong_lineq_unique [THEN ex1_implies_ex, THEN someI_ex])
    40    apply (erule_tac [2] zless_zprime_imp_zrelprime)
    41     apply (unfold zprime_def)
    42     apply auto
    43   done
    44 
    45 lemmas inv_ge = inv_correct [THEN conjunct1, standard]
    46 lemmas inv_less = inv_correct [THEN conjunct2, THEN conjunct1, standard]
    47 lemmas inv_is_inv = inv_correct [THEN conjunct2, THEN conjunct2, standard]
    48 
    49 lemma inv_not_0:
    50   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> 0"
    51   -- {* same as @{text WilsonRuss} *}
    52   apply safe
    53   apply (cut_tac a = a and p = p in inv_is_inv)
    54      apply (unfold zcong_def)
    55      apply auto
    56   apply (subgoal_tac "\<not> p dvd 1")
    57    apply (rule_tac [2] zdvd_not_zless)
    58     apply (subgoal_tac "p dvd 1")
    59      prefer 2
    60      apply (subst zdvd_zminus_iff [symmetric])
    61      apply auto
    62   done
    63 
    64 lemma inv_not_1:
    65   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> 1"
    66   -- {* same as @{text WilsonRuss} *}
    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)
    73           apply auto
    74   done
    75 
    76 lemma aux: "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
    77   -- {* same as @{text WilsonRuss} *}
    78   apply (unfold zcong_def)
    79   apply (simp add: OrderedGroup.diff_diff_eq diff_diff_eq2 zdiff_zmult_distrib2)
    80   apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
    81    apply (simp add: mult_commute)
    82   apply (subst zdvd_zminus_iff)
    83   apply (subst zdvd_reduce)
    84   apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
    85    apply (subst zdvd_reduce)
    86    apply auto
    87   done
    88 
    89 lemma inv_not_p_minus_1:
    90   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> p - 1"
    91   -- {* same as @{text WilsonRuss} *}
    92   apply safe
    93   apply (cut_tac a = a and p = p in inv_is_inv)
    94      apply auto
    95   apply (simp add: aux)
    96   apply (subgoal_tac "a = p - 1")
    97    apply (rule_tac [2] zcong_zless_imp_eq)
    98        apply auto
    99   done
   100 
   101 text {*
   102   Below is slightly different as we don't expand @{term [source] inv}
   103   but use ``@{text correct}'' theorems.
   104 *}
   105 
   106 lemma inv_g_1: "zprime p ==> 1 < a ==> a < p - 1 ==> 1 < 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)
   114         apply auto
   115   apply (rule inv_ge)
   116     apply auto
   117   done
   118 
   119 lemma inv_less_p_minus_1:
   120   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a < p - 1"
   121   -- {* ditto *}
   122   apply (subst order_less_le)
   123   apply (simp add: inv_not_p_minus_1 inv_less)
   124   done
   125 
   126 
   127 text {* \medskip Bijection *}
   128 
   129 lemma aux1: "1 < x ==> 0 \<le> (x::int)"
   130   apply auto
   131   done
   132 
   133 lemma aux2: "1 < x ==> 0 < (x::int)"
   134   apply auto
   135   done
   136 
   137 lemma aux3: "x \<le> p - 2 ==> x < (p::int)"
   138   apply auto
   139   done
   140 
   141 lemma aux4: "x \<le> p - 2 ==> x < (p::int) - 1"
   142   apply auto
   143   done
   144 
   145 lemma inv_inj: "zprime p ==> inj_on (inv p) (d22set (p - 2))"
   146   apply (unfold inj_on_def)
   147   apply auto
   148   apply (rule zcong_zless_imp_eq)
   149       apply (tactic {* stac (thm "zcong_cancel" RS sym) 5 *})
   150         apply (rule_tac [7] zcong_trans)
   151          apply (tactic {* stac (thm "zcong_sym") 8 *})
   152          apply (erule_tac [7] inv_is_inv)
   153           apply (tactic "asm_simp_tac @{simpset} 9")
   154           apply (erule_tac [9] inv_is_inv)
   155            apply (rule_tac [6] zless_zprime_imp_zrelprime)
   156              apply (rule_tac [8] inv_less)
   157                apply (rule_tac [7] inv_g_1 [THEN aux2])
   158                  apply (unfold zprime_def)
   159                  apply (auto intro: d22set_g_1 d22set_le
   160 		   aux1 aux2 aux3 aux4)
   161   done
   162 
   163 lemma inv_d22set_d22set:
   164     "zprime p ==> inv p ` d22set (p - 2) = d22set (p - 2)"
   165   apply (rule endo_inj_surj)
   166     apply (rule d22set_fin)
   167    apply (erule_tac [2] inv_inj)
   168   apply auto
   169   apply (rule d22set_mem)
   170    apply (erule inv_g_1)
   171     apply (subgoal_tac [3] "inv p xa < p - 1")
   172      apply (erule_tac [4] inv_less_p_minus_1)
   173       apply (auto intro: d22set_g_1 d22set_le aux4)
   174   done
   175 
   176 lemma d22set_d22set_bij:
   177     "zprime p ==> (d22set (p - 2), d22set (p - 2)) \<in> bijR (reciR p)"
   178   apply (unfold reciR_def)
   179   apply (rule_tac s = "(d22set (p - 2), inv p ` d22set (p - 2))" in subst)
   180    apply (simp add: inv_d22set_d22set)
   181   apply (rule inj_func_bijR)
   182     apply (rule_tac [3] d22set_fin)
   183    apply (erule_tac [2] inv_inj)
   184   apply auto
   185       apply (erule inv_is_inv)
   186        apply (erule_tac [5] inv_g_1)
   187         apply (erule_tac [7] inv_less_p_minus_1)
   188          apply (auto intro: d22set_g_1 d22set_le aux2 aux3 aux4)
   189   done
   190 
   191 lemma reciP_bijP: "zprime p ==> bijP (reciR p) (d22set (p - 2))"
   192   apply (unfold reciR_def bijP_def)
   193   apply auto
   194   apply (rule d22set_mem)
   195    apply auto
   196   done
   197 
   198 lemma reciP_uniq: "zprime p ==> uniqP (reciR p)"
   199   apply (unfold reciR_def uniqP_def)
   200   apply auto
   201    apply (rule zcong_zless_imp_eq)
   202        apply (tactic {* stac (thm "zcong_cancel2" RS sym) 5 *})
   203          apply (rule_tac [7] zcong_trans)
   204           apply (tactic {* stac (thm "zcong_sym") 8 *})
   205           apply (rule_tac [6] zless_zprime_imp_zrelprime)
   206             apply auto
   207   apply (rule zcong_zless_imp_eq)
   208       apply (tactic {* stac (thm "zcong_cancel" RS sym) 5 *})
   209         apply (rule_tac [7] zcong_trans)
   210          apply (tactic {* stac (thm "zcong_sym") 8 *})
   211          apply (rule_tac [6] zless_zprime_imp_zrelprime)
   212            apply auto
   213   done
   214 
   215 lemma reciP_sym: "zprime p ==> symP (reciR p)"
   216   apply (unfold reciR_def symP_def)
   217   apply (simp add: zmult_commute)
   218   apply auto
   219   done
   220 
   221 lemma bijER_d22set: "zprime p ==> d22set (p - 2) \<in> bijER (reciR p)"
   222   apply (rule bijR_bijER)
   223      apply (erule d22set_d22set_bij)
   224     apply (erule reciP_bijP)
   225    apply (erule reciP_uniq)
   226   apply (erule reciP_sym)
   227   done
   228 
   229 
   230 subsection {* Wilson *}
   231 
   232 lemma bijER_zcong_prod_1:
   233     "zprime p ==> A \<in> bijER (reciR p) ==> [\<Prod>A = 1] (mod p)"
   234   apply (unfold reciR_def)
   235   apply (erule bijER.induct)
   236     apply (subgoal_tac [2] "a = 1 \<or> a = p - 1")
   237      apply (rule_tac [3] zcong_square_zless)
   238         apply auto
   239   apply (subst setprod_insert)
   240     prefer 3
   241     apply (subst setprod_insert)
   242       apply (auto simp add: fin_bijER)
   243   apply (subgoal_tac "zcong ((a * b) * \<Prod>A) (1 * 1) p")
   244    apply (simp add: zmult_assoc)
   245   apply (rule zcong_zmult)
   246    apply auto
   247   done
   248 
   249 theorem Wilson_Bij: "zprime p ==> [zfact (p - 1) = -1] (mod p)"
   250   apply (subgoal_tac "zcong ((p - 1) * zfact (p - 2)) (-1 * 1) p")
   251    apply (rule_tac [2] zcong_zmult)
   252     apply (simp add: zprime_def)
   253     apply (subst zfact.simps)
   254     apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst)
   255      apply auto
   256    apply (simp add: zcong_def)
   257   apply (subst d22set_prod_zfact [symmetric])
   258   apply (rule bijER_zcong_prod_1)
   259    apply (rule_tac [2] bijER_d22set)
   260    apply auto
   261   done
   262 
   263 end