Theory Big_Operators

Up to index of Isabelle/HOL

theory Big_Operators
imports Plain
`(*  Title:      HOL/Big_Operators.thy    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel                with contributions by Jeremy Avigad*)header {* Big operators and finite (non-empty) sets *}theory Big_Operatorsimports Plainbeginsubsection {* Generic monoid operation over a set *}no_notation times (infixl "*" 70)no_notation Groups.one ("1")locale comm_monoid_big = comm_monoid +  fixes F :: "('b => 'a) => 'b set => 'a"  assumes F_eq: "F g A = (if finite A then fold_image (op *) g 1 A else 1)"sublocale comm_monoid_big < folding_image proofqed (simp add: F_eq)context comm_monoid_bigbeginlemma infinite [simp]:  "¬ finite A ==> F g A = 1"  by (simp add: F_eq)lemma F_cong:  assumes "A = B" "!!x. x ∈ B ==> h x = g x"  shows "F h A = F g B"proof cases  assume "finite A"  with assms show ?thesis unfolding `A = B` by (simp cong: cong)next  assume "¬ finite A"  then show ?thesis unfolding `A = B` by simpqedlemma strong_F_cong [cong]:  "[| A = B; !!x. x:B =simp=> g x = h x |]   ==> F (%x. g x) A = F (%x. h x) B"by (rule F_cong) (simp_all add: simp_implies_def)lemma F_neutral[simp]: "F (%i. 1) A = 1"by (cases "finite A") (simp_all add: neutral)lemma F_neutral': "ALL a:A. g a = 1 ==> F g A = 1"by simplemma F_subset_diff: "[| B ⊆ A; finite A |] ==> F g A = F g (A - B) * F g B"by (metis Diff_partition union_disjoint Diff_disjoint finite_Un inf_commute sup_commute)lemma F_mono_neutral_cong_left:  assumes "finite T" and "S ⊆ T" and "∀i ∈ T - S. h i = 1"  and "!!x. x ∈ S ==> g x = h x" shows "F g S = F h T"proof-  have eq: "T = S ∪ (T - S)" using `S ⊆ T` by blast  have d: "S ∩ (T - S) = {}" using `S ⊆ T` by blast  from `finite T` `S ⊆ T` have f: "finite S" "finite (T - S)"    by (auto intro: finite_subset)  show ?thesis using assms(4)    by (simp add: union_disjoint[OF f d, unfolded eq[symmetric]] F_neutral'[OF assms(3)])qedlemma F_mono_neutral_cong_right:  "[| finite T; S ⊆ T; ∀i ∈ T - S. g i = 1; !!x. x ∈ S ==> g x = h x |]   ==> F g T = F h S"by(auto intro!: F_mono_neutral_cong_left[symmetric])lemma F_mono_neutral_left:  "[| finite T; S ⊆ T; ∀i ∈ T - S. g i = 1 |] ==> F g S = F g T"by(blast intro: F_mono_neutral_cong_left)lemma F_mono_neutral_right:  "[| finite T;  S ⊆ T;  ∀i ∈ T - S. g i = 1 |] ==> F g T = F g S"by(blast intro!: F_mono_neutral_left[symmetric])lemma F_delta:   assumes fS: "finite S"  shows "F (λk. if k=a then b k else 1) S = (if a ∈ S then b a else 1)"proof-  let ?f = "(λk. if k=a then b k else 1)"  { assume a: "a ∉ S"    hence "∀k∈S. ?f k = 1" by simp    hence ?thesis  using a by simp }  moreover  { assume a: "a ∈ S"    let ?A = "S - {a}"    let ?B = "{a}"    have eq: "S = ?A ∪ ?B" using a by blast     have dj: "?A ∩ ?B = {}" by simp    from fS have fAB: "finite ?A" "finite ?B" by auto      have "F ?f S = F ?f ?A * F ?f ?B"      using union_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]      by simp    then have ?thesis  using a by simp }  ultimately show ?thesis by blastqedlemma F_delta':   assumes fS: "finite S" shows   "F (λk. if a = k then b k else 1) S = (if a ∈ S then b a else 1)"using F_delta[OF fS, of a b, symmetric] by (auto intro: F_cong)lemma F_fun_f: "F (%x. g x * h x) A = (F g A * F h A)"by (cases "finite A") (simp_all add: distrib)text {* for ad-hoc proofs for @{const fold_image} *}lemma comm_monoid_mult:  "class.comm_monoid_mult (op *) 1"proof qed (auto intro: assoc commute)lemma F_Un_neutral:  assumes fS: "finite S" and fT: "finite T"  and I1: "∀x ∈ S∩T. g x = 1"  shows "F g (S ∪ T) = F g S  * F g T"proof -  interpret comm_monoid_mult "op *" 1 by (fact comm_monoid_mult)  show ?thesis  using fS fT  apply (simp add: F_eq)  apply (rule fold_image_Un_one)  using I1 by autoqedlemma If_cases:  fixes P :: "'b => bool" and g h :: "'b => 'a"  assumes fA: "finite A"  shows "F (λx. if P x then h x else g x) A =         F h (A ∩ {x. P x}) * F g (A ∩ - {x. P x})"proof-  have a: "A = A ∩ {x. P x} ∪ A ∩ -{x. P x}"           "(A ∩ {x. P x}) ∩ (A ∩ -{x. P x}) = {}"     by blast+  from fA   have f: "finite (A ∩ {x. P x})" "finite (A ∩ -{x. P x})" by auto  let ?g = "λx. if P x then h x else g x"  from union_disjoint[OF f a(2), of ?g] a(1)  show ?thesis    by (subst (1 2) F_cong) simp_allqedendtext {* for ad-hoc proofs for @{const fold_image} *}lemma (in comm_monoid_add) comm_monoid_mult:  "class.comm_monoid_mult (op +) 0"proof qed (auto intro: add_assoc add_commute)notation times (infixl "*" 70)notation Groups.one ("1")subsection {* Generalized summation over a set *}definition (in comm_monoid_add) setsum :: "('b => 'a) => 'b set => 'a" where  "setsum f A = (if finite A then fold_image (op +) f 0 A else 0)"sublocale comm_monoid_add < setsum!: comm_monoid_big "op +" 0 setsum proofqed (fact setsum_def)abbreviation  Setsum  ("∑_" [1000] 999) where  "∑A == setsum (%x. x) A"text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} iswritten @{text"∑x∈A. e"}. *}syntax  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)syntax (xsymbols)  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3∑_∈_. _)" [0, 51, 10] 10)syntax (HTML output)  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3∑_∈_. _)" [0, 51, 10] 10)translations -- {* Beware of argument permutation! *}  "SUM i:A. b" == "CONST setsum (%i. b) A"  "∑i∈A. b" == "CONST setsum (%i. b) A"text{* Instead of @{term"∑x∈{x. P}. e"} we introduce the shorter @{text"∑x|P. e"}. *}syntax  "_qsetsum" :: "pttrn => bool => 'a => 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)syntax (xsymbols)  "_qsetsum" :: "pttrn => bool => 'a => 'a" ("(3∑_ | (_)./ _)" [0,0,10] 10)syntax (HTML output)  "_qsetsum" :: "pttrn => bool => 'a => 'a" ("(3∑_ | (_)./ _)" [0,0,10] 10)translations  "SUM x|P. t" => "CONST setsum (%x. t) {x. P}"  "∑x|P. t" => "CONST setsum (%x. t) {x. P}"print_translation {*let  fun setsum_tr' [Abs (x, Tx, t), Const (@{const_syntax Collect}, _) \$ Abs (y, Ty, P)] =        if x <> y then raise Match        else          let            val x' = Syntax_Trans.mark_bound_body (x, Tx);            val t' = subst_bound (x', t);            val P' = subst_bound (x', P);          in            Syntax.const @{syntax_const "_qsetsum"} \$ Syntax_Trans.mark_bound_abs (x, Tx) \$ P' \$ t'          end    | setsum_tr' _ = raise Match;in [(@{const_syntax setsum}, setsum_tr')] end*}lemma setsum_empty:  "setsum f {} = 0"  by (fact setsum.empty)lemma setsum_insert:  "finite F ==> a ∉ F ==> setsum f (insert a F) = f a + setsum f F"  by (fact setsum.insert)lemma setsum_infinite:  "~ finite A ==> setsum f A = 0"  by (fact setsum.infinite)lemma (in comm_monoid_add) setsum_reindex:  assumes "inj_on f B" shows "setsum h (f ` B) = setsum (h o f) B"proof -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)  from assms show ?thesis by (auto simp add: setsum_def fold_image_reindex o_def dest!:finite_imageD)qedlemma setsum_reindex_id:  "inj_on f B ==> setsum f B = setsum id (f ` B)"by (simp add: setsum_reindex)lemma setsum_reindex_nonzero:   assumes fS: "finite S"  and nz: "!! x y. x ∈ S ==> y ∈ S ==> x ≠ y ==> f x = f y ==> h (f x) = 0"  shows "setsum h (f ` S) = setsum (h o f) S"using nzproof(induct rule: finite_induct[OF fS])  case 1 thus ?case by simpnext  case (2 x F)   { assume fxF: "f x ∈ f ` F" hence "∃y ∈ F . f y = f x" by auto    then obtain y where y: "y ∈ F" "f x = f y" by auto     from "2.hyps" y have xy: "x ≠ y" by auto        from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp    have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto    also have "… = setsum (h o f) (insert x F)"       unfolding setsum.insert[OF `finite F` `x∉F`]      using h0      apply (simp cong del:setsum.strong_F_cong)      apply (rule "2.hyps"(3))      apply (rule_tac y="y" in  "2.prems")      apply simp_all      done    finally have ?case . }  moreover  { assume fxF: "f x ∉ f ` F"    have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)"       using fxF "2.hyps" by simp     also have "… = setsum (h o f) (insert x F)"      unfolding setsum.insert[OF `finite F` `x∉F`]      apply (simp cong del:setsum.strong_F_cong)      apply (rule cong [OF refl [of "op + (h (f x))"]])      apply (rule "2.hyps"(3))      apply (rule_tac y="y" in  "2.prems")      apply simp_all      done    finally have ?case . }  ultimately show ?case by blastqedlemma setsum_cong:  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"by (fact setsum.F_cong)lemma strong_setsum_cong:  "A = B ==> (!!x. x:B =simp=> f x = g x)   ==> setsum (%x. f x) A = setsum (%x. g x) B"by (fact setsum.strong_F_cong)lemma setsum_cong2: "[|!!x. x ∈ A ==> f x = g x|] ==> setsum f A = setsum g A"by (auto intro: setsum_cong)lemma setsum_reindex_cong:   "[|inj_on f A; B = f ` A; !!a. a:A ==> g a = h (f a)|]     ==> setsum h B = setsum g A"by (simp add: setsum_reindex)lemmas setsum_0 = setsum.F_neutrallemmas setsum_0' = setsum.F_neutral'lemma setsum_Un_Int: "finite A ==> finite B ==>  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}by (fact setsum.union_inter)lemma setsum_Un_disjoint: "finite A ==> finite B  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"by (fact setsum.union_disjoint)lemma setsum_subset_diff: "[| B ⊆ A; finite A |] ==>    setsum f A = setsum f (A - B) + setsum f B"by(fact setsum.F_subset_diff)lemma setsum_mono_zero_left:   "[| finite T; S ⊆ T; ∀i ∈ T - S. f i = 0 |] ==> setsum f S = setsum f T"by(fact setsum.F_mono_neutral_left)lemmas setsum_mono_zero_right = setsum.F_mono_neutral_rightlemma setsum_mono_zero_cong_left:   "[| finite T; S ⊆ T; ∀i ∈ T - S. g i = 0; !!x. x ∈ S ==> f x = g x |]  ==> setsum f S = setsum g T"by(fact setsum.F_mono_neutral_cong_left)lemmas setsum_mono_zero_cong_right = setsum.F_mono_neutral_cong_rightlemma setsum_delta: "finite S ==>  setsum (λk. if k=a then b k else 0) S = (if a ∈ S then b a else 0)"by(fact setsum.F_delta)lemma setsum_delta': "finite S ==>  setsum (λk. if a = k then b k else 0) S = (if a∈ S then b a else 0)"by(fact setsum.F_delta')lemma setsum_restrict_set:  assumes fA: "finite A"  shows "setsum f (A ∩ B) = setsum (λx. if x ∈ B then f x else 0) A"proof-  from fA have fab: "finite (A ∩ B)" by auto  have aba: "A ∩ B ⊆ A" by blast  let ?g = "λx. if x ∈ A∩B then f x else 0"  from setsum_mono_zero_left[OF fA aba, of ?g]  show ?thesis by simpqedlemma setsum_cases:  assumes fA: "finite A"  shows "setsum (λx. if P x then f x else g x) A =         setsum f (A ∩ {x. P x}) + setsum g (A ∩ - {x. P x})"  using setsum.If_cases[OF fA] .(*But we can't get rid of finite I. If infinite, although the rhs is 0,   the lhs need not be, since UNION I A could still be finite.*)lemma (in comm_monoid_add) setsum_UN_disjoint:  assumes "finite I" and "ALL i:I. finite (A i)"    and "ALL i:I. ALL j:I. i ≠ j --> A i Int A j = {}"  shows "setsum f (UNION I A) = (∑i∈I. setsum f (A i))"proof -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)  from assms show ?thesis by (simp add: setsum_def fold_image_UN_disjoint)qedtext{*No need to assume that @{term C} is finite.  If infinite, the rhs isdirectly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}lemma setsum_Union_disjoint:  assumes "∀A∈C. finite A" "∀A∈C. ∀B∈C. A ≠ B --> A Int B = {}"  shows "setsum f (Union C) = setsum (setsum f) C"proof cases  assume "finite C"  from setsum_UN_disjoint[OF this assms]  show ?thesis    by (simp add: SUP_def)qed (force dest: finite_UnionD simp add: setsum_def)(*But we can't get rid of finite A. If infinite, although the lhs is 0,   the rhs need not be, since SIGMA A B could still be finite.*)lemma (in comm_monoid_add) setsum_Sigma:  assumes "finite A" and  "ALL x:A. finite (B x)"  shows "(∑x∈A. (∑y∈B x. f x y)) = (∑(x,y)∈(SIGMA x:A. B x). f x y)"proof -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)  from assms show ?thesis by (simp add: setsum_def fold_image_Sigma split_def)qedtext{*Here we can eliminate the finiteness assumptions, by cases.*}lemma setsum_cartesian_product:    "(∑x∈A. (∑y∈B. f x y)) = (∑(x,y) ∈ A <*> B. f x y)"apply (cases "finite A")  apply (cases "finite B")   apply (simp add: setsum_Sigma) apply (cases "A={}", simp) apply (simp) apply (auto simp add: setsum_def            dest: finite_cartesian_productD1 finite_cartesian_productD2) donelemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"by (fact setsum.F_fun_f)lemma setsum_Un_zero:    "[| finite S; finite T; ∀x ∈ S∩T. f x = 0 |] ==>  setsum f (S ∪ T) = setsum f S + setsum f T"by(fact setsum.F_Un_neutral)lemma setsum_UNION_zero:   assumes fS: "finite S" and fSS: "∀T ∈ S. finite T"  and f0: "!!T1 T2 x. T1∈S ==> T2∈S ==> T1 ≠ T2 ==> x ∈ T1 ==> x ∈ T2 ==> f x = 0"  shows "setsum f (\<Union>S) = setsum (λT. setsum f T) S"  using fSS f0proof(induct rule: finite_induct[OF fS])  case 1 thus ?case by simpnext  case (2 T F)  then have fTF: "finite T" "∀T∈F. finite T" "finite F" and TF: "T ∉ F"     and H: "setsum f (\<Union> F) = setsum (setsum f) F" by auto  from fTF have fUF: "finite (\<Union>F)" by auto  from "2.prems" TF fTF  show ?case     by (auto simp add: H[symmetric] intro: setsum_Un_zero[OF fTF(1) fUF, of f])qedsubsubsection {* Properties in more restricted classes of structures *}lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"apply (case_tac "finite A") prefer 2 apply (simp add: setsum_def)apply (erule rev_mp)apply (erule finite_induct, auto)donelemma setsum_eq_0_iff [simp]:    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"by (induct set: finite) autolemma setsum_eq_Suc0_iff: "finite A ==>  (setsum f A = Suc 0) = (EX a:A. f a = Suc 0 & (ALL b:A. a≠b --> f b = 0))"apply(erule finite_induct)apply (auto simp add:add_is_1)donelemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]lemma setsum_Un_nat: "finite A ==> finite B ==>  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"  -- {* For the natural numbers, we have subtraction. *}by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)lemma setsum_Un: "finite A ==> finite B ==>  (setsum f (A Un B) :: 'a :: ab_group_add) =   setsum f A + setsum f B - setsum f (A Int B)"by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)lemma setsum_Un2:  assumes "finite (A ∪ B)"  shows "setsum f (A ∪ B) = setsum f (A - B) + setsum f (B - A) + setsum f (A ∩ B)"proof -  have "A ∪ B = A - B ∪ (B - A) ∪ A ∩ B"    by auto  with assms show ?thesis by simp (subst setsum_Un_disjoint, auto)+qedlemma (in comm_monoid_add) setsum_eq_general_reverses:  assumes fS: "finite S" and fT: "finite T"  and kh: "!!y. y ∈ T ==> k y ∈ S ∧ h (k y) = y"  and hk: "!!x. x ∈ S ==> h x ∈ T ∧ k (h x) = x ∧ g (h x) = f x"  shows "setsum f S = setsum g T"proof -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)  show ?thesis  apply (simp add: setsum_def fS fT)  apply (rule fold_image_eq_general_inverses)  apply (rule fS)  apply (erule kh)  apply (erule hk)  doneqedlemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =  (if a:A then setsum f A - f a else setsum f A)"apply (case_tac "finite A") prefer 2 apply (simp add: setsum_def)apply (erule finite_induct) apply (auto simp add: insert_Diff_if)apply (drule_tac a = a in mk_disjoint_insert, auto)donelemma setsum_diff1: "finite A ==>  (setsum f (A - {a}) :: ('a::ab_group_add)) =  (if a:A then setsum f A - f a else setsum f A)"by (erule finite_induct) (auto simp add: insert_Diff_if)lemma setsum_diff1'[rule_format]:  "finite A ==> a ∈ A --> (∑ x ∈ A. f x) = f a + (∑ x ∈ (A - {a}). f x)"apply (erule finite_induct[where F=A and P="% A. (a ∈ A --> (∑ x ∈ A. f x) = f a + (∑ x ∈ (A - {a}). f x))"])apply (auto simp add: insert_Diff_if add_ac)donelemma setsum_diff1_ring: assumes "finite A" "a ∈ A"  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"unfolding setsum_diff1'[OF assms] by auto(* By Jeremy Siek: *)lemma setsum_diff_nat: assumes "finite B" and "B ⊆ A"shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"using assmsproof induct  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simpnext  fix F x assume finF: "finite F" and xnotinF: "x ∉ F"    and xFinA: "insert x F ⊆ A"    and IH: "F ⊆ A ==> setsum f (A - F) = setsum f A - setsum f F"  from xnotinF xFinA have xinAF: "x ∈ (A - F)" by simp  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"    by (simp add: setsum_diff1_nat)  from xFinA have "F ⊆ A" by simp  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"    by simp  from xnotinF have "A - insert x F = (A - F) - {x}" by auto  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"    by simp  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"    by simp  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simpqedlemma setsum_diff:  assumes le: "finite A" "B ⊆ A"  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"proof -  from le have finiteB: "finite B" using finite_subset by auto  show ?thesis using finiteB le  proof induct    case empty    thus ?case by auto  next    case (insert x F)    thus ?case using le finiteB       by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)  qedqedlemma setsum_mono:  assumes le: "!!i. i∈K ==> f (i::'a) ≤ ((g i)::('b::{comm_monoid_add, ordered_ab_semigroup_add}))"  shows "(∑i∈K. f i) ≤ (∑i∈K. g i)"proof (cases "finite K")  case True  thus ?thesis using le  proof induct    case empty    thus ?case by simp  next    case insert    thus ?case using add_mono by fastforce  qednext  case False  thus ?thesis    by (simp add: setsum_def)qedlemma setsum_strict_mono:  fixes f :: "'a => 'b::{ordered_cancel_ab_semigroup_add,comm_monoid_add}"  assumes "finite A"  "A ≠ {}"    and "!!x. x:A ==> f x < g x"  shows "setsum f A < setsum g A"  using assmsproof (induct rule: finite_ne_induct)  case singleton thus ?case by simpnext  case insert thus ?case by (auto simp: add_strict_mono)qedlemma setsum_strict_mono_ex1:fixes f :: "'a => 'b::{comm_monoid_add, ordered_cancel_ab_semigroup_add}"assumes "finite A" and "ALL x:A. f x ≤ g x" and "EX a:A. f a < g a"shows "setsum f A < setsum g A"proof-  from assms(3) obtain a where a: "a:A" "f a < g a" by blast  have "setsum f A = setsum f ((A-{a}) ∪ {a})"    by(simp add:insert_absorb[OF `a:A`])  also have "… = setsum f (A-{a}) + setsum f {a}"    using `finite A` by(subst setsum_Un_disjoint) auto  also have "setsum f (A-{a}) ≤ setsum g (A-{a})"    by(rule setsum_mono)(simp add: assms(2))  also have "setsum f {a} < setsum g {a}" using a by simp  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) ∪ {a})"    using `finite A` by(subst setsum_Un_disjoint[symmetric]) auto  also have "… = setsum g A" by(simp add:insert_absorb[OF `a:A`])  finally show ?thesis by (metis add_right_mono add_strict_left_mono)qedlemma setsum_negf:  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"proof (cases "finite A")  case True thus ?thesis by (induct set: finite) autonext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_subtractf:  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =    setsum f A - setsum g A"proof (cases "finite A")  case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)next  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_nonneg:  assumes nn: "∀x∈A. (0::'a::{ordered_ab_semigroup_add,comm_monoid_add}) ≤ f x"  shows "0 ≤ setsum f A"proof (cases "finite A")  case True thus ?thesis using nn  proof induct    case empty then show ?case by simp  next    case (insert x F)    then have "0 + 0 ≤ f x + setsum f F" by (blast intro: add_mono)    with insert show ?case by simp  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_nonpos:  assumes np: "∀x∈A. f x ≤ (0::'a::{ordered_ab_semigroup_add,comm_monoid_add})"  shows "setsum f A ≤ 0"proof (cases "finite A")  case True thus ?thesis using np  proof induct    case empty then show ?case by simp  next    case (insert x F)    then have "f x + setsum f F ≤ 0 + 0" by (blast intro: add_mono)    with insert show ?case by simp  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_nonneg_leq_bound:  fixes f :: "'a => 'b::{ordered_ab_group_add}"  assumes "finite s" "!!i. i ∈ s ==> f i ≥ 0" "(∑i ∈ s. f i) = B" "i ∈ s"  shows "f i ≤ B"proof -  have "0 ≤ (∑ i ∈ s - {i}. f i)" and "0 ≤ f i"    using assms by (auto intro!: setsum_nonneg)  moreover  have "(∑ i ∈ s - {i}. f i) + f i = B"    using assms by (simp add: setsum_diff1)  ultimately show ?thesis by autoqedlemma setsum_nonneg_0:  fixes f :: "'a => 'b::{ordered_ab_group_add}"  assumes "finite s" and pos: "!! i. i ∈ s ==> f i ≥ 0"  and "(∑ i ∈ s. f i) = 0" and i: "i ∈ s"  shows "f i = 0"  using setsum_nonneg_leq_bound[OF assms] pos[OF i] by autolemma setsum_mono2:fixes f :: "'a => 'b :: ordered_comm_monoid_add"assumes fin: "finite B" and sub: "A ⊆ B" and nn: "!!b. b ∈ B-A ==> 0 ≤ f b"shows "setsum f A ≤ setsum f B"proof -  have "setsum f A ≤ setsum f A + setsum f (B-A)"    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)  also have "… = setsum f (A ∪ (B-A))" using fin finite_subset[OF sub fin]    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)  also have "A ∪ (B-A) = B" using sub by blast  finally show ?thesis .qedlemma setsum_mono3: "finite B ==> A <= B ==>     ALL x: B - A.       0 <= ((f x)::'a::{comm_monoid_add,ordered_ab_semigroup_add}) ==>        setsum f A <= setsum f B"  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")  apply (erule ssubst)  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")  apply simp  apply (rule add_left_mono)  apply (erule setsum_nonneg)  apply (subst setsum_Un_disjoint [THEN sym])  apply (erule finite_subset, assumption)  apply (rule finite_subset)  prefer 2  apply assumption  apply (auto simp add: sup_absorb2)donelemma setsum_right_distrib:   fixes f :: "'a => ('b::semiring_0)"  shows "r * setsum f A = setsum (%n. r * f n) A"proof (cases "finite A")  case True  thus ?thesis  proof induct    case empty thus ?case by simp  next    case (insert x A) thus ?case by (simp add: distrib_left)  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_left_distrib:  "setsum f A * (r::'a::semiring_0) = (∑n∈A. f n * r)"proof (cases "finite A")  case True  then show ?thesis  proof induct    case empty thus ?case by simp  next    case (insert x A) thus ?case by (simp add: distrib_right)  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_divide_distrib:  "setsum f A / (r::'a::field) = (∑n∈A. f n / r)"proof (cases "finite A")  case True  then show ?thesis  proof induct    case empty thus ?case by simp  next    case (insert x A) thus ?case by (simp add: add_divide_distrib)  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_abs[iff]:   fixes f :: "'a => ('b::ordered_ab_group_add_abs)"  shows "abs (setsum f A) ≤ setsum (%i. abs(f i)) A"proof (cases "finite A")  case True  thus ?thesis  proof induct    case empty thus ?case by simp  next    case (insert x A)    thus ?case by (auto intro: abs_triangle_ineq order_trans)  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_abs_ge_zero[iff]:   fixes f :: "'a => ('b::ordered_ab_group_add_abs)"  shows "0 ≤ setsum (%i. abs(f i)) A"proof (cases "finite A")  case True  thus ?thesis  proof induct    case empty thus ?case by simp  next    case (insert x A) thus ?case by auto  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma abs_setsum_abs[simp]:   fixes f :: "'a => ('b::ordered_ab_group_add_abs)"  shows "abs (∑a∈A. abs(f a)) = (∑a∈A. abs(f a))"proof (cases "finite A")  case True  thus ?thesis  proof induct    case empty thus ?case by simp  next    case (insert a A)    hence "¦∑a∈insert a A. ¦f a¦¦ = ¦¦f a¦ + (∑a∈A. ¦f a¦)¦" by simp    also have "… = ¦¦f a¦ + ¦∑a∈A. ¦f a¦¦¦"  using insert by simp    also have "… = ¦f a¦ + ¦∑a∈A. ¦f a¦¦"      by (simp del: abs_of_nonneg)    also have "… = (∑a∈insert a A. ¦f a¦)" using insert by simp    finally show ?case .  qednext  case False thus ?thesis by (simp add: setsum_def)qedlemma setsum_Plus:  fixes A :: "'a set" and B :: "'b set"  assumes fin: "finite A" "finite B"  shows "setsum f (A <+> B) = setsum (f o Inl) A + setsum (f o Inr) B"proof -  have "A <+> B = Inl ` A ∪ Inr ` B" by auto  moreover from fin have "finite (Inl ` A :: ('a + 'b) set)" "finite (Inr ` B :: ('a + 'b) set)"    by auto  moreover have "Inl ` A ∩ Inr ` B = ({} :: ('a + 'b) set)" by auto  moreover have "inj_on (Inl :: 'a => 'a + 'b) A" "inj_on (Inr :: 'b => 'a + 'b) B" by(auto intro: inj_onI)  ultimately show ?thesis using fin by(simp add: setsum_Un_disjoint setsum_reindex)qedtext {* Commuting outer and inner summation *}lemma setsum_commute:  "(∑i∈A. ∑j∈B. f i j) = (∑j∈B. ∑i∈A. f i j)"proof (simp add: setsum_cartesian_product)  have "(∑(x,y) ∈ A <*> B. f x y) =    (∑(y,x) ∈ (%(i, j). (j, i)) ` (A × B). f x y)"    (is "?s = _")    apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on)    apply (simp add: split_def)    done  also have "... = (∑(y,x)∈B × A. f x y)"    (is "_ = ?t")    apply (simp add: swap_product)    done  finally show "?s = ?t" .qedlemma setsum_product:  fixes f :: "'a => ('b::semiring_0)"  shows "setsum f A * setsum g B = (∑i∈A. ∑j∈B. f i * g j)"  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute)lemma setsum_mult_setsum_if_inj:fixes f :: "'a => ('b::semiring_0)"shows "inj_on (%(a,b). f a * g b) (A × B) ==>  setsum f A * setsum g B = setsum id {f a * g b|a b. a:A & b:B}"by(auto simp: setsum_product setsum_cartesian_product        intro!:  setsum_reindex_cong[symmetric])lemma setsum_constant [simp]: "(∑x ∈ A. y) = of_nat(card A) * y"apply (cases "finite A")apply (erule finite_induct)apply (auto simp add: algebra_simps)donelemma setsum_bounded:  assumes le: "!!i. i∈A ==> f i ≤ (K::'a::{semiring_1, ordered_ab_semigroup_add})"  shows "setsum f A ≤ of_nat(card A) * K"proof (cases "finite A")  case True  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simpnext  case False thus ?thesis by (simp add: setsum_def)qedsubsubsection {* Cardinality as special case of @{const setsum} *}lemma card_eq_setsum:  "card A = setsum (λx. 1) A"  by (simp only: card_def setsum_def)lemma card_UN_disjoint:  assumes "finite I" and "∀i∈I. finite (A i)"    and "∀i∈I. ∀j∈I. i ≠ j --> A i ∩ A j = {}"  shows "card (UNION I A) = (∑i∈I. card(A i))"proof -  have "(∑i∈I. card (A i)) = (∑i∈I. ∑x∈A i. 1)" by simp  with assms show ?thesis by (simp add: card_eq_setsum setsum_UN_disjoint del: setsum_constant)qedlemma card_Union_disjoint:  "finite C ==> (ALL A:C. finite A) ==>   (ALL A:C. ALL B:C. A ≠ B --> A Int B = {})   ==> card (Union C) = setsum card C"apply (frule card_UN_disjoint [of C id])apply (simp_all add: SUP_def id_def)donetext{*The image of a finite set can be expressed using @{term fold_image}.*}lemma image_eq_fold_image:  "finite A ==> f ` A = fold_image (op Un) (%x. {f x}) {} A"proof (induct rule: finite_induct)  case empty then show ?case by simpnext  interpret ab_semigroup_mult "op Un"    proof qed auto  case insert   then show ?case by simpqedsubsubsection {* Cardinality of products *}lemma card_SigmaI [simp]:  "[| finite A; ALL a:A. finite (B a) |]  ==> card (SIGMA x: A. B x) = (∑a∈A. card (B a))"by(simp add: card_eq_setsum setsum_Sigma del:setsum_constant)(*lemma SigmaI_insert: "y ∉ A ==>  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) ∪ (SIGMA x: A. B x))"  by auto*)lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"  by (cases "finite A ∧ finite B")    (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"by (simp add: card_cartesian_product)subsection {* Generalized product over a set *}definition (in comm_monoid_mult) setprod :: "('b => 'a) => 'b set => 'a" where  "setprod f A = (if finite A then fold_image (op *) f 1 A else 1)"sublocale comm_monoid_mult < setprod!: comm_monoid_big "op *" 1 setprod proofqed (fact setprod_def)abbreviation  Setprod  ("∏_" [1000] 999) where  "∏A == setprod (%x. x) A"syntax  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)syntax (xsymbols)  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3∏_∈_. _)" [0, 51, 10] 10)syntax (HTML output)  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3∏_∈_. _)" [0, 51, 10] 10)translations -- {* Beware of argument permutation! *}  "PROD i:A. b" == "CONST setprod (%i. b) A"   "∏i∈A. b" == "CONST setprod (%i. b) A" text{* Instead of @{term"∏x∈{x. P}. e"} we introduce the shorter @{text"∏x|P. e"}. *}syntax  "_qsetprod" :: "pttrn => bool => 'a => 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)syntax (xsymbols)  "_qsetprod" :: "pttrn => bool => 'a => 'a" ("(3∏_ | (_)./ _)" [0,0,10] 10)syntax (HTML output)  "_qsetprod" :: "pttrn => bool => 'a => 'a" ("(3∏_ | (_)./ _)" [0,0,10] 10)translations  "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"  "∏x|P. t" => "CONST setprod (%x. t) {x. P}"lemma setprod_empty: "setprod f {} = 1"  by (fact setprod.empty)lemma setprod_insert: "[| finite A; a ∉ A |] ==>    setprod f (insert a A) = f a * setprod f A"  by (fact setprod.insert)lemma setprod_infinite: "~ finite A ==> setprod f A = 1"  by (fact setprod.infinite)lemma setprod_reindex:   "inj_on f B ==> setprod h (f ` B) = setprod (h o f) B"by(auto simp: setprod_def fold_image_reindex o_def dest!:finite_imageD)lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"by (auto simp add: setprod_reindex)lemma setprod_cong:  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"by(fact setprod.F_cong)lemma strong_setprod_cong:  "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"by(fact setprod.strong_F_cong)lemma setprod_reindex_cong: "inj_on f A ==>    B = f ` A ==> g = h o f ==> setprod h B = setprod g A"by (frule setprod_reindex, simp)lemma strong_setprod_reindex_cong: assumes i: "inj_on f A"  and B: "B = f ` A" and eq: "!!x. x ∈ A ==> g x = (h o f) x"  shows "setprod h B = setprod g A"proof-    have "setprod h B = setprod (h o f) A"      by (simp add: B setprod_reindex[OF i, of h])    then show ?thesis apply simp      apply (rule setprod_cong)      apply simp      by (simp add: eq)qedlemma setprod_Un_one: "[| finite S; finite T; ∀x ∈ S∩T. f x = 1 |]  ==> setprod f (S ∪ T) = setprod f S  * setprod f T"by(fact setprod.F_Un_neutral)lemmas setprod_1 = setprod.F_neutrallemmas setprod_1' = setprod.F_neutral'lemma setprod_Un_Int: "finite A ==> finite B    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"by (fact setprod.union_inter)lemma setprod_Un_disjoint: "finite A ==> finite B  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"by (fact setprod.union_disjoint)lemma setprod_subset_diff: "[| B ⊆ A; finite A |] ==>    setprod f A = setprod f (A - B) * setprod f B"by(fact setprod.F_subset_diff)lemma setprod_mono_one_left:  "[| finite T; S ⊆ T; ∀i ∈ T - S. f i = 1 |] ==> setprod f S = setprod f T"by(fact setprod.F_mono_neutral_left)lemmas setprod_mono_one_right = setprod.F_mono_neutral_rightlemma setprod_mono_one_cong_left:   "[| finite T; S ⊆ T; ∀i ∈ T - S. g i = 1; !!x. x ∈ S ==> f x = g x |]  ==> setprod f S = setprod g T"by(fact setprod.F_mono_neutral_cong_left)lemmas setprod_mono_one_cong_right = setprod.F_mono_neutral_cong_rightlemma setprod_delta: "finite S ==>  setprod (λk. if k=a then b k else 1) S = (if a ∈ S then b a else 1)"by(fact setprod.F_delta)lemma setprod_delta': "finite S ==>  setprod (λk. if a = k then b k else 1) S = (if a∈ S then b a else 1)"by(fact setprod.F_delta')lemma setprod_UN_disjoint:    "finite I ==> (ALL i:I. finite (A i)) ==>        (ALL i:I. ALL j:I. i ≠ j --> A i Int A j = {}) ==>      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"  by (simp add: setprod_def fold_image_UN_disjoint)lemma setprod_Union_disjoint:  assumes "∀A∈C. finite A" "∀A∈C. ∀B∈C. A ≠ B --> A Int B = {}"   shows "setprod f (Union C) = setprod (setprod f) C"proof cases  assume "finite C"  from setprod_UN_disjoint[OF this assms]  show ?thesis    by (simp add: SUP_def)qed (force dest: finite_UnionD simp add: setprod_def)lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>    (∏x∈A. (∏y∈ B x. f x y)) =    (∏(x,y)∈(SIGMA x:A. B x). f x y)"by(simp add:setprod_def fold_image_Sigma split_def)text{*Here we can eliminate the finiteness assumptions, by cases.*}lemma setprod_cartesian_product:      "(∏x∈A. (∏y∈ B. f x y)) = (∏(x,y)∈(A <*> B). f x y)"apply (cases "finite A")  apply (cases "finite B")   apply (simp add: setprod_Sigma) apply (cases "A={}", simp) apply (simp) apply (auto simp add: setprod_def            dest: finite_cartesian_productD1 finite_cartesian_productD2) donelemma setprod_timesf: "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"by (fact setprod.F_fun_f)subsubsection {* Properties in more restricted classes of structures *}lemma setprod_eq_1_iff [simp]:  "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"by (induct set: finite) autolemma setprod_zero:     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"apply (induct set: finite, force, clarsimp)apply (erule disjE, auto)donelemma setprod_nonneg [rule_format]:   "(ALL x: A. (0::'a::linordered_semidom) ≤ f x) --> 0 ≤ setprod f A"by (cases "finite A", induct set: finite, simp_all add: mult_nonneg_nonneg)lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::linordered_semidom) < f x)  --> 0 < setprod f A"by (cases "finite A", induct set: finite, simp_all add: mult_pos_pos)lemma setprod_zero_iff[simp]: "finite A ==>   (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =  (EX x: A. f x = 0)"by (erule finite_induct, auto simp:no_zero_divisors)lemma setprod_pos_nat:  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])lemma setprod_pos_nat_iff[simp]:  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x ≠ 0) ==>  (setprod f (A Un B) :: 'a ::{field})   = setprod f A * setprod f B / setprod f (A Int B)"by (subst setprod_Un_Int [symmetric], auto)lemma setprod_Un2:  assumes "finite (A ∪ B)"  shows "setprod f (A ∪ B) = setprod f (A - B) * setprod f (B - A) * setprod f (A ∩ B)"proof -  have "A ∪ B = A - B ∪ (B - A) ∪ A ∩ B"    by auto  with assms show ?thesis by simp (subst setprod_Un_disjoint, auto)+qedlemma setprod_diff1: "finite A ==> f a ≠ 0 ==>  (setprod f (A - {a}) :: 'a :: {field}) =  (if a:A then setprod f A / f a else setprod f A)"  by (erule finite_induct) (auto simp add: insert_Diff_if)lemma setprod_inversef:   fixes f :: "'b => 'a::field_inverse_zero"  shows "finite A ==> setprod (inverse o f) A = inverse (setprod f A)"by (erule finite_induct) autolemma setprod_dividef:  fixes f :: "'b => 'a::field_inverse_zero"  shows "finite A    ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"apply (subgoal_tac         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse o g) x) A")apply (erule ssubst)apply (subst divide_inverse)apply (subst setprod_timesf)apply (subst setprod_inversef, assumption+, rule refl)apply (rule setprod_cong, rule refl)apply (subst divide_inverse, auto)donelemma setprod_dvd_setprod [rule_format]:     "(ALL x : A. f x dvd g x) --> setprod f A dvd setprod g A"  apply (cases "finite A")  apply (induct set: finite)  apply (auto simp add: dvd_def)  apply (rule_tac x = "k * ka" in exI)  apply (simp add: algebra_simps)donelemma setprod_dvd_setprod_subset:  "finite B ==> A <= B ==> setprod f A dvd setprod f B"  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")  apply (unfold dvd_def, blast)  apply (subst setprod_Un_disjoint [symmetric])  apply (auto elim: finite_subset intro: setprod_cong)donelemma setprod_dvd_setprod_subset2:  "finite B ==> A <= B ==> ALL x : A. (f x::'a::comm_semiring_1) dvd g x ==>       setprod f A dvd setprod g B"  apply (rule dvd_trans)  apply (rule setprod_dvd_setprod, erule (1) bspec)  apply (erule (1) setprod_dvd_setprod_subset)donelemma dvd_setprod: "finite A ==> i:A ==>     (f i ::'a::comm_semiring_1) dvd setprod f A"by (induct set: finite) (auto intro: dvd_mult)lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) -->     (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"  apply (cases "finite A")  apply (induct set: finite)  apply autodonelemma setprod_mono:  fixes f :: "'a => 'b::linordered_semidom"  assumes "∀i∈A. 0 ≤ f i ∧ f i ≤ g i"  shows "setprod f A ≤ setprod g A"proof (cases "finite A")  case True  hence ?thesis "setprod f A ≥ 0" using subset_refl[of A]  proof (induct A rule: finite_subset_induct)    case (insert a F)    thus "setprod f (insert a F) ≤ setprod g (insert a F)" "0 ≤ setprod f (insert a F)"      unfolding setprod_insert[OF insert(1,3)]      using assms[rule_format,OF insert(2)] insert      by (auto intro: mult_mono mult_nonneg_nonneg)  qed auto  thus ?thesis by simpqed autolemma abs_setprod:  fixes f :: "'a => 'b::{linordered_field,abs}"  shows "abs (setprod f A) = setprod (λx. abs (f x)) A"proof (cases "finite A")  case True thus ?thesis    by induct (auto simp add: field_simps abs_mult)qed autolemma setprod_constant: "finite A ==> (∏x∈ A. (y::'a::{comm_monoid_mult})) = y^(card A)"apply (erule finite_induct)apply autodonelemma setprod_gen_delta:  assumes fS: "finite S"  shows "setprod (λk. if k=a then b k else c) S = (if a ∈ S then (b a ::'a::{comm_monoid_mult}) * c^ (card S - 1) else c^ card S)"proof-  let ?f = "(λk. if k=a then b k else c)"  {assume a: "a ∉ S"    hence "∀ k∈ S. ?f k = c" by simp    hence ?thesis  using a setprod_constant[OF fS, of c] by simp }  moreover   {assume a: "a ∈ S"    let ?A = "S - {a}"    let ?B = "{a}"    have eq: "S = ?A ∪ ?B" using a by blast     have dj: "?A ∩ ?B = {}" by simp    from fS have fAB: "finite ?A" "finite ?B" by auto      have fA0:"setprod ?f ?A = setprod (λi. c) ?A"      apply (rule setprod_cong) by auto    have cA: "card ?A = card S - 1" using fS a by auto    have fA1: "setprod ?f ?A = c ^ card ?A"  unfolding fA0 apply (rule setprod_constant) using fS by auto    have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"      using setprod_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]      by simp    then have ?thesis using a cA      by (simp add: fA1 field_simps cong add: setprod_cong cong del: if_weak_cong)}  ultimately show ?thesis by blastqedsubsection {* Versions of @{const inf} and @{const sup} on non-empty sets *}no_notation times (infixl "*" 70)no_notation Groups.one ("1")locale semilattice_big = semilattice +  fixes F :: "'a set => 'a"  assumes F_eq: "finite A ==> F A = fold1 (op *) A"sublocale semilattice_big < folding_one_idem proofqed (simp_all add: F_eq)notation times (infixl "*" 70)notation Groups.one ("1")context latticebegindefinition Inf_fin :: "'a set => 'a" ("\<Sqinter>⇘fin⇙_" [900] 900) where  "Inf_fin = fold1 inf"definition Sup_fin :: "'a set => 'a" ("\<Squnion>⇘fin⇙_" [900] 900) where  "Sup_fin = fold1 sup"endsublocale lattice < Inf_fin!: semilattice_big inf Inf_fin proofqed (simp add: Inf_fin_def)sublocale lattice < Sup_fin!: semilattice_big sup Sup_fin proofqed (simp add: Sup_fin_def)context semilattice_infbeginlemma ab_semigroup_idem_mult_inf:  "class.ab_semigroup_idem_mult inf"proof qed (rule inf_assoc inf_commute inf_idem)+lemma fold_inf_insert[simp]: "finite A ==> Finite_Set.fold inf b (insert a A) = inf a (Finite_Set.fold inf b A)"by(rule comp_fun_idem.fold_insert_idem[OF ab_semigroup_idem_mult.comp_fun_idem[OF ab_semigroup_idem_mult_inf]])lemma inf_le_fold_inf: "finite A ==> ALL a:A. b ≤ a ==> inf b c ≤ Finite_Set.fold inf c A"by (induct pred: finite) (auto intro: le_infI1)lemma fold_inf_le_inf: "finite A ==> a ∈ A ==> Finite_Set.fold inf b A ≤ inf a b"proof(induct arbitrary: a pred:finite)  case empty thus ?case by simpnext  case (insert x A)  show ?case  proof cases    assume "A = {}" thus ?thesis using insert by simp  next    assume "A ≠ {}" thus ?thesis using insert by (auto intro: le_infI2)  qedqedlemma below_fold1_iff:  assumes "finite A" "A ≠ {}"  shows "x ≤ fold1 inf A <-> (∀a∈A. x ≤ a)"proof -  interpret ab_semigroup_idem_mult inf    by (rule ab_semigroup_idem_mult_inf)  show ?thesis using assms by (induct rule: finite_ne_induct) simp_allqedlemma fold1_belowI:  assumes "finite A"    and "a ∈ A"  shows "fold1 inf A ≤ a"proof -  from assms have "A ≠ {}" by auto  from `finite A` `A ≠ {}` `a ∈ A` show ?thesis  proof (induct rule: finite_ne_induct)    case singleton thus ?case by simp  next    interpret ab_semigroup_idem_mult inf      by (rule ab_semigroup_idem_mult_inf)    case (insert x F)    from insert(5) have "a = x ∨ a ∈ F" by simp    thus ?case    proof      assume "a = x" thus ?thesis using insert        by (simp add: mult_ac)    next      assume "a ∈ F"      hence bel: "fold1 inf F ≤ a" by (rule insert)      have "inf (fold1 inf (insert x F)) a = inf x (inf (fold1 inf F) a)"        using insert by (simp add: mult_ac)      also have "inf (fold1 inf F) a = fold1 inf F"        using bel by (auto intro: antisym)      also have "inf x … = fold1 inf (insert x F)"        using insert by (simp add: mult_ac)      finally have aux: "inf (fold1 inf (insert x F)) a = fold1 inf (insert x F)" .      moreover have "inf (fold1 inf (insert x F)) a ≤ a" by simp      ultimately show ?thesis by simp    qed  qedqedendcontext semilattice_supbeginlemma ab_semigroup_idem_mult_sup: "class.ab_semigroup_idem_mult sup"by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)lemma fold_sup_insert[simp]: "finite A ==> Finite_Set.fold sup b (insert a A) = sup a (Finite_Set.fold sup b A)"by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)lemma fold_sup_le_sup: "finite A ==> ALL a:A. a ≤ b ==> Finite_Set.fold sup c A ≤ sup b c"by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)lemma sup_le_fold_sup: "finite A ==> a ∈ A ==> sup a b ≤ Finite_Set.fold sup b A"by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)endcontext latticebeginlemma Inf_le_Sup [simp]: "[| finite A; A ≠ {} |] ==> \<Sqinter>⇘fin⇙A ≤ \<Squnion>⇘fin⇙A"apply(unfold Sup_fin_def Inf_fin_def)apply(subgoal_tac "EX a. a:A")prefer 2 apply blastapply(erule exE)apply(rule order_trans)apply(erule (1) fold1_belowI)apply(erule (1) semilattice_inf.fold1_belowI [OF dual_semilattice])donelemma sup_Inf_absorb [simp]:  "finite A ==> a ∈ A ==> sup a (\<Sqinter>⇘fin⇙A) = a"apply(subst sup_commute)apply(simp add: Inf_fin_def sup_absorb2 fold1_belowI)donelemma inf_Sup_absorb [simp]:  "finite A ==> a ∈ A ==> inf a (\<Squnion>⇘fin⇙A) = a"by (simp add: Sup_fin_def inf_absorb1  semilattice_inf.fold1_belowI [OF dual_semilattice])endcontext distrib_latticebeginlemma sup_Inf1_distrib:  assumes "finite A"    and "A ≠ {}"  shows "sup x (\<Sqinter>⇘fin⇙A) = \<Sqinter>⇘fin⇙{sup x a|a. a ∈ A}"proof -  interpret ab_semigroup_idem_mult inf    by (rule ab_semigroup_idem_mult_inf)  from assms show ?thesis    by (simp add: Inf_fin_def image_def      hom_fold1_commute [where h="sup x", OF sup_inf_distrib1])        (rule arg_cong [where f="fold1 inf"], blast)qedlemma sup_Inf2_distrib:  assumes A: "finite A" "A ≠ {}" and B: "finite B" "B ≠ {}"  shows "sup (\<Sqinter>⇘fin⇙A) (\<Sqinter>⇘fin⇙B) = \<Sqinter>⇘fin⇙{sup a b|a b. a ∈ A ∧ b ∈ B}"using A proof (induct rule: finite_ne_induct)  case singleton thus ?case    by (simp add: sup_Inf1_distrib [OF B])next  interpret ab_semigroup_idem_mult inf    by (rule ab_semigroup_idem_mult_inf)  case (insert x A)  have finB: "finite {sup x b |b. b ∈ B}"    by(rule finite_surj[where f = "sup x", OF B(1)], auto)  have finAB: "finite {sup a b |a b. a ∈ A ∧ b ∈ B}"  proof -    have "{sup a b |a b. a ∈ A ∧ b ∈ B} = (UN a:A. UN b:B. {sup a b})"      by blast    thus ?thesis by(simp add: insert(1) B(1))  qed  have ne: "{sup a b |a b. a ∈ A ∧ b ∈ B} ≠ {}" using insert B by blast  have "sup (\<Sqinter>⇘fin⇙(insert x A)) (\<Sqinter>⇘fin⇙B) = sup (inf x (\<Sqinter>⇘fin⇙A)) (\<Sqinter>⇘fin⇙B)"    using insert by simp  also have "… = inf (sup x (\<Sqinter>⇘fin⇙B)) (sup (\<Sqinter>⇘fin⇙A) (\<Sqinter>⇘fin⇙B))" by(rule sup_inf_distrib2)  also have "… = inf (\<Sqinter>⇘fin⇙{sup x b|b. b ∈ B}) (\<Sqinter>⇘fin⇙{sup a b|a b. a ∈ A ∧ b ∈ B})"    using insert by(simp add:sup_Inf1_distrib[OF B])  also have "… = \<Sqinter>⇘fin⇙({sup x b |b. b ∈ B} ∪ {sup a b |a b. a ∈ A ∧ b ∈ B})"    (is "_ = \<Sqinter>⇘fin⇙?M")    using B insert    by (simp add: Inf_fin_def fold1_Un2 [OF finB _ finAB ne])  also have "?M = {sup a b |a b. a ∈ insert x A ∧ b ∈ B}"    by blast  finally show ?case .qedlemma inf_Sup1_distrib:  assumes "finite A" and "A ≠ {}"  shows "inf x (\<Squnion>⇘fin⇙A) = \<Squnion>⇘fin⇙{inf x a|a. a ∈ A}"proof -  interpret ab_semigroup_idem_mult sup    by (rule ab_semigroup_idem_mult_sup)  from assms show ?thesis    by (simp add: Sup_fin_def image_def hom_fold1_commute [where h="inf x", OF inf_sup_distrib1])      (rule arg_cong [where f="fold1 sup"], blast)qedlemma inf_Sup2_distrib:  assumes A: "finite A" "A ≠ {}" and B: "finite B" "B ≠ {}"  shows "inf (\<Squnion>⇘fin⇙A) (\<Squnion>⇘fin⇙B) = \<Squnion>⇘fin⇙{inf a b|a b. a ∈ A ∧ b ∈ B}"using A proof (induct rule: finite_ne_induct)  case singleton thus ?case    by(simp add: inf_Sup1_distrib [OF B])next  case (insert x A)  have finB: "finite {inf x b |b. b ∈ B}"    by(rule finite_surj[where f = "%b. inf x b", OF B(1)], auto)  have finAB: "finite {inf a b |a b. a ∈ A ∧ b ∈ B}"  proof -    have "{inf a b |a b. a ∈ A ∧ b ∈ B} = (UN a:A. UN b:B. {inf a b})"      by blast    thus ?thesis by(simp add: insert(1) B(1))  qed  have ne: "{inf a b |a b. a ∈ A ∧ b ∈ B} ≠ {}" using insert B by blast  interpret ab_semigroup_idem_mult sup    by (rule ab_semigroup_idem_mult_sup)  have "inf (\<Squnion>⇘fin⇙(insert x A)) (\<Squnion>⇘fin⇙B) = inf (sup x (\<Squnion>⇘fin⇙A)) (\<Squnion>⇘fin⇙B)"    using insert by simp  also have "… = sup (inf x (\<Squnion>⇘fin⇙B)) (inf (\<Squnion>⇘fin⇙A) (\<Squnion>⇘fin⇙B))" by(rule inf_sup_distrib2)  also have "… = sup (\<Squnion>⇘fin⇙{inf x b|b. b ∈ B}) (\<Squnion>⇘fin⇙{inf a b|a b. a ∈ A ∧ b ∈ B})"    using insert by(simp add:inf_Sup1_distrib[OF B])  also have "… = \<Squnion>⇘fin⇙({inf x b |b. b ∈ B} ∪ {inf a b |a b. a ∈ A ∧ b ∈ B})"    (is "_ = \<Squnion>⇘fin⇙?M")    using B insert    by (simp add: Sup_fin_def fold1_Un2 [OF finB _ finAB ne])  also have "?M = {inf a b |a b. a ∈ insert x A ∧ b ∈ B}"    by blast  finally show ?case .qedendcontext complete_latticebeginlemma Inf_fin_Inf:  assumes "finite A" and "A ≠ {}"  shows "\<Sqinter>⇘fin⇙A = Inf A"proof -  interpret ab_semigroup_idem_mult inf    by (rule ab_semigroup_idem_mult_inf)  from `A ≠ {}` obtain b B where "A = {b} ∪ B" by auto  moreover with `finite A` have "finite B" by simp  ultimately show ?thesis    by (simp add: Inf_fin_def fold1_eq_fold_idem inf_Inf_fold_inf [symmetric])qedlemma Sup_fin_Sup:  assumes "finite A" and "A ≠ {}"  shows "\<Squnion>⇘fin⇙A = Sup A"proof -  interpret ab_semigroup_idem_mult sup    by (rule ab_semigroup_idem_mult_sup)  from `A ≠ {}` obtain b B where "A = {b} ∪ B" by auto  moreover with `finite A` have "finite B" by simp  ultimately show ?thesis    by (simp add: Sup_fin_def fold1_eq_fold_idem sup_Sup_fold_sup [symmetric])qedendsubsection {* Versions of @{const min} and @{const max} on non-empty sets *}definition (in linorder) Min :: "'a set => 'a" where  "Min = fold1 min"definition (in linorder) Max :: "'a set => 'a" where  "Max = fold1 max"sublocale linorder < Min!: semilattice_big min Min proofqed (simp add: Min_def)sublocale linorder < Max!: semilattice_big max Max proofqed (simp add: Max_def)context linorderbeginlemmas Min_singleton = Min.singletonlemmas Max_singleton = Max.singletonlemma Min_insert:  assumes "finite A" and "A ≠ {}"  shows "Min (insert x A) = min x (Min A)"  using assms by simplemma Max_insert:  assumes "finite A" and "A ≠ {}"  shows "Max (insert x A) = max x (Max A)"  using assms by simplemma Min_Un:  assumes "finite A" and "A ≠ {}" and "finite B" and "B ≠ {}"  shows "Min (A ∪ B) = min (Min A) (Min B)"  using assms by (rule Min.union_idem)lemma Max_Un:  assumes "finite A" and "A ≠ {}" and "finite B" and "B ≠ {}"  shows "Max (A ∪ B) = max (Max A) (Max B)"  using assms by (rule Max.union_idem)lemma hom_Min_commute:  assumes "!!x y. h (min x y) = min (h x) (h y)"    and "finite N" and "N ≠ {}"  shows "h (Min N) = Min (h ` N)"  using assms by (rule Min.hom_commute)lemma hom_Max_commute:  assumes "!!x y. h (max x y) = max (h x) (h y)"    and "finite N" and "N ≠ {}"  shows "h (Max N) = Max (h ` N)"  using assms by (rule Max.hom_commute)lemma ab_semigroup_idem_mult_min:  "class.ab_semigroup_idem_mult min"  proof qed (auto simp add: min_def)lemma ab_semigroup_idem_mult_max:  "class.ab_semigroup_idem_mult max"  proof qed (auto simp add: max_def)lemma max_lattice:  "class.semilattice_inf max (op ≥) (op >)"  by (fact min_max.dual_semilattice)lemma dual_max:  "ord.max (op ≥) = min"  by (auto simp add: ord.max_def min_def fun_eq_iff)lemma dual_min:  "ord.min (op ≥) = max"  by (auto simp add: ord.min_def max_def fun_eq_iff)lemma strict_below_fold1_iff:  assumes "finite A" and "A ≠ {}"  shows "x < fold1 min A <-> (∀a∈A. x < a)"proof -  interpret ab_semigroup_idem_mult min    by (rule ab_semigroup_idem_mult_min)  from assms show ?thesis  by (induct rule: finite_ne_induct)    (simp_all add: fold1_insert)qedlemma fold1_below_iff:  assumes "finite A" and "A ≠ {}"  shows "fold1 min A ≤ x <-> (∃a∈A. a ≤ x)"proof -  interpret ab_semigroup_idem_mult min    by (rule ab_semigroup_idem_mult_min)  from assms show ?thesis  by (induct rule: finite_ne_induct)    (simp_all add: fold1_insert min_le_iff_disj)qedlemma fold1_strict_below_iff:  assumes "finite A" and "A ≠ {}"  shows "fold1 min A < x <-> (∃a∈A. a < x)"proof -  interpret ab_semigroup_idem_mult min    by (rule ab_semigroup_idem_mult_min)  from assms show ?thesis  by (induct rule: finite_ne_induct)    (simp_all add: fold1_insert min_less_iff_disj)qedlemma fold1_antimono:  assumes "A ≠ {}" and "A ⊆ B" and "finite B"  shows "fold1 min B ≤ fold1 min A"proof cases  assume "A = B" thus ?thesis by simpnext  interpret ab_semigroup_idem_mult min    by (rule ab_semigroup_idem_mult_min)  assume neq: "A ≠ B"  have B: "B = A ∪ (B-A)" using `A ⊆ B` by blast  have "fold1 min B = fold1 min (A ∪ (B-A))" by(subst B)(rule refl)  also have "… = min (fold1 min A) (fold1 min (B-A))"  proof -    have "finite A" by(rule finite_subset[OF `A ⊆ B` `finite B`])    moreover have "finite(B-A)" by(rule finite_Diff[OF `finite B`])    moreover have "(B-A) ≠ {}" using assms neq by blast    moreover have "A Int (B-A) = {}" using assms by blast    ultimately show ?thesis using `A ≠ {}` by (rule_tac fold1_Un)  qed  also have "… ≤ fold1 min A" by (simp add: min_le_iff_disj)  finally show ?thesis .qedlemma Min_in [simp]:  assumes "finite A" and "A ≠ {}"  shows "Min A ∈ A"proof -  interpret ab_semigroup_idem_mult min    by (rule ab_semigroup_idem_mult_min)  from assms fold1_in show ?thesis by (fastforce simp: Min_def min_def)qedlemma Max_in [simp]:  assumes "finite A" and "A ≠ {}"  shows "Max A ∈ A"proof -  interpret ab_semigroup_idem_mult max    by (rule ab_semigroup_idem_mult_max)  from assms fold1_in [of A] show ?thesis by (fastforce simp: Max_def max_def)qedlemma Min_le [simp]:  assumes "finite A" and "x ∈ A"  shows "Min A ≤ x"  using assms by (simp add: Min_def min_max.fold1_belowI)lemma Max_ge [simp]:  assumes "finite A" and "x ∈ A"  shows "x ≤ Max A"  by (simp add: Max_def semilattice_inf.fold1_belowI [OF max_lattice] assms)lemma Min_ge_iff [simp, no_atp]:  assumes "finite A" and "A ≠ {}"  shows "x ≤ Min A <-> (∀a∈A. x ≤ a)"  using assms by (simp add: Min_def min_max.below_fold1_iff)lemma Max_le_iff [simp, no_atp]:  assumes "finite A" and "A ≠ {}"  shows "Max A ≤ x <-> (∀a∈A. a ≤ x)"  by (simp add: Max_def semilattice_inf.below_fold1_iff [OF max_lattice] assms)lemma Min_gr_iff [simp, no_atp]:  assumes "finite A" and "A ≠ {}"  shows "x < Min A <-> (∀a∈A. x < a)"  using assms by (simp add: Min_def strict_below_fold1_iff)lemma Max_less_iff [simp, no_atp]:  assumes "finite A" and "A ≠ {}"  shows "Max A < x <-> (∀a∈A. a < x)"  by (simp add: Max_def linorder.dual_max [OF dual_linorder]    linorder.strict_below_fold1_iff [OF dual_linorder] assms)lemma Min_le_iff [no_atp]:  assumes "finite A" and "A ≠ {}"  shows "Min A ≤ x <-> (∃a∈A. a ≤ x)"  using assms by (simp add: Min_def fold1_below_iff)lemma Max_ge_iff [no_atp]:  assumes "finite A" and "A ≠ {}"  shows "x ≤ Max A <-> (∃a∈A. x ≤ a)"  by (simp add: Max_def linorder.dual_max [OF dual_linorder]    linorder.fold1_below_iff [OF dual_linorder] assms)lemma Min_less_iff [no_atp]:  assumes "finite A" and "A ≠ {}"  shows "Min A < x <-> (∃a∈A. a < x)"  using assms by (simp add: Min_def fold1_strict_below_iff)lemma Max_gr_iff [no_atp]:  assumes "finite A" and "A ≠ {}"  shows "x < Max A <-> (∃a∈A. x < a)"  by (simp add: Max_def linorder.dual_max [OF dual_linorder]    linorder.fold1_strict_below_iff [OF dual_linorder] assms)lemma Min_eqI:  assumes "finite A"  assumes "!!y. y ∈ A ==> y ≥ x"    and "x ∈ A"  shows "Min A = x"proof (rule antisym)  from `x ∈ A` have "A ≠ {}" by auto  with assms show "Min A ≥ x" by simpnext  from assms show "x ≥ Min A" by simpqedlemma Max_eqI:  assumes "finite A"  assumes "!!y. y ∈ A ==> y ≤ x"    and "x ∈ A"  shows "Max A = x"proof (rule antisym)  from `x ∈ A` have "A ≠ {}" by auto  with assms show "Max A ≤ x" by simpnext  from assms show "x ≤ Max A" by simpqedlemma Min_antimono:  assumes "M ⊆ N" and "M ≠ {}" and "finite N"  shows "Min N ≤ Min M"  using assms by (simp add: Min_def fold1_antimono)lemma Max_mono:  assumes "M ⊆ N" and "M ≠ {}" and "finite N"  shows "Max M ≤ Max N"  by (simp add: Max_def linorder.dual_max [OF dual_linorder]    linorder.fold1_antimono [OF dual_linorder] assms)lemma finite_linorder_max_induct[consumes 1, case_names empty insert]: assumes fin: "finite A" and   empty: "P {}"  and  insert: "(!!b A. finite A ==> ALL a:A. a < b ==> P A ==> P(insert b A))" shows "P A"using fin empty insertproof (induct rule: finite_psubset_induct)  case (psubset A)  have IH: "!!B. [|B < A; P {}; (!!A b. [|finite A; ∀a∈A. a<b; P A|] ==> P (insert b A))|] ==> P B" by fact   have fin: "finite A" by fact   have empty: "P {}" by fact  have step: "!!b A. [|finite A; ∀a∈A. a < b; P A|] ==> P (insert b A)" by fact  show "P A"  proof (cases "A = {}")    assume "A = {}"     then show "P A" using `P {}` by simp  next    let ?B = "A - {Max A}"     let ?A = "insert (Max A) ?B"    have "finite ?B" using `finite A` by simp    assume "A ≠ {}"    with `finite A` have "Max A : A" by auto    then have A: "?A = A" using insert_Diff_single insert_absorb by auto    then have "P ?B" using `P {}` step IH[of ?B] by blast    moreover     have "∀a∈?B. a < Max A" using Max_ge [OF `finite A`] by fastforce    ultimately show "P A" using A insert_Diff_single step[OF `finite ?B`] by fastforce  qedqedlemma finite_linorder_min_induct[consumes 1, case_names empty insert]: "[|finite A; P {}; !!b A. [|finite A; ∀a∈A. b < a; P A|] ==> P (insert b A)|] ==> P A"by(rule linorder.finite_linorder_max_induct[OF dual_linorder])endcontext linordered_ab_semigroup_addbeginlemma add_Min_commute:  fixes k  assumes "finite N" and "N ≠ {}"  shows "k + Min N = Min {k + m | m. m ∈ N}"proof -  have "!!x y. k + min x y = min (k + x) (k + y)"    by (simp add: min_def not_le)      (blast intro: antisym less_imp_le add_left_mono)  with assms show ?thesis    using hom_Min_commute [of "plus k" N]    by simp (blast intro: arg_cong [where f = Min])qedlemma add_Max_commute:  fixes k  assumes "finite N" and "N ≠ {}"  shows "k + Max N = Max {k + m | m. m ∈ N}"proof -  have "!!x y. k + max x y = max (k + x) (k + y)"    by (simp add: max_def not_le)      (blast intro: antisym less_imp_le add_left_mono)  with assms show ?thesis    using hom_Max_commute [of "plus k" N]    by simp (blast intro: arg_cong [where f = Max])qedendcontext linordered_ab_group_addbeginlemma minus_Max_eq_Min [simp]:  "finite S ==> S ≠ {} ==> - (Max S) = Min (uminus ` S)"  by (induct S rule: finite_ne_induct) (simp_all add: minus_max_eq_min)lemma minus_Min_eq_Max [simp]:  "finite S ==> S ≠ {} ==> - (Min S) = Max (uminus ` S)"  by (induct S rule: finite_ne_induct) (simp_all add: minus_min_eq_max)endend`