src/HOL/Integ/Parity.thy
 author wenzelm Sun Apr 09 18:51:13 2006 +0200 (2006-04-09) changeset 19380 b808efaa5828 parent 18648 22f96cd085d5 permissions -rw-r--r--
tuned syntax/abbreviations;
```     1 (*  Title:      Parity.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Jeremy Avigad
```
```     4 *)
```
```     5
```
```     6 header {* Even and Odd for ints and nats*}
```
```     7
```
```     8 theory Parity
```
```     9 imports Divides IntDiv NatSimprocs
```
```    10 begin
```
```    11
```
```    12 axclass even_odd < type
```
```    13
```
```    14 consts
```
```    15   even :: "'a::even_odd => bool"
```
```    16
```
```    17 instance int :: even_odd ..
```
```    18 instance nat :: even_odd ..
```
```    19
```
```    20 defs (overloaded)
```
```    21   even_def: "even (x::int) == x mod 2 = 0"
```
```    22   even_nat_def: "even (x::nat) == even (int x)"
```
```    23
```
```    24 abbreviation
```
```    25   odd :: "'a::even_odd => bool"
```
```    26   "odd x == \<not> even x"
```
```    27
```
```    28
```
```    29 subsection {* Even and odd are mutually exclusive *}
```
```    30
```
```    31 lemma int_pos_lt_two_imp_zero_or_one:
```
```    32     "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
```
```    33   by auto
```
```    34
```
```    35 lemma neq_one_mod_two [simp]: "((x::int) mod 2 ~= 0) = (x mod 2 = 1)"
```
```    36   apply (subgoal_tac "x mod 2 = 0 | x mod 2 = 1", force)
```
```    37   apply (rule int_pos_lt_two_imp_zero_or_one, auto)
```
```    38   done
```
```    39
```
```    40 subsection {* Behavior under integer arithmetic operations *}
```
```    41
```
```    42 lemma even_times_anything: "even (x::int) ==> even (x * y)"
```
```    43   by (simp add: even_def zmod_zmult1_eq')
```
```    44
```
```    45 lemma anything_times_even: "even (y::int) ==> even (x * y)"
```
```    46   by (simp add: even_def zmod_zmult1_eq)
```
```    47
```
```    48 lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"
```
```    49   by (simp add: even_def zmod_zmult1_eq)
```
```    50
```
```    51 lemma even_product: "even((x::int) * y) = (even x | even y)"
```
```    52   apply (auto simp add: even_times_anything anything_times_even)
```
```    53   apply (rule ccontr)
```
```    54   apply (auto simp add: odd_times_odd)
```
```    55   done
```
```    56
```
```    57 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
```
```    58   by (simp add: even_def zmod_zadd1_eq)
```
```    59
```
```    60 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
```
```    61   by (simp add: even_def zmod_zadd1_eq)
```
```    62
```
```    63 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
```
```    64   by (simp add: even_def zmod_zadd1_eq)
```
```    65
```
```    66 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)"
```
```    67   by (simp add: even_def zmod_zadd1_eq)
```
```    68
```
```    69 lemma even_sum: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
```
```    70   apply (auto intro: even_plus_even odd_plus_odd)
```
```    71   apply (rule ccontr, simp add: even_plus_odd)
```
```    72   apply (rule ccontr, simp add: odd_plus_even)
```
```    73   done
```
```    74
```
```    75 lemma even_neg: "even (-(x::int)) = even x"
```
```    76   by (auto simp add: even_def zmod_zminus1_eq_if)
```
```    77
```
```    78 lemma even_difference:
```
```    79   "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))"
```
```    80   by (simp only: diff_minus even_sum even_neg)
```
```    81
```
```    82 lemma even_pow_gt_zero [rule_format]:
```
```    83     "even (x::int) ==> 0 < n --> even (x^n)"
```
```    84   apply (induct n)
```
```    85   apply (auto simp add: even_product)
```
```    86   done
```
```    87
```
```    88 lemma odd_pow: "odd x ==> odd((x::int)^n)"
```
```    89   apply (induct n)
```
```    90   apply (simp add: even_def)
```
```    91   apply (simp add: even_product)
```
```    92   done
```
```    93
```
```    94 lemma even_power: "even ((x::int)^n) = (even x & 0 < n)"
```
```    95   apply (auto simp add: even_pow_gt_zero)
```
```    96   apply (erule contrapos_pp, erule odd_pow)
```
```    97   apply (erule contrapos_pp, simp add: even_def)
```
```    98   done
```
```    99
```
```   100 lemma even_zero: "even (0::int)"
```
```   101   by (simp add: even_def)
```
```   102
```
```   103 lemma odd_one: "odd (1::int)"
```
```   104   by (simp add: even_def)
```
```   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 (auto simp add: even_def)
```
```   114
```
```   115 lemma two_times_odd_div_two_plus_one: "odd (x::int) ==>
```
```   116     2 * (x div 2) + 1 = x"
```
```   117   apply (insert zmod_zdiv_equality [of x 2, THEN sym])
```
```   118   by (simp add: even_def)
```
```   119
```
```   120 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)"
```
```   121   apply auto
```
```   122   apply (rule exI)
```
```   123   by (erule two_times_even_div_two [THEN sym])
```
```   124
```
```   125 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)"
```
```   126   apply auto
```
```   127   apply (rule exI)
```
```   128   by (erule two_times_odd_div_two_plus_one [THEN sym])
```
```   129
```
```   130
```
```   131 subsection {* even and odd for nats *}
```
```   132
```
```   133 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
```
```   134   by (simp add: even_nat_def)
```
```   135
```
```   136 lemma even_nat_product: "even((x::nat) * y) = (even x | even y)"
```
```   137   by (simp add: even_nat_def int_mult)
```
```   138
```
```   139 lemma even_nat_sum: "even ((x::nat) + y) =
```
```   140     ((even x & even y) | (odd x & odd y))"
```
```   141   by (unfold even_nat_def, simp)
```
```   142
```
```   143 lemma even_nat_difference:
```
```   144     "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
```
```   145   apply (auto simp add: even_nat_def zdiff_int [THEN sym])
```
```   146   apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
```
```   147   apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
```
```   148   done
```
```   149
```
```   150 lemma even_nat_Suc: "even (Suc x) = odd x"
```
```   151   by (simp add: even_nat_def)
```
```   152
```
```   153 lemma even_nat_power: "even ((x::nat)^y) = (even x & 0 < y)"
```
```   154   by (simp add: even_nat_def int_power)
```
```   155
```
```   156 lemma even_nat_zero: "even (0::nat)"
```
```   157   by (simp add: even_nat_def)
```
```   158
```
```   159 lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard]
```
```   160   even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
```
```   161
```
```   162
```
```   163 subsection {* Equivalent definitions *}
```
```   164
```
```   165 lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==>
```
```   166     x = 0 | x = Suc 0"
```
```   167   by auto
```
```   168
```
```   169 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
```
```   170   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
```
```   171   apply (drule subst, assumption)
```
```   172   apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
```
```   173   apply force
```
```   174   apply (subgoal_tac "0 < Suc (Suc 0)")
```
```   175   apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
```
```   176   apply (erule nat_lt_two_imp_zero_or_one, auto)
```
```   177   done
```
```   178
```
```   179 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
```
```   180   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
```
```   181   apply (drule subst, assumption)
```
```   182   apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
```
```   183   apply force
```
```   184   apply (subgoal_tac "0 < Suc (Suc 0)")
```
```   185   apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
```
```   186   apply (erule nat_lt_two_imp_zero_or_one, auto)
```
```   187   done
```
```   188
```
```   189 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
```
```   190   apply (rule iffI)
```
```   191   apply (erule even_nat_mod_two_eq_zero)
```
```   192   apply (insert odd_nat_mod_two_eq_one [of x], auto)
```
```   193   done
```
```   194
```
```   195 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
```
```   196   apply (auto simp add: even_nat_equiv_def)
```
```   197   apply (subgoal_tac "x mod (Suc (Suc 0)) < Suc (Suc 0)")
```
```   198   apply (frule nat_lt_two_imp_zero_or_one, auto)
```
```   199   done
```
```   200
```
```   201 lemma even_nat_div_two_times_two: "even (x::nat) ==>
```
```   202     Suc (Suc 0) * (x div Suc (Suc 0)) = x"
```
```   203   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
```
```   204   apply (drule even_nat_mod_two_eq_zero, simp)
```
```   205   done
```
```   206
```
```   207 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
```
```   208     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x"
```
```   209   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
```
```   210   apply (drule odd_nat_mod_two_eq_one, simp)
```
```   211   done
```
```   212
```
```   213 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
```
```   214   apply (rule iffI, rule exI)
```
```   215   apply (erule even_nat_div_two_times_two [THEN sym], auto)
```
```   216   done
```
```   217
```
```   218 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
```
```   219   apply (rule iffI, rule exI)
```
```   220   apply (erule odd_nat_div_two_times_two_plus_one [THEN sym], auto)
```
```   221   done
```
```   222
```
```   223 subsection {* Parity and powers *}
```
```   224
```
```   225 lemma minus_one_even_odd_power:
```
```   226      "(even x --> (- 1::'a::{comm_ring_1,recpower})^x = 1) &
```
```   227       (odd x --> (- 1::'a)^x = - 1)"
```
```   228   apply (induct x)
```
```   229   apply (rule conjI)
```
```   230   apply simp
```
```   231   apply (insert even_nat_zero, blast)
```
```   232   apply (simp add: power_Suc)
```
```   233 done
```
```   234
```
```   235 lemma minus_one_even_power [simp]:
```
```   236      "even x ==> (- 1::'a::{comm_ring_1,recpower})^x = 1"
```
```   237   by (rule minus_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
```
```   238
```
```   239 lemma minus_one_odd_power [simp]:
```
```   240      "odd x ==> (- 1::'a::{comm_ring_1,recpower})^x = - 1"
```
```   241   by (rule minus_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
```
```   242
```
```   243 lemma neg_one_even_odd_power:
```
```   244      "(even x --> (-1::'a::{number_ring,recpower})^x = 1) &
```
```   245       (odd x --> (-1::'a)^x = -1)"
```
```   246   apply (induct x)
```
```   247   apply (simp, simp add: power_Suc)
```
```   248   done
```
```   249
```
```   250 lemma neg_one_even_power [simp]:
```
```   251      "even x ==> (-1::'a::{number_ring,recpower})^x = 1"
```
```   252   by (rule neg_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
```
```   253
```
```   254 lemma neg_one_odd_power [simp]:
```
```   255      "odd x ==> (-1::'a::{number_ring,recpower})^x = -1"
```
```   256   by (rule neg_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
```
```   257
```
```   258 lemma neg_power_if:
```
```   259      "(-x::'a::{comm_ring_1,recpower}) ^ n =
```
```   260       (if even n then (x ^ n) else -(x ^ n))"
```
```   261   by (induct n, simp_all split: split_if_asm add: power_Suc)
```
```   262
```
```   263 lemma zero_le_even_power: "even n ==>
```
```   264     0 <= (x::'a::{recpower,ordered_ring_strict}) ^ n"
```
```   265   apply (simp add: even_nat_equiv_def2)
```
```   266   apply (erule exE)
```
```   267   apply (erule ssubst)
```
```   268   apply (subst power_add)
```
```   269   apply (rule zero_le_square)
```
```   270   done
```
```   271
```
```   272 lemma zero_le_odd_power: "odd n ==>
```
```   273     (0 <= (x::'a::{recpower,ordered_idom}) ^ n) = (0 <= x)"
```
```   274   apply (simp add: odd_nat_equiv_def2)
```
```   275   apply (erule exE)
```
```   276   apply (erule ssubst)
```
```   277   apply (subst power_Suc)
```
```   278   apply (subst power_add)
```
```   279   apply (subst zero_le_mult_iff)
```
```   280   apply auto
```
```   281   apply (subgoal_tac "x = 0 & 0 < y")
```
```   282   apply (erule conjE, assumption)
```
```   283   apply (subst power_eq_0_iff [THEN sym])
```
```   284   apply (subgoal_tac "0 <= x^y * x^y")
```
```   285   apply simp
```
```   286   apply (rule zero_le_square)+
```
```   287 done
```
```   288
```
```   289 lemma zero_le_power_eq: "(0 <= (x::'a::{recpower,ordered_idom}) ^ n) =
```
```   290     (even n | (odd n & 0 <= x))"
```
```   291   apply auto
```
```   292   apply (subst zero_le_odd_power [THEN sym])
```
```   293   apply assumption+
```
```   294   apply (erule zero_le_even_power)
```
```   295   apply (subst zero_le_odd_power)
```
```   296   apply assumption+
```
```   297 done
```
```   298
```
```   299 lemma zero_less_power_eq: "(0 < (x::'a::{recpower,ordered_idom}) ^ n) =
```
```   300     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
```
```   301   apply (rule iffI)
```
```   302   apply clarsimp
```
```   303   apply (rule conjI)
```
```   304   apply clarsimp
```
```   305   apply (rule ccontr)
```
```   306   apply (subgoal_tac "~ (0 <= x^n)")
```
```   307   apply simp
```
```   308   apply (subst zero_le_odd_power)
```
```   309   apply assumption
```
```   310   apply simp
```
```   311   apply (rule notI)
```
```   312   apply (simp add: power_0_left)
```
```   313   apply (rule notI)
```
```   314   apply (simp add: power_0_left)
```
```   315   apply auto
```
```   316   apply (subgoal_tac "0 <= x^n")
```
```   317   apply (frule order_le_imp_less_or_eq)
```
```   318   apply simp
```
```   319   apply (erule zero_le_even_power)
```
```   320   apply (subgoal_tac "0 <= x^n")
```
```   321   apply (frule order_le_imp_less_or_eq)
```
```   322   apply auto
```
```   323   apply (subst zero_le_odd_power)
```
```   324   apply assumption
```
```   325   apply (erule order_less_imp_le)
```
```   326 done
```
```   327
```
```   328 lemma power_less_zero_eq: "((x::'a::{recpower,ordered_idom}) ^ n < 0) =
```
```   329     (odd n & x < 0)"
```
```   330   apply (subst linorder_not_le [THEN sym])+
```
```   331   apply (subst zero_le_power_eq)
```
```   332   apply auto
```
```   333 done
```
```   334
```
```   335 lemma power_le_zero_eq: "((x::'a::{recpower,ordered_idom}) ^ n <= 0) =
```
```   336     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
```
```   337   apply (subst linorder_not_less [THEN sym])+
```
```   338   apply (subst zero_less_power_eq)
```
```   339   apply auto
```
```   340 done
```
```   341
```
```   342 lemma power_even_abs: "even n ==>
```
```   343     (abs (x::'a::{recpower,ordered_idom}))^n = x^n"
```
```   344   apply (subst power_abs [THEN sym])
```
```   345   apply (simp add: zero_le_even_power)
```
```   346 done
```
```   347
```
```   348 lemma zero_less_power_nat_eq: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"
```
```   349   by (induct n, auto)
```
```   350
```
```   351 lemma power_minus_even [simp]: "even n ==>
```
```   352     (- x)^n = (x^n::'a::{recpower,comm_ring_1})"
```
```   353   apply (subst power_minus)
```
```   354   apply simp
```
```   355 done
```
```   356
```
```   357 lemma power_minus_odd [simp]: "odd n ==>
```
```   358     (- x)^n = - (x^n::'a::{recpower,comm_ring_1})"
```
```   359   apply (subst power_minus)
```
```   360   apply simp
```
```   361 done
```
```   362
```
```   363 (* Simplify, when the exponent is a numeral *)
```
```   364
```
```   365 lemmas power_0_left_number_of = power_0_left [of "number_of w", standard]
```
```   366 declare power_0_left_number_of [simp]
```
```   367
```
```   368 lemmas zero_le_power_eq_number_of =
```
```   369     zero_le_power_eq [of _ "number_of w", standard]
```
```   370 declare zero_le_power_eq_number_of [simp]
```
```   371
```
```   372 lemmas zero_less_power_eq_number_of =
```
```   373     zero_less_power_eq [of _ "number_of w", standard]
```
```   374 declare zero_less_power_eq_number_of [simp]
```
```   375
```
```   376 lemmas power_le_zero_eq_number_of =
```
```   377     power_le_zero_eq [of _ "number_of w", standard]
```
```   378 declare power_le_zero_eq_number_of [simp]
```
```   379
```
```   380 lemmas power_less_zero_eq_number_of =
```
```   381     power_less_zero_eq [of _ "number_of w", standard]
```
```   382 declare power_less_zero_eq_number_of [simp]
```
```   383
```
```   384 lemmas zero_less_power_nat_eq_number_of =
```
```   385     zero_less_power_nat_eq [of _ "number_of w", standard]
```
```   386 declare zero_less_power_nat_eq_number_of [simp]
```
```   387
```
```   388 lemmas power_eq_0_iff_number_of = power_eq_0_iff [of _ "number_of w", standard]
```
```   389 declare power_eq_0_iff_number_of [simp]
```
```   390
```
```   391 lemmas power_even_abs_number_of = power_even_abs [of "number_of w" _, standard]
```
```   392 declare power_even_abs_number_of [simp]
```
```   393
```
```   394
```
```   395 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
```
```   396
```
```   397 lemma even_power_le_0_imp_0:
```
```   398      "a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0"
```
```   399 apply (induct k)
```
```   400 apply (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)
```
```   401 done
```
```   402
```
```   403 lemma zero_le_power_iff:
```
```   404      "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)"
```
```   405       (is "?P n")
```
```   406 proof cases
```
```   407   assume even: "even n"
```
```   408   then obtain k where "n = 2*k"
```
```   409     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
```
```   410   thus ?thesis by (simp add: zero_le_even_power even)
```
```   411 next
```
```   412   assume odd: "odd n"
```
```   413   then obtain k where "n = Suc(2*k)"
```
```   414     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
```
```   415   thus ?thesis
```
```   416     by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power
```
```   417              dest!: even_power_le_0_imp_0)
```
```   418 qed
```
```   419
```
```   420 subsection {* Miscellaneous *}
```
```   421
```
```   422 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"
```
```   423   apply (subst zdiv_zadd1_eq)
```
```   424   apply (simp add: even_def)
```
```   425   done
```
```   426
```
```   427 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1"
```
```   428   apply (subst zdiv_zadd1_eq)
```
```   429   apply (simp add: even_def)
```
```   430   done
```
```   431
```
```   432 lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
```
```   433     (a mod c + Suc 0 mod c) div c"
```
```   434   apply (subgoal_tac "Suc a = a + Suc 0")
```
```   435   apply (erule ssubst)
```
```   436   apply (rule div_add1_eq, simp)
```
```   437   done
```
```   438
```
```   439 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
```
```   440    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)"
```
```   441   apply (subst div_Suc)
```
```   442   apply (simp add: even_nat_equiv_def)
```
```   443   done
```
```   444
```
```   445 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
```
```   446     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))"
```
```   447   apply (subst div_Suc)
```
```   448   apply (simp add: odd_nat_equiv_def)
```
```   449   done
```
```   450
```
```   451 end
```