# Theory Parity

Up to index of Isabelle/HOL

theory Parity
imports Main
`(*  Title:      HOL/Parity.thy    Author:     Jeremy Avigad    Author:     Jacques D. Fleuriot*)header {* Even and Odd for int and nat *}theory Parityimports Mainbeginclass even_odd =   fixes even :: "'a => bool"abbreviation  odd :: "'a::even_odd => bool" where  "odd x ≡ ¬ even x"instantiation nat and int  :: even_oddbegindefinition  even_def [presburger]: "even x <-> (x::int) mod 2 = 0"definition  even_nat_def [presburger]: "even x <-> even (int x)"instance ..endlemma transfer_int_nat_relations:  "even (int x) <-> even x"  by (simp add: even_nat_def)declare transfer_morphism_int_nat[transfer add return:  transfer_int_nat_relations]lemma even_zero_int[simp]: "even (0::int)" by presburgerlemma odd_one_int[simp]: "odd (1::int)" by presburgerlemma even_zero_nat[simp]: "even (0::nat)" by presburgerlemma odd_1_nat [simp]: "odd (1::nat)" by presburgerlemma even_numeral_int [simp]: "even (numeral (Num.Bit0 k) :: int)"  unfolding even_def by simplemma odd_numeral_int [simp]: "odd (numeral (Num.Bit1 k) :: int)"  unfolding even_def by simp(* TODO: proper simp rules for Num.Bit0, Num.Bit1 *)declare even_def[of "neg_numeral v", simp] for vlemma even_numeral_nat [simp]: "even (numeral (Num.Bit0 k) :: nat)"  unfolding even_nat_def by simplemma odd_numeral_nat [simp]: "odd (numeral (Num.Bit1 k) :: nat)"  unfolding even_nat_def by simpsubsection {* Even and odd are mutually exclusive *}lemma int_pos_lt_two_imp_zero_or_one:    "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"  by presburgerlemma neq_one_mod_two [simp, presburger]:   "((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburgersubsection {* Behavior under integer arithmetic operations *}declare dvd_def[algebra]lemma nat_even_iff_2_dvd[algebra]: "even (x::nat) <-> 2 dvd x"  by presburgerlemma int_even_iff_2_dvd[algebra]: "even (x::int) <-> 2 dvd x"  by presburgerlemma even_times_anything: "even (x::int) ==> even (x * y)"  by algebralemma anything_times_even: "even (y::int) ==> even (x * y)" by algebralemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"   by (simp add: even_def mod_mult_right_eq)lemma even_product[simp,presburger]: "even((x::int) * y) = (even x | even y)"  apply (auto simp add: even_times_anything anything_times_even)  apply (rule ccontr)  apply (auto simp add: odd_times_odd)  donelemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"by presburgerlemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"by presburgerlemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"by presburgerlemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburgerlemma even_sum[simp,presburger]:  "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"by presburgerlemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"by presburgerlemma even_difference[simp]:    "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburgerlemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n ≠ 0)"by (induct n) autolemma odd_pow: "odd x ==> odd((x::int)^n)" by simpsubsection {* Equivalent definitions *}lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" by presburgerlemma two_times_odd_div_two_plus_one:  "odd (x::int) ==> 2 * (x div 2) + 1 = x"by presburgerlemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburgerlemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburgersubsection {* even and odd for nats *}lemma pos_int_even_equiv_nat_even: "0 ≤ x ==> even x = even (nat x)"by (simp add: even_nat_def)lemma even_product_nat[simp,presburger,algebra]:  "even((x::nat) * y) = (even x | even y)"by (simp add: even_nat_def int_mult)lemma even_sum_nat[simp,presburger,algebra]:  "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"by presburgerlemma even_difference_nat[simp,presburger,algebra]:  "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"by presburgerlemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"by presburgerlemma even_power_nat[simp,presburger,algebra]:  "even ((x::nat)^y) = (even x & 0 < y)"by (simp add: even_nat_def int_power)subsection {* Equivalent definitions *}lemma nat_lt_two_imp_zero_or_one:  "(x::nat) < Suc (Suc 0) ==> x = 0 | x = Suc 0"by presburgerlemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"by presburgerlemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"by presburgerlemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"by presburgerlemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"by presburgerlemma even_nat_div_two_times_two: "even (x::nat) ==>    Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburgerlemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>    Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburgerlemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"by presburgerlemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"by presburgersubsection {* Parity and powers *}lemma  minus_one_even_odd_power:     "(even x --> (- 1::'a::{comm_ring_1})^x = 1) &      (odd x --> (- 1::'a)^x = - 1)"  apply (induct x)  apply (rule conjI)  apply simp  apply (insert even_zero_nat, blast)  apply simp  donelemma minus_one_even_power [simp]:    "even x ==> (- 1::'a::{comm_ring_1})^x = 1"  using minus_one_even_odd_power by blastlemma minus_one_odd_power [simp]:    "odd x ==> (- 1::'a::{comm_ring_1})^x = - 1"  using minus_one_even_odd_power by blastlemma neg_one_even_odd_power:     "(even x --> (-1::'a::{comm_ring_1})^x = 1) &      (odd x --> (-1::'a)^x = -1)"  apply (induct x)  apply (simp, simp)  donelemma neg_one_even_power [simp]:    "even x ==> (-1::'a::{comm_ring_1})^x = 1"  using neg_one_even_odd_power by blastlemma neg_one_odd_power [simp]:    "odd x ==> (-1::'a::{comm_ring_1})^x = -1"  using neg_one_even_odd_power by blastlemma neg_power_if:     "(-x::'a::{comm_ring_1}) ^ n =      (if even n then (x ^ n) else -(x ^ n))"  apply (induct n)  apply simp_all  donelemma zero_le_even_power: "even n ==>    0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"  apply (simp add: even_nat_equiv_def2)  apply (erule exE)  apply (erule ssubst)  apply (subst power_add)  apply (rule zero_le_square)  donelemma zero_le_odd_power: "odd n ==>    (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)donelemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =    (even n | (odd n & 0 <= x))"  apply auto  apply (subst zero_le_odd_power [symmetric])  apply assumption+  apply (erule zero_le_even_power)  donelemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =    (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"  unfolding order_less_le zero_le_power_eq by autolemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =    (odd n & x < 0)"  apply (subst linorder_not_le [symmetric])+  apply (subst zero_le_power_eq)  apply auto  donelemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =    (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"  apply (subst linorder_not_less [symmetric])+  apply (subst zero_less_power_eq)  apply auto  donelemma power_even_abs: "even n ==>    (abs (x::'a::{linordered_idom}))^n = x^n"  apply (subst power_abs [symmetric])  apply (simp add: zero_le_even_power)  donelemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"  by (induct n) autolemma power_minus_even [simp]: "even n ==>    (- x)^n = (x^n::'a::{comm_ring_1})"  apply (subst power_minus)  apply simp  donelemma power_minus_odd [simp]: "odd n ==>    (- x)^n = - (x^n::'a::{comm_ring_1})"  apply (subst power_minus)  apply simp  donelemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"  assumes "even n" and "¦x¦ ≤ ¦y¦"  shows "x^n ≤ y^n"proof -  have "0 ≤ ¦x¦" by auto  with `¦x¦ ≤ ¦y¦`  have "¦x¦^n ≤ ¦y¦^n" by (rule power_mono)  thus ?thesis unfolding power_even_abs[OF `even n`] .qedlemma odd_pos: "odd (n::nat) ==> 0 < n" by presburgerlemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"  assumes "odd n" and "x ≤ y"  shows "x^n ≤ y^n"proof (cases "y < 0")  case True with `x ≤ y` have "-y ≤ -x" and "0 ≤ -y" by auto  hence "(-y)^n ≤ (-x)^n" by (rule power_mono)  thus ?thesis unfolding power_minus_odd[OF `odd n`] by autonext  case False  show ?thesis  proof (cases "x < 0")    case True hence "n ≠ 0" and "x ≤ 0" using `odd n`[THEN odd_pos] by auto    hence "x^n ≤ 0" unfolding power_le_zero_eq using `odd n` by auto    moreover    from `¬ y < 0` have "0 ≤ y" by auto    hence "0 ≤ y^n" by auto    ultimately show ?thesis by auto  next    case False hence "0 ≤ x" by auto    with `x ≤ y` show ?thesis using power_mono by auto  qedqedsubsection {* More Even/Odd Results *} lemma even_mult_two_ex: "even(n) = (∃m::nat. n = 2*m)" by presburgerlemma odd_Suc_mult_two_ex: "odd(n) = (∃m. n = Suc (2*m))" by presburgerlemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburgerlemma odd_add [simp]: "odd(m + n::nat) = (odd m ≠ odd n)" by presburgerlemma div_Suc: "Suc a div c = a div c + Suc 0 div c +    (a mod c + Suc 0 mod c) div c"   apply (subgoal_tac "Suc a = a + Suc 0")  apply (erule ssubst)  apply (rule div_add1_eq, simp)  donelemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburgerlemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"by presburgerlemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburgerlemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburgerlemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburgerlemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"  by presburgertext {* Simplify, when the exponent is a numeral *}lemmas zero_le_power_eq_numeral [simp] =    zero_le_power_eq [of _ "numeral w"] for wlemmas zero_less_power_eq_numeral [simp] =    zero_less_power_eq [of _ "numeral w"] for wlemmas power_le_zero_eq_numeral [simp] =    power_le_zero_eq [of _ "numeral w"] for wlemmas power_less_zero_eq_numeral [simp] =    power_less_zero_eq [of _ "numeral w"] for wlemmas zero_less_power_nat_eq_numeral [simp] =    zero_less_power_nat_eq [of _ "numeral w"] for wlemmas power_eq_0_iff_numeral [simp] = power_eq_0_iff [of _ "numeral w"] for wlemmas power_even_abs_numeral [simp] = power_even_abs [of "numeral w" _] for wsubsection {* An Equivalence for @{term [source] "0 ≤ a^n"} *}lemma even_power_le_0_imp_0:    "a ^ (2*k) ≤ (0::'a::{linordered_idom}) ==> a=0"  by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)lemma zero_le_power_iff[presburger]:  "(0 ≤ a^n) = (0 ≤ (a::'a::{linordered_idom}) | even n)"proof cases  assume even: "even n"  then obtain k where "n = 2*k"    by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)  thus ?thesis by (simp add: zero_le_even_power even)next  assume odd: "odd n"  then obtain k where "n = Suc(2*k)"    by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)  thus ?thesis    by (auto simp add: zero_le_mult_iff zero_le_even_power             dest!: even_power_le_0_imp_0)qedsubsection {* Miscellaneous *}lemma [presburger]:"(x + 1) div 2 = x div 2 <-> even (x::int)" by presburgerlemma [presburger]: "(x + 1) div 2 = x div 2 + 1 <-> odd (x::int)" by presburgerlemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburgerlemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburgerlemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) <-> even x" by presburgerlemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) <-> even x" by presburgerlemma even_nat_plus_one_div_two: "even (x::nat) ==>    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburgerlemma odd_nat_plus_one_div_two: "odd (x::nat) ==>    (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburgerend`