src/HOL/Library/Set_Algebras.thy
 author Andreas Lochbihler Fri Sep 20 10:09:16 2013 +0200 (2013-09-20) changeset 53745 788730ab7da4 parent 53596 d29d63460d84 child 54230 b1d955791529 permissions -rw-r--r--
prefer Code.abort over code_abort
```     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 instantiation set :: (zero) zero
```
```    38 begin
```
```    39
```
```    40 definition
```
```    41   set_zero[simp]: "0::('a::zero)set == {0}"
```
```    42
```
```    43 instance ..
```
```    44
```
```    45 end
```
```    46
```
```    47 instantiation set :: (one) one
```
```    48 begin
```
```    49
```
```    50 definition
```
```    51   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) where
```
```    58   "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) where
```
```    61   "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) where
```
```    64   "x =o A \<equiv> x \<in> A"
```
```    65
```
```    66 instance set :: (semigroup_add) semigroup_add
```
```    67 by default (force simp add: set_plus_def add.assoc)
```
```    68
```
```    69 instance set :: (ab_semigroup_add) ab_semigroup_add
```
```    70 by default (force simp add: set_plus_def add.commute)
```
```    71
```
```    72 instance set :: (monoid_add) monoid_add
```
```    73 by default (simp_all add: set_plus_def)
```
```    74
```
```    75 instance set :: (comm_monoid_add) comm_monoid_add
```
```    76 by default (simp_all add: set_plus_def)
```
```    77
```
```    78 instance set :: (semigroup_mult) semigroup_mult
```
```    79 by default (force simp add: set_times_def mult.assoc)
```
```    80
```
```    81 instance set :: (ab_semigroup_mult) ab_semigroup_mult
```
```    82 by default (force simp add: set_times_def mult.commute)
```
```    83
```
```    84 instance set :: (monoid_mult) monoid_mult
```
```    85 by default (simp_all add: set_times_def)
```
```    86
```
```    87 instance set :: (comm_monoid_mult) comm_monoid_mult
```
```    88 by default (simp_all add: set_times_def)
```
```    89
```
```    90 lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : 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 : C ==> a + b : a +o C"
```
```    99   by (auto simp add: elt_set_plus_def)
```
```   100
```
```   101 lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) +
```
```   102     (b +o D) = (a + b) +o (C + D)"
```
```   103   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   104    apply (rule_tac x = "ba + bb" in exI)
```
```   105   apply (auto simp add: add_ac)
```
```   106   apply (rule_tac x = "aa + a" in exI)
```
```   107   apply (auto simp add: add_ac)
```
```   108   done
```
```   109
```
```   110 lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
```
```   111     (a + b) +o C"
```
```   112   by (auto simp add: elt_set_plus_def add_assoc)
```
```   113
```
```   114 lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) + C =
```
```   115     a +o (B + C)"
```
```   116   apply (auto simp add: elt_set_plus_def set_plus_def)
```
```   117    apply (blast intro: add_ac)
```
```   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: add_ac)
```
```   124   done
```
```   125
```
```   126 theorem set_plus_rearrange4: "C + ((a::'a::comm_monoid_add) +o D) =
```
```   127     a +o (C + D)"
```
```   128   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   129    apply (rule_tac x = "aa + ba" in exI)
```
```   130    apply (auto simp add: add_ac)
```
```   131   done
```
```   132
```
```   133 theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
```
```   134   set_plus_rearrange3 set_plus_rearrange4
```
```   135
```
```   136 lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
```
```   137   by (auto simp add: elt_set_plus_def)
```
```   138
```
```   139 lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
```
```   140     C + E <= D + F"
```
```   141   by (auto simp add: set_plus_def)
```
```   142
```
```   143 lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C + D"
```
```   144   by (auto simp add: elt_set_plus_def set_plus_def)
```
```   145
```
```   146 lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
```
```   147     a +o D <= D + C"
```
```   148   by (auto simp add: elt_set_plus_def set_plus_def add_ac)
```
```   149
```
```   150 lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C + D"
```
```   151   apply (subgoal_tac "a +o B <= 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 <= D ==> x : a +o C
```
```   158     ==> x : a +o D"
```
```   159   apply (frule set_plus_mono)
```
```   160   apply auto
```
```   161   done
```
```   162
```
```   163 lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C + E ==>
```
```   164     x : D + F"
```
```   165   apply (frule set_plus_mono2)
```
```   166    prefer 2
```
```   167    apply force
```
```   168   apply assumption
```
```   169   done
```
```   170
```
```   171 lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C + D"
```
```   172   apply (frule set_plus_mono3)
```
```   173   apply auto
```
```   174   done
```
```   175
```
```   176 lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
```
```   177     x : a +o D ==> x : D + C"
```
```   178   apply (frule set_plus_mono4)
```
```   179   apply auto
```
```   180   done
```
```   181
```
```   182 lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
```
```   183   by (auto simp add: elt_set_plus_def)
```
```   184
```
```   185 lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A + B"
```
```   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: add_ac)
```
```   190   done
```
```   191
```
```   192 lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
```
```   193   by (auto simp add: elt_set_plus_def add_ac diff_minus)
```
```   194
```
```   195 lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
```
```   196   apply (auto simp add: elt_set_plus_def add_ac diff_minus)
```
```   197   apply (subgoal_tac "a = (a + - b) + b")
```
```   198    apply (rule bexI, assumption, assumption)
```
```   199   apply (auto simp add: add_ac)
```
```   200   done
```
```   201
```
```   202 lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
```
```   203   by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
```
```   204     assumption)
```
```   205
```
```   206 lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C * D"
```
```   207   by (auto simp add: set_times_def)
```
```   208
```
```   209 lemma set_times_elim:
```
```   210   assumes "x \<in> A * B"
```
```   211   obtains a b where "x = a * b" and "a \<in> A" and "b \<in> B"
```
```   212   using assms unfolding set_times_def by fast
```
```   213
```
```   214 lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
```
```   215   by (auto simp add: elt_set_times_def)
```
```   216
```
```   217 lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) *
```
```   218     (b *o D) = (a * b) *o (C * D)"
```
```   219   apply (auto simp add: elt_set_times_def set_times_def)
```
```   220    apply (rule_tac x = "ba * bb" in exI)
```
```   221    apply (auto simp add: mult_ac)
```
```   222   apply (rule_tac x = "aa * a" in exI)
```
```   223   apply (auto simp add: mult_ac)
```
```   224   done
```
```   225
```
```   226 lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
```
```   227     (a * b) *o C"
```
```   228   by (auto simp add: elt_set_times_def mult_assoc)
```
```   229
```
```   230 lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) * C =
```
```   231     a *o (B * C)"
```
```   232   apply (auto simp add: elt_set_times_def set_times_def)
```
```   233    apply (blast intro: mult_ac)
```
```   234   apply (rule_tac x = "a * aa" in exI)
```
```   235   apply (rule conjI)
```
```   236    apply (rule_tac x = "aa" in bexI)
```
```   237     apply auto
```
```   238   apply (rule_tac x = "ba" in bexI)
```
```   239    apply (auto simp add: mult_ac)
```
```   240   done
```
```   241
```
```   242 theorem set_times_rearrange4: "C * ((a::'a::comm_monoid_mult) *o D) =
```
```   243     a *o (C * D)"
```
```   244   apply (auto simp add: elt_set_times_def set_times_def
```
```   245     mult_ac)
```
```   246    apply (rule_tac x = "aa * ba" in exI)
```
```   247    apply (auto simp add: mult_ac)
```
```   248   done
```
```   249
```
```   250 theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
```
```   251   set_times_rearrange3 set_times_rearrange4
```
```   252
```
```   253 lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
```
```   254   by (auto simp add: elt_set_times_def)
```
```   255
```
```   256 lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
```
```   257     C * E <= D * F"
```
```   258   by (auto simp add: set_times_def)
```
```   259
```
```   260 lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C * D"
```
```   261   by (auto simp add: elt_set_times_def set_times_def)
```
```   262
```
```   263 lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
```
```   264     a *o D <= D * C"
```
```   265   by (auto simp add: elt_set_times_def set_times_def mult_ac)
```
```   266
```
```   267 lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C * D"
```
```   268   apply (subgoal_tac "a *o B <= a *o D")
```
```   269    apply (erule order_trans)
```
```   270    apply (erule set_times_mono3)
```
```   271   apply (erule set_times_mono)
```
```   272   done
```
```   273
```
```   274 lemma set_times_mono_b: "C <= D ==> x : a *o C
```
```   275     ==> x : a *o D"
```
```   276   apply (frule set_times_mono)
```
```   277   apply auto
```
```   278   done
```
```   279
```
```   280 lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C * E ==>
```
```   281     x : D * F"
```
```   282   apply (frule set_times_mono2)
```
```   283    prefer 2
```
```   284    apply force
```
```   285   apply assumption
```
```   286   done
```
```   287
```
```   288 lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C * D"
```
```   289   apply (frule set_times_mono3)
```
```   290   apply auto
```
```   291   done
```
```   292
```
```   293 lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
```
```   294     x : a *o D ==> x : D * C"
```
```   295   apply (frule set_times_mono4)
```
```   296   apply auto
```
```   297   done
```
```   298
```
```   299 lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
```
```   300   by (auto simp add: elt_set_times_def)
```
```   301
```
```   302 lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
```
```   303     (a * b) +o (a *o C)"
```
```   304   by (auto simp add: elt_set_plus_def elt_set_times_def ring_distribs)
```
```   305
```
```   306 lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B + C) =
```
```   307     (a *o B) + (a *o C)"
```
```   308   apply (auto simp add: set_plus_def elt_set_times_def ring_distribs)
```
```   309    apply blast
```
```   310   apply (rule_tac x = "b + bb" in exI)
```
```   311   apply (auto simp add: ring_distribs)
```
```   312   done
```
```   313
```
```   314 lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) * D <=
```
```   315     a *o D + C * D"
```
```   316   apply (auto simp add:
```
```   317     elt_set_plus_def elt_set_times_def set_times_def
```
```   318     set_plus_def ring_distribs)
```
```   319   apply auto
```
```   320   done
```
```   321
```
```   322 theorems set_times_plus_distribs =
```
```   323   set_times_plus_distrib
```
```   324   set_times_plus_distrib2
```
```   325
```
```   326 lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
```
```   327     - a : C"
```
```   328   by (auto simp add: elt_set_times_def)
```
```   329
```
```   330 lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
```
```   331     - a : (- 1) *o C"
```
```   332   by (auto simp add: elt_set_times_def)
```
```   333
```
```   334 lemma set_plus_image: "S + T = (\<lambda>(x, y). x + y) ` (S \<times> T)"
```
```   335   unfolding set_plus_def by (fastforce simp: image_iff)
```
```   336
```
```   337 lemma set_times_image: "S * T = (\<lambda>(x, y). x * y) ` (S \<times> T)"
```
```   338   unfolding set_times_def by (fastforce simp: image_iff)
```
```   339
```
```   340 lemma finite_set_plus:
```
```   341   assumes "finite s" and "finite t" shows "finite (s + t)"
```
```   342   using assms unfolding set_plus_image by simp
```
```   343
```
```   344 lemma finite_set_times:
```
```   345   assumes "finite s" and "finite t" shows "finite (s * t)"
```
```   346   using assms unfolding set_times_image by simp
```
```   347
```
```   348 lemma set_setsum_alt:
```
```   349   assumes fin: "finite I"
```
```   350   shows "setsum S I = {setsum s I |s. \<forall>i\<in>I. s i \<in> S i}"
```
```   351     (is "_ = ?setsum I")
```
```   352 using fin proof induct
```
```   353   case (insert x F)
```
```   354   have "setsum S (insert x F) = S x + ?setsum F"
```
```   355     using insert.hyps by auto
```
```   356   also have "...= {s x + setsum s F |s. \<forall> i\<in>insert x F. s i \<in> S i}"
```
```   357     unfolding set_plus_def
```
```   358   proof safe
```
```   359     fix y s assume "y \<in> S x" "\<forall>i\<in>F. s i \<in> S i"
```
```   360     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)"
```
```   361       using insert.hyps
```
```   362       by (intro exI[of _ "\<lambda>i. if i \<in> F then s i else y"]) (auto simp add: set_plus_def)
```
```   363   qed auto
```
```   364   finally show ?case
```
```   365     using insert.hyps by auto
```
```   366 qed auto
```
```   367
```
```   368 lemma setsum_set_cond_linear:
```
```   369   fixes f :: "('a::comm_monoid_add) set \<Rightarrow> ('b::comm_monoid_add) set"
```
```   370   assumes [intro!]: "\<And>A B. P A  \<Longrightarrow> P B  \<Longrightarrow> P (A + B)" "P {0}"
```
```   371     and f: "\<And>A B. P A  \<Longrightarrow> P B \<Longrightarrow> f (A + B) = f A + f B" "f {0} = {0}"
```
```   372   assumes all: "\<And>i. i \<in> I \<Longrightarrow> P (S i)"
```
```   373   shows "f (setsum S I) = setsum (f \<circ> S) I"
```
```   374 proof cases
```
```   375   assume "finite I" from this all show ?thesis
```
```   376   proof induct
```
```   377     case (insert x F)
```
```   378     from `finite F` `\<And>i. i \<in> insert x F \<Longrightarrow> P (S i)` have "P (setsum S F)"
```
```   379       by induct auto
```
```   380     with insert show ?case
```
```   381       by (simp, subst f) auto
```
```   382   qed (auto intro!: f)
```
```   383 qed (auto intro!: f)
```
```   384
```
```   385 lemma setsum_set_linear:
```
```   386   fixes f :: "('a::comm_monoid_add) set => ('b::comm_monoid_add) set"
```
```   387   assumes "\<And>A B. f(A) + f(B) = f(A + B)" "f {0} = {0}"
```
```   388   shows "f (setsum S I) = setsum (f \<circ> S) I"
```
```   389   using setsum_set_cond_linear[of "\<lambda>x. True" f I S] assms by auto
```
```   390
```
```   391 lemma set_times_Un_distrib:
```
```   392   "A * (B \<union> C) = A * B \<union> A * C"
```
```   393   "(A \<union> B) * C = A * C \<union> B * C"
```
```   394 by (auto simp: set_times_def)
```
```   395
```
```   396 lemma set_times_UNION_distrib:
```
```   397   "A * UNION I M = UNION I (%i. A * M i)"
```
```   398   "UNION I M * A = UNION I (%i. M i * A)"
```
```   399 by (auto simp: set_times_def)
```
```   400
```
```   401 end
```