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