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