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