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