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