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