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