REorganized Finite_Set
authornipkow
Sun Dec 12 16:25:47 2004 +0100 (2004-12-12)
changeset 1540297204f3b4705
parent 15401 ba28d103bada
child 15403 9e58e666074d
REorganized Finite_Set
src/HOL/Finite_Set.ML
src/HOL/Finite_Set.thy
src/HOL/Library/Multiset.thy
src/HOL/NumberTheory/Euler.thy
src/HOL/NumberTheory/EulerFermat.thy
src/HOL/NumberTheory/Finite2.thy
src/HOL/NumberTheory/Quadratic_Reciprocity.thy
src/HOL/SetInterval.thy
     1.1 --- a/src/HOL/Finite_Set.ML	Fri Dec 10 22:33:16 2004 +0100
     1.2 +++ b/src/HOL/Finite_Set.ML	Sun Dec 12 16:25:47 2004 +0100
     1.3 @@ -11,16 +11,6 @@
     1.4    val [emptyI, insertI] = thms "Finites.intros";
     1.5  end;
     1.6  
     1.7 -structure cardR =
     1.8 -struct
     1.9 -  val intrs = thms "cardR.intros";
    1.10 -  val elims = thms "cardR.cases";
    1.11 -  val elim = thm "cardR.cases";
    1.12 -  val induct = thm "cardR.induct";
    1.13 -  val mk_cases = InductivePackage.the_mk_cases (the_context ()) "Finite_Set.cardR";
    1.14 -  val [EmptyI, InsertI] = thms "cardR.intros";
    1.15 -end;
    1.16 -
    1.17  structure foldSet =
    1.18  struct
    1.19    val intrs = thms "foldSet.intros";
    1.20 @@ -31,11 +21,6 @@
    1.21    val [emptyI, insertI] = thms "foldSet.intros";
    1.22  end;
    1.23  
    1.24 -val cardR_SucD = thm "cardR_SucD";
    1.25 -val cardR_determ = thm "cardR_determ";
    1.26 -val cardR_emptyE = thm "cardR_emptyE";
    1.27 -val cardR_imp_finite = thm "cardR_imp_finite";
    1.28 -val cardR_insertE = thm "cardR_insertE";
    1.29  val card_0_eq = thm "card_0_eq";
    1.30  val card_Diff1_le = thm "card_Diff1_le";
    1.31  val card_Diff1_less = thm "card_Diff1_less";
    1.32 @@ -50,7 +35,6 @@
    1.33  val card_bij_eq = thm "card_bij_eq";
    1.34  val card_def = thm "card_def";
    1.35  val card_empty = thm "card_empty";
    1.36 -val card_equality = thm "card_equality";
    1.37  val card_eq_setsum = thm "card_eq_setsum";
    1.38  val card_image = thm "card_image";
    1.39  val card_image_le = thm "card_image_le";
    1.40 @@ -87,7 +71,6 @@
    1.41  val finite_empty_induct = thm "finite_empty_induct";
    1.42  val finite_imageD = thm "finite_imageD";
    1.43  val finite_imageI = thm "finite_imageI";
    1.44 -val finite_imp_cardR = thm "finite_imp_cardR";
    1.45  val finite_induct = thm "finite_induct";
    1.46  val finite_insert = thm "finite_insert";
    1.47  val finite_range_imageI = thm "finite_range_imageI";
     2.1 --- a/src/HOL/Finite_Set.thy	Fri Dec 10 22:33:16 2004 +0100
     2.2 +++ b/src/HOL/Finite_Set.thy	Sun Dec 12 16:25:47 2004 +0100
     2.3 @@ -3,7 +3,9 @@
     2.4      Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
     2.5                  Additions by Jeremy Avigad in Feb 2004
     2.6  
     2.7 -FIXME: define card via fold and derive as many lemmas as possible from fold.
     2.8 +Get rid of a couple of superfluous finiteness assumptions in lemmas
     2.9 +about setsum and card - see FIXME.
    2.10 +NB: the analogous lemmas for setprod should also be simplified!
    2.11  *)
    2.12  
    2.13  header {* Finite sets *}
    2.14 @@ -290,6 +292,10 @@
    2.15      "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
    2.16    by (unfold Sigma_def) (blast intro!: finite_UN_I)
    2.17  
    2.18 +lemma finite_cartesian_product: "[| finite A; finite B |] ==>
    2.19 +    finite (A <*> B)"
    2.20 +  by (rule finite_SigmaI)
    2.21 +
    2.22  lemma finite_Prod_UNIV:
    2.23      "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
    2.24    apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
    2.25 @@ -371,10 +377,6 @@
    2.26     apply (auto simp add: finite_Field)
    2.27    done
    2.28  
    2.29 -lemma finite_cartesian_product: "[| finite A; finite B |] ==>
    2.30 -    finite (A <*> B)"
    2.31 -  by (rule finite_SigmaI)
    2.32 -
    2.33  
    2.34  subsection {* A fold functional for finite sets *}
    2.35  
    2.36 @@ -437,6 +439,22 @@
    2.37    thus ?thesis by (subst commute)
    2.38  qed
    2.39  
    2.40 +text{* Instantiation of locales: *}
    2.41 +
    2.42 +lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
    2.43 +by(fastsimp intro: ACf.intro add_assoc add_commute)
    2.44 +
    2.45 +lemma ACe_add: "ACe (op +) (0::'a::comm_monoid_add)"
    2.46 +by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
    2.47 +
    2.48 +
    2.49 +lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
    2.50 +by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
    2.51 +
    2.52 +lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
    2.53 +by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
    2.54 +
    2.55 +
    2.56  subsubsection{*From @{term foldSet} to @{term fold}*}
    2.57  
    2.58  lemma (in ACf) foldSet_determ_aux:
    2.59 @@ -476,8 +494,6 @@
    2.60  	  and z: "(C,z) \<in> foldSet f g e" and notinC: "c \<notin> C"
    2.61  	hence A2: "A = insert c C" and x': "x' = g c \<cdot> z" by auto
    2.62  	let ?h = "%i. if h i = b then h n else h i"
    2.63 -	have finA: "finite A" by(rule foldSet_imp_finite[OF Afoldx])
    2.64 -(* move down? *)
    2.65  	have less: "B = ?h`{i. i<n}" (is "_ = ?r")
    2.66  	proof
    2.67  	  show "B \<subseteq> ?r"
    2.68 @@ -534,7 +550,8 @@
    2.69  	  let ?D = "B - {c}"
    2.70  	  have B: "B = insert c ?D" and C: "C = insert b ?D"
    2.71  	    using A1 A2 notinB notinC diff by(blast elim!:equalityE)+
    2.72 -	  have "finite ?D" using finA A1 by simp
    2.73 +	  have "finite A" by(rule foldSet_imp_finite[OF Afoldx])
    2.74 +	  with A1 have "finite ?D" by simp
    2.75  	  then obtain d where Dfoldd: "(?D,d) \<in> foldSet f g e"
    2.76  	    using finite_imp_foldSet by rules
    2.77  	  moreover have cinB: "c \<in> B" using B by(auto)
    2.78 @@ -708,12 +725,6 @@
    2.79      cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
    2.80    done
    2.81  
    2.82 -text{* Its definitional form: *}
    2.83 -
    2.84 -corollary (in ACf) fold_insert_def:
    2.85 -    "\<lbrakk> F \<equiv> fold f g e; finite A; x \<notin> A \<rbrakk> \<Longrightarrow> F (insert x A) = f (g x) (F A)"
    2.86 -by(simp)
    2.87 -
    2.88  declare
    2.89    empty_foldSetE [rule del]  foldSet.intros [rule del]
    2.90    -- {* Delete rules to do with @{text foldSet} relation. *}
    2.91 @@ -812,98 +823,548 @@
    2.92  done
    2.93  
    2.94  
    2.95 +subsection {* Generalized summation over a set *}
    2.96 +
    2.97 +constdefs
    2.98 +  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
    2.99 +  "setsum f A == if finite A then fold (op +) f 0 A else 0"
   2.100 +
   2.101 +text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
   2.102 +written @{text"\<Sum>x\<in>A. e"}. *}
   2.103 +
   2.104 +syntax
   2.105 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
   2.106 +syntax (xsymbols)
   2.107 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
   2.108 +syntax (HTML output)
   2.109 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
   2.110 +
   2.111 +translations -- {* Beware of argument permutation! *}
   2.112 +  "SUM i:A. b" == "setsum (%i. b) A"
   2.113 +  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
   2.114 +
   2.115 +text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
   2.116 + @{text"\<Sum>x|P. e"}. *}
   2.117 +
   2.118 +syntax
   2.119 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
   2.120 +syntax (xsymbols)
   2.121 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
   2.122 +syntax (HTML output)
   2.123 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
   2.124 +
   2.125 +translations
   2.126 +  "SUM x|P. t" => "setsum (%x. t) {x. P}"
   2.127 +  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
   2.128 +
   2.129 +text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
   2.130 +
   2.131 +syntax
   2.132 +  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
   2.133 +
   2.134 +parse_translation {*
   2.135 +  let
   2.136 +    fun Setsum_tr [A] = Syntax.const "setsum" $ Abs ("", dummyT, Bound 0) $ A
   2.137 +  in [("_Setsum", Setsum_tr)] end;
   2.138 +*}
   2.139 +
   2.140 +print_translation {*
   2.141 +let
   2.142 +  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" $ A
   2.143 +    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
   2.144 +       if x<>y then raise Match
   2.145 +       else let val x' = Syntax.mark_bound x
   2.146 +                val t' = subst_bound(x',t)
   2.147 +                val P' = subst_bound(x',P)
   2.148 +            in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
   2.149 +in
   2.150 +[("setsum", setsum_tr')]
   2.151 +end
   2.152 +*}
   2.153 +
   2.154 +lemma setsum_empty [simp]: "setsum f {} = 0"
   2.155 +  by (simp add: setsum_def)
   2.156 +
   2.157 +lemma setsum_insert [simp]:
   2.158 +    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
   2.159 +  by (simp add: setsum_def ACf.fold_insert [OF ACf_add])
   2.160 +
   2.161 +lemma setsum_reindex:
   2.162 +     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
   2.163 +by(auto simp add: setsum_def ACf.fold_reindex[OF ACf_add] dest!:finite_imageD)
   2.164 +
   2.165 +lemma setsum_reindex_id:
   2.166 +     "inj_on f B ==> setsum f B = setsum id (f ` B)"
   2.167 +by (auto simp add: setsum_reindex)
   2.168 +
   2.169 +lemma setsum_cong:
   2.170 +  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   2.171 +by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
   2.172 +
   2.173 +lemma setsum_reindex_cong:
   2.174 +     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
   2.175 +      ==> setsum h B = setsum g A"
   2.176 +  by (simp add: setsum_reindex cong: setsum_cong)
   2.177 +
   2.178 +lemma setsum_0: "setsum (%i. 0) A = 0"
   2.179 +apply (clarsimp simp: setsum_def)
   2.180 +apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
   2.181 +done
   2.182 +
   2.183 +lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
   2.184 +  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
   2.185 +  apply (erule ssubst, rule setsum_0)
   2.186 +  apply (rule setsum_cong, auto)
   2.187 +  done
   2.188 +
   2.189 +lemma setsum_Un_Int: "finite A ==> finite B ==>
   2.190 +  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
   2.191 +  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
   2.192 +by(simp add: setsum_def ACe.fold_Un_Int[OF ACe_add,symmetric])
   2.193 +
   2.194 +lemma setsum_Un_disjoint: "finite A ==> finite B
   2.195 +  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
   2.196 +by (subst setsum_Un_Int [symmetric], auto)
   2.197 +
   2.198 +(* FIXME get rid of finite I. If infinite, rhs is directly 0, and UNION I A
   2.199 +is also infinite and hence also 0 *)
   2.200 +lemma setsum_UN_disjoint:
   2.201 +    "finite I ==> (ALL i:I. finite (A i)) ==>
   2.202 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
   2.203 +      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
   2.204 +by(simp add: setsum_def ACe.fold_UN_disjoint[OF ACe_add] cong: setsum_cong)
   2.205 +
   2.206 +
   2.207 +(* FIXME get rid of finite C. If infinite, rhs is directly 0, and Union C
   2.208 +is also infinite and hence also 0 *)
   2.209 +lemma setsum_Union_disjoint:
   2.210 +  "finite C ==> (ALL A:C. finite A) ==>
   2.211 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
   2.212 +      setsum f (Union C) = setsum (setsum f) C"
   2.213 +  apply (frule setsum_UN_disjoint [of C id f])
   2.214 +  apply (unfold Union_def id_def, assumption+)
   2.215 +  done
   2.216 +
   2.217 +(* FIXME get rid of finite A. If infinite, lhs is directly 0, and SIGMA A B
   2.218 +is either infinite or empty, and in both cases the rhs is also 0 *)
   2.219 +lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
   2.220 +    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
   2.221 +    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
   2.222 +by(simp add:setsum_def ACe.fold_Sigma[OF ACe_add] split_def cong:setsum_cong)
   2.223 +
   2.224 +lemma setsum_cartesian_product: "finite A ==> finite B ==>
   2.225 +    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
   2.226 +    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
   2.227 +  by (erule setsum_Sigma, auto)
   2.228 +
   2.229 +lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
   2.230 +by(simp add:setsum_def ACe.fold_distrib[OF ACe_add])
   2.231 +
   2.232 +
   2.233 +subsubsection {* Properties in more restricted classes of structures *}
   2.234 +
   2.235 +lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
   2.236 +  apply (case_tac "finite A")
   2.237 +   prefer 2 apply (simp add: setsum_def)
   2.238 +  apply (erule rev_mp)
   2.239 +  apply (erule finite_induct, auto)
   2.240 +  done
   2.241 +
   2.242 +lemma setsum_eq_0_iff [simp]:
   2.243 +    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
   2.244 +  by (induct set: Finites) auto
   2.245 +
   2.246 +lemma setsum_Un_nat: "finite A ==> finite B ==>
   2.247 +    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
   2.248 +  -- {* For the natural numbers, we have subtraction. *}
   2.249 +  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
   2.250 +
   2.251 +lemma setsum_Un: "finite A ==> finite B ==>
   2.252 +    (setsum f (A Un B) :: 'a :: ab_group_add) =
   2.253 +      setsum f A + setsum f B - setsum f (A Int B)"
   2.254 +  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
   2.255 +
   2.256 +lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
   2.257 +    (if a:A then setsum f A - f a else setsum f A)"
   2.258 +  apply (case_tac "finite A")
   2.259 +   prefer 2 apply (simp add: setsum_def)
   2.260 +  apply (erule finite_induct)
   2.261 +   apply (auto simp add: insert_Diff_if)
   2.262 +  apply (drule_tac a = a in mk_disjoint_insert, auto)
   2.263 +  done
   2.264 +
   2.265 +lemma setsum_diff1: "finite A \<Longrightarrow>
   2.266 +  (setsum f (A - {a}) :: ('a::ab_group_add)) =
   2.267 +  (if a:A then setsum f A - f a else setsum f A)"
   2.268 +  by (erule finite_induct) (auto simp add: insert_Diff_if)
   2.269 +
   2.270 +(* By Jeremy Siek: *)
   2.271 +
   2.272 +lemma setsum_diff_nat: 
   2.273 +  assumes finB: "finite B"
   2.274 +  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
   2.275 +using finB
   2.276 +proof (induct)
   2.277 +  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
   2.278 +next
   2.279 +  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
   2.280 +    and xFinA: "insert x F \<subseteq> A"
   2.281 +    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
   2.282 +  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
   2.283 +  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
   2.284 +    by (simp add: setsum_diff1_nat)
   2.285 +  from xFinA have "F \<subseteq> A" by simp
   2.286 +  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
   2.287 +  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
   2.288 +    by simp
   2.289 +  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
   2.290 +  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
   2.291 +    by simp
   2.292 +  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
   2.293 +  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
   2.294 +    by simp
   2.295 +  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
   2.296 +qed
   2.297 +
   2.298 +lemma setsum_diff:
   2.299 +  assumes le: "finite A" "B \<subseteq> A"
   2.300 +  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
   2.301 +proof -
   2.302 +  from le have finiteB: "finite B" using finite_subset by auto
   2.303 +  show ?thesis using finiteB le
   2.304 +    proof (induct)
   2.305 +      case empty
   2.306 +      thus ?case by auto
   2.307 +    next
   2.308 +      case (insert x F)
   2.309 +      thus ?case using le finiteB 
   2.310 +	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
   2.311 +    qed
   2.312 +  qed
   2.313 +
   2.314 +lemma setsum_mono:
   2.315 +  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
   2.316 +  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
   2.317 +proof (cases "finite K")
   2.318 +  case True
   2.319 +  thus ?thesis using le
   2.320 +  proof (induct)
   2.321 +    case empty
   2.322 +    thus ?case by simp
   2.323 +  next
   2.324 +    case insert
   2.325 +    thus ?case using add_mono 
   2.326 +      by force
   2.327 +  qed
   2.328 +next
   2.329 +  case False
   2.330 +  thus ?thesis
   2.331 +    by (simp add: setsum_def)
   2.332 +qed
   2.333 +
   2.334 +lemma setsum_mono2_nat:
   2.335 +  assumes fin: "finite B" and sub: "A \<subseteq> B"
   2.336 +shows "setsum f A \<le> (setsum f B :: nat)"
   2.337 +proof -
   2.338 +  have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
   2.339 +  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
   2.340 +    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
   2.341 +  also have "A \<union> (B-A) = B" using sub by blast
   2.342 +  finally show ?thesis .
   2.343 +qed
   2.344 +
   2.345 +lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
   2.346 +  - setsum f A"
   2.347 +  by (induct set: Finites, auto)
   2.348 +
   2.349 +lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
   2.350 +  setsum f A - setsum g A"
   2.351 +  by (simp add: diff_minus setsum_addf setsum_negf)
   2.352 +
   2.353 +lemma setsum_nonneg: "[| finite A;
   2.354 +    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
   2.355 +    0 \<le> setsum f A";
   2.356 +  apply (induct set: Finites, auto)
   2.357 +  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
   2.358 +  apply (blast intro: add_mono)
   2.359 +  done
   2.360 +
   2.361 +lemma setsum_nonpos: "[| finite A;
   2.362 +    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
   2.363 +    setsum f A \<le> 0";
   2.364 +  apply (induct set: Finites, auto)
   2.365 +  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
   2.366 +  apply (blast intro: add_mono)
   2.367 +  done
   2.368 +
   2.369 +lemma setsum_mult: 
   2.370 +  fixes f :: "'a => ('b::semiring_0_cancel)"
   2.371 +  shows "r * setsum f A = setsum (%n. r * f n) A"
   2.372 +proof (cases "finite A")
   2.373 +  case True
   2.374 +  thus ?thesis
   2.375 +  proof (induct)
   2.376 +    case empty thus ?case by simp
   2.377 +  next
   2.378 +    case (insert x A) thus ?case by (simp add: right_distrib)
   2.379 +  qed
   2.380 +next
   2.381 +  case False thus ?thesis by (simp add: setsum_def)
   2.382 +qed
   2.383 +
   2.384 +lemma setsum_abs: 
   2.385 +  fixes f :: "'a => ('b::lordered_ab_group_abs)"
   2.386 +  assumes fin: "finite A" 
   2.387 +  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
   2.388 +using fin 
   2.389 +proof (induct) 
   2.390 +  case empty thus ?case by simp
   2.391 +next
   2.392 +  case (insert x A)
   2.393 +  thus ?case by (auto intro: abs_triangle_ineq order_trans)
   2.394 +qed
   2.395 +
   2.396 +lemma setsum_abs_ge_zero: 
   2.397 +  fixes f :: "'a => ('b::lordered_ab_group_abs)"
   2.398 +  assumes fin: "finite A" 
   2.399 +  shows "0 \<le> setsum (%i. abs(f i)) A"
   2.400 +using fin 
   2.401 +proof (induct) 
   2.402 +  case empty thus ?case by simp
   2.403 +next
   2.404 +  case (insert x A) thus ?case by (auto intro: order_trans)
   2.405 +qed
   2.406 +
   2.407 +
   2.408 +subsection {* Generalized product over a set *}
   2.409 +
   2.410 +constdefs
   2.411 +  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
   2.412 +  "setprod f A == if finite A then fold (op *) f 1 A else 1"
   2.413 +
   2.414 +syntax
   2.415 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
   2.416 +
   2.417 +syntax (xsymbols)
   2.418 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
   2.419 +syntax (HTML output)
   2.420 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
   2.421 +translations
   2.422 +  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
   2.423 +
   2.424 +syntax
   2.425 +  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
   2.426 +
   2.427 +parse_translation {*
   2.428 +  let
   2.429 +    fun Setprod_tr [A] = Syntax.const "setprod" $ Abs ("", dummyT, Bound 0) $ A
   2.430 +  in [("_Setprod", Setprod_tr)] end;
   2.431 +*}
   2.432 +print_translation {*
   2.433 +let fun setprod_tr' [Abs(x,Tx,t), A] =
   2.434 +    if t = Bound 0 then Syntax.const "_Setprod" $ A else raise Match
   2.435 +in
   2.436 +[("setprod", setprod_tr')]
   2.437 +end
   2.438 +*}
   2.439 +
   2.440 +
   2.441 +lemma setprod_empty [simp]: "setprod f {} = 1"
   2.442 +  by (auto simp add: setprod_def)
   2.443 +
   2.444 +lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
   2.445 +    setprod f (insert a A) = f a * setprod f A"
   2.446 +by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
   2.447 +
   2.448 +lemma setprod_reindex:
   2.449 +     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
   2.450 +by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
   2.451 +
   2.452 +lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
   2.453 +by (auto simp add: setprod_reindex)
   2.454 +
   2.455 +lemma setprod_cong:
   2.456 +  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
   2.457 +by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
   2.458 +
   2.459 +lemma setprod_reindex_cong: "inj_on f A ==>
   2.460 +    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
   2.461 +  by (frule setprod_reindex, simp)
   2.462 +
   2.463 +
   2.464 +lemma setprod_1: "setprod (%i. 1) A = 1"
   2.465 +  apply (case_tac "finite A")
   2.466 +  apply (erule finite_induct, auto simp add: mult_ac)
   2.467 +  apply (simp add: setprod_def)
   2.468 +  done
   2.469 +
   2.470 +lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
   2.471 +  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
   2.472 +  apply (erule ssubst, rule setprod_1)
   2.473 +  apply (rule setprod_cong, auto)
   2.474 +  done
   2.475 +
   2.476 +lemma setprod_Un_Int: "finite A ==> finite B
   2.477 +    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
   2.478 +by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
   2.479 +
   2.480 +lemma setprod_Un_disjoint: "finite A ==> finite B
   2.481 +  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
   2.482 +by (subst setprod_Un_Int [symmetric], auto)
   2.483 +
   2.484 +lemma setprod_UN_disjoint:
   2.485 +    "finite I ==> (ALL i:I. finite (A i)) ==>
   2.486 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
   2.487 +      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
   2.488 +by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
   2.489 +
   2.490 +lemma setprod_Union_disjoint:
   2.491 +  "finite C ==> (ALL A:C. finite A) ==>
   2.492 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
   2.493 +      setprod f (Union C) = setprod (setprod f) C"
   2.494 +  apply (frule setprod_UN_disjoint [of C id f])
   2.495 +  apply (unfold Union_def id_def, assumption+)
   2.496 +  done
   2.497 +
   2.498 +lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
   2.499 +    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
   2.500 +    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
   2.501 +by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
   2.502 +
   2.503 +lemma setprod_cartesian_product: "finite A ==> finite B ==>
   2.504 +    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
   2.505 +    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
   2.506 +  by (erule setprod_Sigma, auto)
   2.507 +
   2.508 +lemma setprod_timesf:
   2.509 +  "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
   2.510 +by(simp add:setprod_def ACe.fold_distrib[OF ACe_mult])
   2.511 +
   2.512 +
   2.513 +subsubsection {* Properties in more restricted classes of structures *}
   2.514 +
   2.515 +lemma setprod_eq_1_iff [simp]:
   2.516 +    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
   2.517 +  by (induct set: Finites) auto
   2.518 +
   2.519 +lemma setprod_zero:
   2.520 +     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
   2.521 +  apply (induct set: Finites, force, clarsimp)
   2.522 +  apply (erule disjE, auto)
   2.523 +  done
   2.524 +
   2.525 +lemma setprod_nonneg [rule_format]:
   2.526 +     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
   2.527 +  apply (case_tac "finite A")
   2.528 +  apply (induct set: Finites, force, clarsimp)
   2.529 +  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
   2.530 +  apply (rule mult_mono, assumption+)
   2.531 +  apply (auto simp add: setprod_def)
   2.532 +  done
   2.533 +
   2.534 +lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
   2.535 +     --> 0 < setprod f A"
   2.536 +  apply (case_tac "finite A")
   2.537 +  apply (induct set: Finites, force, clarsimp)
   2.538 +  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
   2.539 +  apply (rule mult_strict_mono, assumption+)
   2.540 +  apply (auto simp add: setprod_def)
   2.541 +  done
   2.542 +
   2.543 +lemma setprod_nonzero [rule_format]:
   2.544 +    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
   2.545 +      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
   2.546 +  apply (erule finite_induct, auto)
   2.547 +  done
   2.548 +
   2.549 +lemma setprod_zero_eq:
   2.550 +    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
   2.551 +     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
   2.552 +  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
   2.553 +  done
   2.554 +
   2.555 +lemma setprod_nonzero_field:
   2.556 +    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
   2.557 +  apply (rule setprod_nonzero, auto)
   2.558 +  done
   2.559 +
   2.560 +lemma setprod_zero_eq_field:
   2.561 +    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
   2.562 +  apply (rule setprod_zero_eq, auto)
   2.563 +  done
   2.564 +
   2.565 +lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
   2.566 +    (setprod f (A Un B) :: 'a ::{field})
   2.567 +      = setprod f A * setprod f B / setprod f (A Int B)"
   2.568 +  apply (subst setprod_Un_Int [symmetric], auto)
   2.569 +  apply (subgoal_tac "finite (A Int B)")
   2.570 +  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
   2.571 +  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
   2.572 +  done
   2.573 +
   2.574 +lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
   2.575 +    (setprod f (A - {a}) :: 'a :: {field}) =
   2.576 +      (if a:A then setprod f A / f a else setprod f A)"
   2.577 +  apply (erule finite_induct)
   2.578 +   apply (auto simp add: insert_Diff_if)
   2.579 +  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
   2.580 +  apply (erule ssubst)
   2.581 +  apply (subst times_divide_eq_right [THEN sym])
   2.582 +  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
   2.583 +  done
   2.584 +
   2.585 +lemma setprod_inversef: "finite A ==>
   2.586 +    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
   2.587 +      setprod (inverse \<circ> f) A = inverse (setprod f A)"
   2.588 +  apply (erule finite_induct)
   2.589 +  apply (simp, simp)
   2.590 +  done
   2.591 +
   2.592 +lemma setprod_dividef:
   2.593 +     "[|finite A;
   2.594 +        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
   2.595 +      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
   2.596 +  apply (subgoal_tac
   2.597 +         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
   2.598 +  apply (erule ssubst)
   2.599 +  apply (subst divide_inverse)
   2.600 +  apply (subst setprod_timesf)
   2.601 +  apply (subst setprod_inversef, assumption+, rule refl)
   2.602 +  apply (rule setprod_cong, rule refl)
   2.603 +  apply (subst divide_inverse, auto)
   2.604 +  done
   2.605 +
   2.606  subsection {* Finite cardinality *}
   2.607  
   2.608 -text {*
   2.609 -  This definition, although traditional, is ugly to work with: @{text
   2.610 -  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
   2.611 -  switched to an inductive one:
   2.612 +text {* This definition, although traditional, is ugly to work with:
   2.613 +@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
   2.614 +But now that we have @{text setsum} things are easy:
   2.615  *}
   2.616  
   2.617 -consts cardR :: "('a set \<times> nat) set"
   2.618 -
   2.619 -inductive cardR
   2.620 -  intros
   2.621 -    EmptyI: "({}, 0) : cardR"
   2.622 -    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
   2.623 -
   2.624  constdefs
   2.625    card :: "'a set => nat"
   2.626 -  "card A == THE n. (A, n) : cardR"
   2.627 -
   2.628 -inductive_cases cardR_emptyE: "({}, n) : cardR"
   2.629 -inductive_cases cardR_insertE: "(insert a A,n) : cardR"
   2.630 -
   2.631 -lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
   2.632 -  by (induct set: cardR) simp_all
   2.633 -
   2.634 -lemma cardR_determ_aux1:
   2.635 -    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
   2.636 -  apply (induct set: cardR, auto)
   2.637 -  apply (simp add: insert_Diff_if, auto)
   2.638 -  apply (drule cardR_SucD)
   2.639 -  apply (blast intro!: cardR.intros)
   2.640 -  done
   2.641 -
   2.642 -lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
   2.643 -  by (drule cardR_determ_aux1) auto
   2.644 -
   2.645 -lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
   2.646 -  apply (induct set: cardR)
   2.647 -   apply (safe elim!: cardR_emptyE cardR_insertE)
   2.648 -  apply (rename_tac B b m)
   2.649 -  apply (case_tac "a = b")
   2.650 -   apply (subgoal_tac "A = B")
   2.651 -    prefer 2 apply (blast elim: equalityE, blast)
   2.652 -  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
   2.653 -   prefer 2
   2.654 -   apply (rule_tac x = "A Int B" in exI)
   2.655 -   apply (blast elim: equalityE)
   2.656 -  apply (frule_tac A = B in cardR_SucD)
   2.657 -  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
   2.658 -  done
   2.659 -
   2.660 -lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
   2.661 -  by (induct set: cardR) simp_all
   2.662 -
   2.663 -lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
   2.664 -  by (induct set: Finites) (auto intro!: cardR.intros)
   2.665 -
   2.666 -lemma card_equality: "(A,n) : cardR ==> card A = n"
   2.667 -  by (unfold card_def) (blast intro: cardR_determ)
   2.668 +  "card A == setsum (%x. 1::nat) A"
   2.669  
   2.670  lemma card_empty [simp]: "card {} = 0"
   2.671 -  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
   2.672 +  by (simp add: card_def)
   2.673 +
   2.674 +lemma card_eq_setsum: "card A = setsum (%x. 1) A"
   2.675 +by (simp add: card_def)
   2.676  
   2.677  lemma card_insert_disjoint [simp]:
   2.678    "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
   2.679 -proof -
   2.680 -  assume x: "x \<notin> A"
   2.681 -  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
   2.682 -    apply (auto intro!: cardR.intros)
   2.683 -    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
   2.684 -     apply (force dest: cardR_imp_finite)
   2.685 -    apply (blast intro!: cardR.intros intro: cardR_determ)
   2.686 -    done
   2.687 -  assume "finite A"
   2.688 -  thus ?thesis
   2.689 -    apply (simp add: card_def aux)
   2.690 -    apply (rule the_equality)
   2.691 -     apply (auto intro: finite_imp_cardR
   2.692 -       cong: conj_cong simp: card_def [symmetric] card_equality)
   2.693 -    done
   2.694 -qed
   2.695 +by(simp add: card_def ACf.fold_insert[OF ACf_add])
   2.696 +
   2.697 +lemma card_insert_if:
   2.698 +    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
   2.699 +  by (simp add: insert_absorb)
   2.700  
   2.701  lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
   2.702    apply auto
   2.703    apply (drule_tac a = x in mk_disjoint_insert, clarify)
   2.704 -  apply (rotate_tac -1, auto)
   2.705 +  apply (auto)
   2.706    done
   2.707  
   2.708 -lemma card_insert_if:
   2.709 -    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
   2.710 -  by (simp add: insert_absorb)
   2.711 -
   2.712  lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
   2.713  apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
   2.714  apply(simp del:insert_Diff_single)
   2.715 @@ -923,6 +1384,9 @@
   2.716  lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
   2.717    by (simp add: card_insert_if)
   2.718  
   2.719 +lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
   2.720 +by (simp add: card_def setsum_mono2_nat)
   2.721 +
   2.722  lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
   2.723    apply (induct set: Finites, simp, clarify)
   2.724    apply (subgoal_tac "finite A & A - {x} <= F")
   2.725 @@ -937,33 +1401,17 @@
   2.726    apply (blast dest: card_seteq)
   2.727    done
   2.728  
   2.729 -lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
   2.730 -  apply (case_tac "A = B", simp)
   2.731 -  apply (simp add: linorder_not_less [symmetric])
   2.732 -  apply (blast dest: card_seteq intro: order_less_imp_le)
   2.733 -  done
   2.734 -
   2.735  lemma card_Un_Int: "finite A ==> finite B
   2.736      ==> card A + card B = card (A Un B) + card (A Int B)"
   2.737 -  apply (induct set: Finites, simp)
   2.738 -  apply (simp add: insert_absorb Int_insert_left)
   2.739 -  done
   2.740 +by(simp add:card_def setsum_Un_Int)
   2.741  
   2.742  lemma card_Un_disjoint: "finite A ==> finite B
   2.743      ==> A Int B = {} ==> card (A Un B) = card A + card B"
   2.744    by (simp add: card_Un_Int)
   2.745  
   2.746  lemma card_Diff_subset:
   2.747 -    "finite A ==> B <= A ==> card A - card B = card (A - B)"
   2.748 -  apply (subgoal_tac "(A - B) Un B = A")
   2.749 -   prefer 2 apply blast
   2.750 -  apply (rule nat_add_right_cancel [THEN iffD1])
   2.751 -  apply (rule card_Un_disjoint [THEN subst])
   2.752 -     apply (erule_tac [4] ssubst)
   2.753 -     prefer 3 apply blast
   2.754 -    apply (simp_all add: add_commute not_less_iff_le
   2.755 -      add_diff_inverse card_mono finite_subset)
   2.756 -  done
   2.757 +  "finite B ==> B <= A ==> card (A - B) = card A - card B"
   2.758 +by(simp add:card_def setsum_diff_nat)
   2.759  
   2.760  lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
   2.761    apply (rule Suc_less_SucD)
   2.762 @@ -987,8 +1435,8 @@
   2.763  by (erule psubsetI, blast)
   2.764  
   2.765  lemma insert_partition:
   2.766 -     "[| x \<notin> F; \<forall>c1\<in>insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 --> c1 \<inter> c2 = {}|] 
   2.767 -      ==> x \<inter> \<Union> F = {}"
   2.768 +  "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
   2.769 +  \<Longrightarrow> x \<inter> \<Union> F = {}"
   2.770  by auto
   2.771  
   2.772  (* main cardinality theorem *)
   2.773 @@ -1004,6 +1452,39 @@
   2.774  done
   2.775  
   2.776  
   2.777 +lemma setsum_constant_nat:
   2.778 +    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
   2.779 +  -- {* Generalized to any @{text comm_semiring_1_cancel} in
   2.780 +        @{text IntDef} as @{text setsum_constant}. *}
   2.781 +by (erule finite_induct, auto)
   2.782 +
   2.783 +lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
   2.784 +  apply (erule finite_induct)
   2.785 +  apply (auto simp add: power_Suc)
   2.786 +  done
   2.787 +
   2.788 +
   2.789 +subsubsection {* Cardinality of unions *}
   2.790 +
   2.791 +lemma card_UN_disjoint:
   2.792 +    "finite I ==> (ALL i:I. finite (A i)) ==>
   2.793 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
   2.794 +      card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
   2.795 +  apply (simp add: card_def)
   2.796 +  apply (subgoal_tac
   2.797 +           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
   2.798 +  apply (simp add: setsum_UN_disjoint)
   2.799 +  apply (simp add: setsum_constant_nat cong: setsum_cong)
   2.800 +  done
   2.801 +
   2.802 +lemma card_Union_disjoint:
   2.803 +  "finite C ==> (ALL A:C. finite A) ==>
   2.804 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
   2.805 +      card (Union C) = setsum card C"
   2.806 +  apply (frule card_UN_disjoint [of C id])
   2.807 +  apply (unfold Union_def id_def, assumption+)
   2.808 +  done
   2.809 +
   2.810  subsubsection {* Cardinality of image *}
   2.811  
   2.812  lemma card_image_le: "finite A ==> card (f ` A) <= card A"
   2.813 @@ -1011,8 +1492,8 @@
   2.814    apply (simp add: le_SucI finite_imageI card_insert_if)
   2.815    done
   2.816  
   2.817 -lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
   2.818 -by (induct set: Finites, simp_all)
   2.819 +lemma card_image: "inj_on f A ==> card (f ` A) = card A"
   2.820 +by(simp add:card_def setsum_reindex o_def)
   2.821  
   2.822  lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
   2.823    by (simp add: card_seteq card_image)
   2.824 @@ -1030,6 +1511,46 @@
   2.825  by(blast intro: card_image eq_card_imp_inj_on)
   2.826  
   2.827  
   2.828 +lemma card_inj_on_le:
   2.829 +    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
   2.830 +apply (subgoal_tac "finite A") 
   2.831 + apply (force intro: card_mono simp add: card_image [symmetric])
   2.832 +apply (blast intro: finite_imageD dest: finite_subset) 
   2.833 +done
   2.834 +
   2.835 +lemma card_bij_eq:
   2.836 +    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
   2.837 +       finite A; finite B |] ==> card A = card B"
   2.838 +  by (auto intro: le_anti_sym card_inj_on_le)
   2.839 +
   2.840 +
   2.841 +subsubsection {* Cardinality of products *}
   2.842 +
   2.843 +(*
   2.844 +lemma SigmaI_insert: "y \<notin> A ==>
   2.845 +  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
   2.846 +  by auto
   2.847 +*)
   2.848 +
   2.849 +lemma card_SigmaI [simp]:
   2.850 +  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
   2.851 +  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
   2.852 +by(simp add:card_def setsum_Sigma)
   2.853 +
   2.854 +(* FIXME get rid of prems *)
   2.855 +lemma card_cartesian_product:
   2.856 +     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
   2.857 +  by (simp add: setsum_constant_nat)
   2.858 +
   2.859 +(* FIXME should really be a consequence of card_cartesian_product *)
   2.860 +lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
   2.861 +  apply (subgoal_tac "inj_on (%y .(x,y)) A")
   2.862 +  apply (frule card_image)
   2.863 +  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
   2.864 +  apply (auto simp add: inj_on_def)
   2.865 +  done
   2.866 +
   2.867 +
   2.868  subsubsection {* Cardinality of the Powerset *}
   2.869  
   2.870  lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
   2.871 @@ -1084,18 +1605,6 @@
   2.872    apply (auto intro: finite_subset)
   2.873    done
   2.874  
   2.875 -lemma card_inj_on_le:
   2.876 -    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
   2.877 -apply (subgoal_tac "finite A") 
   2.878 - apply (force intro: card_mono simp add: card_image [symmetric])
   2.879 -apply (blast intro: finite_imageD dest: finite_subset) 
   2.880 -done
   2.881 -
   2.882 -lemma card_bij_eq:
   2.883 -    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
   2.884 -       finite A; finite B |] ==> card A = card B"
   2.885 -  by (auto intro: le_anti_sym card_inj_on_le)
   2.886 -
   2.887  text{*There are as many subsets of @{term A} having cardinality @{term k}
   2.888   as there are sets obtained from the former by inserting a fixed element
   2.889   @{term x} into each.*}
   2.890 @@ -1371,7 +1880,7 @@
   2.891    Max :: "('a::linorder)set => 'a"
   2.892    "Max  ==  fold1 max"
   2.893  
   2.894 -text{* Now we instantiate the recursiuon equations and declare them
   2.895 +text{* Now we instantiate the recursion equations and declare them
   2.896  simplification rules: *}
   2.897  
   2.898  declare
   2.899 @@ -1447,577 +1956,4 @@
   2.900  qed
   2.901  
   2.902  
   2.903 -subsection {* Generalized summation over a set *}
   2.904 -
   2.905 -constdefs
   2.906 -  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
   2.907 -  "setsum f A == if finite A then fold (op +) f 0 A else 0"
   2.908 -
   2.909 -text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
   2.910 -written @{text"\<Sum>x\<in>A. e"}. *}
   2.911 -
   2.912 -syntax
   2.913 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
   2.914 -syntax (xsymbols)
   2.915 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
   2.916 -syntax (HTML output)
   2.917 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
   2.918 -
   2.919 -translations -- {* Beware of argument permutation! *}
   2.920 -  "SUM i:A. b" == "setsum (%i. b) A"
   2.921 -  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
   2.922 -
   2.923 -text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
   2.924 - @{text"\<Sum>x|P. e"}. *}
   2.925 -
   2.926 -syntax
   2.927 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
   2.928 -syntax (xsymbols)
   2.929 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
   2.930 -syntax (HTML output)
   2.931 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
   2.932 -
   2.933 -translations
   2.934 -  "SUM x|P. t" => "setsum (%x. t) {x. P}"
   2.935 -  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
   2.936 -
   2.937 -text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
   2.938 -
   2.939 -syntax
   2.940 -  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
   2.941 -
   2.942 -parse_translation {*
   2.943 -  let
   2.944 -    fun Setsum_tr [A] = Syntax.const "setsum" $ Abs ("", dummyT, Bound 0) $ A
   2.945 -  in [("_Setsum", Setsum_tr)] end;
   2.946 -*}
   2.947 -
   2.948 -print_translation {*
   2.949 -let
   2.950 -  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" $ A
   2.951 -    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
   2.952 -       if x<>y then raise Match
   2.953 -       else let val x' = Syntax.mark_bound x
   2.954 -                val t' = subst_bound(x',t)
   2.955 -                val P' = subst_bound(x',P)
   2.956 -            in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
   2.957 -in
   2.958 -[("setsum", setsum_tr')]
   2.959  end
   2.960 -*}
   2.961 -
   2.962 -text{* Instantiation of locales: *}
   2.963 -
   2.964 -lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
   2.965 -by(fastsimp intro: ACf.intro add_assoc add_commute)
   2.966 -
   2.967 -lemma ACe_add: "ACe (op +) (0::'a::comm_monoid_add)"
   2.968 -by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
   2.969 -
   2.970 -lemma setsum_empty [simp]: "setsum f {} = 0"
   2.971 -  by (simp add: setsum_def)
   2.972 -
   2.973 -lemma setsum_insert [simp]:
   2.974 -    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
   2.975 -  by (simp add: setsum_def ACf.fold_insert [OF ACf_add])
   2.976 -
   2.977 -lemma setsum_reindex:
   2.978 -     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
   2.979 -by(auto simp add: setsum_def ACf.fold_reindex[OF ACf_add] dest!:finite_imageD)
   2.980 -
   2.981 -lemma setsum_reindex_id:
   2.982 -     "inj_on f B ==> setsum f B = setsum id (f ` B)"
   2.983 -by (auto simp add: setsum_reindex)
   2.984 -
   2.985 -lemma setsum_cong:
   2.986 -  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   2.987 -by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
   2.988 -
   2.989 -lemma setsum_reindex_cong:
   2.990 -     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
   2.991 -      ==> setsum h B = setsum g A"
   2.992 -  by (simp add: setsum_reindex cong: setsum_cong)
   2.993 -
   2.994 -lemma setsum_0: "setsum (%i. 0) A = 0"
   2.995 -apply (clarsimp simp: setsum_def)
   2.996 -apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
   2.997 -done
   2.998 -
   2.999 -lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
  2.1000 -  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
  2.1001 -  apply (erule ssubst, rule setsum_0)
  2.1002 -  apply (rule setsum_cong, auto)
  2.1003 -  done
  2.1004 -
  2.1005 -lemma card_eq_setsum: "finite A ==> card A = setsum (%x. 1) A"
  2.1006 -  -- {* Could allow many @{text "card"} proofs to be simplified. *}
  2.1007 -  by (induct set: Finites) auto
  2.1008 -
  2.1009 -lemma setsum_Un_Int: "finite A ==> finite B ==>
  2.1010 -  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
  2.1011 -  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
  2.1012 -by(simp add: setsum_def ACe.fold_Un_Int[OF ACe_add,symmetric])
  2.1013 -
  2.1014 -lemma setsum_Un_disjoint: "finite A ==> finite B
  2.1015 -  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
  2.1016 -by (subst setsum_Un_Int [symmetric], auto)
  2.1017 -
  2.1018 -lemma setsum_UN_disjoint:
  2.1019 -    "finite I ==> (ALL i:I. finite (A i)) ==>
  2.1020 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  2.1021 -      setsum f (UNION I A) = setsum (%i. setsum f (A i)) I"
  2.1022 -by(simp add: setsum_def ACe.fold_UN_disjoint[OF ACe_add] cong: setsum_cong)
  2.1023 -
  2.1024 -
  2.1025 -lemma setsum_Union_disjoint:
  2.1026 -  "finite C ==> (ALL A:C. finite A) ==>
  2.1027 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
  2.1028 -      setsum f (Union C) = setsum (setsum f) C"
  2.1029 -  apply (frule setsum_UN_disjoint [of C id f])
  2.1030 -  apply (unfold Union_def id_def, assumption+)
  2.1031 -  done
  2.1032 -
  2.1033 -lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
  2.1034 -    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
  2.1035 -    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
  2.1036 -by(simp add:setsum_def ACe.fold_Sigma[OF ACe_add] split_def cong:setsum_cong)
  2.1037 -
  2.1038 -lemma setsum_cartesian_product: "finite A ==> finite B ==>
  2.1039 -    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
  2.1040 -    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
  2.1041 -  by (erule setsum_Sigma, auto)
  2.1042 -
  2.1043 -lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
  2.1044 -by(simp add:setsum_def ACe.fold_distrib[OF ACe_add])
  2.1045 -
  2.1046 -
  2.1047 -subsubsection {* Properties in more restricted classes of structures *}
  2.1048 -
  2.1049 -lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
  2.1050 -  apply (case_tac "finite A")
  2.1051 -   prefer 2 apply (simp add: setsum_def)
  2.1052 -  apply (erule rev_mp)
  2.1053 -  apply (erule finite_induct, auto)
  2.1054 -  done
  2.1055 -
  2.1056 -lemma setsum_eq_0_iff [simp]:
  2.1057 -    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
  2.1058 -  by (induct set: Finites) auto
  2.1059 -
  2.1060 -lemma setsum_constant_nat:
  2.1061 -    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
  2.1062 -  -- {* Generalized to any @{text comm_semiring_1_cancel} in
  2.1063 -        @{text IntDef} as @{text setsum_constant}. *}
  2.1064 -  by (erule finite_induct, auto)
  2.1065 -
  2.1066 -lemma setsum_Un: "finite A ==> finite B ==>
  2.1067 -    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
  2.1068 -  -- {* For the natural numbers, we have subtraction. *}
  2.1069 -  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
  2.1070 -
  2.1071 -lemma setsum_Un_ring: "finite A ==> finite B ==>
  2.1072 -    (setsum f (A Un B) :: 'a :: ab_group_add) =
  2.1073 -      setsum f A + setsum f B - setsum f (A Int B)"
  2.1074 -  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
  2.1075 -
  2.1076 -lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
  2.1077 -    (if a:A then setsum f A - f a else setsum f A)"
  2.1078 -  apply (case_tac "finite A")
  2.1079 -   prefer 2 apply (simp add: setsum_def)
  2.1080 -  apply (erule finite_induct)
  2.1081 -   apply (auto simp add: insert_Diff_if)
  2.1082 -  apply (drule_tac a = a in mk_disjoint_insert, auto)
  2.1083 -  done
  2.1084 -
  2.1085 -lemma setsum_diff1: "finite A \<Longrightarrow>
  2.1086 -  (setsum f (A - {a}) :: ('a::{pordered_ab_group_add})) =
  2.1087 -  (if a:A then setsum f A - f a else setsum f A)"
  2.1088 -  by (erule finite_induct) (auto simp add: insert_Diff_if)
  2.1089 -
  2.1090 -(* By Jeremy Siek: *)
  2.1091 -
  2.1092 -lemma setsum_diff_nat: 
  2.1093 -  assumes finB: "finite B"
  2.1094 -  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
  2.1095 -using finB
  2.1096 -proof (induct)
  2.1097 -  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
  2.1098 -next
  2.1099 -  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
  2.1100 -    and xFinA: "insert x F \<subseteq> A"
  2.1101 -    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
  2.1102 -  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
  2.1103 -  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
  2.1104 -    by (simp add: setsum_diff1_nat)
  2.1105 -  from xFinA have "F \<subseteq> A" by simp
  2.1106 -  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
  2.1107 -  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
  2.1108 -    by simp
  2.1109 -  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
  2.1110 -  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
  2.1111 -    by simp
  2.1112 -  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
  2.1113 -  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
  2.1114 -    by simp
  2.1115 -  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
  2.1116 -qed
  2.1117 -
  2.1118 -lemma setsum_diff:
  2.1119 -  assumes le: "finite A" "B \<subseteq> A"
  2.1120 -  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::pordered_ab_group_add))"
  2.1121 -proof -
  2.1122 -  from le have finiteB: "finite B" using finite_subset by auto
  2.1123 -  show ?thesis using finiteB le
  2.1124 -    proof (induct)
  2.1125 -      case empty
  2.1126 -      thus ?case by auto
  2.1127 -    next
  2.1128 -      case (insert x F)
  2.1129 -      thus ?case using le finiteB 
  2.1130 -	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
  2.1131 -    qed
  2.1132 -  qed
  2.1133 -
  2.1134 -lemma setsum_mono:
  2.1135 -  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
  2.1136 -  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
  2.1137 -proof (cases "finite K")
  2.1138 -  case True
  2.1139 -  thus ?thesis using le
  2.1140 -  proof (induct)
  2.1141 -    case empty
  2.1142 -    thus ?case by simp
  2.1143 -  next
  2.1144 -    case insert
  2.1145 -    thus ?case using add_mono 
  2.1146 -      by force
  2.1147 -  qed
  2.1148 -next
  2.1149 -  case False
  2.1150 -  thus ?thesis
  2.1151 -    by (simp add: setsum_def)
  2.1152 -qed
  2.1153 -
  2.1154 -lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
  2.1155 -  - setsum f A"
  2.1156 -  by (induct set: Finites, auto)
  2.1157 -
  2.1158 -lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
  2.1159 -  setsum f A - setsum g A"
  2.1160 -  by (simp add: diff_minus setsum_addf setsum_negf)
  2.1161 -
  2.1162 -lemma setsum_nonneg: "[| finite A;
  2.1163 -    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
  2.1164 -    0 \<le> setsum f A";
  2.1165 -  apply (induct set: Finites, auto)
  2.1166 -  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
  2.1167 -  apply (blast intro: add_mono)
  2.1168 -  done
  2.1169 -
  2.1170 -lemma setsum_nonpos: "[| finite A;
  2.1171 -    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
  2.1172 -    setsum f A \<le> 0";
  2.1173 -  apply (induct set: Finites, auto)
  2.1174 -  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
  2.1175 -  apply (blast intro: add_mono)
  2.1176 -  done
  2.1177 -
  2.1178 -lemma setsum_mult: 
  2.1179 -  fixes f :: "'a => ('b::semiring_0_cancel)"
  2.1180 -  shows "r * setsum f A = setsum (%n. r * f n) A"
  2.1181 -proof (cases "finite A")
  2.1182 -  case True
  2.1183 -  thus ?thesis
  2.1184 -  proof (induct)
  2.1185 -    case empty thus ?case by simp
  2.1186 -  next
  2.1187 -    case (insert x A) thus ?case by (simp add: right_distrib)
  2.1188 -  qed
  2.1189 -next
  2.1190 -  case False thus ?thesis by (simp add: setsum_def)
  2.1191 -qed
  2.1192 -
  2.1193 -lemma setsum_abs: 
  2.1194 -  fixes f :: "'a => ('b::lordered_ab_group_abs)"
  2.1195 -  assumes fin: "finite A" 
  2.1196 -  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
  2.1197 -using fin 
  2.1198 -proof (induct) 
  2.1199 -  case empty thus ?case by simp
  2.1200 -next
  2.1201 -  case (insert x A)
  2.1202 -  thus ?case by (auto intro: abs_triangle_ineq order_trans)
  2.1203 -qed
  2.1204 -
  2.1205 -lemma setsum_abs_ge_zero: 
  2.1206 -  fixes f :: "'a => ('b::lordered_ab_group_abs)"
  2.1207 -  assumes fin: "finite A" 
  2.1208 -  shows "0 \<le> setsum (%i. abs(f i)) A"
  2.1209 -using fin 
  2.1210 -proof (induct) 
  2.1211 -  case empty thus ?case by simp
  2.1212 -next
  2.1213 -  case (insert x A) thus ?case by (auto intro: order_trans)
  2.1214 -qed
  2.1215 -
  2.1216 -subsubsection {* Cardinality of unions and Sigma sets *}
  2.1217 -
  2.1218 -lemma card_UN_disjoint:
  2.1219 -    "finite I ==> (ALL i:I. finite (A i)) ==>
  2.1220 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  2.1221 -      card (UNION I A) = setsum (%i. card (A i)) I"
  2.1222 -  apply (subst card_eq_setsum)
  2.1223 -  apply (subst finite_UN, assumption+)
  2.1224 -  apply (subgoal_tac
  2.1225 -           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
  2.1226 -  apply (simp add: setsum_UN_disjoint) 
  2.1227 -  apply (simp add: setsum_constant_nat cong: setsum_cong) 
  2.1228 -  done
  2.1229 -
  2.1230 -lemma card_Union_disjoint:
  2.1231 -  "finite C ==> (ALL A:C. finite A) ==>
  2.1232 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
  2.1233 -      card (Union C) = setsum card C"
  2.1234 -  apply (frule card_UN_disjoint [of C id])
  2.1235 -  apply (unfold Union_def id_def, assumption+)
  2.1236 -  done
  2.1237 -
  2.1238 -lemma SigmaI_insert: "y \<notin> A ==>
  2.1239 -  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
  2.1240 -  by auto
  2.1241 -
  2.1242 -lemma card_cartesian_product_singleton: "finite A ==>
  2.1243 -    card({x} <*> A) = card(A)"
  2.1244 -  apply (subgoal_tac "inj_on (%y .(x,y)) A")
  2.1245 -  apply (frule card_image, assumption)
  2.1246 -  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
  2.1247 -  apply (auto simp add: inj_on_def)
  2.1248 -  done
  2.1249 -
  2.1250 -lemma card_SigmaI [rule_format,simp]: "finite A ==>
  2.1251 -  (ALL a:A. finite (B a)) -->
  2.1252 -  card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
  2.1253 -  apply (erule finite_induct, auto)
  2.1254 -  apply (subst SigmaI_insert, assumption)
  2.1255 -  apply (subst card_Un_disjoint)
  2.1256 -  apply (auto intro: finite_SigmaI simp add: card_cartesian_product_singleton)
  2.1257 -  done
  2.1258 -
  2.1259 -lemma card_cartesian_product:
  2.1260 -     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
  2.1261 -  by (simp add: setsum_constant_nat)
  2.1262 -
  2.1263 -
  2.1264 -
  2.1265 -subsection {* Generalized product over a set *}
  2.1266 -
  2.1267 -constdefs
  2.1268 -  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
  2.1269 -  "setprod f A == if finite A then fold (op *) f 1 A else 1"
  2.1270 -
  2.1271 -syntax
  2.1272 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
  2.1273 -
  2.1274 -syntax (xsymbols)
  2.1275 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
  2.1276 -syntax (HTML output)
  2.1277 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
  2.1278 -translations
  2.1279 -  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
  2.1280 -
  2.1281 -syntax
  2.1282 -  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
  2.1283 -
  2.1284 -parse_translation {*
  2.1285 -  let
  2.1286 -    fun Setprod_tr [A] = Syntax.const "setprod" $ Abs ("", dummyT, Bound 0) $ A
  2.1287 -  in [("_Setprod", Setprod_tr)] end;
  2.1288 -*}
  2.1289 -print_translation {*
  2.1290 -let fun setprod_tr' [Abs(x,Tx,t), A] =
  2.1291 -    if t = Bound 0 then Syntax.const "_Setprod" $ A else raise Match
  2.1292 -in
  2.1293 -[("setprod", setprod_tr')]
  2.1294 -end
  2.1295 -*}
  2.1296 -
  2.1297 -
  2.1298 -text{* Instantiation of locales: *}
  2.1299 -
  2.1300 -lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
  2.1301 -by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
  2.1302 -
  2.1303 -lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
  2.1304 -by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
  2.1305 -
  2.1306 -lemma setprod_empty [simp]: "setprod f {} = 1"
  2.1307 -  by (auto simp add: setprod_def)
  2.1308 -
  2.1309 -lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
  2.1310 -    setprod f (insert a A) = f a * setprod f A"
  2.1311 -by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
  2.1312 -
  2.1313 -lemma setprod_reindex:
  2.1314 -     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
  2.1315 -by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
  2.1316 -
  2.1317 -lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
  2.1318 -by (auto simp add: setprod_reindex)
  2.1319 -
  2.1320 -lemma setprod_cong:
  2.1321 -  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
  2.1322 -by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
  2.1323 -
  2.1324 -lemma setprod_reindex_cong: "inj_on f A ==>
  2.1325 -    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
  2.1326 -  by (frule setprod_reindex, simp)
  2.1327 -
  2.1328 -
  2.1329 -lemma setprod_1: "setprod (%i. 1) A = 1"
  2.1330 -  apply (case_tac "finite A")
  2.1331 -  apply (erule finite_induct, auto simp add: mult_ac)
  2.1332 -  apply (simp add: setprod_def)
  2.1333 -  done
  2.1334 -
  2.1335 -lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
  2.1336 -  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
  2.1337 -  apply (erule ssubst, rule setprod_1)
  2.1338 -  apply (rule setprod_cong, auto)
  2.1339 -  done
  2.1340 -
  2.1341 -lemma setprod_Un_Int: "finite A ==> finite B
  2.1342 -    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
  2.1343 -by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
  2.1344 -
  2.1345 -lemma setprod_Un_disjoint: "finite A ==> finite B
  2.1346 -  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
  2.1347 -by (subst setprod_Un_Int [symmetric], auto)
  2.1348 -
  2.1349 -lemma setprod_UN_disjoint:
  2.1350 -    "finite I ==> (ALL i:I. finite (A i)) ==>
  2.1351 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  2.1352 -      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
  2.1353 -by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
  2.1354 -
  2.1355 -lemma setprod_Union_disjoint:
  2.1356 -  "finite C ==> (ALL A:C. finite A) ==>
  2.1357 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
  2.1358 -      setprod f (Union C) = setprod (setprod f) C"
  2.1359 -  apply (frule setprod_UN_disjoint [of C id f])
  2.1360 -  apply (unfold Union_def id_def, assumption+)
  2.1361 -  done
  2.1362 -
  2.1363 -lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
  2.1364 -    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
  2.1365 -    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
  2.1366 -by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
  2.1367 -
  2.1368 -lemma setprod_cartesian_product: "finite A ==> finite B ==>
  2.1369 -    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
  2.1370 -    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
  2.1371 -  by (erule setprod_Sigma, auto)
  2.1372 -
  2.1373 -lemma setprod_timesf:
  2.1374 -  "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
  2.1375 -by(simp add:setprod_def ACe.fold_distrib[OF ACe_mult])
  2.1376 -
  2.1377 -
  2.1378 -subsubsection {* Properties in more restricted classes of structures *}
  2.1379 -
  2.1380 -lemma setprod_eq_1_iff [simp]:
  2.1381 -    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
  2.1382 -  by (induct set: Finites) auto
  2.1383 -
  2.1384 -lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
  2.1385 -  apply (erule finite_induct)
  2.1386 -  apply (auto simp add: power_Suc)
  2.1387 -  done
  2.1388 -
  2.1389 -lemma setprod_zero:
  2.1390 -     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
  2.1391 -  apply (induct set: Finites, force, clarsimp)
  2.1392 -  apply (erule disjE, auto)
  2.1393 -  done
  2.1394 -
  2.1395 -lemma setprod_nonneg [rule_format]:
  2.1396 -     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
  2.1397 -  apply (case_tac "finite A")
  2.1398 -  apply (induct set: Finites, force, clarsimp)
  2.1399 -  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
  2.1400 -  apply (rule mult_mono, assumption+)
  2.1401 -  apply (auto simp add: setprod_def)
  2.1402 -  done
  2.1403 -
  2.1404 -lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
  2.1405 -     --> 0 < setprod f A"
  2.1406 -  apply (case_tac "finite A")
  2.1407 -  apply (induct set: Finites, force, clarsimp)
  2.1408 -  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
  2.1409 -  apply (rule mult_strict_mono, assumption+)
  2.1410 -  apply (auto simp add: setprod_def)
  2.1411 -  done
  2.1412 -
  2.1413 -lemma setprod_nonzero [rule_format]:
  2.1414 -    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
  2.1415 -      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
  2.1416 -  apply (erule finite_induct, auto)
  2.1417 -  done
  2.1418 -
  2.1419 -lemma setprod_zero_eq:
  2.1420 -    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
  2.1421 -     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
  2.1422 -  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
  2.1423 -  done
  2.1424 -
  2.1425 -lemma setprod_nonzero_field:
  2.1426 -    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
  2.1427 -  apply (rule setprod_nonzero, auto)
  2.1428 -  done
  2.1429 -
  2.1430 -lemma setprod_zero_eq_field:
  2.1431 -    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
  2.1432 -  apply (rule setprod_zero_eq, auto)
  2.1433 -  done
  2.1434 -
  2.1435 -lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
  2.1436 -    (setprod f (A Un B) :: 'a ::{field})
  2.1437 -      = setprod f A * setprod f B / setprod f (A Int B)"
  2.1438 -  apply (subst setprod_Un_Int [symmetric], auto)
  2.1439 -  apply (subgoal_tac "finite (A Int B)")
  2.1440 -  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
  2.1441 -  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
  2.1442 -  done
  2.1443 -
  2.1444 -lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
  2.1445 -    (setprod f (A - {a}) :: 'a :: {field}) =
  2.1446 -      (if a:A then setprod f A / f a else setprod f A)"
  2.1447 -  apply (erule finite_induct)
  2.1448 -   apply (auto simp add: insert_Diff_if)
  2.1449 -  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
  2.1450 -  apply (erule ssubst)
  2.1451 -  apply (subst times_divide_eq_right [THEN sym])
  2.1452 -  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
  2.1453 -  done
  2.1454 -
  2.1455 -lemma setprod_inversef: "finite A ==>
  2.1456 -    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
  2.1457 -      setprod (inverse \<circ> f) A = inverse (setprod f A)"
  2.1458 -  apply (erule finite_induct)
  2.1459 -  apply (simp, simp)
  2.1460 -  done
  2.1461 -
  2.1462 -lemma setprod_dividef:
  2.1463 -     "[|finite A;
  2.1464 -        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
  2.1465 -      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
  2.1466 -  apply (subgoal_tac
  2.1467 -         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
  2.1468 -  apply (erule ssubst)
  2.1469 -  apply (subst divide_inverse)
  2.1470 -  apply (subst setprod_timesf)
  2.1471 -  apply (subst setprod_inversef, assumption+, rule refl)
  2.1472 -  apply (rule setprod_cong, rule refl)
  2.1473 -  apply (subst divide_inverse, auto)
  2.1474 -  done
  2.1475 -
  2.1476 -end
     3.1 --- a/src/HOL/Library/Multiset.thy	Fri Dec 10 22:33:16 2004 +0100
     3.2 +++ b/src/HOL/Library/Multiset.thy	Sun Dec 12 16:25:47 2004 +0100
     3.3 @@ -176,7 +176,7 @@
     3.4    apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
     3.5     prefer 2
     3.6     apply (rule ext, simp)
     3.7 -  apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int)
     3.8 +  apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
     3.9    apply (subst Int_commute)
    3.10    apply (simp (no_asm_simp) add: setsum_count_Int)
    3.11    done
     4.1 --- a/src/HOL/NumberTheory/Euler.thy	Fri Dec 10 22:33:16 2004 +0100
     4.2 +++ b/src/HOL/NumberTheory/Euler.thy	Sun Dec 12 16:25:47 2004 +0100
     4.3 @@ -118,7 +118,7 @@
     4.4  done
     4.5  
     4.6  lemma Union_SetS_finite: "2 < p ==> finite (Union (SetS a p))";
     4.7 -  by (auto simp add: SetS_finite SetS_elems_finite finite_union_finite_subsets)
     4.8 +  by (auto simp add: SetS_finite SetS_elems_finite finite_Union)
     4.9  
    4.10  lemma card_setsum_aux: "[| finite S; \<forall>X \<in> S. finite (X::int set); 
    4.11      \<forall>X \<in> S. card X = n |] ==> setsum card S = setsum (%x. n) S";
    4.12 @@ -134,7 +134,7 @@
    4.13        by (auto simp add: prems MultInvPair_prop2 SRStar_card)
    4.14      also have "... = int (setsum card (SetS a p))";
    4.15        by (auto simp add: prems SetS_finite SetS_elems_finite
    4.16 -                         MultInvPair_prop1c [of p a] card_union_disjoint_sets)
    4.17 +                         MultInvPair_prop1c [of p a] card_Union_disjoint)
    4.18      also have "... = int(setsum (%x.2) (SetS a p))";
    4.19        apply (insert prems)
    4.20        apply (auto simp add: SetS_elems_card SetS_finite SetS_elems_finite 
     5.1 --- a/src/HOL/NumberTheory/EulerFermat.thy	Fri Dec 10 22:33:16 2004 +0100
     5.2 +++ b/src/HOL/NumberTheory/EulerFermat.thy	Sun Dec 12 16:25:47 2004 +0100
     5.3 @@ -244,10 +244,10 @@
     5.4    apply (rule card_seteq)
     5.5      prefer 3
     5.6      apply (subst card_image)
     5.7 -      apply (rule_tac [2] RRset2norRR_inj, auto)
     5.8 -     apply (rule_tac [4] RRset2norRR_correct2, auto)
     5.9 +      apply (rule_tac RRset2norRR_inj, auto)
    5.10 +     apply (rule_tac [3] RRset2norRR_correct2, auto)
    5.11      apply (unfold is_RRset_def phi_def norRRset_def)
    5.12 -    apply (auto simp add: RsetR_fin Bnor_fin)
    5.13 +    apply (auto simp add: Bnor_fin)
    5.14    done
    5.15  
    5.16  
     6.1 --- a/src/HOL/NumberTheory/Finite2.thy	Fri Dec 10 22:33:16 2004 +0100
     6.2 +++ b/src/HOL/NumberTheory/Finite2.thy	Sun Dec 12 16:25:47 2004 +0100
     6.3 @@ -124,11 +124,9 @@
     6.4  proof -
     6.5    fix n::int
     6.6    assume "0 \<le> n"
     6.7 -  have "finite {y. y < nat n}"
     6.8 -    by (rule bdd_nat_set_l_finite)
     6.9 -  moreover have "inj_on (%y. int y) {y. y < nat n}"
    6.10 +  have "inj_on (%y. int y) {y. y < nat n}"
    6.11      by (auto simp add: inj_on_def)
    6.12 -  ultimately have "card (int ` {y. y < nat n}) = card {y. y < nat n}"
    6.13 +  hence "card (int ` {y. y < nat n}) = card {y. y < nat n}"
    6.14      by (rule card_image)
    6.15    also from prems have "int ` {y. y < nat n} = {y. 0 \<le> y & y < n}"
    6.16      apply (auto simp add: zless_nat_eq_int_zless image_def)
    6.17 @@ -150,11 +148,9 @@
    6.18  proof -
    6.19    fix n::int
    6.20    assume "0 \<le> n"
    6.21 -  have "finite {x. 0 \<le> x & x < n}"
    6.22 -    by (rule bdd_int_set_l_finite)
    6.23 -  moreover have "inj_on (%x. x+1) {x. 0 \<le> x & x < n}"
    6.24 +  have "inj_on (%x. x+1) {x. 0 \<le> x & x < n}"
    6.25      by (auto simp add: inj_on_def)
    6.26 -  ultimately have "card ((%x. x+1) ` {x. 0 \<le> x & x < n}) = 
    6.27 +  hence "card ((%x. x+1) ` {x. 0 \<le> x & x < n}) = 
    6.28       card {x. 0 \<le> x & x < n}"
    6.29      by (rule card_image)
    6.30    also from prems have "... = nat n"
    6.31 @@ -192,26 +188,10 @@
    6.32  
    6.33  subsection {* Cardinality of finite cartesian products *}
    6.34  
    6.35 -lemma insert_Sigma [simp]: "~(A = {}) ==>
    6.36 -  (insert x A) <*> B = ({ x } <*> B) \<union> (A <*> B)"
    6.37 +(* FIXME could be useful in general but not needed here
    6.38 +lemma insert_Sigma [simp]: "(insert x A) <*> B = ({ x } <*> B) \<union> (A <*> B)"
    6.39    by blast
    6.40 -
    6.41 -lemma cartesian_product_finite: "[| finite A; finite B |] ==> 
    6.42 -    finite (A <*> B)"
    6.43 -  apply (rule_tac F = A in finite_induct)
    6.44 -  by auto
    6.45 -
    6.46 -lemma cartesian_product_card_a [simp]: "finite A ==> 
    6.47 -    card({x} <*> A) = card(A)" 
    6.48 -  apply (subgoal_tac "inj_on (%y .(x,y)) A")
    6.49 -  apply (frule card_image, assumption)
    6.50 -  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
    6.51 -  by (auto simp add: inj_on_def)
    6.52 -
    6.53 -lemma cartesian_product_card [simp]: "[| finite A; finite B |] ==> 
    6.54 -  card (A <*> B) = card(A) * card(B)"
    6.55 -  apply (rule_tac F = A in finite_induct, auto)
    6.56 -  done
    6.57 + *)
    6.58  
    6.59  (******************************************************************)
    6.60  (*                                                                *)
    6.61 @@ -221,55 +201,6 @@
    6.62  
    6.63  subsection {* Lemmas for counting arguments *}
    6.64  
    6.65 -lemma finite_union_finite_subsets: "[| finite A; \<forall>X \<in> A. finite X |] ==> 
    6.66 -    finite (Union A)"
    6.67 -apply (induct set: Finites)
    6.68 -by auto
    6.69 -
    6.70 -lemma finite_index_UNION_finite_sets: "finite A ==> 
    6.71 -    (\<forall>x \<in> A. finite (f x)) --> finite (UNION A f)"
    6.72 -by (induct_tac rule: finite_induct, auto)
    6.73 -
    6.74 -lemma card_union_disjoint_sets: "finite A ==> 
    6.75 -    ((\<forall>X \<in> A. finite X) & (\<forall>X \<in> A. \<forall>Y \<in> A. X \<noteq> Y --> X \<inter> Y = {})) ==> 
    6.76 -    card (Union A) = setsum card A"
    6.77 -  apply auto
    6.78 -  apply (induct set: Finites, auto)
    6.79 -  apply (frule_tac B = "Union F" and A = x in card_Un_Int)
    6.80 -by (auto simp add: finite_union_finite_subsets)
    6.81 -
    6.82 -lemma int_card_eq_setsum [rule_format]: "finite A ==> 
    6.83 -    int (card A) = setsum (%x. 1) A"
    6.84 -  by (erule finite_induct, auto)
    6.85 -
    6.86 -lemma card_indexed_union_disjoint_sets [rule_format]: "finite A ==> 
    6.87 -    ((\<forall>x \<in> A. finite (g x)) & 
    6.88 -    (\<forall>x \<in> A. \<forall>y \<in> A. x \<noteq> y --> (g x) \<inter> (g y) = {})) --> 
    6.89 -      card (UNION A g) = setsum (%x. card (g x)) A"
    6.90 -apply clarify
    6.91 -apply (frule_tac f = "%x.(1::nat)" and A = g in 
    6.92 -    setsum_UN_disjoint)
    6.93 -apply assumption+
    6.94 -apply (subgoal_tac "finite (UNION A g)")
    6.95 -apply (subgoal_tac "(\<Sum>x \<in> UNION A g. 1) = (\<Sum>x \<in> A. \<Sum>x \<in> g x. 1)")
    6.96 -apply (auto simp only: card_eq_setsum)
    6.97 -apply (rule setsum_cong)
    6.98 -by auto
    6.99 -
   6.100 -lemma int_card_indexed_union_disjoint_sets [rule_format]: "finite A ==> 
   6.101 -    ((\<forall>x \<in> A. finite (g x)) & 
   6.102 -    (\<forall>x \<in> A. \<forall>y \<in> A. x \<noteq> y --> (g x) \<inter> (g y) = {})) --> 
   6.103 -       int(card (UNION A g)) = setsum (%x. int( card (g x))) A"
   6.104 -apply clarify
   6.105 -apply (frule_tac f = "%x.(1::int)" and A = g in 
   6.106 -    setsum_UN_disjoint)
   6.107 -apply assumption+
   6.108 -apply (subgoal_tac "finite (UNION A g)")
   6.109 -apply (subgoal_tac "(\<Sum>x \<in> UNION A g. 1) = (\<Sum>x \<in> A. \<Sum>x \<in> g x. 1)")
   6.110 -apply (auto simp only: int_card_eq_setsum)
   6.111 -apply (rule setsum_cong)
   6.112 -by (auto simp add: int_card_eq_setsum)
   6.113 -
   6.114  lemma setsum_bij_eq: "[| finite A; finite B; f ` A \<subseteq> B; inj_on f A; 
   6.115      g ` B \<subseteq> A; inj_on g B |] ==> setsum g B = setsum (g \<circ> f) A"
   6.116  apply (frule_tac h = g and f = f in setsum_reindex)
     7.1 --- a/src/HOL/NumberTheory/Quadratic_Reciprocity.thy	Fri Dec 10 22:33:16 2004 +0100
     7.2 +++ b/src/HOL/NumberTheory/Quadratic_Reciprocity.thy	Sun Dec 12 16:25:47 2004 +0100
     7.3 @@ -249,7 +249,7 @@
     7.4    by (insert q_fact, auto simp add: Q_set_def bdd_int_set_l_le_finite)
     7.5  
     7.6  lemma (in QRTEMP) S_finite: "finite S"
     7.7 -  by (auto simp add: S_def  P_set_finite Q_set_finite cartesian_product_finite)
     7.8 +  by (auto simp add: S_def  P_set_finite Q_set_finite finite_cartesian_product)
     7.9  
    7.10  lemma (in QRTEMP) S1_finite: "finite S1"
    7.11  proof -
    7.12 @@ -516,7 +516,7 @@
    7.13    moreover note P_set_finite
    7.14    ultimately have "int(card (UNION P_set f1)) = 
    7.15        setsum (%x. int(card (f1 x))) P_set"
    7.16 -    by (rule_tac A = P_set in int_card_indexed_union_disjoint_sets, auto)
    7.17 +    by(simp add:card_UN_disjoint int_setsum o_def)
    7.18    moreover have "S1 = UNION P_set f1"
    7.19      by (auto simp add: f1_def S_def S1_def S2_def P_set_def Q_set_def aux1a)
    7.20    ultimately have "int(card (S1)) = setsum (%j. int(card (f1 j))) P_set" 
    7.21 @@ -540,7 +540,7 @@
    7.22    moreover note Q_set_finite
    7.23    ultimately have "int(card (UNION Q_set f2)) = 
    7.24        setsum (%x. int(card (f2 x))) Q_set"
    7.25 -    by (rule_tac A = Q_set in int_card_indexed_union_disjoint_sets, auto)
    7.26 +    by(simp add:card_UN_disjoint int_setsum o_def)
    7.27    moreover have "S2 = UNION Q_set f2"
    7.28      by (auto simp add: f2_def S_def S1_def S2_def P_set_def Q_set_def aux1b)
    7.29    ultimately have "int(card (S2)) = setsum (%j. int(card (f2 j))) Q_set" 
     8.1 --- a/src/HOL/SetInterval.thy	Fri Dec 10 22:33:16 2004 +0100
     8.2 +++ b/src/HOL/SetInterval.thy	Sun Dec 12 16:25:47 2004 +0100
     8.3 @@ -346,7 +346,6 @@
     8.4    apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
     8.5    apply (erule subst)
     8.6    apply (rule card_image)
     8.7 -  apply (rule finite_lessThan)
     8.8    apply (simp add: inj_on_def)
     8.9    apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
    8.10    apply arith
    8.11 @@ -433,7 +432,6 @@
    8.12    apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
    8.13    apply (erule subst)
    8.14    apply (rule card_image)
    8.15 -  apply (rule finite_atLeastZeroLessThan_int)
    8.16    apply (simp add: inj_on_def)
    8.17    apply (rule image_atLeastLessThan_int_shift)
    8.18    done