src/HOL/Finite_Set.thy
changeset 16733 236dfafbeb63
parent 16632 ad2895beef79
child 16760 5c5f051aaaaa
equal deleted inserted replaced
16732:1bbe526a552c 16733:236dfafbeb63
   889 
   889 
   890 lemma setsum_cong:
   890 lemma setsum_cong:
   891   "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   891   "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   892 by(fastsimp simp: setsum_def intro: AC_add.fold_cong)
   892 by(fastsimp simp: setsum_def intro: AC_add.fold_cong)
   893 
   893 
   894 lemma strong_setsum_cong:
   894 lemma strong_setsum_cong[cong]:
   895   "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setsum f A = setsum g B"
   895   "A = B ==> (!!x. x:B =simp=> f x = g x)
       
   896    ==> setsum (%x. f x) A = setsum (%x. g x) B"
   896 by(fastsimp simp: simp_implies_def setsum_def intro: AC_add.fold_cong)
   897 by(fastsimp simp: simp_implies_def setsum_def intro: AC_add.fold_cong)
   897 
   898 
   898 lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A";
   899 lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A";
   899   by (rule setsum_cong[OF refl], auto);
   900   by (rule setsum_cong[OF refl], auto);
   900 
   901 
  1700 apply(rotate_tac -1)
  1701 apply(rotate_tac -1)
  1701   apply (induct set: Finites, simp_all, clarify)
  1702   apply (induct set: Finites, simp_all, clarify)
  1702   apply (subst card_Un_disjoint)
  1703   apply (subst card_Un_disjoint)
  1703   apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
  1704   apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
  1704   done
  1705   done
  1705 
       
  1706 
       
  1707 subsubsection {* Theorems about @{text "choose"} *}
       
  1708 
       
  1709 text {*
       
  1710   \medskip Basic theorem about @{text "choose"}.  By Florian
       
  1711   Kamm\"uller, tidied by LCP.
       
  1712 *}
       
  1713 
       
  1714 lemma card_s_0_eq_empty:
       
  1715     "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
       
  1716   apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
       
  1717   apply (simp cong add: rev_conj_cong)
       
  1718   done
       
  1719 
       
  1720 lemma choose_deconstruct: "finite M ==> x \<notin> M
       
  1721   ==> {s. s <= insert x M & card(s) = Suc k}
       
  1722        = {s. s <= M & card(s) = Suc k} Un
       
  1723          {s. EX t. t <= M & card(t) = k & s = insert x t}"
       
  1724   apply safe
       
  1725    apply (auto intro: finite_subset [THEN card_insert_disjoint])
       
  1726   apply (drule_tac x = "xa - {x}" in spec)
       
  1727   apply (subgoal_tac "x \<notin> xa", auto)
       
  1728   apply (erule rev_mp, subst card_Diff_singleton)
       
  1729   apply (auto intro: finite_subset)
       
  1730   done
       
  1731 
       
  1732 text{*There are as many subsets of @{term A} having cardinality @{term k}
       
  1733  as there are sets obtained from the former by inserting a fixed element
       
  1734  @{term x} into each.*}
       
  1735 lemma constr_bij:
       
  1736    "[|finite A; x \<notin> A|] ==>
       
  1737     card {B. EX C. C <= A & card(C) = k & B = insert x C} =
       
  1738     card {B. B <= A & card(B) = k}"
       
  1739   apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
       
  1740        apply (auto elim!: equalityE simp add: inj_on_def)
       
  1741     apply (subst Diff_insert0, auto)
       
  1742    txt {* finiteness of the two sets *}
       
  1743    apply (rule_tac [2] B = "Pow (A)" in finite_subset)
       
  1744    apply (rule_tac B = "Pow (insert x A)" in finite_subset)
       
  1745    apply fast+
       
  1746   done
       
  1747 
       
  1748 text {*
       
  1749   Main theorem: combinatorial statement about number of subsets of a set.
       
  1750 *}
       
  1751 
       
  1752 lemma n_sub_lemma:
       
  1753   "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
       
  1754   apply (induct k)
       
  1755    apply (simp add: card_s_0_eq_empty, atomize)
       
  1756   apply (rotate_tac -1, erule finite_induct)
       
  1757    apply (simp_all (no_asm_simp) cong add: conj_cong
       
  1758      add: card_s_0_eq_empty choose_deconstruct)
       
  1759   apply (subst card_Un_disjoint)
       
  1760      prefer 4 apply (force simp add: constr_bij)
       
  1761     prefer 3 apply force
       
  1762    prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
       
  1763      finite_subset [of _ "Pow (insert x F)", standard])
       
  1764   apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
       
  1765   done
       
  1766 
       
  1767 theorem n_subsets:
       
  1768     "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
       
  1769   by (simp add: n_sub_lemma)
       
  1770 
  1706 
  1771 
  1707 
  1772 subsection{* A fold functional for non-empty sets *}
  1708 subsection{* A fold functional for non-empty sets *}
  1773 
  1709 
  1774 text{* Does not require start value. *}
  1710 text{* Does not require start value. *}