src/HOL/Old_Number_Theory/WilsonRuss.thy
author krauss
Tue Mar 02 12:26:50 2010 +0100 (2010-03-02)
changeset 35440 bdf8ad377877
parent 35048 82ab78fff970
child 38159 e9b4835a54ee
permissions -rw-r--r--
killed more recdefs
haftmann@32479
     1
(*  Author:     Thomas M. Rasmussen
wenzelm@11049
     2
    Copyright   2000  University of Cambridge
paulson@9508
     3
*)
paulson@9508
     4
wenzelm@11049
     5
header {* Wilson's Theorem according to Russinoff *}
wenzelm@11049
     6
haftmann@16417
     7
theory WilsonRuss imports EulerFermat begin
wenzelm@11049
     8
wenzelm@11049
     9
text {*
wenzelm@11049
    10
  Wilson's Theorem following quite closely Russinoff's approach
wenzelm@11049
    11
  using Boyer-Moore (using finite sets instead of lists, though).
wenzelm@11049
    12
*}
wenzelm@11049
    13
wenzelm@11049
    14
subsection {* Definitions and lemmas *}
paulson@9508
    15
wenzelm@19670
    16
definition
wenzelm@21404
    17
  inv :: "int => int => int" where
wenzelm@19670
    18
  "inv p a = (a^(nat (p - 2))) mod p"
wenzelm@19670
    19
krauss@35440
    20
fun
krauss@35440
    21
  wset :: "int \<Rightarrow> int => int set"
krauss@35440
    22
where
krauss@35440
    23
  "wset a p =
paulson@11868
    24
    (if 1 < a then
krauss@35440
    25
      let ws = wset (a - 1) p
wenzelm@11049
    26
      in (if a \<in> ws then ws else insert a (insert (inv p a) ws)) else {})"
wenzelm@11049
    27
wenzelm@11049
    28
wenzelm@11049
    29
text {* \medskip @{term [source] inv} *}
wenzelm@11049
    30
wenzelm@13524
    31
lemma inv_is_inv_aux: "1 < m ==> Suc (nat (m - 2)) = nat (m - 1)"
paulson@13833
    32
by (subst int_int_eq [symmetric], auto)
wenzelm@11049
    33
wenzelm@11049
    34
lemma inv_is_inv:
nipkow@16663
    35
    "zprime p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> [a * inv p a = 1] (mod p)"
wenzelm@11049
    36
  apply (unfold inv_def)
wenzelm@11049
    37
  apply (subst zcong_zmod)
wenzelm@11049
    38
  apply (subst zmod_zmult1_eq [symmetric])
wenzelm@11049
    39
  apply (subst zcong_zmod [symmetric])
wenzelm@11049
    40
  apply (subst power_Suc [symmetric])
wenzelm@13524
    41
  apply (subst inv_is_inv_aux)
wenzelm@11049
    42
   apply (erule_tac [2] Little_Fermat)
wenzelm@11049
    43
   apply (erule_tac [2] zdvd_not_zless)
paulson@13833
    44
   apply (unfold zprime_def, auto)
wenzelm@11049
    45
  done
wenzelm@11049
    46
wenzelm@11049
    47
lemma inv_distinct:
nipkow@16663
    48
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> a \<noteq> inv p a"
wenzelm@11049
    49
  apply safe
wenzelm@11049
    50
  apply (cut_tac a = a and p = p in zcong_square)
paulson@13833
    51
     apply (cut_tac [3] a = a and p = p in inv_is_inv, auto)
paulson@11868
    52
   apply (subgoal_tac "a = 1")
wenzelm@11049
    53
    apply (rule_tac [2] m = p in zcong_zless_imp_eq)
paulson@11868
    54
        apply (subgoal_tac [7] "a = p - 1")
paulson@13833
    55
         apply (rule_tac [8] m = p in zcong_zless_imp_eq, auto)
wenzelm@11049
    56
  done
wenzelm@11049
    57
wenzelm@11049
    58
lemma inv_not_0:
nipkow@16663
    59
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 0"
wenzelm@11049
    60
  apply safe
wenzelm@11049
    61
  apply (cut_tac a = a and p = p in inv_is_inv)
paulson@13833
    62
     apply (unfold zcong_def, auto)
paulson@11868
    63
  apply (subgoal_tac "\<not> p dvd 1")
wenzelm@11049
    64
   apply (rule_tac [2] zdvd_not_zless)
paulson@11868
    65
    apply (subgoal_tac "p dvd 1")
wenzelm@11049
    66
     prefer 2
nipkow@30042
    67
     apply (subst dvd_minus_iff [symmetric], auto)
wenzelm@11049
    68
  done
wenzelm@11049
    69
wenzelm@11049
    70
lemma inv_not_1:
nipkow@16663
    71
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
wenzelm@11049
    72
  apply safe
wenzelm@11049
    73
  apply (cut_tac a = a and p = p in inv_is_inv)
wenzelm@11049
    74
     prefer 4
wenzelm@11049
    75
     apply simp
paulson@11868
    76
     apply (subgoal_tac "a = 1")
paulson@13833
    77
      apply (rule_tac [2] zcong_zless_imp_eq, auto)
wenzelm@11049
    78
  done
wenzelm@11049
    79
wenzelm@19670
    80
lemma inv_not_p_minus_1_aux:
wenzelm@19670
    81
    "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
wenzelm@11049
    82
  apply (unfold zcong_def)
haftmann@35048
    83
  apply (simp add: diff_diff_eq diff_diff_eq2 zdiff_zmult_distrib2)
paulson@11868
    84
  apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
haftmann@35048
    85
   apply (simp add: algebra_simps)
nipkow@30042
    86
  apply (subst dvd_minus_iff)
wenzelm@11049
    87
  apply (subst zdvd_reduce)
paulson@11868
    88
  apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
paulson@13833
    89
   apply (subst zdvd_reduce, auto)
wenzelm@11049
    90
  done
wenzelm@11049
    91
wenzelm@11049
    92
lemma inv_not_p_minus_1:
nipkow@16663
    93
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
wenzelm@11049
    94
  apply safe
paulson@13833
    95
  apply (cut_tac a = a and p = p in inv_is_inv, auto)
wenzelm@13524
    96
  apply (simp add: inv_not_p_minus_1_aux)
paulson@11868
    97
  apply (subgoal_tac "a = p - 1")
paulson@13833
    98
   apply (rule_tac [2] zcong_zless_imp_eq, auto)
wenzelm@11049
    99
  done
wenzelm@11049
   100
wenzelm@11049
   101
lemma inv_g_1:
nipkow@16663
   102
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
paulson@11868
   103
  apply (case_tac "0\<le> inv p a")
paulson@11868
   104
   apply (subgoal_tac "inv p a \<noteq> 1")
paulson@11868
   105
    apply (subgoal_tac "inv p a \<noteq> 0")
wenzelm@11049
   106
     apply (subst order_less_le)
wenzelm@11049
   107
     apply (subst zle_add1_eq_le [symmetric])
wenzelm@11049
   108
     apply (subst order_less_le)
wenzelm@11049
   109
     apply (rule_tac [2] inv_not_0)
paulson@13833
   110
       apply (rule_tac [5] inv_not_1, auto)
paulson@13833
   111
  apply (unfold inv_def zprime_def, simp)
wenzelm@11049
   112
  done
wenzelm@11049
   113
wenzelm@11049
   114
lemma inv_less_p_minus_1:
nipkow@16663
   115
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
wenzelm@11049
   116
  apply (case_tac "inv p a < p")
wenzelm@11049
   117
   apply (subst order_less_le)
paulson@13833
   118
   apply (simp add: inv_not_p_minus_1, auto)
paulson@13833
   119
  apply (unfold inv_def zprime_def, simp)
wenzelm@11049
   120
  done
wenzelm@11049
   121
wenzelm@13524
   122
lemma inv_inv_aux: "5 \<le> p ==>
paulson@11868
   123
    nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
wenzelm@11049
   124
  apply (subst int_int_eq [symmetric])
wenzelm@11049
   125
  apply (simp add: zmult_int [symmetric])
wenzelm@11049
   126
  apply (simp add: zdiff_zmult_distrib zdiff_zmult_distrib2)
wenzelm@11049
   127
  done
wenzelm@11049
   128
wenzelm@11049
   129
lemma zcong_zpower_zmult:
paulson@11868
   130
    "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
wenzelm@11049
   131
  apply (induct z)
wenzelm@11049
   132
   apply (auto simp add: zpower_zadd_distrib)
nipkow@15236
   133
  apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
paulson@13833
   134
   apply (rule_tac [2] zcong_zmult, simp_all)
wenzelm@11049
   135
  done
wenzelm@11049
   136
nipkow@16663
   137
lemma inv_inv: "zprime p \<Longrightarrow>
paulson@11868
   138
    5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
wenzelm@11049
   139
  apply (unfold inv_def)
wenzelm@11049
   140
  apply (subst zpower_zmod)
wenzelm@11049
   141
  apply (subst zpower_zpower)
wenzelm@11049
   142
  apply (rule zcong_zless_imp_eq)
wenzelm@11049
   143
      prefer 5
wenzelm@11049
   144
      apply (subst zcong_zmod)
wenzelm@11049
   145
      apply (subst mod_mod_trivial)
wenzelm@11049
   146
      apply (subst zcong_zmod [symmetric])
wenzelm@13524
   147
      apply (subst inv_inv_aux)
wenzelm@11049
   148
       apply (subgoal_tac [2]
wenzelm@32960
   149
         "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
wenzelm@11049
   150
        apply (rule_tac [3] zcong_zmult)
wenzelm@11049
   151
         apply (rule_tac [4] zcong_zpower_zmult)
wenzelm@11049
   152
         apply (erule_tac [4] Little_Fermat)
paulson@13833
   153
         apply (rule_tac [4] zdvd_not_zless, simp_all)
wenzelm@11049
   154
  done
wenzelm@11049
   155
wenzelm@11049
   156
wenzelm@11049
   157
text {* \medskip @{term wset} *}
wenzelm@11049
   158
wenzelm@11049
   159
declare wset.simps [simp del]
paulson@9508
   160
wenzelm@11049
   161
lemma wset_induct:
wenzelm@18369
   162
  assumes "!!a p. P {} a p"
wenzelm@19670
   163
    and "!!a p. 1 < (a::int) \<Longrightarrow>
krauss@35440
   164
      P (wset (a - 1) p) (a - 1) p ==> P (wset a p) a p"
krauss@35440
   165
  shows "P (wset u v) u v"
krauss@35440
   166
  apply (rule wset.induct)
krauss@35440
   167
  apply (case_tac "1 < a")
krauss@35440
   168
   apply (rule assms)
krauss@35440
   169
    apply (simp_all add: wset.simps assms)
wenzelm@18369
   170
  done
wenzelm@11049
   171
wenzelm@11049
   172
lemma wset_mem_imp_or [rule_format]:
krauss@35440
   173
  "1 < a \<Longrightarrow> b \<notin> wset (a - 1) p
krauss@35440
   174
    ==> b \<in> wset a p --> b = a \<or> b = inv p a"
wenzelm@11049
   175
  apply (subst wset.simps)
paulson@13833
   176
  apply (unfold Let_def, simp)
wenzelm@11049
   177
  done
wenzelm@11049
   178
krauss@35440
   179
lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset a p"
wenzelm@11049
   180
  apply (subst wset.simps)
paulson@13833
   181
  apply (unfold Let_def, simp)
wenzelm@11049
   182
  done
wenzelm@11049
   183
krauss@35440
   184
lemma wset_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1) p ==> b \<in> wset a p"
wenzelm@11049
   185
  apply (subst wset.simps)
paulson@13833
   186
  apply (unfold Let_def, auto)
wenzelm@11049
   187
  done
wenzelm@11049
   188
wenzelm@11049
   189
lemma wset_g_1 [rule_format]:
krauss@35440
   190
    "zprime p --> a < p - 1 --> b \<in> wset a p --> 1 < b"
paulson@13833
   191
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   192
  apply (case_tac "b = a")
wenzelm@11049
   193
   apply (case_tac [2] "b = inv p a")
wenzelm@11049
   194
    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
wenzelm@11049
   195
     apply (rule_tac [4] wset_mem_imp_or)
wenzelm@11049
   196
       prefer 2
wenzelm@11049
   197
       apply simp
paulson@13833
   198
       apply (rule inv_g_1, auto)
wenzelm@11049
   199
  done
wenzelm@11049
   200
wenzelm@11049
   201
lemma wset_less [rule_format]:
krauss@35440
   202
    "zprime p --> a < p - 1 --> b \<in> wset a p --> b < p - 1"
paulson@13833
   203
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   204
  apply (case_tac "b = a")
wenzelm@11049
   205
   apply (case_tac [2] "b = inv p a")
wenzelm@11049
   206
    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
wenzelm@11049
   207
     apply (rule_tac [4] wset_mem_imp_or)
wenzelm@11049
   208
       prefer 2
wenzelm@11049
   209
       apply simp
paulson@13833
   210
       apply (rule inv_less_p_minus_1, auto)
wenzelm@11049
   211
  done
wenzelm@11049
   212
wenzelm@11049
   213
lemma wset_mem [rule_format]:
nipkow@16663
   214
  "zprime p -->
krauss@35440
   215
    a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset a p"
paulson@13833
   216
  apply (induct a p rule: wset.induct, auto)
nipkow@15197
   217
  apply (rule_tac wset_subset)
nipkow@15197
   218
  apply (simp (no_asm_simp))
nipkow@15197
   219
  apply auto
wenzelm@11049
   220
  done
wenzelm@11049
   221
wenzelm@11049
   222
lemma wset_mem_inv_mem [rule_format]:
krauss@35440
   223
  "zprime p --> 5 \<le> p --> a < p - 1 --> b \<in> wset a p
krauss@35440
   224
    --> inv p b \<in> wset a p"
paulson@13833
   225
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   226
   apply (case_tac "b = a")
wenzelm@11049
   227
    apply (subst wset.simps)
wenzelm@11049
   228
    apply (unfold Let_def)
paulson@13833
   229
    apply (rule_tac [3] wset_subset, auto)
wenzelm@11049
   230
  apply (case_tac "b = inv p a")
wenzelm@11049
   231
   apply (simp (no_asm_simp))
wenzelm@11049
   232
   apply (subst inv_inv)
wenzelm@11049
   233
       apply (subgoal_tac [6] "b = a \<or> b = inv p a")
paulson@13833
   234
        apply (rule_tac [7] wset_mem_imp_or, auto)
wenzelm@11049
   235
  done
wenzelm@11049
   236
wenzelm@11049
   237
lemma wset_inv_mem_mem:
nipkow@16663
   238
  "zprime p \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
krauss@35440
   239
    \<Longrightarrow> inv p b \<in> wset a p \<Longrightarrow> b \<in> wset a p"
wenzelm@11049
   240
  apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
wenzelm@11049
   241
   apply (rule_tac [2] wset_mem_inv_mem)
paulson@13833
   242
      apply (rule inv_inv, simp_all)
wenzelm@11049
   243
  done
wenzelm@11049
   244
krauss@35440
   245
lemma wset_fin: "finite (wset a p)"
wenzelm@11049
   246
  apply (induct a p rule: wset_induct)
wenzelm@11049
   247
   prefer 2
wenzelm@11049
   248
   apply (subst wset.simps)
paulson@13833
   249
   apply (unfold Let_def, auto)
wenzelm@11049
   250
  done
wenzelm@11049
   251
wenzelm@11049
   252
lemma wset_zcong_prod_1 [rule_format]:
nipkow@16663
   253
  "zprime p -->
krauss@35440
   254
    5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset a p. x) = 1] (mod p)"
wenzelm@11049
   255
  apply (induct a p rule: wset_induct)
wenzelm@11049
   256
   prefer 2
wenzelm@11049
   257
   apply (subst wset.simps)
krauss@35440
   258
   apply (auto, unfold Let_def, auto)
wenzelm@11049
   259
  apply (subst setprod_insert)
wenzelm@11049
   260
    apply (tactic {* stac (thm "setprod_insert") 3 *})
wenzelm@11049
   261
      apply (subgoal_tac [5]
krauss@35440
   262
        "zcong (a * inv p a * (\<Prod>x\<in>wset (a - 1) p. x)) (1 * 1) p")
wenzelm@11049
   263
       prefer 5
wenzelm@11049
   264
       apply (simp add: zmult_assoc)
wenzelm@11049
   265
      apply (rule_tac [5] zcong_zmult)
wenzelm@11049
   266
       apply (rule_tac [5] inv_is_inv)
wenzelm@23894
   267
         apply (tactic "clarify_tac @{claset} 4")
krauss@35440
   268
         apply (subgoal_tac [4] "a \<in> wset (a - 1) p")
wenzelm@11049
   269
          apply (rule_tac [5] wset_inv_mem_mem)
wenzelm@11049
   270
               apply (simp_all add: wset_fin)
paulson@13833
   271
  apply (rule inv_distinct, auto)
wenzelm@11049
   272
  done
wenzelm@11049
   273
krauss@35440
   274
lemma d22set_eq_wset: "zprime p ==> d22set (p - 2) = wset (p - 2) p"
wenzelm@11049
   275
  apply safe
wenzelm@11049
   276
   apply (erule wset_mem)
wenzelm@11049
   277
     apply (rule_tac [2] d22set_g_1)
wenzelm@11049
   278
     apply (rule_tac [3] d22set_le)
wenzelm@11049
   279
     apply (rule_tac [4] d22set_mem)
wenzelm@11049
   280
      apply (erule_tac [4] wset_g_1)
wenzelm@11049
   281
       prefer 6
wenzelm@11049
   282
       apply (subst zle_add1_eq_le [symmetric])
paulson@11868
   283
       apply (subgoal_tac "p - 2 + 1 = p - 1")
wenzelm@11049
   284
        apply (simp (no_asm_simp))
paulson@13833
   285
        apply (erule wset_less, auto)
wenzelm@11049
   286
  done
wenzelm@11049
   287
wenzelm@11049
   288
wenzelm@11049
   289
subsection {* Wilson *}
wenzelm@11049
   290
nipkow@16663
   291
lemma prime_g_5: "zprime p \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
wenzelm@11049
   292
  apply (unfold zprime_def dvd_def)
paulson@13833
   293
  apply (case_tac "p = 4", auto)
wenzelm@11049
   294
   apply (rule notE)
wenzelm@11049
   295
    prefer 2
wenzelm@11049
   296
    apply assumption
wenzelm@11049
   297
   apply (simp (no_asm))
paulson@13833
   298
   apply (rule_tac x = 2 in exI)
paulson@13833
   299
   apply (safe, arith)
paulson@13833
   300
     apply (rule_tac x = 2 in exI, auto)
wenzelm@11049
   301
  done
wenzelm@11049
   302
wenzelm@11049
   303
theorem Wilson_Russ:
nipkow@16663
   304
    "zprime p ==> [zfact (p - 1) = -1] (mod p)"
paulson@11868
   305
  apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
wenzelm@11049
   306
   apply (rule_tac [2] zcong_zmult)
wenzelm@11049
   307
    apply (simp only: zprime_def)
wenzelm@11049
   308
    apply (subst zfact.simps)
paulson@13833
   309
    apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
wenzelm@11049
   310
   apply (simp only: zcong_def)
wenzelm@11049
   311
   apply (simp (no_asm_simp))
wenzelm@11704
   312
  apply (case_tac "p = 2")
wenzelm@11049
   313
   apply (simp add: zfact.simps)
wenzelm@11704
   314
  apply (case_tac "p = 3")
wenzelm@11049
   315
   apply (simp add: zfact.simps)
wenzelm@11704
   316
  apply (subgoal_tac "5 \<le> p")
wenzelm@11049
   317
   apply (erule_tac [2] prime_g_5)
wenzelm@11049
   318
    apply (subst d22set_prod_zfact [symmetric])
wenzelm@11049
   319
    apply (subst d22set_eq_wset)
paulson@13833
   320
     apply (rule_tac [2] wset_zcong_prod_1, auto)
wenzelm@11049
   321
  done
paulson@9508
   322
paulson@9508
   323
end