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