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