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