src/HOL/Library/Boolean_Algebra.thy
author wenzelm
Thu Aug 06 14:29:05 2015 +0200 (2015-08-06)
changeset 60855 6449ae4b85f9
parent 60500 903bb1495239
child 61605 1bf7b186542e
permissions -rw-r--r--
tuned;
     1 (*  Title:      HOL/Library/Boolean_Algebra.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 section \<open>Boolean Algebras\<close>
     6 
     7 theory Boolean_Algebra
     8 imports Main
     9 begin
    10 
    11 locale boolean =
    12   fixes conj :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "\<sqinter>" 70)
    13   fixes disj :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "\<squnion>" 65)
    14   fixes compl :: "'a \<Rightarrow> 'a" ("\<sim> _" [81] 80)
    15   fixes zero :: "'a" ("\<zero>")
    16   fixes one  :: "'a" ("\<one>")
    17   assumes conj_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
    18   assumes disj_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
    19   assumes conj_commute: "x \<sqinter> y = y \<sqinter> x"
    20   assumes disj_commute: "x \<squnion> y = y \<squnion> x"
    21   assumes conj_disj_distrib: "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
    22   assumes disj_conj_distrib: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
    23   assumes conj_one_right [simp]: "x \<sqinter> \<one> = x"
    24   assumes disj_zero_right [simp]: "x \<squnion> \<zero> = x"
    25   assumes conj_cancel_right [simp]: "x \<sqinter> \<sim> x = \<zero>"
    26   assumes disj_cancel_right [simp]: "x \<squnion> \<sim> x = \<one>"
    27 begin
    28 
    29 sublocale conj!: abel_semigroup conj
    30   by standard (fact conj_assoc conj_commute)+
    31 
    32 sublocale disj!: abel_semigroup disj
    33   by standard (fact disj_assoc disj_commute)+
    34 
    35 lemmas conj_left_commute = conj.left_commute
    36 
    37 lemmas disj_left_commute = disj.left_commute
    38 
    39 lemmas conj_ac = conj.assoc conj.commute conj.left_commute
    40 lemmas disj_ac = disj.assoc disj.commute disj.left_commute
    41 
    42 lemma dual: "boolean disj conj compl one zero"
    43 apply (rule boolean.intro)
    44 apply (rule disj_assoc)
    45 apply (rule conj_assoc)
    46 apply (rule disj_commute)
    47 apply (rule conj_commute)
    48 apply (rule disj_conj_distrib)
    49 apply (rule conj_disj_distrib)
    50 apply (rule disj_zero_right)
    51 apply (rule conj_one_right)
    52 apply (rule disj_cancel_right)
    53 apply (rule conj_cancel_right)
    54 done
    55 
    56 
    57 subsection \<open>Complement\<close>
    58 
    59 lemma complement_unique:
    60   assumes 1: "a \<sqinter> x = \<zero>"
    61   assumes 2: "a \<squnion> x = \<one>"
    62   assumes 3: "a \<sqinter> y = \<zero>"
    63   assumes 4: "a \<squnion> y = \<one>"
    64   shows "x = y"
    65 proof -
    66   have "(a \<sqinter> x) \<squnion> (x \<sqinter> y) = (a \<sqinter> y) \<squnion> (x \<sqinter> y)" using 1 3 by simp
    67   hence "(x \<sqinter> a) \<squnion> (x \<sqinter> y) = (y \<sqinter> a) \<squnion> (y \<sqinter> x)" using conj_commute by simp
    68   hence "x \<sqinter> (a \<squnion> y) = y \<sqinter> (a \<squnion> x)" using conj_disj_distrib by simp
    69   hence "x \<sqinter> \<one> = y \<sqinter> \<one>" using 2 4 by simp
    70   thus "x = y" using conj_one_right by simp
    71 qed
    72 
    73 lemma compl_unique: "\<lbrakk>x \<sqinter> y = \<zero>; x \<squnion> y = \<one>\<rbrakk> \<Longrightarrow> \<sim> x = y"
    74 by (rule complement_unique [OF conj_cancel_right disj_cancel_right])
    75 
    76 lemma double_compl [simp]: "\<sim> (\<sim> x) = x"
    77 proof (rule compl_unique)
    78   from conj_cancel_right show "\<sim> x \<sqinter> x = \<zero>" by (simp only: conj_commute)
    79   from disj_cancel_right show "\<sim> x \<squnion> x = \<one>" by (simp only: disj_commute)
    80 qed
    81 
    82 lemma compl_eq_compl_iff [simp]: "(\<sim> x = \<sim> y) = (x = y)"
    83 by (rule inj_eq [OF inj_on_inverseI], rule double_compl)
    84 
    85 
    86 subsection \<open>Conjunction\<close>
    87 
    88 lemma conj_absorb [simp]: "x \<sqinter> x = x"
    89 proof -
    90   have "x \<sqinter> x = (x \<sqinter> x) \<squnion> \<zero>" using disj_zero_right by simp
    91   also have "... = (x \<sqinter> x) \<squnion> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
    92   also have "... = x \<sqinter> (x \<squnion> \<sim> x)" using conj_disj_distrib by (simp only:)
    93   also have "... = x \<sqinter> \<one>" using disj_cancel_right by simp
    94   also have "... = x" using conj_one_right by simp
    95   finally show ?thesis .
    96 qed
    97 
    98 lemma conj_zero_right [simp]: "x \<sqinter> \<zero> = \<zero>"
    99 proof -
   100   have "x \<sqinter> \<zero> = x \<sqinter> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
   101   also have "... = (x \<sqinter> x) \<sqinter> \<sim> x" using conj_assoc by (simp only:)
   102   also have "... = x \<sqinter> \<sim> x" using conj_absorb by simp
   103   also have "... = \<zero>" using conj_cancel_right by simp
   104   finally show ?thesis .
   105 qed
   106 
   107 lemma compl_one [simp]: "\<sim> \<one> = \<zero>"
   108 by (rule compl_unique [OF conj_zero_right disj_zero_right])
   109 
   110 lemma conj_zero_left [simp]: "\<zero> \<sqinter> x = \<zero>"
   111 by (subst conj_commute) (rule conj_zero_right)
   112 
   113 lemma conj_one_left [simp]: "\<one> \<sqinter> x = x"
   114 by (subst conj_commute) (rule conj_one_right)
   115 
   116 lemma conj_cancel_left [simp]: "\<sim> x \<sqinter> x = \<zero>"
   117 by (subst conj_commute) (rule conj_cancel_right)
   118 
   119 lemma conj_left_absorb [simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
   120 by (simp only: conj_assoc [symmetric] conj_absorb)
   121 
   122 lemma conj_disj_distrib2:
   123   "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
   124 by (simp only: conj_commute conj_disj_distrib)
   125 
   126 lemmas conj_disj_distribs =
   127    conj_disj_distrib conj_disj_distrib2
   128 
   129 
   130 subsection \<open>Disjunction\<close>
   131 
   132 lemma disj_absorb [simp]: "x \<squnion> x = x"
   133 by (rule boolean.conj_absorb [OF dual])
   134 
   135 lemma disj_one_right [simp]: "x \<squnion> \<one> = \<one>"
   136 by (rule boolean.conj_zero_right [OF dual])
   137 
   138 lemma compl_zero [simp]: "\<sim> \<zero> = \<one>"
   139 by (rule boolean.compl_one [OF dual])
   140 
   141 lemma disj_zero_left [simp]: "\<zero> \<squnion> x = x"
   142 by (rule boolean.conj_one_left [OF dual])
   143 
   144 lemma disj_one_left [simp]: "\<one> \<squnion> x = \<one>"
   145 by (rule boolean.conj_zero_left [OF dual])
   146 
   147 lemma disj_cancel_left [simp]: "\<sim> x \<squnion> x = \<one>"
   148 by (rule boolean.conj_cancel_left [OF dual])
   149 
   150 lemma disj_left_absorb [simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
   151 by (rule boolean.conj_left_absorb [OF dual])
   152 
   153 lemma disj_conj_distrib2:
   154   "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
   155 by (rule boolean.conj_disj_distrib2 [OF dual])
   156 
   157 lemmas disj_conj_distribs =
   158    disj_conj_distrib disj_conj_distrib2
   159 
   160 
   161 subsection \<open>De Morgan's Laws\<close>
   162 
   163 lemma de_Morgan_conj [simp]: "\<sim> (x \<sqinter> y) = \<sim> x \<squnion> \<sim> y"
   164 proof (rule compl_unique)
   165   have "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = ((x \<sqinter> y) \<sqinter> \<sim> x) \<squnion> ((x \<sqinter> y) \<sqinter> \<sim> y)"
   166     by (rule conj_disj_distrib)
   167   also have "... = (y \<sqinter> (x \<sqinter> \<sim> x)) \<squnion> (x \<sqinter> (y \<sqinter> \<sim> y))"
   168     by (simp only: conj_ac)
   169   finally show "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = \<zero>"
   170     by (simp only: conj_cancel_right conj_zero_right disj_zero_right)
   171 next
   172   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))"
   173     by (rule disj_conj_distrib2)
   174   also have "... = (\<sim> y \<squnion> (x \<squnion> \<sim> x)) \<sqinter> (\<sim> x \<squnion> (y \<squnion> \<sim> y))"
   175     by (simp only: disj_ac)
   176   finally show "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = \<one>"
   177     by (simp only: disj_cancel_right disj_one_right conj_one_right)
   178 qed
   179 
   180 lemma de_Morgan_disj [simp]: "\<sim> (x \<squnion> y) = \<sim> x \<sqinter> \<sim> y"
   181 by (rule boolean.de_Morgan_conj [OF dual])
   182 
   183 end
   184 
   185 
   186 subsection \<open>Symmetric Difference\<close>
   187 
   188 locale boolean_xor = boolean +
   189   fixes xor :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "\<oplus>" 65)
   190   assumes xor_def: "x \<oplus> y = (x \<sqinter> \<sim> y) \<squnion> (\<sim> x \<sqinter> y)"
   191 begin
   192 
   193 sublocale xor!: abel_semigroup xor
   194 proof
   195   fix x y z :: 'a
   196   let ?t = "(x \<sqinter> y \<sqinter> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> \<sim> z) \<squnion>
   197             (\<sim> x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (\<sim> x \<sqinter> \<sim> y \<sqinter> z)"
   198   have "?t \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> y \<sqinter> \<sim> y) =
   199         ?t \<squnion> (x \<sqinter> y \<sqinter> \<sim> y) \<squnion> (x \<sqinter> z \<sqinter> \<sim> z)"
   200     by (simp only: conj_cancel_right conj_zero_right)
   201   thus "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
   202     apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   203     apply (simp only: conj_disj_distribs conj_ac disj_ac)
   204     done
   205   show "x \<oplus> y = y \<oplus> x"
   206     by (simp only: xor_def conj_commute disj_commute)
   207 qed
   208 
   209 lemmas xor_assoc = xor.assoc
   210 lemmas xor_commute = xor.commute
   211 lemmas xor_left_commute = xor.left_commute
   212 
   213 lemmas xor_ac = xor.assoc xor.commute xor.left_commute
   214 
   215 lemma xor_def2:
   216   "x \<oplus> y = (x \<squnion> y) \<sqinter> (\<sim> x \<squnion> \<sim> y)"
   217 by (simp only: xor_def conj_disj_distribs
   218                disj_ac conj_ac conj_cancel_right disj_zero_left)
   219 
   220 lemma xor_zero_right [simp]: "x \<oplus> \<zero> = x"
   221 by (simp only: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)
   222 
   223 lemma xor_zero_left [simp]: "\<zero> \<oplus> x = x"
   224 by (subst xor_commute) (rule xor_zero_right)
   225 
   226 lemma xor_one_right [simp]: "x \<oplus> \<one> = \<sim> x"
   227 by (simp only: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)
   228 
   229 lemma xor_one_left [simp]: "\<one> \<oplus> x = \<sim> x"
   230 by (subst xor_commute) (rule xor_one_right)
   231 
   232 lemma xor_self [simp]: "x \<oplus> x = \<zero>"
   233 by (simp only: xor_def conj_cancel_right conj_cancel_left disj_zero_right)
   234 
   235 lemma xor_left_self [simp]: "x \<oplus> (x \<oplus> y) = y"
   236 by (simp only: xor_assoc [symmetric] xor_self xor_zero_left)
   237 
   238 lemma xor_compl_left [simp]: "\<sim> x \<oplus> y = \<sim> (x \<oplus> y)"
   239 apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   240 apply (simp only: conj_disj_distribs)
   241 apply (simp only: conj_cancel_right conj_cancel_left)
   242 apply (simp only: disj_zero_left disj_zero_right)
   243 apply (simp only: disj_ac conj_ac)
   244 done
   245 
   246 lemma xor_compl_right [simp]: "x \<oplus> \<sim> y = \<sim> (x \<oplus> y)"
   247 apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   248 apply (simp only: conj_disj_distribs)
   249 apply (simp only: conj_cancel_right conj_cancel_left)
   250 apply (simp only: disj_zero_left disj_zero_right)
   251 apply (simp only: disj_ac conj_ac)
   252 done
   253 
   254 lemma xor_cancel_right: "x \<oplus> \<sim> x = \<one>"
   255 by (simp only: xor_compl_right xor_self compl_zero)
   256 
   257 lemma xor_cancel_left: "\<sim> x \<oplus> x = \<one>"
   258 by (simp only: xor_compl_left xor_self compl_zero)
   259 
   260 lemma conj_xor_distrib: "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   261 proof -
   262   have "(x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z) =
   263         (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)"
   264     by (simp only: conj_cancel_right conj_zero_right disj_zero_left)
   265   thus "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   266     by (simp (no_asm_use) only:
   267         xor_def de_Morgan_disj de_Morgan_conj double_compl
   268         conj_disj_distribs conj_ac disj_ac)
   269 qed
   270 
   271 lemma conj_xor_distrib2: "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   272 proof -
   273   have "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   274     by (rule conj_xor_distrib)
   275   thus "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   276     by (simp only: conj_commute)
   277 qed
   278 
   279 lemmas conj_xor_distribs = conj_xor_distrib conj_xor_distrib2
   280 
   281 end
   282 
   283 end