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