src/HOL/Old_Number_Theory/EvenOdd.thy
author haftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963 977b94b64905
parent 32479 521cc9bf2958
child 38159 e9b4835a54ee
permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
paulson@13871
     1
(*  Title:      HOL/Quadratic_Reciprocity/EvenOdd.thy
paulson@13871
     2
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871
     3
*)
paulson@13871
     4
paulson@13871
     5
header {*Parity: Even and Odd Integers*}
paulson@13871
     6
haftmann@27651
     7
theory EvenOdd
haftmann@27651
     8
imports Int2
haftmann@27651
     9
begin
paulson@13871
    10
wenzelm@19670
    11
definition
wenzelm@21404
    12
  zOdd    :: "int set" where
wenzelm@19670
    13
  "zOdd = {x. \<exists>k. x = 2 * k + 1}"
wenzelm@21404
    14
wenzelm@21404
    15
definition
wenzelm@21404
    16
  zEven   :: "int set" where
wenzelm@19670
    17
  "zEven = {x. \<exists>k. x = 2 * k}"
paulson@13871
    18
wenzelm@19670
    19
subsection {* Some useful properties about even and odd *}
paulson@13871
    20
wenzelm@18369
    21
lemma zOddI [intro?]: "x = 2 * k + 1 \<Longrightarrow> x \<in> zOdd"
wenzelm@18369
    22
  and zOddE [elim?]: "x \<in> zOdd \<Longrightarrow> (!!k. x = 2 * k + 1 \<Longrightarrow> C) \<Longrightarrow> C"
wenzelm@18369
    23
  by (auto simp add: zOdd_def)
paulson@13871
    24
wenzelm@18369
    25
lemma zEvenI [intro?]: "x = 2 * k \<Longrightarrow> x \<in> zEven"
wenzelm@18369
    26
  and zEvenE [elim?]: "x \<in> zEven \<Longrightarrow> (!!k. x = 2 * k \<Longrightarrow> C) \<Longrightarrow> C"
wenzelm@18369
    27
  by (auto simp add: zEven_def)
wenzelm@18369
    28
wenzelm@18369
    29
lemma one_not_even: "~(1 \<in> zEven)"
wenzelm@18369
    30
proof
wenzelm@18369
    31
  assume "1 \<in> zEven"
wenzelm@18369
    32
  then obtain k :: int where "1 = 2 * k" ..
wenzelm@18369
    33
  then show False by arith
wenzelm@18369
    34
qed
paulson@13871
    35
wenzelm@18369
    36
lemma even_odd_conj: "~(x \<in> zOdd & x \<in> zEven)"
wenzelm@18369
    37
proof -
wenzelm@18369
    38
  {
wenzelm@18369
    39
    fix a b
wenzelm@18369
    40
    assume "2 * (a::int) = 2 * (b::int) + 1"
wenzelm@18369
    41
    then have "2 * (a::int) - 2 * (b :: int) = 1"
wenzelm@18369
    42
      by arith
wenzelm@18369
    43
    then have "2 * (a - b) = 1"
wenzelm@18369
    44
      by (auto simp add: zdiff_zmult_distrib)
wenzelm@18369
    45
    moreover have "(2 * (a - b)):zEven"
wenzelm@18369
    46
      by (auto simp only: zEven_def)
wenzelm@18369
    47
    ultimately have False
wenzelm@18369
    48
      by (auto simp add: one_not_even)
wenzelm@18369
    49
  }
wenzelm@18369
    50
  then show ?thesis
wenzelm@18369
    51
    by (auto simp add: zOdd_def zEven_def)
wenzelm@18369
    52
qed
paulson@13871
    53
wenzelm@18369
    54
lemma even_odd_disj: "(x \<in> zOdd | x \<in> zEven)"
wenzelm@18369
    55
  by (simp add: zOdd_def zEven_def) arith
paulson@13871
    56
wenzelm@18369
    57
lemma not_odd_impl_even: "~(x \<in> zOdd) ==> x \<in> zEven"
wenzelm@18369
    58
  using even_odd_disj by auto
paulson@13871
    59
wenzelm@18369
    60
lemma odd_mult_odd_prop: "(x*y):zOdd ==> x \<in> zOdd"
wenzelm@18369
    61
proof (rule classical)
wenzelm@18369
    62
  assume "\<not> ?thesis"
wenzelm@18369
    63
  then have "x \<in> zEven" by (rule not_odd_impl_even)
wenzelm@18369
    64
  then obtain a where a: "x = 2 * a" ..
wenzelm@18369
    65
  assume "x * y : zOdd"
wenzelm@18369
    66
  then obtain b where "x * y = 2 * b + 1" ..
wenzelm@18369
    67
  with a have "2 * a * y = 2 * b + 1" by simp
wenzelm@18369
    68
  then have "2 * a * y - 2 * b = 1"
wenzelm@18369
    69
    by arith
wenzelm@18369
    70
  then have "2 * (a * y - b) = 1"
wenzelm@18369
    71
    by (auto simp add: zdiff_zmult_distrib)
wenzelm@18369
    72
  moreover have "(2 * (a * y - b)):zEven"
wenzelm@18369
    73
    by (auto simp only: zEven_def)
wenzelm@18369
    74
  ultimately have False
wenzelm@18369
    75
    by (auto simp add: one_not_even)
wenzelm@18369
    76
  then show ?thesis ..
wenzelm@18369
    77
qed
wenzelm@18369
    78
wenzelm@18369
    79
lemma odd_minus_one_even: "x \<in> zOdd ==> (x - 1):zEven"
paulson@13871
    80
  by (auto simp add: zOdd_def zEven_def)
paulson@13871
    81
wenzelm@18369
    82
lemma even_div_2_prop1: "x \<in> zEven ==> (x mod 2) = 0"
paulson@13871
    83
  by (auto simp add: zEven_def)
paulson@13871
    84
wenzelm@18369
    85
lemma even_div_2_prop2: "x \<in> zEven ==> (2 * (x div 2)) = x"
paulson@13871
    86
  by (auto simp add: zEven_def)
paulson@13871
    87
wenzelm@18369
    88
lemma even_plus_even: "[| x \<in> zEven; y \<in> zEven |] ==> x + y \<in> zEven"
paulson@13871
    89
  apply (auto simp add: zEven_def)
wenzelm@18369
    90
  apply (auto simp only: zadd_zmult_distrib2 [symmetric])
wenzelm@18369
    91
  done
paulson@13871
    92
wenzelm@18369
    93
lemma even_times_either: "x \<in> zEven ==> x * y \<in> zEven"
paulson@13871
    94
  by (auto simp add: zEven_def)
paulson@13871
    95
wenzelm@18369
    96
lemma even_minus_even: "[| x \<in> zEven; y \<in> zEven |] ==> x - y \<in> zEven"
paulson@13871
    97
  apply (auto simp add: zEven_def)
wenzelm@18369
    98
  apply (auto simp only: zdiff_zmult_distrib2 [symmetric])
wenzelm@18369
    99
  done
paulson@13871
   100
wenzelm@18369
   101
lemma odd_minus_odd: "[| x \<in> zOdd; y \<in> zOdd |] ==> x - y \<in> zEven"
paulson@13871
   102
  apply (auto simp add: zOdd_def zEven_def)
wenzelm@18369
   103
  apply (auto simp only: zdiff_zmult_distrib2 [symmetric])
wenzelm@18369
   104
  done
paulson@13871
   105
wenzelm@18369
   106
lemma even_minus_odd: "[| x \<in> zEven; y \<in> zOdd |] ==> x - y \<in> zOdd"
paulson@13871
   107
  apply (auto simp add: zOdd_def zEven_def)
paulson@13871
   108
  apply (rule_tac x = "k - ka - 1" in exI)
wenzelm@18369
   109
  apply auto
wenzelm@18369
   110
  done
paulson@13871
   111
wenzelm@18369
   112
lemma odd_minus_even: "[| x \<in> zOdd; y \<in> zEven |] ==> x - y \<in> zOdd"
paulson@13871
   113
  apply (auto simp add: zOdd_def zEven_def)
wenzelm@18369
   114
  apply (auto simp only: zdiff_zmult_distrib2 [symmetric])
wenzelm@18369
   115
  done
paulson@13871
   116
wenzelm@18369
   117
lemma odd_times_odd: "[| x \<in> zOdd;  y \<in> zOdd |] ==> x * y \<in> zOdd"
paulson@13871
   118
  apply (auto simp add: zOdd_def zadd_zmult_distrib zadd_zmult_distrib2)
paulson@13871
   119
  apply (rule_tac x = "2 * ka * k + ka + k" in exI)
wenzelm@18369
   120
  apply (auto simp add: zadd_zmult_distrib)
wenzelm@18369
   121
  done
paulson@13871
   122
wenzelm@18369
   123
lemma odd_iff_not_even: "(x \<in> zOdd) = (~ (x \<in> zEven))"
wenzelm@18369
   124
  using even_odd_conj even_odd_disj by auto
wenzelm@18369
   125
wenzelm@18369
   126
lemma even_product: "x * y \<in> zEven ==> x \<in> zEven | y \<in> zEven"
wenzelm@18369
   127
  using odd_iff_not_even odd_times_odd by auto
paulson@13871
   128
wenzelm@18369
   129
lemma even_diff: "x - y \<in> zEven = ((x \<in> zEven) = (y \<in> zEven))"
wenzelm@18369
   130
proof
wenzelm@18369
   131
  assume xy: "x - y \<in> zEven"
wenzelm@18369
   132
  {
wenzelm@18369
   133
    assume x: "x \<in> zEven"
wenzelm@18369
   134
    have "y \<in> zEven"
wenzelm@18369
   135
    proof (rule classical)
wenzelm@18369
   136
      assume "\<not> ?thesis"
wenzelm@18369
   137
      then have "y \<in> zOdd"
wenzelm@18369
   138
        by (simp add: odd_iff_not_even)
wenzelm@18369
   139
      with x have "x - y \<in> zOdd"
wenzelm@18369
   140
        by (simp add: even_minus_odd)
wenzelm@18369
   141
      with xy have False
wenzelm@18369
   142
        by (auto simp add: odd_iff_not_even)
wenzelm@18369
   143
      then show ?thesis ..
wenzelm@18369
   144
    qed
wenzelm@18369
   145
  } moreover {
wenzelm@18369
   146
    assume y: "y \<in> zEven"
wenzelm@18369
   147
    have "x \<in> zEven"
wenzelm@18369
   148
    proof (rule classical)
wenzelm@18369
   149
      assume "\<not> ?thesis"
wenzelm@18369
   150
      then have "x \<in> zOdd"
wenzelm@18369
   151
        by (auto simp add: odd_iff_not_even)
wenzelm@18369
   152
      with y have "x - y \<in> zOdd"
wenzelm@18369
   153
        by (simp add: odd_minus_even)
wenzelm@18369
   154
      with xy have False
wenzelm@18369
   155
        by (auto simp add: odd_iff_not_even)
wenzelm@18369
   156
      then show ?thesis ..
wenzelm@18369
   157
    qed
wenzelm@18369
   158
  }
wenzelm@18369
   159
  ultimately show "(x \<in> zEven) = (y \<in> zEven)"
wenzelm@18369
   160
    by (auto simp add: odd_iff_not_even even_minus_even odd_minus_odd
wenzelm@18369
   161
      even_minus_odd odd_minus_even)
wenzelm@18369
   162
next
wenzelm@18369
   163
  assume "(x \<in> zEven) = (y \<in> zEven)"
wenzelm@18369
   164
  then show "x - y \<in> zEven"
wenzelm@18369
   165
    by (auto simp add: odd_iff_not_even even_minus_even odd_minus_odd
wenzelm@18369
   166
      even_minus_odd odd_minus_even)
wenzelm@18369
   167
qed
paulson@13871
   168
wenzelm@18369
   169
lemma neg_one_even_power: "[| x \<in> zEven; 0 \<le> x |] ==> (-1::int)^(nat x) = 1"
wenzelm@18369
   170
proof -
wenzelm@20369
   171
  assume "x \<in> zEven" and "0 \<le> x"
wenzelm@20369
   172
  from `x \<in> zEven` obtain a where "x = 2 * a" ..
wenzelm@20369
   173
  with `0 \<le> x` have "0 \<le> a" by simp
wenzelm@20369
   174
  from `0 \<le> x` and `x = 2 * a` have "nat x = nat (2 * a)"
wenzelm@18369
   175
    by simp
wenzelm@20369
   176
  also from `x = 2 * a` have "nat (2 * a) = 2 * nat a"
wenzelm@18369
   177
    by (simp add: nat_mult_distrib)
wenzelm@18369
   178
  finally have "(-1::int)^nat x = (-1)^(2 * nat a)"
wenzelm@18369
   179
    by simp
wenzelm@18369
   180
  also have "... = ((-1::int)^2)^ (nat a)"
wenzelm@18369
   181
    by (simp add: zpower_zpower [symmetric])
wenzelm@18369
   182
  also have "(-1::int)^2 = 1"
wenzelm@18369
   183
    by simp
wenzelm@18369
   184
  finally show ?thesis
wenzelm@18369
   185
    by simp
wenzelm@18369
   186
qed
paulson@13871
   187
wenzelm@18369
   188
lemma neg_one_odd_power: "[| x \<in> zOdd; 0 \<le> x |] ==> (-1::int)^(nat x) = -1"
wenzelm@18369
   189
proof -
wenzelm@20369
   190
  assume "x \<in> zOdd" and "0 \<le> x"
wenzelm@20369
   191
  from `x \<in> zOdd` obtain a where "x = 2 * a + 1" ..
wenzelm@20369
   192
  with `0 \<le> x` have a: "0 \<le> a" by simp
wenzelm@20369
   193
  with `0 \<le> x` and `x = 2 * a + 1` have "nat x = nat (2 * a + 1)"
wenzelm@18369
   194
    by simp
wenzelm@18369
   195
  also from a have "nat (2 * a + 1) = 2 * nat a + 1"
wenzelm@18369
   196
    by (auto simp add: nat_mult_distrib nat_add_distrib)
wenzelm@18369
   197
  finally have "(-1::int)^nat x = (-1)^(2 * nat a + 1)"
wenzelm@18369
   198
    by simp
wenzelm@18369
   199
  also have "... = ((-1::int)^2)^ (nat a) * (-1)^1"
wenzelm@18369
   200
    by (auto simp add: zpower_zpower [symmetric] zpower_zadd_distrib)
wenzelm@18369
   201
  also have "(-1::int)^2 = 1"
wenzelm@18369
   202
    by simp
wenzelm@18369
   203
  finally show ?thesis
wenzelm@18369
   204
    by simp
wenzelm@18369
   205
qed
wenzelm@18369
   206
wenzelm@18369
   207
lemma neg_one_power_parity: "[| 0 \<le> x; 0 \<le> y; (x \<in> zEven) = (y \<in> zEven) |] ==>
wenzelm@20369
   208
    (-1::int)^(nat x) = (-1::int)^(nat y)"
wenzelm@18369
   209
  using even_odd_disj [of x] even_odd_disj [of y]
paulson@13871
   210
  by (auto simp add: neg_one_even_power neg_one_odd_power)
paulson@13871
   211
wenzelm@18369
   212
wenzelm@18369
   213
lemma one_not_neg_one_mod_m: "2 < m ==> ~([1 = -1] (mod m))"
paulson@13871
   214
  by (auto simp add: zcong_def zdvd_not_zless)
paulson@13871
   215
wenzelm@18369
   216
lemma even_div_2_l: "[| y \<in> zEven; x < y |] ==> x div 2 < y div 2"
wenzelm@18369
   217
proof -
wenzelm@20369
   218
  assume "y \<in> zEven" and "x < y"
wenzelm@20369
   219
  from `y \<in> zEven` obtain k where k: "y = 2 * k" ..
wenzelm@20369
   220
  with `x < y` have "x < 2 * k" by simp
wenzelm@18369
   221
  then have "x div 2 < k" by (auto simp add: div_prop1)
wenzelm@18369
   222
  also have "k = (2 * k) div 2" by simp
wenzelm@18369
   223
  finally have "x div 2 < 2 * k div 2" by simp
wenzelm@18369
   224
  with k show ?thesis by simp
wenzelm@18369
   225
qed
paulson@13871
   226
wenzelm@18369
   227
lemma even_sum_div_2: "[| x \<in> zEven; y \<in> zEven |] ==> (x + y) div 2 = x div 2 + y div 2"
haftmann@27651
   228
  by (auto simp add: zEven_def)
paulson@13871
   229
wenzelm@18369
   230
lemma even_prod_div_2: "[| x \<in> zEven |] ==> (x * y) div 2 = (x div 2) * y"
paulson@13871
   231
  by (auto simp add: zEven_def)
paulson@13871
   232
paulson@13871
   233
(* An odd prime is greater than 2 *)
paulson@13871
   234
wenzelm@18369
   235
lemma zprime_zOdd_eq_grt_2: "zprime p ==> (p \<in> zOdd) = (2 < p)"
paulson@13871
   236
  apply (auto simp add: zOdd_def zprime_def)
paulson@13871
   237
  apply (drule_tac x = 2 in allE)
wenzelm@18369
   238
  using odd_iff_not_even [of p]
wenzelm@18369
   239
  apply (auto simp add: zOdd_def zEven_def)
wenzelm@18369
   240
  done
paulson@13871
   241
paulson@13871
   242
(* Powers of -1 and parity *)
paulson@13871
   243
wenzelm@18369
   244
lemma neg_one_special: "finite A ==>
wenzelm@18369
   245
    ((-1 :: int) ^ card A) * (-1 ^ card A) = 1"
berghofe@22274
   246
  by (induct set: finite) auto
paulson@13871
   247
wenzelm@18369
   248
lemma neg_one_power: "(-1::int)^n = 1 | (-1::int)^n = -1"
wenzelm@18369
   249
  by (induct n) auto
paulson@13871
   250
paulson@13871
   251
lemma neg_one_power_eq_mod_m: "[| 2 < m; [(-1::int)^j = (-1::int)^k] (mod m) |]
wenzelm@18369
   252
    ==> ((-1::int)^j = (-1::int)^k)"
wenzelm@26289
   253
  using neg_one_power [of j] and ListMem.insert neg_one_power [of k]
paulson@13871
   254
  by (auto simp add: one_not_neg_one_mod_m zcong_sym)
paulson@13871
   255
wenzelm@18369
   256
end