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