src/HOL/Library/Boolean_Algebra.thy
changeset 24332 e3a2b75b1cf9
child 24357 d42cf77da51f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Boolean_Algebra.thy	Mon Aug 20 00:22:18 2007 +0200
     1.3 @@ -0,0 +1,276 @@
     1.4 +(* 
     1.5 +  ID:     $Id$
     1.6 +  Author: Brian Huffman
     1.7 +
     1.8 +  Boolean algebras as locales.
     1.9 +*)
    1.10 +
    1.11 +header {* Boolean Algebras *}
    1.12 +
    1.13 +theory Boolean_Algebra
    1.14 +imports Main
    1.15 +begin
    1.16 +
    1.17 +locale boolean =
    1.18 +  fixes conj :: "'a => 'a => 'a" (infixr "\<sqinter>" 70)
    1.19 +  fixes disj :: "'a => 'a => 'a" (infixr "\<squnion>" 65)
    1.20 +  fixes compl :: "'a => 'a" ("\<sim> _" [81] 80)
    1.21 +  fixes zero :: "'a" ("\<zero>")
    1.22 +  fixes one  :: "'a" ("\<one>")
    1.23 +  assumes conj_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
    1.24 +  assumes disj_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
    1.25 +  assumes conj_commute: "x \<sqinter> y = y \<sqinter> x"
    1.26 +  assumes disj_commute: "x \<squnion> y = y \<squnion> x"
    1.27 +  assumes conj_disj_distrib: "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
    1.28 +  assumes disj_conj_distrib: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
    1.29 +  assumes conj_one_right: "x \<sqinter> \<one> = x"
    1.30 +  assumes disj_zero_right: "x \<squnion> \<zero> = x"
    1.31 +  assumes conj_cancel_right: "x \<sqinter> \<sim> x = \<zero>"
    1.32 +  assumes disj_cancel_right: "x \<squnion> \<sim> x = \<one>"
    1.33 +begin
    1.34 +
    1.35 +lemmas disj_ac =
    1.36 +  disj_assoc disj_commute
    1.37 +  mk_left_commute [of "disj", OF disj_assoc disj_commute]
    1.38 +
    1.39 +lemmas conj_ac =
    1.40 +  conj_assoc conj_commute
    1.41 +  mk_left_commute [of "conj", OF conj_assoc conj_commute]
    1.42 +
    1.43 +lemma dual: "boolean disj conj compl one zero"
    1.44 +apply (rule boolean.intro)
    1.45 +apply (rule disj_assoc)
    1.46 +apply (rule conj_assoc)
    1.47 +apply (rule disj_commute)
    1.48 +apply (rule conj_commute)
    1.49 +apply (rule disj_conj_distrib)
    1.50 +apply (rule conj_disj_distrib)
    1.51 +apply (rule disj_zero_right)
    1.52 +apply (rule conj_one_right)
    1.53 +apply (rule disj_cancel_right)
    1.54 +apply (rule conj_cancel_right)
    1.55 +done
    1.56 +
    1.57 +text {* Complement *}
    1.58 +
    1.59 +lemma complement_unique:
    1.60 +  assumes 1: "a \<sqinter> x = \<zero>"
    1.61 +  assumes 2: "a \<squnion> x = \<one>"
    1.62 +  assumes 3: "a \<sqinter> y = \<zero>"
    1.63 +  assumes 4: "a \<squnion> y = \<one>"
    1.64 +  shows "x = y"
    1.65 +proof -
    1.66 +  have "(a \<sqinter> x) \<squnion> (x \<sqinter> y) = (a \<sqinter> y) \<squnion> (x \<sqinter> y)" using 1 3 by simp
    1.67 +  hence "(x \<sqinter> a) \<squnion> (x \<sqinter> y) = (y \<sqinter> a) \<squnion> (y \<sqinter> x)" using conj_commute by simp
    1.68 +  hence "x \<sqinter> (a \<squnion> y) = y \<sqinter> (a \<squnion> x)" using conj_disj_distrib by simp
    1.69 +  hence "x \<sqinter> \<one> = y \<sqinter> \<one>" using 2 4 by simp
    1.70 +  thus "x = y" using conj_one_right by simp
    1.71 +qed
    1.72 +
    1.73 +lemma compl_unique: "[| x \<sqinter> y = \<zero>; x \<squnion> y = \<one> |] ==> \<sim> x = y"
    1.74 +by (rule complement_unique [OF conj_cancel_right disj_cancel_right])
    1.75 +
    1.76 +lemma double_compl [simp]: "\<sim> (\<sim> x) = x"
    1.77 +proof (rule compl_unique)
    1.78 +  from conj_cancel_right show "\<sim> x \<sqinter> x = \<zero>" by (simp add: conj_commute)
    1.79 +  from disj_cancel_right show "\<sim> x \<squnion> x = \<one>" by (simp add: disj_commute)
    1.80 +qed
    1.81 +
    1.82 +lemma compl_eq_compl_iff [simp]: "(\<sim> x = \<sim> y) = (x = y)"
    1.83 +by (rule inj_eq [OF inj_on_inverseI], rule double_compl)
    1.84 +
    1.85 +text {* Conjunction *}
    1.86 +
    1.87 +lemma conj_absorb: "x \<sqinter> x = x"
    1.88 +proof -
    1.89 +  have "x \<sqinter> x = (x \<sqinter> x) \<squnion> \<zero>" using disj_zero_right by simp
    1.90 +  also have "... = (x \<sqinter> x) \<squnion> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
    1.91 +  also have "... = x \<sqinter> (x \<squnion> \<sim> x)" using conj_disj_distrib by simp
    1.92 +  also have "... = x \<sqinter> \<one>" using disj_cancel_right by simp
    1.93 +  also have "... = x" using conj_one_right by simp
    1.94 +  finally show ?thesis .
    1.95 +qed
    1.96 +
    1.97 +lemma conj_zero_right [simp]: "x \<sqinter> \<zero> = \<zero>"
    1.98 +proof -
    1.99 +  have "x \<sqinter> \<zero> = x \<sqinter> (x \<sqinter> \<sim> x)" using conj_cancel_right by simp
   1.100 +  also have "... = (x \<sqinter> x) \<sqinter> \<sim> x" using conj_assoc by simp
   1.101 +  also have "... = x \<sqinter> \<sim> x" using conj_absorb by simp
   1.102 +  also have "... = \<zero>" using conj_cancel_right by simp
   1.103 +  finally show ?thesis .
   1.104 +qed
   1.105 +
   1.106 +lemma compl_one [simp]: "\<sim> \<one> = \<zero>"
   1.107 +by (rule compl_unique [OF conj_zero_right disj_zero_right])
   1.108 +
   1.109 +lemma conj_zero_left [simp]: "\<zero> \<sqinter> x = \<zero>"
   1.110 +by (subst conj_commute) (rule conj_zero_right)
   1.111 +
   1.112 +lemma conj_one_left [simp]: "\<one> \<sqinter> x = x"
   1.113 +by (subst conj_commute) (rule conj_one_right)
   1.114 +
   1.115 +lemma conj_cancel_left [simp]: "\<sim> x \<sqinter> x = \<zero>"
   1.116 +by (subst conj_commute) (rule conj_cancel_right)
   1.117 +
   1.118 +lemma conj_left_absorb [simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
   1.119 +by (simp add: conj_assoc [symmetric] conj_absorb)
   1.120 +
   1.121 +lemma conj_disj_distrib2:
   1.122 +  "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)" 
   1.123 +by (simp add: conj_commute conj_disj_distrib)
   1.124 +
   1.125 +lemmas conj_disj_distribs =
   1.126 +   conj_disj_distrib conj_disj_distrib2
   1.127 +
   1.128 +text {* Disjunction *}
   1.129 +
   1.130 +lemma disj_absorb [simp]: "x \<squnion> x = x"
   1.131 +by (rule boolean.conj_absorb [OF dual])
   1.132 +
   1.133 +lemma disj_one_right [simp]: "x \<squnion> \<one> = \<one>"
   1.134 +by (rule boolean.conj_zero_right [OF dual])
   1.135 +
   1.136 +lemma compl_zero [simp]: "\<sim> \<zero> = \<one>"
   1.137 +by (rule boolean.compl_one [OF dual])
   1.138 +
   1.139 +lemma disj_zero_left [simp]: "\<zero> \<squnion> x = x"
   1.140 +by (rule boolean.conj_one_left [OF dual])
   1.141 +
   1.142 +lemma disj_one_left [simp]: "\<one> \<squnion> x = \<one>"
   1.143 +by (rule boolean.conj_zero_left [OF dual])
   1.144 +
   1.145 +lemma disj_cancel_left [simp]: "\<sim> x \<squnion> x = \<one>"
   1.146 +by (rule boolean.conj_cancel_left [OF dual])
   1.147 +
   1.148 +lemma disj_left_absorb [simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
   1.149 +by (rule boolean.conj_left_absorb [OF dual])
   1.150 +
   1.151 +lemma disj_conj_distrib2:
   1.152 +  "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
   1.153 +by (rule boolean.conj_disj_distrib2 [OF dual])
   1.154 +
   1.155 +lemmas disj_conj_distribs =
   1.156 +   disj_conj_distrib disj_conj_distrib2
   1.157 +
   1.158 +text {* De Morgan's Laws *}
   1.159 +
   1.160 +lemma de_Morgan_conj [simp]: "\<sim> (x \<sqinter> y) = \<sim> x \<squnion> \<sim> y"
   1.161 +proof (rule compl_unique)
   1.162 +  have "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = ((x \<sqinter> y) \<sqinter> \<sim> x) \<squnion> ((x \<sqinter> y) \<sqinter> \<sim> y)"
   1.163 +    by (rule conj_disj_distrib)
   1.164 +  also have "... = (y \<sqinter> (x \<sqinter> \<sim> x)) \<squnion> (x \<sqinter> (y \<sqinter> \<sim> y))"
   1.165 +    by (simp add: conj_ac)
   1.166 +  finally show "(x \<sqinter> y) \<sqinter> (\<sim> x \<squnion> \<sim> y) = \<zero>"
   1.167 +    by (simp add: conj_cancel_right conj_zero_right disj_zero_right)
   1.168 +next
   1.169 +  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))"
   1.170 +    by (rule disj_conj_distrib2)
   1.171 +  also have "... = (\<sim> y \<squnion> (x \<squnion> \<sim> x)) \<sqinter> (\<sim> x \<squnion> (y \<squnion> \<sim> y))"
   1.172 +    by (simp add: disj_ac)
   1.173 +  finally show "(x \<sqinter> y) \<squnion> (\<sim> x \<squnion> \<sim> y) = \<one>"
   1.174 +    by (simp add: disj_cancel_right disj_one_right conj_one_right)
   1.175 +qed
   1.176 +
   1.177 +lemma de_Morgan_disj [simp]: "\<sim> (x \<squnion> y) = \<sim> x \<sqinter> \<sim> y"
   1.178 +by (rule boolean.de_Morgan_conj [OF dual])
   1.179 +
   1.180 +end
   1.181 +
   1.182 +text {* Symmetric Difference *}
   1.183 +
   1.184 +locale boolean_xor = boolean +
   1.185 +  fixes xor :: "'a => 'a => 'a"  (infixr "\<oplus>" 65)
   1.186 +  assumes xor_def: "x \<oplus> y = (x \<sqinter> \<sim> y) \<squnion> (\<sim> x \<sqinter> y)"
   1.187 +begin
   1.188 +
   1.189 +lemma xor_def2:
   1.190 +  "x \<oplus> y = (x \<squnion> y) \<sqinter> (\<sim> x \<squnion> \<sim> y)"
   1.191 +by (simp add: xor_def conj_disj_distribs
   1.192 +              disj_ac conj_ac conj_cancel_right disj_zero_left)
   1.193 +
   1.194 +lemma xor_commute: "x \<oplus> y = y \<oplus> x"
   1.195 +by (simp add: xor_def conj_commute disj_commute)
   1.196 +
   1.197 +lemma xor_assoc: "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
   1.198 +proof -
   1.199 +  let ?t = "(x \<sqinter> y \<sqinter> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> \<sim> z) \<squnion>
   1.200 +            (\<sim> x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (\<sim> x \<sqinter> \<sim> y \<sqinter> z)"
   1.201 +  have "?t \<squnion> (z \<sqinter> x \<sqinter> \<sim> x) \<squnion> (z \<sqinter> y \<sqinter> \<sim> y) =
   1.202 +        ?t \<squnion> (x \<sqinter> y \<sqinter> \<sim> y) \<squnion> (x \<sqinter> z \<sqinter> \<sim> z)"
   1.203 +    by (simp add: conj_cancel_right conj_zero_right)
   1.204 +  thus "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
   1.205 +    apply (simp add: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   1.206 +    apply (simp add: conj_disj_distribs conj_ac disj_ac)
   1.207 +    done
   1.208 +qed
   1.209 +
   1.210 +lemmas xor_ac =
   1.211 +  xor_assoc xor_commute
   1.212 +  mk_left_commute [of "xor", OF xor_assoc xor_commute]
   1.213 +
   1.214 +lemma xor_zero_right [simp]: "x \<oplus> \<zero> = x"
   1.215 +by (simp add: xor_def compl_zero conj_one_right conj_zero_right disj_zero_right)
   1.216 +
   1.217 +lemma xor_zero_left [simp]: "\<zero> \<oplus> x = x"
   1.218 +by (subst xor_commute) (rule xor_zero_right)
   1.219 +
   1.220 +lemma xor_one_right [simp]: "x \<oplus> \<one> = \<sim> x"
   1.221 +by (simp add: xor_def compl_one conj_zero_right conj_one_right disj_zero_left)
   1.222 +
   1.223 +lemma xor_one_left [simp]: "\<one> \<oplus> x = \<sim> x"
   1.224 +by (subst xor_commute) (rule xor_one_right)
   1.225 +
   1.226 +lemma xor_self [simp]: "x \<oplus> x = \<zero>"
   1.227 +by (simp add: xor_def conj_cancel_right conj_cancel_left disj_zero_right)
   1.228 +
   1.229 +lemma xor_left_self [simp]: "x \<oplus> (x \<oplus> y) = y"
   1.230 +by (simp add: xor_assoc [symmetric] xor_self xor_zero_left)
   1.231 +
   1.232 +lemma xor_compl_left: "\<sim> x \<oplus> y = \<sim> (x \<oplus> y)"
   1.233 +apply (simp add: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   1.234 +apply (simp add: conj_disj_distribs)
   1.235 +apply (simp add: conj_cancel_right conj_cancel_left)
   1.236 +apply (simp add: disj_zero_left disj_zero_right)
   1.237 +apply (simp add: disj_ac conj_ac)
   1.238 +done
   1.239 +
   1.240 +lemma xor_compl_right: "x \<oplus> \<sim> y = \<sim> (x \<oplus> y)"
   1.241 +apply (simp add: xor_def de_Morgan_disj de_Morgan_conj double_compl)
   1.242 +apply (simp add: conj_disj_distribs)
   1.243 +apply (simp add: conj_cancel_right conj_cancel_left)
   1.244 +apply (simp add: disj_zero_left disj_zero_right)
   1.245 +apply (simp add: disj_ac conj_ac)
   1.246 +done
   1.247 +
   1.248 +lemma xor_cancel_right [simp]: "x \<oplus> \<sim> x = \<one>"
   1.249 +by (simp add: xor_compl_right xor_self compl_zero)
   1.250 +
   1.251 +lemma xor_cancel_left [simp]: "\<sim> x \<oplus> x = \<one>"
   1.252 +by (subst xor_commute) (rule xor_cancel_right)
   1.253 +
   1.254 +lemma conj_xor_distrib: "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   1.255 +proof -
   1.256 +  have "(x \<sqinter> y \<sqinter> \<sim> z) \<squnion> (x \<sqinter> \<sim> y \<sqinter> z) =
   1.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)"
   1.258 +    by (simp add: conj_cancel_right conj_zero_right disj_zero_left)
   1.259 +  thus "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   1.260 +    by (simp (no_asm_use) add:
   1.261 +        xor_def de_Morgan_disj de_Morgan_conj double_compl
   1.262 +        conj_disj_distribs conj_ac disj_ac)
   1.263 +qed
   1.264 +
   1.265 +lemma conj_xor_distrib2:
   1.266 +  "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   1.267 +proof -
   1.268 +  have "x \<sqinter> (y \<oplus> z) = (x \<sqinter> y) \<oplus> (x \<sqinter> z)"
   1.269 +    by (rule conj_xor_distrib)
   1.270 +  thus "(y \<oplus> z) \<sqinter> x = (y \<sqinter> x) \<oplus> (z \<sqinter> x)"
   1.271 +    by (simp add: conj_commute)
   1.272 +qed
   1.273 +
   1.274 +lemmas conj_xor_distribs =
   1.275 +   conj_xor_distrib conj_xor_distrib2
   1.276 +
   1.277 +end
   1.278 +
   1.279 +end