src/HOL/NumberTheory/Euler.thy
author berghofe
Wed Feb 07 17:51:38 2007 +0100 (2007-02-07)
changeset 22274 ce1459004c8d
parent 21404 eb85850d3eb7
child 23373 ead82c82da9e
permissions -rw-r--r--
Adapted to changes in Finite_Set theory.
paulson@13871
     1
(*  Title:      HOL/Quadratic_Reciprocity/Euler.thy
kleing@14981
     2
    ID:         $Id$
paulson@13871
     3
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871
     4
*)
paulson@13871
     5
paulson@13871
     6
header {* Euler's criterion *}
paulson@13871
     7
wenzelm@16974
     8
theory Euler imports Residues EvenOdd begin
paulson@13871
     9
wenzelm@19670
    10
definition
wenzelm@21404
    11
  MultInvPair :: "int => int => int => int set" where
wenzelm@19670
    12
  "MultInvPair a p j = {StandardRes p j, StandardRes p (a * (MultInv p j))}"
wenzelm@19670
    13
wenzelm@21404
    14
definition
wenzelm@21404
    15
  SetS        :: "int => int => int set set" where
wenzelm@19670
    16
  "SetS        a p   =  (MultInvPair a p ` SRStar p)"
paulson@13871
    17
wenzelm@19670
    18
wenzelm@19670
    19
subsection {* Property for MultInvPair *}
paulson@13871
    20
wenzelm@19670
    21
lemma MultInvPair_prop1a:
wenzelm@19670
    22
  "[| zprime p; 2 < p; ~([a = 0](mod p));
wenzelm@19670
    23
      X \<in> (SetS a p); Y \<in> (SetS a p);
wenzelm@19670
    24
      ~((X \<inter> Y) = {}) |] ==> X = Y"
paulson@13871
    25
  apply (auto simp add: SetS_def)
wenzelm@16974
    26
  apply (drule StandardRes_SRStar_prop1a)+ defer 1
wenzelm@16974
    27
  apply (drule StandardRes_SRStar_prop1a)+
paulson@13871
    28
  apply (auto simp add: MultInvPair_def StandardRes_prop2 zcong_sym)
wenzelm@20369
    29
  apply (drule notE, rule MultInv_zcong_prop1, auto)[]
wenzelm@20369
    30
  apply (drule notE, rule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    31
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    32
  apply (drule MultInv_zcong_prop3, auto simp add: zcong_sym)[]
wenzelm@20369
    33
  apply (drule MultInv_zcong_prop1, auto)[]
wenzelm@20369
    34
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    35
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    36
  apply (drule MultInv_zcong_prop3, auto simp add: zcong_sym)[]
wenzelm@19670
    37
  done
paulson@13871
    38
wenzelm@19670
    39
lemma MultInvPair_prop1b:
wenzelm@19670
    40
  "[| zprime p; 2 < p; ~([a = 0](mod p));
wenzelm@19670
    41
      X \<in> (SetS a p); Y \<in> (SetS a p);
wenzelm@19670
    42
      X \<noteq> Y |] ==> X \<inter> Y = {}"
paulson@13871
    43
  apply (rule notnotD)
paulson@13871
    44
  apply (rule notI)
paulson@13871
    45
  apply (drule MultInvPair_prop1a, auto)
wenzelm@19670
    46
  done
paulson@13871
    47
nipkow@16663
    48
lemma MultInvPair_prop1c: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==>  
paulson@13871
    49
    \<forall>X \<in> SetS a p. \<forall>Y \<in> SetS a p. X \<noteq> Y --> X\<inter>Y = {}"
paulson@13871
    50
  by (auto simp add: MultInvPair_prop1b)
paulson@13871
    51
nipkow@16663
    52
lemma MultInvPair_prop2: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==> 
wenzelm@16974
    53
                          Union ( SetS a p) = SRStar p"
paulson@13871
    54
  apply (auto simp add: SetS_def MultInvPair_def StandardRes_SRStar_prop4 
paulson@13871
    55
    SRStar_mult_prop2)
paulson@13871
    56
  apply (frule StandardRes_SRStar_prop3)
paulson@13871
    57
  apply (rule bexI, auto)
wenzelm@19670
    58
  done
paulson@13871
    59
nipkow@16663
    60
lemma MultInvPair_distinct: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
    61
                                ~([j = 0] (mod p)); 
paulson@13871
    62
                                ~(QuadRes p a) |]  ==> 
wenzelm@16974
    63
                             ~([j = a * MultInv p j] (mod p))"
wenzelm@20369
    64
proof
nipkow@16663
    65
  assume "zprime p" and "2 < p" and "~([a = 0] (mod p))" and 
wenzelm@16974
    66
    "~([j = 0] (mod p))" and "~(QuadRes p a)"
wenzelm@16974
    67
  assume "[j = a * MultInv p j] (mod p)"
wenzelm@16974
    68
  then have "[j * j = (a * MultInv p j) * j] (mod p)"
paulson@13871
    69
    by (auto simp add: zcong_scalar)
wenzelm@16974
    70
  then have a:"[j * j = a * (MultInv p j * j)] (mod p)"
paulson@13871
    71
    by (auto simp add: zmult_ac)
wenzelm@16974
    72
  have "[j * j = a] (mod p)"
wenzelm@16974
    73
    proof -
wenzelm@16974
    74
      from prems have b: "[MultInv p j * j = 1] (mod p)"
paulson@13871
    75
        by (simp add: MultInv_prop2a)
wenzelm@16974
    76
      from b a show ?thesis
paulson@13871
    77
        by (auto simp add: zcong_zmult_prop2)
wenzelm@16974
    78
    qed
wenzelm@16974
    79
  then have "[j^2 = a] (mod p)"
wenzelm@16974
    80
    apply(subgoal_tac "2 = Suc(Suc(0))")
paulson@13871
    81
    apply (erule ssubst)
paulson@13871
    82
    apply (auto simp only: power_Suc power_0)
paulson@13871
    83
    by auto
wenzelm@16974
    84
  with prems show False
paulson@13871
    85
    by (simp add: QuadRes_def)
wenzelm@16974
    86
qed
paulson@13871
    87
nipkow@16663
    88
lemma MultInvPair_card_two: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
    89
                                ~(QuadRes p a); ~([j = 0] (mod p)) |]  ==> 
wenzelm@16974
    90
                             card (MultInvPair a p j) = 2"
paulson@13871
    91
  apply (auto simp add: MultInvPair_def)
wenzelm@16974
    92
  apply (subgoal_tac "~ (StandardRes p j = StandardRes p (a * MultInv p j))")
paulson@13871
    93
  apply auto
paulson@13871
    94
  apply (simp only: StandardRes_prop2)
paulson@13871
    95
  apply (drule MultInvPair_distinct)
wenzelm@20369
    96
  apply auto back
wenzelm@20369
    97
  done
paulson@13871
    98
wenzelm@19670
    99
wenzelm@19670
   100
subsection {* Properties of SetS *}
paulson@13871
   101
wenzelm@16974
   102
lemma SetS_finite: "2 < p ==> finite (SetS a p)"
paulson@13871
   103
  by (auto simp add: SetS_def SRStar_finite [of p] finite_imageI)
paulson@13871
   104
wenzelm@16974
   105
lemma SetS_elems_finite: "\<forall>X \<in> SetS a p. finite X"
paulson@13871
   106
  by (auto simp add: SetS_def MultInvPair_def)
paulson@13871
   107
nipkow@16663
   108
lemma SetS_elems_card: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
   109
                        ~(QuadRes p a) |]  ==>
wenzelm@16974
   110
                        \<forall>X \<in> SetS a p. card X = 2"
paulson@13871
   111
  apply (auto simp add: SetS_def)
paulson@13871
   112
  apply (frule StandardRes_SRStar_prop1a)
paulson@13871
   113
  apply (rule MultInvPair_card_two, auto)
wenzelm@19670
   114
  done
paulson@13871
   115
wenzelm@16974
   116
lemma Union_SetS_finite: "2 < p ==> finite (Union (SetS a p))"
nipkow@15402
   117
  by (auto simp add: SetS_finite SetS_elems_finite finite_Union)
paulson@13871
   118
paulson@13871
   119
lemma card_setsum_aux: "[| finite S; \<forall>X \<in> S. finite (X::int set); 
wenzelm@16974
   120
    \<forall>X \<in> S. card X = n |] ==> setsum card S = setsum (%x. n) S"
berghofe@22274
   121
  by (induct set: finite) auto
paulson@13871
   122
nipkow@16663
   123
lemma SetS_card: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==> 
wenzelm@16974
   124
                  int(card(SetS a p)) = (p - 1) div 2"
wenzelm@16974
   125
proof -
wenzelm@16974
   126
  assume "zprime p" and "2 < p" and  "~([a = 0] (mod p))" and "~(QuadRes p a)"
wenzelm@16974
   127
  then have "(p - 1) = 2 * int(card(SetS a p))"
wenzelm@16974
   128
  proof -
wenzelm@16974
   129
    have "p - 1 = int(card(Union (SetS a p)))"
paulson@13871
   130
      by (auto simp add: prems MultInvPair_prop2 SRStar_card)
wenzelm@16974
   131
    also have "... = int (setsum card (SetS a p))"
paulson@13871
   132
      by (auto simp add: prems SetS_finite SetS_elems_finite
nipkow@15402
   133
                         MultInvPair_prop1c [of p a] card_Union_disjoint)
wenzelm@16974
   134
    also have "... = int(setsum (%x.2) (SetS a p))"
wenzelm@19670
   135
      using prems
wenzelm@19670
   136
      by (auto simp add: SetS_elems_card SetS_finite SetS_elems_finite 
paulson@15047
   137
        card_setsum_aux simp del: setsum_constant)
wenzelm@16974
   138
    also have "... = 2 * int(card( SetS a p))"
paulson@13871
   139
      by (auto simp add: prems SetS_finite setsum_const2)
wenzelm@16974
   140
    finally show ?thesis .
wenzelm@16974
   141
  qed
wenzelm@16974
   142
  from this show ?thesis
paulson@13871
   143
    by auto
wenzelm@16974
   144
qed
paulson@13871
   145
nipkow@16663
   146
lemma SetS_setprod_prop: "[| zprime p; 2 < p; ~([a = 0] (mod p));
paulson@13871
   147
                              ~(QuadRes p a); x \<in> (SetS a p) |] ==> 
wenzelm@16974
   148
                          [\<Prod>x = a] (mod p)"
paulson@13871
   149
  apply (auto simp add: SetS_def MultInvPair_def)
paulson@13871
   150
  apply (frule StandardRes_SRStar_prop1a)
wenzelm@16974
   151
  apply (subgoal_tac "StandardRes p x \<noteq> StandardRes p (a * MultInv p x)")
paulson@13871
   152
  apply (auto simp add: StandardRes_prop2 MultInvPair_distinct)
paulson@13871
   153
  apply (frule_tac m = p and x = x and y = "(a * MultInv p x)" in 
wenzelm@16974
   154
    StandardRes_prop4)
wenzelm@16974
   155
  apply (subgoal_tac "[x * (a * MultInv p x) = a * (x * MultInv p x)] (mod p)")
paulson@13871
   156
  apply (drule_tac a = "StandardRes p x * StandardRes p (a * MultInv p x)" and
paulson@13871
   157
                   b = "x * (a * MultInv p x)" and
wenzelm@16974
   158
                   c = "a * (x * MultInv p x)" in  zcong_trans, force)
paulson@13871
   159
  apply (frule_tac p = p and x = x in MultInv_prop2, auto)
paulson@13871
   160
  apply (drule_tac a = "x * MultInv p x" and b = 1 in zcong_zmult_prop2)
paulson@13871
   161
  apply (auto simp add: zmult_ac)
wenzelm@19670
   162
  done
paulson@13871
   163
wenzelm@16974
   164
lemma aux1: "[| 0 < x; (x::int) < a; x \<noteq> (a - 1) |] ==> x < a - 1"
paulson@13871
   165
  by arith
paulson@13871
   166
wenzelm@16974
   167
lemma aux2: "[| (a::int) < c; b < c |] ==> (a \<le> b | b \<le> a)"
paulson@13871
   168
  by auto
paulson@13871
   169
wenzelm@18369
   170
lemma SRStar_d22set_prop: "2 < p \<Longrightarrow> (SRStar p) = {1} \<union> (d22set (p - 1))"
wenzelm@18369
   171
  apply (induct p rule: d22set.induct)
wenzelm@18369
   172
  apply auto
nipkow@16733
   173
  apply (simp add: SRStar_def d22set.simps)
paulson@13871
   174
  apply (simp add: SRStar_def d22set.simps, clarify)
paulson@13871
   175
  apply (frule aux1)
paulson@13871
   176
  apply (frule aux2, auto)
paulson@13871
   177
  apply (simp_all add: SRStar_def)
paulson@13871
   178
  apply (simp add: d22set.simps)
paulson@13871
   179
  apply (frule d22set_le)
paulson@13871
   180
  apply (frule d22set_g_1, auto)
wenzelm@18369
   181
  done
paulson@13871
   182
nipkow@16663
   183
lemma Union_SetS_setprod_prop1: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==>
nipkow@15392
   184
                                 [\<Prod>(Union (SetS a p)) = a ^ nat ((p - 1) div 2)] (mod p)"
nipkow@15392
   185
proof -
nipkow@16663
   186
  assume "zprime p" and "2 < p" and  "~([a = 0] (mod p))" and "~(QuadRes p a)"
nipkow@15392
   187
  then have "[\<Prod>(Union (SetS a p)) = 
nipkow@15392
   188
      setprod (setprod (%x. x)) (SetS a p)] (mod p)"
paulson@13871
   189
    by (auto simp add: SetS_finite SetS_elems_finite
nipkow@15392
   190
                       MultInvPair_prop1c setprod_Union_disjoint)
nipkow@15392
   191
  also have "[setprod (setprod (%x. x)) (SetS a p) = 
nipkow@15392
   192
      setprod (%x. a) (SetS a p)] (mod p)"
wenzelm@18369
   193
    by (rule setprod_same_function_zcong)
wenzelm@18369
   194
      (auto simp add: prems SetS_setprod_prop SetS_finite)
nipkow@15392
   195
  also (zcong_trans) have "[setprod (%x. a) (SetS a p) = 
nipkow@15392
   196
      a^(card (SetS a p))] (mod p)"
nipkow@15392
   197
    by (auto simp add: prems SetS_finite setprod_constant)
nipkow@15392
   198
  finally (zcong_trans) show ?thesis
paulson@13871
   199
    apply (rule zcong_trans)
nipkow@15392
   200
    apply (subgoal_tac "card(SetS a p) = nat((p - 1) div 2)", auto)
nipkow@15392
   201
    apply (subgoal_tac "nat(int(card(SetS a p))) = nat((p - 1) div 2)", force)
paulson@13871
   202
    apply (auto simp add: prems SetS_card)
wenzelm@18369
   203
    done
nipkow@15392
   204
qed
paulson@13871
   205
nipkow@16663
   206
lemma Union_SetS_setprod_prop2: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==> 
wenzelm@16974
   207
                                    \<Prod>(Union (SetS a p)) = zfact (p - 1)"
wenzelm@16974
   208
proof -
wenzelm@16974
   209
  assume "zprime p" and "2 < p" and "~([a = 0](mod p))"
nipkow@15392
   210
  then have "\<Prod>(Union (SetS a p)) = \<Prod>(SRStar p)"
paulson@13871
   211
    by (auto simp add: MultInvPair_prop2)
nipkow@15392
   212
  also have "... = \<Prod>({1} \<union> (d22set (p - 1)))"
paulson@13871
   213
    by (auto simp add: prems SRStar_d22set_prop)
nipkow@15392
   214
  also have "... = zfact(p - 1)"
nipkow@15392
   215
  proof -
wenzelm@18369
   216
    have "~(1 \<in> d22set (p - 1)) & finite( d22set (p - 1))"
paulson@13871
   217
      apply (insert prems, auto)
paulson@13871
   218
      apply (drule d22set_g_1)
paulson@13871
   219
      apply (auto simp add: d22set_fin)
wenzelm@18369
   220
      done
wenzelm@18369
   221
    then have "\<Prod>({1} \<union> (d22set (p - 1))) = \<Prod>(d22set (p - 1))"
wenzelm@18369
   222
      by auto
wenzelm@18369
   223
    then show ?thesis
wenzelm@18369
   224
      by (auto simp add: d22set_prod_zfact)
wenzelm@16974
   225
  qed
nipkow@15392
   226
  finally show ?thesis .
wenzelm@16974
   227
qed
paulson@13871
   228
nipkow@16663
   229
lemma zfact_prop: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==>
wenzelm@16974
   230
                   [zfact (p - 1) = a ^ nat ((p - 1) div 2)] (mod p)"
paulson@13871
   231
  apply (frule Union_SetS_setprod_prop1) 
paulson@13871
   232
  apply (auto simp add: Union_SetS_setprod_prop2)
wenzelm@18369
   233
  done
paulson@13871
   234
wenzelm@19670
   235
text {* \medskip Prove the first part of Euler's Criterion: *}
paulson@13871
   236
nipkow@16663
   237
lemma Euler_part1: "[| 2 < p; zprime p; ~([x = 0](mod p)); 
paulson@13871
   238
    ~(QuadRes p x) |] ==> 
wenzelm@16974
   239
      [x^(nat (((p) - 1) div 2)) = -1](mod p)"
paulson@13871
   240
  apply (frule zfact_prop, auto)
paulson@13871
   241
  apply (frule Wilson_Russ)
paulson@13871
   242
  apply (auto simp add: zcong_sym)
paulson@13871
   243
  apply (rule zcong_trans, auto)
wenzelm@18369
   244
  done
paulson@13871
   245
wenzelm@19670
   246
text {* \medskip Prove another part of Euler Criterion: *}
paulson@13871
   247
wenzelm@16974
   248
lemma aux_1: "0 < p ==> (a::int) ^ nat (p) = a * a ^ (nat (p) - 1)"
wenzelm@16974
   249
proof -
wenzelm@16974
   250
  assume "0 < p"
wenzelm@16974
   251
  then have "a ^ (nat p) =  a ^ (1 + (nat p - 1))"
paulson@13871
   252
    by (auto simp add: diff_add_assoc)
wenzelm@16974
   253
  also have "... = (a ^ 1) * a ^ (nat(p) - 1)"
paulson@13871
   254
    by (simp only: zpower_zadd_distrib)
wenzelm@16974
   255
  also have "... = a * a ^ (nat(p) - 1)"
paulson@13871
   256
    by auto
wenzelm@16974
   257
  finally show ?thesis .
wenzelm@16974
   258
qed
paulson@13871
   259
wenzelm@16974
   260
lemma aux_2: "[| (2::int) < p; p \<in> zOdd |] ==> 0 < ((p - 1) div 2)"
wenzelm@16974
   261
proof -
wenzelm@16974
   262
  assume "2 < p" and "p \<in> zOdd"
wenzelm@16974
   263
  then have "(p - 1):zEven"
paulson@13871
   264
    by (auto simp add: zEven_def zOdd_def)
wenzelm@16974
   265
  then have aux_1: "2 * ((p - 1) div 2) = (p - 1)"
paulson@13871
   266
    by (auto simp add: even_div_2_prop2)
paulson@13871
   267
  then have "1 < (p - 1)"
paulson@13871
   268
    by auto
wenzelm@16974
   269
  then have " 1 < (2 * ((p - 1) div 2))"
paulson@13871
   270
    by (auto simp add: aux_1)
wenzelm@16974
   271
  then have "0 < (2 * ((p - 1) div 2)) div 2"
paulson@13871
   272
    by auto
paulson@13871
   273
  then show ?thesis by auto
wenzelm@16974
   274
qed
paulson@13871
   275
wenzelm@19670
   276
lemma Euler_part2:
wenzelm@19670
   277
    "[| 2 < p; zprime p; [a = 0] (mod p) |] ==> [0 = a ^ nat ((p - 1) div 2)] (mod p)"
paulson@13871
   278
  apply (frule zprime_zOdd_eq_grt_2)
paulson@13871
   279
  apply (frule aux_2, auto)
paulson@13871
   280
  apply (frule_tac a = a in aux_1, auto)
paulson@13871
   281
  apply (frule zcong_zmult_prop1, auto)
wenzelm@18369
   282
  done
paulson@13871
   283
wenzelm@19670
   284
text {* \medskip Prove the final part of Euler's Criterion: *}
paulson@13871
   285
wenzelm@16974
   286
lemma aux__1: "[| ~([x = 0] (mod p)); [y ^ 2 = x] (mod p)|] ==> ~(p dvd y)"
paulson@13871
   287
  apply (subgoal_tac "[| ~([x = 0] (mod p)); [y ^ 2 = x] (mod p)|] ==> 
wenzelm@16974
   288
    ~([y ^ 2 = 0] (mod p))")
paulson@13871
   289
  apply (auto simp add: zcong_sym [of "y^2" x p] intro: zcong_trans)
paulson@13871
   290
  apply (auto simp add: zcong_eq_zdvd_prop intro: zpower_zdvd_prop1)
wenzelm@18369
   291
  done
paulson@13871
   292
wenzelm@16974
   293
lemma aux__2: "2 * nat((p - 1) div 2) =  nat (2 * ((p - 1) div 2))"
paulson@13871
   294
  by (auto simp add: nat_mult_distrib)
paulson@13871
   295
nipkow@16663
   296
lemma Euler_part3: "[| 2 < p; zprime p; ~([x = 0](mod p)); QuadRes p x |] ==> 
wenzelm@16974
   297
                      [x^(nat (((p) - 1) div 2)) = 1](mod p)"
paulson@13871
   298
  apply (subgoal_tac "p \<in> zOdd")
paulson@13871
   299
  apply (auto simp add: QuadRes_def)
paulson@13871
   300
  apply (frule aux__1, auto)
wenzelm@16974
   301
  apply (drule_tac z = "nat ((p - 1) div 2)" in zcong_zpower)
paulson@13871
   302
  apply (auto simp add: zpower_zpower)
paulson@13871
   303
  apply (rule zcong_trans)
wenzelm@16974
   304
  apply (auto simp add: zcong_sym [of "x ^ nat ((p - 1) div 2)"])
paulson@13871
   305
  apply (simp add: aux__2)
paulson@13871
   306
  apply (frule odd_minus_one_even)
paulson@13871
   307
  apply (frule even_div_2_prop2)
paulson@13871
   308
  apply (auto intro: Little_Fermat simp add: zprime_zOdd_eq_grt_2)
wenzelm@18369
   309
  done
paulson@13871
   310
wenzelm@19670
   311
wenzelm@19670
   312
text {* \medskip Finally show Euler's Criterion: *}
paulson@13871
   313
nipkow@16663
   314
theorem Euler_Criterion: "[| 2 < p; zprime p |] ==> [(Legendre a p) =
wenzelm@16974
   315
    a^(nat (((p) - 1) div 2))] (mod p)"
paulson@13871
   316
  apply (auto simp add: Legendre_def Euler_part2)
wenzelm@20369
   317
  apply (frule Euler_part3, auto simp add: zcong_sym)[]
wenzelm@20369
   318
  apply (frule Euler_part1, auto simp add: zcong_sym)[]
wenzelm@18369
   319
  done
paulson@13871
   320
wenzelm@18369
   321
end