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