src/HOL/Library/Set_Algebras.thy
 author krauss Thu Apr 12 19:58:59 2012 +0200 (2012-04-12) changeset 47443 aeff49a3369b parent 44890 22f665a2e91c child 47444 d21c95af2df7 permissions -rw-r--r--
backported Set_Algebras to use type classes (basically reverting b3e8d5ec721d from 2008)
```     1 (*  Title:      HOL/Library/Set_Algebras.thy
```
```     2     Author:     Jeremy Avigad and Kevin Donnelly; Florian Haftmann, TUM
```
```     3 *)
```
```     4
```
```     5 header {* Algebraic operations on sets *}
```
```     6
```
```     7 theory Set_Algebras
```
```     8 imports Main
```
```     9 begin
```
```    10
```
```    11 text {*
```
```    12   This library lifts operations like addition and muliplication to
```
```    13   sets.  It was designed to support asymptotic calculations. See the
```
```    14   comments at the top of theory @{text BigO}.
```
```    15 *}
```
```    16
```
```    17 instantiation set :: (plus) plus
```
```    18 begin
```
```    19
```
```    20 definition plus_set :: "'a::plus set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
```
```    21   set_plus_def: "A + B = {c. \<exists>a\<in>A. \<exists>b\<in>B. c = a + b}"
```
```    22
```
```    23 instance ..
```
```    24
```
```    25 end
```
```    26
```
```    27 instantiation set :: (times) times
```
```    28 begin
```
```    29
```
```    30 definition times_set :: "'a::times set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
```
```    31   set_times_def: "A * B = {c. \<exists>a\<in>A. \<exists>b\<in>B. c = a * b}"
```
```    32
```
```    33 instance ..
```
```    34
```
```    35 end
```
```    36
```
```    37
```
```    38 text {* Legacy syntax: *}
```
```    39
```
```    40 abbreviation (input) set_plus :: "'a::plus set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "\<oplus>" 65) where
```
```    41   "A \<oplus> B \<equiv> A + B"
```
```    42 abbreviation (input) set_times :: "'a::times set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "\<otimes>" 70) where
```
```    43   "A \<otimes> B \<equiv> A * B"
```
```    44
```
```    45 instantiation set :: (zero) zero
```
```    46 begin
```
```    47
```
```    48 definition
```
```    49   set_zero[simp]: "0::('a::zero)set == {0}"
```
```    50
```
```    51 instance ..
```
```    52
```
```    53 end
```
```    54
```
```    55 instantiation set :: (one) one
```
```    56 begin
```
```    57
```
```    58 definition
```
```    59   set_one[simp]: "1::('a::one)set == {1}"
```
```    60
```
```    61 instance ..
```
```    62
```
```    63 end
```
```    64
```
```    65 definition elt_set_plus :: "'a::plus \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "+o" 70) where
```
```    66   "a +o B = {c. \<exists>b\<in>B. c = a + b}"
```
```    67
```
```    68 definition elt_set_times :: "'a::times \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "*o" 80) where
```
```    69   "a *o B = {c. \<exists>b\<in>B. c = a * b}"
```
```    70
```
```    71 abbreviation (input) elt_set_eq :: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infix "=o" 50) where
```
```    72   "x =o A \<equiv> x \<in> A"
```
```    73
```
```    74 instance set :: (semigroup_add) semigroup_add
```
```    75 by default (force simp add: set_plus_def add.assoc)
```
```    76
```
```    77 instance set :: (ab_semigroup_add) ab_semigroup_add
```
```    78 by default (force simp add: set_plus_def add.commute)
```
```    79
```
```    80 instance set :: (monoid_add) monoid_add
```
```    81 by default (simp_all add: set_plus_def)
```
```    82
```
```    83 instance set :: (comm_monoid_add) comm_monoid_add
```
```    84 by default (simp_all add: set_plus_def)
```
```    85
```
```    86 instance set :: (semigroup_mult) semigroup_mult
```
```    87 by default (force simp add: set_times_def mult.assoc)
```
```    88
```
```    89 instance set :: (ab_semigroup_mult) ab_semigroup_mult
```
```    90 by default (force simp add: set_times_def mult.commute)
```
```    91
```
```    92 instance set :: (monoid_mult) monoid_mult
```
```    93 by default (simp_all add: set_times_def)
```
```    94
```
```    95 instance set :: (comm_monoid_mult) comm_monoid_mult
```
```    96 by default (simp_all add: set_times_def)
```
```    97
```
```    98 lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : C \<oplus> D"
```
```    99   by (auto simp add: set_plus_def)
```
```   100
```
```   101 lemma set_plus_intro2 [intro]: "b : C ==> a + b : a +o C"
```
```   102   by (auto simp add: elt_set_plus_def)
```
```   103
```
```   104 lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) \<oplus>
```
```   105     (b +o D) = (a + b) +o (C \<oplus> D)"
```
```   106   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   107    apply (rule_tac x = "ba + bb" in exI)
```
```   108   apply (auto simp add: add_ac)
```
```   109   apply (rule_tac x = "aa + a" in exI)
```
```   110   apply (auto simp add: add_ac)
```
```   111   done
```
```   112
```
```   113 lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
```
```   114     (a + b) +o C"
```
```   115   by (auto simp add: elt_set_plus_def add_assoc)
```
```   116
```
```   117 lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) \<oplus> C =
```
```   118     a +o (B \<oplus> C)"
```
```   119   apply (auto simp add: elt_set_plus_def set_plus_def)
```
```   120    apply (blast intro: add_ac)
```
```   121   apply (rule_tac x = "a + aa" in exI)
```
```   122   apply (rule conjI)
```
```   123    apply (rule_tac x = "aa" in bexI)
```
```   124     apply auto
```
```   125   apply (rule_tac x = "ba" in bexI)
```
```   126    apply (auto simp add: add_ac)
```
```   127   done
```
```   128
```
```   129 theorem set_plus_rearrange4: "C \<oplus> ((a::'a::comm_monoid_add) +o D) =
```
```   130     a +o (C \<oplus> D)"
```
```   131   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   132    apply (rule_tac x = "aa + ba" in exI)
```
```   133    apply (auto simp add: add_ac)
```
```   134   done
```
```   135
```
```   136 theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
```
```   137   set_plus_rearrange3 set_plus_rearrange4
```
```   138
```
```   139 lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
```
```   140   by (auto simp add: elt_set_plus_def)
```
```   141
```
```   142 lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
```
```   143     C \<oplus> E <= D \<oplus> F"
```
```   144   by (auto simp add: set_plus_def)
```
```   145
```
```   146 lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C \<oplus> D"
```
```   147   by (auto simp add: elt_set_plus_def set_plus_def)
```
```   148
```
```   149 lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
```
```   150     a +o D <= D \<oplus> C"
```
```   151   by (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   152
```
```   153 lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C \<oplus> D"
```
```   154   apply (subgoal_tac "a +o B <= a +o D")
```
```   155    apply (erule order_trans)
```
```   156    apply (erule set_plus_mono3)
```
```   157   apply (erule set_plus_mono)
```
```   158   done
```
```   159
```
```   160 lemma set_plus_mono_b: "C <= D ==> x : a +o C
```
```   161     ==> x : a +o D"
```
```   162   apply (frule set_plus_mono)
```
```   163   apply auto
```
```   164   done
```
```   165
```
```   166 lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C \<oplus> E ==>
```
```   167     x : D \<oplus> F"
```
```   168   apply (frule set_plus_mono2)
```
```   169    prefer 2
```
```   170    apply force
```
```   171   apply assumption
```
```   172   done
```
```   173
```
```   174 lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C \<oplus> D"
```
```   175   apply (frule set_plus_mono3)
```
```   176   apply auto
```
```   177   done
```
```   178
```
```   179 lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
```
```   180     x : a +o D ==> x : D \<oplus> C"
```
```   181   apply (frule set_plus_mono4)
```
```   182   apply auto
```
```   183   done
```
```   184
```
```   185 lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
```
```   186   by (auto simp add: elt_set_plus_def)
```
```   187
```
```   188 lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A \<oplus> B"
```
```   189   apply (auto simp add: set_plus_def)
```
```   190   apply (rule_tac x = 0 in bexI)
```
```   191    apply (rule_tac x = x in bexI)
```
```   192     apply (auto simp add: add_ac)
```
```   193   done
```
```   194
```
```   195 lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
```
```   196   by (auto simp add: elt_set_plus_def add_ac diff_minus)
```
```   197
```
```   198 lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
```
```   199   apply (auto simp add: elt_set_plus_def add_ac diff_minus)
```
```   200   apply (subgoal_tac "a = (a + - b) + b")
```
```   201    apply (rule bexI, assumption, assumption)
```
```   202   apply (auto simp add: add_ac)
```
```   203   done
```
```   204
```
```   205 lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
```
```   206   by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
```
```   207     assumption)
```
```   208
```
```   209 lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C \<otimes> D"
```
```   210   by (auto simp add: set_times_def)
```
```   211
```
```   212 lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
```
```   213   by (auto simp add: elt_set_times_def)
```
```   214
```
```   215 lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) \<otimes>
```
```   216     (b *o D) = (a * b) *o (C \<otimes> D)"
```
```   217   apply (auto simp add: elt_set_times_def set_times_def)
```
```   218    apply (rule_tac x = "ba * bb" in exI)
```
```   219    apply (auto simp add: mult_ac)
```
```   220   apply (rule_tac x = "aa * a" in exI)
```
```   221   apply (auto simp add: mult_ac)
```
```   222   done
```
```   223
```
```   224 lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
```
```   225     (a * b) *o C"
```
```   226   by (auto simp add: elt_set_times_def mult_assoc)
```
```   227
```
```   228 lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) \<otimes> C =
```
```   229     a *o (B \<otimes> C)"
```
```   230   apply (auto simp add: elt_set_times_def set_times_def)
```
```   231    apply (blast intro: mult_ac)
```
```   232   apply (rule_tac x = "a * aa" in exI)
```
```   233   apply (rule conjI)
```
```   234    apply (rule_tac x = "aa" in bexI)
```
```   235     apply auto
```
```   236   apply (rule_tac x = "ba" in bexI)
```
```   237    apply (auto simp add: mult_ac)
```
```   238   done
```
```   239
```
```   240 theorem set_times_rearrange4: "C \<otimes> ((a::'a::comm_monoid_mult) *o D) =
```
```   241     a *o (C \<otimes> D)"
```
```   242   apply (auto simp add: elt_set_times_def set_times_def
```
```   243     mult_ac)
```
```   244    apply (rule_tac x = "aa * ba" in exI)
```
```   245    apply (auto simp add: mult_ac)
```
```   246   done
```
```   247
```
```   248 theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
```
```   249   set_times_rearrange3 set_times_rearrange4
```
```   250
```
```   251 lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
```
```   252   by (auto simp add: elt_set_times_def)
```
```   253
```
```   254 lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
```
```   255     C \<otimes> E <= D \<otimes> F"
```
```   256   by (auto simp add: set_times_def)
```
```   257
```
```   258 lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C \<otimes> D"
```
```   259   by (auto simp add: elt_set_times_def set_times_def)
```
```   260
```
```   261 lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
```
```   262     a *o D <= D \<otimes> C"
```
```   263   by (auto simp add: elt_set_times_def set_times_def mult_ac)
```
```   264
```
```   265 lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C \<otimes> D"
```
```   266   apply (subgoal_tac "a *o B <= a *o D")
```
```   267    apply (erule order_trans)
```
```   268    apply (erule set_times_mono3)
```
```   269   apply (erule set_times_mono)
```
```   270   done
```
```   271
```
```   272 lemma set_times_mono_b: "C <= D ==> x : a *o C
```
```   273     ==> x : a *o D"
```
```   274   apply (frule set_times_mono)
```
```   275   apply auto
```
```   276   done
```
```   277
```
```   278 lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C \<otimes> E ==>
```
```   279     x : D \<otimes> F"
```
```   280   apply (frule set_times_mono2)
```
```   281    prefer 2
```
```   282    apply force
```
```   283   apply assumption
```
```   284   done
```
```   285
```
```   286 lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C \<otimes> D"
```
```   287   apply (frule set_times_mono3)
```
```   288   apply auto
```
```   289   done
```
```   290
```
```   291 lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
```
```   292     x : a *o D ==> x : D \<otimes> C"
```
```   293   apply (frule set_times_mono4)
```
```   294   apply auto
```
```   295   done
```
```   296
```
```   297 lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
```
```   298   by (auto simp add: elt_set_times_def)
```
```   299
```
```   300 lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
```
```   301     (a * b) +o (a *o C)"
```
```   302   by (auto simp add: elt_set_plus_def elt_set_times_def ring_distribs)
```
```   303
```
```   304 lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B \<oplus> C) =
```
```   305     (a *o B) \<oplus> (a *o C)"
```
```   306   apply (auto simp add: set_plus_def elt_set_times_def ring_distribs)
```
```   307    apply blast
```
```   308   apply (rule_tac x = "b + bb" in exI)
```
```   309   apply (auto simp add: ring_distribs)
```
```   310   done
```
```   311
```
```   312 lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) \<otimes> D <=
```
```   313     a *o D \<oplus> C \<otimes> D"
```
```   314   apply (auto simp add:
```
```   315     elt_set_plus_def elt_set_times_def set_times_def
```
```   316     set_plus_def ring_distribs)
```
```   317   apply auto
```
```   318   done
```
```   319
```
```   320 theorems set_times_plus_distribs =
```
```   321   set_times_plus_distrib
```
```   322   set_times_plus_distrib2
```
```   323
```
```   324 lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
```
```   325     - a : C"
```
```   326   by (auto simp add: elt_set_times_def)
```
```   327
```
```   328 lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
```
```   329     - a : (- 1) *o C"
```
```   330   by (auto simp add: elt_set_times_def)
```
```   331
```
```   332 lemma set_plus_image:
```
```   333   fixes S T :: "'n::semigroup_add set" shows "S \<oplus> T = (\<lambda>(x, y). x + y) ` (S \<times> T)"
```
```   334   unfolding set_plus_def by (fastforce simp: image_iff)
```
```   335
```
```   336 text {* Legacy syntax: *}
```
```   337
```
```   338 abbreviation (input) setsum_set :: "('b \<Rightarrow> ('a::comm_monoid_add) set) \<Rightarrow> 'b set \<Rightarrow> 'a set" where
```
```   339    "setsum_set == setsum"
```
```   340
```
```   341 lemma set_setsum_alt:
```
```   342   assumes fin: "finite I"
```
```   343   shows "setsum_set S I = {setsum s I |s. \<forall>i\<in>I. s i \<in> S i}"
```
```   344     (is "_ = ?setsum I")
```
```   345 using fin proof induct
```
```   346   case (insert x F)
```
```   347   have "setsum_set S (insert x F) = S x \<oplus> ?setsum F"
```
```   348     using insert.hyps by auto
```
```   349   also have "...= {s x + setsum s F |s. \<forall> i\<in>insert x F. s i \<in> S i}"
```
```   350     unfolding set_plus_def
```
```   351   proof safe
```
```   352     fix y s assume "y \<in> S x" "\<forall>i\<in>F. s i \<in> S i"
```
```   353     then show "\<exists>s'. y + setsum s F = s' x + setsum s' F \<and> (\<forall>i\<in>insert x F. s' i \<in> S i)"
```
```   354       using insert.hyps
```
```   355       by (intro exI[of _ "\<lambda>i. if i \<in> F then s i else y"]) (auto simp add: set_plus_def)
```
```   356   qed auto
```
```   357   finally show ?case
```
```   358     using insert.hyps by auto
```
```   359 qed auto
```
```   360
```
```   361 lemma setsum_set_cond_linear:
```
```   362   fixes f :: "('a::comm_monoid_add) set \<Rightarrow> ('b::comm_monoid_add) set"
```
```   363   assumes [intro!]: "\<And>A B. P A  \<Longrightarrow> P B  \<Longrightarrow> P (A \<oplus> B)" "P {0}"
```
```   364     and f: "\<And>A B. P A  \<Longrightarrow> P B \<Longrightarrow> f (A \<oplus> B) = f A \<oplus> f B" "f {0} = {0}"
```
```   365   assumes all: "\<And>i. i \<in> I \<Longrightarrow> P (S i)"
```
```   366   shows "f (setsum_set S I) = setsum_set (f \<circ> S) I"
```
```   367 proof cases
```
```   368   assume "finite I" from this all show ?thesis
```
```   369   proof induct
```
```   370     case (insert x F)
```
```   371     from `finite F` `\<And>i. i \<in> insert x F \<Longrightarrow> P (S i)` have "P (setsum_set S F)"
```
```   372       by induct auto
```
```   373     with insert show ?case
```
```   374       by (simp, subst f) auto
```
```   375   qed (auto intro!: f)
```
```   376 qed (auto intro!: f)
```
```   377
```
```   378 lemma setsum_set_linear:
```
```   379   fixes f :: "('a::comm_monoid_add) set => ('b::comm_monoid_add) set"
```
```   380   assumes "\<And>A B. f(A) \<oplus> f(B) = f(A \<oplus> B)" "f {0} = {0}"
```
```   381   shows "f (setsum_set S I) = setsum_set (f \<circ> S) I"
```
```   382   using setsum_set_cond_linear[of "\<lambda>x. True" f I S] assms by auto
```
```   383
```
```   384 end
```