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