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