
haftmann@29629: (*  Title:      HOL/Library/Boolean_Algebra.thy
haftmann@29629:     Author:     Brian Huffman
kleing@24332: *)
kleing@24332: 
kleing@24332: header {* Boolean Algebras *}
kleing@24332: 
kleing@24332: theory Boolean_Algebra
haftmann@30663: imports Main
kleing@24332: begin
kleing@24332: 
kleing@24332: locale boolean =
huffman@24357:   fixes conj :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "\<sqinter>" 70)
huffman@24357:   fixes disj :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "\<squnion>" 65)
huffman@24357:   fixes compl :: "'a \<Rightarrow> 'a" ("\<sim> _" [81] 80)
kleing@24332:   fixes zero :: "'a" ("\<zero>")
kleing@24332:   fixes one  :: "'a" ("\<one>")
kleing@24332:   assumes conj_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
kleing@24332:   assumes disj_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
kleing@24332:   assumes conj_commute: "x \<sqinter> y = y \<sqinter> x"
kleing@24332:   assumes disj_commute: "x \<squnion> y = y \<squnion> x"
kleing@24332:   assumes conj_disj_distrib: "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
kleing@24332:   assumes disj_conj_distrib: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
huffman@24357:   assumes conj_one_right [simp]: "x \<sqinter> \<one> = x"
huffman@24357:   assumes disj_zero_right [simp]: "x \<squnion> \<zero> = x"
huffman@24357:   assumes conj_cancel_right [simp]: "x \<sqinter> \<sim> x = \<zero>"
huffman@24357:   assumes disj_cancel_right [simp]: "x \<squnion> \<sim> x = \<one>"
haftmann@54868: begin
haftmann@34973: 
haftmann@54868: sublocale conj!: abel_semigroup conj proof
haftmann@34973: qed (fact conj_assoc conj_commute)+
haftmann@34973: 
haftmann@54868: sublocale disj!: abel_semigroup disj proof
haftmann@34973: qed (fact disj_assoc disj_commute)+
haftmann@34973: 
haftmann@34973: lemmas conj_left_commute = conj.left_commute
kleing@24332: 
haftmann@34973: lemmas disj_left_commute = disj.left_commute
haftmann@34973: 
haftmann@34973: lemmas conj_ac = conj.assoc conj.commute conj.left_commute
haftmann@34973: lemmas disj_ac = disj.assoc disj.commute disj.left_commute
kleing@24332: 
kleing@24332: lemma dual: "boolean disj conj compl one zero"
kleing@24332: apply (rule boolean.intro)
kleing@24332: apply (rule disj_assoc)
kleing@24332: apply (rule conj_assoc)
kleing@24332: apply (rule disj_commute)
kleing@24332: apply (rule conj_commute)
kleing@24332: apply (rule disj_conj_distrib)
kleing@24332: apply (rule conj_disj_distrib)
kleing@24332: apply (rule disj_zero_right)
kleing@24332: apply (rule conj_one_right)
kleing@24332: apply (rule disj_cancel_right)
kleing@24332: apply (rule conj_cancel_right)
kleing@24332: done
kleing@24332: 
huffman@24357: subsection {* Complement *}
kleing@24332: 
kleing@24332: lemma complement_unique:
kleing@24332:   assumes 1: "a \<sqinter> x = \<zero>"
kleing@24332:   assumes 2: "a \<squnion> x = \<one>"
kleing@24332:   assumes 3: "a \<sqinter> y = \<zero>"
kleing@24332:   assumes 4: "a \<squnion> y = \<one>"
kleing@24332:   shows "x = y"
kleing@24332: proof -
kleing@24332:   have "(a \<sqinter> x) \<squnion> (x \<sqinter> y) = (a \<sqinter> y) \<squnion> (x \<sqinter> y)" using 1 3 by simp
kleing@24332:   hence "(x \<sqinter> a) \<squnion> (x \<sqinter> y) = (y \<sqinter> a) \<squnion> (y \<sqinter> x)" using conj_commute by simp
kleing@24332:   hence "x \<sqinter> (a \<squnion> y) = y \<sqinter> (a \<squnion> x)" using conj_disj_distrib by simp
kleing@24332:   hence "x \<sqinter> \<one> = y \<sqinter> \<one>" using 2 4 by simp
kleing@24332:   thus "x = y" using conj_one_right by simp
kleing@24332: qed
kleing@24332: 
huffman@24357: lemma compl_unique: "\<lbrakk>x \<sqinter> y = \<zero>; x \<squnion> y = \<one>\<rbrakk> \<Longrightarrow> \<sim> x = y"
kleing@24332: by (rule complement_unique [OF conj_cancel_right disj_cancel_right])
kleing@24332: 
kleing@24332: lemma double_compl [simp]: "\<sim> (\<sim> x) = x"
kleing@24332: proof (rule compl_unique)
huffman@24357:   from conj_cancel_right show "\<sim> x \<sqinter> x = \<zero>" by (simp only: conj_commute)
huffman@24357:   from disj_cancel_right show "\<sim> x \<squnion> x = \<one>" by (simp only: disj_commute)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma compl_eq_compl_iff [simp]: "(\<sim> x = \<sim> y) = (x = y)"
kleing@24332: by (rule inj_eq [OF inj_on_inverseI], rule double_compl)
kleing@24332: 
huffman@24357: subsection {* Conjunction *}
kleing@24332: 
huffman@24393: lemma conj_absorb [simp]: "x \<sqinter> x = x"
kleing@24332: proof -
kleing@24332:   have "x \<sqinter> x = (x \<sqinter> x) \<squnion> \<zero>" using disj_zero_right by simp
kleing@24332:   also have "... = (x \<sqinter> x) \<squnion> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
huffman@24357:   also have "... = x \<sqinter> (x \<squnion> \<sim> x)" using conj_disj_distrib by (simp only:)
kleing@24332:   also have "... = x \<sqinter> \<one>" using disj_cancel_right by simp
kleing@24332:   also have "... = x" using conj_one_right by simp
kleing@24332:   finally show ?thesis .
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma conj_zero_right [simp]: "x \<sqinter> \<zero> = \<zero>"
kleing@24332: proof -
kleing@24332:   have "x \<sqinter> \<zero> = x \<sqinter> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
huffman@24393:   also have "... = (x \<sqinter> x) \<sqinter> \<sim> x" using conj_assoc by (simp only:)
kleing@24332:   also have "... = x \<sqinter> \<sim> x" using conj_absorb by simp
kleing@24332:   also have "... = \<zero>" using conj_cancel_right by simp
kleing@24332:   finally show ?thesis .
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma compl_one [simp]: "\<sim> \<one> = \<zero>"
kleing@24332: by (rule compl_unique [OF conj_zero_right disj_zero_right])
kleing@24332: 
kleing@24332: lemma conj_zero_left [simp]: "\<zero> \<sqinter> x = \<zero>"
kleing@24332: by (subst conj_commute) (rule conj_zero_right)
kleing@24332: 
kleing@24332: lemma conj_one_left [simp]: "\<one> \<sqinter> x = x"
kleing@24332: by (subst conj_commute) (rule conj_one_right)
kleing@24332: 
kleing@24332: lemma conj_cancel_left [simp]: "\<sim> x \<sqinter> x = \<zero>"
kleing@24332: by (subst conj_commute) (rule conj_cancel_right)
kleing@24332: 
kleing@24332: lemma conj_left_absorb [simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
huffman@24357: by (simp only: conj_assoc [symmetric] conj_absorb)
kleing@24332: 
kleing@24332: lemma conj_disj_distrib2:
kleing@24332:   "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)" 
huffman@24357: by (simp only: conj_commute conj_disj_distrib)
kleing@24332: 
kleing@24332: lemmas conj_disj_distribs =
kleing@24332:    conj_disj_distrib conj_disj_distrib2
kleing@24332: 
huffman@24357: subsection {* Disjunction *}
kleing@24332: 
kleing@24332: lemma disj_absorb [simp]: "x \<squnion> x = x"
kleing@24332: by (rule boolean.conj_absorb [OF dual])
kleing@24332: 
kleing@24332: lemma disj_one_right [simp]: "x \<squnion> \<one> = \<one>"
kleing@24332: by (rule boolean.conj_zero_right [OF dual])
kleing@24332: 
kleing@24332: lemma compl_zero [simp]: "\<sim> \<zero> = \<one>"
kleing@24332: by (rule boolean.compl_one [OF dual])
kleing@24332: 
kleing@24332: lemma disj_zero_left [simp]: "\<zero> \<squnion> x = x"
kleing@24332: by (rule boolean.conj_one_left [OF dual])
kleing@24332: 
kleing@24332: lemma disj_one_left [simp]: "\<one> \<squnion> x = \<one>"
kleing@24332: by (rule boolean.conj_zero_left [OF dual])
kleing@24332: 
kleing@24332: lemma disj_cancel_left [simp]: "\<sim> x \<squnion> x = \<one>"
kleing@24332: by (rule boolean.conj_cancel_left [OF dual])
kleing@24332: 
kleing@24332: lemma disj_left_absorb [simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
kleing@24332: by (rule boolean.conj_left_absorb [OF dual])
kleing@24332: 
kleing@24332: lemma disj_conj_distrib2:
kleing@24332:   "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
kleing@24332: by (rule boolean.conj_disj_distrib2 [OF dual])
kleing@24332: 
kleing@24332: lemmas disj_conj_distribs =
kleing@24332:    disj_conj_distrib disj_conj_distrib2
kleing@24332: 
huffman@24357: subsection {* De Morgan's Laws *}
kleing@24332: 
kleing@24332: lemma de_Morgan_conj [simp]: "\<sim> (x \<sqinter> y) = \<sim> x \<squnion> \<sim> y"
kleing@24332: proof (rule compl_unique)
kleing@24332:   have "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = ((x \<sqinter> y) \<sqinter> \<sim> x) \<squnion> ((x \<sqinter> y) \<sqinter> \<sim> y)"
kleing@24332:     by (rule conj_disj_distrib)
kleing@24332:   also have "... = (y \<sqinter> (x \<sqinter> \<sim> x)) \<squnion> (x \<sqinter> (y \<sqinter> \<sim> y))"
huffman@24357:     by (simp only: conj_ac)
kleing@24332:   finally show "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = \<zero>"
huffman@24357:     by (simp only: conj_cancel_right conj_zero_right disj_zero_right)
kleing@24332: next
kleing@24332:   have "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = (x \<squnion> (\<sim> x \<squnion> \<sim> y)) \<sqinter> (y \<squnion> (\<sim> x \<squnion> \<sim> y))"
kleing@24332:     by (rule disj_conj_distrib2)
kleing@24332:   also have "... = (\<sim> y \<squnion> (x \<squnion> \<sim> x)) \<sqinter> (\<sim> x \<squnion> (y \<squnion> \<sim> y))"
huffman@24357:     by (simp only: disj_ac)
kleing@24332:   finally show "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = \<one>"
huffman@24357:     by (simp only: disj_cancel_right disj_one_right conj_one_right)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma de_Morgan_disj [simp]: "\<sim> (x \<squnion> y) = \<sim> x \<sqinter> \<sim> y"
kleing@24332: by (rule boolean.de_Morgan_conj [OF dual])
kleing@24332: 
kleing@24332: end
kleing@24332: 
huffman@24357: subsection {* Symmetric Difference *}
kleing@24332: 
kleing@24332: locale boolean_xor = boolean +
kleing@24332:   fixes xor :: "'a => 'a => 'a"  (infixr "\<oplus>" 65)
kleing@24332:   assumes xor_def: "x \<oplus> y = (x \<sqinter> \<sim> y) \<squnion> (\<sim> x \<sqinter> y)"
haftmann@54868: begin
kleing@24332: 
haftmann@54868: sublocale xor!: abel_semigroup xor proof
haftmann@34973:   fix x y z :: 'a
kleing@24332:   let ?t = "(x \<sqinter> y \<sqinter> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> \<sim> z) \<squnion>
kleing@24332:             (\<sim> x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (\<sim> x \<sqinter> \<sim> y \<sqinter> z)"
kleing@24332:   have "?t \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> y \<sqinter> \<sim> y) =
kleing@24332:         ?t \<squnion> (x \<sqinter> y \<sqinter> \<sim> y) \<squnion> (x \<sqinter> z \<sqinter> \<sim> z)"
huffman@24357:     by (simp only: conj_cancel_right conj_zero_right)
kleing@24332:   thus "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
huffman@24357:     apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
huffman@24357:     apply (simp only: conj_disj_distribs conj_ac disj_ac)
kleing@24332:     done
haftmann@34973:   show "x \<oplus> y = y \<oplus> x"
haftmann@34973:     by (simp only: xor_def conj_commute disj_commute)
kleing@24332: qed
kleing@24332: 
haftmann@34973: lemmas xor_assoc = xor.assoc
haftmann@34973: lemmas xor_commute = xor.commute
haftmann@34973: lemmas xor_left_commute = xor.left_commute
haftmann@34973: 
haftmann@34973: lemmas xor_ac = xor.assoc xor.commute xor.left_commute
haftmann@34973: 
haftmann@34973: lemma xor_def2:
haftmann@34973:   "x \<oplus> y = (x \<squnion> y) \<sqinter> (\<sim> x \<squnion> \<sim> y)"
haftmann@34973: by (simp only: xor_def conj_disj_distribs
haftmann@34973:                disj_ac conj_ac conj_cancel_right disj_zero_left)
kleing@24332: 
kleing@24332: lemma xor_zero_right [simp]: "x \<oplus> \<zero> = x"
huffman@24357: by (simp only: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)
kleing@24332: 
kleing@24332: lemma xor_zero_left [simp]: "\<zero> \<oplus> x = x"
kleing@24332: by (subst xor_commute) (rule xor_zero_right)
kleing@24332: 
kleing@24332: lemma xor_one_right [simp]: "x \<oplus> \<one> = \<sim> x"
huffman@24357: by (simp only: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)
kleing@24332: 
kleing@24332: lemma xor_one_left [simp]: "\<one> \<oplus> x = \<sim> x"
kleing@24332: by (subst xor_commute) (rule xor_one_right)
kleing@24332: 
kleing@24332: lemma xor_self [simp]: "x \<oplus> x = \<zero>"
huffman@24357: by (simp only: xor_def conj_cancel_right conj_cancel_left disj_zero_right)
kleing@24332: 
kleing@24332: lemma xor_left_self [simp]: "x \<oplus> (x \<oplus> y) = y"
huffman@24357: by (simp only: xor_assoc [symmetric] xor_self xor_zero_left)
kleing@24332: 
huffman@29996: lemma xor_compl_left [simp]: "\<sim> x \<oplus> y = \<sim> (x \<oplus> y)"
huffman@24357: apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
huffman@24357: apply (simp only: conj_disj_distribs)
huffman@24357: apply (simp only: conj_cancel_right conj_cancel_left)
huffman@24357: apply (simp only: disj_zero_left disj_zero_right)
huffman@24357: apply (simp only: disj_ac conj_ac)
kleing@24332: done
kleing@24332: 
huffman@29996: lemma xor_compl_right [simp]: "x \<oplus> \<sim> y = \<sim> (x \<oplus> y)"
huffman@24357: apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
huffman@24357: apply (simp only: conj_disj_distribs)
huffman@24357: apply (simp only: conj_cancel_right conj_cancel_left)
huffman@24357: apply (simp only: disj_zero_left disj_zero_right)
huffman@24357: apply (simp only: disj_ac conj_ac)
kleing@24332: done
kleing@24332: 
huffman@29996: lemma xor_cancel_right: "x \<oplus> \<sim> x = \<one>"
huffman@24357: by (simp only: xor_compl_right xor_self compl_zero)
kleing@24332: 
huffman@29996: lemma xor_cancel_left: "\<sim> x \<oplus> x = \<one>"
huffman@29996: by (simp only: xor_compl_left xor_self compl_zero)
kleing@24332: 
kleing@24332: lemma conj_xor_distrib: "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
kleing@24332: proof -
kleing@24332:   have "(x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z) =
kleing@24332:         (y \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z)"
huffman@24357:     by (simp only: conj_cancel_right conj_zero_right disj_zero_left)
kleing@24332:   thus "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
huffman@24357:     by (simp (no_asm_use) only:
kleing@24332:         xor_def de_Morgan_disj de_Morgan_conj double_compl
kleing@24332:         conj_disj_distribs conj_ac disj_ac)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma conj_xor_distrib2:
kleing@24332:   "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
kleing@24332: proof -
kleing@24332:   have "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
kleing@24332:     by (rule conj_xor_distrib)
kleing@24332:   thus "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
huffman@24357:     by (simp only: conj_commute)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemmas conj_xor_distribs =
kleing@24332:    conj_xor_distrib conj_xor_distrib2
kleing@24332: 
kleing@24332: end
kleing@24332: 
kleing@24332: end




