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