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