src/HOL/Parity.thy
 author blanchet Wed Sep 24 15:45:55 2014 +0200 (2014-09-24) changeset 58425 246985c6b20b parent 54489 03ff4d1e6784 child 58645 94bef115c08f permissions -rw-r--r--
simpler proof
```     1 (*  Title:      HOL/Parity.thy
```
```     2     Author:     Jeremy Avigad
```
```     3     Author:     Jacques D. Fleuriot
```
```     4 *)
```
```     5
```
```     6 header {* Even and Odd for int and nat *}
```
```     7
```
```     8 theory Parity
```
```     9 imports Main
```
```    10 begin
```
```    11
```
```    12 class even_odd = semiring_div_parity
```
```    13 begin
```
```    14
```
```    15 definition even :: "'a \<Rightarrow> bool"
```
```    16 where
```
```    17   even_def [presburger]: "even a \<longleftrightarrow> a mod 2 = 0"
```
```    18
```
```    19 lemma even_iff_2_dvd [algebra]:
```
```    20   "even a \<longleftrightarrow> 2 dvd a"
```
```    21   by (simp add: even_def dvd_eq_mod_eq_0)
```
```    22
```
```    23 lemma even_zero [simp]:
```
```    24   "even 0"
```
```    25   by (simp add: even_def)
```
```    26
```
```    27 lemma even_times_anything:
```
```    28   "even a \<Longrightarrow> even (a * b)"
```
```    29   by (simp add: even_iff_2_dvd)
```
```    30
```
```    31 lemma anything_times_even:
```
```    32   "even a \<Longrightarrow> even (b * a)"
```
```    33   by (simp add: even_iff_2_dvd)
```
```    34
```
```    35 abbreviation odd :: "'a \<Rightarrow> bool"
```
```    36 where
```
```    37   "odd a \<equiv> \<not> even a"
```
```    38
```
```    39 lemma odd_times_odd:
```
```    40   "odd a \<Longrightarrow> odd b \<Longrightarrow> odd (a * b)"
```
```    41   by (auto simp add: even_def mod_mult_left_eq)
```
```    42
```
```    43 lemma even_product [simp, presburger]:
```
```    44   "even (a * b) \<longleftrightarrow> even a \<or> even b"
```
```    45   apply (auto simp add: even_times_anything anything_times_even)
```
```    46   apply (rule ccontr)
```
```    47   apply (auto simp add: odd_times_odd)
```
```    48   done
```
```    49
```
```    50 end
```
```    51
```
```    52 instance nat and int  :: even_odd ..
```
```    53
```
```    54 lemma even_nat_def [presburger]:
```
```    55   "even x \<longleftrightarrow> even (int x)"
```
```    56   by (auto simp add: even_def int_eq_iff int_mult nat_mult_distrib)
```
```    57
```
```    58 lemma transfer_int_nat_relations:
```
```    59   "even (int x) \<longleftrightarrow> even x"
```
```    60   by (simp add: even_nat_def)
```
```    61
```
```    62 declare transfer_morphism_int_nat[transfer add return:
```
```    63   transfer_int_nat_relations
```
```    64 ]
```
```    65
```
```    66 lemma odd_one_int [simp]:
```
```    67   "odd (1::int)"
```
```    68   by presburger
```
```    69
```
```    70 lemma odd_1_nat [simp]:
```
```    71   "odd (1::nat)"
```
```    72   by presburger
```
```    73
```
```    74 lemma even_numeral_int [simp]: "even (numeral (Num.Bit0 k) :: int)"
```
```    75   unfolding even_def by simp
```
```    76
```
```    77 lemma odd_numeral_int [simp]: "odd (numeral (Num.Bit1 k) :: int)"
```
```    78   unfolding even_def by simp
```
```    79
```
```    80 (* TODO: proper simp rules for Num.Bit0, Num.Bit1 *)
```
```    81 declare even_def [of "- numeral v", simp] for v
```
```    82
```
```    83 lemma even_numeral_nat [simp]: "even (numeral (Num.Bit0 k) :: nat)"
```
```    84   unfolding even_nat_def by simp
```
```    85
```
```    86 lemma odd_numeral_nat [simp]: "odd (numeral (Num.Bit1 k) :: nat)"
```
```    87   unfolding even_nat_def by simp
```
```    88
```
```    89 subsection {* Even and odd are mutually exclusive *}
```
```    90
```
```    91
```
```    92 subsection {* Behavior under integer arithmetic operations *}
```
```    93
```
```    94 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
```
```    95 by presburger
```
```    96
```
```    97 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
```
```    98 by presburger
```
```    99
```
```   100 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
```
```   101 by presburger
```
```   102
```
```   103 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
```
```   104
```
```   105 lemma even_sum[simp,presburger]:
```
```   106   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
```
```   107 by presburger
```
```   108
```
```   109 lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
```
```   110 by presburger
```
```   111
```
```   112 lemma even_difference[simp]:
```
```   113     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
```
```   114
```
```   115 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
```
```   116 by (induct n) auto
```
```   117
```
```   118 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
```
```   119
```
```   120
```
```   121 subsection {* Equivalent definitions *}
```
```   122
```
```   123 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x"
```
```   124 by presburger
```
```   125
```
```   126 lemma two_times_odd_div_two_plus_one:
```
```   127   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
```
```   128 by presburger
```
```   129
```
```   130 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
```
```   131
```
```   132 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
```
```   133
```
```   134 subsection {* even and odd for nats *}
```
```   135
```
```   136 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
```
```   137 by (simp add: even_nat_def)
```
```   138
```
```   139 lemma even_product_nat[simp,presburger,algebra]:
```
```   140   "even((x::nat) * y) = (even x | even y)"
```
```   141 by (simp add: even_nat_def int_mult)
```
```   142
```
```   143 lemma even_sum_nat[simp,presburger,algebra]:
```
```   144   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
```
```   145 by presburger
```
```   146
```
```   147 lemma even_difference_nat[simp,presburger,algebra]:
```
```   148   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
```
```   149 by presburger
```
```   150
```
```   151 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
```
```   152 by presburger
```
```   153
```
```   154 lemma even_power_nat[simp,presburger,algebra]:
```
```   155   "even ((x::nat)^y) = (even x & 0 < y)"
```
```   156 by (simp add: even_nat_def int_power)
```
```   157
```
```   158
```
```   159 subsection {* Equivalent definitions *}
```
```   160
```
```   161 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
```
```   162 by presburger
```
```   163
```
```   164 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
```
```   165 by presburger
```
```   166
```
```   167 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
```
```   168 by presburger
```
```   169
```
```   170 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
```
```   171 by presburger
```
```   172
```
```   173 lemma even_nat_div_two_times_two: "even (x::nat) ==>
```
```   174     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
```
```   175
```
```   176 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
```
```   177     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
```
```   178
```
```   179 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
```
```   180 by presburger
```
```   181
```
```   182 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
```
```   183 by presburger
```
```   184
```
```   185
```
```   186 subsection {* Parity and powers *}
```
```   187
```
```   188 lemma (in comm_ring_1) neg_power_if:
```
```   189   "(- a) ^ n = (if even n then (a ^ n) else - (a ^ n))"
```
```   190   by (induct n) simp_all
```
```   191
```
```   192 lemma (in comm_ring_1)
```
```   193   shows neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
```
```   194   and neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
```
```   195   by (simp_all add: neg_power_if)
```
```   196
```
```   197 lemma zero_le_even_power: "even n ==>
```
```   198     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
```
```   199   apply (simp add: even_nat_equiv_def2)
```
```   200   apply (erule exE)
```
```   201   apply (erule ssubst)
```
```   202   apply (subst power_add)
```
```   203   apply (rule zero_le_square)
```
```   204   done
```
```   205
```
```   206 lemma zero_le_odd_power: "odd n ==>
```
```   207     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
```
```   208 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
```
```   209 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
```
```   210 done
```
```   211
```
```   212 lemma zero_le_power_eq [presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
```
```   213     (even n | (odd n & 0 <= x))"
```
```   214   apply auto
```
```   215   apply (subst zero_le_odd_power [symmetric])
```
```   216   apply assumption+
```
```   217   apply (erule zero_le_even_power)
```
```   218   done
```
```   219
```
```   220 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
```
```   221     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
```
```   222
```
```   223   unfolding order_less_le zero_le_power_eq by auto
```
```   224
```
```   225 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
```
```   226     (odd n & x < 0)"
```
```   227   apply (subst linorder_not_le [symmetric])+
```
```   228   apply (subst zero_le_power_eq)
```
```   229   apply auto
```
```   230   done
```
```   231
```
```   232 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
```
```   233     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
```
```   234   apply (subst linorder_not_less [symmetric])+
```
```   235   apply (subst zero_less_power_eq)
```
```   236   apply auto
```
```   237   done
```
```   238
```
```   239 lemma power_even_abs: "even n ==>
```
```   240     (abs (x::'a::{linordered_idom}))^n = x^n"
```
```   241   apply (subst power_abs [symmetric])
```
```   242   apply (simp add: zero_le_even_power)
```
```   243   done
```
```   244
```
```   245 lemma power_minus_even [simp]: "even n ==>
```
```   246     (- x)^n = (x^n::'a::{comm_ring_1})"
```
```   247   apply (subst power_minus)
```
```   248   apply simp
```
```   249   done
```
```   250
```
```   251 lemma power_minus_odd [simp]: "odd n ==>
```
```   252     (- x)^n = - (x^n::'a::{comm_ring_1})"
```
```   253   apply (subst power_minus)
```
```   254   apply simp
```
```   255   done
```
```   256
```
```   257 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
```
```   258   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
```
```   259   shows "x^n \<le> y^n"
```
```   260 proof -
```
```   261   have "0 \<le> \<bar>x\<bar>" by auto
```
```   262   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
```
```   263   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
```
```   264   thus ?thesis unfolding power_even_abs[OF `even n`] .
```
```   265 qed
```
```   266
```
```   267 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
```
```   268
```
```   269 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
```
```   270   assumes "odd n" and "x \<le> y"
```
```   271   shows "x^n \<le> y^n"
```
```   272 proof (cases "y < 0")
```
```   273   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
```
```   274   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
```
```   275   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
```
```   276 next
```
```   277   case False
```
```   278   show ?thesis
```
```   279   proof (cases "x < 0")
```
```   280     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
```
```   281     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
```
```   282     moreover
```
```   283     from `\<not> y < 0` have "0 \<le> y" by auto
```
```   284     hence "0 \<le> y^n" by auto
```
```   285     ultimately show ?thesis by auto
```
```   286   next
```
```   287     case False hence "0 \<le> x" by auto
```
```   288     with `x \<le> y` show ?thesis using power_mono by auto
```
```   289   qed
```
```   290 qed
```
```   291
```
```   292
```
```   293 subsection {* More Even/Odd Results *}
```
```   294
```
```   295 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
```
```   296 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
```
```   297 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
```
```   298
```
```   299 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
```
```   300
```
```   301 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
```
```   302
```
```   303 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
```
```   304 by presburger
```
```   305
```
```   306 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
```
```   307 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
```
```   308
```
```   309 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
```
```   310
```
```   311 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
```
```   312   by presburger
```
```   313
```
```   314 text {* Simplify, when the exponent is a numeral *}
```
```   315
```
```   316 lemmas zero_le_power_eq_numeral [simp] =
```
```   317   zero_le_power_eq [of _ "numeral w"] for w
```
```   318
```
```   319 lemmas zero_less_power_eq_numeral [simp] =
```
```   320   zero_less_power_eq [of _ "numeral w"] for w
```
```   321
```
```   322 lemmas power_le_zero_eq_numeral [simp] =
```
```   323   power_le_zero_eq [of _ "numeral w"] for w
```
```   324
```
```   325 lemmas power_less_zero_eq_numeral [simp] =
```
```   326   power_less_zero_eq [of _ "numeral w"] for w
```
```   327
```
```   328 lemmas zero_less_power_nat_eq_numeral [simp] =
```
```   329   nat_zero_less_power_iff [of _ "numeral w"] for w
```
```   330
```
```   331 lemmas power_eq_0_iff_numeral [simp] =
```
```   332   power_eq_0_iff [of _ "numeral w"] for w
```
```   333
```
```   334 lemmas power_even_abs_numeral [simp] =
```
```   335   power_even_abs [of "numeral w" _] for w
```
```   336
```
```   337
```
```   338 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
```
```   339
```
```   340 lemma zero_le_power_iff[presburger]:
```
```   341   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
```
```   342 proof cases
```
```   343   assume even: "even n"
```
```   344   then obtain k where "n = 2*k"
```
```   345     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
```
```   346   thus ?thesis by (simp add: zero_le_even_power even)
```
```   347 next
```
```   348   assume odd: "odd n"
```
```   349   then obtain k where "n = Suc(2*k)"
```
```   350     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
```
```   351   moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
```
```   352     by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
```
```   353   ultimately show ?thesis
```
```   354     by (auto simp add: zero_le_mult_iff zero_le_even_power)
```
```   355 qed
```
```   356
```
```   357
```
```   358 subsection {* Miscellaneous *}
```
```   359
```
```   360 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
```
```   361 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
```
```   362 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
```
```   363 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
```
```   364
```
```   365 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
```
```   366 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
```
```   367     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
```
```   368
```
```   369 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
```
```   370     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
```
```   371
```
```   372 end
```
```   373
```