src/HOL/Library/Boolean_Algebra.thy
author wenzelm
Wed Mar 08 10:50:59 2017 +0100 (2017-03-08)
changeset 65151 a7394aa4d21c
parent 63462 c1fe30f2bc32
child 65343 0a8e30a7b10e
permissions -rw-r--r--
tuned proofs;
     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)"
    67     using 1 3 by simp
    68   then have "(x \<sqinter> a) \<squnion> (x \<sqinter> y) = (y \<sqinter> a) \<squnion> (y \<sqinter> x)"
    69     using conj_commute by simp
    70   then have "x \<sqinter> (a \<squnion> y) = y \<sqinter> (a \<squnion> x)"
    71     using conj_disj_distrib by simp
    72   then have "x \<sqinter> \<one> = y \<sqinter> \<one>"
    73     using 2 4 by simp
    74   then show "x = y"
    75     using conj_one_right by simp
    76 qed
    77 
    78 lemma compl_unique: "x \<sqinter> y = \<zero> \<Longrightarrow> x \<squnion> y = \<one> \<Longrightarrow> \<sim> x = y"
    79   by (rule complement_unique [OF conj_cancel_right disj_cancel_right])
    80 
    81 lemma double_compl [simp]: "\<sim> (\<sim> x) = x"
    82 proof (rule compl_unique)
    83   from conj_cancel_right show "\<sim> x \<sqinter> x = \<zero>"
    84     by (simp only: conj_commute)
    85   from disj_cancel_right show "\<sim> x \<squnion> x = \<one>"
    86     by (simp only: disj_commute)
    87 qed
    88 
    89 lemma compl_eq_compl_iff [simp]: "\<sim> x = \<sim> y \<longleftrightarrow> x = y"
    90   by (rule inj_eq [OF inj_on_inverseI]) (rule double_compl)
    91 
    92 
    93 subsection \<open>Conjunction\<close>
    94 
    95 lemma conj_absorb [simp]: "x \<sqinter> x = x"
    96 proof -
    97   have "x \<sqinter> x = (x \<sqinter> x) \<squnion> \<zero>"
    98     using disj_zero_right by simp
    99   also have "... = (x \<sqinter> x) \<squnion> (x \<sqinter> \<sim> x)"
   100     using conj_cancel_right by simp
   101   also have "... = x \<sqinter> (x \<squnion> \<sim> x)"
   102     using conj_disj_distrib by (simp only:)
   103   also have "... = x \<sqinter> \<one>"
   104     using disj_cancel_right by simp
   105   also have "... = x"
   106     using conj_one_right by simp
   107   finally show ?thesis .
   108 qed
   109 
   110 lemma conj_zero_right [simp]: "x \<sqinter> \<zero> = \<zero>"
   111 proof -
   112   have "x \<sqinter> \<zero> = x \<sqinter> (x \<sqinter> \<sim> x)"
   113     using conj_cancel_right by simp
   114   also have "... = (x \<sqinter> x) \<sqinter> \<sim> x"
   115     using conj_assoc by (simp only:)
   116   also have "... = x \<sqinter> \<sim> x"
   117     using conj_absorb by simp
   118   also have "... = \<zero>"
   119     using conj_cancel_right by simp
   120   finally show ?thesis .
   121 qed
   122 
   123 lemma compl_one [simp]: "\<sim> \<one> = \<zero>"
   124   by (rule compl_unique [OF conj_zero_right disj_zero_right])
   125 
   126 lemma conj_zero_left [simp]: "\<zero> \<sqinter> x = \<zero>"
   127   by (subst conj_commute) (rule conj_zero_right)
   128 
   129 lemma conj_one_left [simp]: "\<one> \<sqinter> x = x"
   130   by (subst conj_commute) (rule conj_one_right)
   131 
   132 lemma conj_cancel_left [simp]: "\<sim> x \<sqinter> x = \<zero>"
   133   by (subst conj_commute) (rule conj_cancel_right)
   134 
   135 lemma conj_left_absorb [simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
   136   by (simp only: conj_assoc [symmetric] conj_absorb)
   137 
   138 lemma conj_disj_distrib2: "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
   139   by (simp only: conj_commute conj_disj_distrib)
   140 
   141 lemmas conj_disj_distribs = conj_disj_distrib conj_disj_distrib2
   142 
   143 
   144 subsection \<open>Disjunction\<close>
   145 
   146 lemma disj_absorb [simp]: "x \<squnion> x = x"
   147   by (rule boolean.conj_absorb [OF dual])
   148 
   149 lemma disj_one_right [simp]: "x \<squnion> \<one> = \<one>"
   150   by (rule boolean.conj_zero_right [OF dual])
   151 
   152 lemma compl_zero [simp]: "\<sim> \<zero> = \<one>"
   153   by (rule boolean.compl_one [OF dual])
   154 
   155 lemma disj_zero_left [simp]: "\<zero> \<squnion> x = x"
   156   by (rule boolean.conj_one_left [OF dual])
   157 
   158 lemma disj_one_left [simp]: "\<one> \<squnion> x = \<one>"
   159   by (rule boolean.conj_zero_left [OF dual])
   160 
   161 lemma disj_cancel_left [simp]: "\<sim> x \<squnion> x = \<one>"
   162   by (rule boolean.conj_cancel_left [OF dual])
   163 
   164 lemma disj_left_absorb [simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
   165   by (rule boolean.conj_left_absorb [OF dual])
   166 
   167 lemma disj_conj_distrib2: "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
   168   by (rule boolean.conj_disj_distrib2 [OF dual])
   169 
   170 lemmas disj_conj_distribs = disj_conj_distrib disj_conj_distrib2
   171 
   172 
   173 subsection \<open>De Morgan's Laws\<close>
   174 
   175 lemma de_Morgan_conj [simp]: "\<sim> (x \<sqinter> y) = \<sim> x \<squnion> \<sim> y"
   176 proof (rule compl_unique)
   177   have "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = ((x \<sqinter> y) \<sqinter> \<sim> x) \<squnion> ((x \<sqinter> y) \<sqinter> \<sim> y)"
   178     by (rule conj_disj_distrib)
   179   also have "... = (y \<sqinter> (x \<sqinter> \<sim> x)) \<squnion> (x \<sqinter> (y \<sqinter> \<sim> y))"
   180     by (simp only: conj_ac)
   181   finally show "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = \<zero>"
   182     by (simp only: conj_cancel_right conj_zero_right disj_zero_right)
   183 next
   184   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))"
   185     by (rule disj_conj_distrib2)
   186   also have "... = (\<sim> y \<squnion> (x \<squnion> \<sim> x)) \<sqinter> (\<sim> x \<squnion> (y \<squnion> \<sim> y))"
   187     by (simp only: disj_ac)
   188   finally show "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = \<one>"
   189     by (simp only: disj_cancel_right disj_one_right conj_one_right)
   190 qed
   191 
   192 lemma de_Morgan_disj [simp]: "\<sim> (x \<squnion> y) = \<sim> x \<sqinter> \<sim> y"
   193   by (rule boolean.de_Morgan_conj [OF dual])
   194 
   195 end
   196 
   197 
   198 subsection \<open>Symmetric Difference\<close>
   199 
   200 locale boolean_xor = boolean +
   201   fixes xor :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "\<oplus>" 65)
   202   assumes xor_def: "x \<oplus> y = (x \<sqinter> \<sim> y) \<squnion> (\<sim> x \<sqinter> y)"
   203 begin
   204 
   205 sublocale xor: abel_semigroup xor
   206 proof
   207   fix x y z :: 'a
   208   let ?t = "(x \<sqinter> y \<sqinter> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> \<sim> z) \<squnion>
   209             (\<sim> x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (\<sim> x \<sqinter> \<sim> y \<sqinter> z)"
   210   have "?t \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> y \<sqinter> \<sim> y) =
   211         ?t \<squnion> (x \<sqinter> y \<sqinter> \<sim> y) \<squnion> (x \<sqinter> z \<sqinter> \<sim> z)"
   212     by (simp only: conj_cancel_right conj_zero_right)
   213   then show "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
   214     apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   215     apply (simp only: conj_disj_distribs conj_ac disj_ac)
   216     done
   217   show "x \<oplus> y = y \<oplus> x"
   218     by (simp only: xor_def conj_commute disj_commute)
   219 qed
   220 
   221 lemmas xor_assoc = xor.assoc
   222 lemmas xor_commute = xor.commute
   223 lemmas xor_left_commute = xor.left_commute
   224 
   225 lemmas xor_ac = xor.assoc xor.commute xor.left_commute
   226 
   227 lemma xor_def2: "x \<oplus> y = (x \<squnion> y) \<sqinter> (\<sim> x \<squnion> \<sim> y)"
   228   by (simp only: xor_def conj_disj_distribs disj_ac conj_ac conj_cancel_right disj_zero_left)
   229 
   230 lemma xor_zero_right [simp]: "x \<oplus> \<zero> = x"
   231   by (simp only: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)
   232 
   233 lemma xor_zero_left [simp]: "\<zero> \<oplus> x = x"
   234   by (subst xor_commute) (rule xor_zero_right)
   235 
   236 lemma xor_one_right [simp]: "x \<oplus> \<one> = \<sim> x"
   237   by (simp only: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)
   238 
   239 lemma xor_one_left [simp]: "\<one> \<oplus> x = \<sim> x"
   240   by (subst xor_commute) (rule xor_one_right)
   241 
   242 lemma xor_self [simp]: "x \<oplus> x = \<zero>"
   243   by (simp only: xor_def conj_cancel_right conj_cancel_left disj_zero_right)
   244 
   245 lemma xor_left_self [simp]: "x \<oplus> (x \<oplus> y) = y"
   246   by (simp only: xor_assoc [symmetric] xor_self xor_zero_left)
   247 
   248 lemma xor_compl_left [simp]: "\<sim> x \<oplus> y = \<sim> (x \<oplus> y)"
   249   apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   250   apply (simp only: conj_disj_distribs)
   251   apply (simp only: conj_cancel_right conj_cancel_left)
   252   apply (simp only: disj_zero_left disj_zero_right)
   253   apply (simp only: disj_ac conj_ac)
   254   done
   255 
   256 lemma xor_compl_right [simp]: "x \<oplus> \<sim> y = \<sim> (x \<oplus> y)"
   257   apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   258   apply (simp only: conj_disj_distribs)
   259   apply (simp only: conj_cancel_right conj_cancel_left)
   260   apply (simp only: disj_zero_left disj_zero_right)
   261   apply (simp only: disj_ac conj_ac)
   262   done
   263 
   264 lemma xor_cancel_right: "x \<oplus> \<sim> x = \<one>"
   265   by (simp only: xor_compl_right xor_self compl_zero)
   266 
   267 lemma xor_cancel_left: "\<sim> x \<oplus> x = \<one>"
   268   by (simp only: xor_compl_left xor_self compl_zero)
   269 
   270 lemma conj_xor_distrib: "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   271 proof -
   272   have *: "(x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z) =
   273         (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)"
   274     by (simp only: conj_cancel_right conj_zero_right disj_zero_left)
   275   then show "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   276     by (simp (no_asm_use) only:
   277         xor_def de_Morgan_disj de_Morgan_conj double_compl
   278         conj_disj_distribs conj_ac disj_ac)
   279 qed
   280 
   281 lemma conj_xor_distrib2: "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   282 proof -
   283   have "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   284     by (rule conj_xor_distrib)
   285   then show "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   286     by (simp only: conj_commute)
   287 qed
   288 
   289 lemmas conj_xor_distribs = conj_xor_distrib conj_xor_distrib2
   290 
   291 end
   292 
   293 end