src/HOL/Parity.thy
 author kuncar Fri Dec 09 18:07:04 2011 +0100 (2011-12-09) changeset 45802 b16f976db515 parent 45607 16b4f5774621 child 47108 2a1953f0d20d permissions -rw-r--r--
Quotient_Info stores only relation maps
```     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 =
```
```    13   fixes even :: "'a \<Rightarrow> bool"
```
```    14
```
```    15 abbreviation
```
```    16   odd :: "'a\<Colon>even_odd \<Rightarrow> bool" where
```
```    17   "odd x \<equiv> \<not> even x"
```
```    18
```
```    19 instantiation nat and int  :: even_odd
```
```    20 begin
```
```    21
```
```    22 definition
```
```    23   even_def [presburger]: "even x \<longleftrightarrow> (x\<Colon>int) mod 2 = 0"
```
```    24
```
```    25 definition
```
```    26   even_nat_def [presburger]: "even x \<longleftrightarrow> even (int x)"
```
```    27
```
```    28 instance ..
```
```    29
```
```    30 end
```
```    31
```
```    32 lemma transfer_int_nat_relations:
```
```    33   "even (int x) \<longleftrightarrow> even x"
```
```    34   by (simp add: even_nat_def)
```
```    35
```
```    36 declare transfer_morphism_int_nat[transfer add return:
```
```    37   transfer_int_nat_relations
```
```    38 ]
```
```    39
```
```    40 lemma even_zero_int[simp]: "even (0::int)" by presburger
```
```    41
```
```    42 lemma odd_one_int[simp]: "odd (1::int)" by presburger
```
```    43
```
```    44 lemma even_zero_nat[simp]: "even (0::nat)" by presburger
```
```    45
```
```    46 lemma odd_1_nat [simp]: "odd (1::nat)" by presburger
```
```    47
```
```    48 declare even_def[of "number_of v", simp] for v
```
```    49
```
```    50 declare even_nat_def[of "number_of v", simp] for v
```
```    51
```
```    52 subsection {* Even and odd are mutually exclusive *}
```
```    53
```
```    54 lemma int_pos_lt_two_imp_zero_or_one:
```
```    55     "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
```
```    56   by presburger
```
```    57
```
```    58 lemma neq_one_mod_two [simp, presburger]:
```
```    59   "((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburger
```
```    60
```
```    61
```
```    62 subsection {* Behavior under integer arithmetic operations *}
```
```    63 declare dvd_def[algebra]
```
```    64 lemma nat_even_iff_2_dvd[algebra]: "even (x::nat) \<longleftrightarrow> 2 dvd x"
```
```    65   by presburger
```
```    66 lemma int_even_iff_2_dvd[algebra]: "even (x::int) \<longleftrightarrow> 2 dvd x"
```
```    67   by presburger
```
```    68
```
```    69 lemma even_times_anything: "even (x::int) ==> even (x * y)"
```
```    70   by algebra
```
```    71
```
```    72 lemma anything_times_even: "even (y::int) ==> even (x * y)" by algebra
```
```    73
```
```    74 lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"
```
```    75   by (simp add: even_def zmod_zmult1_eq)
```
```    76
```
```    77 lemma even_product[simp,presburger]: "even((x::int) * y) = (even x | even y)"
```
```    78   apply (auto simp add: even_times_anything anything_times_even)
```
```    79   apply (rule ccontr)
```
```    80   apply (auto simp add: odd_times_odd)
```
```    81   done
```
```    82
```
```    83 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
```
```    84 by presburger
```
```    85
```
```    86 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
```
```    87 by presburger
```
```    88
```
```    89 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
```
```    90 by presburger
```
```    91
```
```    92 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
```
```    93
```
```    94 lemma even_sum[simp,presburger]:
```
```    95   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
```
```    96 by presburger
```
```    97
```
```    98 lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
```
```    99 by presburger
```
```   100
```
```   101 lemma even_difference[simp]:
```
```   102     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
```
```   103
```
```   104 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
```
```   105 by (induct n) auto
```
```   106
```
```   107 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
```
```   108
```
```   109
```
```   110 subsection {* Equivalent definitions *}
```
```   111
```
```   112 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x"
```
```   113 by presburger
```
```   114
```
```   115 lemma two_times_odd_div_two_plus_one:
```
```   116   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
```
```   117 by presburger
```
```   118
```
```   119 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
```
```   120
```
```   121 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
```
```   122
```
```   123 subsection {* even and odd for nats *}
```
```   124
```
```   125 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
```
```   126 by (simp add: even_nat_def)
```
```   127
```
```   128 lemma even_product_nat[simp,presburger,algebra]:
```
```   129   "even((x::nat) * y) = (even x | even y)"
```
```   130 by (simp add: even_nat_def int_mult)
```
```   131
```
```   132 lemma even_sum_nat[simp,presburger,algebra]:
```
```   133   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
```
```   134 by presburger
```
```   135
```
```   136 lemma even_difference_nat[simp,presburger,algebra]:
```
```   137   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
```
```   138 by presburger
```
```   139
```
```   140 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
```
```   141 by presburger
```
```   142
```
```   143 lemma even_power_nat[simp,presburger,algebra]:
```
```   144   "even ((x::nat)^y) = (even x & 0 < y)"
```
```   145 by (simp add: even_nat_def int_power)
```
```   146
```
```   147
```
```   148 subsection {* Equivalent definitions *}
```
```   149
```
```   150 lemma nat_lt_two_imp_zero_or_one:
```
```   151   "(x::nat) < Suc (Suc 0) ==> x = 0 | x = Suc 0"
```
```   152 by presburger
```
```   153
```
```   154 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
```
```   155 by presburger
```
```   156
```
```   157 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
```
```   158 by presburger
```
```   159
```
```   160 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
```
```   161 by presburger
```
```   162
```
```   163 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
```
```   164 by presburger
```
```   165
```
```   166 lemma even_nat_div_two_times_two: "even (x::nat) ==>
```
```   167     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
```
```   168
```
```   169 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
```
```   170     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
```
```   171
```
```   172 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
```
```   173 by presburger
```
```   174
```
```   175 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
```
```   176 by presburger
```
```   177
```
```   178
```
```   179 subsection {* Parity and powers *}
```
```   180
```
```   181 lemma  minus_one_even_odd_power:
```
```   182      "(even x --> (- 1::'a::{comm_ring_1})^x = 1) &
```
```   183       (odd x --> (- 1::'a)^x = - 1)"
```
```   184   apply (induct x)
```
```   185   apply (rule conjI)
```
```   186   apply simp
```
```   187   apply (insert even_zero_nat, blast)
```
```   188   apply simp
```
```   189   done
```
```   190
```
```   191 lemma minus_one_even_power [simp]:
```
```   192     "even x ==> (- 1::'a::{comm_ring_1})^x = 1"
```
```   193   using minus_one_even_odd_power by blast
```
```   194
```
```   195 lemma minus_one_odd_power [simp]:
```
```   196     "odd x ==> (- 1::'a::{comm_ring_1})^x = - 1"
```
```   197   using minus_one_even_odd_power by blast
```
```   198
```
```   199 lemma neg_one_even_odd_power:
```
```   200      "(even x --> (-1::'a::{number_ring})^x = 1) &
```
```   201       (odd x --> (-1::'a)^x = -1)"
```
```   202   apply (induct x)
```
```   203   apply (simp, simp)
```
```   204   done
```
```   205
```
```   206 lemma neg_one_even_power [simp]:
```
```   207     "even x ==> (-1::'a::{number_ring})^x = 1"
```
```   208   using neg_one_even_odd_power by blast
```
```   209
```
```   210 lemma neg_one_odd_power [simp]:
```
```   211     "odd x ==> (-1::'a::{number_ring})^x = -1"
```
```   212   using neg_one_even_odd_power by blast
```
```   213
```
```   214 lemma neg_power_if:
```
```   215      "(-x::'a::{comm_ring_1}) ^ n =
```
```   216       (if even n then (x ^ n) else -(x ^ n))"
```
```   217   apply (induct n)
```
```   218   apply simp_all
```
```   219   done
```
```   220
```
```   221 lemma zero_le_even_power: "even n ==>
```
```   222     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
```
```   223   apply (simp add: even_nat_equiv_def2)
```
```   224   apply (erule exE)
```
```   225   apply (erule ssubst)
```
```   226   apply (subst power_add)
```
```   227   apply (rule zero_le_square)
```
```   228   done
```
```   229
```
```   230 lemma zero_le_odd_power: "odd n ==>
```
```   231     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
```
```   232 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
```
```   233 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
```
```   234 done
```
```   235
```
```   236 lemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
```
```   237     (even n | (odd n & 0 <= x))"
```
```   238   apply auto
```
```   239   apply (subst zero_le_odd_power [symmetric])
```
```   240   apply assumption+
```
```   241   apply (erule zero_le_even_power)
```
```   242   done
```
```   243
```
```   244 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
```
```   245     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
```
```   246
```
```   247   unfolding order_less_le zero_le_power_eq by auto
```
```   248
```
```   249 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
```
```   250     (odd n & x < 0)"
```
```   251   apply (subst linorder_not_le [symmetric])+
```
```   252   apply (subst zero_le_power_eq)
```
```   253   apply auto
```
```   254   done
```
```   255
```
```   256 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
```
```   257     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
```
```   258   apply (subst linorder_not_less [symmetric])+
```
```   259   apply (subst zero_less_power_eq)
```
```   260   apply auto
```
```   261   done
```
```   262
```
```   263 lemma power_even_abs: "even n ==>
```
```   264     (abs (x::'a::{linordered_idom}))^n = x^n"
```
```   265   apply (subst power_abs [symmetric])
```
```   266   apply (simp add: zero_le_even_power)
```
```   267   done
```
```   268
```
```   269 lemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"
```
```   270   by (induct n) auto
```
```   271
```
```   272 lemma power_minus_even [simp]: "even n ==>
```
```   273     (- x)^n = (x^n::'a::{comm_ring_1})"
```
```   274   apply (subst power_minus)
```
```   275   apply simp
```
```   276   done
```
```   277
```
```   278 lemma power_minus_odd [simp]: "odd n ==>
```
```   279     (- x)^n = - (x^n::'a::{comm_ring_1})"
```
```   280   apply (subst power_minus)
```
```   281   apply simp
```
```   282   done
```
```   283
```
```   284 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
```
```   285   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
```
```   286   shows "x^n \<le> y^n"
```
```   287 proof -
```
```   288   have "0 \<le> \<bar>x\<bar>" by auto
```
```   289   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
```
```   290   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
```
```   291   thus ?thesis unfolding power_even_abs[OF `even n`] .
```
```   292 qed
```
```   293
```
```   294 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
```
```   295
```
```   296 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
```
```   297   assumes "odd n" and "x \<le> y"
```
```   298   shows "x^n \<le> y^n"
```
```   299 proof (cases "y < 0")
```
```   300   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
```
```   301   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
```
```   302   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
```
```   303 next
```
```   304   case False
```
```   305   show ?thesis
```
```   306   proof (cases "x < 0")
```
```   307     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
```
```   308     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
```
```   309     moreover
```
```   310     from `\<not> y < 0` have "0 \<le> y" by auto
```
```   311     hence "0 \<le> y^n" by auto
```
```   312     ultimately show ?thesis by auto
```
```   313   next
```
```   314     case False hence "0 \<le> x" by auto
```
```   315     with `x \<le> y` show ?thesis using power_mono by auto
```
```   316   qed
```
```   317 qed
```
```   318
```
```   319
```
```   320 subsection {* More Even/Odd Results *}
```
```   321
```
```   322 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
```
```   323 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
```
```   324 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
```
```   325
```
```   326 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
```
```   327
```
```   328 lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
```
```   329     (a mod c + Suc 0 mod c) div c"
```
```   330   apply (subgoal_tac "Suc a = a + Suc 0")
```
```   331   apply (erule ssubst)
```
```   332   apply (rule div_add1_eq, simp)
```
```   333   done
```
```   334
```
```   335 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
```
```   336
```
```   337 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
```
```   338 by presburger
```
```   339
```
```   340 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
```
```   341 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
```
```   342
```
```   343 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
```
```   344
```
```   345 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
```
```   346   by presburger
```
```   347
```
```   348 text {* Simplify, when the exponent is a numeral *}
```
```   349
```
```   350 lemmas power_0_left_number_of = power_0_left [of "number_of w"] for w
```
```   351 declare power_0_left_number_of [simp]
```
```   352
```
```   353 lemmas zero_le_power_eq_number_of [simp] =
```
```   354     zero_le_power_eq [of _ "number_of w"] for w
```
```   355
```
```   356 lemmas zero_less_power_eq_number_of [simp] =
```
```   357     zero_less_power_eq [of _ "number_of w"] for w
```
```   358
```
```   359 lemmas power_le_zero_eq_number_of [simp] =
```
```   360     power_le_zero_eq [of _ "number_of w"] for w
```
```   361
```
```   362 lemmas power_less_zero_eq_number_of [simp] =
```
```   363     power_less_zero_eq [of _ "number_of w"] for w
```
```   364
```
```   365 lemmas zero_less_power_nat_eq_number_of [simp] =
```
```   366     zero_less_power_nat_eq [of _ "number_of w"] for w
```
```   367
```
```   368 lemmas power_eq_0_iff_number_of [simp] = power_eq_0_iff [of _ "number_of w"] for w
```
```   369
```
```   370 lemmas power_even_abs_number_of [simp] = power_even_abs [of "number_of w" _] for w
```
```   371
```
```   372
```
```   373 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
```
```   374
```
```   375 lemma even_power_le_0_imp_0:
```
```   376     "a ^ (2*k) \<le> (0::'a::{linordered_idom}) ==> a=0"
```
```   377   by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
```
```   378
```
```   379 lemma zero_le_power_iff[presburger]:
```
```   380   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
```
```   381 proof cases
```
```   382   assume even: "even n"
```
```   383   then obtain k where "n = 2*k"
```
```   384     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
```
```   385   thus ?thesis by (simp add: zero_le_even_power even)
```
```   386 next
```
```   387   assume odd: "odd n"
```
```   388   then obtain k where "n = Suc(2*k)"
```
```   389     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
```
```   390   thus ?thesis
```
```   391     by (auto simp add: zero_le_mult_iff zero_le_even_power
```
```   392              dest!: even_power_le_0_imp_0)
```
```   393 qed
```
```   394
```
```   395
```
```   396 subsection {* Miscellaneous *}
```
```   397
```
```   398 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
```
```   399 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
```
```   400 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
```
```   401 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
```
```   402
```
```   403 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
```
```   404 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
```
```   405 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
```
```   406     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
```
```   407
```
```   408 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
```
```   409     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
```
```   410
```
```   411 end
```