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