src/HOL/Library/Set_Algebras.thy
author wenzelm
Wed Sep 12 13:42:28 2012 +0200 (2012-09-12)
changeset 49322 fbb320d02420
parent 47446 ed0795caec95
child 53596 d29d63460d84
permissions -rw-r--r--
tuned headers;
     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_intro2 [intro]: "b : C ==> a + b : a +o C"
    94   by (auto simp add: elt_set_plus_def)
    95 
    96 lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) +
    97     (b +o D) = (a + b) +o (C + D)"
    98   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
    99    apply (rule_tac x = "ba + bb" in exI)
   100   apply (auto simp add: add_ac)
   101   apply (rule_tac x = "aa + a" in exI)
   102   apply (auto simp add: add_ac)
   103   done
   104 
   105 lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
   106     (a + b) +o C"
   107   by (auto simp add: elt_set_plus_def add_assoc)
   108 
   109 lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) + C =
   110     a +o (B + C)"
   111   apply (auto simp add: elt_set_plus_def set_plus_def)
   112    apply (blast intro: add_ac)
   113   apply (rule_tac x = "a + aa" in exI)
   114   apply (rule conjI)
   115    apply (rule_tac x = "aa" in bexI)
   116     apply auto
   117   apply (rule_tac x = "ba" in bexI)
   118    apply (auto simp add: add_ac)
   119   done
   120 
   121 theorem set_plus_rearrange4: "C + ((a::'a::comm_monoid_add) +o D) =
   122     a +o (C + D)"
   123   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
   124    apply (rule_tac x = "aa + ba" in exI)
   125    apply (auto simp add: add_ac)
   126   done
   127 
   128 theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
   129   set_plus_rearrange3 set_plus_rearrange4
   130 
   131 lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
   132   by (auto simp add: elt_set_plus_def)
   133 
   134 lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
   135     C + E <= D + F"
   136   by (auto simp add: set_plus_def)
   137 
   138 lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C + D"
   139   by (auto simp add: elt_set_plus_def set_plus_def)
   140 
   141 lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
   142     a +o D <= D + C"
   143   by (auto simp add: elt_set_plus_def set_plus_def add_ac)
   144 
   145 lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C + D"
   146   apply (subgoal_tac "a +o B <= a +o D")
   147    apply (erule order_trans)
   148    apply (erule set_plus_mono3)
   149   apply (erule set_plus_mono)
   150   done
   151 
   152 lemma set_plus_mono_b: "C <= D ==> x : a +o C
   153     ==> x : a +o D"
   154   apply (frule set_plus_mono)
   155   apply auto
   156   done
   157 
   158 lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C + E ==>
   159     x : D + F"
   160   apply (frule set_plus_mono2)
   161    prefer 2
   162    apply force
   163   apply assumption
   164   done
   165 
   166 lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C + D"
   167   apply (frule set_plus_mono3)
   168   apply auto
   169   done
   170 
   171 lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
   172     x : a +o D ==> x : D + C"
   173   apply (frule set_plus_mono4)
   174   apply auto
   175   done
   176 
   177 lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
   178   by (auto simp add: elt_set_plus_def)
   179 
   180 lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A + B"
   181   apply (auto simp add: set_plus_def)
   182   apply (rule_tac x = 0 in bexI)
   183    apply (rule_tac x = x in bexI)
   184     apply (auto simp add: add_ac)
   185   done
   186 
   187 lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
   188   by (auto simp add: elt_set_plus_def add_ac diff_minus)
   189 
   190 lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
   191   apply (auto simp add: elt_set_plus_def add_ac diff_minus)
   192   apply (subgoal_tac "a = (a + - b) + b")
   193    apply (rule bexI, assumption, assumption)
   194   apply (auto simp add: add_ac)
   195   done
   196 
   197 lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
   198   by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
   199     assumption)
   200 
   201 lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C * D"
   202   by (auto simp add: set_times_def)
   203 
   204 lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
   205   by (auto simp add: elt_set_times_def)
   206 
   207 lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) *
   208     (b *o D) = (a * b) *o (C * D)"
   209   apply (auto simp add: elt_set_times_def set_times_def)
   210    apply (rule_tac x = "ba * bb" in exI)
   211    apply (auto simp add: mult_ac)
   212   apply (rule_tac x = "aa * a" in exI)
   213   apply (auto simp add: mult_ac)
   214   done
   215 
   216 lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
   217     (a * b) *o C"
   218   by (auto simp add: elt_set_times_def mult_assoc)
   219 
   220 lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) * C =
   221     a *o (B * C)"
   222   apply (auto simp add: elt_set_times_def set_times_def)
   223    apply (blast intro: mult_ac)
   224   apply (rule_tac x = "a * aa" in exI)
   225   apply (rule conjI)
   226    apply (rule_tac x = "aa" in bexI)
   227     apply auto
   228   apply (rule_tac x = "ba" in bexI)
   229    apply (auto simp add: mult_ac)
   230   done
   231 
   232 theorem set_times_rearrange4: "C * ((a::'a::comm_monoid_mult) *o D) =
   233     a *o (C * D)"
   234   apply (auto simp add: elt_set_times_def set_times_def
   235     mult_ac)
   236    apply (rule_tac x = "aa * ba" in exI)
   237    apply (auto simp add: mult_ac)
   238   done
   239 
   240 theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
   241   set_times_rearrange3 set_times_rearrange4
   242 
   243 lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
   244   by (auto simp add: elt_set_times_def)
   245 
   246 lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
   247     C * E <= D * F"
   248   by (auto simp add: set_times_def)
   249 
   250 lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C * D"
   251   by (auto simp add: elt_set_times_def set_times_def)
   252 
   253 lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
   254     a *o D <= D * C"
   255   by (auto simp add: elt_set_times_def set_times_def mult_ac)
   256 
   257 lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C * D"
   258   apply (subgoal_tac "a *o B <= a *o D")
   259    apply (erule order_trans)
   260    apply (erule set_times_mono3)
   261   apply (erule set_times_mono)
   262   done
   263 
   264 lemma set_times_mono_b: "C <= D ==> x : a *o C
   265     ==> x : a *o D"
   266   apply (frule set_times_mono)
   267   apply auto
   268   done
   269 
   270 lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C * E ==>
   271     x : D * F"
   272   apply (frule set_times_mono2)
   273    prefer 2
   274    apply force
   275   apply assumption
   276   done
   277 
   278 lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C * D"
   279   apply (frule set_times_mono3)
   280   apply auto
   281   done
   282 
   283 lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
   284     x : a *o D ==> x : D * C"
   285   apply (frule set_times_mono4)
   286   apply auto
   287   done
   288 
   289 lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
   290   by (auto simp add: elt_set_times_def)
   291 
   292 lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
   293     (a * b) +o (a *o C)"
   294   by (auto simp add: elt_set_plus_def elt_set_times_def ring_distribs)
   295 
   296 lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B + C) =
   297     (a *o B) + (a *o C)"
   298   apply (auto simp add: set_plus_def elt_set_times_def ring_distribs)
   299    apply blast
   300   apply (rule_tac x = "b + bb" in exI)
   301   apply (auto simp add: ring_distribs)
   302   done
   303 
   304 lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) * D <=
   305     a *o D + C * D"
   306   apply (auto simp add:
   307     elt_set_plus_def elt_set_times_def set_times_def
   308     set_plus_def ring_distribs)
   309   apply auto
   310   done
   311 
   312 theorems set_times_plus_distribs =
   313   set_times_plus_distrib
   314   set_times_plus_distrib2
   315 
   316 lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
   317     - a : C"
   318   by (auto simp add: elt_set_times_def)
   319 
   320 lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
   321     - a : (- 1) *o C"
   322   by (auto simp add: elt_set_times_def)
   323 
   324 lemma set_plus_image:
   325   fixes S T :: "'n::semigroup_add set" shows "S + T = (\<lambda>(x, y). x + y) ` (S \<times> T)"
   326   unfolding set_plus_def by (fastforce simp: image_iff)
   327 
   328 lemma set_setsum_alt:
   329   assumes fin: "finite I"
   330   shows "setsum S I = {setsum s I |s. \<forall>i\<in>I. s i \<in> S i}"
   331     (is "_ = ?setsum I")
   332 using fin proof induct
   333   case (insert x F)
   334   have "setsum S (insert x F) = S x + ?setsum F"
   335     using insert.hyps by auto
   336   also have "...= {s x + setsum s F |s. \<forall> i\<in>insert x F. s i \<in> S i}"
   337     unfolding set_plus_def
   338   proof safe
   339     fix y s assume "y \<in> S x" "\<forall>i\<in>F. s i \<in> S i"
   340     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)"
   341       using insert.hyps
   342       by (intro exI[of _ "\<lambda>i. if i \<in> F then s i else y"]) (auto simp add: set_plus_def)
   343   qed auto
   344   finally show ?case
   345     using insert.hyps by auto
   346 qed auto
   347 
   348 lemma setsum_set_cond_linear:
   349   fixes f :: "('a::comm_monoid_add) set \<Rightarrow> ('b::comm_monoid_add) set"
   350   assumes [intro!]: "\<And>A B. P A  \<Longrightarrow> P B  \<Longrightarrow> P (A + B)" "P {0}"
   351     and f: "\<And>A B. P A  \<Longrightarrow> P B \<Longrightarrow> f (A + B) = f A + f B" "f {0} = {0}"
   352   assumes all: "\<And>i. i \<in> I \<Longrightarrow> P (S i)"
   353   shows "f (setsum S I) = setsum (f \<circ> S) I"
   354 proof cases
   355   assume "finite I" from this all show ?thesis
   356   proof induct
   357     case (insert x F)
   358     from `finite F` `\<And>i. i \<in> insert x F \<Longrightarrow> P (S i)` have "P (setsum S F)"
   359       by induct auto
   360     with insert show ?case
   361       by (simp, subst f) auto
   362   qed (auto intro!: f)
   363 qed (auto intro!: f)
   364 
   365 lemma setsum_set_linear:
   366   fixes f :: "('a::comm_monoid_add) set => ('b::comm_monoid_add) set"
   367   assumes "\<And>A B. f(A) + f(B) = f(A + B)" "f {0} = {0}"
   368   shows "f (setsum S I) = setsum (f \<circ> S) I"
   369   using setsum_set_cond_linear[of "\<lambda>x. True" f I S] assms by auto
   370 
   371 lemma set_times_Un_distrib:
   372   "A * (B \<union> C) = A * B \<union> A * C"
   373   "(A \<union> B) * C = A * C \<union> B * C"
   374 by (auto simp: set_times_def)
   375 
   376 lemma set_times_UNION_distrib:
   377   "A * UNION I M = UNION I (%i. A * M i)"
   378   "UNION I M * A = UNION I (%i. M i * A)"
   379 by (auto simp: set_times_def)
   380 
   381 end