src/HOL/Finite_Set.thy
 changeset 15402 97204f3b4705 parent 15392 290bc97038c7 child 15409 a063687d24eb
```     1.1 --- a/src/HOL/Finite_Set.thy	Fri Dec 10 22:33:16 2004 +0100
1.2 +++ b/src/HOL/Finite_Set.thy	Sun Dec 12 16:25:47 2004 +0100
1.3 @@ -3,7 +3,9 @@
1.4      Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
1.6
1.7 -FIXME: define card via fold and derive as many lemmas as possible from fold.
1.8 +Get rid of a couple of superfluous finiteness assumptions in lemmas
1.9 +about setsum and card - see FIXME.
1.10 +NB: the analogous lemmas for setprod should also be simplified!
1.11  *)
1.12
1.13  header {* Finite sets *}
1.14 @@ -290,6 +292,10 @@
1.15      "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
1.16    by (unfold Sigma_def) (blast intro!: finite_UN_I)
1.17
1.18 +lemma finite_cartesian_product: "[| finite A; finite B |] ==>
1.19 +    finite (A <*> B)"
1.20 +  by (rule finite_SigmaI)
1.21 +
1.22  lemma finite_Prod_UNIV:
1.23      "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
1.24    apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
1.25 @@ -371,10 +377,6 @@
1.26     apply (auto simp add: finite_Field)
1.27    done
1.28
1.29 -lemma finite_cartesian_product: "[| finite A; finite B |] ==>
1.30 -    finite (A <*> B)"
1.31 -  by (rule finite_SigmaI)
1.32 -
1.33
1.34  subsection {* A fold functional for finite sets *}
1.35
1.36 @@ -437,6 +439,22 @@
1.37    thus ?thesis by (subst commute)
1.38  qed
1.39
1.40 +text{* Instantiation of locales: *}
1.41 +
1.42 +lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
1.44 +
1.46 +by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
1.47 +
1.48 +
1.49 +lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
1.50 +by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
1.51 +
1.52 +lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
1.53 +by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
1.54 +
1.55 +
1.56  subsubsection{*From @{term foldSet} to @{term fold}*}
1.57
1.58  lemma (in ACf) foldSet_determ_aux:
1.59 @@ -476,8 +494,6 @@
1.60  	  and z: "(C,z) \<in> foldSet f g e" and notinC: "c \<notin> C"
1.61  	hence A2: "A = insert c C" and x': "x' = g c \<cdot> z" by auto
1.62  	let ?h = "%i. if h i = b then h n else h i"
1.63 -	have finA: "finite A" by(rule foldSet_imp_finite[OF Afoldx])
1.64 -(* move down? *)
1.65  	have less: "B = ?h`{i. i<n}" (is "_ = ?r")
1.66  	proof
1.67  	  show "B \<subseteq> ?r"
1.68 @@ -534,7 +550,8 @@
1.69  	  let ?D = "B - {c}"
1.70  	  have B: "B = insert c ?D" and C: "C = insert b ?D"
1.71  	    using A1 A2 notinB notinC diff by(blast elim!:equalityE)+
1.72 -	  have "finite ?D" using finA A1 by simp
1.73 +	  have "finite A" by(rule foldSet_imp_finite[OF Afoldx])
1.74 +	  with A1 have "finite ?D" by simp
1.75  	  then obtain d where Dfoldd: "(?D,d) \<in> foldSet f g e"
1.76  	    using finite_imp_foldSet by rules
1.77  	  moreover have cinB: "c \<in> B" using B by(auto)
1.78 @@ -708,12 +725,6 @@
1.80    done
1.81
1.82 -text{* Its definitional form: *}
1.83 -
1.84 -corollary (in ACf) fold_insert_def:
1.85 -    "\<lbrakk> F \<equiv> fold f g e; finite A; x \<notin> A \<rbrakk> \<Longrightarrow> F (insert x A) = f (g x) (F A)"
1.86 -by(simp)
1.87 -
1.88  declare
1.89    empty_foldSetE [rule del]  foldSet.intros [rule del]
1.90    -- {* Delete rules to do with @{text foldSet} relation. *}
1.91 @@ -812,98 +823,548 @@
1.92  done
1.93
1.94
1.95 +subsection {* Generalized summation over a set *}
1.96 +
1.97 +constdefs
1.98 +  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
1.99 +  "setsum f A == if finite A then fold (op +) f 0 A else 0"
1.100 +
1.101 +text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
1.102 +written @{text"\<Sum>x\<in>A. e"}. *}
1.103 +
1.104 +syntax
1.105 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
1.106 +syntax (xsymbols)
1.107 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
1.108 +syntax (HTML output)
1.109 +  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
1.110 +
1.111 +translations -- {* Beware of argument permutation! *}
1.112 +  "SUM i:A. b" == "setsum (%i. b) A"
1.113 +  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
1.114 +
1.115 +text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
1.116 + @{text"\<Sum>x|P. e"}. *}
1.117 +
1.118 +syntax
1.119 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
1.120 +syntax (xsymbols)
1.121 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
1.122 +syntax (HTML output)
1.123 +  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
1.124 +
1.125 +translations
1.126 +  "SUM x|P. t" => "setsum (%x. t) {x. P}"
1.127 +  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
1.128 +
1.129 +text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
1.130 +
1.131 +syntax
1.132 +  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
1.133 +
1.134 +parse_translation {*
1.135 +  let
1.136 +    fun Setsum_tr [A] = Syntax.const "setsum" \$ Abs ("", dummyT, Bound 0) \$ A
1.137 +  in [("_Setsum", Setsum_tr)] end;
1.138 +*}
1.139 +
1.140 +print_translation {*
1.141 +let
1.142 +  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" \$ A
1.143 +    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) \$ Abs(y,Ty,P)] =
1.144 +       if x<>y then raise Match
1.145 +       else let val x' = Syntax.mark_bound x
1.146 +                val t' = subst_bound(x',t)
1.147 +                val P' = subst_bound(x',P)
1.148 +            in Syntax.const "_qsetsum" \$ Syntax.mark_bound x \$ P' \$ t' end
1.149 +in
1.150 +[("setsum", setsum_tr')]
1.151 +end
1.152 +*}
1.153 +
1.154 +lemma setsum_empty [simp]: "setsum f {} = 0"
1.155 +  by (simp add: setsum_def)
1.156 +
1.157 +lemma setsum_insert [simp]:
1.158 +    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
1.160 +
1.161 +lemma setsum_reindex:
1.162 +     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
1.164 +
1.165 +lemma setsum_reindex_id:
1.166 +     "inj_on f B ==> setsum f B = setsum id (f ` B)"
1.167 +by (auto simp add: setsum_reindex)
1.168 +
1.169 +lemma setsum_cong:
1.170 +  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
1.171 +by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
1.172 +
1.173 +lemma setsum_reindex_cong:
1.174 +     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|]
1.175 +      ==> setsum h B = setsum g A"
1.176 +  by (simp add: setsum_reindex cong: setsum_cong)
1.177 +
1.178 +lemma setsum_0: "setsum (%i. 0) A = 0"
1.179 +apply (clarsimp simp: setsum_def)
1.180 +apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
1.181 +done
1.182 +
1.183 +lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
1.184 +  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
1.185 +  apply (erule ssubst, rule setsum_0)
1.186 +  apply (rule setsum_cong, auto)
1.187 +  done
1.188 +
1.189 +lemma setsum_Un_Int: "finite A ==> finite B ==>
1.190 +  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
1.191 +  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
1.193 +
1.194 +lemma setsum_Un_disjoint: "finite A ==> finite B
1.195 +  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
1.196 +by (subst setsum_Un_Int [symmetric], auto)
1.197 +
1.198 +(* FIXME get rid of finite I. If infinite, rhs is directly 0, and UNION I A
1.199 +is also infinite and hence also 0 *)
1.200 +lemma setsum_UN_disjoint:
1.201 +    "finite I ==> (ALL i:I. finite (A i)) ==>
1.202 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.203 +      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
1.205 +
1.206 +
1.207 +(* FIXME get rid of finite C. If infinite, rhs is directly 0, and Union C
1.208 +is also infinite and hence also 0 *)
1.209 +lemma setsum_Union_disjoint:
1.210 +  "finite C ==> (ALL A:C. finite A) ==>
1.211 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.212 +      setsum f (Union C) = setsum (setsum f) C"
1.213 +  apply (frule setsum_UN_disjoint [of C id f])
1.214 +  apply (unfold Union_def id_def, assumption+)
1.215 +  done
1.216 +
1.217 +(* FIXME get rid of finite A. If infinite, lhs is directly 0, and SIGMA A B
1.218 +is either infinite or empty, and in both cases the rhs is also 0 *)
1.219 +lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
1.220 +    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
1.221 +    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
1.223 +
1.224 +lemma setsum_cartesian_product: "finite A ==> finite B ==>
1.225 +    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
1.226 +    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
1.227 +  by (erule setsum_Sigma, auto)
1.228 +
1.229 +lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
1.231 +
1.232 +
1.233 +subsubsection {* Properties in more restricted classes of structures *}
1.234 +
1.235 +lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
1.236 +  apply (case_tac "finite A")
1.237 +   prefer 2 apply (simp add: setsum_def)
1.238 +  apply (erule rev_mp)
1.239 +  apply (erule finite_induct, auto)
1.240 +  done
1.241 +
1.242 +lemma setsum_eq_0_iff [simp]:
1.243 +    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
1.244 +  by (induct set: Finites) auto
1.245 +
1.246 +lemma setsum_Un_nat: "finite A ==> finite B ==>
1.247 +    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
1.248 +  -- {* For the natural numbers, we have subtraction. *}
1.249 +  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
1.250 +
1.251 +lemma setsum_Un: "finite A ==> finite B ==>
1.252 +    (setsum f (A Un B) :: 'a :: ab_group_add) =
1.253 +      setsum f A + setsum f B - setsum f (A Int B)"
1.254 +  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
1.255 +
1.256 +lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
1.257 +    (if a:A then setsum f A - f a else setsum f A)"
1.258 +  apply (case_tac "finite A")
1.259 +   prefer 2 apply (simp add: setsum_def)
1.260 +  apply (erule finite_induct)
1.261 +   apply (auto simp add: insert_Diff_if)
1.262 +  apply (drule_tac a = a in mk_disjoint_insert, auto)
1.263 +  done
1.264 +
1.265 +lemma setsum_diff1: "finite A \<Longrightarrow>
1.266 +  (setsum f (A - {a}) :: ('a::ab_group_add)) =
1.267 +  (if a:A then setsum f A - f a else setsum f A)"
1.268 +  by (erule finite_induct) (auto simp add: insert_Diff_if)
1.269 +
1.270 +(* By Jeremy Siek: *)
1.271 +
1.272 +lemma setsum_diff_nat:
1.273 +  assumes finB: "finite B"
1.274 +  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
1.275 +using finB
1.276 +proof (induct)
1.277 +  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
1.278 +next
1.279 +  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
1.280 +    and xFinA: "insert x F \<subseteq> A"
1.281 +    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
1.282 +  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
1.283 +  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
1.284 +    by (simp add: setsum_diff1_nat)
1.285 +  from xFinA have "F \<subseteq> A" by simp
1.286 +  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
1.287 +  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
1.288 +    by simp
1.289 +  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
1.290 +  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
1.291 +    by simp
1.292 +  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
1.293 +  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
1.294 +    by simp
1.295 +  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
1.296 +qed
1.297 +
1.298 +lemma setsum_diff:
1.299 +  assumes le: "finite A" "B \<subseteq> A"
1.300 +  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
1.301 +proof -
1.302 +  from le have finiteB: "finite B" using finite_subset by auto
1.303 +  show ?thesis using finiteB le
1.304 +    proof (induct)
1.305 +      case empty
1.306 +      thus ?case by auto
1.307 +    next
1.308 +      case (insert x F)
1.309 +      thus ?case using le finiteB
1.310 +	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
1.311 +    qed
1.312 +  qed
1.313 +
1.314 +lemma setsum_mono:
1.315 +  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
1.316 +  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
1.317 +proof (cases "finite K")
1.318 +  case True
1.319 +  thus ?thesis using le
1.320 +  proof (induct)
1.321 +    case empty
1.322 +    thus ?case by simp
1.323 +  next
1.324 +    case insert
1.325 +    thus ?case using add_mono
1.326 +      by force
1.327 +  qed
1.328 +next
1.329 +  case False
1.330 +  thus ?thesis
1.331 +    by (simp add: setsum_def)
1.332 +qed
1.333 +
1.334 +lemma setsum_mono2_nat:
1.335 +  assumes fin: "finite B" and sub: "A \<subseteq> B"
1.336 +shows "setsum f A \<le> (setsum f B :: nat)"
1.337 +proof -
1.338 +  have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
1.339 +  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
1.340 +    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
1.341 +  also have "A \<union> (B-A) = B" using sub by blast
1.342 +  finally show ?thesis .
1.343 +qed
1.344 +
1.345 +lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
1.346 +  - setsum f A"
1.347 +  by (induct set: Finites, auto)
1.348 +
1.349 +lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
1.350 +  setsum f A - setsum g A"
1.352 +
1.353 +lemma setsum_nonneg: "[| finite A;
1.354 +    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
1.355 +    0 \<le> setsum f A";
1.356 +  apply (induct set: Finites, auto)
1.357 +  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
1.358 +  apply (blast intro: add_mono)
1.359 +  done
1.360 +
1.361 +lemma setsum_nonpos: "[| finite A;
1.362 +    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
1.363 +    setsum f A \<le> 0";
1.364 +  apply (induct set: Finites, auto)
1.365 +  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
1.366 +  apply (blast intro: add_mono)
1.367 +  done
1.368 +
1.369 +lemma setsum_mult:
1.370 +  fixes f :: "'a => ('b::semiring_0_cancel)"
1.371 +  shows "r * setsum f A = setsum (%n. r * f n) A"
1.372 +proof (cases "finite A")
1.373 +  case True
1.374 +  thus ?thesis
1.375 +  proof (induct)
1.376 +    case empty thus ?case by simp
1.377 +  next
1.378 +    case (insert x A) thus ?case by (simp add: right_distrib)
1.379 +  qed
1.380 +next
1.381 +  case False thus ?thesis by (simp add: setsum_def)
1.382 +qed
1.383 +
1.384 +lemma setsum_abs:
1.385 +  fixes f :: "'a => ('b::lordered_ab_group_abs)"
1.386 +  assumes fin: "finite A"
1.387 +  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
1.388 +using fin
1.389 +proof (induct)
1.390 +  case empty thus ?case by simp
1.391 +next
1.392 +  case (insert x A)
1.393 +  thus ?case by (auto intro: abs_triangle_ineq order_trans)
1.394 +qed
1.395 +
1.396 +lemma setsum_abs_ge_zero:
1.397 +  fixes f :: "'a => ('b::lordered_ab_group_abs)"
1.398 +  assumes fin: "finite A"
1.399 +  shows "0 \<le> setsum (%i. abs(f i)) A"
1.400 +using fin
1.401 +proof (induct)
1.402 +  case empty thus ?case by simp
1.403 +next
1.404 +  case (insert x A) thus ?case by (auto intro: order_trans)
1.405 +qed
1.406 +
1.407 +
1.408 +subsection {* Generalized product over a set *}
1.409 +
1.410 +constdefs
1.411 +  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
1.412 +  "setprod f A == if finite A then fold (op *) f 1 A else 1"
1.413 +
1.414 +syntax
1.415 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
1.416 +
1.417 +syntax (xsymbols)
1.418 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
1.419 +syntax (HTML output)
1.420 +  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
1.421 +translations
1.422 +  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
1.423 +
1.424 +syntax
1.425 +  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
1.426 +
1.427 +parse_translation {*
1.428 +  let
1.429 +    fun Setprod_tr [A] = Syntax.const "setprod" \$ Abs ("", dummyT, Bound 0) \$ A
1.430 +  in [("_Setprod", Setprod_tr)] end;
1.431 +*}
1.432 +print_translation {*
1.433 +let fun setprod_tr' [Abs(x,Tx,t), A] =
1.434 +    if t = Bound 0 then Syntax.const "_Setprod" \$ A else raise Match
1.435 +in
1.436 +[("setprod", setprod_tr')]
1.437 +end
1.438 +*}
1.439 +
1.440 +
1.441 +lemma setprod_empty [simp]: "setprod f {} = 1"
1.442 +  by (auto simp add: setprod_def)
1.443 +
1.444 +lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
1.445 +    setprod f (insert a A) = f a * setprod f A"
1.446 +by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
1.447 +
1.448 +lemma setprod_reindex:
1.449 +     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
1.450 +by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
1.451 +
1.452 +lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
1.453 +by (auto simp add: setprod_reindex)
1.454 +
1.455 +lemma setprod_cong:
1.456 +  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
1.457 +by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
1.458 +
1.459 +lemma setprod_reindex_cong: "inj_on f A ==>
1.460 +    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
1.461 +  by (frule setprod_reindex, simp)
1.462 +
1.463 +
1.464 +lemma setprod_1: "setprod (%i. 1) A = 1"
1.465 +  apply (case_tac "finite A")
1.466 +  apply (erule finite_induct, auto simp add: mult_ac)
1.467 +  apply (simp add: setprod_def)
1.468 +  done
1.469 +
1.470 +lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
1.471 +  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
1.472 +  apply (erule ssubst, rule setprod_1)
1.473 +  apply (rule setprod_cong, auto)
1.474 +  done
1.475 +
1.476 +lemma setprod_Un_Int: "finite A ==> finite B
1.477 +    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
1.478 +by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
1.479 +
1.480 +lemma setprod_Un_disjoint: "finite A ==> finite B
1.481 +  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
1.482 +by (subst setprod_Un_Int [symmetric], auto)
1.483 +
1.484 +lemma setprod_UN_disjoint:
1.485 +    "finite I ==> (ALL i:I. finite (A i)) ==>
1.486 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.487 +      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
1.488 +by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
1.489 +
1.490 +lemma setprod_Union_disjoint:
1.491 +  "finite C ==> (ALL A:C. finite A) ==>
1.492 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.493 +      setprod f (Union C) = setprod (setprod f) C"
1.494 +  apply (frule setprod_UN_disjoint [of C id f])
1.495 +  apply (unfold Union_def id_def, assumption+)
1.496 +  done
1.497 +
1.498 +lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
1.499 +    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
1.500 +    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
1.501 +by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
1.502 +
1.503 +lemma setprod_cartesian_product: "finite A ==> finite B ==>
1.504 +    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
1.505 +    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
1.506 +  by (erule setprod_Sigma, auto)
1.507 +
1.508 +lemma setprod_timesf:
1.509 +  "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
1.511 +
1.512 +
1.513 +subsubsection {* Properties in more restricted classes of structures *}
1.514 +
1.515 +lemma setprod_eq_1_iff [simp]:
1.516 +    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
1.517 +  by (induct set: Finites) auto
1.518 +
1.519 +lemma setprod_zero:
1.520 +     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
1.521 +  apply (induct set: Finites, force, clarsimp)
1.522 +  apply (erule disjE, auto)
1.523 +  done
1.524 +
1.525 +lemma setprod_nonneg [rule_format]:
1.526 +     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
1.527 +  apply (case_tac "finite A")
1.528 +  apply (induct set: Finites, force, clarsimp)
1.529 +  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
1.530 +  apply (rule mult_mono, assumption+)
1.531 +  apply (auto simp add: setprod_def)
1.532 +  done
1.533 +
1.534 +lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
1.535 +     --> 0 < setprod f A"
1.536 +  apply (case_tac "finite A")
1.537 +  apply (induct set: Finites, force, clarsimp)
1.538 +  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
1.539 +  apply (rule mult_strict_mono, assumption+)
1.540 +  apply (auto simp add: setprod_def)
1.541 +  done
1.542 +
1.543 +lemma setprod_nonzero [rule_format]:
1.544 +    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
1.545 +      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
1.546 +  apply (erule finite_induct, auto)
1.547 +  done
1.548 +
1.549 +lemma setprod_zero_eq:
1.550 +    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
1.551 +     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
1.552 +  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
1.553 +  done
1.554 +
1.555 +lemma setprod_nonzero_field:
1.556 +    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
1.557 +  apply (rule setprod_nonzero, auto)
1.558 +  done
1.559 +
1.560 +lemma setprod_zero_eq_field:
1.561 +    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
1.562 +  apply (rule setprod_zero_eq, auto)
1.563 +  done
1.564 +
1.565 +lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
1.566 +    (setprod f (A Un B) :: 'a ::{field})
1.567 +      = setprod f A * setprod f B / setprod f (A Int B)"
1.568 +  apply (subst setprod_Un_Int [symmetric], auto)
1.569 +  apply (subgoal_tac "finite (A Int B)")
1.570 +  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
1.571 +  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
1.572 +  done
1.573 +
1.574 +lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
1.575 +    (setprod f (A - {a}) :: 'a :: {field}) =
1.576 +      (if a:A then setprod f A / f a else setprod f A)"
1.577 +  apply (erule finite_induct)
1.578 +   apply (auto simp add: insert_Diff_if)
1.579 +  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
1.580 +  apply (erule ssubst)
1.581 +  apply (subst times_divide_eq_right [THEN sym])
1.582 +  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
1.583 +  done
1.584 +
1.585 +lemma setprod_inversef: "finite A ==>
1.586 +    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
1.587 +      setprod (inverse \<circ> f) A = inverse (setprod f A)"
1.588 +  apply (erule finite_induct)
1.589 +  apply (simp, simp)
1.590 +  done
1.591 +
1.592 +lemma setprod_dividef:
1.593 +     "[|finite A;
1.594 +        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
1.595 +      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
1.596 +  apply (subgoal_tac
1.597 +         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
1.598 +  apply (erule ssubst)
1.599 +  apply (subst divide_inverse)
1.600 +  apply (subst setprod_timesf)
1.601 +  apply (subst setprod_inversef, assumption+, rule refl)
1.602 +  apply (rule setprod_cong, rule refl)
1.603 +  apply (subst divide_inverse, auto)
1.604 +  done
1.605 +
1.606  subsection {* Finite cardinality *}
1.607
1.608 -text {*
1.609 -  This definition, although traditional, is ugly to work with: @{text
1.610 -  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
1.611 -  switched to an inductive one:
1.612 +text {* This definition, although traditional, is ugly to work with:
1.613 +@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
1.614 +But now that we have @{text setsum} things are easy:
1.615  *}
1.616
1.617 -consts cardR :: "('a set \<times> nat) set"
1.618 -
1.619 -inductive cardR
1.620 -  intros
1.621 -    EmptyI: "({}, 0) : cardR"
1.622 -    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
1.623 -
1.624  constdefs
1.625    card :: "'a set => nat"
1.626 -  "card A == THE n. (A, n) : cardR"
1.627 -
1.628 -inductive_cases cardR_emptyE: "({}, n) : cardR"
1.629 -inductive_cases cardR_insertE: "(insert a A,n) : cardR"
1.630 -
1.631 -lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
1.632 -  by (induct set: cardR) simp_all
1.633 -
1.634 -lemma cardR_determ_aux1:
1.635 -    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
1.636 -  apply (induct set: cardR, auto)
1.637 -  apply (simp add: insert_Diff_if, auto)
1.638 -  apply (drule cardR_SucD)
1.639 -  apply (blast intro!: cardR.intros)
1.640 -  done
1.641 -
1.642 -lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
1.643 -  by (drule cardR_determ_aux1) auto
1.644 -
1.645 -lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
1.646 -  apply (induct set: cardR)
1.647 -   apply (safe elim!: cardR_emptyE cardR_insertE)
1.648 -  apply (rename_tac B b m)
1.649 -  apply (case_tac "a = b")
1.650 -   apply (subgoal_tac "A = B")
1.651 -    prefer 2 apply (blast elim: equalityE, blast)
1.652 -  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
1.653 -   prefer 2
1.654 -   apply (rule_tac x = "A Int B" in exI)
1.655 -   apply (blast elim: equalityE)
1.656 -  apply (frule_tac A = B in cardR_SucD)
1.657 -  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
1.658 -  done
1.659 -
1.660 -lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
1.661 -  by (induct set: cardR) simp_all
1.662 -
1.663 -lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
1.664 -  by (induct set: Finites) (auto intro!: cardR.intros)
1.665 -
1.666 -lemma card_equality: "(A,n) : cardR ==> card A = n"
1.667 -  by (unfold card_def) (blast intro: cardR_determ)
1.668 +  "card A == setsum (%x. 1::nat) A"
1.669
1.670  lemma card_empty [simp]: "card {} = 0"
1.671 -  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
1.672 +  by (simp add: card_def)
1.673 +
1.674 +lemma card_eq_setsum: "card A = setsum (%x. 1) A"
1.676
1.677  lemma card_insert_disjoint [simp]:
1.678    "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
1.679 -proof -
1.680 -  assume x: "x \<notin> A"
1.681 -  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
1.682 -    apply (auto intro!: cardR.intros)
1.683 -    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
1.684 -     apply (force dest: cardR_imp_finite)
1.685 -    apply (blast intro!: cardR.intros intro: cardR_determ)
1.686 -    done
1.687 -  assume "finite A"
1.688 -  thus ?thesis
1.689 -    apply (simp add: card_def aux)
1.690 -    apply (rule the_equality)
1.691 -     apply (auto intro: finite_imp_cardR
1.692 -       cong: conj_cong simp: card_def [symmetric] card_equality)
1.693 -    done
1.694 -qed
1.696 +
1.697 +lemma card_insert_if:
1.698 +    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
1.699 +  by (simp add: insert_absorb)
1.700
1.701  lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
1.702    apply auto
1.703    apply (drule_tac a = x in mk_disjoint_insert, clarify)
1.704 -  apply (rotate_tac -1, auto)
1.705 +  apply (auto)
1.706    done
1.707
1.708 -lemma card_insert_if:
1.709 -    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
1.710 -  by (simp add: insert_absorb)
1.711 -
1.712  lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
1.713  apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
1.714  apply(simp del:insert_Diff_single)
1.715 @@ -923,6 +1384,9 @@
1.716  lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
1.718
1.719 +lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
1.720 +by (simp add: card_def setsum_mono2_nat)
1.721 +
1.722  lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
1.723    apply (induct set: Finites, simp, clarify)
1.724    apply (subgoal_tac "finite A & A - {x} <= F")
1.725 @@ -937,33 +1401,17 @@
1.726    apply (blast dest: card_seteq)
1.727    done
1.728
1.729 -lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
1.730 -  apply (case_tac "A = B", simp)
1.731 -  apply (simp add: linorder_not_less [symmetric])
1.732 -  apply (blast dest: card_seteq intro: order_less_imp_le)
1.733 -  done
1.734 -
1.735  lemma card_Un_Int: "finite A ==> finite B
1.736      ==> card A + card B = card (A Un B) + card (A Int B)"
1.737 -  apply (induct set: Finites, simp)
1.738 -  apply (simp add: insert_absorb Int_insert_left)
1.739 -  done
1.741
1.742  lemma card_Un_disjoint: "finite A ==> finite B
1.743      ==> A Int B = {} ==> card (A Un B) = card A + card B"
1.745
1.746  lemma card_Diff_subset:
1.747 -    "finite A ==> B <= A ==> card A - card B = card (A - B)"
1.748 -  apply (subgoal_tac "(A - B) Un B = A")
1.749 -   prefer 2 apply blast
1.750 -  apply (rule nat_add_right_cancel [THEN iffD1])
1.751 -  apply (rule card_Un_disjoint [THEN subst])
1.752 -     apply (erule_tac [4] ssubst)
1.753 -     prefer 3 apply blast
1.756 -  done
1.757 +  "finite B ==> B <= A ==> card (A - B) = card A - card B"
1.759
1.760  lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
1.761    apply (rule Suc_less_SucD)
1.762 @@ -987,8 +1435,8 @@
1.763  by (erule psubsetI, blast)
1.764
1.765  lemma insert_partition:
1.766 -     "[| x \<notin> F; \<forall>c1\<in>insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 --> c1 \<inter> c2 = {}|]
1.767 -      ==> x \<inter> \<Union> F = {}"
1.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>
1.769 +  \<Longrightarrow> x \<inter> \<Union> F = {}"
1.770  by auto
1.771
1.772  (* main cardinality theorem *)
1.773 @@ -1004,6 +1452,39 @@
1.774  done
1.775
1.776
1.777 +lemma setsum_constant_nat:
1.778 +    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
1.779 +  -- {* Generalized to any @{text comm_semiring_1_cancel} in
1.780 +        @{text IntDef} as @{text setsum_constant}. *}
1.781 +by (erule finite_induct, auto)
1.782 +
1.783 +lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
1.784 +  apply (erule finite_induct)
1.785 +  apply (auto simp add: power_Suc)
1.786 +  done
1.787 +
1.788 +
1.789 +subsubsection {* Cardinality of unions *}
1.790 +
1.791 +lemma card_UN_disjoint:
1.792 +    "finite I ==> (ALL i:I. finite (A i)) ==>
1.793 +        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.794 +      card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
1.795 +  apply (simp add: card_def)
1.796 +  apply (subgoal_tac
1.797 +           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
1.798 +  apply (simp add: setsum_UN_disjoint)
1.799 +  apply (simp add: setsum_constant_nat cong: setsum_cong)
1.800 +  done
1.801 +
1.802 +lemma card_Union_disjoint:
1.803 +  "finite C ==> (ALL A:C. finite A) ==>
1.804 +        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.805 +      card (Union C) = setsum card C"
1.806 +  apply (frule card_UN_disjoint [of C id])
1.807 +  apply (unfold Union_def id_def, assumption+)
1.808 +  done
1.809 +
1.810  subsubsection {* Cardinality of image *}
1.811
1.812  lemma card_image_le: "finite A ==> card (f ` A) <= card A"
1.813 @@ -1011,8 +1492,8 @@
1.814    apply (simp add: le_SucI finite_imageI card_insert_if)
1.815    done
1.816
1.817 -lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
1.818 -by (induct set: Finites, simp_all)
1.819 +lemma card_image: "inj_on f A ==> card (f ` A) = card A"
1.821
1.822  lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
1.823    by (simp add: card_seteq card_image)
1.824 @@ -1030,6 +1511,46 @@
1.825  by(blast intro: card_image eq_card_imp_inj_on)
1.826
1.827
1.828 +lemma card_inj_on_le:
1.829 +    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
1.830 +apply (subgoal_tac "finite A")
1.831 + apply (force intro: card_mono simp add: card_image [symmetric])
1.832 +apply (blast intro: finite_imageD dest: finite_subset)
1.833 +done
1.834 +
1.835 +lemma card_bij_eq:
1.836 +    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
1.837 +       finite A; finite B |] ==> card A = card B"
1.838 +  by (auto intro: le_anti_sym card_inj_on_le)
1.839 +
1.840 +
1.841 +subsubsection {* Cardinality of products *}
1.842 +
1.843 +(*
1.844 +lemma SigmaI_insert: "y \<notin> A ==>
1.845 +  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
1.846 +  by auto
1.847 +*)
1.848 +
1.849 +lemma card_SigmaI [simp]:
1.850 +  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
1.851 +  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
1.853 +
1.854 +(* FIXME get rid of prems *)
1.855 +lemma card_cartesian_product:
1.856 +     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
1.857 +  by (simp add: setsum_constant_nat)
1.858 +
1.859 +(* FIXME should really be a consequence of card_cartesian_product *)
1.860 +lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
1.861 +  apply (subgoal_tac "inj_on (%y .(x,y)) A")
1.862 +  apply (frule card_image)
1.863 +  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
1.864 +  apply (auto simp add: inj_on_def)
1.865 +  done
1.866 +
1.867 +
1.868  subsubsection {* Cardinality of the Powerset *}
1.869
1.870  lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
1.871 @@ -1084,18 +1605,6 @@
1.872    apply (auto intro: finite_subset)
1.873    done
1.874
1.875 -lemma card_inj_on_le:
1.876 -    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
1.877 -apply (subgoal_tac "finite A")
1.878 - apply (force intro: card_mono simp add: card_image [symmetric])
1.879 -apply (blast intro: finite_imageD dest: finite_subset)
1.880 -done
1.881 -
1.882 -lemma card_bij_eq:
1.883 -    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
1.884 -       finite A; finite B |] ==> card A = card B"
1.885 -  by (auto intro: le_anti_sym card_inj_on_le)
1.886 -
1.887  text{*There are as many subsets of @{term A} having cardinality @{term k}
1.888   as there are sets obtained from the former by inserting a fixed element
1.889   @{term x} into each.*}
1.890 @@ -1371,7 +1880,7 @@
1.891    Max :: "('a::linorder)set => 'a"
1.892    "Max  ==  fold1 max"
1.893
1.894 -text{* Now we instantiate the recursiuon equations and declare them
1.895 +text{* Now we instantiate the recursion equations and declare them
1.896  simplification rules: *}
1.897
1.898  declare
1.899 @@ -1447,577 +1956,4 @@
1.900  qed
1.901
1.902
1.903 -subsection {* Generalized summation over a set *}
1.904 -
1.905 -constdefs
1.906 -  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
1.907 -  "setsum f A == if finite A then fold (op +) f 0 A else 0"
1.908 -
1.909 -text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
1.910 -written @{text"\<Sum>x\<in>A. e"}. *}
1.911 -
1.912 -syntax
1.913 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
1.914 -syntax (xsymbols)
1.915 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
1.916 -syntax (HTML output)
1.917 -  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
1.918 -
1.919 -translations -- {* Beware of argument permutation! *}
1.920 -  "SUM i:A. b" == "setsum (%i. b) A"
1.921 -  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
1.922 -
1.923 -text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
1.924 - @{text"\<Sum>x|P. e"}. *}
1.925 -
1.926 -syntax
1.927 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
1.928 -syntax (xsymbols)
1.929 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
1.930 -syntax (HTML output)
1.931 -  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
1.932 -
1.933 -translations
1.934 -  "SUM x|P. t" => "setsum (%x. t) {x. P}"
1.935 -  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
1.936 -
1.937 -text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
1.938 -
1.939 -syntax
1.940 -  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
1.941 -
1.942 -parse_translation {*
1.943 -  let
1.944 -    fun Setsum_tr [A] = Syntax.const "setsum" \$ Abs ("", dummyT, Bound 0) \$ A
1.945 -  in [("_Setsum", Setsum_tr)] end;
1.946 -*}
1.947 -
1.948 -print_translation {*
1.949 -let
1.950 -  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" \$ A
1.951 -    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) \$ Abs(y,Ty,P)] =
1.952 -       if x<>y then raise Match
1.953 -       else let val x' = Syntax.mark_bound x
1.954 -                val t' = subst_bound(x',t)
1.955 -                val P' = subst_bound(x',P)
1.956 -            in Syntax.const "_qsetsum" \$ Syntax.mark_bound x \$ P' \$ t' end
1.957 -in
1.958 -[("setsum", setsum_tr')]
1.959  end
1.960 -*}
1.961 -
1.962 -text{* Instantiation of locales: *}
1.963 -
1.964 -lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
1.966 -
1.968 -by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
1.969 -
1.970 -lemma setsum_empty [simp]: "setsum f {} = 0"
1.971 -  by (simp add: setsum_def)
1.972 -
1.973 -lemma setsum_insert [simp]:
1.974 -    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
1.976 -
1.977 -lemma setsum_reindex:
1.978 -     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
1.980 -
1.981 -lemma setsum_reindex_id:
1.982 -     "inj_on f B ==> setsum f B = setsum id (f ` B)"
1.983 -by (auto simp add: setsum_reindex)
1.984 -
1.985 -lemma setsum_cong:
1.986 -  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
1.987 -by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
1.988 -
1.989 -lemma setsum_reindex_cong:
1.990 -     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|]
1.991 -      ==> setsum h B = setsum g A"
1.992 -  by (simp add: setsum_reindex cong: setsum_cong)
1.993 -
1.994 -lemma setsum_0: "setsum (%i. 0) A = 0"
1.995 -apply (clarsimp simp: setsum_def)
1.996 -apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
1.997 -done
1.998 -
1.999 -lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
1.1000 -  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
1.1001 -  apply (erule ssubst, rule setsum_0)
1.1002 -  apply (rule setsum_cong, auto)
1.1003 -  done
1.1004 -
1.1005 -lemma card_eq_setsum: "finite A ==> card A = setsum (%x. 1) A"
1.1006 -  -- {* Could allow many @{text "card"} proofs to be simplified. *}
1.1007 -  by (induct set: Finites) auto
1.1008 -
1.1009 -lemma setsum_Un_Int: "finite A ==> finite B ==>
1.1010 -  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
1.1011 -  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
1.1013 -
1.1014 -lemma setsum_Un_disjoint: "finite A ==> finite B
1.1015 -  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
1.1016 -by (subst setsum_Un_Int [symmetric], auto)
1.1017 -
1.1018 -lemma setsum_UN_disjoint:
1.1019 -    "finite I ==> (ALL i:I. finite (A i)) ==>
1.1020 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.1021 -      setsum f (UNION I A) = setsum (%i. setsum f (A i)) I"
1.1023 -
1.1024 -
1.1025 -lemma setsum_Union_disjoint:
1.1026 -  "finite C ==> (ALL A:C. finite A) ==>
1.1027 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.1028 -      setsum f (Union C) = setsum (setsum f) C"
1.1029 -  apply (frule setsum_UN_disjoint [of C id f])
1.1030 -  apply (unfold Union_def id_def, assumption+)
1.1031 -  done
1.1032 -
1.1033 -lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
1.1034 -    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
1.1035 -    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
1.1037 -
1.1038 -lemma setsum_cartesian_product: "finite A ==> finite B ==>
1.1039 -    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
1.1040 -    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
1.1041 -  by (erule setsum_Sigma, auto)
1.1042 -
1.1043 -lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
1.1045 -
1.1046 -
1.1047 -subsubsection {* Properties in more restricted classes of structures *}
1.1048 -
1.1049 -lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
1.1050 -  apply (case_tac "finite A")
1.1051 -   prefer 2 apply (simp add: setsum_def)
1.1052 -  apply (erule rev_mp)
1.1053 -  apply (erule finite_induct, auto)
1.1054 -  done
1.1055 -
1.1056 -lemma setsum_eq_0_iff [simp]:
1.1057 -    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
1.1058 -  by (induct set: Finites) auto
1.1059 -
1.1060 -lemma setsum_constant_nat:
1.1061 -    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
1.1062 -  -- {* Generalized to any @{text comm_semiring_1_cancel} in
1.1063 -        @{text IntDef} as @{text setsum_constant}. *}
1.1064 -  by (erule finite_induct, auto)
1.1065 -
1.1066 -lemma setsum_Un: "finite A ==> finite B ==>
1.1067 -    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
1.1068 -  -- {* For the natural numbers, we have subtraction. *}
1.1069 -  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
1.1070 -
1.1071 -lemma setsum_Un_ring: "finite A ==> finite B ==>
1.1072 -    (setsum f (A Un B) :: 'a :: ab_group_add) =
1.1073 -      setsum f A + setsum f B - setsum f (A Int B)"
1.1074 -  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
1.1075 -
1.1076 -lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
1.1077 -    (if a:A then setsum f A - f a else setsum f A)"
1.1078 -  apply (case_tac "finite A")
1.1079 -   prefer 2 apply (simp add: setsum_def)
1.1080 -  apply (erule finite_induct)
1.1081 -   apply (auto simp add: insert_Diff_if)
1.1082 -  apply (drule_tac a = a in mk_disjoint_insert, auto)
1.1083 -  done
1.1084 -
1.1085 -lemma setsum_diff1: "finite A \<Longrightarrow>
1.1086 -  (setsum f (A - {a}) :: ('a::{pordered_ab_group_add})) =
1.1087 -  (if a:A then setsum f A - f a else setsum f A)"
1.1088 -  by (erule finite_induct) (auto simp add: insert_Diff_if)
1.1089 -
1.1090 -(* By Jeremy Siek: *)
1.1091 -
1.1092 -lemma setsum_diff_nat:
1.1093 -  assumes finB: "finite B"
1.1094 -  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
1.1095 -using finB
1.1096 -proof (induct)
1.1097 -  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
1.1098 -next
1.1099 -  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
1.1100 -    and xFinA: "insert x F \<subseteq> A"
1.1101 -    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
1.1102 -  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
1.1103 -  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
1.1104 -    by (simp add: setsum_diff1_nat)
1.1105 -  from xFinA have "F \<subseteq> A" by simp
1.1106 -  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
1.1107 -  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
1.1108 -    by simp
1.1109 -  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
1.1110 -  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
1.1111 -    by simp
1.1112 -  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
1.1113 -  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
1.1114 -    by simp
1.1115 -  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
1.1116 -qed
1.1117 -
1.1118 -lemma setsum_diff:
1.1119 -  assumes le: "finite A" "B \<subseteq> A"
1.1120 -  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::pordered_ab_group_add))"
1.1121 -proof -
1.1122 -  from le have finiteB: "finite B" using finite_subset by auto
1.1123 -  show ?thesis using finiteB le
1.1124 -    proof (induct)
1.1125 -      case empty
1.1126 -      thus ?case by auto
1.1127 -    next
1.1128 -      case (insert x F)
1.1129 -      thus ?case using le finiteB
1.1130 -	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
1.1131 -    qed
1.1132 -  qed
1.1133 -
1.1134 -lemma setsum_mono:
1.1135 -  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
1.1136 -  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
1.1137 -proof (cases "finite K")
1.1138 -  case True
1.1139 -  thus ?thesis using le
1.1140 -  proof (induct)
1.1141 -    case empty
1.1142 -    thus ?case by simp
1.1143 -  next
1.1144 -    case insert
1.1145 -    thus ?case using add_mono
1.1146 -      by force
1.1147 -  qed
1.1148 -next
1.1149 -  case False
1.1150 -  thus ?thesis
1.1151 -    by (simp add: setsum_def)
1.1152 -qed
1.1153 -
1.1154 -lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
1.1155 -  - setsum f A"
1.1156 -  by (induct set: Finites, auto)
1.1157 -
1.1158 -lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
1.1159 -  setsum f A - setsum g A"
1.1161 -
1.1162 -lemma setsum_nonneg: "[| finite A;
1.1163 -    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
1.1164 -    0 \<le> setsum f A";
1.1165 -  apply (induct set: Finites, auto)
1.1166 -  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
1.1167 -  apply (blast intro: add_mono)
1.1168 -  done
1.1169 -
1.1170 -lemma setsum_nonpos: "[| finite A;
1.1171 -    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
1.1172 -    setsum f A \<le> 0";
1.1173 -  apply (induct set: Finites, auto)
1.1174 -  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
1.1175 -  apply (blast intro: add_mono)
1.1176 -  done
1.1177 -
1.1178 -lemma setsum_mult:
1.1179 -  fixes f :: "'a => ('b::semiring_0_cancel)"
1.1180 -  shows "r * setsum f A = setsum (%n. r * f n) A"
1.1181 -proof (cases "finite A")
1.1182 -  case True
1.1183 -  thus ?thesis
1.1184 -  proof (induct)
1.1185 -    case empty thus ?case by simp
1.1186 -  next
1.1187 -    case (insert x A) thus ?case by (simp add: right_distrib)
1.1188 -  qed
1.1189 -next
1.1190 -  case False thus ?thesis by (simp add: setsum_def)
1.1191 -qed
1.1192 -
1.1193 -lemma setsum_abs:
1.1194 -  fixes f :: "'a => ('b::lordered_ab_group_abs)"
1.1195 -  assumes fin: "finite A"
1.1196 -  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
1.1197 -using fin
1.1198 -proof (induct)
1.1199 -  case empty thus ?case by simp
1.1200 -next
1.1201 -  case (insert x A)
1.1202 -  thus ?case by (auto intro: abs_triangle_ineq order_trans)
1.1203 -qed
1.1204 -
1.1205 -lemma setsum_abs_ge_zero:
1.1206 -  fixes f :: "'a => ('b::lordered_ab_group_abs)"
1.1207 -  assumes fin: "finite A"
1.1208 -  shows "0 \<le> setsum (%i. abs(f i)) A"
1.1209 -using fin
1.1210 -proof (induct)
1.1211 -  case empty thus ?case by simp
1.1212 -next
1.1213 -  case (insert x A) thus ?case by (auto intro: order_trans)
1.1214 -qed
1.1215 -
1.1216 -subsubsection {* Cardinality of unions and Sigma sets *}
1.1217 -
1.1218 -lemma card_UN_disjoint:
1.1219 -    "finite I ==> (ALL i:I. finite (A i)) ==>
1.1220 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.1221 -      card (UNION I A) = setsum (%i. card (A i)) I"
1.1222 -  apply (subst card_eq_setsum)
1.1223 -  apply (subst finite_UN, assumption+)
1.1224 -  apply (subgoal_tac
1.1225 -           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
1.1226 -  apply (simp add: setsum_UN_disjoint)
1.1227 -  apply (simp add: setsum_constant_nat cong: setsum_cong)
1.1228 -  done
1.1229 -
1.1230 -lemma card_Union_disjoint:
1.1231 -  "finite C ==> (ALL A:C. finite A) ==>
1.1232 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.1233 -      card (Union C) = setsum card C"
1.1234 -  apply (frule card_UN_disjoint [of C id])
1.1235 -  apply (unfold Union_def id_def, assumption+)
1.1236 -  done
1.1237 -
1.1238 -lemma SigmaI_insert: "y \<notin> A ==>
1.1239 -  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
1.1240 -  by auto
1.1241 -
1.1242 -lemma card_cartesian_product_singleton: "finite A ==>
1.1243 -    card({x} <*> A) = card(A)"
1.1244 -  apply (subgoal_tac "inj_on (%y .(x,y)) A")
1.1245 -  apply (frule card_image, assumption)
1.1246 -  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
1.1247 -  apply (auto simp add: inj_on_def)
1.1248 -  done
1.1249 -
1.1250 -lemma card_SigmaI [rule_format,simp]: "finite A ==>
1.1251 -  (ALL a:A. finite (B a)) -->
1.1252 -  card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
1.1253 -  apply (erule finite_induct, auto)
1.1254 -  apply (subst SigmaI_insert, assumption)
1.1255 -  apply (subst card_Un_disjoint)
1.1256 -  apply (auto intro: finite_SigmaI simp add: card_cartesian_product_singleton)
1.1257 -  done
1.1258 -
1.1259 -lemma card_cartesian_product:
1.1260 -     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
1.1261 -  by (simp add: setsum_constant_nat)
1.1262 -
1.1263 -
1.1264 -
1.1265 -subsection {* Generalized product over a set *}
1.1266 -
1.1267 -constdefs
1.1268 -  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
1.1269 -  "setprod f A == if finite A then fold (op *) f 1 A else 1"
1.1270 -
1.1271 -syntax
1.1272 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
1.1273 -
1.1274 -syntax (xsymbols)
1.1275 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
1.1276 -syntax (HTML output)
1.1277 -  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
1.1278 -translations
1.1279 -  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
1.1280 -
1.1281 -syntax
1.1282 -  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
1.1283 -
1.1284 -parse_translation {*
1.1285 -  let
1.1286 -    fun Setprod_tr [A] = Syntax.const "setprod" \$ Abs ("", dummyT, Bound 0) \$ A
1.1287 -  in [("_Setprod", Setprod_tr)] end;
1.1288 -*}
1.1289 -print_translation {*
1.1290 -let fun setprod_tr' [Abs(x,Tx,t), A] =
1.1291 -    if t = Bound 0 then Syntax.const "_Setprod" \$ A else raise Match
1.1292 -in
1.1293 -[("setprod", setprod_tr')]
1.1294 -end
1.1295 -*}
1.1296 -
1.1297 -
1.1298 -text{* Instantiation of locales: *}
1.1299 -
1.1300 -lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
1.1301 -by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
1.1302 -
1.1303 -lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
1.1304 -by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
1.1305 -
1.1306 -lemma setprod_empty [simp]: "setprod f {} = 1"
1.1307 -  by (auto simp add: setprod_def)
1.1308 -
1.1309 -lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
1.1310 -    setprod f (insert a A) = f a * setprod f A"
1.1311 -by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
1.1312 -
1.1313 -lemma setprod_reindex:
1.1314 -     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
1.1315 -by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
1.1316 -
1.1317 -lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
1.1318 -by (auto simp add: setprod_reindex)
1.1319 -
1.1320 -lemma setprod_cong:
1.1321 -  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
1.1322 -by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
1.1323 -
1.1324 -lemma setprod_reindex_cong: "inj_on f A ==>
1.1325 -    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
1.1326 -  by (frule setprod_reindex, simp)
1.1327 -
1.1328 -
1.1329 -lemma setprod_1: "setprod (%i. 1) A = 1"
1.1330 -  apply (case_tac "finite A")
1.1331 -  apply (erule finite_induct, auto simp add: mult_ac)
1.1332 -  apply (simp add: setprod_def)
1.1333 -  done
1.1334 -
1.1335 -lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
1.1336 -  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
1.1337 -  apply (erule ssubst, rule setprod_1)
1.1338 -  apply (rule setprod_cong, auto)
1.1339 -  done
1.1340 -
1.1341 -lemma setprod_Un_Int: "finite A ==> finite B
1.1342 -    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
1.1343 -by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
1.1344 -
1.1345 -lemma setprod_Un_disjoint: "finite A ==> finite B
1.1346 -  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
1.1347 -by (subst setprod_Un_Int [symmetric], auto)
1.1348 -
1.1349 -lemma setprod_UN_disjoint:
1.1350 -    "finite I ==> (ALL i:I. finite (A i)) ==>
1.1351 -        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
1.1352 -      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
1.1353 -by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
1.1354 -
1.1355 -lemma setprod_Union_disjoint:
1.1356 -  "finite C ==> (ALL A:C. finite A) ==>
1.1357 -        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
1.1358 -      setprod f (Union C) = setprod (setprod f) C"
1.1359 -  apply (frule setprod_UN_disjoint [of C id f])
1.1360 -  apply (unfold Union_def id_def, assumption+)
1.1361 -  done
1.1362 -
1.1363 -lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
1.1364 -    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
1.1365 -    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
1.1366 -by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
1.1367 -
1.1368 -lemma setprod_cartesian_product: "finite A ==> finite B ==>
1.1369 -    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
1.1370 -    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
1.1371 -  by (erule setprod_Sigma, auto)
1.1372 -
1.1373 -lemma setprod_timesf:
1.1374 -  "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
1.1376 -
1.1377 -
1.1378 -subsubsection {* Properties in more restricted classes of structures *}
1.1379 -
1.1380 -lemma setprod_eq_1_iff [simp]:
1.1381 -    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
1.1382 -  by (induct set: Finites) auto
1.1383 -
1.1384 -lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
1.1385 -  apply (erule finite_induct)
1.1386 -  apply (auto simp add: power_Suc)
1.1387 -  done
1.1388 -
1.1389 -lemma setprod_zero:
1.1390 -     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
1.1391 -  apply (induct set: Finites, force, clarsimp)
1.1392 -  apply (erule disjE, auto)
1.1393 -  done
1.1394 -
1.1395 -lemma setprod_nonneg [rule_format]:
1.1396 -     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
1.1397 -  apply (case_tac "finite A")
1.1398 -  apply (induct set: Finites, force, clarsimp)
1.1399 -  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
1.1400 -  apply (rule mult_mono, assumption+)
1.1401 -  apply (auto simp add: setprod_def)
1.1402 -  done
1.1403 -
1.1404 -lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
1.1405 -     --> 0 < setprod f A"
1.1406 -  apply (case_tac "finite A")
1.1407 -  apply (induct set: Finites, force, clarsimp)
1.1408 -  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
1.1409 -  apply (rule mult_strict_mono, assumption+)
1.1410 -  apply (auto simp add: setprod_def)
1.1411 -  done
1.1412 -
1.1413 -lemma setprod_nonzero [rule_format]:
1.1414 -    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
1.1415 -      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
1.1416 -  apply (erule finite_induct, auto)
1.1417 -  done
1.1418 -
1.1419 -lemma setprod_zero_eq:
1.1420 -    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
1.1421 -     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
1.1422 -  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
1.1423 -  done
1.1424 -
1.1425 -lemma setprod_nonzero_field:
1.1426 -    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
1.1427 -  apply (rule setprod_nonzero, auto)
1.1428 -  done
1.1429 -
1.1430 -lemma setprod_zero_eq_field:
1.1431 -    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
1.1432 -  apply (rule setprod_zero_eq, auto)
1.1433 -  done
1.1434 -
1.1435 -lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
1.1436 -    (setprod f (A Un B) :: 'a ::{field})
1.1437 -      = setprod f A * setprod f B / setprod f (A Int B)"
1.1438 -  apply (subst setprod_Un_Int [symmetric], auto)
1.1439 -  apply (subgoal_tac "finite (A Int B)")
1.1440 -  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
1.1441 -  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
1.1442 -  done
1.1443 -
1.1444 -lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
1.1445 -    (setprod f (A - {a}) :: 'a :: {field}) =
1.1446 -      (if a:A then setprod f A / f a else setprod f A)"
1.1447 -  apply (erule finite_induct)
1.1448 -   apply (auto simp add: insert_Diff_if)
1.1449 -  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
1.1450 -  apply (erule ssubst)
1.1451 -  apply (subst times_divide_eq_right [THEN sym])
1.1452 -  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
1.1453 -  done
1.1454 -
1.1455 -lemma setprod_inversef: "finite A ==>
1.1456 -    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
1.1457 -      setprod (inverse \<circ> f) A = inverse (setprod f A)"
1.1458 -  apply (erule finite_induct)
1.1459 -  apply (simp, simp)
1.1460 -  done
1.1461 -
1.1462 -lemma setprod_dividef:
1.1463 -     "[|finite A;
1.1464 -        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
1.1465 -      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
1.1466 -  apply (subgoal_tac
1.1467 -         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
1.1468 -  apply (erule ssubst)
1.1469 -  apply (subst divide_inverse)
1.1470 -  apply (subst setprod_timesf)
1.1471 -  apply (subst setprod_inversef, assumption+, rule refl)
1.1472 -  apply (rule setprod_cong, rule refl)
1.1473 -  apply (subst divide_inverse, auto)
1.1474 -  done
1.1475 -
1.1476 -end
```