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