src/HOL/Old_Number_Theory/WilsonRuss.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 57512 cc97b347b301
child 58889 5b7a9633cfa8
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
wenzelm@38159
     1
(*  Title:      HOL/Old_Number_Theory/WilsonRuss.thy
wenzelm@38159
     2
    Author:     Thomas M. Rasmussen
wenzelm@11049
     3
    Copyright   2000  University of Cambridge
paulson@9508
     4
*)
paulson@9508
     5
wenzelm@11049
     6
header {* Wilson's Theorem according to Russinoff *}
wenzelm@11049
     7
wenzelm@38159
     8
theory WilsonRuss
wenzelm@38159
     9
imports EulerFermat
wenzelm@38159
    10
begin
wenzelm@11049
    11
wenzelm@11049
    12
text {*
wenzelm@11049
    13
  Wilson's Theorem following quite closely Russinoff's approach
wenzelm@11049
    14
  using Boyer-Moore (using finite sets instead of lists, though).
wenzelm@11049
    15
*}
wenzelm@11049
    16
wenzelm@11049
    17
subsection {* Definitions and lemmas *}
paulson@9508
    18
wenzelm@38159
    19
definition inv :: "int => int => int"
wenzelm@38159
    20
  where "inv p a = (a^(nat (p - 2))) mod p"
wenzelm@19670
    21
wenzelm@38159
    22
fun wset :: "int \<Rightarrow> int => int set" 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)"
wenzelm@38159
    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)
huffman@47163
    38
  apply (subst mod_mult_right_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)
wenzelm@11049
    63
  done
wenzelm@11049
    64
wenzelm@11049
    65
lemma inv_not_1:
nipkow@16663
    66
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> 1"
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")
paulson@13833
    72
      apply (rule_tac [2] zcong_zless_imp_eq, auto)
wenzelm@11049
    73
  done
wenzelm@11049
    74
wenzelm@19670
    75
lemma inv_not_p_minus_1_aux:
wenzelm@19670
    76
    "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
wenzelm@11049
    77
  apply (unfold zcong_def)
huffman@44766
    78
  apply (simp add: diff_diff_eq diff_diff_eq2 right_diff_distrib)
paulson@11868
    79
  apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
haftmann@35048
    80
   apply (simp add: algebra_simps)
nipkow@30042
    81
  apply (subst dvd_minus_iff)
wenzelm@11049
    82
  apply (subst zdvd_reduce)
paulson@11868
    83
  apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
paulson@13833
    84
   apply (subst zdvd_reduce, auto)
wenzelm@11049
    85
  done
wenzelm@11049
    86
wenzelm@11049
    87
lemma inv_not_p_minus_1:
nipkow@16663
    88
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a \<noteq> p - 1"
wenzelm@11049
    89
  apply safe
paulson@13833
    90
  apply (cut_tac a = a and p = p in inv_is_inv, auto)
wenzelm@13524
    91
  apply (simp add: inv_not_p_minus_1_aux)
paulson@11868
    92
  apply (subgoal_tac "a = p - 1")
paulson@13833
    93
   apply (rule_tac [2] zcong_zless_imp_eq, auto)
wenzelm@11049
    94
  done
wenzelm@11049
    95
wenzelm@11049
    96
lemma inv_g_1:
nipkow@16663
    97
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> 1 < inv p a"
paulson@11868
    98
  apply (case_tac "0\<le> inv p a")
paulson@11868
    99
   apply (subgoal_tac "inv p a \<noteq> 1")
paulson@11868
   100
    apply (subgoal_tac "inv p a \<noteq> 0")
wenzelm@11049
   101
     apply (subst order_less_le)
wenzelm@11049
   102
     apply (subst zle_add1_eq_le [symmetric])
wenzelm@11049
   103
     apply (subst order_less_le)
wenzelm@11049
   104
     apply (rule_tac [2] inv_not_0)
paulson@13833
   105
       apply (rule_tac [5] inv_not_1, auto)
paulson@13833
   106
  apply (unfold inv_def zprime_def, simp)
wenzelm@11049
   107
  done
wenzelm@11049
   108
wenzelm@11049
   109
lemma inv_less_p_minus_1:
nipkow@16663
   110
    "zprime p \<Longrightarrow> 1 < a \<Longrightarrow> a < p - 1 ==> inv p a < p - 1"
wenzelm@11049
   111
  apply (case_tac "inv p a < p")
wenzelm@11049
   112
   apply (subst order_less_le)
paulson@13833
   113
   apply (simp add: inv_not_p_minus_1, auto)
paulson@13833
   114
  apply (unfold inv_def zprime_def, simp)
wenzelm@11049
   115
  done
wenzelm@11049
   116
wenzelm@13524
   117
lemma inv_inv_aux: "5 \<le> p ==>
paulson@11868
   118
    nat (p - 2) * nat (p - 2) = Suc (nat (p - 1) * nat (p - 3))"
wenzelm@11049
   119
  apply (subst int_int_eq [symmetric])
huffman@44821
   120
  apply (simp add: of_nat_mult)
huffman@44766
   121
  apply (simp add: left_diff_distrib right_diff_distrib)
wenzelm@11049
   122
  done
wenzelm@11049
   123
wenzelm@11049
   124
lemma zcong_zpower_zmult:
paulson@11868
   125
    "[x^y = 1] (mod p) \<Longrightarrow> [x^(y * z) = 1] (mod p)"
wenzelm@11049
   126
  apply (induct z)
huffman@44766
   127
   apply (auto simp add: power_add)
nipkow@15236
   128
  apply (subgoal_tac "zcong (x^y * x^(y * z)) (1 * 1) p")
paulson@13833
   129
   apply (rule_tac [2] zcong_zmult, simp_all)
wenzelm@11049
   130
  done
wenzelm@11049
   131
nipkow@16663
   132
lemma inv_inv: "zprime p \<Longrightarrow>
paulson@11868
   133
    5 \<le> p \<Longrightarrow> 0 < a \<Longrightarrow> a < p ==> inv p (inv p a) = a"
wenzelm@11049
   134
  apply (unfold inv_def)
huffman@47164
   135
  apply (subst power_mod)
wenzelm@11049
   136
  apply (subst zpower_zpower)
wenzelm@11049
   137
  apply (rule zcong_zless_imp_eq)
wenzelm@11049
   138
      prefer 5
wenzelm@11049
   139
      apply (subst zcong_zmod)
wenzelm@11049
   140
      apply (subst mod_mod_trivial)
wenzelm@11049
   141
      apply (subst zcong_zmod [symmetric])
wenzelm@13524
   142
      apply (subst inv_inv_aux)
wenzelm@11049
   143
       apply (subgoal_tac [2]
wenzelm@32960
   144
         "zcong (a * a^(nat (p - 1) * nat (p - 3))) (a * 1) p")
wenzelm@11049
   145
        apply (rule_tac [3] zcong_zmult)
wenzelm@11049
   146
         apply (rule_tac [4] zcong_zpower_zmult)
wenzelm@11049
   147
         apply (erule_tac [4] Little_Fermat)
paulson@13833
   148
         apply (rule_tac [4] zdvd_not_zless, simp_all)
wenzelm@11049
   149
  done
wenzelm@11049
   150
wenzelm@11049
   151
wenzelm@11049
   152
text {* \medskip @{term wset} *}
wenzelm@11049
   153
wenzelm@11049
   154
declare wset.simps [simp del]
paulson@9508
   155
wenzelm@11049
   156
lemma wset_induct:
wenzelm@18369
   157
  assumes "!!a p. P {} a p"
wenzelm@19670
   158
    and "!!a p. 1 < (a::int) \<Longrightarrow>
krauss@35440
   159
      P (wset (a - 1) p) (a - 1) p ==> P (wset a p) a p"
krauss@35440
   160
  shows "P (wset u v) u v"
krauss@35440
   161
  apply (rule wset.induct)
krauss@35440
   162
  apply (case_tac "1 < a")
krauss@35440
   163
   apply (rule assms)
krauss@35440
   164
    apply (simp_all add: wset.simps assms)
wenzelm@18369
   165
  done
wenzelm@11049
   166
wenzelm@11049
   167
lemma wset_mem_imp_or [rule_format]:
krauss@35440
   168
  "1 < a \<Longrightarrow> b \<notin> wset (a - 1) p
krauss@35440
   169
    ==> b \<in> wset a p --> b = a \<or> b = inv p a"
wenzelm@11049
   170
  apply (subst wset.simps)
paulson@13833
   171
  apply (unfold Let_def, simp)
wenzelm@11049
   172
  done
wenzelm@11049
   173
krauss@35440
   174
lemma wset_mem_mem [simp]: "1 < a ==> a \<in> wset a p"
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_subset: "1 < a \<Longrightarrow> b \<in> wset (a - 1) p ==> b \<in> wset a p"
wenzelm@11049
   180
  apply (subst wset.simps)
paulson@13833
   181
  apply (unfold Let_def, auto)
wenzelm@11049
   182
  done
wenzelm@11049
   183
wenzelm@11049
   184
lemma wset_g_1 [rule_format]:
krauss@35440
   185
    "zprime p --> a < p - 1 --> b \<in> wset a p --> 1 < b"
paulson@13833
   186
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   187
  apply (case_tac "b = a")
wenzelm@11049
   188
   apply (case_tac [2] "b = inv p a")
wenzelm@11049
   189
    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
wenzelm@11049
   190
     apply (rule_tac [4] wset_mem_imp_or)
wenzelm@11049
   191
       prefer 2
wenzelm@11049
   192
       apply simp
paulson@13833
   193
       apply (rule inv_g_1, auto)
wenzelm@11049
   194
  done
wenzelm@11049
   195
wenzelm@11049
   196
lemma wset_less [rule_format]:
krauss@35440
   197
    "zprime p --> a < p - 1 --> b \<in> wset a p --> b < p - 1"
paulson@13833
   198
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   199
  apply (case_tac "b = a")
wenzelm@11049
   200
   apply (case_tac [2] "b = inv p a")
wenzelm@11049
   201
    apply (subgoal_tac [3] "b = a \<or> b = inv p a")
wenzelm@11049
   202
     apply (rule_tac [4] wset_mem_imp_or)
wenzelm@11049
   203
       prefer 2
wenzelm@11049
   204
       apply simp
paulson@13833
   205
       apply (rule inv_less_p_minus_1, auto)
wenzelm@11049
   206
  done
wenzelm@11049
   207
wenzelm@11049
   208
lemma wset_mem [rule_format]:
nipkow@16663
   209
  "zprime p -->
krauss@35440
   210
    a < p - 1 --> 1 < b --> b \<le> a --> b \<in> wset a p"
paulson@13833
   211
  apply (induct a p rule: wset.induct, auto)
nipkow@15197
   212
  apply (rule_tac wset_subset)
nipkow@15197
   213
  apply (simp (no_asm_simp))
nipkow@15197
   214
  apply auto
wenzelm@11049
   215
  done
wenzelm@11049
   216
wenzelm@11049
   217
lemma wset_mem_inv_mem [rule_format]:
krauss@35440
   218
  "zprime p --> 5 \<le> p --> a < p - 1 --> b \<in> wset a p
krauss@35440
   219
    --> inv p b \<in> wset a p"
paulson@13833
   220
  apply (induct a p rule: wset_induct, auto)
wenzelm@11049
   221
   apply (case_tac "b = a")
wenzelm@11049
   222
    apply (subst wset.simps)
wenzelm@11049
   223
    apply (unfold Let_def)
paulson@13833
   224
    apply (rule_tac [3] wset_subset, auto)
wenzelm@11049
   225
  apply (case_tac "b = inv p a")
wenzelm@11049
   226
   apply (simp (no_asm_simp))
wenzelm@11049
   227
   apply (subst inv_inv)
wenzelm@11049
   228
       apply (subgoal_tac [6] "b = a \<or> b = inv p a")
paulson@13833
   229
        apply (rule_tac [7] wset_mem_imp_or, auto)
wenzelm@11049
   230
  done
wenzelm@11049
   231
wenzelm@11049
   232
lemma wset_inv_mem_mem:
nipkow@16663
   233
  "zprime p \<Longrightarrow> 5 \<le> p \<Longrightarrow> a < p - 1 \<Longrightarrow> 1 < b \<Longrightarrow> b < p - 1
krauss@35440
   234
    \<Longrightarrow> inv p b \<in> wset a p \<Longrightarrow> b \<in> wset a p"
wenzelm@11049
   235
  apply (rule_tac s = "inv p (inv p b)" and t = b in subst)
wenzelm@11049
   236
   apply (rule_tac [2] wset_mem_inv_mem)
paulson@13833
   237
      apply (rule inv_inv, simp_all)
wenzelm@11049
   238
  done
wenzelm@11049
   239
krauss@35440
   240
lemma wset_fin: "finite (wset a p)"
wenzelm@11049
   241
  apply (induct a p rule: wset_induct)
wenzelm@11049
   242
   prefer 2
wenzelm@11049
   243
   apply (subst wset.simps)
paulson@13833
   244
   apply (unfold Let_def, auto)
wenzelm@11049
   245
  done
wenzelm@11049
   246
wenzelm@11049
   247
lemma wset_zcong_prod_1 [rule_format]:
nipkow@16663
   248
  "zprime p -->
krauss@35440
   249
    5 \<le> p --> a < p - 1 --> [(\<Prod>x\<in>wset a p. x) = 1] (mod p)"
wenzelm@11049
   250
  apply (induct a p rule: wset_induct)
wenzelm@11049
   251
   prefer 2
wenzelm@11049
   252
   apply (subst wset.simps)
krauss@35440
   253
   apply (auto, unfold Let_def, auto)
haftmann@57418
   254
  apply (subst setprod.insert)
haftmann@57418
   255
    apply (tactic {* stac @{thm setprod.insert} 3 *})
wenzelm@11049
   256
      apply (subgoal_tac [5]
krauss@35440
   257
        "zcong (a * inv p a * (\<Prod>x\<in>wset (a - 1) p. x)) (1 * 1) p")
wenzelm@11049
   258
       prefer 5
haftmann@57512
   259
       apply (simp add: mult.assoc)
wenzelm@11049
   260
      apply (rule_tac [5] zcong_zmult)
wenzelm@11049
   261
       apply (rule_tac [5] inv_is_inv)
wenzelm@42793
   262
         apply (tactic "clarify_tac @{context} 4")
krauss@35440
   263
         apply (subgoal_tac [4] "a \<in> wset (a - 1) p")
wenzelm@11049
   264
          apply (rule_tac [5] wset_inv_mem_mem)
wenzelm@11049
   265
               apply (simp_all add: wset_fin)
paulson@13833
   266
  apply (rule inv_distinct, auto)
wenzelm@11049
   267
  done
wenzelm@11049
   268
krauss@35440
   269
lemma d22set_eq_wset: "zprime p ==> d22set (p - 2) = wset (p - 2) p"
wenzelm@11049
   270
  apply safe
wenzelm@11049
   271
   apply (erule wset_mem)
wenzelm@11049
   272
     apply (rule_tac [2] d22set_g_1)
wenzelm@11049
   273
     apply (rule_tac [3] d22set_le)
wenzelm@11049
   274
     apply (rule_tac [4] d22set_mem)
wenzelm@11049
   275
      apply (erule_tac [4] wset_g_1)
wenzelm@11049
   276
       prefer 6
wenzelm@11049
   277
       apply (subst zle_add1_eq_le [symmetric])
paulson@11868
   278
       apply (subgoal_tac "p - 2 + 1 = p - 1")
wenzelm@11049
   279
        apply (simp (no_asm_simp))
paulson@13833
   280
        apply (erule wset_less, auto)
wenzelm@11049
   281
  done
wenzelm@11049
   282
wenzelm@11049
   283
wenzelm@11049
   284
subsection {* Wilson *}
wenzelm@11049
   285
nipkow@16663
   286
lemma prime_g_5: "zprime p \<Longrightarrow> p \<noteq> 2 \<Longrightarrow> p \<noteq> 3 ==> 5 \<le> p"
wenzelm@11049
   287
  apply (unfold zprime_def dvd_def)
paulson@13833
   288
  apply (case_tac "p = 4", auto)
wenzelm@11049
   289
   apply (rule notE)
wenzelm@11049
   290
    prefer 2
wenzelm@11049
   291
    apply assumption
wenzelm@11049
   292
   apply (simp (no_asm))
paulson@13833
   293
   apply (rule_tac x = 2 in exI)
paulson@13833
   294
   apply (safe, arith)
paulson@13833
   295
     apply (rule_tac x = 2 in exI, auto)
wenzelm@11049
   296
  done
wenzelm@11049
   297
wenzelm@11049
   298
theorem Wilson_Russ:
nipkow@16663
   299
    "zprime p ==> [zfact (p - 1) = -1] (mod p)"
paulson@11868
   300
  apply (subgoal_tac "[(p - 1) * zfact (p - 2) = -1 * 1] (mod p)")
wenzelm@11049
   301
   apply (rule_tac [2] zcong_zmult)
wenzelm@11049
   302
    apply (simp only: zprime_def)
wenzelm@11049
   303
    apply (subst zfact.simps)
paulson@13833
   304
    apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst, auto)
wenzelm@11049
   305
   apply (simp only: zcong_def)
wenzelm@11049
   306
   apply (simp (no_asm_simp))
wenzelm@11704
   307
  apply (case_tac "p = 2")
wenzelm@11049
   308
   apply (simp add: zfact.simps)
wenzelm@11704
   309
  apply (case_tac "p = 3")
wenzelm@11049
   310
   apply (simp add: zfact.simps)
wenzelm@11704
   311
  apply (subgoal_tac "5 \<le> p")
wenzelm@11049
   312
   apply (erule_tac [2] prime_g_5)
wenzelm@11049
   313
    apply (subst d22set_prod_zfact [symmetric])
wenzelm@11049
   314
    apply (subst d22set_eq_wset)
paulson@13833
   315
     apply (rule_tac [2] wset_zcong_prod_1, auto)
wenzelm@11049
   316
  done
paulson@9508
   317
paulson@9508
   318
end