src/HOL/Finite_Set.thy
author hoelzl
Tue Nov 05 09:44:57 2013 +0100 (2013-11-05)
changeset 54257 5c7a3b6b05a9
parent 54148 c8cc5ab4a863
child 54413 88a036a95967
permissions -rw-r--r--
generalize SUP and INF to the syntactic type classes Sup and Inf
     1 (*  Title:      HOL/Finite_Set.thy
     2     Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
     3                 with contributions by Jeremy Avigad
     4 *)
     5 
     6 header {* Finite sets *}
     7 
     8 theory Finite_Set
     9 imports Option Power
    10 begin
    11 
    12 subsection {* Predicate for finite sets *}
    13 
    14 inductive finite :: "'a set \<Rightarrow> bool"
    15   where
    16     emptyI [simp, intro!]: "finite {}"
    17   | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)"
    18 
    19 simproc_setup finite_Collect ("finite (Collect P)") = {* K Set_Comprehension_Pointfree.simproc *}
    20 
    21 lemma finite_induct [case_names empty insert, induct set: finite]:
    22   -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
    23   assumes "finite F"
    24   assumes "P {}"
    25     and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
    26   shows "P F"
    27 using `finite F`
    28 proof induct
    29   show "P {}" by fact
    30   fix x F assume F: "finite F" and P: "P F"
    31   show "P (insert x F)"
    32   proof cases
    33     assume "x \<in> F"
    34     hence "insert x F = F" by (rule insert_absorb)
    35     with P show ?thesis by (simp only:)
    36   next
    37     assume "x \<notin> F"
    38     from F this P show ?thesis by (rule insert)
    39   qed
    40 qed
    41 
    42 lemma infinite_finite_induct [case_names infinite empty insert]:
    43   assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A"
    44   assumes empty: "P {}"
    45   assumes insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
    46   shows "P A"
    47 proof (cases "finite A")
    48   case False with infinite show ?thesis .
    49 next
    50   case True then show ?thesis by (induct A) (fact empty insert)+
    51 qed
    52 
    53 
    54 subsubsection {* Choice principles *}
    55 
    56 lemma ex_new_if_finite: -- "does not depend on def of finite at all"
    57   assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
    58   shows "\<exists>a::'a. a \<notin> A"
    59 proof -
    60   from assms have "A \<noteq> UNIV" by blast
    61   then show ?thesis by blast
    62 qed
    63 
    64 text {* A finite choice principle. Does not need the SOME choice operator. *}
    65 
    66 lemma finite_set_choice:
    67   "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
    68 proof (induct rule: finite_induct)
    69   case empty then show ?case by simp
    70 next
    71   case (insert a A)
    72   then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
    73   show ?case (is "EX f. ?P f")
    74   proof
    75     show "?P(%x. if x = a then b else f x)" using f ab by auto
    76   qed
    77 qed
    78 
    79 
    80 subsubsection {* Finite sets are the images of initial segments of natural numbers *}
    81 
    82 lemma finite_imp_nat_seg_image_inj_on:
    83   assumes "finite A" 
    84   shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
    85 using assms
    86 proof induct
    87   case empty
    88   show ?case
    89   proof
    90     show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}" by simp 
    91   qed
    92 next
    93   case (insert a A)
    94   have notinA: "a \<notin> A" by fact
    95   from insert.hyps obtain n f
    96     where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
    97   hence "insert a A = f(n:=a) ` {i. i < Suc n}"
    98         "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
    99     by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
   100   thus ?case by blast
   101 qed
   102 
   103 lemma nat_seg_image_imp_finite:
   104   "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
   105 proof (induct n arbitrary: A)
   106   case 0 thus ?case by simp
   107 next
   108   case (Suc n)
   109   let ?B = "f ` {i. i < n}"
   110   have finB: "finite ?B" by(rule Suc.hyps[OF refl])
   111   show ?case
   112   proof cases
   113     assume "\<exists>k<n. f n = f k"
   114     hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
   115     thus ?thesis using finB by simp
   116   next
   117     assume "\<not>(\<exists> k<n. f n = f k)"
   118     hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
   119     thus ?thesis using finB by simp
   120   qed
   121 qed
   122 
   123 lemma finite_conv_nat_seg_image:
   124   "finite A \<longleftrightarrow> (\<exists>(n::nat) f. A = f ` {i::nat. i < n})"
   125   by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
   126 
   127 lemma finite_imp_inj_to_nat_seg:
   128   assumes "finite A"
   129   shows "\<exists>f n::nat. f ` A = {i. i < n} \<and> inj_on f A"
   130 proof -
   131   from finite_imp_nat_seg_image_inj_on[OF `finite A`]
   132   obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
   133     by (auto simp:bij_betw_def)
   134   let ?f = "the_inv_into {i. i<n} f"
   135   have "inj_on ?f A & ?f ` A = {i. i<n}"
   136     by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
   137   thus ?thesis by blast
   138 qed
   139 
   140 lemma finite_Collect_less_nat [iff]:
   141   "finite {n::nat. n < k}"
   142   by (fastforce simp: finite_conv_nat_seg_image)
   143 
   144 lemma finite_Collect_le_nat [iff]:
   145   "finite {n::nat. n \<le> k}"
   146   by (simp add: le_eq_less_or_eq Collect_disj_eq)
   147 
   148 
   149 subsubsection {* Finiteness and common set operations *}
   150 
   151 lemma rev_finite_subset:
   152   "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A"
   153 proof (induct arbitrary: A rule: finite_induct)
   154   case empty
   155   then show ?case by simp
   156 next
   157   case (insert x F A)
   158   have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})" by fact+
   159   show "finite A"
   160   proof cases
   161     assume x: "x \<in> A"
   162     with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
   163     with r have "finite (A - {x})" .
   164     hence "finite (insert x (A - {x}))" ..
   165     also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
   166     finally show ?thesis .
   167   next
   168     show "A \<subseteq> F ==> ?thesis" by fact
   169     assume "x \<notin> A"
   170     with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
   171   qed
   172 qed
   173 
   174 lemma finite_subset:
   175   "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
   176   by (rule rev_finite_subset)
   177 
   178 lemma finite_UnI:
   179   assumes "finite F" and "finite G"
   180   shows "finite (F \<union> G)"
   181   using assms by induct simp_all
   182 
   183 lemma finite_Un [iff]:
   184   "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G"
   185   by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"])
   186 
   187 lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A"
   188 proof -
   189   have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
   190   then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
   191   then show ?thesis by simp
   192 qed
   193 
   194 lemma finite_Int [simp, intro]:
   195   "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)"
   196   by (blast intro: finite_subset)
   197 
   198 lemma finite_Collect_conjI [simp, intro]:
   199   "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
   200   by (simp add: Collect_conj_eq)
   201 
   202 lemma finite_Collect_disjI [simp]:
   203   "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
   204   by (simp add: Collect_disj_eq)
   205 
   206 lemma finite_Diff [simp, intro]:
   207   "finite A \<Longrightarrow> finite (A - B)"
   208   by (rule finite_subset, rule Diff_subset)
   209 
   210 lemma finite_Diff2 [simp]:
   211   assumes "finite B"
   212   shows "finite (A - B) \<longleftrightarrow> finite A"
   213 proof -
   214   have "finite A \<longleftrightarrow> finite((A - B) \<union> (A \<inter> B))" by (simp add: Un_Diff_Int)
   215   also have "\<dots> \<longleftrightarrow> finite (A - B)" using `finite B` by simp
   216   finally show ?thesis ..
   217 qed
   218 
   219 lemma finite_Diff_insert [iff]:
   220   "finite (A - insert a B) \<longleftrightarrow> finite (A - B)"
   221 proof -
   222   have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
   223   moreover have "A - insert a B = A - B - {a}" by auto
   224   ultimately show ?thesis by simp
   225 qed
   226 
   227 lemma finite_compl[simp]:
   228   "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)"
   229   by (simp add: Compl_eq_Diff_UNIV)
   230 
   231 lemma finite_Collect_not[simp]:
   232   "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
   233   by (simp add: Collect_neg_eq)
   234 
   235 lemma finite_Union [simp, intro]:
   236   "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite(\<Union>A)"
   237   by (induct rule: finite_induct) simp_all
   238 
   239 lemma finite_UN_I [intro]:
   240   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)"
   241   by (induct rule: finite_induct) simp_all
   242 
   243 lemma finite_UN [simp]:
   244   "finite A \<Longrightarrow> finite (UNION A B) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))"
   245   by (blast intro: finite_subset)
   246 
   247 lemma finite_Inter [intro]:
   248   "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)"
   249   by (blast intro: Inter_lower finite_subset)
   250 
   251 lemma finite_INT [intro]:
   252   "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)"
   253   by (blast intro: INT_lower finite_subset)
   254 
   255 lemma finite_imageI [simp, intro]:
   256   "finite F \<Longrightarrow> finite (h ` F)"
   257   by (induct rule: finite_induct) simp_all
   258 
   259 lemma finite_image_set [simp]:
   260   "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
   261   by (simp add: image_Collect [symmetric])
   262 
   263 lemma finite_imageD:
   264   assumes "finite (f ` A)" and "inj_on f A"
   265   shows "finite A"
   266 using assms
   267 proof (induct "f ` A" arbitrary: A)
   268   case empty then show ?case by simp
   269 next
   270   case (insert x B)
   271   then have B_A: "insert x B = f ` A" by simp
   272   then obtain y where "x = f y" and "y \<in> A" by blast
   273   from B_A `x \<notin> B` have "B = f ` A - {x}" by blast
   274   with B_A `x \<notin> B` `x = f y` `inj_on f A` `y \<in> A` have "B = f ` (A - {y})" by (simp add: inj_on_image_set_diff)
   275   moreover from `inj_on f A` have "inj_on f (A - {y})" by (rule inj_on_diff)
   276   ultimately have "finite (A - {y})" by (rule insert.hyps)
   277   then show "finite A" by simp
   278 qed
   279 
   280 lemma finite_surj:
   281   "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B"
   282   by (erule finite_subset) (rule finite_imageI)
   283 
   284 lemma finite_range_imageI:
   285   "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))"
   286   by (drule finite_imageI) (simp add: range_composition)
   287 
   288 lemma finite_subset_image:
   289   assumes "finite B"
   290   shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
   291 using assms
   292 proof induct
   293   case empty then show ?case by simp
   294 next
   295   case insert then show ?case
   296     by (clarsimp simp del: image_insert simp add: image_insert [symmetric])
   297        blast
   298 qed
   299 
   300 lemma finite_vimage_IntI:
   301   "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)"
   302   apply (induct rule: finite_induct)
   303    apply simp_all
   304   apply (subst vimage_insert)
   305   apply (simp add: finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2)
   306   done
   307 
   308 lemma finite_vimageI:
   309   "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)"
   310   using finite_vimage_IntI[of F h UNIV] by auto
   311 
   312 lemma finite_vimageD:
   313   assumes fin: "finite (h -` F)" and surj: "surj h"
   314   shows "finite F"
   315 proof -
   316   have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
   317   also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
   318   finally show "finite F" .
   319 qed
   320 
   321 lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
   322   unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
   323 
   324 lemma finite_Collect_bex [simp]:
   325   assumes "finite A"
   326   shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
   327 proof -
   328   have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
   329   with assms show ?thesis by simp
   330 qed
   331 
   332 lemma finite_Collect_bounded_ex [simp]:
   333   assumes "finite {y. P y}"
   334   shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
   335 proof -
   336   have "{x. EX y. P y & Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})" by auto
   337   with assms show ?thesis by simp
   338 qed
   339 
   340 lemma finite_Plus:
   341   "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)"
   342   by (simp add: Plus_def)
   343 
   344 lemma finite_PlusD: 
   345   fixes A :: "'a set" and B :: "'b set"
   346   assumes fin: "finite (A <+> B)"
   347   shows "finite A" "finite B"
   348 proof -
   349   have "Inl ` A \<subseteq> A <+> B" by auto
   350   then have "finite (Inl ` A :: ('a + 'b) set)" using fin by (rule finite_subset)
   351   then show "finite A" by (rule finite_imageD) (auto intro: inj_onI)
   352 next
   353   have "Inr ` B \<subseteq> A <+> B" by auto
   354   then have "finite (Inr ` B :: ('a + 'b) set)" using fin by (rule finite_subset)
   355   then show "finite B" by (rule finite_imageD) (auto intro: inj_onI)
   356 qed
   357 
   358 lemma finite_Plus_iff [simp]:
   359   "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
   360   by (auto intro: finite_PlusD finite_Plus)
   361 
   362 lemma finite_Plus_UNIV_iff [simp]:
   363   "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
   364   by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff)
   365 
   366 lemma finite_SigmaI [simp, intro]:
   367   "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) ==> finite (SIGMA a:A. B a)"
   368   by (unfold Sigma_def) blast
   369 
   370 lemma finite_SigmaI2:
   371   assumes "finite {x\<in>A. B x \<noteq> {}}"
   372   and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)"
   373   shows "finite (Sigma A B)"
   374 proof -
   375   from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)" by auto
   376   also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B" by auto
   377   finally show ?thesis .
   378 qed
   379 
   380 lemma finite_cartesian_product:
   381   "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)"
   382   by (rule finite_SigmaI)
   383 
   384 lemma finite_Prod_UNIV:
   385   "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
   386   by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product)
   387 
   388 lemma finite_cartesian_productD1:
   389   assumes "finite (A \<times> B)" and "B \<noteq> {}"
   390   shows "finite A"
   391 proof -
   392   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
   393     by (auto simp add: finite_conv_nat_seg_image)
   394   then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}" by simp
   395   with `B \<noteq> {}` have "A = (fst \<circ> f) ` {i::nat. i < n}"
   396     by (simp add: image_compose)
   397   then have "\<exists>n f. A = f ` {i::nat. i < n}" by blast
   398   then show ?thesis
   399     by (auto simp add: finite_conv_nat_seg_image)
   400 qed
   401 
   402 lemma finite_cartesian_productD2:
   403   assumes "finite (A \<times> B)" and "A \<noteq> {}"
   404   shows "finite B"
   405 proof -
   406   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
   407     by (auto simp add: finite_conv_nat_seg_image)
   408   then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}" by simp
   409   with `A \<noteq> {}` have "B = (snd \<circ> f) ` {i::nat. i < n}"
   410     by (simp add: image_compose)
   411   then have "\<exists>n f. B = f ` {i::nat. i < n}" by blast
   412   then show ?thesis
   413     by (auto simp add: finite_conv_nat_seg_image)
   414 qed
   415 
   416 lemma finite_prod: 
   417   "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
   418 by(auto simp add: UNIV_Times_UNIV[symmetric] simp del: UNIV_Times_UNIV 
   419    dest: finite_cartesian_productD1 finite_cartesian_productD2)
   420 
   421 lemma finite_Pow_iff [iff]:
   422   "finite (Pow A) \<longleftrightarrow> finite A"
   423 proof
   424   assume "finite (Pow A)"
   425   then have "finite ((%x. {x}) ` A)" by (blast intro: finite_subset)
   426   then show "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
   427 next
   428   assume "finite A"
   429   then show "finite (Pow A)"
   430     by induct (simp_all add: Pow_insert)
   431 qed
   432 
   433 corollary finite_Collect_subsets [simp, intro]:
   434   "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
   435   by (simp add: Pow_def [symmetric])
   436 
   437 lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)"
   438 by(simp only: finite_Pow_iff Pow_UNIV[symmetric])
   439 
   440 lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
   441   by (blast intro: finite_subset [OF subset_Pow_Union])
   442 
   443 lemma finite_set_of_finite_funs: assumes "finite A" "finite B"
   444 shows "finite{f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
   445 proof-
   446   let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
   447   have "?F ` ?S \<subseteq> Pow(A \<times> B)" by auto
   448   from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" by simp
   449   have 2: "inj_on ?F ?S"
   450     by(fastforce simp add: inj_on_def set_eq_iff fun_eq_iff)
   451   show ?thesis by(rule finite_imageD[OF 1 2])
   452 qed
   453 
   454 subsubsection {* Further induction rules on finite sets *}
   455 
   456 lemma finite_ne_induct [case_names singleton insert, consumes 2]:
   457   assumes "finite F" and "F \<noteq> {}"
   458   assumes "\<And>x. P {x}"
   459     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
   460   shows "P F"
   461 using assms
   462 proof induct
   463   case empty then show ?case by simp
   464 next
   465   case (insert x F) then show ?case by cases auto
   466 qed
   467 
   468 lemma finite_subset_induct [consumes 2, case_names empty insert]:
   469   assumes "finite F" and "F \<subseteq> A"
   470   assumes empty: "P {}"
   471     and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)"
   472   shows "P F"
   473 using `finite F` `F \<subseteq> A`
   474 proof induct
   475   show "P {}" by fact
   476 next
   477   fix x F
   478   assume "finite F" and "x \<notin> F" and
   479     P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
   480   show "P (insert x F)"
   481   proof (rule insert)
   482     from i show "x \<in> A" by blast
   483     from i have "F \<subseteq> A" by blast
   484     with P show "P F" .
   485     show "finite F" by fact
   486     show "x \<notin> F" by fact
   487   qed
   488 qed
   489 
   490 lemma finite_empty_induct:
   491   assumes "finite A"
   492   assumes "P A"
   493     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
   494   shows "P {}"
   495 proof -
   496   have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
   497   proof -
   498     fix B :: "'a set"
   499     assume "B \<subseteq> A"
   500     with `finite A` have "finite B" by (rule rev_finite_subset)
   501     from this `B \<subseteq> A` show "P (A - B)"
   502     proof induct
   503       case empty
   504       from `P A` show ?case by simp
   505     next
   506       case (insert b B)
   507       have "P (A - B - {b})"
   508       proof (rule remove)
   509         from `finite A` show "finite (A - B)" by induct auto
   510         from insert show "b \<in> A - B" by simp
   511         from insert show "P (A - B)" by simp
   512       qed
   513       also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
   514       finally show ?case .
   515     qed
   516   qed
   517   then have "P (A - A)" by blast
   518   then show ?thesis by simp
   519 qed
   520 
   521 
   522 subsection {* Class @{text finite}  *}
   523 
   524 class finite =
   525   assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
   526 begin
   527 
   528 lemma finite [simp]: "finite (A \<Colon> 'a set)"
   529   by (rule subset_UNIV finite_UNIV finite_subset)+
   530 
   531 lemma finite_code [code]: "finite (A \<Colon> 'a set) \<longleftrightarrow> True"
   532   by simp
   533 
   534 end
   535 
   536 instance prod :: (finite, finite) finite
   537   by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
   538 
   539 lemma inj_graph: "inj (%f. {(x, y). y = f x})"
   540   by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
   541 
   542 instance "fun" :: (finite, finite) finite
   543 proof
   544   show "finite (UNIV :: ('a => 'b) set)"
   545   proof (rule finite_imageD)
   546     let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
   547     have "range ?graph \<subseteq> Pow UNIV" by simp
   548     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
   549       by (simp only: finite_Pow_iff finite)
   550     ultimately show "finite (range ?graph)"
   551       by (rule finite_subset)
   552     show "inj ?graph" by (rule inj_graph)
   553   qed
   554 qed
   555 
   556 instance bool :: finite
   557   by default (simp add: UNIV_bool)
   558 
   559 instance set :: (finite) finite
   560   by default (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
   561 
   562 instance unit :: finite
   563   by default (simp add: UNIV_unit)
   564 
   565 instance sum :: (finite, finite) finite
   566   by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
   567 
   568 lemma finite_option_UNIV [simp]:
   569   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   570   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   571 
   572 instance option :: (finite) finite
   573   by default (simp add: UNIV_option_conv)
   574 
   575 
   576 subsection {* A basic fold functional for finite sets *}
   577 
   578 text {* The intended behaviour is
   579 @{text "fold f z {x\<^sub>1, ..., x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)"}
   580 if @{text f} is ``left-commutative'':
   581 *}
   582 
   583 locale comp_fun_commute =
   584   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
   585   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
   586 begin
   587 
   588 lemma fun_left_comm: "f y (f x z) = f x (f y z)"
   589   using comp_fun_commute by (simp add: fun_eq_iff)
   590 
   591 lemma commute_left_comp:
   592   "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
   593   by (simp add: o_assoc comp_fun_commute)
   594 
   595 end
   596 
   597 inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
   598 for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
   599   emptyI [intro]: "fold_graph f z {} z" |
   600   insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
   601       \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
   602 
   603 inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
   604 
   605 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
   606   "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
   607 
   608 text{*A tempting alternative for the definiens is
   609 @{term "if finite A then THE y. fold_graph f z A y else e"}.
   610 It allows the removal of finiteness assumptions from the theorems
   611 @{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
   612 The proofs become ugly. It is not worth the effort. (???) *}
   613 
   614 lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
   615 by (induct rule: finite_induct) auto
   616 
   617 
   618 subsubsection{*From @{const fold_graph} to @{term fold}*}
   619 
   620 context comp_fun_commute
   621 begin
   622 
   623 lemma fold_graph_finite:
   624   assumes "fold_graph f z A y"
   625   shows "finite A"
   626   using assms by induct simp_all
   627 
   628 lemma fold_graph_insertE_aux:
   629   "fold_graph f z A y \<Longrightarrow> a \<in> A \<Longrightarrow> \<exists>y'. y = f a y' \<and> fold_graph f z (A - {a}) y'"
   630 proof (induct set: fold_graph)
   631   case (insertI x A y) show ?case
   632   proof (cases "x = a")
   633     assume "x = a" with insertI show ?case by auto
   634   next
   635     assume "x \<noteq> a"
   636     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
   637       using insertI by auto
   638     have "f x y = f a (f x y')"
   639       unfolding y by (rule fun_left_comm)
   640     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
   641       using y' and `x \<noteq> a` and `x \<notin> A`
   642       by (simp add: insert_Diff_if fold_graph.insertI)
   643     ultimately show ?case by fast
   644   qed
   645 qed simp
   646 
   647 lemma fold_graph_insertE:
   648   assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
   649   obtains y where "v = f x y" and "fold_graph f z A y"
   650 using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
   651 
   652 lemma fold_graph_determ:
   653   "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
   654 proof (induct arbitrary: y set: fold_graph)
   655   case (insertI x A y v)
   656   from `fold_graph f z (insert x A) v` and `x \<notin> A`
   657   obtain y' where "v = f x y'" and "fold_graph f z A y'"
   658     by (rule fold_graph_insertE)
   659   from `fold_graph f z A y'` have "y' = y" by (rule insertI)
   660   with `v = f x y'` show "v = f x y" by simp
   661 qed fast
   662 
   663 lemma fold_equality:
   664   "fold_graph f z A y \<Longrightarrow> fold f z A = y"
   665   by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
   666 
   667 lemma fold_graph_fold:
   668   assumes "finite A"
   669   shows "fold_graph f z A (fold f z A)"
   670 proof -
   671   from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
   672   moreover note fold_graph_determ
   673   ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
   674   then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
   675   with assms show ?thesis by (simp add: fold_def)
   676 qed
   677 
   678 text {* The base case for @{text fold}: *}
   679 
   680 lemma (in -) fold_infinite [simp]:
   681   assumes "\<not> finite A"
   682   shows "fold f z A = z"
   683   using assms by (auto simp add: fold_def)
   684 
   685 lemma (in -) fold_empty [simp]:
   686   "fold f z {} = z"
   687   by (auto simp add: fold_def)
   688 
   689 text{* The various recursion equations for @{const fold}: *}
   690 
   691 lemma fold_insert [simp]:
   692   assumes "finite A" and "x \<notin> A"
   693   shows "fold f z (insert x A) = f x (fold f z A)"
   694 proof (rule fold_equality)
   695   fix z
   696   from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
   697   with `x \<notin> A` have "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
   698   then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
   699 qed
   700 
   701 declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
   702   -- {* No more proofs involve these. *}
   703 
   704 lemma fold_fun_left_comm:
   705   "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
   706 proof (induct rule: finite_induct)
   707   case empty then show ?case by simp
   708 next
   709   case (insert y A) then show ?case
   710     by (simp add: fun_left_comm [of x])
   711 qed
   712 
   713 lemma fold_insert2:
   714   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
   715   by (simp add: fold_fun_left_comm)
   716 
   717 lemma fold_rec:
   718   assumes "finite A" and "x \<in> A"
   719   shows "fold f z A = f x (fold f z (A - {x}))"
   720 proof -
   721   have A: "A = insert x (A - {x})" using `x \<in> A` by blast
   722   then have "fold f z A = fold f z (insert x (A - {x}))" by simp
   723   also have "\<dots> = f x (fold f z (A - {x}))"
   724     by (rule fold_insert) (simp add: `finite A`)+
   725   finally show ?thesis .
   726 qed
   727 
   728 lemma fold_insert_remove:
   729   assumes "finite A"
   730   shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
   731 proof -
   732   from `finite A` have "finite (insert x A)" by auto
   733   moreover have "x \<in> insert x A" by auto
   734   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
   735     by (rule fold_rec)
   736   then show ?thesis by simp
   737 qed
   738 
   739 end
   740 
   741 text{* Other properties of @{const fold}: *}
   742 
   743 lemma fold_image:
   744   assumes "inj_on g A"
   745   shows "fold f z (g ` A) = fold (f \<circ> g) z A"
   746 proof (cases "finite A")
   747   case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
   748 next
   749   case True
   750   have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
   751   proof
   752     fix w
   753     show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
   754     proof
   755       assume ?P then show ?Q using assms
   756       proof (induct "g ` A" w arbitrary: A)
   757         case emptyI then show ?case by (auto intro: fold_graph.emptyI)
   758       next
   759         case (insertI x A r B)
   760         from `inj_on g B` `x \<notin> A` `insert x A = image g B` obtain x' A' where
   761           "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
   762           by (rule inj_img_insertE)
   763         from insertI.prems have "fold_graph (f o g) z A' r"
   764           by (auto intro: insertI.hyps)
   765         with `x' \<notin> A'` have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
   766           by (rule fold_graph.insertI)
   767         then show ?case by simp
   768       qed
   769     next
   770       assume ?Q then show ?P using assms
   771       proof induct
   772         case emptyI thus ?case by (auto intro: fold_graph.emptyI)
   773       next
   774         case (insertI x A r)
   775         from `x \<notin> A` insertI.prems have "g x \<notin> g ` A" by auto
   776         moreover from insertI have "fold_graph f z (g ` A) r" by simp
   777         ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
   778           by (rule fold_graph.insertI)
   779         then show ?case by simp
   780       qed
   781     qed
   782   qed
   783   with True assms show ?thesis by (auto simp add: fold_def)
   784 qed
   785 
   786 lemma fold_cong:
   787   assumes "comp_fun_commute f" "comp_fun_commute g"
   788   assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
   789     and "s = t" and "A = B"
   790   shows "fold f s A = fold g t B"
   791 proof -
   792   have "fold f s A = fold g s A"  
   793   using `finite A` cong proof (induct A)
   794     case empty then show ?case by simp
   795   next
   796     case (insert x A)
   797     interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
   798     interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
   799     from insert show ?case by simp
   800   qed
   801   with assms show ?thesis by simp
   802 qed
   803 
   804 
   805 text {* A simplified version for idempotent functions: *}
   806 
   807 locale comp_fun_idem = comp_fun_commute +
   808   assumes comp_fun_idem: "f x \<circ> f x = f x"
   809 begin
   810 
   811 lemma fun_left_idem: "f x (f x z) = f x z"
   812   using comp_fun_idem by (simp add: fun_eq_iff)
   813 
   814 lemma fold_insert_idem:
   815   assumes fin: "finite A"
   816   shows "fold f z (insert x A)  = f x (fold f z A)"
   817 proof cases
   818   assume "x \<in> A"
   819   then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
   820   then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
   821 next
   822   assume "x \<notin> A" then show ?thesis using assms by simp
   823 qed
   824 
   825 declare fold_insert [simp del] fold_insert_idem [simp]
   826 
   827 lemma fold_insert_idem2:
   828   "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   829   by (simp add: fold_fun_left_comm)
   830 
   831 end
   832 
   833 
   834 subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
   835 
   836 lemma (in comp_fun_commute) comp_comp_fun_commute:
   837   "comp_fun_commute (f \<circ> g)"
   838 proof
   839 qed (simp_all add: comp_fun_commute)
   840 
   841 lemma (in comp_fun_idem) comp_comp_fun_idem:
   842   "comp_fun_idem (f \<circ> g)"
   843   by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
   844     (simp_all add: comp_fun_idem)
   845 
   846 lemma (in comp_fun_commute) comp_fun_commute_funpow:
   847   "comp_fun_commute (\<lambda>x. f x ^^ g x)"
   848 proof
   849   fix y x
   850   show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
   851   proof (cases "x = y")
   852     case True then show ?thesis by simp
   853   next
   854     case False show ?thesis
   855     proof (induct "g x" arbitrary: g)
   856       case 0 then show ?case by simp
   857     next
   858       case (Suc n g)
   859       have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
   860       proof (induct "g y" arbitrary: g)
   861         case 0 then show ?case by simp
   862       next
   863         case (Suc n g)
   864         def h \<equiv> "\<lambda>z. g z - 1"
   865         with Suc have "n = h y" by simp
   866         with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
   867           by auto
   868         from Suc h_def have "g y = Suc (h y)" by simp
   869         then show ?case by (simp add: comp_assoc hyp)
   870           (simp add: o_assoc comp_fun_commute)
   871       qed
   872       def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
   873       with Suc have "n = h x" by simp
   874       with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
   875         by auto
   876       with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" by simp
   877       from Suc h_def have "g x = Suc (h x)" by simp
   878       then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
   879         (simp add: comp_assoc hyp1)
   880     qed
   881   qed
   882 qed
   883 
   884 
   885 subsubsection {* Expressing set operations via @{const fold} *}
   886 
   887 lemma comp_fun_commute_const:
   888   "comp_fun_commute (\<lambda>_. f)"
   889 proof
   890 qed rule
   891 
   892 lemma comp_fun_idem_insert:
   893   "comp_fun_idem insert"
   894 proof
   895 qed auto
   896 
   897 lemma comp_fun_idem_remove:
   898   "comp_fun_idem Set.remove"
   899 proof
   900 qed auto
   901 
   902 lemma (in semilattice_inf) comp_fun_idem_inf:
   903   "comp_fun_idem inf"
   904 proof
   905 qed (auto simp add: inf_left_commute)
   906 
   907 lemma (in semilattice_sup) comp_fun_idem_sup:
   908   "comp_fun_idem sup"
   909 proof
   910 qed (auto simp add: sup_left_commute)
   911 
   912 lemma union_fold_insert:
   913   assumes "finite A"
   914   shows "A \<union> B = fold insert B A"
   915 proof -
   916   interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
   917   from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
   918 qed
   919 
   920 lemma minus_fold_remove:
   921   assumes "finite A"
   922   shows "B - A = fold Set.remove B A"
   923 proof -
   924   interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
   925   from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
   926   then show ?thesis ..
   927 qed
   928 
   929 lemma comp_fun_commute_filter_fold:
   930   "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
   931 proof - 
   932   interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
   933   show ?thesis by default (auto simp: fun_eq_iff)
   934 qed
   935 
   936 lemma Set_filter_fold:
   937   assumes "finite A"
   938   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
   939 using assms
   940 by (induct A) 
   941   (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
   942 
   943 lemma inter_Set_filter:     
   944   assumes "finite B"
   945   shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
   946 using assms 
   947 by (induct B) (auto simp: Set.filter_def)
   948 
   949 lemma image_fold_insert:
   950   assumes "finite A"
   951   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
   952 using assms
   953 proof -
   954   interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
   955   show ?thesis using assms by (induct A) auto
   956 qed
   957 
   958 lemma Ball_fold:
   959   assumes "finite A"
   960   shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
   961 using assms
   962 proof -
   963   interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
   964   show ?thesis using assms by (induct A) auto
   965 qed
   966 
   967 lemma Bex_fold:
   968   assumes "finite A"
   969   shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
   970 using assms
   971 proof -
   972   interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
   973   show ?thesis using assms by (induct A) auto
   974 qed
   975 
   976 lemma comp_fun_commute_Pow_fold: 
   977   "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
   978   by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
   979 
   980 lemma Pow_fold:
   981   assumes "finite A"
   982   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
   983 using assms
   984 proof -
   985   interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
   986   show ?thesis using assms by (induct A) (auto simp: Pow_insert)
   987 qed
   988 
   989 lemma fold_union_pair:
   990   assumes "finite B"
   991   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
   992 proof -
   993   interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
   994   show ?thesis using assms  by (induct B arbitrary: A) simp_all
   995 qed
   996 
   997 lemma comp_fun_commute_product_fold: 
   998   assumes "finite B"
   999   shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
  1000 by default (auto simp: fold_union_pair[symmetric] assms)
  1001 
  1002 lemma product_fold:
  1003   assumes "finite A"
  1004   assumes "finite B"
  1005   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
  1006 using assms unfolding Sigma_def 
  1007 by (induct A) 
  1008   (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
  1009 
  1010 
  1011 context complete_lattice
  1012 begin
  1013 
  1014 lemma inf_Inf_fold_inf:
  1015   assumes "finite A"
  1016   shows "inf (Inf A) B = fold inf B A"
  1017 proof -
  1018   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1019   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1020     (simp_all add: inf_commute fun_eq_iff)
  1021 qed
  1022 
  1023 lemma sup_Sup_fold_sup:
  1024   assumes "finite A"
  1025   shows "sup (Sup A) B = fold sup B A"
  1026 proof -
  1027   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1028   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1029     (simp_all add: sup_commute fun_eq_iff)
  1030 qed
  1031 
  1032 lemma Inf_fold_inf:
  1033   assumes "finite A"
  1034   shows "Inf A = fold inf top A"
  1035   using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
  1036 
  1037 lemma Sup_fold_sup:
  1038   assumes "finite A"
  1039   shows "Sup A = fold sup bot A"
  1040   using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
  1041 
  1042 lemma inf_INF_fold_inf:
  1043   assumes "finite A"
  1044   shows "inf B (INFI A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
  1045 proof (rule sym)
  1046   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1047   interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
  1048   from `finite A` show "?fold = ?inf"
  1049     by (induct A arbitrary: B)
  1050       (simp_all add: INF_def inf_left_commute)
  1051 qed
  1052 
  1053 lemma sup_SUP_fold_sup:
  1054   assumes "finite A"
  1055   shows "sup B (SUPR A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
  1056 proof (rule sym)
  1057   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1058   interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
  1059   from `finite A` show "?fold = ?sup"
  1060     by (induct A arbitrary: B)
  1061       (simp_all add: SUP_def sup_left_commute)
  1062 qed
  1063 
  1064 lemma INF_fold_inf:
  1065   assumes "finite A"
  1066   shows "INFI A f = fold (inf \<circ> f) top A"
  1067   using assms inf_INF_fold_inf [of A top] by simp
  1068 
  1069 lemma SUP_fold_sup:
  1070   assumes "finite A"
  1071   shows "SUPR A f = fold (sup \<circ> f) bot A"
  1072   using assms sup_SUP_fold_sup [of A bot] by simp
  1073 
  1074 end
  1075 
  1076 
  1077 subsection {* Locales as mini-packages for fold operations *}
  1078 
  1079 subsubsection {* The natural case *}
  1080 
  1081 locale folding =
  1082   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1083   fixes z :: "'b"
  1084   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
  1085 begin
  1086 
  1087 definition F :: "'a set \<Rightarrow> 'b"
  1088 where
  1089   eq_fold: "F A = fold f z A"
  1090 
  1091 lemma empty [simp]:
  1092   "F {} = z"
  1093   by (simp add: eq_fold)
  1094 
  1095 lemma infinite [simp]:
  1096   "\<not> finite A \<Longrightarrow> F A = z"
  1097   by (simp add: eq_fold)
  1098  
  1099 lemma insert [simp]:
  1100   assumes "finite A" and "x \<notin> A"
  1101   shows "F (insert x A) = f x (F A)"
  1102 proof -
  1103   interpret comp_fun_commute f
  1104     by default (insert comp_fun_commute, simp add: fun_eq_iff)
  1105   from fold_insert assms
  1106   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1107   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1108 qed
  1109  
  1110 lemma remove:
  1111   assumes "finite A" and "x \<in> A"
  1112   shows "F A = f x (F (A - {x}))"
  1113 proof -
  1114   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
  1115     by (auto dest: mk_disjoint_insert)
  1116   moreover from `finite A` A have "finite B" by simp
  1117   ultimately show ?thesis by simp
  1118 qed
  1119 
  1120 lemma insert_remove:
  1121   assumes "finite A"
  1122   shows "F (insert x A) = f x (F (A - {x}))"
  1123   using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
  1124 
  1125 end
  1126 
  1127 
  1128 subsubsection {* With idempotency *}
  1129 
  1130 locale folding_idem = folding +
  1131   assumes comp_fun_idem: "f x \<circ> f x = f x"
  1132 begin
  1133 
  1134 declare insert [simp del]
  1135 
  1136 lemma insert_idem [simp]:
  1137   assumes "finite A"
  1138   shows "F (insert x A) = f x (F A)"
  1139 proof -
  1140   interpret comp_fun_idem f
  1141     by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
  1142   from fold_insert_idem assms
  1143   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1144   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1145 qed
  1146 
  1147 end
  1148 
  1149 
  1150 subsection {* Finite cardinality *}
  1151 
  1152 text {*
  1153   The traditional definition
  1154   @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
  1155   is ugly to work with.
  1156   But now that we have @{const fold} things are easy:
  1157 *}
  1158 
  1159 definition card :: "'a set \<Rightarrow> nat" where
  1160   "card = folding.F (\<lambda>_. Suc) 0"
  1161 
  1162 interpretation card!: folding "\<lambda>_. Suc" 0
  1163 where
  1164   "folding.F (\<lambda>_. Suc) 0 = card"
  1165 proof -
  1166   show "folding (\<lambda>_. Suc)" by default rule
  1167   then interpret card!: folding "\<lambda>_. Suc" 0 .
  1168   from card_def show "folding.F (\<lambda>_. Suc) 0 = card" by rule
  1169 qed
  1170 
  1171 lemma card_infinite:
  1172   "\<not> finite A \<Longrightarrow> card A = 0"
  1173   by (fact card.infinite)
  1174 
  1175 lemma card_empty:
  1176   "card {} = 0"
  1177   by (fact card.empty)
  1178 
  1179 lemma card_insert_disjoint:
  1180   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
  1181   by (fact card.insert)
  1182 
  1183 lemma card_insert_if:
  1184   "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
  1185   by auto (simp add: card.insert_remove card.remove)
  1186 
  1187 lemma card_ge_0_finite:
  1188   "card A > 0 \<Longrightarrow> finite A"
  1189   by (rule ccontr) simp
  1190 
  1191 lemma card_0_eq [simp]:
  1192   "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
  1193   by (auto dest: mk_disjoint_insert)
  1194 
  1195 lemma finite_UNIV_card_ge_0:
  1196   "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
  1197   by (rule ccontr) simp
  1198 
  1199 lemma card_eq_0_iff:
  1200   "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
  1201   by auto
  1202 
  1203 lemma card_gt_0_iff:
  1204   "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
  1205   by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
  1206 
  1207 lemma card_Suc_Diff1:
  1208   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
  1209 apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
  1210 apply(simp del:insert_Diff_single)
  1211 done
  1212 
  1213 lemma card_Diff_singleton:
  1214   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
  1215   by (simp add: card_Suc_Diff1 [symmetric])
  1216 
  1217 lemma card_Diff_singleton_if:
  1218   "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
  1219   by (simp add: card_Diff_singleton)
  1220 
  1221 lemma card_Diff_insert[simp]:
  1222   assumes "finite A" and "a \<in> A" and "a \<notin> B"
  1223   shows "card (A - insert a B) = card (A - B) - 1"
  1224 proof -
  1225   have "A - insert a B = (A - B) - {a}" using assms by blast
  1226   then show ?thesis using assms by(simp add: card_Diff_singleton)
  1227 qed
  1228 
  1229 lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
  1230   by (fact card.insert_remove)
  1231 
  1232 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  1233 by (simp add: card_insert_if)
  1234 
  1235 lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
  1236 by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
  1237 
  1238 lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
  1239 using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
  1240 
  1241 lemma card_mono:
  1242   assumes "finite B" and "A \<subseteq> B"
  1243   shows "card A \<le> card B"
  1244 proof -
  1245   from assms have "finite A" by (auto intro: finite_subset)
  1246   then show ?thesis using assms proof (induct A arbitrary: B)
  1247     case empty then show ?case by simp
  1248   next
  1249     case (insert x A)
  1250     then have "x \<in> B" by simp
  1251     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
  1252     with insert.hyps have "card A \<le> card (B - {x})" by auto
  1253     with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
  1254   qed
  1255 qed
  1256 
  1257 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
  1258 apply (induct rule: finite_induct)
  1259 apply simp
  1260 apply clarify
  1261 apply (subgoal_tac "finite A & A - {x} <= F")
  1262  prefer 2 apply (blast intro: finite_subset, atomize)
  1263 apply (drule_tac x = "A - {x}" in spec)
  1264 apply (simp add: card_Diff_singleton_if split add: split_if_asm)
  1265 apply (case_tac "card A", auto)
  1266 done
  1267 
  1268 lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
  1269 apply (simp add: psubset_eq linorder_not_le [symmetric])
  1270 apply (blast dest: card_seteq)
  1271 done
  1272 
  1273 lemma card_Un_Int:
  1274   assumes "finite A" and "finite B"
  1275   shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
  1276 using assms proof (induct A)
  1277   case empty then show ?case by simp
  1278 next
  1279  case (insert x A) then show ?case
  1280     by (auto simp add: insert_absorb Int_insert_left)
  1281 qed
  1282 
  1283 lemma card_Un_disjoint:
  1284   assumes "finite A" and "finite B"
  1285   assumes "A \<inter> B = {}"
  1286   shows "card (A \<union> B) = card A + card B"
  1287 using assms card_Un_Int [of A B] by simp
  1288 
  1289 lemma card_Diff_subset:
  1290   assumes "finite B" and "B \<subseteq> A"
  1291   shows "card (A - B) = card A - card B"
  1292 proof (cases "finite A")
  1293   case False with assms show ?thesis by simp
  1294 next
  1295   case True with assms show ?thesis by (induct B arbitrary: A) simp_all
  1296 qed
  1297 
  1298 lemma card_Diff_subset_Int:
  1299   assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
  1300 proof -
  1301   have "A - B = A - A \<inter> B" by auto
  1302   thus ?thesis
  1303     by (simp add: card_Diff_subset AB) 
  1304 qed
  1305 
  1306 lemma diff_card_le_card_Diff:
  1307 assumes "finite B" shows "card A - card B \<le> card(A - B)"
  1308 proof-
  1309   have "card A - card B \<le> card A - card (A \<inter> B)"
  1310     using card_mono[OF assms Int_lower2, of A] by arith
  1311   also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
  1312   finally show ?thesis .
  1313 qed
  1314 
  1315 lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
  1316 apply (rule Suc_less_SucD)
  1317 apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
  1318 done
  1319 
  1320 lemma card_Diff2_less:
  1321   "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
  1322 apply (case_tac "x = y")
  1323  apply (simp add: card_Diff1_less del:card_Diff_insert)
  1324 apply (rule less_trans)
  1325  prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
  1326 done
  1327 
  1328 lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
  1329 apply (case_tac "x : A")
  1330  apply (simp_all add: card_Diff1_less less_imp_le)
  1331 done
  1332 
  1333 lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
  1334 by (erule psubsetI, blast)
  1335 
  1336 lemma insert_partition:
  1337   "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
  1338   \<Longrightarrow> x \<inter> \<Union> F = {}"
  1339 by auto
  1340 
  1341 lemma finite_psubset_induct[consumes 1, case_names psubset]:
  1342   assumes fin: "finite A" 
  1343   and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
  1344   shows "P A"
  1345 using fin
  1346 proof (induct A taking: card rule: measure_induct_rule)
  1347   case (less A)
  1348   have fin: "finite A" by fact
  1349   have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
  1350   { fix B 
  1351     assume asm: "B \<subset> A"
  1352     from asm have "card B < card A" using psubset_card_mono fin by blast
  1353     moreover
  1354     from asm have "B \<subseteq> A" by auto
  1355     then have "finite B" using fin finite_subset by blast
  1356     ultimately 
  1357     have "P B" using ih by simp
  1358   }
  1359   with fin show "P A" using major by blast
  1360 qed
  1361 
  1362 text{* main cardinality theorem *}
  1363 lemma card_partition [rule_format]:
  1364   "finite C ==>
  1365      finite (\<Union> C) -->
  1366      (\<forall>c\<in>C. card c = k) -->
  1367      (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
  1368      k * card(C) = card (\<Union> C)"
  1369 apply (erule finite_induct, simp)
  1370 apply (simp add: card_Un_disjoint insert_partition 
  1371        finite_subset [of _ "\<Union> (insert x F)"])
  1372 done
  1373 
  1374 lemma card_eq_UNIV_imp_eq_UNIV:
  1375   assumes fin: "finite (UNIV :: 'a set)"
  1376   and card: "card A = card (UNIV :: 'a set)"
  1377   shows "A = (UNIV :: 'a set)"
  1378 proof
  1379   show "A \<subseteq> UNIV" by simp
  1380   show "UNIV \<subseteq> A"
  1381   proof
  1382     fix x
  1383     show "x \<in> A"
  1384     proof (rule ccontr)
  1385       assume "x \<notin> A"
  1386       then have "A \<subset> UNIV" by auto
  1387       with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
  1388       with card show False by simp
  1389     qed
  1390   qed
  1391 qed
  1392 
  1393 text{*The form of a finite set of given cardinality*}
  1394 
  1395 lemma card_eq_SucD:
  1396 assumes "card A = Suc k"
  1397 shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
  1398 proof -
  1399   have fin: "finite A" using assms by (auto intro: ccontr)
  1400   moreover have "card A \<noteq> 0" using assms by auto
  1401   ultimately obtain b where b: "b \<in> A" by auto
  1402   show ?thesis
  1403   proof (intro exI conjI)
  1404     show "A = insert b (A-{b})" using b by blast
  1405     show "b \<notin> A - {b}" by blast
  1406     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
  1407       using assms b fin by(fastforce dest:mk_disjoint_insert)+
  1408   qed
  1409 qed
  1410 
  1411 lemma card_Suc_eq:
  1412   "(card A = Suc k) =
  1413    (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
  1414 apply(rule iffI)
  1415  apply(erule card_eq_SucD)
  1416 apply(auto)
  1417 apply(subst card.insert)
  1418  apply(auto intro:ccontr)
  1419 done
  1420 
  1421 lemma card_le_Suc_iff: "finite A \<Longrightarrow>
  1422   Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
  1423 by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
  1424   dest: subset_singletonD split: nat.splits if_splits)
  1425 
  1426 lemma finite_fun_UNIVD2:
  1427   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
  1428   shows "finite (UNIV :: 'b set)"
  1429 proof -
  1430   from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
  1431     by (rule finite_imageI)
  1432   moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
  1433     by (rule UNIV_eq_I) auto
  1434   ultimately show "finite (UNIV :: 'b set)" by simp
  1435 qed
  1436 
  1437 lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
  1438   unfolding UNIV_unit by simp
  1439 
  1440 lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
  1441   unfolding UNIV_bool by simp
  1442 
  1443 
  1444 subsubsection {* Cardinality of image *}
  1445 
  1446 lemma card_image_le: "finite A ==> card (f ` A) <= card A"
  1447 apply (induct rule: finite_induct)
  1448  apply simp
  1449 apply (simp add: le_SucI card_insert_if)
  1450 done
  1451 
  1452 lemma card_image:
  1453   assumes "inj_on f A"
  1454   shows "card (f ` A) = card A"
  1455 proof (cases "finite A")
  1456   case True then show ?thesis using assms by (induct A) simp_all
  1457 next
  1458   case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
  1459   with False show ?thesis by simp
  1460 qed
  1461 
  1462 lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
  1463 by(auto simp: card_image bij_betw_def)
  1464 
  1465 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
  1466 by (simp add: card_seteq card_image)
  1467 
  1468 lemma eq_card_imp_inj_on:
  1469   "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
  1470 apply (induct rule:finite_induct)
  1471 apply simp
  1472 apply(frule card_image_le[where f = f])
  1473 apply(simp add:card_insert_if split:if_splits)
  1474 done
  1475 
  1476 lemma inj_on_iff_eq_card:
  1477   "finite A ==> inj_on f A = (card(f ` A) = card A)"
  1478 by(blast intro: card_image eq_card_imp_inj_on)
  1479 
  1480 
  1481 lemma card_inj_on_le:
  1482   "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
  1483 apply (subgoal_tac "finite A") 
  1484  apply (force intro: card_mono simp add: card_image [symmetric])
  1485 apply (blast intro: finite_imageD dest: finite_subset) 
  1486 done
  1487 
  1488 lemma card_bij_eq:
  1489   "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
  1490      finite A; finite B |] ==> card A = card B"
  1491 by (auto intro: le_antisym card_inj_on_le)
  1492 
  1493 lemma bij_betw_finite:
  1494   assumes "bij_betw f A B"
  1495   shows "finite A \<longleftrightarrow> finite B"
  1496 using assms unfolding bij_betw_def
  1497 using finite_imageD[of f A] by auto
  1498 
  1499 
  1500 subsubsection {* Pigeonhole Principles *}
  1501 
  1502 lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
  1503 by (auto dest: card_image less_irrefl_nat)
  1504 
  1505 lemma pigeonhole_infinite:
  1506 assumes  "~ finite A" and "finite(f`A)"
  1507 shows "EX a0:A. ~finite{a:A. f a = f a0}"
  1508 proof -
  1509   have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
  1510   proof(induct "f`A" arbitrary: A rule: finite_induct)
  1511     case empty thus ?case by simp
  1512   next
  1513     case (insert b F)
  1514     show ?case
  1515     proof cases
  1516       assume "finite{a:A. f a = b}"
  1517       hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
  1518       also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
  1519       finally have "~ finite({a:A. f a \<noteq> b})" .
  1520       from insert(3)[OF _ this]
  1521       show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
  1522     next
  1523       assume 1: "~finite{a:A. f a = b}"
  1524       hence "{a \<in> A. f a = b} \<noteq> {}" by force
  1525       thus ?thesis using 1 by blast
  1526     qed
  1527   qed
  1528   from this[OF assms(2,1)] show ?thesis .
  1529 qed
  1530 
  1531 lemma pigeonhole_infinite_rel:
  1532 assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
  1533 shows "EX b:B. ~finite{a:A. R a b}"
  1534 proof -
  1535    let ?F = "%a. {b:B. R a b}"
  1536    from finite_Pow_iff[THEN iffD2, OF `finite B`]
  1537    have "finite(?F ` A)" by(blast intro: rev_finite_subset)
  1538    from pigeonhole_infinite[where f = ?F, OF assms(1) this]
  1539    obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
  1540    obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
  1541    { assume "finite{a:A. R a b0}"
  1542      then have "finite {a\<in>A. ?F a = ?F a0}"
  1543        using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
  1544    }
  1545    with 1 `b0 : B` show ?thesis by blast
  1546 qed
  1547 
  1548 
  1549 subsubsection {* Cardinality of sums *}
  1550 
  1551 lemma card_Plus:
  1552   assumes "finite A" and "finite B"
  1553   shows "card (A <+> B) = card A + card B"
  1554 proof -
  1555   have "Inl`A \<inter> Inr`B = {}" by fast
  1556   with assms show ?thesis
  1557     unfolding Plus_def
  1558     by (simp add: card_Un_disjoint card_image)
  1559 qed
  1560 
  1561 lemma card_Plus_conv_if:
  1562   "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
  1563   by (auto simp add: card_Plus)
  1564 
  1565 
  1566 subsubsection {* Cardinality of the Powerset *}
  1567 
  1568 lemma card_Pow: "finite A ==> card (Pow A) = 2 ^ card A"
  1569 apply (induct rule: finite_induct)
  1570  apply (simp_all add: Pow_insert)
  1571 apply (subst card_Un_disjoint, blast)
  1572   apply (blast, blast)
  1573 apply (subgoal_tac "inj_on (insert x) (Pow F)")
  1574  apply (subst mult_2)
  1575  apply (simp add: card_image Pow_insert)
  1576 apply (unfold inj_on_def)
  1577 apply (blast elim!: equalityE)
  1578 done
  1579 
  1580 text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
  1581 
  1582 lemma dvd_partition:
  1583   "finite (Union C) ==>
  1584     ALL c : C. k dvd card c ==>
  1585     (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
  1586   k dvd card (Union C)"
  1587 apply (frule finite_UnionD)
  1588 apply (rotate_tac -1)
  1589 apply (induct rule: finite_induct)
  1590 apply simp_all
  1591 apply clarify
  1592 apply (subst card_Un_disjoint)
  1593    apply (auto simp add: disjoint_eq_subset_Compl)
  1594 done
  1595 
  1596 
  1597 subsubsection {* Relating injectivity and surjectivity *}
  1598 
  1599 lemma finite_surj_inj: "finite A \<Longrightarrow> A \<subseteq> f ` A \<Longrightarrow> inj_on f A"
  1600 apply(rule eq_card_imp_inj_on, assumption)
  1601 apply(frule finite_imageI)
  1602 apply(drule (1) card_seteq)
  1603  apply(erule card_image_le)
  1604 apply simp
  1605 done
  1606 
  1607 lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
  1608 shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
  1609 by (blast intro: finite_surj_inj subset_UNIV)
  1610 
  1611 lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
  1612 shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
  1613 by(fastforce simp:surj_def dest!: endo_inj_surj)
  1614 
  1615 corollary infinite_UNIV_nat [iff]:
  1616   "\<not> finite (UNIV :: nat set)"
  1617 proof
  1618   assume "finite (UNIV :: nat set)"
  1619   with finite_UNIV_inj_surj [of Suc]
  1620   show False by simp (blast dest: Suc_neq_Zero surjD)
  1621 qed
  1622 
  1623 lemma infinite_UNIV_char_0:
  1624   "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
  1625 proof
  1626   assume "finite (UNIV :: 'a set)"
  1627   with subset_UNIV have "finite (range of_nat :: 'a set)"
  1628     by (rule finite_subset)
  1629   moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
  1630     by (simp add: inj_on_def)
  1631   ultimately have "finite (UNIV :: nat set)"
  1632     by (rule finite_imageD)
  1633   then show False
  1634     by simp
  1635 qed
  1636 
  1637 hide_const (open) Finite_Set.fold
  1638 
  1639 end
  1640