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