src/HOL/Finite_Set.thy
author haftmann
Sat Jul 05 11:01:53 2014 +0200 (2014-07-05)
changeset 57514 bdc2c6b40bf2
parent 57447 87429bdecad5
child 57598 56ed992b6d65
permissions -rw-r--r--
prefer ac_simps collections over separate name bindings for add and mult
     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 end
   737 
   738 text{* Other properties of @{const fold}: *}
   739 
   740 lemma fold_image:
   741   assumes "inj_on g A"
   742   shows "fold f z (g ` A) = fold (f \<circ> g) z A"
   743 proof (cases "finite A")
   744   case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
   745 next
   746   case True
   747   have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
   748   proof
   749     fix w
   750     show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
   751     proof
   752       assume ?P then show ?Q using assms
   753       proof (induct "g ` A" w arbitrary: A)
   754         case emptyI then show ?case by (auto intro: fold_graph.emptyI)
   755       next
   756         case (insertI x A r B)
   757         from `inj_on g B` `x \<notin> A` `insert x A = image g B` obtain x' A' where
   758           "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
   759           by (rule inj_img_insertE)
   760         from insertI.prems have "fold_graph (f o g) z A' r"
   761           by (auto intro: insertI.hyps)
   762         with `x' \<notin> A'` have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
   763           by (rule fold_graph.insertI)
   764         then show ?case by simp
   765       qed
   766     next
   767       assume ?Q then show ?P using assms
   768       proof induct
   769         case emptyI thus ?case by (auto intro: fold_graph.emptyI)
   770       next
   771         case (insertI x A r)
   772         from `x \<notin> A` insertI.prems have "g x \<notin> g ` A" by auto
   773         moreover from insertI have "fold_graph f z (g ` A) r" by simp
   774         ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
   775           by (rule fold_graph.insertI)
   776         then show ?case by simp
   777       qed
   778     qed
   779   qed
   780   with True assms show ?thesis by (auto simp add: fold_def)
   781 qed
   782 
   783 lemma fold_cong:
   784   assumes "comp_fun_commute f" "comp_fun_commute g"
   785   assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
   786     and "s = t" and "A = B"
   787   shows "fold f s A = fold g t B"
   788 proof -
   789   have "fold f s A = fold g s A"  
   790   using `finite A` cong proof (induct A)
   791     case empty then show ?case by simp
   792   next
   793     case (insert x A)
   794     interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
   795     interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
   796     from insert show ?case by simp
   797   qed
   798   with assms show ?thesis by simp
   799 qed
   800 
   801 
   802 text {* A simplified version for idempotent functions: *}
   803 
   804 locale comp_fun_idem = comp_fun_commute +
   805   assumes comp_fun_idem: "f x \<circ> f x = f x"
   806 begin
   807 
   808 lemma fun_left_idem: "f x (f x z) = f x z"
   809   using comp_fun_idem by (simp add: fun_eq_iff)
   810 
   811 lemma fold_insert_idem:
   812   assumes fin: "finite A"
   813   shows "fold f z (insert x A)  = f x (fold f z A)"
   814 proof cases
   815   assume "x \<in> A"
   816   then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
   817   then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
   818 next
   819   assume "x \<notin> A" then show ?thesis using assms by simp
   820 qed
   821 
   822 declare fold_insert [simp del] fold_insert_idem [simp]
   823 
   824 lemma fold_insert_idem2:
   825   "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   826   by (simp add: fold_fun_left_comm)
   827 
   828 end
   829 
   830 
   831 subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
   832 
   833 lemma (in comp_fun_commute) comp_comp_fun_commute:
   834   "comp_fun_commute (f \<circ> g)"
   835 proof
   836 qed (simp_all add: comp_fun_commute)
   837 
   838 lemma (in comp_fun_idem) comp_comp_fun_idem:
   839   "comp_fun_idem (f \<circ> g)"
   840   by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
   841     (simp_all add: comp_fun_idem)
   842 
   843 lemma (in comp_fun_commute) comp_fun_commute_funpow:
   844   "comp_fun_commute (\<lambda>x. f x ^^ g x)"
   845 proof
   846   fix y x
   847   show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
   848   proof (cases "x = y")
   849     case True then show ?thesis by simp
   850   next
   851     case False show ?thesis
   852     proof (induct "g x" arbitrary: g)
   853       case 0 then show ?case by simp
   854     next
   855       case (Suc n g)
   856       have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
   857       proof (induct "g y" arbitrary: g)
   858         case 0 then show ?case by simp
   859       next
   860         case (Suc n g)
   861         def h \<equiv> "\<lambda>z. g z - 1"
   862         with Suc have "n = h y" by simp
   863         with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
   864           by auto
   865         from Suc h_def have "g y = Suc (h y)" by simp
   866         then show ?case by (simp add: comp_assoc hyp)
   867           (simp add: o_assoc comp_fun_commute)
   868       qed
   869       def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
   870       with Suc have "n = h x" by simp
   871       with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
   872         by auto
   873       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
   874       from Suc h_def have "g x = Suc (h x)" by simp
   875       then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
   876         (simp add: comp_assoc hyp1)
   877     qed
   878   qed
   879 qed
   880 
   881 
   882 subsubsection {* Expressing set operations via @{const fold} *}
   883 
   884 lemma comp_fun_commute_const:
   885   "comp_fun_commute (\<lambda>_. f)"
   886 proof
   887 qed rule
   888 
   889 lemma comp_fun_idem_insert:
   890   "comp_fun_idem insert"
   891 proof
   892 qed auto
   893 
   894 lemma comp_fun_idem_remove:
   895   "comp_fun_idem Set.remove"
   896 proof
   897 qed auto
   898 
   899 lemma (in semilattice_inf) comp_fun_idem_inf:
   900   "comp_fun_idem inf"
   901 proof
   902 qed (auto simp add: inf_left_commute)
   903 
   904 lemma (in semilattice_sup) comp_fun_idem_sup:
   905   "comp_fun_idem sup"
   906 proof
   907 qed (auto simp add: sup_left_commute)
   908 
   909 lemma union_fold_insert:
   910   assumes "finite A"
   911   shows "A \<union> B = fold insert B A"
   912 proof -
   913   interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
   914   from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
   915 qed
   916 
   917 lemma minus_fold_remove:
   918   assumes "finite A"
   919   shows "B - A = fold Set.remove B A"
   920 proof -
   921   interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
   922   from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
   923   then show ?thesis ..
   924 qed
   925 
   926 lemma comp_fun_commute_filter_fold:
   927   "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
   928 proof - 
   929   interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
   930   show ?thesis by default (auto simp: fun_eq_iff)
   931 qed
   932 
   933 lemma Set_filter_fold:
   934   assumes "finite A"
   935   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
   936 using assms
   937 by (induct A) 
   938   (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
   939 
   940 lemma inter_Set_filter:     
   941   assumes "finite B"
   942   shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
   943 using assms 
   944 by (induct B) (auto simp: Set.filter_def)
   945 
   946 lemma image_fold_insert:
   947   assumes "finite A"
   948   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
   949 using assms
   950 proof -
   951   interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
   952   show ?thesis using assms by (induct A) auto
   953 qed
   954 
   955 lemma Ball_fold:
   956   assumes "finite A"
   957   shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
   958 using assms
   959 proof -
   960   interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
   961   show ?thesis using assms by (induct A) auto
   962 qed
   963 
   964 lemma Bex_fold:
   965   assumes "finite A"
   966   shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
   967 using assms
   968 proof -
   969   interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
   970   show ?thesis using assms by (induct A) auto
   971 qed
   972 
   973 lemma comp_fun_commute_Pow_fold: 
   974   "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
   975   by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
   976 
   977 lemma Pow_fold:
   978   assumes "finite A"
   979   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
   980 using assms
   981 proof -
   982   interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
   983   show ?thesis using assms by (induct A) (auto simp: Pow_insert)
   984 qed
   985 
   986 lemma fold_union_pair:
   987   assumes "finite B"
   988   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
   989 proof -
   990   interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
   991   show ?thesis using assms  by (induct B arbitrary: A) simp_all
   992 qed
   993 
   994 lemma comp_fun_commute_product_fold: 
   995   assumes "finite B"
   996   shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
   997 by default (auto simp: fold_union_pair[symmetric] assms)
   998 
   999 lemma product_fold:
  1000   assumes "finite A"
  1001   assumes "finite B"
  1002   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
  1003 using assms unfolding Sigma_def 
  1004 by (induct A) 
  1005   (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
  1006 
  1007 
  1008 context complete_lattice
  1009 begin
  1010 
  1011 lemma inf_Inf_fold_inf:
  1012   assumes "finite A"
  1013   shows "inf (Inf A) B = fold inf B A"
  1014 proof -
  1015   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1016   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1017     (simp_all add: inf_commute fun_eq_iff)
  1018 qed
  1019 
  1020 lemma sup_Sup_fold_sup:
  1021   assumes "finite A"
  1022   shows "sup (Sup A) B = fold sup B A"
  1023 proof -
  1024   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1025   from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
  1026     (simp_all add: sup_commute fun_eq_iff)
  1027 qed
  1028 
  1029 lemma Inf_fold_inf:
  1030   assumes "finite A"
  1031   shows "Inf A = fold inf top A"
  1032   using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
  1033 
  1034 lemma Sup_fold_sup:
  1035   assumes "finite A"
  1036   shows "Sup A = fold sup bot A"
  1037   using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
  1038 
  1039 lemma inf_INF_fold_inf:
  1040   assumes "finite A"
  1041   shows "inf B (INFIMUM A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
  1042 proof (rule sym)
  1043   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
  1044   interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
  1045   from `finite A` show "?fold = ?inf"
  1046     by (induct A arbitrary: B)
  1047       (simp_all add: inf_left_commute)
  1048 qed
  1049 
  1050 lemma sup_SUP_fold_sup:
  1051   assumes "finite A"
  1052   shows "sup B (SUPREMUM A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
  1053 proof (rule sym)
  1054   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
  1055   interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
  1056   from `finite A` show "?fold = ?sup"
  1057     by (induct A arbitrary: B)
  1058       (simp_all add: sup_left_commute)
  1059 qed
  1060 
  1061 lemma INF_fold_inf:
  1062   assumes "finite A"
  1063   shows "INFIMUM A f = fold (inf \<circ> f) top A"
  1064   using assms inf_INF_fold_inf [of A top] by simp
  1065 
  1066 lemma SUP_fold_sup:
  1067   assumes "finite A"
  1068   shows "SUPREMUM A f = fold (sup \<circ> f) bot A"
  1069   using assms sup_SUP_fold_sup [of A bot] by simp
  1070 
  1071 end
  1072 
  1073 
  1074 subsection {* Locales as mini-packages for fold operations *}
  1075 
  1076 subsubsection {* The natural case *}
  1077 
  1078 locale folding =
  1079   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1080   fixes z :: "'b"
  1081   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
  1082 begin
  1083 
  1084 interpretation fold?: comp_fun_commute f
  1085   by default (insert comp_fun_commute, simp add: fun_eq_iff)
  1086 
  1087 definition F :: "'a set \<Rightarrow> 'b"
  1088 where
  1089   eq_fold: "F A = fold f z A"
  1090 
  1091 lemma empty [simp]:
  1092   "F {} = z"
  1093   by (simp add: eq_fold)
  1094 
  1095 lemma infinite [simp]:
  1096   "\<not> finite A \<Longrightarrow> F A = z"
  1097   by (simp add: eq_fold)
  1098  
  1099 lemma insert [simp]:
  1100   assumes "finite A" and "x \<notin> A"
  1101   shows "F (insert x A) = f x (F A)"
  1102 proof -
  1103   from fold_insert assms
  1104   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1105   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1106 qed
  1107  
  1108 lemma remove:
  1109   assumes "finite A" and "x \<in> A"
  1110   shows "F A = f x (F (A - {x}))"
  1111 proof -
  1112   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
  1113     by (auto dest: mk_disjoint_insert)
  1114   moreover from `finite A` A have "finite B" by simp
  1115   ultimately show ?thesis by simp
  1116 qed
  1117 
  1118 lemma insert_remove:
  1119   assumes "finite A"
  1120   shows "F (insert x A) = f x (F (A - {x}))"
  1121   using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
  1122 
  1123 end
  1124 
  1125 
  1126 subsubsection {* With idempotency *}
  1127 
  1128 locale folding_idem = folding +
  1129   assumes comp_fun_idem: "f x \<circ> f x = f x"
  1130 begin
  1131 
  1132 declare insert [simp del]
  1133 
  1134 interpretation fold?: comp_fun_idem f
  1135   by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
  1136 
  1137 lemma insert_idem [simp]:
  1138   assumes "finite A"
  1139   shows "F (insert x A) = f x (F A)"
  1140 proof -
  1141   from fold_insert_idem assms
  1142   have "fold f z (insert x A) = f x (fold f z A)" by simp
  1143   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
  1144 qed
  1145 
  1146 end
  1147 
  1148 
  1149 subsection {* Finite cardinality *}
  1150 
  1151 text {*
  1152   The traditional definition
  1153   @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
  1154   is ugly to work with.
  1155   But now that we have @{const fold} things are easy:
  1156 *}
  1157 
  1158 definition card :: "'a set \<Rightarrow> nat" where
  1159   "card = folding.F (\<lambda>_. Suc) 0"
  1160 
  1161 interpretation card!: folding "\<lambda>_. Suc" 0
  1162 where
  1163   "folding.F (\<lambda>_. Suc) 0 = card"
  1164 proof -
  1165   show "folding (\<lambda>_. Suc)" by default rule
  1166   then interpret card!: folding "\<lambda>_. Suc" 0 .
  1167   from card_def show "folding.F (\<lambda>_. Suc) 0 = card" by rule
  1168 qed
  1169 
  1170 lemma card_infinite:
  1171   "\<not> finite A \<Longrightarrow> card A = 0"
  1172   by (fact card.infinite)
  1173 
  1174 lemma card_empty:
  1175   "card {} = 0"
  1176   by (fact card.empty)
  1177 
  1178 lemma card_insert_disjoint:
  1179   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
  1180   by (fact card.insert)
  1181 
  1182 lemma card_insert_if:
  1183   "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
  1184   by auto (simp add: card.insert_remove card.remove)
  1185 
  1186 lemma card_ge_0_finite:
  1187   "card A > 0 \<Longrightarrow> finite A"
  1188   by (rule ccontr) simp
  1189 
  1190 lemma card_0_eq [simp]:
  1191   "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
  1192   by (auto dest: mk_disjoint_insert)
  1193 
  1194 lemma finite_UNIV_card_ge_0:
  1195   "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
  1196   by (rule ccontr) simp
  1197 
  1198 lemma card_eq_0_iff:
  1199   "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
  1200   by auto
  1201 
  1202 lemma card_gt_0_iff:
  1203   "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
  1204   by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
  1205 
  1206 lemma card_Suc_Diff1:
  1207   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
  1208 apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
  1209 apply(simp del:insert_Diff_single)
  1210 done
  1211 
  1212 lemma card_Diff_singleton:
  1213   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
  1214   by (simp add: card_Suc_Diff1 [symmetric])
  1215 
  1216 lemma card_Diff_singleton_if:
  1217   "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
  1218   by (simp add: card_Diff_singleton)
  1219 
  1220 lemma card_Diff_insert[simp]:
  1221   assumes "finite A" and "a \<in> A" and "a \<notin> B"
  1222   shows "card (A - insert a B) = card (A - B) - 1"
  1223 proof -
  1224   have "A - insert a B = (A - B) - {a}" using assms by blast
  1225   then show ?thesis using assms by(simp add: card_Diff_singleton)
  1226 qed
  1227 
  1228 lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
  1229   by (fact card.insert_remove)
  1230 
  1231 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  1232 by (simp add: card_insert_if)
  1233 
  1234 lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
  1235 by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
  1236 
  1237 lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
  1238 using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
  1239 
  1240 lemma card_mono:
  1241   assumes "finite B" and "A \<subseteq> B"
  1242   shows "card A \<le> card B"
  1243 proof -
  1244   from assms have "finite A" by (auto intro: finite_subset)
  1245   then show ?thesis using assms proof (induct A arbitrary: B)
  1246     case empty then show ?case by simp
  1247   next
  1248     case (insert x A)
  1249     then have "x \<in> B" by simp
  1250     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
  1251     with insert.hyps have "card A \<le> card (B - {x})" by auto
  1252     with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
  1253   qed
  1254 qed
  1255 
  1256 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
  1257 apply (induct rule: finite_induct)
  1258 apply simp
  1259 apply clarify
  1260 apply (subgoal_tac "finite A & A - {x} <= F")
  1261  prefer 2 apply (blast intro: finite_subset, atomize)
  1262 apply (drule_tac x = "A - {x}" in spec)
  1263 apply (simp add: card_Diff_singleton_if split add: split_if_asm)
  1264 apply (case_tac "card A", auto)
  1265 done
  1266 
  1267 lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
  1268 apply (simp add: psubset_eq linorder_not_le [symmetric])
  1269 apply (blast dest: card_seteq)
  1270 done
  1271 
  1272 lemma card_Un_Int:
  1273   assumes "finite A" and "finite B"
  1274   shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
  1275 using assms proof (induct A)
  1276   case empty then show ?case by simp
  1277 next
  1278  case (insert x A) then show ?case
  1279     by (auto simp add: insert_absorb Int_insert_left)
  1280 qed
  1281 
  1282 lemma card_Un_disjoint:
  1283   assumes "finite A" and "finite B"
  1284   assumes "A \<inter> B = {}"
  1285   shows "card (A \<union> B) = card A + card B"
  1286 using assms card_Un_Int [of A B] by simp
  1287 
  1288 lemma card_Diff_subset:
  1289   assumes "finite B" and "B \<subseteq> A"
  1290   shows "card (A - B) = card A - card B"
  1291 proof (cases "finite A")
  1292   case False with assms show ?thesis by simp
  1293 next
  1294   case True with assms show ?thesis by (induct B arbitrary: A) simp_all
  1295 qed
  1296 
  1297 lemma card_Diff_subset_Int:
  1298   assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
  1299 proof -
  1300   have "A - B = A - A \<inter> B" by auto
  1301   thus ?thesis
  1302     by (simp add: card_Diff_subset AB) 
  1303 qed
  1304 
  1305 lemma diff_card_le_card_Diff:
  1306 assumes "finite B" shows "card A - card B \<le> card(A - B)"
  1307 proof-
  1308   have "card A - card B \<le> card A - card (A \<inter> B)"
  1309     using card_mono[OF assms Int_lower2, of A] by arith
  1310   also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
  1311   finally show ?thesis .
  1312 qed
  1313 
  1314 lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
  1315 apply (rule Suc_less_SucD)
  1316 apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
  1317 done
  1318 
  1319 lemma card_Diff2_less:
  1320   "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
  1321 apply (case_tac "x = y")
  1322  apply (simp add: card_Diff1_less del:card_Diff_insert)
  1323 apply (rule less_trans)
  1324  prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
  1325 done
  1326 
  1327 lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
  1328 apply (case_tac "x : A")
  1329  apply (simp_all add: card_Diff1_less less_imp_le)
  1330 done
  1331 
  1332 lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
  1333 by (erule psubsetI, blast)
  1334 
  1335 lemma card_le_inj:
  1336   assumes fA: "finite A"
  1337     and fB: "finite B"
  1338     and c: "card A \<le> card B"
  1339   shows "\<exists>f. f ` A \<subseteq> B \<and> inj_on f A"
  1340   using fA fB c
  1341 proof (induct arbitrary: B rule: finite_induct)
  1342   case empty
  1343   then show ?case by simp
  1344 next
  1345   case (insert x s t)
  1346   then show ?case
  1347   proof (induct rule: finite_induct[OF "insert.prems"(1)])
  1348     case 1
  1349     then show ?case by simp
  1350   next
  1351     case (2 y t)
  1352     from "2.prems"(1,2,5) "2.hyps"(1,2) have cst: "card s \<le> card t"
  1353       by simp
  1354     from "2.prems"(3) [OF "2.hyps"(1) cst]
  1355     obtain f where "f ` s \<subseteq> t" "inj_on f s"
  1356       by blast
  1357     with "2.prems"(2) "2.hyps"(2) show ?case
  1358       apply -
  1359       apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
  1360       apply (auto simp add: inj_on_def)
  1361       done
  1362   qed
  1363 qed
  1364 
  1365 lemma card_subset_eq:
  1366   assumes fB: "finite B"
  1367     and AB: "A \<subseteq> B"
  1368     and c: "card A = card B"
  1369   shows "A = B"
  1370 proof -
  1371   from fB AB have fA: "finite A"
  1372     by (auto intro: finite_subset)
  1373   from fA fB have fBA: "finite (B - A)"
  1374     by auto
  1375   have e: "A \<inter> (B - A) = {}"
  1376     by blast
  1377   have eq: "A \<union> (B - A) = B"
  1378     using AB by blast
  1379   from card_Un_disjoint[OF fA fBA e, unfolded eq c] have "card (B - A) = 0"
  1380     by arith
  1381   then have "B - A = {}"
  1382     unfolding card_eq_0_iff using fA fB by simp
  1383   with AB show "A = B"
  1384     by blast
  1385 qed
  1386 
  1387 lemma insert_partition:
  1388   "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
  1389   \<Longrightarrow> x \<inter> \<Union> F = {}"
  1390 by auto
  1391 
  1392 lemma finite_psubset_induct[consumes 1, case_names psubset]:
  1393   assumes fin: "finite A" 
  1394   and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
  1395   shows "P A"
  1396 using fin
  1397 proof (induct A taking: card rule: measure_induct_rule)
  1398   case (less A)
  1399   have fin: "finite A" by fact
  1400   have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
  1401   { fix B 
  1402     assume asm: "B \<subset> A"
  1403     from asm have "card B < card A" using psubset_card_mono fin by blast
  1404     moreover
  1405     from asm have "B \<subseteq> A" by auto
  1406     then have "finite B" using fin finite_subset by blast
  1407     ultimately 
  1408     have "P B" using ih by simp
  1409   }
  1410   with fin show "P A" using major by blast
  1411 qed
  1412 
  1413 lemma finite_induct_select[consumes 1, case_names empty select]:
  1414   assumes "finite S"
  1415   assumes "P {}"
  1416   assumes select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)"
  1417   shows "P S"
  1418 proof -
  1419   have "0 \<le> card S" by simp
  1420   then have "\<exists>T \<subseteq> S. card T = card S \<and> P T"
  1421   proof (induct rule: dec_induct)
  1422     case base with `P {}` show ?case
  1423       by (intro exI[of _ "{}"]) auto
  1424   next
  1425     case (step n)
  1426     then obtain T where T: "T \<subseteq> S" "card T = n" "P T"
  1427       by auto
  1428     with `n < card S` have "T \<subset> S" "P T"
  1429       by auto
  1430     with select[of T] obtain s where "s \<in> S" "s \<notin> T" "P (insert s T)"
  1431       by auto
  1432     with step(2) T `finite S` show ?case
  1433       by (intro exI[of _ "insert s T"]) (auto dest: finite_subset)
  1434   qed
  1435   with `finite S` show "P S"
  1436     by (auto dest: card_subset_eq)
  1437 qed
  1438 
  1439 text{* main cardinality theorem *}
  1440 lemma card_partition [rule_format]:
  1441   "finite C ==>
  1442      finite (\<Union> C) -->
  1443      (\<forall>c\<in>C. card c = k) -->
  1444      (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
  1445      k * card(C) = card (\<Union> C)"
  1446 apply (erule finite_induct, simp)
  1447 apply (simp add: card_Un_disjoint insert_partition 
  1448        finite_subset [of _ "\<Union> (insert x F)"])
  1449 done
  1450 
  1451 lemma card_eq_UNIV_imp_eq_UNIV:
  1452   assumes fin: "finite (UNIV :: 'a set)"
  1453   and card: "card A = card (UNIV :: 'a set)"
  1454   shows "A = (UNIV :: 'a set)"
  1455 proof
  1456   show "A \<subseteq> UNIV" by simp
  1457   show "UNIV \<subseteq> A"
  1458   proof
  1459     fix x
  1460     show "x \<in> A"
  1461     proof (rule ccontr)
  1462       assume "x \<notin> A"
  1463       then have "A \<subset> UNIV" by auto
  1464       with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
  1465       with card show False by simp
  1466     qed
  1467   qed
  1468 qed
  1469 
  1470 text{*The form of a finite set of given cardinality*}
  1471 
  1472 lemma card_eq_SucD:
  1473 assumes "card A = Suc k"
  1474 shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
  1475 proof -
  1476   have fin: "finite A" using assms by (auto intro: ccontr)
  1477   moreover have "card A \<noteq> 0" using assms by auto
  1478   ultimately obtain b where b: "b \<in> A" by auto
  1479   show ?thesis
  1480   proof (intro exI conjI)
  1481     show "A = insert b (A-{b})" using b by blast
  1482     show "b \<notin> A - {b}" by blast
  1483     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
  1484       using assms b fin by(fastforce dest:mk_disjoint_insert)+
  1485   qed
  1486 qed
  1487 
  1488 lemma card_Suc_eq:
  1489   "(card A = Suc k) =
  1490    (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
  1491  apply(auto elim!: card_eq_SucD)
  1492  apply(subst card.insert)
  1493  apply(auto simp add: intro:ccontr)
  1494  done
  1495 
  1496 lemma card_le_Suc_iff: "finite A \<Longrightarrow>
  1497   Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
  1498 by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
  1499   dest: subset_singletonD split: nat.splits if_splits)
  1500 
  1501 lemma finite_fun_UNIVD2:
  1502   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
  1503   shows "finite (UNIV :: 'b set)"
  1504 proof -
  1505   from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
  1506     by (rule finite_imageI)
  1507   moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
  1508     by (rule UNIV_eq_I) auto
  1509   ultimately show "finite (UNIV :: 'b set)" by simp
  1510 qed
  1511 
  1512 lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
  1513   unfolding UNIV_unit by simp
  1514 
  1515 lemma infinite_arbitrarily_large:
  1516   assumes "\<not> finite A"
  1517   shows "\<exists>B. finite B \<and> card B = n \<and> B \<subseteq> A"
  1518 proof (induction n)
  1519   case 0 show ?case by (intro exI[of _ "{}"]) auto
  1520 next 
  1521   case (Suc n)
  1522   then guess B .. note B = this
  1523   with `\<not> finite A` have "A \<noteq> B" by auto
  1524   with B have "B \<subset> A" by auto
  1525   hence "\<exists>x. x \<in> A - B" by (elim psubset_imp_ex_mem)
  1526   then guess x .. note x = this
  1527   with B have "finite (insert x B) \<and> card (insert x B) = Suc n \<and> insert x B \<subseteq> A"
  1528     by auto
  1529   thus "\<exists>B. finite B \<and> card B = Suc n \<and> B \<subseteq> A" ..
  1530 qed
  1531 
  1532 subsubsection {* Cardinality of image *}
  1533 
  1534 lemma card_image_le: "finite A ==> card (f ` A) \<le> card A"
  1535   by (induct rule: finite_induct) (simp_all add: le_SucI card_insert_if)
  1536 
  1537 lemma card_image:
  1538   assumes "inj_on f A"
  1539   shows "card (f ` A) = card A"
  1540 proof (cases "finite A")
  1541   case True then show ?thesis using assms by (induct A) simp_all
  1542 next
  1543   case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
  1544   with False show ?thesis by simp
  1545 qed
  1546 
  1547 lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
  1548 by(auto simp: card_image bij_betw_def)
  1549 
  1550 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
  1551 by (simp add: card_seteq card_image)
  1552 
  1553 lemma eq_card_imp_inj_on:
  1554   assumes "finite A" "card(f ` A) = card A" shows "inj_on f A"
  1555 using assms
  1556 proof (induct rule:finite_induct)
  1557   case empty show ?case by simp
  1558 next
  1559   case (insert x A)
  1560   then show ?case using card_image_le [of A f]
  1561     by (simp add: card_insert_if split: if_splits)
  1562 qed
  1563 
  1564 lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card(f ` A) = card A"
  1565   by (blast intro: card_image eq_card_imp_inj_on)
  1566 
  1567 lemma card_inj_on_le:
  1568   assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" shows "card A \<le> card B"
  1569 proof -
  1570   have "finite A" using assms
  1571     by (blast intro: finite_imageD dest: finite_subset)
  1572   then show ?thesis using assms 
  1573    by (force intro: card_mono simp: card_image [symmetric])
  1574 qed
  1575 
  1576 lemma card_bij_eq:
  1577   "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
  1578      finite A; finite B |] ==> card A = card B"
  1579 by (auto intro: le_antisym card_inj_on_le)
  1580 
  1581 lemma bij_betw_finite:
  1582   assumes "bij_betw f A B"
  1583   shows "finite A \<longleftrightarrow> finite B"
  1584 using assms unfolding bij_betw_def
  1585 using finite_imageD[of f A] by auto
  1586 
  1587 lemma inj_on_finite:
  1588 assumes "inj_on f A" "f ` A \<le> B" "finite B"
  1589 shows "finite A"
  1590 using assms finite_imageD finite_subset by blast
  1591 
  1592 
  1593 subsubsection {* Pigeonhole Principles *}
  1594 
  1595 lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
  1596 by (auto dest: card_image less_irrefl_nat)
  1597 
  1598 lemma pigeonhole_infinite:
  1599 assumes  "~ finite A" and "finite(f`A)"
  1600 shows "EX a0:A. ~finite{a:A. f a = f a0}"
  1601 proof -
  1602   have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
  1603   proof(induct "f`A" arbitrary: A rule: finite_induct)
  1604     case empty thus ?case by simp
  1605   next
  1606     case (insert b F)
  1607     show ?case
  1608     proof cases
  1609       assume "finite{a:A. f a = b}"
  1610       hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
  1611       also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
  1612       finally have "~ finite({a:A. f a \<noteq> b})" .
  1613       from insert(3)[OF _ this]
  1614       show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
  1615     next
  1616       assume 1: "~finite{a:A. f a = b}"
  1617       hence "{a \<in> A. f a = b} \<noteq> {}" by force
  1618       thus ?thesis using 1 by blast
  1619     qed
  1620   qed
  1621   from this[OF assms(2,1)] show ?thesis .
  1622 qed
  1623 
  1624 lemma pigeonhole_infinite_rel:
  1625 assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
  1626 shows "EX b:B. ~finite{a:A. R a b}"
  1627 proof -
  1628    let ?F = "%a. {b:B. R a b}"
  1629    from finite_Pow_iff[THEN iffD2, OF `finite B`]
  1630    have "finite(?F ` A)" by(blast intro: rev_finite_subset)
  1631    from pigeonhole_infinite[where f = ?F, OF assms(1) this]
  1632    obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
  1633    obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
  1634    { assume "finite{a:A. R a b0}"
  1635      then have "finite {a\<in>A. ?F a = ?F a0}"
  1636        using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
  1637    }
  1638    with 1 `b0 : B` show ?thesis by blast
  1639 qed
  1640 
  1641 
  1642 subsubsection {* Cardinality of sums *}
  1643 
  1644 lemma card_Plus:
  1645   assumes "finite A" and "finite B"
  1646   shows "card (A <+> B) = card A + card B"
  1647 proof -
  1648   have "Inl`A \<inter> Inr`B = {}" by fast
  1649   with assms show ?thesis
  1650     unfolding Plus_def
  1651     by (simp add: card_Un_disjoint card_image)
  1652 qed
  1653 
  1654 lemma card_Plus_conv_if:
  1655   "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
  1656   by (auto simp add: card_Plus)
  1657 
  1658 text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
  1659 
  1660 lemma dvd_partition:
  1661   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 = {}"
  1662     shows "k dvd card (\<Union>C)"
  1663 proof -
  1664   have "finite C" 
  1665     by (rule finite_UnionD [OF f])
  1666   then show ?thesis using assms
  1667   proof (induct rule: finite_induct)
  1668     case empty show ?case by simp
  1669   next
  1670     case (insert c C)
  1671     then show ?case 
  1672       apply simp
  1673       apply (subst card_Un_disjoint)
  1674       apply (auto simp add: disjoint_eq_subset_Compl)
  1675       done
  1676   qed
  1677 qed
  1678 
  1679 subsubsection {* Relating injectivity and surjectivity *}
  1680 
  1681 lemma finite_surj_inj: assumes "finite A" "A \<subseteq> f ` A" shows "inj_on f A"
  1682 proof -
  1683   have "f ` A = A" 
  1684     by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le)
  1685   then show ?thesis using assms
  1686     by (simp add: eq_card_imp_inj_on)
  1687 qed
  1688 
  1689 lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
  1690 shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
  1691 by (blast intro: finite_surj_inj subset_UNIV)
  1692 
  1693 lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
  1694 shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
  1695 by(fastforce simp:surj_def dest!: endo_inj_surj)
  1696 
  1697 corollary infinite_UNIV_nat [iff]:
  1698   "\<not> finite (UNIV :: nat set)"
  1699 proof
  1700   assume "finite (UNIV :: nat set)"
  1701   with finite_UNIV_inj_surj [of Suc]
  1702   show False by simp (blast dest: Suc_neq_Zero surjD)
  1703 qed
  1704 
  1705 lemma infinite_UNIV_char_0:
  1706   "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
  1707 proof
  1708   assume "finite (UNIV :: 'a set)"
  1709   with subset_UNIV have "finite (range of_nat :: 'a set)"
  1710     by (rule finite_subset)
  1711   moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
  1712     by (simp add: inj_on_def)
  1713   ultimately have "finite (UNIV :: nat set)"
  1714     by (rule finite_imageD)
  1715   then show False
  1716     by simp
  1717 qed
  1718 
  1719 hide_const (open) Finite_Set.fold
  1720 
  1721 end