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