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