src/HOL/NumberTheory/Quadratic_Reciprocity.thy
author wenzelm
Mon Oct 09 02:19:51 2006 +0200 (2006-10-09)
changeset 20898 113c9516a2d7
parent 20432 07ec57376051
child 21233 5a5c8ea5f66a
permissions -rw-r--r--
attribute symmetric: zero_var_indexes;
webertj@20346
     1
(*  Title:      HOL/NumberTheory/Quadratic_Reciprocity.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 {* The law of Quadratic reciprocity *}
paulson@13871
     7
nipkow@15392
     8
theory Quadratic_Reciprocity
nipkow@15392
     9
imports Gauss
nipkow@15392
    10
begin
paulson@13871
    11
wenzelm@19670
    12
text {*
wenzelm@19670
    13
  Lemmas leading up to the proof of theorem 3.3 in Niven and
wenzelm@19670
    14
  Zuckerman's presentation.
wenzelm@19670
    15
*}
paulson@13871
    16
wenzelm@18369
    17
lemma (in GAUSS) QRLemma1: "a * setsum id A =
nipkow@15392
    18
  p * setsum (%x. ((x * a) div p)) A + setsum id D + setsum id E"
nipkow@15392
    19
proof -
wenzelm@18369
    20
  from finite_A have "a * setsum id A = setsum (%x. a * x) A"
paulson@13871
    21
    by (auto simp add: setsum_const_mult id_def)
wenzelm@18369
    22
  also have "setsum (%x. a * x) = setsum (%x. x * a)"
paulson@13871
    23
    by (auto simp add: zmult_commute)
nipkow@15392
    24
  also have "setsum (%x. x * a) A = setsum id B"
nipkow@16733
    25
    by (simp add: B_def setsum_reindex_id[OF inj_on_xa_A])
nipkow@15392
    26
  also have "... = setsum (%x. p * (x div p) + StandardRes p x) B"
nipkow@16733
    27
    by (auto simp add: StandardRes_def zmod_zdiv_equality)
nipkow@15392
    28
  also have "... = setsum (%x. p * (x div p)) B + setsum (StandardRes p) B"
paulson@13871
    29
    by (rule setsum_addf)
nipkow@15392
    30
  also have "setsum (StandardRes p) B = setsum id C"
nipkow@16733
    31
    by (auto simp add: C_def setsum_reindex_id[OF SR_B_inj])
nipkow@15392
    32
  also from C_eq have "... = setsum id (D \<union> E)"
paulson@13871
    33
    by auto
nipkow@15392
    34
  also from finite_D finite_E have "... = setsum id D + setsum id E"
wenzelm@18369
    35
    by (rule setsum_Un_disjoint) (auto simp add: D_def E_def)
wenzelm@18369
    36
  also have "setsum (%x. p * (x div p)) B =
nipkow@15392
    37
      setsum ((%x. p * (x div p)) o (%x. (x * a))) A"
nipkow@16733
    38
    by (auto simp add: B_def setsum_reindex inj_on_xa_A)
nipkow@15392
    39
  also have "... = setsum (%x. p * ((x * a) div p)) A"
paulson@13871
    40
    by (auto simp add: o_def)
wenzelm@18369
    41
  also from finite_A have "setsum (%x. p * ((x * a) div p)) A =
nipkow@15392
    42
    p * setsum (%x. ((x * a) div p)) A"
paulson@13871
    43
    by (auto simp add: setsum_const_mult)
paulson@13871
    44
  finally show ?thesis by arith
nipkow@15392
    45
qed
paulson@13871
    46
wenzelm@18369
    47
lemma (in GAUSS) QRLemma2: "setsum id A = p * int (card E) - setsum id E +
wenzelm@18369
    48
  setsum id D"
nipkow@15392
    49
proof -
nipkow@15392
    50
  from F_Un_D_eq_A have "setsum id A = setsum id (D \<union> F)"
paulson@13871
    51
    by (simp add: Un_commute)
wenzelm@18369
    52
  also from F_D_disj finite_D finite_F
wenzelm@18369
    53
  have "... = setsum id D + setsum id F"
wenzelm@18369
    54
    by (auto simp add: Int_commute intro: setsum_Un_disjoint)
nipkow@15392
    55
  also from F_def have "F = (%x. (p - x)) ` E"
paulson@13871
    56
    by auto
paulson@13871
    57
  also from finite_E inj_on_pminusx_E have "setsum id ((%x. (p - x)) ` E) =
nipkow@15392
    58
      setsum (%x. (p - x)) E"
nipkow@15392
    59
    by (auto simp add: setsum_reindex)
nipkow@15392
    60
  also from finite_E have "setsum (op - p) E = setsum (%x. p) E - setsum id E"
nipkow@15392
    61
    by (auto simp add: setsum_subtractf id_def)
nipkow@15392
    62
  also from finite_E have "setsum (%x. p) E = p * int(card E)"
paulson@13871
    63
    by (intro setsum_const)
nipkow@15392
    64
  finally show ?thesis
paulson@13871
    65
    by arith
nipkow@15392
    66
qed
paulson@13871
    67
wenzelm@18369
    68
lemma (in GAUSS) QRLemma3: "(a - 1) * setsum id A =
nipkow@15392
    69
    p * (setsum (%x. ((x * a) div p)) A - int(card E)) + 2 * setsum id E"
nipkow@15392
    70
proof -
nipkow@15392
    71
  have "(a - 1) * setsum id A = a * setsum id A - setsum id A"
wenzelm@18369
    72
    by (auto simp add: zdiff_zmult_distrib)
nipkow@15392
    73
  also note QRLemma1
wenzelm@18369
    74
  also from QRLemma2 have "p * (\<Sum>x \<in> A. x * a div p) + setsum id D +
wenzelm@18369
    75
     setsum id E - setsum id A =
wenzelm@18369
    76
      p * (\<Sum>x \<in> A. x * a div p) + setsum id D +
nipkow@15392
    77
      setsum id E - (p * int (card E) - setsum id E + setsum id D)"
paulson@13871
    78
    by auto
wenzelm@18369
    79
  also have "... = p * (\<Sum>x \<in> A. x * a div p) -
wenzelm@18369
    80
      p * int (card E) + 2 * setsum id E"
paulson@13871
    81
    by arith
nipkow@15392
    82
  finally show ?thesis
paulson@13871
    83
    by (auto simp only: zdiff_zmult_distrib2)
nipkow@15392
    84
qed
paulson@13871
    85
wenzelm@18369
    86
lemma (in GAUSS) QRLemma4: "a \<in> zOdd ==>
nipkow@15392
    87
    (setsum (%x. ((x * a) div p)) A \<in> zEven) = (int(card E): zEven)"
nipkow@15392
    88
proof -
nipkow@15392
    89
  assume a_odd: "a \<in> zOdd"
paulson@13871
    90
  from QRLemma3 have a: "p * (setsum (%x. ((x * a) div p)) A - int(card E)) =
wenzelm@18369
    91
      (a - 1) * setsum id A - 2 * setsum id E"
paulson@13871
    92
    by arith
paulson@13871
    93
  from a_odd have "a - 1 \<in> zEven"
paulson@13871
    94
    by (rule odd_minus_one_even)
nipkow@15392
    95
  hence "(a - 1) * setsum id A \<in> zEven"
paulson@13871
    96
    by (rule even_times_either)
nipkow@15392
    97
  moreover have "2 * setsum id E \<in> zEven"
paulson@13871
    98
    by (auto simp add: zEven_def)
paulson@13871
    99
  ultimately have "(a - 1) * setsum id A - 2 * setsum id E \<in> zEven"
paulson@13871
   100
    by (rule even_minus_even)
nipkow@15392
   101
  with a have "p * (setsum (%x. ((x * a) div p)) A - int(card E)): zEven"
paulson@13871
   102
    by simp
nipkow@15392
   103
  hence "p \<in> zEven | (setsum (%x. ((x * a) div p)) A - int(card E)): zEven"
paulson@14434
   104
    by (rule EvenOdd.even_product)
nipkow@15392
   105
  with p_odd have "(setsum (%x. ((x * a) div p)) A - int(card E)): zEven"
paulson@13871
   106
    by (auto simp add: odd_iff_not_even)
nipkow@15392
   107
  thus ?thesis
wenzelm@18369
   108
    by (auto simp only: even_diff [symmetric])
nipkow@15392
   109
qed
paulson@13871
   110
wenzelm@18369
   111
lemma (in GAUSS) QRLemma5: "a \<in> zOdd ==>
nipkow@15392
   112
   (-1::int)^(card E) = (-1::int)^(nat(setsum (%x. ((x * a) div p)) A))"
nipkow@15392
   113
proof -
nipkow@15392
   114
  assume "a \<in> zOdd"
paulson@13871
   115
  from QRLemma4 have
nipkow@15392
   116
    "(int(card E): zEven) = (setsum (%x. ((x * a) div p)) A \<in> zEven)"..
nipkow@15392
   117
  moreover have "0 \<le> int(card E)"
paulson@13871
   118
    by auto
nipkow@15392
   119
  moreover have "0 \<le> setsum (%x. ((x * a) div p)) A"
nipkow@15392
   120
    proof (intro setsum_nonneg)
nipkow@15537
   121
      show "\<forall>x \<in> A. 0 \<le> x * a div p"
nipkow@15392
   122
      proof
nipkow@15392
   123
        fix x
nipkow@15392
   124
        assume "x \<in> A"
nipkow@15392
   125
        then have "0 \<le> x"
paulson@13871
   126
          by (auto simp add: A_def)
nipkow@15392
   127
        with a_nonzero have "0 \<le> x * a"
paulson@14353
   128
          by (auto simp add: zero_le_mult_iff)
wenzelm@18369
   129
        with p_g_2 show "0 \<le> x * a div p"
paulson@13871
   130
          by (auto simp add: pos_imp_zdiv_nonneg_iff)
nipkow@15392
   131
      qed
nipkow@15392
   132
    qed
paulson@13871
   133
  ultimately have "(-1::int)^nat((int (card E))) =
nipkow@15392
   134
      (-1)^nat(((\<Sum>x \<in> A. x * a div p)))"
paulson@13871
   135
    by (intro neg_one_power_parity, auto)
nipkow@15392
   136
  also have "nat (int(card E)) = card E"
paulson@13871
   137
    by auto
nipkow@15392
   138
  finally show ?thesis .
nipkow@15392
   139
qed
paulson@13871
   140
nipkow@16663
   141
lemma MainQRLemma: "[| a \<in> zOdd; 0 < a; ~([a = 0] (mod p)); zprime p; 2 < p;
wenzelm@18369
   142
  A = {x. 0 < x & x \<le> (p - 1) div 2} |] ==>
nipkow@15392
   143
  (Legendre a p) = (-1::int)^(nat(setsum (%x. ((x * a) div p)) A))"
paulson@13871
   144
  apply (subst GAUSS.gauss_lemma)
paulson@13871
   145
  apply (auto simp add: GAUSS_def)
paulson@13871
   146
  apply (subst GAUSS.QRLemma5)
wenzelm@18369
   147
  apply (auto simp add: GAUSS_def)
wenzelm@18369
   148
  done
paulson@13871
   149
wenzelm@19670
   150
wenzelm@19670
   151
subsection {* Stuff about S, S1 and S2 *}
paulson@13871
   152
paulson@13871
   153
locale QRTEMP =
paulson@13871
   154
  fixes p     :: "int"
paulson@13871
   155
  fixes q     :: "int"
paulson@13871
   156
  fixes P_set :: "int set"
paulson@13871
   157
  fixes Q_set :: "int set"
paulson@13871
   158
  fixes S     :: "(int * int) set"
paulson@13871
   159
  fixes S1    :: "(int * int) set"
paulson@13871
   160
  fixes S2    :: "(int * int) set"
paulson@13871
   161
  fixes f1    :: "int => (int * int) set"
paulson@13871
   162
  fixes f2    :: "int => (int * int) set"
paulson@13871
   163
nipkow@16663
   164
  assumes p_prime: "zprime p"
paulson@13871
   165
  assumes p_g_2: "2 < p"
nipkow@16663
   166
  assumes q_prime: "zprime q"
paulson@13871
   167
  assumes q_g_2: "2 < q"
paulson@13871
   168
  assumes p_neq_q:      "p \<noteq> q"
paulson@13871
   169
paulson@13871
   170
  defines P_set_def: "P_set == {x. 0 < x & x \<le> ((p - 1) div 2) }"
paulson@13871
   171
  defines Q_set_def: "Q_set == {x. 0 < x & x \<le> ((q - 1) div 2) }"
paulson@13871
   172
  defines S_def:     "S     == P_set <*> Q_set"
paulson@13871
   173
  defines S1_def:    "S1    == { (x, y). (x, y):S & ((p * y) < (q * x)) }"
paulson@13871
   174
  defines S2_def:    "S2    == { (x, y). (x, y):S & ((q * x) < (p * y)) }"
wenzelm@18369
   175
  defines f1_def:    "f1 j  == { (j1, y). (j1, y):S & j1 = j &
paulson@13871
   176
                                 (y \<le> (q * j) div p) }"
wenzelm@18369
   177
  defines f2_def:    "f2 j  == { (x, j1). (x, j1):S & j1 = j &
nipkow@15392
   178
                                 (x \<le> (p * j) div q) }"
paulson@13871
   179
nipkow@15392
   180
lemma (in QRTEMP) p_fact: "0 < (p - 1) div 2"
nipkow@15392
   181
proof -
paulson@13871
   182
  from prems have "2 < p" by (simp add: QRTEMP_def)
paulson@13871
   183
  then have "2 \<le> p - 1" by arith
paulson@13871
   184
  then have "2 div 2 \<le> (p - 1) div 2" by (rule zdiv_mono1, auto)
paulson@13871
   185
  then show ?thesis by auto
nipkow@15392
   186
qed
paulson@13871
   187
nipkow@15392
   188
lemma (in QRTEMP) q_fact: "0 < (q - 1) div 2"
nipkow@15392
   189
proof -
paulson@13871
   190
  from prems have "2 < q" by (simp add: QRTEMP_def)
paulson@13871
   191
  then have "2 \<le> q - 1" by arith
paulson@13871
   192
  then have "2 div 2 \<le> (q - 1) div 2" by (rule zdiv_mono1, auto)
paulson@13871
   193
  then show ?thesis by auto
nipkow@15392
   194
qed
paulson@13871
   195
wenzelm@18369
   196
lemma (in QRTEMP) pb_neq_qa: "[|1 \<le> b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   197
    (p * b \<noteq> q * a)"
nipkow@15392
   198
proof
nipkow@15392
   199
  assume "p * b = q * a" and "1 \<le> b" and "b \<le> (q - 1) div 2"
paulson@13871
   200
  then have "q dvd (p * b)" by (auto simp add: dvd_def)
nipkow@15392
   201
  with q_prime p_g_2 have "q dvd p | q dvd b"
paulson@13871
   202
    by (auto simp add: zprime_zdvd_zmult)
nipkow@15392
   203
  moreover have "~ (q dvd p)"
nipkow@15392
   204
  proof
nipkow@15392
   205
    assume "q dvd p"
paulson@13871
   206
    with p_prime have "q = 1 | q = p"
paulson@13871
   207
      apply (auto simp add: zprime_def QRTEMP_def)
paulson@13871
   208
      apply (drule_tac x = q and R = False in allE)
wenzelm@18369
   209
      apply (simp add: QRTEMP_def)
paulson@13871
   210
      apply (subgoal_tac "0 \<le> q", simp add: QRTEMP_def)
paulson@13871
   211
      apply (insert prems)
wenzelm@18369
   212
      apply (auto simp add: QRTEMP_def)
wenzelm@18369
   213
      done
paulson@13871
   214
    with q_g_2 p_neq_q show False by auto
nipkow@15392
   215
  qed
paulson@13871
   216
  ultimately have "q dvd b" by auto
nipkow@15392
   217
  then have "q \<le> b"
nipkow@15392
   218
  proof -
nipkow@15392
   219
    assume "q dvd b"
paulson@13871
   220
    moreover from prems have "0 < b" by auto
wenzelm@18369
   221
    ultimately show ?thesis using zdvd_bounds [of q b] by auto
nipkow@15392
   222
  qed
paulson@13871
   223
  with prems have "q \<le> (q - 1) div 2" by auto
paulson@13871
   224
  then have "2 * q \<le> 2 * ((q - 1) div 2)" by arith
nipkow@15392
   225
  then have "2 * q \<le> q - 1"
nipkow@15392
   226
  proof -
nipkow@15392
   227
    assume "2 * q \<le> 2 * ((q - 1) div 2)"
paulson@13871
   228
    with prems have "q \<in> zOdd" by (auto simp add: QRTEMP_def zprime_zOdd_eq_grt_2)
paulson@13871
   229
    with odd_minus_one_even have "(q - 1):zEven" by auto
paulson@13871
   230
    with even_div_2_prop2 have "(q - 1) = 2 * ((q - 1) div 2)" by auto
paulson@13871
   231
    with prems show ?thesis by auto
nipkow@15392
   232
  qed
paulson@13871
   233
  then have p1: "q \<le> -1" by arith
paulson@13871
   234
  with q_g_2 show False by auto
nipkow@15392
   235
qed
paulson@13871
   236
nipkow@15392
   237
lemma (in QRTEMP) P_set_finite: "finite (P_set)"
wenzelm@18369
   238
  using p_fact by (auto simp add: P_set_def bdd_int_set_l_le_finite)
paulson@13871
   239
nipkow@15392
   240
lemma (in QRTEMP) Q_set_finite: "finite (Q_set)"
wenzelm@18369
   241
  using q_fact by (auto simp add: Q_set_def bdd_int_set_l_le_finite)
paulson@13871
   242
nipkow@15392
   243
lemma (in QRTEMP) S_finite: "finite S"
nipkow@15402
   244
  by (auto simp add: S_def  P_set_finite Q_set_finite finite_cartesian_product)
paulson@13871
   245
nipkow@15392
   246
lemma (in QRTEMP) S1_finite: "finite S1"
nipkow@15392
   247
proof -
paulson@13871
   248
  have "finite S" by (auto simp add: S_finite)
paulson@13871
   249
  moreover have "S1 \<subseteq> S" by (auto simp add: S1_def S_def)
paulson@13871
   250
  ultimately show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   251
qed
paulson@13871
   252
nipkow@15392
   253
lemma (in QRTEMP) S2_finite: "finite S2"
nipkow@15392
   254
proof -
paulson@13871
   255
  have "finite S" by (auto simp add: S_finite)
paulson@13871
   256
  moreover have "S2 \<subseteq> S" by (auto simp add: S2_def S_def)
paulson@13871
   257
  ultimately show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   258
qed
paulson@13871
   259
nipkow@15392
   260
lemma (in QRTEMP) P_set_card: "(p - 1) div 2 = int (card (P_set))"
wenzelm@18369
   261
  using p_fact by (auto simp add: P_set_def card_bdd_int_set_l_le)
paulson@13871
   262
nipkow@15392
   263
lemma (in QRTEMP) Q_set_card: "(q - 1) div 2 = int (card (Q_set))"
wenzelm@18369
   264
  using q_fact by (auto simp add: Q_set_def card_bdd_int_set_l_le)
paulson@13871
   265
nipkow@15392
   266
lemma (in QRTEMP) S_card: "((p - 1) div 2) * ((q - 1) div 2) = int (card(S))"
wenzelm@18369
   267
  using P_set_card Q_set_card P_set_finite Q_set_finite
wenzelm@18369
   268
  by (auto simp add: S_def zmult_int setsum_constant)
paulson@13871
   269
nipkow@15392
   270
lemma (in QRTEMP) S1_Int_S2_prop: "S1 \<inter> S2 = {}"
paulson@13871
   271
  by (auto simp add: S1_def S2_def)
paulson@13871
   272
nipkow@15392
   273
lemma (in QRTEMP) S1_Union_S2_prop: "S = S1 \<union> S2"
paulson@13871
   274
  apply (auto simp add: S_def P_set_def Q_set_def S1_def S2_def)
wenzelm@18369
   275
proof -
wenzelm@18369
   276
  fix a and b
wenzelm@18369
   277
  assume "~ q * a < p * b" and b1: "0 < b" and b2: "b \<le> (q - 1) div 2"
wenzelm@18369
   278
  with zless_linear have "(p * b < q * a) | (p * b = q * a)" by auto
wenzelm@18369
   279
  moreover from pb_neq_qa b1 b2 have "(p * b \<noteq> q * a)" by auto
wenzelm@18369
   280
  ultimately show "p * b < q * a" by auto
wenzelm@18369
   281
qed
paulson@13871
   282
wenzelm@18369
   283
lemma (in QRTEMP) card_sum_S1_S2: "((p - 1) div 2) * ((q - 1) div 2) =
nipkow@15392
   284
    int(card(S1)) + int(card(S2))"
wenzelm@18369
   285
proof -
nipkow@15392
   286
  have "((p - 1) div 2) * ((q - 1) div 2) = int (card(S))"
paulson@13871
   287
    by (auto simp add: S_card)
nipkow@15392
   288
  also have "... = int( card(S1) + card(S2))"
paulson@13871
   289
    apply (insert S1_finite S2_finite S1_Int_S2_prop S1_Union_S2_prop)
paulson@13871
   290
    apply (drule card_Un_disjoint, auto)
wenzelm@18369
   291
    done
paulson@13871
   292
  also have "... = int(card(S1)) + int(card(S2))" by auto
nipkow@15392
   293
  finally show ?thesis .
nipkow@15392
   294
qed
paulson@13871
   295
wenzelm@18369
   296
lemma (in QRTEMP) aux1a: "[| 0 < a; a \<le> (p - 1) div 2;
paulson@13871
   297
                             0 < b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   298
                          (p * b < q * a) = (b \<le> q * a div p)"
nipkow@15392
   299
proof -
nipkow@15392
   300
  assume "0 < a" and "a \<le> (p - 1) div 2" and "0 < b" and "b \<le> (q - 1) div 2"
nipkow@15392
   301
  have "p * b < q * a ==> b \<le> q * a div p"
nipkow@15392
   302
  proof -
nipkow@15392
   303
    assume "p * b < q * a"
paulson@13871
   304
    then have "p * b \<le> q * a" by auto
nipkow@15392
   305
    then have "(p * b) div p \<le> (q * a) div p"
wenzelm@18369
   306
      by (rule zdiv_mono1) (insert p_g_2, auto)
nipkow@15392
   307
    then show "b \<le> (q * a) div p"
paulson@13871
   308
      apply (subgoal_tac "p \<noteq> 0")
paulson@13871
   309
      apply (frule zdiv_zmult_self2, force)
wenzelm@18369
   310
      apply (insert p_g_2, auto)
wenzelm@18369
   311
      done
nipkow@15392
   312
  qed
nipkow@15392
   313
  moreover have "b \<le> q * a div p ==> p * b < q * a"
nipkow@15392
   314
  proof -
nipkow@15392
   315
    assume "b \<le> q * a div p"
nipkow@15392
   316
    then have "p * b \<le> p * ((q * a) div p)"
wenzelm@18369
   317
      using p_g_2 by (auto simp add: mult_le_cancel_left)
nipkow@15392
   318
    also have "... \<le> q * a"
wenzelm@18369
   319
      by (rule zdiv_leq_prop) (insert p_g_2, auto)
nipkow@15392
   320
    finally have "p * b \<le> q * a" .
nipkow@15392
   321
    then have "p * b < q * a | p * b = q * a"
paulson@13871
   322
      by (simp only: order_le_imp_less_or_eq)
nipkow@15392
   323
    moreover have "p * b \<noteq> q * a"
wenzelm@18369
   324
      by (rule  pb_neq_qa) (insert prems, auto)
paulson@13871
   325
    ultimately show ?thesis by auto
nipkow@15392
   326
  qed
nipkow@15392
   327
  ultimately show ?thesis ..
nipkow@15392
   328
qed
paulson@13871
   329
wenzelm@18369
   330
lemma (in QRTEMP) aux1b: "[| 0 < a; a \<le> (p - 1) div 2;
paulson@13871
   331
                             0 < b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   332
                          (q * a < p * b) = (a \<le> p * b div q)"
nipkow@15392
   333
proof -
nipkow@15392
   334
  assume "0 < a" and "a \<le> (p - 1) div 2" and "0 < b" and "b \<le> (q - 1) div 2"
nipkow@15392
   335
  have "q * a < p * b ==> a \<le> p * b div q"
nipkow@15392
   336
  proof -
nipkow@15392
   337
    assume "q * a < p * b"
paulson@13871
   338
    then have "q * a \<le> p * b" by auto
nipkow@15392
   339
    then have "(q * a) div q \<le> (p * b) div q"
wenzelm@18369
   340
      by (rule zdiv_mono1) (insert q_g_2, auto)
nipkow@15392
   341
    then show "a \<le> (p * b) div q"
paulson@13871
   342
      apply (subgoal_tac "q \<noteq> 0")
paulson@13871
   343
      apply (frule zdiv_zmult_self2, force)
wenzelm@18369
   344
      apply (insert q_g_2, auto)
wenzelm@18369
   345
      done
nipkow@15392
   346
  qed
nipkow@15392
   347
  moreover have "a \<le> p * b div q ==> q * a < p * b"
nipkow@15392
   348
  proof -
nipkow@15392
   349
    assume "a \<le> p * b div q"
nipkow@15392
   350
    then have "q * a \<le> q * ((p * b) div q)"
wenzelm@18369
   351
      using q_g_2 by (auto simp add: mult_le_cancel_left)
nipkow@15392
   352
    also have "... \<le> p * b"
wenzelm@18369
   353
      by (rule zdiv_leq_prop) (insert q_g_2, auto)
nipkow@15392
   354
    finally have "q * a \<le> p * b" .
nipkow@15392
   355
    then have "q * a < p * b | q * a = p * b"
paulson@13871
   356
      by (simp only: order_le_imp_less_or_eq)
nipkow@15392
   357
    moreover have "p * b \<noteq> q * a"
wenzelm@18369
   358
      by (rule  pb_neq_qa) (insert prems, auto)
paulson@13871
   359
    ultimately show ?thesis by auto
nipkow@15392
   360
  qed
nipkow@15392
   361
  ultimately show ?thesis ..
nipkow@15392
   362
qed
paulson@13871
   363
wenzelm@18369
   364
lemma aux2: "[| zprime p; zprime q; 2 < p; 2 < q |] ==>
nipkow@15392
   365
             (q * ((p - 1) div 2)) div p \<le> (q - 1) div 2"
nipkow@15392
   366
proof-
nipkow@16663
   367
  assume "zprime p" and "zprime q" and "2 < p" and "2 < q"
paulson@13871
   368
  (* Set up what's even and odd *)
nipkow@15392
   369
  then have "p \<in> zOdd & q \<in> zOdd"
paulson@13871
   370
    by (auto simp add:  zprime_zOdd_eq_grt_2)
nipkow@15392
   371
  then have even1: "(p - 1):zEven & (q - 1):zEven"
paulson@13871
   372
    by (auto simp add: odd_minus_one_even)
nipkow@15392
   373
  then have even2: "(2 * p):zEven & ((q - 1) * p):zEven"
paulson@13871
   374
    by (auto simp add: zEven_def)
nipkow@15392
   375
  then have even3: "(((q - 1) * p) + (2 * p)):zEven"
paulson@14434
   376
    by (auto simp: EvenOdd.even_plus_even)
paulson@13871
   377
  (* using these prove it *)
nipkow@15392
   378
  from prems have "q * (p - 1) < ((q - 1) * p) + (2 * p)"
paulson@13871
   379
    by (auto simp add: int_distrib)
nipkow@15392
   380
  then have "((p - 1) * q) div 2 < (((q - 1) * p) + (2 * p)) div 2"
nipkow@15392
   381
    apply (rule_tac x = "((p - 1) * q)" in even_div_2_l)
paulson@13871
   382
    by (auto simp add: even3, auto simp add: zmult_ac)
nipkow@15392
   383
  also have "((p - 1) * q) div 2 = q * ((p - 1) div 2)"
paulson@13871
   384
    by (auto simp add: even1 even_prod_div_2)
nipkow@15392
   385
  also have "(((q - 1) * p) + (2 * p)) div 2 = (((q - 1) div 2) * p) + p"
paulson@13871
   386
    by (auto simp add: even1 even2 even_prod_div_2 even_sum_div_2)
wenzelm@18369
   387
  finally show ?thesis
wenzelm@18369
   388
    apply (rule_tac x = " q * ((p - 1) div 2)" and
nipkow@15392
   389
                    y = "(q - 1) div 2" in div_prop2)
wenzelm@18369
   390
    using prems by auto
nipkow@15392
   391
qed
paulson@13871
   392
nipkow@15392
   393
lemma (in QRTEMP) aux3a: "\<forall>j \<in> P_set. int (card (f1 j)) = (q * j) div p"
nipkow@15392
   394
proof
nipkow@15392
   395
  fix j
nipkow@15392
   396
  assume j_fact: "j \<in> P_set"
nipkow@15392
   397
  have "int (card (f1 j)) = int (card {y. y \<in> Q_set & y \<le> (q * j) div p})"
nipkow@15392
   398
  proof -
nipkow@15392
   399
    have "finite (f1 j)"
nipkow@15392
   400
    proof -
paulson@13871
   401
      have "(f1 j) \<subseteq> S" by (auto simp add: f1_def)
paulson@13871
   402
      with S_finite show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   403
    qed
nipkow@15392
   404
    moreover have "inj_on (%(x,y). y) (f1 j)"
paulson@13871
   405
      by (auto simp add: f1_def inj_on_def)
nipkow@15392
   406
    ultimately have "card ((%(x,y). y) ` (f1 j)) = card  (f1 j)"
paulson@13871
   407
      by (auto simp add: f1_def card_image)
nipkow@15392
   408
    moreover have "((%(x,y). y) ` (f1 j)) = {y. y \<in> Q_set & y \<le> (q * j) div p}"
wenzelm@18369
   409
      using prems by (auto simp add: f1_def S_def Q_set_def P_set_def image_def)
paulson@13871
   410
    ultimately show ?thesis by (auto simp add: f1_def)
nipkow@15392
   411
  qed
nipkow@15392
   412
  also have "... = int (card {y. 0 < y & y \<le> (q * j) div p})"
nipkow@15392
   413
  proof -
wenzelm@18369
   414
    have "{y. y \<in> Q_set & y \<le> (q * j) div p} =
nipkow@15392
   415
        {y. 0 < y & y \<le> (q * j) div p}"
paulson@13871
   416
      apply (auto simp add: Q_set_def)
wenzelm@18369
   417
    proof -
wenzelm@18369
   418
      fix x
wenzelm@18369
   419
      assume "0 < x" and "x \<le> q * j div p"
wenzelm@18369
   420
      with j_fact P_set_def  have "j \<le> (p - 1) div 2" by auto
wenzelm@18369
   421
      with q_g_2 have "q * j \<le> q * ((p - 1) div 2)"
wenzelm@18369
   422
        by (auto simp add: mult_le_cancel_left)
wenzelm@18369
   423
      with p_g_2 have "q * j div p \<le> q * ((p - 1) div 2) div p"
wenzelm@18369
   424
        by (auto simp add: zdiv_mono1)
wenzelm@18369
   425
      also from prems have "... \<le> (q - 1) div 2"
wenzelm@18369
   426
        apply simp
wenzelm@18369
   427
        apply (insert aux2)
wenzelm@18369
   428
        apply (simp add: QRTEMP_def)
wenzelm@18369
   429
        done
wenzelm@18369
   430
      finally show "x \<le> (q - 1) div 2" using prems by auto
wenzelm@18369
   431
    qed
paulson@13871
   432
    then show ?thesis by auto
nipkow@15392
   433
  qed
nipkow@15392
   434
  also have "... = (q * j) div p"
nipkow@15392
   435
  proof -
paulson@13871
   436
    from j_fact P_set_def have "0 \<le> j" by auto
paulson@14387
   437
    with q_g_2 have "q * 0 \<le> q * j" by (auto simp only: mult_left_mono)
paulson@13871
   438
    then have "0 \<le> q * j" by auto
nipkow@15392
   439
    then have "0 div p \<le> (q * j) div p"
paulson@13871
   440
      apply (rule_tac a = 0 in zdiv_mono1)
wenzelm@18369
   441
      apply (insert p_g_2, auto)
wenzelm@18369
   442
      done
paulson@13871
   443
    also have "0 div p = 0" by auto
paulson@13871
   444
    finally show ?thesis by (auto simp add: card_bdd_int_set_l_le)
nipkow@15392
   445
  qed
nipkow@15392
   446
  finally show "int (card (f1 j)) = q * j div p" .
nipkow@15392
   447
qed
paulson@13871
   448
nipkow@15392
   449
lemma (in QRTEMP) aux3b: "\<forall>j \<in> Q_set. int (card (f2 j)) = (p * j) div q"
nipkow@15392
   450
proof
nipkow@15392
   451
  fix j
nipkow@15392
   452
  assume j_fact: "j \<in> Q_set"
nipkow@15392
   453
  have "int (card (f2 j)) = int (card {y. y \<in> P_set & y \<le> (p * j) div q})"
nipkow@15392
   454
  proof -
nipkow@15392
   455
    have "finite (f2 j)"
nipkow@15392
   456
    proof -
paulson@13871
   457
      have "(f2 j) \<subseteq> S" by (auto simp add: f2_def)
paulson@13871
   458
      with S_finite show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   459
    qed
nipkow@15392
   460
    moreover have "inj_on (%(x,y). x) (f2 j)"
paulson@13871
   461
      by (auto simp add: f2_def inj_on_def)
nipkow@15392
   462
    ultimately have "card ((%(x,y). x) ` (f2 j)) = card  (f2 j)"
paulson@13871
   463
      by (auto simp add: f2_def card_image)
nipkow@15392
   464
    moreover have "((%(x,y). x) ` (f2 j)) = {y. y \<in> P_set & y \<le> (p * j) div q}"
wenzelm@18369
   465
      using prems by (auto simp add: f2_def S_def Q_set_def P_set_def image_def)
paulson@13871
   466
    ultimately show ?thesis by (auto simp add: f2_def)
nipkow@15392
   467
  qed
nipkow@15392
   468
  also have "... = int (card {y. 0 < y & y \<le> (p * j) div q})"
nipkow@15392
   469
  proof -
wenzelm@18369
   470
    have "{y. y \<in> P_set & y \<le> (p * j) div q} =
nipkow@15392
   471
        {y. 0 < y & y \<le> (p * j) div q}"
paulson@13871
   472
      apply (auto simp add: P_set_def)
wenzelm@18369
   473
    proof -
wenzelm@18369
   474
      fix x
wenzelm@18369
   475
      assume "0 < x" and "x \<le> p * j div q"
wenzelm@18369
   476
      with j_fact Q_set_def  have "j \<le> (q - 1) div 2" by auto
wenzelm@18369
   477
      with p_g_2 have "p * j \<le> p * ((q - 1) div 2)"
wenzelm@18369
   478
        by (auto simp add: mult_le_cancel_left)
wenzelm@18369
   479
      with q_g_2 have "p * j div q \<le> p * ((q - 1) div 2) div q"
wenzelm@18369
   480
        by (auto simp add: zdiv_mono1)
wenzelm@18369
   481
      also from prems have "... \<le> (p - 1) div 2"
wenzelm@18369
   482
        by (auto simp add: aux2 QRTEMP_def)
wenzelm@18369
   483
      finally show "x \<le> (p - 1) div 2" using prems by auto
nipkow@15392
   484
      qed
paulson@13871
   485
    then show ?thesis by auto
nipkow@15392
   486
  qed
nipkow@15392
   487
  also have "... = (p * j) div q"
nipkow@15392
   488
  proof -
paulson@13871
   489
    from j_fact Q_set_def have "0 \<le> j" by auto
paulson@14387
   490
    with p_g_2 have "p * 0 \<le> p * j" by (auto simp only: mult_left_mono)
paulson@13871
   491
    then have "0 \<le> p * j" by auto
nipkow@15392
   492
    then have "0 div q \<le> (p * j) div q"
paulson@13871
   493
      apply (rule_tac a = 0 in zdiv_mono1)
wenzelm@18369
   494
      apply (insert q_g_2, auto)
wenzelm@18369
   495
      done
paulson@13871
   496
    also have "0 div q = 0" by auto
paulson@13871
   497
    finally show ?thesis by (auto simp add: card_bdd_int_set_l_le)
nipkow@15392
   498
  qed
nipkow@15392
   499
  finally show "int (card (f2 j)) = p * j div q" .
nipkow@15392
   500
qed
paulson@13871
   501
nipkow@15392
   502
lemma (in QRTEMP) S1_card: "int (card(S1)) = setsum (%j. (q * j) div p) P_set"
nipkow@15392
   503
proof -
nipkow@15392
   504
  have "\<forall>x \<in> P_set. finite (f1 x)"
nipkow@15392
   505
  proof
nipkow@15392
   506
    fix x
paulson@13871
   507
    have "f1 x \<subseteq> S" by (auto simp add: f1_def)
paulson@13871
   508
    with S_finite show "finite (f1 x)" by (auto simp add: finite_subset)
nipkow@15392
   509
  qed
nipkow@15392
   510
  moreover have "(\<forall>x \<in> P_set. \<forall>y \<in> P_set. x \<noteq> y --> (f1 x) \<inter> (f1 y) = {})"
paulson@13871
   511
    by (auto simp add: f1_def)
nipkow@15392
   512
  moreover note P_set_finite
wenzelm@18369
   513
  ultimately have "int(card (UNION P_set f1)) =
nipkow@15392
   514
      setsum (%x. int(card (f1 x))) P_set"
nipkow@15402
   515
    by(simp add:card_UN_disjoint int_setsum o_def)
nipkow@15392
   516
  moreover have "S1 = UNION P_set f1"
paulson@13871
   517
    by (auto simp add: f1_def S_def S1_def S2_def P_set_def Q_set_def aux1a)
wenzelm@18369
   518
  ultimately have "int(card (S1)) = setsum (%j. int(card (f1 j))) P_set"
paulson@13871
   519
    by auto
nipkow@15392
   520
  also have "... = setsum (%j. q * j div p) P_set"
nipkow@15392
   521
    using aux3a by(fastsimp intro: setsum_cong)
nipkow@15392
   522
  finally show ?thesis .
nipkow@15392
   523
qed
paulson@13871
   524
nipkow@15392
   525
lemma (in QRTEMP) S2_card: "int (card(S2)) = setsum (%j. (p * j) div q) Q_set"
nipkow@15392
   526
proof -
nipkow@15392
   527
  have "\<forall>x \<in> Q_set. finite (f2 x)"
nipkow@15392
   528
  proof
nipkow@15392
   529
    fix x
paulson@13871
   530
    have "f2 x \<subseteq> S" by (auto simp add: f2_def)
paulson@13871
   531
    with S_finite show "finite (f2 x)" by (auto simp add: finite_subset)
nipkow@15392
   532
  qed
wenzelm@18369
   533
  moreover have "(\<forall>x \<in> Q_set. \<forall>y \<in> Q_set. x \<noteq> y -->
nipkow@15392
   534
      (f2 x) \<inter> (f2 y) = {})"
paulson@13871
   535
    by (auto simp add: f2_def)
nipkow@15392
   536
  moreover note Q_set_finite
wenzelm@18369
   537
  ultimately have "int(card (UNION Q_set f2)) =
nipkow@15392
   538
      setsum (%x. int(card (f2 x))) Q_set"
nipkow@15402
   539
    by(simp add:card_UN_disjoint int_setsum o_def)
nipkow@15392
   540
  moreover have "S2 = UNION Q_set f2"
paulson@13871
   541
    by (auto simp add: f2_def S_def S1_def S2_def P_set_def Q_set_def aux1b)
wenzelm@18369
   542
  ultimately have "int(card (S2)) = setsum (%j. int(card (f2 j))) Q_set"
paulson@13871
   543
    by auto
nipkow@15392
   544
  also have "... = setsum (%j. p * j div q) Q_set"
nipkow@15392
   545
    using aux3b by(fastsimp intro: setsum_cong)
nipkow@15392
   546
  finally show ?thesis .
nipkow@15392
   547
qed
paulson@13871
   548
wenzelm@18369
   549
lemma (in QRTEMP) S1_carda: "int (card(S1)) =
nipkow@15392
   550
    setsum (%j. (j * q) div p) P_set"
paulson@13871
   551
  by (auto simp add: S1_card zmult_ac)
paulson@13871
   552
wenzelm@18369
   553
lemma (in QRTEMP) S2_carda: "int (card(S2)) =
nipkow@15392
   554
    setsum (%j. (j * p) div q) Q_set"
paulson@13871
   555
  by (auto simp add: S2_card zmult_ac)
paulson@13871
   556
wenzelm@18369
   557
lemma (in QRTEMP) pq_sum_prop: "(setsum (%j. (j * p) div q) Q_set) +
nipkow@15392
   558
    (setsum (%j. (j * q) div p) P_set) = ((p - 1) div 2) * ((q - 1) div 2)"
nipkow@15392
   559
proof -
wenzelm@18369
   560
  have "(setsum (%j. (j * p) div q) Q_set) +
nipkow@15392
   561
      (setsum (%j. (j * q) div p) P_set) = int (card S2) + int (card S1)"
paulson@13871
   562
    by (auto simp add: S1_carda S2_carda)
nipkow@15392
   563
  also have "... = int (card S1) + int (card S2)"
paulson@13871
   564
    by auto
nipkow@15392
   565
  also have "... = ((p - 1) div 2) * ((q - 1) div 2)"
paulson@13871
   566
    by (auto simp add: card_sum_S1_S2)
nipkow@15392
   567
  finally show ?thesis .
nipkow@15392
   568
qed
paulson@13871
   569
nipkow@16663
   570
lemma pq_prime_neq: "[| zprime p; zprime q; p \<noteq> q |] ==> (~[p = 0] (mod q))"
paulson@13871
   571
  apply (auto simp add: zcong_eq_zdvd_prop zprime_def)
paulson@13871
   572
  apply (drule_tac x = q in allE)
paulson@13871
   573
  apply (drule_tac x = p in allE)
wenzelm@18369
   574
  apply auto
wenzelm@18369
   575
  done
paulson@13871
   576
wenzelm@18369
   577
lemma (in QRTEMP) QR_short: "(Legendre p q) * (Legendre q p) =
nipkow@15392
   578
    (-1::int)^nat(((p - 1) div 2)*((q - 1) div 2))"
nipkow@15392
   579
proof -
nipkow@15392
   580
  from prems have "~([p = 0] (mod q))"
paulson@13871
   581
    by (auto simp add: pq_prime_neq QRTEMP_def)
wenzelm@18369
   582
  with prems have a1: "(Legendre p q) = (-1::int) ^
nipkow@15392
   583
      nat(setsum (%x. ((x * p) div q)) Q_set)"
paulson@13871
   584
    apply (rule_tac p = q in  MainQRLemma)
wenzelm@18369
   585
    apply (auto simp add: zprime_zOdd_eq_grt_2 QRTEMP_def)
wenzelm@18369
   586
    done
nipkow@15392
   587
  from prems have "~([q = 0] (mod p))"
paulson@13871
   588
    apply (rule_tac p = q and q = p in pq_prime_neq)
nipkow@15392
   589
    apply (simp add: QRTEMP_def)+
nipkow@16733
   590
    done
wenzelm@18369
   591
  with prems have a2: "(Legendre q p) =
nipkow@15392
   592
      (-1::int) ^ nat(setsum (%x. ((x * q) div p)) P_set)"
paulson@13871
   593
    apply (rule_tac p = p in  MainQRLemma)
wenzelm@18369
   594
    apply (auto simp add: zprime_zOdd_eq_grt_2 QRTEMP_def)
wenzelm@18369
   595
    done
wenzelm@18369
   596
  from a1 a2 have "(Legendre p q) * (Legendre q p) =
paulson@13871
   597
      (-1::int) ^ nat(setsum (%x. ((x * p) div q)) Q_set) *
nipkow@15392
   598
        (-1::int) ^ nat(setsum (%x. ((x * q) div p)) P_set)"
paulson@13871
   599
    by auto
wenzelm@18369
   600
  also have "... = (-1::int) ^ (nat(setsum (%x. ((x * p) div q)) Q_set) +
nipkow@15392
   601
                   nat(setsum (%x. ((x * q) div p)) P_set))"
paulson@13871
   602
    by (auto simp add: zpower_zadd_distrib)
wenzelm@18369
   603
  also have "nat(setsum (%x. ((x * p) div q)) Q_set) +
paulson@13871
   604
      nat(setsum (%x. ((x * q) div p)) P_set) =
wenzelm@18369
   605
        nat((setsum (%x. ((x * p) div q)) Q_set) +
nipkow@15392
   606
          (setsum (%x. ((x * q) div p)) P_set))"
wenzelm@20898
   607
    apply (rule_tac z = "setsum (%x. ((x * p) div q)) Q_set" in
wenzelm@18369
   608
      nat_add_distrib [symmetric])
wenzelm@18369
   609
    apply (auto simp add: S1_carda [symmetric] S2_carda [symmetric])
wenzelm@18369
   610
    done
nipkow@15392
   611
  also have "... = nat(((p - 1) div 2) * ((q - 1) div 2))"
paulson@13871
   612
    by (auto simp add: pq_sum_prop)
nipkow@15392
   613
  finally show ?thesis .
nipkow@15392
   614
qed
paulson@13871
   615
paulson@13871
   616
theorem Quadratic_Reciprocity:
wenzelm@18369
   617
     "[| p \<in> zOdd; zprime p; q \<in> zOdd; zprime q;
wenzelm@18369
   618
         p \<noteq> q |]
wenzelm@18369
   619
      ==> (Legendre p q) * (Legendre q p) =
nipkow@15392
   620
          (-1::int)^nat(((p - 1) div 2)*((q - 1) div 2))"
wenzelm@18369
   621
  by (auto simp add: QRTEMP.QR_short zprime_zOdd_eq_grt_2 [symmetric]
paulson@13871
   622
                     QRTEMP_def)
paulson@13871
   623
paulson@13871
   624
end