src/HOL/Finite_Set.thy
author wenzelm
Tue Sep 03 01:12:40 2013 +0200 (2013-09-03)
changeset 53374 a14d2a854c02
parent 53015 a1119cf551e8
child 53820 9c7e97d67b45
permissions -rw-r--r--
tuned proofs -- clarified flow of facts wrt. calculation;
     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 
   444 subsubsection {* Further induction rules on finite sets *}
   445 
   446 lemma finite_ne_induct [case_names singleton insert, consumes 2]:
   447   assumes "finite F" and "F \<noteq> {}"
   448   assumes "\<And>x. P {x}"
   449     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
   450   shows "P F"
   451 using assms
   452 proof induct
   453   case empty then show ?case by simp
   454 next
   455   case (insert x F) then show ?case by cases auto
   456 qed
   457 
   458 lemma finite_subset_induct [consumes 2, case_names empty insert]:
   459   assumes "finite F" and "F \<subseteq> A"
   460   assumes empty: "P {}"
   461     and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)"
   462   shows "P F"
   463 using `finite F` `F \<subseteq> A`
   464 proof induct
   465   show "P {}" by fact
   466 next
   467   fix x F
   468   assume "finite F" and "x \<notin> F" and
   469     P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
   470   show "P (insert x F)"
   471   proof (rule insert)
   472     from i show "x \<in> A" by blast
   473     from i have "F \<subseteq> A" by blast
   474     with P show "P F" .
   475     show "finite F" by fact
   476     show "x \<notin> F" by fact
   477   qed
   478 qed
   479 
   480 lemma finite_empty_induct:
   481   assumes "finite A"
   482   assumes "P A"
   483     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
   484   shows "P {}"
   485 proof -
   486   have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
   487   proof -
   488     fix B :: "'a set"
   489     assume "B \<subseteq> A"
   490     with `finite A` have "finite B" by (rule rev_finite_subset)
   491     from this `B \<subseteq> A` show "P (A - B)"
   492     proof induct
   493       case empty
   494       from `P A` show ?case by simp
   495     next
   496       case (insert b B)
   497       have "P (A - B - {b})"
   498       proof (rule remove)
   499         from `finite A` show "finite (A - B)" by induct auto
   500         from insert show "b \<in> A - B" by simp
   501         from insert show "P (A - B)" by simp
   502       qed
   503       also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
   504       finally show ?case .
   505     qed
   506   qed
   507   then have "P (A - A)" by blast
   508   then show ?thesis by simp
   509 qed
   510 
   511 
   512 subsection {* Class @{text finite}  *}
   513 
   514 class finite =
   515   assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
   516 begin
   517 
   518 lemma finite [simp]: "finite (A \<Colon> 'a set)"
   519   by (rule subset_UNIV finite_UNIV finite_subset)+
   520 
   521 lemma finite_code [code]: "finite (A \<Colon> 'a set) \<longleftrightarrow> True"
   522   by simp
   523 
   524 end
   525 
   526 instance prod :: (finite, finite) finite
   527   by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
   528 
   529 lemma inj_graph: "inj (%f. {(x, y). y = f x})"
   530   by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
   531 
   532 instance "fun" :: (finite, finite) finite
   533 proof
   534   show "finite (UNIV :: ('a => 'b) set)"
   535   proof (rule finite_imageD)
   536     let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
   537     have "range ?graph \<subseteq> Pow UNIV" by simp
   538     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
   539       by (simp only: finite_Pow_iff finite)
   540     ultimately show "finite (range ?graph)"
   541       by (rule finite_subset)
   542     show "inj ?graph" by (rule inj_graph)
   543   qed
   544 qed
   545 
   546 instance bool :: finite
   547   by default (simp add: UNIV_bool)
   548 
   549 instance set :: (finite) finite
   550   by default (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
   551 
   552 instance unit :: finite
   553   by default (simp add: UNIV_unit)
   554 
   555 instance sum :: (finite, finite) finite
   556   by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
   557 
   558 lemma finite_option_UNIV [simp]:
   559   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   560   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   561 
   562 instance option :: (finite) finite
   563   by default (simp add: UNIV_option_conv)
   564 
   565 
   566 subsection {* A basic fold functional for finite sets *}
   567 
   568 text {* The intended behaviour is
   569 @{text "fold f z {x\<^sub>1, ..., x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)"}
   570 if @{text f} is ``left-commutative'':
   571 *}
   572 
   573 locale comp_fun_commute =
   574   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
   575   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
   576 begin
   577 
   578 lemma fun_left_comm: "f y (f x z) = f x (f y z)"
   579   using comp_fun_commute by (simp add: fun_eq_iff)
   580 
   581 lemma commute_left_comp:
   582   "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
   583   by (simp add: o_assoc comp_fun_commute)
   584 
   585 end
   586 
   587 inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
   588 for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
   589   emptyI [intro]: "fold_graph f z {} z" |
   590   insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
   591       \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
   592 
   593 inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
   594 
   595 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
   596   "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
   597 
   598 text{*A tempting alternative for the definiens is
   599 @{term "if finite A then THE y. fold_graph f z A y else e"}.
   600 It allows the removal of finiteness assumptions from the theorems
   601 @{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
   602 The proofs become ugly. It is not worth the effort. (???) *}
   603 
   604 lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
   605 by (induct rule: finite_induct) auto
   606 
   607 
   608 subsubsection{*From @{const fold_graph} to @{term fold}*}
   609 
   610 context comp_fun_commute
   611 begin
   612 
   613 lemma fold_graph_finite:
   614   assumes "fold_graph f z A y"
   615   shows "finite A"
   616   using assms by induct simp_all
   617 
   618 lemma fold_graph_insertE_aux:
   619   "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'"
   620 proof (induct set: fold_graph)
   621   case (insertI x A y) show ?case
   622   proof (cases "x = a")
   623     assume "x = a" with insertI show ?case by auto
   624   next
   625     assume "x \<noteq> a"
   626     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
   627       using insertI by auto
   628     have "f x y = f a (f x y')"
   629       unfolding y by (rule fun_left_comm)
   630     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
   631       using y' and `x \<noteq> a` and `x \<notin> A`
   632       by (simp add: insert_Diff_if fold_graph.insertI)
   633     ultimately show ?case by fast
   634   qed
   635 qed simp
   636 
   637 lemma fold_graph_insertE:
   638   assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
   639   obtains y where "v = f x y" and "fold_graph f z A y"
   640 using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
   641 
   642 lemma fold_graph_determ:
   643   "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
   644 proof (induct arbitrary: y set: fold_graph)
   645   case (insertI x A y v)
   646   from `fold_graph f z (insert x A) v` and `x \<notin> A`
   647   obtain y' where "v = f x y'" and "fold_graph f z A y'"
   648     by (rule fold_graph_insertE)
   649   from `fold_graph f z A y'` have "y' = y" by (rule insertI)
   650   with `v = f x y'` show "v = f x y" by simp
   651 qed fast
   652 
   653 lemma fold_equality:
   654   "fold_graph f z A y \<Longrightarrow> fold f z A = y"
   655   by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
   656 
   657 lemma fold_graph_fold:
   658   assumes "finite A"
   659   shows "fold_graph f z A (fold f z A)"
   660 proof -
   661   from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
   662   moreover note fold_graph_determ
   663   ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
   664   then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
   665   with assms show ?thesis by (simp add: fold_def)
   666 qed
   667 
   668 text {* The base case for @{text fold}: *}
   669 
   670 lemma (in -) fold_infinite [simp]:
   671   assumes "\<not> finite A"
   672   shows "fold f z A = z"
   673   using assms by (auto simp add: fold_def)
   674 
   675 lemma (in -) fold_empty [simp]:
   676   "fold f z {} = z"
   677   by (auto simp add: fold_def)
   678 
   679 text{* The various recursion equations for @{const fold}: *}
   680 
   681 lemma fold_insert [simp]:
   682   assumes "finite A" and "x \<notin> A"
   683   shows "fold f z (insert x A) = f x (fold f z A)"
   684 proof (rule fold_equality)
   685   fix z
   686   from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
   687   with `x \<notin> A` have "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
   688   then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
   689 qed
   690 
   691 declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
   692   -- {* No more proofs involve these. *}
   693 
   694 lemma fold_fun_left_comm:
   695   "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
   696 proof (induct rule: finite_induct)
   697   case empty then show ?case by simp
   698 next
   699   case (insert y A) then show ?case
   700     by (simp add: fun_left_comm [of x])
   701 qed
   702 
   703 lemma fold_insert2:
   704   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
   705   by (simp add: fold_fun_left_comm)
   706 
   707 lemma fold_rec:
   708   assumes "finite A" and "x \<in> A"
   709   shows "fold f z A = f x (fold f z (A - {x}))"
   710 proof -
   711   have A: "A = insert x (A - {x})" using `x \<in> A` by blast
   712   then have "fold f z A = fold f z (insert x (A - {x}))" by simp
   713   also have "\<dots> = f x (fold f z (A - {x}))"
   714     by (rule fold_insert) (simp add: `finite A`)+
   715   finally show ?thesis .
   716 qed
   717 
   718 lemma fold_insert_remove:
   719   assumes "finite A"
   720   shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
   721 proof -
   722   from `finite A` have "finite (insert x A)" by auto
   723   moreover have "x \<in> insert x A" by auto
   724   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
   725     by (rule fold_rec)
   726   then show ?thesis by simp
   727 qed
   728 
   729 end
   730 
   731 text{* Other properties of @{const fold}: *}
   732 
   733 lemma fold_image:
   734   assumes "inj_on g A"
   735   shows "fold f z (g ` A) = fold (f \<circ> g) z A"
   736 proof (cases "finite A")
   737   case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
   738 next
   739   case True
   740   have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
   741   proof
   742     fix w
   743     show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
   744     proof
   745       assume ?P then show ?Q using assms
   746       proof (induct "g ` A" w arbitrary: A)
   747         case emptyI then show ?case by (auto intro: fold_graph.emptyI)
   748       next
   749         case (insertI x A r B)
   750         from `inj_on g B` `x \<notin> A` `insert x A = image g B` obtain x' A' where
   751           "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
   752           by (rule inj_img_insertE)
   753         from insertI.prems have "fold_graph (f o g) z A' r"
   754           by (auto intro: insertI.hyps)
   755         with `x' \<notin> A'` have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
   756           by (rule fold_graph.insertI)
   757         then show ?case by simp
   758       qed
   759     next
   760       assume ?Q then show ?P using assms
   761       proof induct
   762         case emptyI thus ?case by (auto intro: fold_graph.emptyI)
   763       next
   764         case (insertI x A r)
   765         from `x \<notin> A` insertI.prems have "g x \<notin> g ` A" by auto
   766         moreover from insertI have "fold_graph f z (g ` A) r" by simp
   767         ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
   768           by (rule fold_graph.insertI)
   769         then show ?case by simp
   770       qed
   771     qed
   772   qed
   773   with True assms show ?thesis by (auto simp add: fold_def)
   774 qed
   775 
   776 lemma fold_cong:
   777   assumes "comp_fun_commute f" "comp_fun_commute g"
   778   assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
   779     and "s = t" and "A = B"
   780   shows "fold f s A = fold g t B"
   781 proof -
   782   have "fold f s A = fold g s A"  
   783   using `finite A` cong proof (induct A)
   784     case empty then show ?case by simp
   785   next
   786     case (insert x A)
   787     interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
   788     interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
   789     from insert show ?case by simp
   790   qed
   791   with assms show ?thesis by simp
   792 qed
   793 
   794 
   795 text {* A simplified version for idempotent functions: *}
   796 
   797 locale comp_fun_idem = comp_fun_commute +
   798   assumes comp_fun_idem: "f x \<circ> f x = f x"
   799 begin
   800 
   801 lemma fun_left_idem: "f x (f x z) = f x z"
   802   using comp_fun_idem by (simp add: fun_eq_iff)
   803 
   804 lemma fold_insert_idem:
   805   assumes fin: "finite A"
   806   shows "fold f z (insert x A)  = f x (fold f z A)"
   807 proof cases
   808   assume "x \<in> A"
   809   then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
   810   then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
   811 next
   812   assume "x \<notin> A" then show ?thesis using assms by simp
   813 qed
   814 
   815 declare fold_insert [simp del] fold_insert_idem [simp]
   816 
   817 lemma fold_insert_idem2:
   818   "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   819   by (simp add: fold_fun_left_comm)
   820 
   821 end
   822 
   823 
   824 subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
   825 
   826 lemma (in comp_fun_commute) comp_comp_fun_commute:
   827   "comp_fun_commute (f \<circ> g)"
   828 proof
   829 qed (simp_all add: comp_fun_commute)
   830 
   831 lemma (in comp_fun_idem) comp_comp_fun_idem:
   832   "comp_fun_idem (f \<circ> g)"
   833   by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
   834     (simp_all add: comp_fun_idem)
   835 
   836 lemma (in comp_fun_commute) comp_fun_commute_funpow:
   837   "comp_fun_commute (\<lambda>x. f x ^^ g x)"
   838 proof
   839   fix y x
   840   show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
   841   proof (cases "x = y")
   842     case True then show ?thesis by simp
   843   next
   844     case False show ?thesis
   845     proof (induct "g x" arbitrary: g)
   846       case 0 then show ?case by simp
   847     next
   848       case (Suc n g)
   849       have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
   850       proof (induct "g y" arbitrary: g)
   851         case 0 then show ?case by simp
   852       next
   853         case (Suc n g)
   854         def h \<equiv> "\<lambda>z. g z - 1"
   855         with Suc have "n = h y" by simp
   856         with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
   857           by auto
   858         from Suc h_def have "g y = Suc (h y)" by simp
   859         then show ?case by (simp add: comp_assoc hyp)
   860           (simp add: o_assoc comp_fun_commute)
   861       qed
   862       def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
   863       with Suc have "n = h x" by simp
   864       with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
   865         by auto
   866       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
   867       from Suc h_def have "g x = Suc (h x)" by simp
   868       then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
   869         (simp add: comp_assoc hyp1)
   870     qed
   871   qed
   872 qed
   873 
   874 
   875 subsubsection {* Expressing set operations via @{const fold} *}
   876 
   877 lemma comp_fun_commute_const:
   878   "comp_fun_commute (\<lambda>_. f)"
   879 proof
   880 qed rule
   881 
   882 lemma comp_fun_idem_insert:
   883   "comp_fun_idem insert"
   884 proof
   885 qed auto
   886 
   887 lemma comp_fun_idem_remove:
   888   "comp_fun_idem Set.remove"
   889 proof
   890 qed auto
   891 
   892 lemma (in semilattice_inf) comp_fun_idem_inf:
   893   "comp_fun_idem inf"
   894 proof
   895 qed (auto simp add: inf_left_commute)
   896 
   897 lemma (in semilattice_sup) comp_fun_idem_sup:
   898   "comp_fun_idem sup"
   899 proof
   900 qed (auto simp add: sup_left_commute)
   901 
   902 lemma union_fold_insert:
   903   assumes "finite A"
   904   shows "A \<union> B = fold insert B A"
   905 proof -
   906   interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
   907   from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
   908 qed
   909 
   910 lemma minus_fold_remove:
   911   assumes "finite A"
   912   shows "B - A = fold Set.remove B A"
   913 proof -
   914   interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
   915   from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
   916   then show ?thesis ..
   917 qed
   918 
   919 lemma comp_fun_commute_filter_fold:
   920   "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
   921 proof - 
   922   interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
   923   show ?thesis by default (auto simp: fun_eq_iff)
   924 qed
   925 
   926 lemma Set_filter_fold:
   927   assumes "finite A"
   928   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
   929 using assms
   930 by (induct A) 
   931   (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
   932 
   933 lemma inter_Set_filter:     
   934   assumes "finite B"
   935   shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
   936 using assms 
   937 by (induct B) (auto simp: Set.filter_def)
   938 
   939 lemma image_fold_insert:
   940   assumes "finite A"
   941   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
   942 using assms
   943 proof -
   944   interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
   945   show ?thesis using assms by (induct A) auto
   946 qed
   947 
   948 lemma Ball_fold:
   949   assumes "finite A"
   950   shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
   951 using assms
   952 proof -
   953   interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
   954   show ?thesis using assms by (induct A) auto
   955 qed
   956 
   957 lemma Bex_fold:
   958   assumes "finite A"
   959   shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
   960 using assms
   961 proof -
   962   interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
   963   show ?thesis using assms by (induct A) auto
   964 qed
   965 
   966 lemma comp_fun_commute_Pow_fold: 
   967   "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
   968   by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
   969 
   970 lemma Pow_fold:
   971   assumes "finite A"
   972   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
   973 using assms
   974 proof -
   975   interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
   976   show ?thesis using assms by (induct A) (auto simp: Pow_insert)
   977 qed
   978 
   979 lemma fold_union_pair:
   980   assumes "finite B"
   981   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
   982 proof -
   983   interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
   984   show ?thesis using assms  by (induct B arbitrary: A) simp_all
   985 qed
   986 
   987 lemma comp_fun_commute_product_fold: 
   988   assumes "finite B"
   989   shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
   990 by default (auto simp: fold_union_pair[symmetric] assms)
   991 
   992 lemma product_fold:
   993   assumes "finite A"
   994   assumes "finite B"
   995   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
   996 using assms unfolding Sigma_def 
   997 by (induct A) 
   998   (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
   999 
  1000 
  1001 context complete_lattice
  1002 begin
  1003 
  1004 lemma inf_Inf_fold_inf:
  1005   assumes "finite A"
  1006   shows "inf (Inf A) B = fold inf B A"
  1007 proof -
  1008   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1009   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1010     (simp_all add: inf_commute fun_eq_iff)
  1011 qed
  1012 
  1013 lemma sup_Sup_fold_sup:
  1014   assumes "finite A"
  1015   shows "sup (Sup A) B = fold sup B A"
  1016 proof -
  1017   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1018   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1019     (simp_all add: sup_commute fun_eq_iff)
  1020 qed
  1021 
  1022 lemma Inf_fold_inf:
  1023   assumes "finite A"
  1024   shows "Inf A = fold inf top A"
  1025   using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
  1026 
  1027 lemma Sup_fold_sup:
  1028   assumes "finite A"
  1029   shows "Sup A = fold sup bot A"
  1030   using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
  1031 
  1032 lemma inf_INF_fold_inf:
  1033   assumes "finite A"
  1034   shows "inf B (INFI A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
  1035 proof (rule sym)
  1036   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1037   interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
  1038   from `finite A` show "?fold = ?inf"
  1039     by (induct A arbitrary: B)
  1040       (simp_all add: INF_def inf_left_commute)
  1041 qed
  1042 
  1043 lemma sup_SUP_fold_sup:
  1044   assumes "finite A"
  1045   shows "sup B (SUPR A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
  1046 proof (rule sym)
  1047   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1048   interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
  1049   from `finite A` show "?fold = ?sup"
  1050     by (induct A arbitrary: B)
  1051       (simp_all add: SUP_def sup_left_commute)
  1052 qed
  1053 
  1054 lemma INF_fold_inf:
  1055   assumes "finite A"
  1056   shows "INFI A f = fold (inf \<circ> f) top A"
  1057   using assms inf_INF_fold_inf [of A top] by simp
  1058 
  1059 lemma SUP_fold_sup:
  1060   assumes "finite A"
  1061   shows "SUPR A f = fold (sup \<circ> f) bot A"
  1062   using assms sup_SUP_fold_sup [of A bot] by simp
  1063 
  1064 end
  1065 
  1066 
  1067 subsection {* Locales as mini-packages for fold operations *}
  1068 
  1069 subsubsection {* The natural case *}
  1070 
  1071 locale folding =
  1072   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1073   fixes z :: "'b"
  1074   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
  1075 begin
  1076 
  1077 definition F :: "'a set \<Rightarrow> 'b"
  1078 where
  1079   eq_fold: "F A = fold f z A"
  1080 
  1081 lemma empty [simp]:
  1082   "F {} = z"
  1083   by (simp add: eq_fold)
  1084 
  1085 lemma infinite [simp]:
  1086   "\<not> finite A \<Longrightarrow> F A = z"
  1087   by (simp add: eq_fold)
  1088  
  1089 lemma insert [simp]:
  1090   assumes "finite A" and "x \<notin> A"
  1091   shows "F (insert x A) = f x (F A)"
  1092 proof -
  1093   interpret comp_fun_commute f
  1094     by default (insert comp_fun_commute, simp add: fun_eq_iff)
  1095   from fold_insert assms
  1096   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1097   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1098 qed
  1099  
  1100 lemma remove:
  1101   assumes "finite A" and "x \<in> A"
  1102   shows "F A = f x (F (A - {x}))"
  1103 proof -
  1104   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
  1105     by (auto dest: mk_disjoint_insert)
  1106   moreover from `finite A` A have "finite B" by simp
  1107   ultimately show ?thesis by simp
  1108 qed
  1109 
  1110 lemma insert_remove:
  1111   assumes "finite A"
  1112   shows "F (insert x A) = f x (F (A - {x}))"
  1113   using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
  1114 
  1115 end
  1116 
  1117 
  1118 subsubsection {* With idempotency *}
  1119 
  1120 locale folding_idem = folding +
  1121   assumes comp_fun_idem: "f x \<circ> f x = f x"
  1122 begin
  1123 
  1124 declare insert [simp del]
  1125 
  1126 lemma insert_idem [simp]:
  1127   assumes "finite A"
  1128   shows "F (insert x A) = f x (F A)"
  1129 proof -
  1130   interpret comp_fun_idem f
  1131     by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
  1132   from fold_insert_idem assms
  1133   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1134   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1135 qed
  1136 
  1137 end
  1138 
  1139 
  1140 subsection {* Finite cardinality *}
  1141 
  1142 text {*
  1143   The traditional definition
  1144   @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
  1145   is ugly to work with.
  1146   But now that we have @{const fold} things are easy:
  1147 *}
  1148 
  1149 definition card :: "'a set \<Rightarrow> nat" where
  1150   "card = folding.F (\<lambda>_. Suc) 0"
  1151 
  1152 interpretation card!: folding "\<lambda>_. Suc" 0
  1153 where
  1154   "folding.F (\<lambda>_. Suc) 0 = card"
  1155 proof -
  1156   show "folding (\<lambda>_. Suc)" by default rule
  1157   then interpret card!: folding "\<lambda>_. Suc" 0 .
  1158   from card_def show "folding.F (\<lambda>_. Suc) 0 = card" by rule
  1159 qed
  1160 
  1161 lemma card_infinite:
  1162   "\<not> finite A \<Longrightarrow> card A = 0"
  1163   by (fact card.infinite)
  1164 
  1165 lemma card_empty:
  1166   "card {} = 0"
  1167   by (fact card.empty)
  1168 
  1169 lemma card_insert_disjoint:
  1170   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
  1171   by (fact card.insert)
  1172 
  1173 lemma card_insert_if:
  1174   "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
  1175   by auto (simp add: card.insert_remove card.remove)
  1176 
  1177 lemma card_ge_0_finite:
  1178   "card A > 0 \<Longrightarrow> finite A"
  1179   by (rule ccontr) simp
  1180 
  1181 lemma card_0_eq [simp, no_atp]:
  1182   "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
  1183   by (auto dest: mk_disjoint_insert)
  1184 
  1185 lemma finite_UNIV_card_ge_0:
  1186   "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
  1187   by (rule ccontr) simp
  1188 
  1189 lemma card_eq_0_iff:
  1190   "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
  1191   by auto
  1192 
  1193 lemma card_gt_0_iff:
  1194   "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
  1195   by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
  1196 
  1197 lemma card_Suc_Diff1:
  1198   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
  1199 apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
  1200 apply(simp del:insert_Diff_single)
  1201 done
  1202 
  1203 lemma card_Diff_singleton:
  1204   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
  1205   by (simp add: card_Suc_Diff1 [symmetric])
  1206 
  1207 lemma card_Diff_singleton_if:
  1208   "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
  1209   by (simp add: card_Diff_singleton)
  1210 
  1211 lemma card_Diff_insert[simp]:
  1212   assumes "finite A" and "a \<in> A" and "a \<notin> B"
  1213   shows "card (A - insert a B) = card (A - B) - 1"
  1214 proof -
  1215   have "A - insert a B = (A - B) - {a}" using assms by blast
  1216   then show ?thesis using assms by(simp add: card_Diff_singleton)
  1217 qed
  1218 
  1219 lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
  1220   by (fact card.insert_remove)
  1221 
  1222 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  1223 by (simp add: card_insert_if)
  1224 
  1225 lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
  1226 by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
  1227 
  1228 lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
  1229 using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
  1230 
  1231 lemma card_mono:
  1232   assumes "finite B" and "A \<subseteq> B"
  1233   shows "card A \<le> card B"
  1234 proof -
  1235   from assms have "finite A" by (auto intro: finite_subset)
  1236   then show ?thesis using assms proof (induct A arbitrary: B)
  1237     case empty then show ?case by simp
  1238   next
  1239     case (insert x A)
  1240     then have "x \<in> B" by simp
  1241     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
  1242     with insert.hyps have "card A \<le> card (B - {x})" by auto
  1243     with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
  1244   qed
  1245 qed
  1246 
  1247 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
  1248 apply (induct rule: finite_induct)
  1249 apply simp
  1250 apply clarify
  1251 apply (subgoal_tac "finite A & A - {x} <= F")
  1252  prefer 2 apply (blast intro: finite_subset, atomize)
  1253 apply (drule_tac x = "A - {x}" in spec)
  1254 apply (simp add: card_Diff_singleton_if split add: split_if_asm)
  1255 apply (case_tac "card A", auto)
  1256 done
  1257 
  1258 lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
  1259 apply (simp add: psubset_eq linorder_not_le [symmetric])
  1260 apply (blast dest: card_seteq)
  1261 done
  1262 
  1263 lemma card_Un_Int:
  1264   assumes "finite A" and "finite B"
  1265   shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
  1266 using assms proof (induct A)
  1267   case empty then show ?case by simp
  1268 next
  1269  case (insert x A) then show ?case
  1270     by (auto simp add: insert_absorb Int_insert_left)
  1271 qed
  1272 
  1273 lemma card_Un_disjoint:
  1274   assumes "finite A" and "finite B"
  1275   assumes "A \<inter> B = {}"
  1276   shows "card (A \<union> B) = card A + card B"
  1277 using assms card_Un_Int [of A B] by simp
  1278 
  1279 lemma card_Diff_subset:
  1280   assumes "finite B" and "B \<subseteq> A"
  1281   shows "card (A - B) = card A - card B"
  1282 proof (cases "finite A")
  1283   case False with assms show ?thesis by simp
  1284 next
  1285   case True with assms show ?thesis by (induct B arbitrary: A) simp_all
  1286 qed
  1287 
  1288 lemma card_Diff_subset_Int:
  1289   assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
  1290 proof -
  1291   have "A - B = A - A \<inter> B" by auto
  1292   thus ?thesis
  1293     by (simp add: card_Diff_subset AB) 
  1294 qed
  1295 
  1296 lemma diff_card_le_card_Diff:
  1297 assumes "finite B" shows "card A - card B \<le> card(A - B)"
  1298 proof-
  1299   have "card A - card B \<le> card A - card (A \<inter> B)"
  1300     using card_mono[OF assms Int_lower2, of A] by arith
  1301   also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
  1302   finally show ?thesis .
  1303 qed
  1304 
  1305 lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
  1306 apply (rule Suc_less_SucD)
  1307 apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
  1308 done
  1309 
  1310 lemma card_Diff2_less:
  1311   "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
  1312 apply (case_tac "x = y")
  1313  apply (simp add: card_Diff1_less del:card_Diff_insert)
  1314 apply (rule less_trans)
  1315  prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
  1316 done
  1317 
  1318 lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
  1319 apply (case_tac "x : A")
  1320  apply (simp_all add: card_Diff1_less less_imp_le)
  1321 done
  1322 
  1323 lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
  1324 by (erule psubsetI, blast)
  1325 
  1326 lemma insert_partition:
  1327   "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
  1328   \<Longrightarrow> x \<inter> \<Union> F = {}"
  1329 by auto
  1330 
  1331 lemma finite_psubset_induct[consumes 1, case_names psubset]:
  1332   assumes fin: "finite A" 
  1333   and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
  1334   shows "P A"
  1335 using fin
  1336 proof (induct A taking: card rule: measure_induct_rule)
  1337   case (less A)
  1338   have fin: "finite A" by fact
  1339   have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
  1340   { fix B 
  1341     assume asm: "B \<subset> A"
  1342     from asm have "card B < card A" using psubset_card_mono fin by blast
  1343     moreover
  1344     from asm have "B \<subseteq> A" by auto
  1345     then have "finite B" using fin finite_subset by blast
  1346     ultimately 
  1347     have "P B" using ih by simp
  1348   }
  1349   with fin show "P A" using major by blast
  1350 qed
  1351 
  1352 text{* main cardinality theorem *}
  1353 lemma card_partition [rule_format]:
  1354   "finite C ==>
  1355      finite (\<Union> C) -->
  1356      (\<forall>c\<in>C. card c = k) -->
  1357      (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
  1358      k * card(C) = card (\<Union> C)"
  1359 apply (erule finite_induct, simp)
  1360 apply (simp add: card_Un_disjoint insert_partition 
  1361        finite_subset [of _ "\<Union> (insert x F)"])
  1362 done
  1363 
  1364 lemma card_eq_UNIV_imp_eq_UNIV:
  1365   assumes fin: "finite (UNIV :: 'a set)"
  1366   and card: "card A = card (UNIV :: 'a set)"
  1367   shows "A = (UNIV :: 'a set)"
  1368 proof
  1369   show "A \<subseteq> UNIV" by simp
  1370   show "UNIV \<subseteq> A"
  1371   proof
  1372     fix x
  1373     show "x \<in> A"
  1374     proof (rule ccontr)
  1375       assume "x \<notin> A"
  1376       then have "A \<subset> UNIV" by auto
  1377       with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
  1378       with card show False by simp
  1379     qed
  1380   qed
  1381 qed
  1382 
  1383 text{*The form of a finite set of given cardinality*}
  1384 
  1385 lemma card_eq_SucD:
  1386 assumes "card A = Suc k"
  1387 shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
  1388 proof -
  1389   have fin: "finite A" using assms by (auto intro: ccontr)
  1390   moreover have "card A \<noteq> 0" using assms by auto
  1391   ultimately obtain b where b: "b \<in> A" by auto
  1392   show ?thesis
  1393   proof (intro exI conjI)
  1394     show "A = insert b (A-{b})" using b by blast
  1395     show "b \<notin> A - {b}" by blast
  1396     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
  1397       using assms b fin by(fastforce dest:mk_disjoint_insert)+
  1398   qed
  1399 qed
  1400 
  1401 lemma card_Suc_eq:
  1402   "(card A = Suc k) =
  1403    (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
  1404 apply(rule iffI)
  1405  apply(erule card_eq_SucD)
  1406 apply(auto)
  1407 apply(subst card.insert)
  1408  apply(auto intro:ccontr)
  1409 done
  1410 
  1411 lemma card_le_Suc_iff: "finite A \<Longrightarrow>
  1412   Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
  1413 by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
  1414   dest: subset_singletonD split: nat.splits if_splits)
  1415 
  1416 lemma finite_fun_UNIVD2:
  1417   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
  1418   shows "finite (UNIV :: 'b set)"
  1419 proof -
  1420   from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
  1421     by (rule finite_imageI)
  1422   moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
  1423     by (rule UNIV_eq_I) auto
  1424   ultimately show "finite (UNIV :: 'b set)" by simp
  1425 qed
  1426 
  1427 lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
  1428   unfolding UNIV_unit by simp
  1429 
  1430 lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
  1431   unfolding UNIV_bool by simp
  1432 
  1433 
  1434 subsubsection {* Cardinality of image *}
  1435 
  1436 lemma card_image_le: "finite A ==> card (f ` A) <= card A"
  1437 apply (induct rule: finite_induct)
  1438  apply simp
  1439 apply (simp add: le_SucI card_insert_if)
  1440 done
  1441 
  1442 lemma card_image:
  1443   assumes "inj_on f A"
  1444   shows "card (f ` A) = card A"
  1445 proof (cases "finite A")
  1446   case True then show ?thesis using assms by (induct A) simp_all
  1447 next
  1448   case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
  1449   with False show ?thesis by simp
  1450 qed
  1451 
  1452 lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
  1453 by(auto simp: card_image bij_betw_def)
  1454 
  1455 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
  1456 by (simp add: card_seteq card_image)
  1457 
  1458 lemma eq_card_imp_inj_on:
  1459   "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
  1460 apply (induct rule:finite_induct)
  1461 apply simp
  1462 apply(frule card_image_le[where f = f])
  1463 apply(simp add:card_insert_if split:if_splits)
  1464 done
  1465 
  1466 lemma inj_on_iff_eq_card:
  1467   "finite A ==> inj_on f A = (card(f ` A) = card A)"
  1468 by(blast intro: card_image eq_card_imp_inj_on)
  1469 
  1470 
  1471 lemma card_inj_on_le:
  1472   "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
  1473 apply (subgoal_tac "finite A") 
  1474  apply (force intro: card_mono simp add: card_image [symmetric])
  1475 apply (blast intro: finite_imageD dest: finite_subset) 
  1476 done
  1477 
  1478 lemma card_bij_eq:
  1479   "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
  1480      finite A; finite B |] ==> card A = card B"
  1481 by (auto intro: le_antisym card_inj_on_le)
  1482 
  1483 lemma bij_betw_finite:
  1484   assumes "bij_betw f A B"
  1485   shows "finite A \<longleftrightarrow> finite B"
  1486 using assms unfolding bij_betw_def
  1487 using finite_imageD[of f A] by auto
  1488 
  1489 
  1490 subsubsection {* Pigeonhole Principles *}
  1491 
  1492 lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
  1493 by (auto dest: card_image less_irrefl_nat)
  1494 
  1495 lemma pigeonhole_infinite:
  1496 assumes  "~ finite A" and "finite(f`A)"
  1497 shows "EX a0:A. ~finite{a:A. f a = f a0}"
  1498 proof -
  1499   have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
  1500   proof(induct "f`A" arbitrary: A rule: finite_induct)
  1501     case empty thus ?case by simp
  1502   next
  1503     case (insert b F)
  1504     show ?case
  1505     proof cases
  1506       assume "finite{a:A. f a = b}"
  1507       hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
  1508       also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
  1509       finally have "~ finite({a:A. f a \<noteq> b})" .
  1510       from insert(3)[OF _ this]
  1511       show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
  1512     next
  1513       assume 1: "~finite{a:A. f a = b}"
  1514       hence "{a \<in> A. f a = b} \<noteq> {}" by force
  1515       thus ?thesis using 1 by blast
  1516     qed
  1517   qed
  1518   from this[OF assms(2,1)] show ?thesis .
  1519 qed
  1520 
  1521 lemma pigeonhole_infinite_rel:
  1522 assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
  1523 shows "EX b:B. ~finite{a:A. R a b}"
  1524 proof -
  1525    let ?F = "%a. {b:B. R a b}"
  1526    from finite_Pow_iff[THEN iffD2, OF `finite B`]
  1527    have "finite(?F ` A)" by(blast intro: rev_finite_subset)
  1528    from pigeonhole_infinite[where f = ?F, OF assms(1) this]
  1529    obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
  1530    obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
  1531    { assume "finite{a:A. R a b0}"
  1532      then have "finite {a\<in>A. ?F a = ?F a0}"
  1533        using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
  1534    }
  1535    with 1 `b0 : B` show ?thesis by blast
  1536 qed
  1537 
  1538 
  1539 subsubsection {* Cardinality of sums *}
  1540 
  1541 lemma card_Plus:
  1542   assumes "finite A" and "finite B"
  1543   shows "card (A <+> B) = card A + card B"
  1544 proof -
  1545   have "Inl`A \<inter> Inr`B = {}" by fast
  1546   with assms show ?thesis
  1547     unfolding Plus_def
  1548     by (simp add: card_Un_disjoint card_image)
  1549 qed
  1550 
  1551 lemma card_Plus_conv_if:
  1552   "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
  1553   by (auto simp add: card_Plus)
  1554 
  1555 
  1556 subsubsection {* Cardinality of the Powerset *}
  1557 
  1558 lemma card_Pow: "finite A ==> card (Pow A) = 2 ^ card A"
  1559 apply (induct rule: finite_induct)
  1560  apply (simp_all add: Pow_insert)
  1561 apply (subst card_Un_disjoint, blast)
  1562   apply (blast, blast)
  1563 apply (subgoal_tac "inj_on (insert x) (Pow F)")
  1564  apply (subst mult_2)
  1565  apply (simp add: card_image Pow_insert)
  1566 apply (unfold inj_on_def)
  1567 apply (blast elim!: equalityE)
  1568 done
  1569 
  1570 text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
  1571 
  1572 lemma dvd_partition:
  1573   "finite (Union C) ==>
  1574     ALL c : C. k dvd card c ==>
  1575     (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
  1576   k dvd card (Union C)"
  1577 apply (frule finite_UnionD)
  1578 apply (rotate_tac -1)
  1579 apply (induct rule: finite_induct)
  1580 apply simp_all
  1581 apply clarify
  1582 apply (subst card_Un_disjoint)
  1583    apply (auto simp add: disjoint_eq_subset_Compl)
  1584 done
  1585 
  1586 
  1587 subsubsection {* Relating injectivity and surjectivity *}
  1588 
  1589 lemma finite_surj_inj: "finite A \<Longrightarrow> A \<subseteq> f ` A \<Longrightarrow> inj_on f A"
  1590 apply(rule eq_card_imp_inj_on, assumption)
  1591 apply(frule finite_imageI)
  1592 apply(drule (1) card_seteq)
  1593  apply(erule card_image_le)
  1594 apply simp
  1595 done
  1596 
  1597 lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
  1598 shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
  1599 by (blast intro: finite_surj_inj subset_UNIV)
  1600 
  1601 lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
  1602 shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
  1603 by(fastforce simp:surj_def dest!: endo_inj_surj)
  1604 
  1605 corollary infinite_UNIV_nat [iff]:
  1606   "\<not> finite (UNIV :: nat set)"
  1607 proof
  1608   assume "finite (UNIV :: nat set)"
  1609   with finite_UNIV_inj_surj [of Suc]
  1610   show False by simp (blast dest: Suc_neq_Zero surjD)
  1611 qed
  1612 
  1613 (* Often leads to bogus ATP proofs because of reduced type information, hence no_atp *)
  1614 lemma infinite_UNIV_char_0 [no_atp]:
  1615   "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
  1616 proof
  1617   assume "finite (UNIV :: 'a set)"
  1618   with subset_UNIV have "finite (range of_nat :: 'a set)"
  1619     by (rule finite_subset)
  1620   moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
  1621     by (simp add: inj_on_def)
  1622   ultimately have "finite (UNIV :: nat set)"
  1623     by (rule finite_imageD)
  1624   then show False
  1625     by simp
  1626 qed
  1627 
  1628 hide_const (open) Finite_Set.fold
  1629 
  1630 end
  1631