src/HOL/NumberTheory/Quadratic_Reciprocity.thy
author nipkow
Sat Feb 21 09:58:26 2009 +0100 (2009-02-21)
changeset 30034 60f64f112174
parent 23431 25ca91279a9b
permissions -rw-r--r--
removed redundant thms
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"
wenzelm@23373
   118
  from QRLemma4 [OF this] have
wenzelm@23373
   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@21404
   171
  P_set :: "int set" where
wenzelm@21233
   172
  "P_set = {x. 0 < x & x \<le> ((p - 1) div 2) }"
wenzelm@21233
   173
wenzelm@21404
   174
definition
wenzelm@21404
   175
  Q_set :: "int set" where
wenzelm@21233
   176
  "Q_set = {x. 0 < x & x \<le> ((q - 1) div 2) }"
wenzelm@21233
   177
  
wenzelm@21404
   178
definition
wenzelm@21404
   179
  S :: "(int * int) set" where
wenzelm@21233
   180
  "S = P_set <*> Q_set"
wenzelm@21233
   181
wenzelm@21404
   182
definition
wenzelm@21404
   183
  S1 :: "(int * int) set" where
wenzelm@21233
   184
  "S1 = { (x, y). (x, y):S & ((p * y) < (q * x)) }"
paulson@13871
   185
wenzelm@21404
   186
definition
wenzelm@21404
   187
  S2 :: "(int * int) set" where
wenzelm@21233
   188
  "S2 = { (x, y). (x, y):S & ((q * x) < (p * y)) }"
wenzelm@21233
   189
wenzelm@21404
   190
definition
wenzelm@21404
   191
  f1 :: "int => (int * int) set" where
wenzelm@21233
   192
  "f1 j = { (j1, y). (j1, y):S & j1 = j & (y \<le> (q * j) div p) }"
wenzelm@21233
   193
wenzelm@21404
   194
definition
wenzelm@21404
   195
  f2 :: "int => (int * int) set" where
wenzelm@21233
   196
  "f2 j = { (x, j1). (x, j1):S & j1 = j & (x \<le> (p * j) div q) }"
wenzelm@21233
   197
wenzelm@21233
   198
lemma p_fact: "0 < (p - 1) div 2"
nipkow@15392
   199
proof -
wenzelm@21233
   200
  from p_g_2 have "2 \<le> p - 1" by arith
paulson@13871
   201
  then have "2 div 2 \<le> (p - 1) div 2" by (rule zdiv_mono1, auto)
paulson@13871
   202
  then show ?thesis by auto
nipkow@15392
   203
qed
paulson@13871
   204
wenzelm@21233
   205
lemma q_fact: "0 < (q - 1) div 2"
nipkow@15392
   206
proof -
wenzelm@21233
   207
  from q_g_2 have "2 \<le> q - 1" by arith
paulson@13871
   208
  then have "2 div 2 \<le> (q - 1) div 2" by (rule zdiv_mono1, auto)
paulson@13871
   209
  then show ?thesis by auto
nipkow@15392
   210
qed
paulson@13871
   211
wenzelm@21233
   212
lemma pb_neq_qa: "[|1 \<le> b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   213
    (p * b \<noteq> q * a)"
nipkow@15392
   214
proof
nipkow@15392
   215
  assume "p * b = q * a" and "1 \<le> b" and "b \<le> (q - 1) div 2"
paulson@13871
   216
  then have "q dvd (p * b)" by (auto simp add: dvd_def)
nipkow@15392
   217
  with q_prime p_g_2 have "q dvd p | q dvd b"
paulson@13871
   218
    by (auto simp add: zprime_zdvd_zmult)
nipkow@15392
   219
  moreover have "~ (q dvd p)"
nipkow@15392
   220
  proof
nipkow@15392
   221
    assume "q dvd p"
paulson@13871
   222
    with p_prime have "q = 1 | q = p"
paulson@13871
   223
      apply (auto simp add: zprime_def QRTEMP_def)
paulson@13871
   224
      apply (drule_tac x = q and R = False in allE)
wenzelm@18369
   225
      apply (simp add: QRTEMP_def)
paulson@13871
   226
      apply (subgoal_tac "0 \<le> q", simp add: QRTEMP_def)
paulson@13871
   227
      apply (insert prems)
wenzelm@18369
   228
      apply (auto simp add: QRTEMP_def)
wenzelm@18369
   229
      done
paulson@13871
   230
    with q_g_2 p_neq_q show False by auto
nipkow@15392
   231
  qed
paulson@13871
   232
  ultimately have "q dvd b" by auto
nipkow@15392
   233
  then have "q \<le> b"
nipkow@15392
   234
  proof -
nipkow@15392
   235
    assume "q dvd b"
paulson@13871
   236
    moreover from prems have "0 < b" by auto
wenzelm@18369
   237
    ultimately show ?thesis using zdvd_bounds [of q b] by auto
nipkow@15392
   238
  qed
paulson@13871
   239
  with prems have "q \<le> (q - 1) div 2" by auto
paulson@13871
   240
  then have "2 * q \<le> 2 * ((q - 1) div 2)" by arith
nipkow@15392
   241
  then have "2 * q \<le> q - 1"
nipkow@15392
   242
  proof -
nipkow@15392
   243
    assume "2 * q \<le> 2 * ((q - 1) div 2)"
paulson@13871
   244
    with prems have "q \<in> zOdd" by (auto simp add: QRTEMP_def zprime_zOdd_eq_grt_2)
paulson@13871
   245
    with odd_minus_one_even have "(q - 1):zEven" by auto
paulson@13871
   246
    with even_div_2_prop2 have "(q - 1) = 2 * ((q - 1) div 2)" by auto
paulson@13871
   247
    with prems show ?thesis by auto
nipkow@15392
   248
  qed
paulson@13871
   249
  then have p1: "q \<le> -1" by arith
paulson@13871
   250
  with q_g_2 show False by auto
nipkow@15392
   251
qed
paulson@13871
   252
wenzelm@21233
   253
lemma P_set_finite: "finite (P_set)"
wenzelm@18369
   254
  using p_fact by (auto simp add: P_set_def bdd_int_set_l_le_finite)
paulson@13871
   255
wenzelm@21233
   256
lemma Q_set_finite: "finite (Q_set)"
wenzelm@18369
   257
  using q_fact by (auto simp add: Q_set_def bdd_int_set_l_le_finite)
paulson@13871
   258
wenzelm@21233
   259
lemma S_finite: "finite S"
nipkow@15402
   260
  by (auto simp add: S_def  P_set_finite Q_set_finite finite_cartesian_product)
paulson@13871
   261
wenzelm@21233
   262
lemma S1_finite: "finite S1"
nipkow@15392
   263
proof -
paulson@13871
   264
  have "finite S" by (auto simp add: S_finite)
paulson@13871
   265
  moreover have "S1 \<subseteq> S" by (auto simp add: S1_def S_def)
paulson@13871
   266
  ultimately show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   267
qed
paulson@13871
   268
wenzelm@21233
   269
lemma S2_finite: "finite S2"
nipkow@15392
   270
proof -
paulson@13871
   271
  have "finite S" by (auto simp add: S_finite)
paulson@13871
   272
  moreover have "S2 \<subseteq> S" by (auto simp add: S2_def S_def)
paulson@13871
   273
  ultimately show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   274
qed
paulson@13871
   275
wenzelm@21233
   276
lemma P_set_card: "(p - 1) div 2 = int (card (P_set))"
wenzelm@18369
   277
  using p_fact by (auto simp add: P_set_def card_bdd_int_set_l_le)
paulson@13871
   278
wenzelm@21233
   279
lemma Q_set_card: "(q - 1) div 2 = int (card (Q_set))"
wenzelm@18369
   280
  using q_fact by (auto simp add: Q_set_def card_bdd_int_set_l_le)
paulson@13871
   281
wenzelm@21233
   282
lemma S_card: "((p - 1) div 2) * ((q - 1) div 2) = int (card(S))"
wenzelm@18369
   283
  using P_set_card Q_set_card P_set_finite Q_set_finite
huffman@23431
   284
  by (auto simp add: S_def zmult_int setsum_constant)
paulson@13871
   285
wenzelm@21233
   286
lemma S1_Int_S2_prop: "S1 \<inter> S2 = {}"
paulson@13871
   287
  by (auto simp add: S1_def S2_def)
paulson@13871
   288
wenzelm@21233
   289
lemma S1_Union_S2_prop: "S = S1 \<union> S2"
paulson@13871
   290
  apply (auto simp add: S_def P_set_def Q_set_def S1_def S2_def)
wenzelm@18369
   291
proof -
wenzelm@18369
   292
  fix a and b
wenzelm@18369
   293
  assume "~ q * a < p * b" and b1: "0 < b" and b2: "b \<le> (q - 1) div 2"
wenzelm@18369
   294
  with zless_linear have "(p * b < q * a) | (p * b = q * a)" by auto
wenzelm@18369
   295
  moreover from pb_neq_qa b1 b2 have "(p * b \<noteq> q * a)" by auto
wenzelm@18369
   296
  ultimately show "p * b < q * a" by auto
wenzelm@18369
   297
qed
paulson@13871
   298
wenzelm@21233
   299
lemma card_sum_S1_S2: "((p - 1) div 2) * ((q - 1) div 2) =
nipkow@15392
   300
    int(card(S1)) + int(card(S2))"
wenzelm@18369
   301
proof -
nipkow@15392
   302
  have "((p - 1) div 2) * ((q - 1) div 2) = int (card(S))"
paulson@13871
   303
    by (auto simp add: S_card)
nipkow@15392
   304
  also have "... = int( card(S1) + card(S2))"
paulson@13871
   305
    apply (insert S1_finite S2_finite S1_Int_S2_prop S1_Union_S2_prop)
paulson@13871
   306
    apply (drule card_Un_disjoint, auto)
wenzelm@18369
   307
    done
paulson@13871
   308
  also have "... = int(card(S1)) + int(card(S2))" by auto
nipkow@15392
   309
  finally show ?thesis .
nipkow@15392
   310
qed
paulson@13871
   311
wenzelm@21233
   312
lemma aux1a: "[| 0 < a; a \<le> (p - 1) div 2;
paulson@13871
   313
                             0 < b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   314
                          (p * b < q * a) = (b \<le> q * a div p)"
nipkow@15392
   315
proof -
nipkow@15392
   316
  assume "0 < a" and "a \<le> (p - 1) div 2" and "0 < b" and "b \<le> (q - 1) div 2"
nipkow@15392
   317
  have "p * b < q * a ==> b \<le> q * a div p"
nipkow@15392
   318
  proof -
nipkow@15392
   319
    assume "p * b < q * a"
paulson@13871
   320
    then have "p * b \<le> q * a" by auto
nipkow@15392
   321
    then have "(p * b) div p \<le> (q * a) div p"
wenzelm@18369
   322
      by (rule zdiv_mono1) (insert p_g_2, auto)
nipkow@15392
   323
    then show "b \<le> (q * a) div p"
paulson@13871
   324
      apply (subgoal_tac "p \<noteq> 0")
nipkow@30034
   325
      apply (frule div_mult_self1_is_id, force)
wenzelm@18369
   326
      apply (insert p_g_2, auto)
wenzelm@18369
   327
      done
nipkow@15392
   328
  qed
nipkow@15392
   329
  moreover have "b \<le> q * a div p ==> p * b < q * a"
nipkow@15392
   330
  proof -
nipkow@15392
   331
    assume "b \<le> q * a div p"
nipkow@15392
   332
    then have "p * b \<le> p * ((q * a) div p)"
wenzelm@18369
   333
      using p_g_2 by (auto simp add: mult_le_cancel_left)
nipkow@15392
   334
    also have "... \<le> q * a"
wenzelm@18369
   335
      by (rule zdiv_leq_prop) (insert p_g_2, auto)
nipkow@15392
   336
    finally have "p * b \<le> q * a" .
nipkow@15392
   337
    then have "p * b < q * a | p * b = q * a"
paulson@13871
   338
      by (simp only: order_le_imp_less_or_eq)
nipkow@15392
   339
    moreover have "p * b \<noteq> q * a"
wenzelm@18369
   340
      by (rule  pb_neq_qa) (insert prems, auto)
paulson@13871
   341
    ultimately show ?thesis by auto
nipkow@15392
   342
  qed
nipkow@15392
   343
  ultimately show ?thesis ..
nipkow@15392
   344
qed
paulson@13871
   345
wenzelm@21233
   346
lemma aux1b: "[| 0 < a; a \<le> (p - 1) div 2;
paulson@13871
   347
                             0 < b; b \<le> (q - 1) div 2 |] ==>
nipkow@15392
   348
                          (q * a < p * b) = (a \<le> p * b div q)"
nipkow@15392
   349
proof -
nipkow@15392
   350
  assume "0 < a" and "a \<le> (p - 1) div 2" and "0 < b" and "b \<le> (q - 1) div 2"
nipkow@15392
   351
  have "q * a < p * b ==> a \<le> p * b div q"
nipkow@15392
   352
  proof -
nipkow@15392
   353
    assume "q * a < p * b"
paulson@13871
   354
    then have "q * a \<le> p * b" by auto
nipkow@15392
   355
    then have "(q * a) div q \<le> (p * b) div q"
wenzelm@18369
   356
      by (rule zdiv_mono1) (insert q_g_2, auto)
nipkow@15392
   357
    then show "a \<le> (p * b) div q"
paulson@13871
   358
      apply (subgoal_tac "q \<noteq> 0")
nipkow@30034
   359
      apply (frule div_mult_self1_is_id, force)
wenzelm@18369
   360
      apply (insert q_g_2, auto)
wenzelm@18369
   361
      done
nipkow@15392
   362
  qed
nipkow@15392
   363
  moreover have "a \<le> p * b div q ==> q * a < p * b"
nipkow@15392
   364
  proof -
nipkow@15392
   365
    assume "a \<le> p * b div q"
nipkow@15392
   366
    then have "q * a \<le> q * ((p * b) div q)"
wenzelm@18369
   367
      using q_g_2 by (auto simp add: mult_le_cancel_left)
nipkow@15392
   368
    also have "... \<le> p * b"
wenzelm@18369
   369
      by (rule zdiv_leq_prop) (insert q_g_2, auto)
nipkow@15392
   370
    finally have "q * a \<le> p * b" .
nipkow@15392
   371
    then have "q * a < p * b | q * a = p * b"
paulson@13871
   372
      by (simp only: order_le_imp_less_or_eq)
nipkow@15392
   373
    moreover have "p * b \<noteq> q * a"
wenzelm@18369
   374
      by (rule  pb_neq_qa) (insert prems, auto)
paulson@13871
   375
    ultimately show ?thesis by auto
nipkow@15392
   376
  qed
nipkow@15392
   377
  ultimately show ?thesis ..
nipkow@15392
   378
qed
paulson@13871
   379
wenzelm@21288
   380
lemma (in -) aux2: "[| zprime p; zprime q; 2 < p; 2 < q |] ==>
nipkow@15392
   381
             (q * ((p - 1) div 2)) div p \<le> (q - 1) div 2"
nipkow@15392
   382
proof-
nipkow@16663
   383
  assume "zprime p" and "zprime q" and "2 < p" and "2 < q"
paulson@13871
   384
  (* Set up what's even and odd *)
nipkow@15392
   385
  then have "p \<in> zOdd & q \<in> zOdd"
paulson@13871
   386
    by (auto simp add:  zprime_zOdd_eq_grt_2)
nipkow@15392
   387
  then have even1: "(p - 1):zEven & (q - 1):zEven"
paulson@13871
   388
    by (auto simp add: odd_minus_one_even)
nipkow@15392
   389
  then have even2: "(2 * p):zEven & ((q - 1) * p):zEven"
paulson@13871
   390
    by (auto simp add: zEven_def)
nipkow@15392
   391
  then have even3: "(((q - 1) * p) + (2 * p)):zEven"
paulson@14434
   392
    by (auto simp: EvenOdd.even_plus_even)
paulson@13871
   393
  (* using these prove it *)
nipkow@15392
   394
  from prems have "q * (p - 1) < ((q - 1) * p) + (2 * p)"
paulson@13871
   395
    by (auto simp add: int_distrib)
nipkow@15392
   396
  then have "((p - 1) * q) div 2 < (((q - 1) * p) + (2 * p)) div 2"
nipkow@15392
   397
    apply (rule_tac x = "((p - 1) * q)" in even_div_2_l)
paulson@13871
   398
    by (auto simp add: even3, auto simp add: zmult_ac)
nipkow@15392
   399
  also have "((p - 1) * q) div 2 = q * ((p - 1) div 2)"
paulson@13871
   400
    by (auto simp add: even1 even_prod_div_2)
nipkow@15392
   401
  also have "(((q - 1) * p) + (2 * p)) div 2 = (((q - 1) div 2) * p) + p"
paulson@13871
   402
    by (auto simp add: even1 even2 even_prod_div_2 even_sum_div_2)
wenzelm@18369
   403
  finally show ?thesis
wenzelm@18369
   404
    apply (rule_tac x = " q * ((p - 1) div 2)" and
nipkow@15392
   405
                    y = "(q - 1) div 2" in div_prop2)
wenzelm@18369
   406
    using prems by auto
nipkow@15392
   407
qed
paulson@13871
   408
wenzelm@21233
   409
lemma aux3a: "\<forall>j \<in> P_set. int (card (f1 j)) = (q * j) div p"
nipkow@15392
   410
proof
nipkow@15392
   411
  fix j
nipkow@15392
   412
  assume j_fact: "j \<in> P_set"
nipkow@15392
   413
  have "int (card (f1 j)) = int (card {y. y \<in> Q_set & y \<le> (q * j) div p})"
nipkow@15392
   414
  proof -
nipkow@15392
   415
    have "finite (f1 j)"
nipkow@15392
   416
    proof -
paulson@13871
   417
      have "(f1 j) \<subseteq> S" by (auto simp add: f1_def)
paulson@13871
   418
      with S_finite show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   419
    qed
nipkow@15392
   420
    moreover have "inj_on (%(x,y). y) (f1 j)"
paulson@13871
   421
      by (auto simp add: f1_def inj_on_def)
nipkow@15392
   422
    ultimately have "card ((%(x,y). y) ` (f1 j)) = card  (f1 j)"
paulson@13871
   423
      by (auto simp add: f1_def card_image)
nipkow@15392
   424
    moreover have "((%(x,y). y) ` (f1 j)) = {y. y \<in> Q_set & y \<le> (q * j) div p}"
wenzelm@18369
   425
      using prems by (auto simp add: f1_def S_def Q_set_def P_set_def image_def)
paulson@13871
   426
    ultimately show ?thesis by (auto simp add: f1_def)
nipkow@15392
   427
  qed
nipkow@15392
   428
  also have "... = int (card {y. 0 < y & y \<le> (q * j) div p})"
nipkow@15392
   429
  proof -
wenzelm@18369
   430
    have "{y. y \<in> Q_set & y \<le> (q * j) div p} =
nipkow@15392
   431
        {y. 0 < y & y \<le> (q * j) div p}"
paulson@13871
   432
      apply (auto simp add: Q_set_def)
wenzelm@18369
   433
    proof -
wenzelm@18369
   434
      fix x
wenzelm@18369
   435
      assume "0 < x" and "x \<le> q * j div p"
wenzelm@18369
   436
      with j_fact P_set_def  have "j \<le> (p - 1) div 2" by auto
wenzelm@18369
   437
      with q_g_2 have "q * j \<le> q * ((p - 1) div 2)"
wenzelm@18369
   438
        by (auto simp add: mult_le_cancel_left)
wenzelm@18369
   439
      with p_g_2 have "q * j div p \<le> q * ((p - 1) div 2) div p"
wenzelm@18369
   440
        by (auto simp add: zdiv_mono1)
wenzelm@21233
   441
      also from prems P_set_def have "... \<le> (q - 1) div 2"
wenzelm@18369
   442
        apply simp
wenzelm@18369
   443
        apply (insert aux2)
wenzelm@18369
   444
        apply (simp add: QRTEMP_def)
wenzelm@18369
   445
        done
wenzelm@18369
   446
      finally show "x \<le> (q - 1) div 2" using prems by auto
wenzelm@18369
   447
    qed
paulson@13871
   448
    then show ?thesis by auto
nipkow@15392
   449
  qed
nipkow@15392
   450
  also have "... = (q * j) div p"
nipkow@15392
   451
  proof -
paulson@13871
   452
    from j_fact P_set_def have "0 \<le> j" by auto
paulson@14387
   453
    with q_g_2 have "q * 0 \<le> q * j" by (auto simp only: mult_left_mono)
paulson@13871
   454
    then have "0 \<le> q * j" by auto
nipkow@15392
   455
    then have "0 div p \<le> (q * j) div p"
paulson@13871
   456
      apply (rule_tac a = 0 in zdiv_mono1)
wenzelm@18369
   457
      apply (insert p_g_2, auto)
wenzelm@18369
   458
      done
paulson@13871
   459
    also have "0 div p = 0" by auto
paulson@13871
   460
    finally show ?thesis by (auto simp add: card_bdd_int_set_l_le)
nipkow@15392
   461
  qed
nipkow@15392
   462
  finally show "int (card (f1 j)) = q * j div p" .
nipkow@15392
   463
qed
paulson@13871
   464
wenzelm@21233
   465
lemma aux3b: "\<forall>j \<in> Q_set. int (card (f2 j)) = (p * j) div q"
nipkow@15392
   466
proof
nipkow@15392
   467
  fix j
nipkow@15392
   468
  assume j_fact: "j \<in> Q_set"
nipkow@15392
   469
  have "int (card (f2 j)) = int (card {y. y \<in> P_set & y \<le> (p * j) div q})"
nipkow@15392
   470
  proof -
nipkow@15392
   471
    have "finite (f2 j)"
nipkow@15392
   472
    proof -
paulson@13871
   473
      have "(f2 j) \<subseteq> S" by (auto simp add: f2_def)
paulson@13871
   474
      with S_finite show ?thesis by (auto simp add: finite_subset)
nipkow@15392
   475
    qed
nipkow@15392
   476
    moreover have "inj_on (%(x,y). x) (f2 j)"
paulson@13871
   477
      by (auto simp add: f2_def inj_on_def)
nipkow@15392
   478
    ultimately have "card ((%(x,y). x) ` (f2 j)) = card  (f2 j)"
paulson@13871
   479
      by (auto simp add: f2_def card_image)
nipkow@15392
   480
    moreover have "((%(x,y). x) ` (f2 j)) = {y. y \<in> P_set & y \<le> (p * j) div q}"
wenzelm@18369
   481
      using prems by (auto simp add: f2_def S_def Q_set_def P_set_def image_def)
paulson@13871
   482
    ultimately show ?thesis by (auto simp add: f2_def)
nipkow@15392
   483
  qed
nipkow@15392
   484
  also have "... = int (card {y. 0 < y & y \<le> (p * j) div q})"
nipkow@15392
   485
  proof -
wenzelm@18369
   486
    have "{y. y \<in> P_set & y \<le> (p * j) div q} =
nipkow@15392
   487
        {y. 0 < y & y \<le> (p * j) div q}"
paulson@13871
   488
      apply (auto simp add: P_set_def)
wenzelm@18369
   489
    proof -
wenzelm@18369
   490
      fix x
wenzelm@18369
   491
      assume "0 < x" and "x \<le> p * j div q"
wenzelm@18369
   492
      with j_fact Q_set_def  have "j \<le> (q - 1) div 2" by auto
wenzelm@18369
   493
      with p_g_2 have "p * j \<le> p * ((q - 1) div 2)"
wenzelm@18369
   494
        by (auto simp add: mult_le_cancel_left)
wenzelm@18369
   495
      with q_g_2 have "p * j div q \<le> p * ((q - 1) div 2) div q"
wenzelm@18369
   496
        by (auto simp add: zdiv_mono1)
wenzelm@18369
   497
      also from prems have "... \<le> (p - 1) div 2"
wenzelm@18369
   498
        by (auto simp add: aux2 QRTEMP_def)
wenzelm@18369
   499
      finally show "x \<le> (p - 1) div 2" using prems by auto
nipkow@15392
   500
      qed
paulson@13871
   501
    then show ?thesis by auto
nipkow@15392
   502
  qed
nipkow@15392
   503
  also have "... = (p * j) div q"
nipkow@15392
   504
  proof -
paulson@13871
   505
    from j_fact Q_set_def have "0 \<le> j" by auto
paulson@14387
   506
    with p_g_2 have "p * 0 \<le> p * j" by (auto simp only: mult_left_mono)
paulson@13871
   507
    then have "0 \<le> p * j" by auto
nipkow@15392
   508
    then have "0 div q \<le> (p * j) div q"
paulson@13871
   509
      apply (rule_tac a = 0 in zdiv_mono1)
wenzelm@18369
   510
      apply (insert q_g_2, auto)
wenzelm@18369
   511
      done
paulson@13871
   512
    also have "0 div q = 0" by auto
paulson@13871
   513
    finally show ?thesis by (auto simp add: card_bdd_int_set_l_le)
nipkow@15392
   514
  qed
nipkow@15392
   515
  finally show "int (card (f2 j)) = p * j div q" .
nipkow@15392
   516
qed
paulson@13871
   517
wenzelm@21233
   518
lemma S1_card: "int (card(S1)) = setsum (%j. (q * j) div p) P_set"
nipkow@15392
   519
proof -
nipkow@15392
   520
  have "\<forall>x \<in> P_set. finite (f1 x)"
nipkow@15392
   521
  proof
nipkow@15392
   522
    fix x
paulson@13871
   523
    have "f1 x \<subseteq> S" by (auto simp add: f1_def)
paulson@13871
   524
    with S_finite show "finite (f1 x)" by (auto simp add: finite_subset)
nipkow@15392
   525
  qed
nipkow@15392
   526
  moreover have "(\<forall>x \<in> P_set. \<forall>y \<in> P_set. x \<noteq> y --> (f1 x) \<inter> (f1 y) = {})"
paulson@13871
   527
    by (auto simp add: f1_def)
nipkow@15392
   528
  moreover note P_set_finite
wenzelm@18369
   529
  ultimately have "int(card (UNION P_set f1)) =
nipkow@15392
   530
      setsum (%x. int(card (f1 x))) P_set"
nipkow@15402
   531
    by(simp add:card_UN_disjoint int_setsum o_def)
nipkow@15392
   532
  moreover have "S1 = UNION P_set f1"
paulson@13871
   533
    by (auto simp add: f1_def S_def S1_def S2_def P_set_def Q_set_def aux1a)
wenzelm@18369
   534
  ultimately have "int(card (S1)) = setsum (%j. int(card (f1 j))) P_set"
paulson@13871
   535
    by auto
nipkow@15392
   536
  also have "... = setsum (%j. q * j div p) P_set"
nipkow@15392
   537
    using aux3a by(fastsimp intro: setsum_cong)
nipkow@15392
   538
  finally show ?thesis .
nipkow@15392
   539
qed
paulson@13871
   540
wenzelm@21233
   541
lemma S2_card: "int (card(S2)) = setsum (%j. (p * j) div q) Q_set"
nipkow@15392
   542
proof -
nipkow@15392
   543
  have "\<forall>x \<in> Q_set. finite (f2 x)"
nipkow@15392
   544
  proof
nipkow@15392
   545
    fix x
paulson@13871
   546
    have "f2 x \<subseteq> S" by (auto simp add: f2_def)
paulson@13871
   547
    with S_finite show "finite (f2 x)" by (auto simp add: finite_subset)
nipkow@15392
   548
  qed
wenzelm@18369
   549
  moreover have "(\<forall>x \<in> Q_set. \<forall>y \<in> Q_set. x \<noteq> y -->
nipkow@15392
   550
      (f2 x) \<inter> (f2 y) = {})"
paulson@13871
   551
    by (auto simp add: f2_def)
nipkow@15392
   552
  moreover note Q_set_finite
wenzelm@18369
   553
  ultimately have "int(card (UNION Q_set f2)) =
nipkow@15392
   554
      setsum (%x. int(card (f2 x))) Q_set"
nipkow@15402
   555
    by(simp add:card_UN_disjoint int_setsum o_def)
nipkow@15392
   556
  moreover have "S2 = UNION Q_set f2"
paulson@13871
   557
    by (auto simp add: f2_def S_def S1_def S2_def P_set_def Q_set_def aux1b)
wenzelm@18369
   558
  ultimately have "int(card (S2)) = setsum (%j. int(card (f2 j))) Q_set"
paulson@13871
   559
    by auto
nipkow@15392
   560
  also have "... = setsum (%j. p * j div q) Q_set"
nipkow@15392
   561
    using aux3b by(fastsimp intro: setsum_cong)
nipkow@15392
   562
  finally show ?thesis .
nipkow@15392
   563
qed
paulson@13871
   564
wenzelm@21233
   565
lemma S1_carda: "int (card(S1)) =
nipkow@15392
   566
    setsum (%j. (j * q) div p) P_set"
paulson@13871
   567
  by (auto simp add: S1_card zmult_ac)
paulson@13871
   568
wenzelm@21233
   569
lemma S2_carda: "int (card(S2)) =
nipkow@15392
   570
    setsum (%j. (j * p) div q) Q_set"
paulson@13871
   571
  by (auto simp add: S2_card zmult_ac)
paulson@13871
   572
wenzelm@21233
   573
lemma pq_sum_prop: "(setsum (%j. (j * p) div q) Q_set) +
nipkow@15392
   574
    (setsum (%j. (j * q) div p) P_set) = ((p - 1) div 2) * ((q - 1) div 2)"
nipkow@15392
   575
proof -
wenzelm@18369
   576
  have "(setsum (%j. (j * p) div q) Q_set) +
nipkow@15392
   577
      (setsum (%j. (j * q) div p) P_set) = int (card S2) + int (card S1)"
paulson@13871
   578
    by (auto simp add: S1_carda S2_carda)
nipkow@15392
   579
  also have "... = int (card S1) + int (card S2)"
paulson@13871
   580
    by auto
nipkow@15392
   581
  also have "... = ((p - 1) div 2) * ((q - 1) div 2)"
paulson@13871
   582
    by (auto simp add: card_sum_S1_S2)
nipkow@15392
   583
  finally show ?thesis .
nipkow@15392
   584
qed
paulson@13871
   585
wenzelm@21233
   586
wenzelm@21288
   587
lemma (in -) 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
wenzelm@21233
   595
lemma QR_short: "(Legendre p q) * (Legendre q p) =
nipkow@15392
   596
    (-1::int)^nat(((p - 1) div 2)*((q - 1) div 2))"
nipkow@15392
   597
proof -
nipkow@15392
   598
  from prems have "~([p = 0] (mod q))"
paulson@13871
   599
    by (auto simp add: pq_prime_neq QRTEMP_def)
wenzelm@21233
   600
  with prems Q_set_def have a1: "(Legendre p q) = (-1::int) ^
nipkow@15392
   601
      nat(setsum (%x. ((x * p) div q)) Q_set)"
paulson@13871
   602
    apply (rule_tac p = q in  MainQRLemma)
wenzelm@18369
   603
    apply (auto simp add: zprime_zOdd_eq_grt_2 QRTEMP_def)
wenzelm@18369
   604
    done
nipkow@15392
   605
  from prems have "~([q = 0] (mod p))"
paulson@13871
   606
    apply (rule_tac p = q and q = p in pq_prime_neq)
nipkow@15392
   607
    apply (simp add: QRTEMP_def)+
nipkow@16733
   608
    done
wenzelm@21233
   609
  with prems P_set_def have a2: "(Legendre q p) =
nipkow@15392
   610
      (-1::int) ^ nat(setsum (%x. ((x * q) div p)) P_set)"
paulson@13871
   611
    apply (rule_tac p = p in  MainQRLemma)
wenzelm@18369
   612
    apply (auto simp add: zprime_zOdd_eq_grt_2 QRTEMP_def)
wenzelm@18369
   613
    done
wenzelm@18369
   614
  from a1 a2 have "(Legendre p q) * (Legendre q p) =
paulson@13871
   615
      (-1::int) ^ nat(setsum (%x. ((x * p) div q)) Q_set) *
nipkow@15392
   616
        (-1::int) ^ nat(setsum (%x. ((x * q) div p)) P_set)"
paulson@13871
   617
    by auto
wenzelm@18369
   618
  also have "... = (-1::int) ^ (nat(setsum (%x. ((x * p) div q)) Q_set) +
nipkow@15392
   619
                   nat(setsum (%x. ((x * q) div p)) P_set))"
paulson@13871
   620
    by (auto simp add: zpower_zadd_distrib)
wenzelm@18369
   621
  also have "nat(setsum (%x. ((x * p) div q)) Q_set) +
paulson@13871
   622
      nat(setsum (%x. ((x * q) div p)) P_set) =
wenzelm@18369
   623
        nat((setsum (%x. ((x * p) div q)) Q_set) +
nipkow@15392
   624
          (setsum (%x. ((x * q) div p)) P_set))"
wenzelm@20898
   625
    apply (rule_tac z = "setsum (%x. ((x * p) div q)) Q_set" in
wenzelm@18369
   626
      nat_add_distrib [symmetric])
wenzelm@18369
   627
    apply (auto simp add: S1_carda [symmetric] S2_carda [symmetric])
wenzelm@18369
   628
    done
nipkow@15392
   629
  also have "... = nat(((p - 1) div 2) * ((q - 1) div 2))"
paulson@13871
   630
    by (auto simp add: pq_sum_prop)
nipkow@15392
   631
  finally show ?thesis .
nipkow@15392
   632
qed
paulson@13871
   633
wenzelm@21233
   634
end
wenzelm@21233
   635
paulson@13871
   636
theorem Quadratic_Reciprocity:
wenzelm@18369
   637
     "[| p \<in> zOdd; zprime p; q \<in> zOdd; zprime q;
wenzelm@18369
   638
         p \<noteq> q |]
wenzelm@18369
   639
      ==> (Legendre p q) * (Legendre q p) =
nipkow@15392
   640
          (-1::int)^nat(((p - 1) div 2)*((q - 1) div 2))"
wenzelm@18369
   641
  by (auto simp add: QRTEMP.QR_short zprime_zOdd_eq_grt_2 [symmetric]
paulson@13871
   642
                     QRTEMP_def)
paulson@13871
   643
paulson@13871
   644
end