src/HOL/Finite_Set.thy
changeset 15539 333a88244569
parent 15535 a0cf3a19ee36
child 15542 ee6cd48cf840
equal deleted inserted replaced
15538:d8edf54cc28c 15539:333a88244569
  1062   case False
  1062   case False
  1063   thus ?thesis
  1063   thus ?thesis
  1064     by (simp add: setsum_def)
  1064     by (simp add: setsum_def)
  1065 qed
  1065 qed
  1066 
  1066 
       
  1067 lemma setsum_negf:
       
  1068  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
       
  1069 proof (cases "finite A")
       
  1070   case True thus ?thesis by (induct set: Finites, auto)
       
  1071 next
       
  1072   case False thus ?thesis by (simp add: setsum_def)
       
  1073 qed
       
  1074 
       
  1075 lemma setsum_subtractf:
       
  1076  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
       
  1077   setsum f A - setsum g A"
       
  1078 proof (cases "finite A")
       
  1079   case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
       
  1080 next
       
  1081   case False thus ?thesis by (simp add: setsum_def)
       
  1082 qed
       
  1083 
       
  1084 lemma setsum_nonneg:
       
  1085 assumes nn: "\<forall>x\<in>A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
       
  1086 shows "0 \<le> setsum f A"
       
  1087 proof (cases "finite A")
       
  1088   case True thus ?thesis using nn
       
  1089   apply (induct set: Finites, auto)
       
  1090   apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
       
  1091   apply (blast intro: add_mono)
       
  1092   done
       
  1093 next
       
  1094   case False thus ?thesis by (simp add: setsum_def)
       
  1095 qed
       
  1096 
       
  1097 lemma setsum_nonpos:
       
  1098 assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})"
       
  1099 shows "setsum f A \<le> 0"
       
  1100 proof (cases "finite A")
       
  1101   case True thus ?thesis using np
       
  1102   apply (induct set: Finites, auto)
       
  1103   apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
       
  1104   apply (blast intro: add_mono)
       
  1105   done
       
  1106 next
       
  1107   case False thus ?thesis by (simp add: setsum_def)
       
  1108 qed
       
  1109 
       
  1110 lemma setsum_mono2:
       
  1111 fixes f :: "'a \<Rightarrow> 'b :: {pordered_ab_semigroup_add_imp_le,comm_monoid_add}"
       
  1112 assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
       
  1113 shows "setsum f A \<le> setsum f B"
       
  1114 proof -
       
  1115   have "setsum f A \<le> setsum f A + setsum f (B-A)"
       
  1116     by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
       
  1117   also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
       
  1118     by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
       
  1119   also have "A \<union> (B-A) = B" using sub by blast
       
  1120   finally show ?thesis .
       
  1121 qed
       
  1122 (*
  1067 lemma setsum_mono2_nat:
  1123 lemma setsum_mono2_nat:
  1068   assumes fin: "finite B" and sub: "A \<subseteq> B"
  1124   assumes fin: "finite B" and sub: "A \<subseteq> B"
  1069 shows "setsum f A \<le> (setsum f B :: nat)"
  1125 shows "setsum f A \<le> (setsum f B :: nat)"
  1070 proof -
  1126 proof -
  1071   have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
  1127   have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
  1072   also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
  1128   also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
  1073     by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
  1129     by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
  1074   also have "A \<union> (B-A) = B" using sub by blast
  1130   also have "A \<union> (B-A) = B" using sub by blast
  1075   finally show ?thesis .
  1131   finally show ?thesis .
  1076 qed
  1132 qed
  1077 
  1133 *)
  1078 lemma setsum_negf:
       
  1079  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
       
  1080 proof (cases "finite A")
       
  1081   case True thus ?thesis by (induct set: Finites, auto)
       
  1082 next
       
  1083   case False thus ?thesis by (simp add: setsum_def)
       
  1084 qed
       
  1085 
       
  1086 lemma setsum_subtractf:
       
  1087  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
       
  1088   setsum f A - setsum g A"
       
  1089 proof (cases "finite A")
       
  1090   case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
       
  1091 next
       
  1092   case False thus ?thesis by (simp add: setsum_def)
       
  1093 qed
       
  1094 
       
  1095 lemma setsum_nonneg:
       
  1096 assumes nn: "\<forall>x\<in>A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
       
  1097 shows "0 \<le> setsum f A"
       
  1098 proof (cases "finite A")
       
  1099   case True thus ?thesis using nn
       
  1100   apply (induct set: Finites, auto)
       
  1101   apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
       
  1102   apply (blast intro: add_mono)
       
  1103   done
       
  1104 next
       
  1105   case False thus ?thesis by (simp add: setsum_def)
       
  1106 qed
       
  1107 
       
  1108 lemma setsum_nonpos:
       
  1109 assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})"
       
  1110 shows "setsum f A \<le> 0"
       
  1111 proof (cases "finite A")
       
  1112   case True thus ?thesis using np
       
  1113   apply (induct set: Finites, auto)
       
  1114   apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
       
  1115   apply (blast intro: add_mono)
       
  1116   done
       
  1117 next
       
  1118   case False thus ?thesis by (simp add: setsum_def)
       
  1119 qed
       
  1120 
       
  1121 lemma setsum_mult: 
  1134 lemma setsum_mult: 
  1122   fixes f :: "'a => ('b::semiring_0_cancel)"
  1135   fixes f :: "'a => ('b::semiring_0_cancel)"
  1123   shows "r * setsum f A = setsum (%n. r * f n) A"
  1136   shows "r * setsum f A = setsum (%n. r * f n) A"
  1124 proof (cases "finite A")
  1137 proof (cases "finite A")
  1125   case True
  1138   case True
  1157   thus ?thesis
  1170   thus ?thesis
  1158   proof (induct)
  1171   proof (induct)
  1159     case empty thus ?case by simp
  1172     case empty thus ?case by simp
  1160   next
  1173   next
  1161     case (insert x A) thus ?case by (auto intro: order_trans)
  1174     case (insert x A) thus ?case by (auto intro: order_trans)
       
  1175   qed
       
  1176 next
       
  1177   case False thus ?thesis by (simp add: setsum_def)
       
  1178 qed
       
  1179 
       
  1180 lemma abs_setsum_abs[simp]: 
       
  1181   fixes f :: "'a => ('b::lordered_ab_group_abs)"
       
  1182   shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
       
  1183 proof (cases "finite A")
       
  1184   case True
       
  1185   thus ?thesis
       
  1186   proof (induct)
       
  1187     case empty thus ?case by simp
       
  1188   next
       
  1189     case (insert a A)
       
  1190     hence "\<bar>\<Sum>a\<in>insert a A. \<bar>f a\<bar>\<bar> = \<bar>\<bar>f a\<bar> + (\<Sum>a\<in>A. \<bar>f a\<bar>)\<bar>" by simp
       
  1191     also have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>"  using insert by simp
       
  1192     also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>" by simp
       
  1193     also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
       
  1194     finally show ?case .
  1162   qed
  1195   qed
  1163 next
  1196 next
  1164   case False thus ?thesis by (simp add: setsum_def)
  1197   case False thus ?thesis by (simp add: setsum_def)
  1165 qed
  1198 qed
  1166 
  1199 
  1428 
  1461 
  1429 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  1462 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  1430   by (simp add: card_insert_if)
  1463   by (simp add: card_insert_if)
  1431 
  1464 
  1432 lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
  1465 lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
  1433 by (simp add: card_def setsum_mono2_nat)
  1466 by (simp add: card_def setsum_mono2)
  1434 
  1467 
  1435 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
  1468 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
  1436   apply (induct set: Finites, simp, clarify)
  1469   apply (induct set: Finites, simp, clarify)
  1437   apply (subgoal_tac "finite A & A - {x} <= F")
  1470   apply (subgoal_tac "finite A & A - {x} <= F")
  1438    prefer 2 apply (blast intro: finite_subset, atomize)
  1471    prefer 2 apply (blast intro: finite_subset, atomize)
  1495 apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
  1528 apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
  1496        finite_subset [of _ "\<Union> (insert x F)"])
  1529        finite_subset [of _ "\<Union> (insert x F)"])
  1497 done
  1530 done
  1498 
  1531 
  1499 
  1532 
  1500 lemma setsum_constant_nat: "(\<Sum>x\<in>A. y) = (card A) * y"
  1533 lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat(card A) * y"
  1501   -- {* Generalized to any @{text comm_semiring_1_cancel} in
  1534 apply (cases "finite A")
  1502         @{text IntDef} as @{text setsum_constant}. *}
  1535 apply (erule finite_induct)
  1503 apply (cases "finite A") 
  1536 apply (auto simp add: ring_distrib add_ac)
  1504 apply (erule finite_induct, auto)
  1537 done
  1505 done
  1538 
  1506 
  1539 
  1507 lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
  1540 lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
  1508   apply (erule finite_induct)
  1541   apply (erule finite_induct)
  1509   apply (auto simp add: power_Suc)
  1542   apply (auto simp add: power_Suc)
  1510   done
  1543   done
  1511 
  1544 
  1512 
  1545 
  1513 subsubsection {* Cardinality of unions *}
  1546 subsubsection {* Cardinality of unions *}
       
  1547 
       
  1548 lemma of_nat_id[simp]: "(of_nat n :: nat) = n"
       
  1549 by(induct n, auto)
  1514 
  1550 
  1515 lemma card_UN_disjoint:
  1551 lemma card_UN_disjoint:
  1516     "finite I ==> (ALL i:I. finite (A i)) ==>
  1552     "finite I ==> (ALL i:I. finite (A i)) ==>
  1517         (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  1553         (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  1518       card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
  1554       card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
  1519   apply (simp add: card_def)
  1555   apply (simp add: card_def del: setsum_constant)
  1520   apply (subgoal_tac
  1556   apply (subgoal_tac
  1521            "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
  1557            "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
  1522   apply (simp add: setsum_UN_disjoint)
  1558   apply (simp add: setsum_UN_disjoint del: setsum_constant)
  1523   apply (simp add: setsum_constant_nat cong: setsum_cong)
  1559   apply (simp cong: setsum_cong)
  1524   done
  1560   done
  1525 
  1561 
  1526 lemma card_Union_disjoint:
  1562 lemma card_Union_disjoint:
  1527   "finite C ==> (ALL A:C. finite A) ==>
  1563   "finite C ==> (ALL A:C. finite A) ==>
  1528         (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
  1564         (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
  1544   apply (induct set: Finites, simp)
  1580   apply (induct set: Finites, simp)
  1545   apply (simp add: le_SucI finite_imageI card_insert_if)
  1581   apply (simp add: le_SucI finite_imageI card_insert_if)
  1546   done
  1582   done
  1547 
  1583 
  1548 lemma card_image: "inj_on f A ==> card (f ` A) = card A"
  1584 lemma card_image: "inj_on f A ==> card (f ` A) = card A"
  1549 by(simp add:card_def setsum_reindex o_def)
  1585 by(simp add:card_def setsum_reindex o_def del:setsum_constant)
  1550 
  1586 
  1551 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
  1587 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
  1552   by (simp add: card_seteq card_image)
  1588   by (simp add: card_seteq card_image)
  1553 
  1589 
  1554 lemma eq_card_imp_inj_on:
  1590 lemma eq_card_imp_inj_on:
  1585 *)
  1621 *)
  1586 
  1622 
  1587 lemma card_SigmaI [simp]:
  1623 lemma card_SigmaI [simp]:
  1588   "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
  1624   "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
  1589   \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
  1625   \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
  1590 by(simp add:card_def setsum_Sigma)
  1626 by(simp add:card_def setsum_Sigma del:setsum_constant)
  1591 
  1627 
  1592 lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
  1628 lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
  1593 apply (cases "finite A") 
  1629 apply (cases "finite A") 
  1594 apply (cases "finite B") 
  1630 apply (cases "finite B") 
  1595   apply (simp add: setsum_constant_nat) 
       
  1596 apply (auto simp add: card_eq_0_iff
  1631 apply (auto simp add: card_eq_0_iff
  1597             dest: finite_cartesian_productD1 finite_cartesian_productD2) 
  1632             dest: finite_cartesian_productD1 finite_cartesian_productD2)
  1598 done
  1633 done
  1599 
  1634 
  1600 lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
  1635 lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
  1601 by (simp add: card_cartesian_product) 
  1636 by (simp add: card_cartesian_product)
  1602 
  1637 
  1603 
  1638 
  1604 
  1639 
  1605 subsubsection {* Cardinality of the Powerset *}
  1640 subsubsection {* Cardinality of the Powerset *}
  1606 
  1641