# Theory Parity

```(*  Title:      HOL/Parity.thy
Author:     Jacques D. Fleuriot
*)

section ‹Parity in rings and semirings›

theory Parity
imports Euclidean_Division
begin

subsection ‹Ring structures with parity and ‹even›/‹odd› predicates›

class semiring_parity = comm_semiring_1 + semiring_modulo +
assumes even_iff_mod_2_eq_zero: "2 dvd a ⟷ a mod 2 = 0"
and odd_iff_mod_2_eq_one: "¬ 2 dvd a ⟷ a mod 2 = 1"
and odd_one [simp]: "¬ 2 dvd 1"
begin

abbreviation even :: "'a ⇒ bool"
where "even a ≡ 2 dvd a"

abbreviation odd :: "'a ⇒ bool"
where "odd a ≡ ¬ 2 dvd a"

lemma parity_cases [case_names even odd]:
assumes "even a ⟹ a mod 2 = 0 ⟹ P"
assumes "odd a ⟹ a mod 2 = 1 ⟹ P"
shows P
using assms by (cases "even a")
(simp_all add: even_iff_mod_2_eq_zero [symmetric] odd_iff_mod_2_eq_one [symmetric])

lemma odd_of_bool_self [simp]:
‹odd (of_bool p) ⟷ p›
by (cases p) simp_all

lemma not_mod_2_eq_0_eq_1 [simp]:
"a mod 2 ≠ 0 ⟷ a mod 2 = 1"
by (cases a rule: parity_cases) simp_all

lemma not_mod_2_eq_1_eq_0 [simp]:
"a mod 2 ≠ 1 ⟷ a mod 2 = 0"
by (cases a rule: parity_cases) simp_all

lemma evenE [elim?]:
assumes "even a"
obtains b where "a = 2 * b"
using assms by (rule dvdE)

lemma oddE [elim?]:
assumes "odd a"
obtains b where "a = 2 * b + 1"
proof -
have "a = 2 * (a div 2) + a mod 2"
with assms have "a = 2 * (a div 2) + 1"
then show ?thesis ..
qed

lemma mod_2_eq_odd:
"a mod 2 = of_bool (odd a)"
by (auto elim: oddE simp add: even_iff_mod_2_eq_zero)

lemma of_bool_odd_eq_mod_2:
"of_bool (odd a) = a mod 2"

lemma even_mod_2_iff [simp]:
‹even (a mod 2) ⟷ even a›

lemma mod2_eq_if:
"a mod 2 = (if even a then 0 else 1)"

lemma even_zero [simp]:
"even 0"
by (fact dvd_0_right)

"even (a + b)" if "odd a" and "odd b"
proof -
from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"
by (blast elim: oddE)
then have "a + b = 2 * c + 2 * d + (1 + 1)"
by (simp only: ac_simps)
also have "… = 2 * (c + d + 1)"
finally show ?thesis ..
qed

"even (a + b) ⟷ (even a ⟷ even b)"

"odd (a + b) ⟷ ¬ (odd a ⟷ odd b)"
by simp

lemma even_plus_one_iff [simp]:
"even (a + 1) ⟷ odd a"

lemma even_mult_iff [simp]:
"even (a * b) ⟷ even a ∨ even b" (is "?P ⟷ ?Q")
proof
assume ?Q
then show ?P
by auto
next
assume ?P
show ?Q
proof (rule ccontr)
assume "¬ (even a ∨ even b)"
then have "odd a" and "odd b"
by auto
then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"
by (blast elim: oddE)
then have "a * b = (2 * r + 1) * (2 * s + 1)"
by simp
also have "… = 2 * (2 * r * s + r + s) + 1"
finally have "odd (a * b)"
by simp
with ‹?P› show False
by auto
qed
qed

lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
proof -
have "even (2 * numeral n)"
unfolding even_mult_iff by simp
then have "even (numeral n + numeral n)"
unfolding mult_2 .
then show ?thesis
unfolding numeral.simps .
qed

lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
proof
assume "even (numeral (num.Bit1 n))"
then have "even (numeral n + numeral n + 1)"
unfolding numeral.simps .
then have "even (2 * numeral n + 1)"
unfolding mult_2 .
then have "2 dvd numeral n * 2 + 1"
then have "2 dvd 1"
using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
then show False by simp
qed

lemma odd_numeral_BitM [simp]:
‹odd (numeral (Num.BitM w))›
by (cases w) simp_all

lemma even_power [simp]: "even (a ^ n) ⟷ even a ∧ n > 0"
by (induct n) auto

‹2 ^ n - 1 = (∑m∈{q. q < n}. 2 ^ m)›
proof -
have *: ‹{q. q < Suc m} = insert m {q. q < m}› for m
by auto
have ‹2 ^ n = (∑m∈{q. q < n}. 2 ^ m) + 1›
by (induction n) (simp_all add: ac_simps mult_2 *)
then have ‹2 ^ n - 1 = (∑m∈{q. q < n}. 2 ^ m) + 1 - 1›
by simp
then show ?thesis
by simp
qed

end

class ring_parity = ring + semiring_parity
begin

subclass comm_ring_1 ..

lemma even_minus:
"even (- a) ⟷ even a"
by (fact dvd_minus_iff)

lemma even_diff [simp]:
"even (a - b) ⟷ even (a + b)"
using even_add [of a "- b"] by simp

end

subsection ‹Special case: euclidean rings containing the natural numbers›

context unique_euclidean_semiring_with_nat
begin

subclass semiring_parity
proof
show "2 dvd a ⟷ a mod 2 = 0" for a
by (fact dvd_eq_mod_eq_0)
show "¬ 2 dvd a ⟷ a mod 2 = 1" for a
proof
assume "a mod 2 = 1"
then show "¬ 2 dvd a"
by auto
next
assume "¬ 2 dvd a"
have eucl: "euclidean_size (a mod 2) = 1"
proof (rule order_antisym)
show "euclidean_size (a mod 2) ≤ 1"
using mod_size_less [of 2 a] by simp
show "1 ≤ euclidean_size (a mod 2)"
using ‹¬ 2 dvd a› by (simp add: Suc_le_eq dvd_eq_mod_eq_0)
qed
from ‹¬ 2 dvd a› have "¬ of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"
by simp
then have "¬ of_nat 2 dvd of_nat (euclidean_size a)"
by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)
then have "¬ 2 dvd euclidean_size a"
using of_nat_dvd_iff [of 2] by simp
then have "euclidean_size a mod 2 = 1"
then have "of_nat (euclidean_size a mod 2) = of_nat 1"
by simp
then have "of_nat (euclidean_size a) mod 2 = 1"
from ‹¬ 2 dvd a› eucl
show "a mod 2 = 1"
by (auto intro: division_segment_eq_iff simp add: division_segment_mod)
qed
show "¬ is_unit 2"
proof (rule notI)
assume "is_unit 2"
then have "of_nat 2 dvd of_nat 1"
by simp
then have "is_unit (2::nat)"
by (simp only: of_nat_dvd_iff)
then show False
by simp
qed
qed

lemma even_succ_div_two [simp]:
"even a ⟹ (a + 1) div 2 = a div 2"
by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)

lemma odd_succ_div_two [simp]:
"odd a ⟹ (a + 1) div 2 = a div 2 + 1"

lemma even_two_times_div_two:
"even a ⟹ 2 * (a div 2) = a"
by (fact dvd_mult_div_cancel)

lemma odd_two_times_div_two_succ [simp]:
"odd a ⟹ 2 * (a div 2) + 1 = a"
using mult_div_mod_eq [of 2 a]

lemma coprime_left_2_iff_odd [simp]:
"coprime 2 a ⟷ odd a"
proof
assume "odd a"
show "coprime 2 a"
proof (rule coprimeI)
fix b
assume "b dvd 2" "b dvd a"
then have "b dvd a mod 2"
by (auto intro: dvd_mod)
with ‹odd a› show "is_unit b"
qed
next
assume "coprime 2 a"
show "odd a"
proof (rule notI)
assume "even a"
then obtain b where "a = 2 * b" ..
with ‹coprime 2 a› have "coprime 2 (2 * b)"
by simp
moreover have "¬ coprime 2 (2 * b)"
by (rule not_coprimeI [of 2]) simp_all
ultimately show False
by blast
qed
qed

lemma coprime_right_2_iff_odd [simp]:
"coprime a 2 ⟷ odd a"
using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)

end

context unique_euclidean_ring_with_nat
begin

subclass ring_parity ..

lemma minus_1_mod_2_eq [simp]:
"- 1 mod 2 = 1"

lemma minus_1_div_2_eq [simp]:
"- 1 div 2 = - 1"
proof -
from div_mult_mod_eq [of "- 1" 2]
have "- 1 div 2 * 2 = - 1 * 2"
then show ?thesis
using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp
qed

end

subsection ‹Instance for \<^typ>‹nat››

instance nat :: unique_euclidean_semiring_with_nat

lemma even_Suc_Suc_iff [simp]:
"even (Suc (Suc n)) ⟷ even n"
using dvd_add_triv_right_iff [of 2 n] by simp

lemma even_Suc [simp]: "even (Suc n) ⟷ odd n"
using even_plus_one_iff [of n] by simp

lemma even_diff_nat [simp]:
"even (m - n) ⟷ m < n ∨ even (m + n)" for m n :: nat
proof (cases "n ≤ m")
case True
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
moreover have "even (m - n) ⟷ even (m - n + n * 2)" by simp
ultimately have "even (m - n) ⟷ even (m + n)" by (simp only:)
then show ?thesis by auto
next
case False
then show ?thesis by simp
qed

lemma odd_pos:
"odd n ⟹ 0 < n" for n :: nat
by (auto elim: oddE)

lemma Suc_double_not_eq_double:
"Suc (2 * m) ≠ 2 * n"
proof
assume "Suc (2 * m) = 2 * n"
moreover have "odd (Suc (2 * m))" and "even (2 * n)"
by simp_all
ultimately show False by simp
qed

lemma double_not_eq_Suc_double:
"2 * m ≠ Suc (2 * n)"
using Suc_double_not_eq_double [of n m] by simp

lemma odd_Suc_minus_one [simp]: "odd n ⟹ Suc (n - Suc 0) = n"
by (auto elim: oddE)

lemma even_Suc_div_two [simp]:
"even n ⟹ Suc n div 2 = n div 2"
using even_succ_div_two [of n] by simp

lemma odd_Suc_div_two [simp]:
"odd n ⟹ Suc n div 2 = Suc (n div 2)"
using odd_succ_div_two [of n] by simp

lemma odd_two_times_div_two_nat [simp]:
assumes "odd n"
shows "2 * (n div 2) = n - (1 :: nat)"
proof -
from assms have "2 * (n div 2) + 1 = n"
by (rule odd_two_times_div_two_succ)
then have "Suc (2 * (n div 2)) - 1 = n - 1"
by simp
then show ?thesis
by simp
qed

lemma not_mod2_eq_Suc_0_eq_0 [simp]:
"n mod 2 ≠ Suc 0 ⟷ n mod 2 = 0"
using not_mod_2_eq_1_eq_0 [of n] by simp

lemma odd_card_imp_not_empty:
‹A ≠ {}› if ‹odd (card A)›
using that by auto

lemma nat_induct2 [case_names 0 1 step]:
assumes "P 0" "P 1" and step: "⋀n::nat. P n ⟹ P (n + 2)"
shows "P n"
proof (induct n rule: less_induct)
case (less n)
show ?case
proof (cases "n < Suc (Suc 0)")
case True
then show ?thesis
using assms by (auto simp: less_Suc_eq)
next
case False
then obtain k where k: "n = Suc (Suc k)"
then have "k<n"
by simp
with less assms have "P (k+2)"
by blast
then show ?thesis
qed
qed

‹2 ^ n - Suc 0 = (∑m∈{q. q < n}. 2 ^ m)›
using mask_eq_sum_exp [where ?'a = nat] by simp

context semiring_parity
begin

lemma even_of_nat_iff [simp]:
"even (of_nat n) ⟷ even n"
by (induction n) simp_all

lemma even_sum_iff:
‹even (sum f A) ⟷ even (card {a∈A. odd (f a)})› if ‹finite A›
using that proof (induction A)
case empty
then show ?case
by simp
next
case (insert a A)
moreover have ‹{b ∈ insert a A. odd (f b)} = (if odd (f a) then {a} else {}) ∪ {b ∈ A. odd (f b)}›
by auto
ultimately show ?case
by simp
qed

lemma even_prod_iff:
‹even (prod f A) ⟷ (∃a∈A. even (f a))› if ‹finite A›
using that by (induction A) simp_all

‹even (2 ^ n - 1) ⟷ n = 0›
proof (cases ‹n = 0›)
case True
then show ?thesis
by simp
next
case False
then have ‹{a. a = 0 ∧ a < n} = {0}›
by auto
then show ?thesis
qed

end

subsection ‹Parity and powers›

context ring_1
begin

lemma power_minus_even [simp]: "even n ⟹ (- a) ^ n = a ^ n"
by (auto elim: evenE)

lemma power_minus_odd [simp]: "odd n ⟹ (- a) ^ n = - (a ^ n)"
by (auto elim: oddE)

lemma uminus_power_if:
"(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
by auto

lemma neg_one_even_power [simp]: "even n ⟹ (- 1) ^ n = 1"
by simp

lemma neg_one_odd_power [simp]: "odd n ⟹ (- 1) ^ n = - 1"
by simp

lemma neg_one_power_add_eq_neg_one_power_diff: "k ≤ n ⟹ (- 1) ^ (n + k) = (- 1) ^ (n - k)"
by (cases "even (n + k)") auto

lemma minus_one_power_iff: "(- 1) ^ n = (if even n then 1 else - 1)"
by (induct n) auto

end

context linordered_idom
begin

lemma zero_le_even_power: "even n ⟹ 0 ≤ a ^ n"
by (auto elim: evenE)

lemma zero_le_odd_power: "odd n ⟹ 0 ≤ a ^ n ⟷ 0 ≤ a"
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)

lemma zero_le_power_eq: "0 ≤ a ^ n ⟷ even n ∨ odd n ∧ 0 ≤ a"
by (auto simp add: zero_le_even_power zero_le_odd_power)

lemma zero_less_power_eq: "0 < a ^ n ⟷ n = 0 ∨ even n ∧ a ≠ 0 ∨ odd n ∧ 0 < a"
proof -
have [simp]: "0 = a ^ n ⟷ a = 0 ∧ n > 0"
unfolding power_eq_0_iff [of a n, symmetric] by blast
show ?thesis
unfolding less_le zero_le_power_eq by auto
qed

lemma power_less_zero_eq [simp]: "a ^ n < 0 ⟷ odd n ∧ a < 0"
unfolding not_le [symmetric] zero_le_power_eq by auto

lemma power_le_zero_eq: "a ^ n ≤ 0 ⟷ n > 0 ∧ (odd n ∧ a ≤ 0 ∨ even n ∧ a = 0)"
unfolding not_less [symmetric] zero_less_power_eq by auto

lemma power_even_abs: "even n ⟹ ¦a¦ ^ n = a ^ n"
using power_abs [of a n] by (simp add: zero_le_even_power)

lemma power_mono_even:
assumes "even n" and "¦a¦ ≤ ¦b¦"
shows "a ^ n ≤ b ^ n"
proof -
have "0 ≤ ¦a¦" by auto
with ‹¦a¦ ≤ ¦b¦› have "¦a¦ ^ n ≤ ¦b¦ ^ n"
by (rule power_mono)
with ‹even n› show ?thesis
qed

lemma power_mono_odd:
assumes "odd n" and "a ≤ b"
shows "a ^ n ≤ b ^ n"
proof (cases "b < 0")
case True
with ‹a ≤ b› have "- b ≤ - a" and "0 ≤ - b" by auto
then have "(- b) ^ n ≤ (- a) ^ n" by (rule power_mono)
with ‹odd n› show ?thesis by simp
next
case False
then have "0 ≤ b" by auto
show ?thesis
proof (cases "a < 0")
case True
then have "n ≠ 0" and "a ≤ 0" using ‹odd n› [THEN odd_pos] by auto
then have "a ^ n ≤ 0" unfolding power_le_zero_eq using ‹odd n› by auto
moreover from ‹0 ≤ b› have "0 ≤ b ^ n" by auto
ultimately show ?thesis by auto
next
case False
then have "0 ≤ a" by auto
with ‹a ≤ b› show ?thesis
using power_mono by auto
qed
qed

text ‹Simplify, when the exponent is a numeral›

lemma zero_le_power_eq_numeral [simp]:
"0 ≤ a ^ numeral w ⟷ even (numeral w :: nat) ∨ odd (numeral w :: nat) ∧ 0 ≤ a"
by (fact zero_le_power_eq)

lemma zero_less_power_eq_numeral [simp]:
"0 < a ^ numeral w ⟷
numeral w = (0 :: nat) ∨
even (numeral w :: nat) ∧ a ≠ 0 ∨
odd (numeral w :: nat) ∧ 0 < a"
by (fact zero_less_power_eq)

lemma power_le_zero_eq_numeral [simp]:
"a ^ numeral w ≤ 0 ⟷
(0 :: nat) < numeral w ∧
(odd (numeral w :: nat) ∧ a ≤ 0 ∨ even (numeral w :: nat) ∧ a = 0)"
by (fact power_le_zero_eq)

lemma power_less_zero_eq_numeral [simp]:
"a ^ numeral w < 0 ⟷ odd (numeral w :: nat) ∧ a < 0"
by (fact power_less_zero_eq)

lemma power_even_abs_numeral [simp]:
"even (numeral w :: nat) ⟹ ¦a¦ ^ numeral w = a ^ numeral w"
by (fact power_even_abs)

end

context unique_euclidean_semiring_with_nat
begin

‹even ((2 ^ m - 1) div 2 ^ n) ⟷ m ≤ n›
proof -
have ‹even ((2 ^ m - 1) div 2 ^ n) ⟷ even (of_nat ((2 ^ m - Suc 0) div 2 ^ n))›
by (simp only: of_nat_div) (simp add: of_nat_diff)
also have ‹… ⟷ even ((2 ^ m - Suc 0) div 2 ^ n)›
by simp
also have ‹… ⟷ m ≤ n›
proof (cases ‹m ≤ n›)
case True
then show ?thesis
next
case False
then obtain r where r: ‹m = n + Suc r›
from r have ‹{q. q < m} ∩ {q. 2 ^ n dvd (2::nat) ^ q} = {q. n ≤ q ∧ q < m}›
moreover from r have ‹{q. q < m} ∩ {q. ¬ 2 ^ n dvd (2::nat) ^ q} = {q. q < n}›
moreover from False have ‹{q. n ≤ q ∧ q < m ∧ q ≤ n} = {n}›
by auto
then have ‹odd ((∑a∈{q. n ≤ q ∧ q < m}. 2 ^ a div (2::nat) ^ n) + sum ((^) 2) {q. q < n} div 2 ^ n)›
ultimately have ‹odd (sum ((^) (2::nat)) {q. q < m} div 2 ^ n)›
by (subst euclidean_semiring_cancel_class.sum_div_partition) simp_all
with False show ?thesis
qed
finally show ?thesis .
qed

end

subsection ‹Instance for \<^typ>‹int››

lemma even_diff_iff:
"even (k - l) ⟷ even (k + l)" for k l :: int
by (fact even_diff)

"even (¦k¦ + l) ⟷ even (k + l)" for k l :: int
by simp

"even (k + ¦l¦) ⟷ even (k + l)" for k l :: int
by simp

lemma even_nat_iff: "0 ≤ k ⟹ even (nat k) ⟷ even k"
by (simp add: even_of_nat_iff [of "nat k", where ?'a = int, symmetric])

context
assumes "SORT_CONSTRAINT('a::division_ring)"
begin

lemma power_int_minus_left:
"power_int (-a :: 'a) n = (if even n then power_int a n else -power_int a n)"
by (auto simp: power_int_def minus_one_power_iff even_nat_iff)

lemma power_int_minus_left_even [simp]: "even n ⟹ power_int (-a :: 'a) n = power_int a n"

lemma power_int_minus_left_odd [simp]: "odd n ⟹ power_int (-a :: 'a) n = -power_int a n"

lemma power_int_minus_left_distrib:
"NO_MATCH (-1) x ⟹ power_int (-a :: 'a) n = power_int (-1) n * power_int a n"

lemma power_int_minus_one_minus: "power_int (-1 :: 'a) (-n) = power_int (-1) n"

lemma power_int_minus_one_diff_commute: "power_int (-1 :: 'a) (a - b) = power_int (-1) (b - a)"
by (subst power_int_minus_one_minus [symmetric]) auto

lemma power_int_minus_one_mult_self [simp]:
"power_int (-1 :: 'a) m * power_int (-1) m = 1"

lemma power_int_minus_one_mult_self' [simp]:
"power_int (-1 :: 'a) m * (power_int (-1) m * b) = b"

end

subsection ‹Computing congruences modulo ‹2 ^ q››

context unique_euclidean_semiring_with_nat_division
begin

lemma cong_exp_iff_simps:
"numeral n mod numeral Num.One = 0
⟷ True"
"numeral (Num.Bit0 n) mod numeral (Num.Bit0 q) = 0
⟷ numeral n mod numeral q = 0"
"numeral (Num.Bit1 n) mod numeral (Num.Bit0 q) = 0
⟷ False"
"numeral m mod numeral Num.One = (numeral n mod numeral Num.One)
⟷ True"
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
⟷ True"
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
⟷ False"
"numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
⟷ (numeral n mod numeral q) = 0"
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
⟷ False"
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
⟷ numeral m mod numeral q = (numeral n mod numeral q)"
"numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
⟷ False"
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
⟷ (numeral m mod numeral q) = 0"
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
⟷ False"
"numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
⟷ numeral m mod numeral q = (numeral n mod numeral q)"
by (auto simp add: case_prod_beta dest: arg_cong [of _ _ even])

end

code_identifier
code_module Parity ⇀ (SML) Arith and (OCaml) Arith and (Haskell) Arith

lemmas even_of_nat = even_of_nat_iff

end
```