Theory Boolean_Algebra

theory Boolean_Algebra
imports Main
(*  Title:      HOL/Library/Boolean_Algebra.thy
    Author:     Brian Huffman
*)

section ‹Boolean Algebras›

theory Boolean_Algebra
imports Main
begin

locale boolean =
  fixes conj :: "'a ⇒ 'a ⇒ 'a" (infixr "⊓" 70)
  fixes disj :: "'a ⇒ 'a ⇒ 'a" (infixr "⊔" 65)
  fixes compl :: "'a ⇒ 'a" ("∼ _" [81] 80)
  fixes zero :: "'a" ("𝟬")
  fixes one  :: "'a" ("𝟭")
  assumes conj_assoc: "(x ⊓ y) ⊓ z = x ⊓ (y ⊓ z)"
  assumes disj_assoc: "(x ⊔ y) ⊔ z = x ⊔ (y ⊔ z)"
  assumes conj_commute: "x ⊓ y = y ⊓ x"
  assumes disj_commute: "x ⊔ y = y ⊔ x"
  assumes conj_disj_distrib: "x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z)"
  assumes disj_conj_distrib: "x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z)"
  assumes conj_one_right [simp]: "x ⊓ 𝟭 = x"
  assumes disj_zero_right [simp]: "x ⊔ 𝟬 = x"
  assumes conj_cancel_right [simp]: "x ⊓ ∼ x = 𝟬"
  assumes disj_cancel_right [simp]: "x ⊔ ∼ x = 𝟭"
begin

sublocale conj: abel_semigroup conj
  by standard (fact conj_assoc conj_commute)+

sublocale disj: abel_semigroup disj
  by standard (fact disj_assoc disj_commute)+

lemmas conj_left_commute = conj.left_commute

lemmas disj_left_commute = disj.left_commute

lemmas conj_ac = conj.assoc conj.commute conj.left_commute
lemmas disj_ac = disj.assoc disj.commute disj.left_commute

lemma dual: "boolean disj conj compl one zero"
apply (rule boolean.intro)
apply (rule disj_assoc)
apply (rule conj_assoc)
apply (rule disj_commute)
apply (rule conj_commute)
apply (rule disj_conj_distrib)
apply (rule conj_disj_distrib)
apply (rule disj_zero_right)
apply (rule conj_one_right)
apply (rule disj_cancel_right)
apply (rule conj_cancel_right)
done


subsection ‹Complement›

lemma complement_unique:
  assumes 1: "a ⊓ x = 𝟬"
  assumes 2: "a ⊔ x = 𝟭"
  assumes 3: "a ⊓ y = 𝟬"
  assumes 4: "a ⊔ y = 𝟭"
  shows "x = y"
proof -
  have "(a ⊓ x) ⊔ (x ⊓ y) = (a ⊓ y) ⊔ (x ⊓ y)" using 1 3 by simp
  hence "(x ⊓ a) ⊔ (x ⊓ y) = (y ⊓ a) ⊔ (y ⊓ x)" using conj_commute by simp
  hence "x ⊓ (a ⊔ y) = y ⊓ (a ⊔ x)" using conj_disj_distrib by simp
  hence "x ⊓ 𝟭 = y ⊓ 𝟭" using 2 4 by simp
  thus "x = y" using conj_one_right by simp
qed

lemma compl_unique: "⟦x ⊓ y = 𝟬; x ⊔ y = 𝟭⟧ ⟹ ∼ x = y"
by (rule complement_unique [OF conj_cancel_right disj_cancel_right])

lemma double_compl [simp]: "∼ (∼ x) = x"
proof (rule compl_unique)
  from conj_cancel_right show "∼ x ⊓ x = 𝟬" by (simp only: conj_commute)
  from disj_cancel_right show "∼ x ⊔ x = 𝟭" by (simp only: disj_commute)
qed

lemma compl_eq_compl_iff [simp]: "(∼ x = ∼ y) = (x = y)"
by (rule inj_eq [OF inj_on_inverseI], rule double_compl)


subsection ‹Conjunction›

lemma conj_absorb [simp]: "x ⊓ x = x"
proof -
  have "x ⊓ x = (x ⊓ x) ⊔ 𝟬" using disj_zero_right by simp
  also have "... = (x ⊓ x) ⊔ (x ⊓ ∼ x)" using conj_cancel_right by simp
  also have "... = x ⊓ (x ⊔ ∼ x)" using conj_disj_distrib by (simp only:)
  also have "... = x ⊓ 𝟭" using disj_cancel_right by simp
  also have "... = x" using conj_one_right by simp
  finally show ?thesis .
qed

lemma conj_zero_right [simp]: "x ⊓ 𝟬 = 𝟬"
proof -
  have "x ⊓ 𝟬 = x ⊓ (x ⊓ ∼ x)" using conj_cancel_right by simp
  also have "... = (x ⊓ x) ⊓ ∼ x" using conj_assoc by (simp only:)
  also have "... = x ⊓ ∼ x" using conj_absorb by simp
  also have "... = 𝟬" using conj_cancel_right by simp
  finally show ?thesis .
qed

lemma compl_one [simp]: "∼ 𝟭 = 𝟬"
by (rule compl_unique [OF conj_zero_right disj_zero_right])

lemma conj_zero_left [simp]: "𝟬 ⊓ x = 𝟬"
by (subst conj_commute) (rule conj_zero_right)

lemma conj_one_left [simp]: "𝟭 ⊓ x = x"
by (subst conj_commute) (rule conj_one_right)

lemma conj_cancel_left [simp]: "∼ x ⊓ x = 𝟬"
by (subst conj_commute) (rule conj_cancel_right)

lemma conj_left_absorb [simp]: "x ⊓ (x ⊓ y) = x ⊓ y"
by (simp only: conj_assoc [symmetric] conj_absorb)

lemma conj_disj_distrib2:
  "(y ⊔ z) ⊓ x = (y ⊓ x) ⊔ (z ⊓ x)"
by (simp only: conj_commute conj_disj_distrib)

lemmas conj_disj_distribs =
   conj_disj_distrib conj_disj_distrib2


subsection ‹Disjunction›

lemma disj_absorb [simp]: "x ⊔ x = x"
by (rule boolean.conj_absorb [OF dual])

lemma disj_one_right [simp]: "x ⊔ 𝟭 = 𝟭"
by (rule boolean.conj_zero_right [OF dual])

lemma compl_zero [simp]: "∼ 𝟬 = 𝟭"
by (rule boolean.compl_one [OF dual])

lemma disj_zero_left [simp]: "𝟬 ⊔ x = x"
by (rule boolean.conj_one_left [OF dual])

lemma disj_one_left [simp]: "𝟭 ⊔ x = 𝟭"
by (rule boolean.conj_zero_left [OF dual])

lemma disj_cancel_left [simp]: "∼ x ⊔ x = 𝟭"
by (rule boolean.conj_cancel_left [OF dual])

lemma disj_left_absorb [simp]: "x ⊔ (x ⊔ y) = x ⊔ y"
by (rule boolean.conj_left_absorb [OF dual])

lemma disj_conj_distrib2:
  "(y ⊓ z) ⊔ x = (y ⊔ x) ⊓ (z ⊔ x)"
by (rule boolean.conj_disj_distrib2 [OF dual])

lemmas disj_conj_distribs =
   disj_conj_distrib disj_conj_distrib2


subsection ‹De Morgan's Laws›

lemma de_Morgan_conj [simp]: "∼ (x ⊓ y) = ∼ x ⊔ ∼ y"
proof (rule compl_unique)
  have "(x ⊓ y) ⊓ (∼ x ⊔ ∼ y) = ((x ⊓ y) ⊓ ∼ x) ⊔ ((x ⊓ y) ⊓ ∼ y)"
    by (rule conj_disj_distrib)
  also have "... = (y ⊓ (x ⊓ ∼ x)) ⊔ (x ⊓ (y ⊓ ∼ y))"
    by (simp only: conj_ac)
  finally show "(x ⊓ y) ⊓ (∼ x ⊔ ∼ y) = 𝟬"
    by (simp only: conj_cancel_right conj_zero_right disj_zero_right)
next
  have "(x ⊓ y) ⊔ (∼ x ⊔ ∼ y) = (x ⊔ (∼ x ⊔ ∼ y)) ⊓ (y ⊔ (∼ x ⊔ ∼ y))"
    by (rule disj_conj_distrib2)
  also have "... = (∼ y ⊔ (x ⊔ ∼ x)) ⊓ (∼ x ⊔ (y ⊔ ∼ y))"
    by (simp only: disj_ac)
  finally show "(x ⊓ y) ⊔ (∼ x ⊔ ∼ y) = 𝟭"
    by (simp only: disj_cancel_right disj_one_right conj_one_right)
qed

lemma de_Morgan_disj [simp]: "∼ (x ⊔ y) = ∼ x ⊓ ∼ y"
by (rule boolean.de_Morgan_conj [OF dual])

end


subsection ‹Symmetric Difference›

locale boolean_xor = boolean +
  fixes xor :: "'a ⇒ 'a ⇒ 'a"  (infixr "⊕" 65)
  assumes xor_def: "x ⊕ y = (x ⊓ ∼ y) ⊔ (∼ x ⊓ y)"
begin

sublocale xor: abel_semigroup xor
proof
  fix x y z :: 'a
  let ?t = "(x ⊓ y ⊓ z) ⊔ (x ⊓ ∼ y ⊓ ∼ z) ⊔
            (∼ x ⊓ y ⊓ ∼ z) ⊔ (∼ x ⊓ ∼ y ⊓ z)"
  have "?t ⊔ (z ⊓ x ⊓ ∼ x) ⊔ (z ⊓ y ⊓ ∼ y) =
        ?t ⊔ (x ⊓ y ⊓ ∼ y) ⊔ (x ⊓ z ⊓ ∼ z)"
    by (simp only: conj_cancel_right conj_zero_right)
  thus "(x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)"
    apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
    apply (simp only: conj_disj_distribs conj_ac disj_ac)
    done
  show "x ⊕ y = y ⊕ x"
    by (simp only: xor_def conj_commute disj_commute)
qed

lemmas xor_assoc = xor.assoc
lemmas xor_commute = xor.commute
lemmas xor_left_commute = xor.left_commute

lemmas xor_ac = xor.assoc xor.commute xor.left_commute

lemma xor_def2:
  "x ⊕ y = (x ⊔ y) ⊓ (∼ x ⊔ ∼ y)"
by (simp only: xor_def conj_disj_distribs
               disj_ac conj_ac conj_cancel_right disj_zero_left)

lemma xor_zero_right [simp]: "x ⊕ 𝟬 = x"
by (simp only: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)

lemma xor_zero_left [simp]: "𝟬 ⊕ x = x"
by (subst xor_commute) (rule xor_zero_right)

lemma xor_one_right [simp]: "x ⊕ 𝟭 = ∼ x"
by (simp only: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)

lemma xor_one_left [simp]: "𝟭 ⊕ x = ∼ x"
by (subst xor_commute) (rule xor_one_right)

lemma xor_self [simp]: "x ⊕ x = 𝟬"
by (simp only: xor_def conj_cancel_right conj_cancel_left disj_zero_right)

lemma xor_left_self [simp]: "x ⊕ (x ⊕ y) = y"
by (simp only: xor_assoc [symmetric] xor_self xor_zero_left)

lemma xor_compl_left [simp]: "∼ x ⊕ y = ∼ (x ⊕ y)"
apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
apply (simp only: conj_disj_distribs)
apply (simp only: conj_cancel_right conj_cancel_left)
apply (simp only: disj_zero_left disj_zero_right)
apply (simp only: disj_ac conj_ac)
done

lemma xor_compl_right [simp]: "x ⊕ ∼ y = ∼ (x ⊕ y)"
apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
apply (simp only: conj_disj_distribs)
apply (simp only: conj_cancel_right conj_cancel_left)
apply (simp only: disj_zero_left disj_zero_right)
apply (simp only: disj_ac conj_ac)
done

lemma xor_cancel_right: "x ⊕ ∼ x = 𝟭"
by (simp only: xor_compl_right xor_self compl_zero)

lemma xor_cancel_left: "∼ x ⊕ x = 𝟭"
by (simp only: xor_compl_left xor_self compl_zero)

lemma conj_xor_distrib: "x ⊓ (y ⊕ z) = (x ⊓ y) ⊕ (x ⊓ z)"
proof -
  have "(x ⊓ y ⊓ ∼ z) ⊔ (x ⊓ ∼ y ⊓ z) =
        (y ⊓ x ⊓ ∼ x) ⊔ (z ⊓ x ⊓ ∼ x) ⊔ (x ⊓ y ⊓ ∼ z) ⊔ (x ⊓ ∼ y ⊓ z)"
    by (simp only: conj_cancel_right conj_zero_right disj_zero_left)
  thus "x ⊓ (y ⊕ z) = (x ⊓ y) ⊕ (x ⊓ z)"
    by (simp (no_asm_use) only:
        xor_def de_Morgan_disj de_Morgan_conj double_compl
        conj_disj_distribs conj_ac disj_ac)
qed

lemma conj_xor_distrib2: "(y ⊕ z) ⊓ x = (y ⊓ x) ⊕ (z ⊓ x)"
proof -
  have "x ⊓ (y ⊕ z) = (x ⊓ y) ⊕ (x ⊓ z)"
    by (rule conj_xor_distrib)
  thus "(y ⊕ z) ⊓ x = (y ⊓ x) ⊕ (z ⊓ x)"
    by (simp only: conj_commute)
qed

lemmas conj_xor_distribs = conj_xor_distrib conj_xor_distrib2

end

end