src/HOL/Library/Boolean_Algebra.thy
 author hoelzl Tue Jul 19 14:37:49 2011 +0200 (2011-07-19) changeset 43922 c6f35921056e parent 34973 ae634fad947e child 54868 bab6cade3cc5 permissions -rw-r--r--
```     1 (*  Title:      HOL/Library/Boolean_Algebra.thy
```
```     2     Author:     Brian Huffman
```
```     3 *)
```
```     4
```
```     5 header {* Boolean Algebras *}
```
```     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
```
```    28 sublocale boolean < conj!: abel_semigroup conj proof
```
```    29 qed (fact conj_assoc conj_commute)+
```
```    30
```
```    31 sublocale boolean < disj!: abel_semigroup disj proof
```
```    32 qed (fact disj_assoc disj_commute)+
```
```    33
```
```    34 context boolean
```
```    35 begin
```
```    36
```
```    37 lemmas conj_left_commute = conj.left_commute
```
```    38
```
```    39 lemmas disj_left_commute = disj.left_commute
```
```    40
```
```    41 lemmas conj_ac = conj.assoc conj.commute conj.left_commute
```
```    42 lemmas disj_ac = disj.assoc disj.commute disj.left_commute
```
```    43
```
```    44 lemma dual: "boolean disj conj compl one zero"
```
```    45 apply (rule boolean.intro)
```
```    46 apply (rule disj_assoc)
```
```    47 apply (rule conj_assoc)
```
```    48 apply (rule disj_commute)
```
```    49 apply (rule conj_commute)
```
```    50 apply (rule disj_conj_distrib)
```
```    51 apply (rule conj_disj_distrib)
```
```    52 apply (rule disj_zero_right)
```
```    53 apply (rule conj_one_right)
```
```    54 apply (rule disj_cancel_right)
```
```    55 apply (rule conj_cancel_right)
```
```    56 done
```
```    57
```
```    58 subsection {* Complement *}
```
```    59
```
```    60 lemma complement_unique:
```
```    61   assumes 1: "a \<sqinter> x = \<zero>"
```
```    62   assumes 2: "a \<squnion> x = \<one>"
```
```    63   assumes 3: "a \<sqinter> y = \<zero>"
```
```    64   assumes 4: "a \<squnion> y = \<one>"
```
```    65   shows "x = y"
```
```    66 proof -
```
```    67   have "(a \<sqinter> x) \<squnion> (x \<sqinter> y) = (a \<sqinter> y) \<squnion> (x \<sqinter> y)" using 1 3 by simp
```
```    68   hence "(x \<sqinter> a) \<squnion> (x \<sqinter> y) = (y \<sqinter> a) \<squnion> (y \<sqinter> x)" using conj_commute by simp
```
```    69   hence "x \<sqinter> (a \<squnion> y) = y \<sqinter> (a \<squnion> x)" using conj_disj_distrib by simp
```
```    70   hence "x \<sqinter> \<one> = y \<sqinter> \<one>" using 2 4 by simp
```
```    71   thus "x = y" using conj_one_right by simp
```
```    72 qed
```
```    73
```
```    74 lemma compl_unique: "\<lbrakk>x \<sqinter> y = \<zero>; x \<squnion> y = \<one>\<rbrakk> \<Longrightarrow> \<sim> x = y"
```
```    75 by (rule complement_unique [OF conj_cancel_right disj_cancel_right])
```
```    76
```
```    77 lemma double_compl [simp]: "\<sim> (\<sim> x) = x"
```
```    78 proof (rule compl_unique)
```
```    79   from conj_cancel_right show "\<sim> x \<sqinter> x = \<zero>" by (simp only: conj_commute)
```
```    80   from disj_cancel_right show "\<sim> x \<squnion> x = \<one>" by (simp only: disj_commute)
```
```    81 qed
```
```    82
```
```    83 lemma compl_eq_compl_iff [simp]: "(\<sim> x = \<sim> y) = (x = y)"
```
```    84 by (rule inj_eq [OF inj_on_inverseI], rule double_compl)
```
```    85
```
```    86 subsection {* Conjunction *}
```
```    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 subsection {* Disjunction *}
```
```   130
```
```   131 lemma disj_absorb [simp]: "x \<squnion> x = x"
```
```   132 by (rule boolean.conj_absorb [OF dual])
```
```   133
```
```   134 lemma disj_one_right [simp]: "x \<squnion> \<one> = \<one>"
```
```   135 by (rule boolean.conj_zero_right [OF dual])
```
```   136
```
```   137 lemma compl_zero [simp]: "\<sim> \<zero> = \<one>"
```
```   138 by (rule boolean.compl_one [OF dual])
```
```   139
```
```   140 lemma disj_zero_left [simp]: "\<zero> \<squnion> x = x"
```
```   141 by (rule boolean.conj_one_left [OF dual])
```
```   142
```
```   143 lemma disj_one_left [simp]: "\<one> \<squnion> x = \<one>"
```
```   144 by (rule boolean.conj_zero_left [OF dual])
```
```   145
```
```   146 lemma disj_cancel_left [simp]: "\<sim> x \<squnion> x = \<one>"
```
```   147 by (rule boolean.conj_cancel_left [OF dual])
```
```   148
```
```   149 lemma disj_left_absorb [simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
```
```   150 by (rule boolean.conj_left_absorb [OF dual])
```
```   151
```
```   152 lemma disj_conj_distrib2:
```
```   153   "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
```
```   154 by (rule boolean.conj_disj_distrib2 [OF dual])
```
```   155
```
```   156 lemmas disj_conj_distribs =
```
```   157    disj_conj_distrib disj_conj_distrib2
```
```   158
```
```   159 subsection {* De Morgan's Laws *}
```
```   160
```
```   161 lemma de_Morgan_conj [simp]: "\<sim> (x \<sqinter> y) = \<sim> x \<squnion> \<sim> y"
```
```   162 proof (rule compl_unique)
```
```   163   have "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = ((x \<sqinter> y) \<sqinter> \<sim> x) \<squnion> ((x \<sqinter> y) \<sqinter> \<sim> y)"
```
```   164     by (rule conj_disj_distrib)
```
```   165   also have "... = (y \<sqinter> (x \<sqinter> \<sim> x)) \<squnion> (x \<sqinter> (y \<sqinter> \<sim> y))"
```
```   166     by (simp only: conj_ac)
```
```   167   finally show "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = \<zero>"
```
```   168     by (simp only: conj_cancel_right conj_zero_right disj_zero_right)
```
```   169 next
```
```   170   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))"
```
```   171     by (rule disj_conj_distrib2)
```
```   172   also have "... = (\<sim> y \<squnion> (x \<squnion> \<sim> x)) \<sqinter> (\<sim> x \<squnion> (y \<squnion> \<sim> y))"
```
```   173     by (simp only: disj_ac)
```
```   174   finally show "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = \<one>"
```
```   175     by (simp only: disj_cancel_right disj_one_right conj_one_right)
```
```   176 qed
```
```   177
```
```   178 lemma de_Morgan_disj [simp]: "\<sim> (x \<squnion> y) = \<sim> x \<sqinter> \<sim> y"
```
```   179 by (rule boolean.de_Morgan_conj [OF dual])
```
```   180
```
```   181 end
```
```   182
```
```   183 subsection {* Symmetric Difference *}
```
```   184
```
```   185 locale boolean_xor = boolean +
```
```   186   fixes xor :: "'a => 'a => 'a"  (infixr "\<oplus>" 65)
```
```   187   assumes xor_def: "x \<oplus> y = (x \<sqinter> \<sim> y) \<squnion> (\<sim> x \<sqinter> y)"
```
```   188
```
```   189 sublocale boolean_xor < xor!: abel_semigroup xor proof
```
```   190   fix x y z :: 'a
```
```   191   let ?t = "(x \<sqinter> y \<sqinter> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> \<sim> z) \<squnion>
```
```   192             (\<sim> x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (\<sim> x \<sqinter> \<sim> y \<sqinter> z)"
```
```   193   have "?t \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> y \<sqinter> \<sim> y) =
```
```   194         ?t \<squnion> (x \<sqinter> y \<sqinter> \<sim> y) \<squnion> (x \<sqinter> z \<sqinter> \<sim> z)"
```
```   195     by (simp only: conj_cancel_right conj_zero_right)
```
```   196   thus "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
```
```   197     apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
```
```   198     apply (simp only: conj_disj_distribs conj_ac disj_ac)
```
```   199     done
```
```   200   show "x \<oplus> y = y \<oplus> x"
```
```   201     by (simp only: xor_def conj_commute disj_commute)
```
```   202 qed
```
```   203
```
```   204 context boolean_xor
```
```   205 begin
```
```   206
```
```   207 lemmas xor_assoc = xor.assoc
```
```   208 lemmas xor_commute = xor.commute
```
```   209 lemmas xor_left_commute = xor.left_commute
```
```   210
```
```   211 lemmas xor_ac = xor.assoc xor.commute xor.left_commute
```
```   212
```
```   213 lemma xor_def2:
```
```   214   "x \<oplus> y = (x \<squnion> y) \<sqinter> (\<sim> x \<squnion> \<sim> y)"
```
```   215 by (simp only: xor_def conj_disj_distribs
```
```   216                disj_ac conj_ac conj_cancel_right disj_zero_left)
```
```   217
```
```   218 lemma xor_zero_right [simp]: "x \<oplus> \<zero> = x"
```
```   219 by (simp only: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)
```
```   220
```
```   221 lemma xor_zero_left [simp]: "\<zero> \<oplus> x = x"
```
```   222 by (subst xor_commute) (rule xor_zero_right)
```
```   223
```
```   224 lemma xor_one_right [simp]: "x \<oplus> \<one> = \<sim> x"
```
```   225 by (simp only: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)
```
```   226
```
```   227 lemma xor_one_left [simp]: "\<one> \<oplus> x = \<sim> x"
```
```   228 by (subst xor_commute) (rule xor_one_right)
```
```   229
```
```   230 lemma xor_self [simp]: "x \<oplus> x = \<zero>"
```
```   231 by (simp only: xor_def conj_cancel_right conj_cancel_left disj_zero_right)
```
```   232
```
```   233 lemma xor_left_self [simp]: "x \<oplus> (x \<oplus> y) = y"
```
```   234 by (simp only: xor_assoc [symmetric] xor_self xor_zero_left)
```
```   235
```
```   236 lemma xor_compl_left [simp]: "\<sim> x \<oplus> y = \<sim> (x \<oplus> y)"
```
```   237 apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
```
```   238 apply (simp only: conj_disj_distribs)
```
```   239 apply (simp only: conj_cancel_right conj_cancel_left)
```
```   240 apply (simp only: disj_zero_left disj_zero_right)
```
```   241 apply (simp only: disj_ac conj_ac)
```
```   242 done
```
```   243
```
```   244 lemma xor_compl_right [simp]: "x \<oplus> \<sim> y = \<sim> (x \<oplus> y)"
```
```   245 apply (simp only: xor_def de_Morgan_disj de_Morgan_conj double_compl)
```
```   246 apply (simp only: conj_disj_distribs)
```
```   247 apply (simp only: conj_cancel_right conj_cancel_left)
```
```   248 apply (simp only: disj_zero_left disj_zero_right)
```
```   249 apply (simp only: disj_ac conj_ac)
```
```   250 done
```
```   251
```
```   252 lemma xor_cancel_right: "x \<oplus> \<sim> x = \<one>"
```
```   253 by (simp only: xor_compl_right xor_self compl_zero)
```
```   254
```
```   255 lemma xor_cancel_left: "\<sim> x \<oplus> x = \<one>"
```
```   256 by (simp only: xor_compl_left xor_self compl_zero)
```
```   257
```
```   258 lemma conj_xor_distrib: "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
```
```   259 proof -
```
```   260   have "(x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z) =
```
```   261         (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)"
```
```   262     by (simp only: conj_cancel_right conj_zero_right disj_zero_left)
```
```   263   thus "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
```
```   264     by (simp (no_asm_use) only:
```
```   265         xor_def de_Morgan_disj de_Morgan_conj double_compl
```
```   266         conj_disj_distribs conj_ac disj_ac)
```
```   267 qed
```
```   268
```
```   269 lemma conj_xor_distrib2:
```
```   270   "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
```
```   271 proof -
```
```   272   have "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
```
```   273     by (rule conj_xor_distrib)
```
```   274   thus "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
```
```   275     by (simp only: conj_commute)
```
```   276 qed
```
```   277
```
```   278 lemmas conj_xor_distribs =
```
```   279    conj_xor_distrib conj_xor_distrib2
```
```   280
```
```   281 end
```
```   282
```
```   283 end
```