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