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