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