src/HOL/Finite_Set.thy
 author hoelzl Thu Jan 31 11:31:27 2013 +0100 (2013-01-31) changeset 50999 3de230ed0547 parent 49806 acb6fa98e310 child 51290 c48477e76de5 permissions -rw-r--r--
introduce order topology
```     1 (*  Title:      HOL/Finite_Set.thy
```
```     2     Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
```
```     3                 with contributions by Jeremy Avigad
```
```     4 *)
```
```     5
```
```     6 header {* Finite sets *}
```
```     7
```
```     8 theory Finite_Set
```
```     9 imports Option Power
```
```    10 begin
```
```    11
```
```    12 subsection {* Predicate for finite sets *}
```
```    13
```
```    14 inductive finite :: "'a set \<Rightarrow> bool"
```
```    15   where
```
```    16     emptyI [simp, intro!]: "finite {}"
```
```    17   | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)"
```
```    18
```
```    19 simproc_setup finite_Collect ("finite (Collect P)") = {* K Set_Comprehension_Pointfree.simproc *}
```
```    20
```
```    21 lemma finite_induct [case_names empty insert, induct set: finite]:
```
```    22   -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
```
```    23   assumes "finite F"
```
```    24   assumes "P {}"
```
```    25     and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
```
```    26   shows "P F"
```
```    27 using `finite F`
```
```    28 proof induct
```
```    29   show "P {}" by fact
```
```    30   fix x F assume F: "finite F" and P: "P F"
```
```    31   show "P (insert x F)"
```
```    32   proof cases
```
```    33     assume "x \<in> F"
```
```    34     hence "insert x F = F" by (rule insert_absorb)
```
```    35     with P show ?thesis by (simp only:)
```
```    36   next
```
```    37     assume "x \<notin> F"
```
```    38     from F this P show ?thesis by (rule insert)
```
```    39   qed
```
```    40 qed
```
```    41
```
```    42
```
```    43 subsubsection {* Choice principles *}
```
```    44
```
```    45 lemma ex_new_if_finite: -- "does not depend on def of finite at all"
```
```    46   assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
```
```    47   shows "\<exists>a::'a. a \<notin> A"
```
```    48 proof -
```
```    49   from assms have "A \<noteq> UNIV" by blast
```
```    50   then show ?thesis by blast
```
```    51 qed
```
```    52
```
```    53 text {* A finite choice principle. Does not need the SOME choice operator. *}
```
```    54
```
```    55 lemma finite_set_choice:
```
```    56   "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
```
```    57 proof (induct rule: finite_induct)
```
```    58   case empty then show ?case by simp
```
```    59 next
```
```    60   case (insert a A)
```
```    61   then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
```
```    62   show ?case (is "EX f. ?P f")
```
```    63   proof
```
```    64     show "?P(%x. if x = a then b else f x)" using f ab by auto
```
```    65   qed
```
```    66 qed
```
```    67
```
```    68
```
```    69 subsubsection {* Finite sets are the images of initial segments of natural numbers *}
```
```    70
```
```    71 lemma finite_imp_nat_seg_image_inj_on:
```
```    72   assumes "finite A"
```
```    73   shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
```
```    74 using assms
```
```    75 proof induct
```
```    76   case empty
```
```    77   show ?case
```
```    78   proof
```
```    79     show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}" by simp
```
```    80   qed
```
```    81 next
```
```    82   case (insert a A)
```
```    83   have notinA: "a \<notin> A" by fact
```
```    84   from insert.hyps obtain n f
```
```    85     where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
```
```    86   hence "insert a A = f(n:=a) ` {i. i < Suc n}"
```
```    87         "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
```
```    88     by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
```
```    89   thus ?case by blast
```
```    90 qed
```
```    91
```
```    92 lemma nat_seg_image_imp_finite:
```
```    93   "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
```
```    94 proof (induct n arbitrary: A)
```
```    95   case 0 thus ?case by simp
```
```    96 next
```
```    97   case (Suc n)
```
```    98   let ?B = "f ` {i. i < n}"
```
```    99   have finB: "finite ?B" by(rule Suc.hyps[OF refl])
```
```   100   show ?case
```
```   101   proof cases
```
```   102     assume "\<exists>k<n. f n = f k"
```
```   103     hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
```
```   104     thus ?thesis using finB by simp
```
```   105   next
```
```   106     assume "\<not>(\<exists> k<n. f n = f k)"
```
```   107     hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
```
```   108     thus ?thesis using finB by simp
```
```   109   qed
```
```   110 qed
```
```   111
```
```   112 lemma finite_conv_nat_seg_image:
```
```   113   "finite A \<longleftrightarrow> (\<exists>(n::nat) f. A = f ` {i::nat. i < n})"
```
```   114   by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
```
```   115
```
```   116 lemma finite_imp_inj_to_nat_seg:
```
```   117   assumes "finite A"
```
```   118   shows "\<exists>f n::nat. f ` A = {i. i < n} \<and> inj_on f A"
```
```   119 proof -
```
```   120   from finite_imp_nat_seg_image_inj_on[OF `finite A`]
```
```   121   obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
```
```   122     by (auto simp:bij_betw_def)
```
```   123   let ?f = "the_inv_into {i. i<n} f"
```
```   124   have "inj_on ?f A & ?f ` A = {i. i<n}"
```
```   125     by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
```
```   126   thus ?thesis by blast
```
```   127 qed
```
```   128
```
```   129 lemma finite_Collect_less_nat [iff]:
```
```   130   "finite {n::nat. n < k}"
```
```   131   by (fastforce simp: finite_conv_nat_seg_image)
```
```   132
```
```   133 lemma finite_Collect_le_nat [iff]:
```
```   134   "finite {n::nat. n \<le> k}"
```
```   135   by (simp add: le_eq_less_or_eq Collect_disj_eq)
```
```   136
```
```   137
```
```   138 subsubsection {* Finiteness and common set operations *}
```
```   139
```
```   140 lemma rev_finite_subset:
```
```   141   "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A"
```
```   142 proof (induct arbitrary: A rule: finite_induct)
```
```   143   case empty
```
```   144   then show ?case by simp
```
```   145 next
```
```   146   case (insert x F A)
```
```   147   have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})" by fact+
```
```   148   show "finite A"
```
```   149   proof cases
```
```   150     assume x: "x \<in> A"
```
```   151     with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
```
```   152     with r have "finite (A - {x})" .
```
```   153     hence "finite (insert x (A - {x}))" ..
```
```   154     also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
```
```   155     finally show ?thesis .
```
```   156   next
```
```   157     show "A \<subseteq> F ==> ?thesis" by fact
```
```   158     assume "x \<notin> A"
```
```   159     with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
```
```   160   qed
```
```   161 qed
```
```   162
```
```   163 lemma finite_subset:
```
```   164   "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
```
```   165   by (rule rev_finite_subset)
```
```   166
```
```   167 lemma finite_UnI:
```
```   168   assumes "finite F" and "finite G"
```
```   169   shows "finite (F \<union> G)"
```
```   170   using assms by induct simp_all
```
```   171
```
```   172 lemma finite_Un [iff]:
```
```   173   "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G"
```
```   174   by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"])
```
```   175
```
```   176 lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A"
```
```   177 proof -
```
```   178   have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
```
```   179   then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
```
```   180   then show ?thesis by simp
```
```   181 qed
```
```   182
```
```   183 lemma finite_Int [simp, intro]:
```
```   184   "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)"
```
```   185   by (blast intro: finite_subset)
```
```   186
```
```   187 lemma finite_Collect_conjI [simp, intro]:
```
```   188   "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
```
```   189   by (simp add: Collect_conj_eq)
```
```   190
```
```   191 lemma finite_Collect_disjI [simp]:
```
```   192   "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
```
```   193   by (simp add: Collect_disj_eq)
```
```   194
```
```   195 lemma finite_Diff [simp, intro]:
```
```   196   "finite A \<Longrightarrow> finite (A - B)"
```
```   197   by (rule finite_subset, rule Diff_subset)
```
```   198
```
```   199 lemma finite_Diff2 [simp]:
```
```   200   assumes "finite B"
```
```   201   shows "finite (A - B) \<longleftrightarrow> finite A"
```
```   202 proof -
```
```   203   have "finite A \<longleftrightarrow> finite((A - B) \<union> (A \<inter> B))" by (simp add: Un_Diff_Int)
```
```   204   also have "\<dots> \<longleftrightarrow> finite (A - B)" using `finite B` by simp
```
```   205   finally show ?thesis ..
```
```   206 qed
```
```   207
```
```   208 lemma finite_Diff_insert [iff]:
```
```   209   "finite (A - insert a B) \<longleftrightarrow> finite (A - B)"
```
```   210 proof -
```
```   211   have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
```
```   212   moreover have "A - insert a B = A - B - {a}" by auto
```
```   213   ultimately show ?thesis by simp
```
```   214 qed
```
```   215
```
```   216 lemma finite_compl[simp]:
```
```   217   "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)"
```
```   218   by (simp add: Compl_eq_Diff_UNIV)
```
```   219
```
```   220 lemma finite_Collect_not[simp]:
```
```   221   "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
```
```   222   by (simp add: Collect_neg_eq)
```
```   223
```
```   224 lemma finite_Union [simp, intro]:
```
```   225   "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite(\<Union>A)"
```
```   226   by (induct rule: finite_induct) simp_all
```
```   227
```
```   228 lemma finite_UN_I [intro]:
```
```   229   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)"
```
```   230   by (induct rule: finite_induct) simp_all
```
```   231
```
```   232 lemma finite_UN [simp]:
```
```   233   "finite A \<Longrightarrow> finite (UNION A B) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))"
```
```   234   by (blast intro: finite_subset)
```
```   235
```
```   236 lemma finite_Inter [intro]:
```
```   237   "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)"
```
```   238   by (blast intro: Inter_lower finite_subset)
```
```   239
```
```   240 lemma finite_INT [intro]:
```
```   241   "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)"
```
```   242   by (blast intro: INT_lower finite_subset)
```
```   243
```
```   244 lemma finite_imageI [simp, intro]:
```
```   245   "finite F \<Longrightarrow> finite (h ` F)"
```
```   246   by (induct rule: finite_induct) simp_all
```
```   247
```
```   248 lemma finite_image_set [simp]:
```
```   249   "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
```
```   250   by (simp add: image_Collect [symmetric])
```
```   251
```
```   252 lemma finite_imageD:
```
```   253   assumes "finite (f ` A)" and "inj_on f A"
```
```   254   shows "finite A"
```
```   255 using assms
```
```   256 proof (induct "f ` A" arbitrary: A)
```
```   257   case empty then show ?case by simp
```
```   258 next
```
```   259   case (insert x B)
```
```   260   then have B_A: "insert x B = f ` A" by simp
```
```   261   then obtain y where "x = f y" and "y \<in> A" by blast
```
```   262   from B_A `x \<notin> B` have "B = f ` A - {x}" by blast
```
```   263   with B_A `x \<notin> B` `x = f y` `inj_on f A` `y \<in> A` have "B = f ` (A - {y})" by (simp add: inj_on_image_set_diff)
```
```   264   moreover from `inj_on f A` have "inj_on f (A - {y})" by (rule inj_on_diff)
```
```   265   ultimately have "finite (A - {y})" by (rule insert.hyps)
```
```   266   then show "finite A" by simp
```
```   267 qed
```
```   268
```
```   269 lemma finite_surj:
```
```   270   "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B"
```
```   271   by (erule finite_subset) (rule finite_imageI)
```
```   272
```
```   273 lemma finite_range_imageI:
```
```   274   "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))"
```
```   275   by (drule finite_imageI) (simp add: range_composition)
```
```   276
```
```   277 lemma finite_subset_image:
```
```   278   assumes "finite B"
```
```   279   shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
```
```   280 using assms
```
```   281 proof induct
```
```   282   case empty then show ?case by simp
```
```   283 next
```
```   284   case insert then show ?case
```
```   285     by (clarsimp simp del: image_insert simp add: image_insert [symmetric])
```
```   286        blast
```
```   287 qed
```
```   288
```
```   289 lemma finite_vimage_IntI:
```
```   290   "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)"
```
```   291   apply (induct rule: finite_induct)
```
```   292    apply simp_all
```
```   293   apply (subst vimage_insert)
```
```   294   apply (simp add: finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2)
```
```   295   done
```
```   296
```
```   297 lemma finite_vimageI:
```
```   298   "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)"
```
```   299   using finite_vimage_IntI[of F h UNIV] by auto
```
```   300
```
```   301 lemma finite_vimageD:
```
```   302   assumes fin: "finite (h -` F)" and surj: "surj h"
```
```   303   shows "finite F"
```
```   304 proof -
```
```   305   have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
```
```   306   also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
```
```   307   finally show "finite F" .
```
```   308 qed
```
```   309
```
```   310 lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
```
```   311   unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
```
```   312
```
```   313 lemma finite_Collect_bex [simp]:
```
```   314   assumes "finite A"
```
```   315   shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
```
```   316 proof -
```
```   317   have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
```
```   318   with assms show ?thesis by simp
```
```   319 qed
```
```   320
```
```   321 lemma finite_Collect_bounded_ex [simp]:
```
```   322   assumes "finite {y. P y}"
```
```   323   shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
```
```   324 proof -
```
```   325   have "{x. EX y. P y & Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})" by auto
```
```   326   with assms show ?thesis by simp
```
```   327 qed
```
```   328
```
```   329 lemma finite_Plus:
```
```   330   "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)"
```
```   331   by (simp add: Plus_def)
```
```   332
```
```   333 lemma finite_PlusD:
```
```   334   fixes A :: "'a set" and B :: "'b set"
```
```   335   assumes fin: "finite (A <+> B)"
```
```   336   shows "finite A" "finite B"
```
```   337 proof -
```
```   338   have "Inl ` A \<subseteq> A <+> B" by auto
```
```   339   then have "finite (Inl ` A :: ('a + 'b) set)" using fin by (rule finite_subset)
```
```   340   then show "finite A" by (rule finite_imageD) (auto intro: inj_onI)
```
```   341 next
```
```   342   have "Inr ` B \<subseteq> A <+> B" by auto
```
```   343   then have "finite (Inr ` B :: ('a + 'b) set)" using fin by (rule finite_subset)
```
```   344   then show "finite B" by (rule finite_imageD) (auto intro: inj_onI)
```
```   345 qed
```
```   346
```
```   347 lemma finite_Plus_iff [simp]:
```
```   348   "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
```
```   349   by (auto intro: finite_PlusD finite_Plus)
```
```   350
```
```   351 lemma finite_Plus_UNIV_iff [simp]:
```
```   352   "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
```
```   353   by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff)
```
```   354
```
```   355 lemma finite_SigmaI [simp, intro]:
```
```   356   "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) ==> finite (SIGMA a:A. B a)"
```
```   357   by (unfold Sigma_def) blast
```
```   358
```
```   359 lemma finite_cartesian_product:
```
```   360   "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)"
```
```   361   by (rule finite_SigmaI)
```
```   362
```
```   363 lemma finite_Prod_UNIV:
```
```   364   "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
```
```   365   by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product)
```
```   366
```
```   367 lemma finite_cartesian_productD1:
```
```   368   assumes "finite (A \<times> B)" and "B \<noteq> {}"
```
```   369   shows "finite A"
```
```   370 proof -
```
```   371   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
```
```   372     by (auto simp add: finite_conv_nat_seg_image)
```
```   373   then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}" by simp
```
```   374   with `B \<noteq> {}` have "A = (fst \<circ> f) ` {i::nat. i < n}"
```
```   375     by (simp add: image_compose)
```
```   376   then have "\<exists>n f. A = f ` {i::nat. i < n}" by blast
```
```   377   then show ?thesis
```
```   378     by (auto simp add: finite_conv_nat_seg_image)
```
```   379 qed
```
```   380
```
```   381 lemma finite_cartesian_productD2:
```
```   382   assumes "finite (A \<times> B)" and "A \<noteq> {}"
```
```   383   shows "finite B"
```
```   384 proof -
```
```   385   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
```
```   386     by (auto simp add: finite_conv_nat_seg_image)
```
```   387   then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}" by simp
```
```   388   with `A \<noteq> {}` have "B = (snd \<circ> f) ` {i::nat. i < n}"
```
```   389     by (simp add: image_compose)
```
```   390   then have "\<exists>n f. B = f ` {i::nat. i < n}" by blast
```
```   391   then show ?thesis
```
```   392     by (auto simp add: finite_conv_nat_seg_image)
```
```   393 qed
```
```   394
```
```   395 lemma finite_prod:
```
```   396   "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
```
```   397 by(auto simp add: UNIV_Times_UNIV[symmetric] simp del: UNIV_Times_UNIV
```
```   398    dest: finite_cartesian_productD1 finite_cartesian_productD2)
```
```   399
```
```   400 lemma finite_Pow_iff [iff]:
```
```   401   "finite (Pow A) \<longleftrightarrow> finite A"
```
```   402 proof
```
```   403   assume "finite (Pow A)"
```
```   404   then have "finite ((%x. {x}) ` A)" by (blast intro: finite_subset)
```
```   405   then show "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
```
```   406 next
```
```   407   assume "finite A"
```
```   408   then show "finite (Pow A)"
```
```   409     by induct (simp_all add: Pow_insert)
```
```   410 qed
```
```   411
```
```   412 corollary finite_Collect_subsets [simp, intro]:
```
```   413   "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
```
```   414   by (simp add: Pow_def [symmetric])
```
```   415
```
```   416 lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)"
```
```   417 by(simp only: finite_Pow_iff Pow_UNIV[symmetric])
```
```   418
```
```   419 lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
```
```   420   by (blast intro: finite_subset [OF subset_Pow_Union])
```
```   421
```
```   422
```
```   423 subsubsection {* Further induction rules on finite sets *}
```
```   424
```
```   425 lemma finite_ne_induct [case_names singleton insert, consumes 2]:
```
```   426   assumes "finite F" and "F \<noteq> {}"
```
```   427   assumes "\<And>x. P {x}"
```
```   428     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
```
```   429   shows "P F"
```
```   430 using assms
```
```   431 proof induct
```
```   432   case empty then show ?case by simp
```
```   433 next
```
```   434   case (insert x F) then show ?case by cases auto
```
```   435 qed
```
```   436
```
```   437 lemma finite_subset_induct [consumes 2, case_names empty insert]:
```
```   438   assumes "finite F" and "F \<subseteq> A"
```
```   439   assumes empty: "P {}"
```
```   440     and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)"
```
```   441   shows "P F"
```
```   442 using `finite F` `F \<subseteq> A`
```
```   443 proof induct
```
```   444   show "P {}" by fact
```
```   445 next
```
```   446   fix x F
```
```   447   assume "finite F" and "x \<notin> F" and
```
```   448     P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
```
```   449   show "P (insert x F)"
```
```   450   proof (rule insert)
```
```   451     from i show "x \<in> A" by blast
```
```   452     from i have "F \<subseteq> A" by blast
```
```   453     with P show "P F" .
```
```   454     show "finite F" by fact
```
```   455     show "x \<notin> F" by fact
```
```   456   qed
```
```   457 qed
```
```   458
```
```   459 lemma finite_empty_induct:
```
```   460   assumes "finite A"
```
```   461   assumes "P A"
```
```   462     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
```
```   463   shows "P {}"
```
```   464 proof -
```
```   465   have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
```
```   466   proof -
```
```   467     fix B :: "'a set"
```
```   468     assume "B \<subseteq> A"
```
```   469     with `finite A` have "finite B" by (rule rev_finite_subset)
```
```   470     from this `B \<subseteq> A` show "P (A - B)"
```
```   471     proof induct
```
```   472       case empty
```
```   473       from `P A` show ?case by simp
```
```   474     next
```
```   475       case (insert b B)
```
```   476       have "P (A - B - {b})"
```
```   477       proof (rule remove)
```
```   478         from `finite A` show "finite (A - B)" by induct auto
```
```   479         from insert show "b \<in> A - B" by simp
```
```   480         from insert show "P (A - B)" by simp
```
```   481       qed
```
```   482       also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
```
```   483       finally show ?case .
```
```   484     qed
```
```   485   qed
```
```   486   then have "P (A - A)" by blast
```
```   487   then show ?thesis by simp
```
```   488 qed
```
```   489
```
```   490
```
```   491 subsection {* Class @{text finite}  *}
```
```   492
```
```   493 class finite =
```
```   494   assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
```
```   495 begin
```
```   496
```
```   497 lemma finite [simp]: "finite (A \<Colon> 'a set)"
```
```   498   by (rule subset_UNIV finite_UNIV finite_subset)+
```
```   499
```
```   500 lemma finite_code [code]: "finite (A \<Colon> 'a set) \<longleftrightarrow> True"
```
```   501   by simp
```
```   502
```
```   503 end
```
```   504
```
```   505 instance prod :: (finite, finite) finite
```
```   506   by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
```
```   507
```
```   508 lemma inj_graph: "inj (%f. {(x, y). y = f x})"
```
```   509   by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
```
```   510
```
```   511 instance "fun" :: (finite, finite) finite
```
```   512 proof
```
```   513   show "finite (UNIV :: ('a => 'b) set)"
```
```   514   proof (rule finite_imageD)
```
```   515     let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
```
```   516     have "range ?graph \<subseteq> Pow UNIV" by simp
```
```   517     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
```
```   518       by (simp only: finite_Pow_iff finite)
```
```   519     ultimately show "finite (range ?graph)"
```
```   520       by (rule finite_subset)
```
```   521     show "inj ?graph" by (rule inj_graph)
```
```   522   qed
```
```   523 qed
```
```   524
```
```   525 instance bool :: finite
```
```   526   by default (simp add: UNIV_bool)
```
```   527
```
```   528 instance set :: (finite) finite
```
```   529   by default (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
```
```   530
```
```   531 instance unit :: finite
```
```   532   by default (simp add: UNIV_unit)
```
```   533
```
```   534 instance sum :: (finite, finite) finite
```
```   535   by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
```
```   536
```
```   537 lemma finite_option_UNIV [simp]:
```
```   538   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
```
```   539   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
```
```   540
```
```   541 instance option :: (finite) finite
```
```   542   by default (simp add: UNIV_option_conv)
```
```   543
```
```   544
```
```   545 subsection {* A basic fold functional for finite sets *}
```
```   546
```
```   547 text {* The intended behaviour is
```
```   548 @{text "fold f z {x\<^isub>1, ..., x\<^isub>n} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
```
```   549 if @{text f} is ``left-commutative'':
```
```   550 *}
```
```   551
```
```   552 locale comp_fun_commute =
```
```   553   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
```
```   554   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
```
```   555 begin
```
```   556
```
```   557 lemma fun_left_comm: "f x (f y z) = f y (f x z)"
```
```   558   using comp_fun_commute by (simp add: fun_eq_iff)
```
```   559
```
```   560 end
```
```   561
```
```   562 inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
```
```   563 for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
```
```   564   emptyI [intro]: "fold_graph f z {} z" |
```
```   565   insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
```
```   566       \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
```
```   567
```
```   568 inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
```
```   569
```
```   570 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
```
```   571   "fold f z A = (THE y. fold_graph f z A y)"
```
```   572
```
```   573 text{*A tempting alternative for the definiens is
```
```   574 @{term "if finite A then THE y. fold_graph f z A y else e"}.
```
```   575 It allows the removal of finiteness assumptions from the theorems
```
```   576 @{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
```
```   577 The proofs become ugly. It is not worth the effort. (???) *}
```
```   578
```
```   579 lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
```
```   580 by (induct rule: finite_induct) auto
```
```   581
```
```   582
```
```   583 subsubsection{*From @{const fold_graph} to @{term fold}*}
```
```   584
```
```   585 context comp_fun_commute
```
```   586 begin
```
```   587
```
```   588 lemma fold_graph_insertE_aux:
```
```   589   "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'"
```
```   590 proof (induct set: fold_graph)
```
```   591   case (insertI x A y) show ?case
```
```   592   proof (cases "x = a")
```
```   593     assume "x = a" with insertI show ?case by auto
```
```   594   next
```
```   595     assume "x \<noteq> a"
```
```   596     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
```
```   597       using insertI by auto
```
```   598     have "f x y = f a (f x y')"
```
```   599       unfolding y by (rule fun_left_comm)
```
```   600     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
```
```   601       using y' and `x \<noteq> a` and `x \<notin> A`
```
```   602       by (simp add: insert_Diff_if fold_graph.insertI)
```
```   603     ultimately show ?case by fast
```
```   604   qed
```
```   605 qed simp
```
```   606
```
```   607 lemma fold_graph_insertE:
```
```   608   assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
```
```   609   obtains y where "v = f x y" and "fold_graph f z A y"
```
```   610 using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
```
```   611
```
```   612 lemma fold_graph_determ:
```
```   613   "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
```
```   614 proof (induct arbitrary: y set: fold_graph)
```
```   615   case (insertI x A y v)
```
```   616   from `fold_graph f z (insert x A) v` and `x \<notin> A`
```
```   617   obtain y' where "v = f x y'" and "fold_graph f z A y'"
```
```   618     by (rule fold_graph_insertE)
```
```   619   from `fold_graph f z A y'` have "y' = y" by (rule insertI)
```
```   620   with `v = f x y'` show "v = f x y" by simp
```
```   621 qed fast
```
```   622
```
```   623 lemma fold_equality:
```
```   624   "fold_graph f z A y \<Longrightarrow> fold f z A = y"
```
```   625 by (unfold fold_def) (blast intro: fold_graph_determ)
```
```   626
```
```   627 lemma fold_graph_fold:
```
```   628   assumes "finite A"
```
```   629   shows "fold_graph f z A (fold f z A)"
```
```   630 proof -
```
```   631   from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
```
```   632   moreover note fold_graph_determ
```
```   633   ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
```
```   634   then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
```
```   635   then show ?thesis by (unfold fold_def)
```
```   636 qed
```
```   637
```
```   638 text{* The base case for @{text fold}: *}
```
```   639
```
```   640 lemma (in -) fold_empty [simp]: "fold f z {} = z"
```
```   641 by (unfold fold_def) blast
```
```   642
```
```   643 text{* The various recursion equations for @{const fold}: *}
```
```   644
```
```   645 lemma fold_insert [simp]:
```
```   646   assumes "finite A" and "x \<notin> A"
```
```   647   shows "fold f z (insert x A) = f x (fold f z A)"
```
```   648 proof (rule fold_equality)
```
```   649   from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
```
```   650   with `x \<notin> A`show "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
```
```   651 qed
```
```   652
```
```   653 lemma fold_fun_comm:
```
```   654   "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
```
```   655 proof (induct rule: finite_induct)
```
```   656   case empty then show ?case by simp
```
```   657 next
```
```   658   case (insert y A) then show ?case
```
```   659     by (simp add: fun_left_comm[of x])
```
```   660 qed
```
```   661
```
```   662 lemma fold_insert2:
```
```   663   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
```
```   664 by (simp add: fold_fun_comm)
```
```   665
```
```   666 lemma fold_rec:
```
```   667   assumes "finite A" and "x \<in> A"
```
```   668   shows "fold f z A = f x (fold f z (A - {x}))"
```
```   669 proof -
```
```   670   have A: "A = insert x (A - {x})" using `x \<in> A` by blast
```
```   671   then have "fold f z A = fold f z (insert x (A - {x}))" by simp
```
```   672   also have "\<dots> = f x (fold f z (A - {x}))"
```
```   673     by (rule fold_insert) (simp add: `finite A`)+
```
```   674   finally show ?thesis .
```
```   675 qed
```
```   676
```
```   677 lemma fold_insert_remove:
```
```   678   assumes "finite A"
```
```   679   shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
```
```   680 proof -
```
```   681   from `finite A` have "finite (insert x A)" by auto
```
```   682   moreover have "x \<in> insert x A" by auto
```
```   683   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
```
```   684     by (rule fold_rec)
```
```   685   then show ?thesis by simp
```
```   686 qed
```
```   687
```
```   688 text{* Other properties of @{const fold}: *}
```
```   689
```
```   690 lemma fold_image:
```
```   691   assumes "finite A" and "inj_on g A"
```
```   692   shows "fold f x (g ` A) = fold (f \<circ> g) x A"
```
```   693 using assms
```
```   694 proof induction
```
```   695   case (insert a F)
```
```   696     interpret comp_fun_commute "\<lambda>x. f (g x)" by default (simp add: comp_fun_commute)
```
```   697     from insert show ?case by auto
```
```   698 qed (simp)
```
```   699
```
```   700 end
```
```   701
```
```   702 lemma fold_cong:
```
```   703   assumes "comp_fun_commute f" "comp_fun_commute g"
```
```   704   assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
```
```   705     and "A = B" and "s = t"
```
```   706   shows "Finite_Set.fold f s A = Finite_Set.fold g t B"
```
```   707 proof -
```
```   708   have "Finite_Set.fold f s A = Finite_Set.fold g s A"
```
```   709   using `finite A` cong proof (induct A)
```
```   710     case empty then show ?case by simp
```
```   711   next
```
```   712     case (insert x A)
```
```   713     interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
```
```   714     interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
```
```   715     from insert show ?case by simp
```
```   716   qed
```
```   717   with assms show ?thesis by simp
```
```   718 qed
```
```   719
```
```   720
```
```   721 text{* A simplified version for idempotent functions: *}
```
```   722
```
```   723 locale comp_fun_idem = comp_fun_commute +
```
```   724   assumes comp_fun_idem: "f x o f x = f x"
```
```   725 begin
```
```   726
```
```   727 lemma fun_left_idem: "f x (f x z) = f x z"
```
```   728   using comp_fun_idem by (simp add: fun_eq_iff)
```
```   729
```
```   730 lemma fold_insert_idem:
```
```   731   assumes fin: "finite A"
```
```   732   shows "fold f z (insert x A) = f x (fold f z A)"
```
```   733 proof cases
```
```   734   assume "x \<in> A"
```
```   735   then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
```
```   736   then show ?thesis using assms by (simp add:fun_left_idem)
```
```   737 next
```
```   738   assume "x \<notin> A" then show ?thesis using assms by simp
```
```   739 qed
```
```   740
```
```   741 declare fold_insert[simp del] fold_insert_idem[simp]
```
```   742
```
```   743 lemma fold_insert_idem2:
```
```   744   "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
```
```   745 by(simp add:fold_fun_comm)
```
```   746
```
```   747 end
```
```   748
```
```   749
```
```   750 subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
```
```   751
```
```   752 lemma (in comp_fun_commute) comp_comp_fun_commute:
```
```   753   "comp_fun_commute (f \<circ> g)"
```
```   754 proof
```
```   755 qed (simp_all add: comp_fun_commute)
```
```   756
```
```   757 lemma (in comp_fun_idem) comp_comp_fun_idem:
```
```   758   "comp_fun_idem (f \<circ> g)"
```
```   759   by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
```
```   760     (simp_all add: comp_fun_idem)
```
```   761
```
```   762 lemma (in comp_fun_commute) comp_fun_commute_funpow:
```
```   763   "comp_fun_commute (\<lambda>x. f x ^^ g x)"
```
```   764 proof
```
```   765   fix y x
```
```   766   show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
```
```   767   proof (cases "x = y")
```
```   768     case True then show ?thesis by simp
```
```   769   next
```
```   770     case False show ?thesis
```
```   771     proof (induct "g x" arbitrary: g)
```
```   772       case 0 then show ?case by simp
```
```   773     next
```
```   774       case (Suc n g)
```
```   775       have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
```
```   776       proof (induct "g y" arbitrary: g)
```
```   777         case 0 then show ?case by simp
```
```   778       next
```
```   779         case (Suc n g)
```
```   780         def h \<equiv> "\<lambda>z. g z - 1"
```
```   781         with Suc have "n = h y" by simp
```
```   782         with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
```
```   783           by auto
```
```   784         from Suc h_def have "g y = Suc (h y)" by simp
```
```   785         then show ?case by (simp add: comp_assoc hyp)
```
```   786           (simp add: o_assoc comp_fun_commute)
```
```   787       qed
```
```   788       def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
```
```   789       with Suc have "n = h x" by simp
```
```   790       with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
```
```   791         by auto
```
```   792       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
```
```   793       from Suc h_def have "g x = Suc (h x)" by simp
```
```   794       then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
```
```   795         (simp add: comp_assoc hyp1)
```
```   796     qed
```
```   797   qed
```
```   798 qed
```
```   799
```
```   800
```
```   801 subsubsection {* Expressing set operations via @{const fold} *}
```
```   802
```
```   803 lemma comp_fun_idem_insert:
```
```   804   "comp_fun_idem insert"
```
```   805 proof
```
```   806 qed auto
```
```   807
```
```   808 lemma comp_fun_idem_remove:
```
```   809   "comp_fun_idem Set.remove"
```
```   810 proof
```
```   811 qed auto
```
```   812
```
```   813 lemma (in semilattice_inf) comp_fun_idem_inf:
```
```   814   "comp_fun_idem inf"
```
```   815 proof
```
```   816 qed (auto simp add: inf_left_commute)
```
```   817
```
```   818 lemma (in semilattice_sup) comp_fun_idem_sup:
```
```   819   "comp_fun_idem sup"
```
```   820 proof
```
```   821 qed (auto simp add: sup_left_commute)
```
```   822
```
```   823 lemma union_fold_insert:
```
```   824   assumes "finite A"
```
```   825   shows "A \<union> B = fold insert B A"
```
```   826 proof -
```
```   827   interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
```
```   828   from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
```
```   829 qed
```
```   830
```
```   831 lemma minus_fold_remove:
```
```   832   assumes "finite A"
```
```   833   shows "B - A = fold Set.remove B A"
```
```   834 proof -
```
```   835   interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
```
```   836   from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
```
```   837   then show ?thesis ..
```
```   838 qed
```
```   839
```
```   840 lemma comp_fun_commute_filter_fold: "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
```
```   841 proof -
```
```   842   interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
```
```   843   show ?thesis by default (auto simp: fun_eq_iff)
```
```   844 qed
```
```   845
```
```   846 lemma Set_filter_fold:
```
```   847   assumes "finite A"
```
```   848   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
```
```   849 using assms
```
```   850 by (induct A)
```
```   851   (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
```
```   852
```
```   853 lemma inter_Set_filter:
```
```   854   assumes "finite B"
```
```   855   shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
```
```   856 using assms
```
```   857 by (induct B) (auto simp: Set.filter_def)
```
```   858
```
```   859 lemma image_fold_insert:
```
```   860   assumes "finite A"
```
```   861   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
```
```   862 using assms
```
```   863 proof -
```
```   864   interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
```
```   865   show ?thesis using assms by (induct A) auto
```
```   866 qed
```
```   867
```
```   868 lemma Ball_fold:
```
```   869   assumes "finite A"
```
```   870   shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
```
```   871 using assms
```
```   872 proof -
```
```   873   interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
```
```   874   show ?thesis using assms by (induct A) auto
```
```   875 qed
```
```   876
```
```   877 lemma Bex_fold:
```
```   878   assumes "finite A"
```
```   879   shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
```
```   880 using assms
```
```   881 proof -
```
```   882   interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
```
```   883   show ?thesis using assms by (induct A) auto
```
```   884 qed
```
```   885
```
```   886 lemma comp_fun_commute_Pow_fold:
```
```   887   "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)"
```
```   888   by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
```
```   889
```
```   890 lemma Pow_fold:
```
```   891   assumes "finite A"
```
```   892   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
```
```   893 using assms
```
```   894 proof -
```
```   895   interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
```
```   896   show ?thesis using assms by (induct A) (auto simp: Pow_insert)
```
```   897 qed
```
```   898
```
```   899 lemma fold_union_pair:
```
```   900   assumes "finite B"
```
```   901   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
```
```   902 proof -
```
```   903   interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
```
```   904   show ?thesis using assms  by (induct B arbitrary: A) simp_all
```
```   905 qed
```
```   906
```
```   907 lemma comp_fun_commute_product_fold:
```
```   908   assumes "finite B"
```
```   909   shows "comp_fun_commute (\<lambda>x A. fold (\<lambda>y. Set.insert (x, y)) A B)"
```
```   910 by default (auto simp: fold_union_pair[symmetric] assms)
```
```   911
```
```   912 lemma product_fold:
```
```   913   assumes "finite A"
```
```   914   assumes "finite B"
```
```   915   shows "A \<times> B = fold (\<lambda>x A. fold (\<lambda>y. Set.insert (x, y)) A B) {} A"
```
```   916 using assms unfolding Sigma_def
```
```   917 by (induct A)
```
```   918   (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
```
```   919
```
```   920
```
```   921 context complete_lattice
```
```   922 begin
```
```   923
```
```   924 lemma inf_Inf_fold_inf:
```
```   925   assumes "finite A"
```
```   926   shows "inf B (Inf A) = fold inf B A"
```
```   927 proof -
```
```   928   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
```
```   929   from `finite A` show ?thesis by (induct A arbitrary: B)
```
```   930     (simp_all add: inf_commute fold_fun_comm)
```
```   931 qed
```
```   932
```
```   933 lemma sup_Sup_fold_sup:
```
```   934   assumes "finite A"
```
```   935   shows "sup B (Sup A) = fold sup B A"
```
```   936 proof -
```
```   937   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
```
```   938   from `finite A` show ?thesis by (induct A arbitrary: B)
```
```   939     (simp_all add: sup_commute fold_fun_comm)
```
```   940 qed
```
```   941
```
```   942 lemma Inf_fold_inf:
```
```   943   assumes "finite A"
```
```   944   shows "Inf A = fold inf top A"
```
```   945   using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
```
```   946
```
```   947 lemma Sup_fold_sup:
```
```   948   assumes "finite A"
```
```   949   shows "Sup A = fold sup bot A"
```
```   950   using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
```
```   951
```
```   952 lemma inf_INF_fold_inf:
```
```   953   assumes "finite A"
```
```   954   shows "inf B (INFI A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold")
```
```   955 proof (rule sym)
```
```   956   interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
```
```   957   interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
```
```   958   from `finite A` show "?fold = ?inf"
```
```   959     by (induct A arbitrary: B)
```
```   960       (simp_all add: INF_def inf_left_commute)
```
```   961 qed
```
```   962
```
```   963 lemma sup_SUP_fold_sup:
```
```   964   assumes "finite A"
```
```   965   shows "sup B (SUPR A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold")
```
```   966 proof (rule sym)
```
```   967   interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
```
```   968   interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
```
```   969   from `finite A` show "?fold = ?sup"
```
```   970     by (induct A arbitrary: B)
```
```   971       (simp_all add: SUP_def sup_left_commute)
```
```   972 qed
```
```   973
```
```   974 lemma INF_fold_inf:
```
```   975   assumes "finite A"
```
```   976   shows "INFI A f = fold (inf \<circ> f) top A"
```
```   977   using assms inf_INF_fold_inf [of A top] by simp
```
```   978
```
```   979 lemma SUP_fold_sup:
```
```   980   assumes "finite A"
```
```   981   shows "SUPR A f = fold (sup \<circ> f) bot A"
```
```   982   using assms sup_SUP_fold_sup [of A bot] by simp
```
```   983
```
```   984 end
```
```   985
```
```   986
```
```   987 subsection {* The derived combinator @{text fold_image} *}
```
```   988
```
```   989 definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
```
```   990   where "fold_image f g = fold (\<lambda>x y. f (g x) y)"
```
```   991
```
```   992 lemma fold_image_empty[simp]: "fold_image f g z {} = z"
```
```   993   by (simp add:fold_image_def)
```
```   994
```
```   995 context ab_semigroup_mult
```
```   996 begin
```
```   997
```
```   998 lemma fold_image_insert[simp]:
```
```   999   assumes "finite A" and "a \<notin> A"
```
```  1000   shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
```
```  1001 proof -
```
```  1002   interpret comp_fun_commute "%x y. (g x) * y"
```
```  1003     by default (simp add: fun_eq_iff mult_ac)
```
```  1004   from assms show ?thesis by (simp add: fold_image_def)
```
```  1005 qed
```
```  1006
```
```  1007 lemma fold_image_reindex:
```
```  1008   assumes "finite A"
```
```  1009   shows "inj_on h A \<Longrightarrow> fold_image times g z (h ` A) = fold_image times (g \<circ> h) z A"
```
```  1010   using assms by induct auto
```
```  1011
```
```  1012 lemma fold_image_cong:
```
```  1013   assumes "finite A" and g_h: "\<And>x. x\<in>A \<Longrightarrow> g x = h x"
```
```  1014   shows "fold_image times g z A = fold_image times h z A"
```
```  1015 proof -
```
```  1016   from `finite A`
```
```  1017   have "\<And>C. C <= A --> (ALL x:C. g x = h x) --> fold_image times g z C = fold_image times h z C"
```
```  1018   proof (induct arbitrary: C)
```
```  1019     case empty then show ?case by simp
```
```  1020   next
```
```  1021     case (insert x F) then show ?case apply -
```
```  1022     apply (simp add: subset_insert_iff, clarify)
```
```  1023     apply (subgoal_tac "finite C")
```
```  1024       prefer 2 apply (blast dest: finite_subset [rotated])
```
```  1025     apply (subgoal_tac "C = insert x (C - {x})")
```
```  1026       prefer 2 apply blast
```
```  1027     apply (erule ssubst)
```
```  1028     apply (simp add: Ball_def del: insert_Diff_single)
```
```  1029     done
```
```  1030   qed
```
```  1031   with g_h show ?thesis by simp
```
```  1032 qed
```
```  1033
```
```  1034 end
```
```  1035
```
```  1036 context comm_monoid_mult
```
```  1037 begin
```
```  1038
```
```  1039 lemma fold_image_1:
```
```  1040   "finite S \<Longrightarrow> (\<forall>x\<in>S. f x = 1) \<Longrightarrow> fold_image op * f 1 S = 1"
```
```  1041   apply (induct rule: finite_induct)
```
```  1042   apply simp by auto
```
```  1043
```
```  1044 lemma fold_image_Un_Int:
```
```  1045   "finite A ==> finite B ==>
```
```  1046     fold_image times g 1 A * fold_image times g 1 B =
```
```  1047     fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
```
```  1048   apply (induct rule: finite_induct)
```
```  1049 by (induct set: finite)
```
```  1050    (auto simp add: mult_ac insert_absorb Int_insert_left)
```
```  1051
```
```  1052 lemma fold_image_Un_one:
```
```  1053   assumes fS: "finite S" and fT: "finite T"
```
```  1054   and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
```
```  1055   shows "fold_image (op *) f 1 (S \<union> T) = fold_image (op *) f 1 S * fold_image (op *) f 1 T"
```
```  1056 proof-
```
```  1057   have "fold_image op * f 1 (S \<inter> T) = 1"
```
```  1058     apply (rule fold_image_1)
```
```  1059     using fS fT I0 by auto
```
```  1060   with fold_image_Un_Int[OF fS fT] show ?thesis by simp
```
```  1061 qed
```
```  1062
```
```  1063 corollary fold_Un_disjoint:
```
```  1064   "finite A ==> finite B ==> A Int B = {} ==>
```
```  1065    fold_image times g 1 (A Un B) =
```
```  1066    fold_image times g 1 A * fold_image times g 1 B"
```
```  1067 by (simp add: fold_image_Un_Int)
```
```  1068
```
```  1069 lemma fold_image_UN_disjoint:
```
```  1070   "\<lbrakk> finite I; ALL i:I. finite (A i);
```
```  1071      ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
```
```  1072    \<Longrightarrow> fold_image times g 1 (UNION I A) =
```
```  1073        fold_image times (%i. fold_image times g 1 (A i)) 1 I"
```
```  1074 apply (induct rule: finite_induct)
```
```  1075 apply simp
```
```  1076 apply atomize
```
```  1077 apply (subgoal_tac "ALL i:F. x \<noteq> i")
```
```  1078  prefer 2 apply blast
```
```  1079 apply (subgoal_tac "A x Int UNION F A = {}")
```
```  1080  prefer 2 apply blast
```
```  1081 apply (simp add: fold_Un_disjoint)
```
```  1082 done
```
```  1083
```
```  1084 lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
```
```  1085   fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
```
```  1086   fold_image times (split g) 1 (SIGMA x:A. B x)"
```
```  1087 apply (subst Sigma_def)
```
```  1088 apply (subst fold_image_UN_disjoint, assumption, simp)
```
```  1089  apply blast
```
```  1090 apply (erule fold_image_cong)
```
```  1091 apply (subst fold_image_UN_disjoint, simp, simp)
```
```  1092  apply blast
```
```  1093 apply simp
```
```  1094 done
```
```  1095
```
```  1096 lemma fold_image_distrib: "finite A \<Longrightarrow>
```
```  1097    fold_image times (%x. g x * h x) 1 A =
```
```  1098    fold_image times g 1 A *  fold_image times h 1 A"
```
```  1099 by (erule finite_induct) (simp_all add: mult_ac)
```
```  1100
```
```  1101 lemma fold_image_related:
```
```  1102   assumes Re: "R e e"
```
```  1103   and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)"
```
```  1104   and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
```
```  1105   shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
```
```  1106   using fS by (rule finite_subset_induct) (insert assms, auto)
```
```  1107
```
```  1108 lemma  fold_image_eq_general:
```
```  1109   assumes fS: "finite S"
```
```  1110   and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y"
```
```  1111   and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
```
```  1112   shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
```
```  1113 proof-
```
```  1114   from h f12 have hS: "h ` S = S'" by auto
```
```  1115   {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
```
```  1116     from f12 h H  have "x = y" by auto }
```
```  1117   hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
```
```  1118   from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto
```
```  1119   from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
```
```  1120   also have "\<dots> = fold_image (op *) (f2 o h) e S"
```
```  1121     using fold_image_reindex[OF fS hinj, of f2 e] .
```
```  1122   also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
```
```  1123     by blast
```
```  1124   finally show ?thesis ..
```
```  1125 qed
```
```  1126
```
```  1127 lemma fold_image_eq_general_inverses:
```
```  1128   assumes fS: "finite S"
```
```  1129   and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
```
```  1130   and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
```
```  1131   shows "fold_image (op *) f e S = fold_image (op *) g e T"
```
```  1132   (* metis solves it, but not yet available here *)
```
```  1133   apply (rule fold_image_eq_general[OF fS, of T h g f e])
```
```  1134   apply (rule ballI)
```
```  1135   apply (frule kh)
```
```  1136   apply (rule ex1I[])
```
```  1137   apply blast
```
```  1138   apply clarsimp
```
```  1139   apply (drule hk) apply simp
```
```  1140   apply (rule sym)
```
```  1141   apply (erule conjunct1[OF conjunct2[OF hk]])
```
```  1142   apply (rule ballI)
```
```  1143   apply (drule  hk)
```
```  1144   apply blast
```
```  1145   done
```
```  1146
```
```  1147 end
```
```  1148
```
```  1149
```
```  1150 subsection {* A fold functional for non-empty sets *}
```
```  1151
```
```  1152 text{* Does not require start value. *}
```
```  1153
```
```  1154 inductive
```
```  1155   fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
```
```  1156   for f :: "'a => 'a => 'a"
```
```  1157 where
```
```  1158   fold1Set_insertI [intro]:
```
```  1159    "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
```
```  1160
```
```  1161 definition fold1 :: "('a => 'a => 'a) => 'a set => 'a" where
```
```  1162   "fold1 f A == THE x. fold1Set f A x"
```
```  1163
```
```  1164 lemma fold1Set_nonempty:
```
```  1165   "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
```
```  1166 by(erule fold1Set.cases, simp_all)
```
```  1167
```
```  1168 inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
```
```  1169
```
```  1170 inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
```
```  1171
```
```  1172
```
```  1173 lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
```
```  1174 by (blast elim: fold_graph.cases)
```
```  1175
```
```  1176 lemma fold1_singleton [simp]: "fold1 f {a} = a"
```
```  1177 by (unfold fold1_def) blast
```
```  1178
```
```  1179 lemma finite_nonempty_imp_fold1Set:
```
```  1180   "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
```
```  1181 apply (induct A rule: finite_induct)
```
```  1182 apply (auto dest: finite_imp_fold_graph [of _ f])
```
```  1183 done
```
```  1184
```
```  1185 text{*First, some lemmas about @{const fold_graph}.*}
```
```  1186
```
```  1187 context ab_semigroup_mult
```
```  1188 begin
```
```  1189
```
```  1190 lemma comp_fun_commute: "comp_fun_commute (op *)"
```
```  1191   by default (simp add: fun_eq_iff mult_ac)
```
```  1192
```
```  1193 lemma fold_graph_insert_swap:
```
```  1194 assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
```
```  1195 shows "fold_graph times z (insert b A) (z * y)"
```
```  1196 proof -
```
```  1197   interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
```
```  1198 from assms show ?thesis
```
```  1199 proof (induct rule: fold_graph.induct)
```
```  1200   case emptyI show ?case by (subst mult_commute [of z b], fast)
```
```  1201 next
```
```  1202   case (insertI x A y)
```
```  1203     have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
```
```  1204       using insertI by force  --{*how does @{term id} get unfolded?*}
```
```  1205     thus ?case by (simp add: insert_commute mult_ac)
```
```  1206 qed
```
```  1207 qed
```
```  1208
```
```  1209 lemma fold_graph_permute_diff:
```
```  1210 assumes fold: "fold_graph times b A x"
```
```  1211 shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
```
```  1212 using fold
```
```  1213 proof (induct rule: fold_graph.induct)
```
```  1214   case emptyI thus ?case by simp
```
```  1215 next
```
```  1216   case (insertI x A y)
```
```  1217   have "a = x \<or> a \<in> A" using insertI by simp
```
```  1218   thus ?case
```
```  1219   proof
```
```  1220     assume "a = x"
```
```  1221     with insertI show ?thesis
```
```  1222       by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
```
```  1223   next
```
```  1224     assume ainA: "a \<in> A"
```
```  1225     hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
```
```  1226       using insertI by force
```
```  1227     moreover
```
```  1228     have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
```
```  1229       using ainA insertI by blast
```
```  1230     ultimately show ?thesis by simp
```
```  1231   qed
```
```  1232 qed
```
```  1233
```
```  1234 lemma fold1_eq_fold:
```
```  1235 assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
```
```  1236 proof -
```
```  1237   interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
```
```  1238   from assms show ?thesis
```
```  1239 apply (simp add: fold1_def fold_def)
```
```  1240 apply (rule the_equality)
```
```  1241 apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
```
```  1242 apply (rule sym, clarify)
```
```  1243 apply (case_tac "Aa=A")
```
```  1244  apply (best intro: fold_graph_determ)
```
```  1245 apply (subgoal_tac "fold_graph times a A x")
```
```  1246  apply (best intro: fold_graph_determ)
```
```  1247 apply (subgoal_tac "insert aa (Aa - {a}) = A")
```
```  1248  prefer 2 apply (blast elim: equalityE)
```
```  1249 apply (auto dest: fold_graph_permute_diff [where a=a])
```
```  1250 done
```
```  1251 qed
```
```  1252
```
```  1253 lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
```
```  1254 apply safe
```
```  1255  apply simp
```
```  1256  apply (drule_tac x=x in spec)
```
```  1257  apply (drule_tac x="A-{x}" in spec, auto)
```
```  1258 done
```
```  1259
```
```  1260 lemma fold1_insert:
```
```  1261   assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
```
```  1262   shows "fold1 times (insert x A) = x * fold1 times A"
```
```  1263 proof -
```
```  1264   interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
```
```  1265   from nonempty obtain a A' where "A = insert a A' & a ~: A'"
```
```  1266     by (auto simp add: nonempty_iff)
```
```  1267   with A show ?thesis
```
```  1268     by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
```
```  1269 qed
```
```  1270
```
```  1271 end
```
```  1272
```
```  1273 context ab_semigroup_idem_mult
```
```  1274 begin
```
```  1275
```
```  1276 lemma comp_fun_idem: "comp_fun_idem (op *)"
```
```  1277   by default (simp_all add: fun_eq_iff mult_left_commute)
```
```  1278
```
```  1279 lemma fold1_insert_idem [simp]:
```
```  1280   assumes nonempty: "A \<noteq> {}" and A: "finite A"
```
```  1281   shows "fold1 times (insert x A) = x * fold1 times A"
```
```  1282 proof -
```
```  1283   interpret comp_fun_idem "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a"
```
```  1284     by (rule comp_fun_idem)
```
```  1285   from nonempty obtain a A' where A': "A = insert a A' & a ~: A'"
```
```  1286     by (auto simp add: nonempty_iff)
```
```  1287   show ?thesis
```
```  1288   proof cases
```
```  1289     assume a: "a = x"
```
```  1290     show ?thesis
```
```  1291     proof cases
```
```  1292       assume "A' = {}"
```
```  1293       with A' a show ?thesis by simp
```
```  1294     next
```
```  1295       assume "A' \<noteq> {}"
```
```  1296       with A A' a show ?thesis
```
```  1297         by (simp add: fold1_insert mult_assoc [symmetric])
```
```  1298     qed
```
```  1299   next
```
```  1300     assume "a \<noteq> x"
```
```  1301     with A A' show ?thesis
```
```  1302       by (simp add: insert_commute fold1_eq_fold)
```
```  1303   qed
```
```  1304 qed
```
```  1305
```
```  1306 lemma hom_fold1_commute:
```
```  1307 assumes hom: "!!x y. h (x * y) = h x * h y"
```
```  1308 and N: "finite N" "N \<noteq> {}" shows "h (fold1 times N) = fold1 times (h ` N)"
```
```  1309 using N
```
```  1310 proof (induct rule: finite_ne_induct)
```
```  1311   case singleton thus ?case by simp
```
```  1312 next
```
```  1313   case (insert n N)
```
```  1314   then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp
```
```  1315   also have "\<dots> = h n * h (fold1 times N)" by(rule hom)
```
```  1316   also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert)
```
```  1317   also have "times (h n) \<dots> = fold1 times (insert (h n) (h ` N))"
```
```  1318     using insert by(simp)
```
```  1319   also have "insert (h n) (h ` N) = h ` insert n N" by simp
```
```  1320   finally show ?case .
```
```  1321 qed
```
```  1322
```
```  1323 lemma fold1_eq_fold_idem:
```
```  1324   assumes "finite A"
```
```  1325   shows "fold1 times (insert a A) = fold times a A"
```
```  1326 proof (cases "a \<in> A")
```
```  1327   case False
```
```  1328   with assms show ?thesis by (simp add: fold1_eq_fold)
```
```  1329 next
```
```  1330   interpret comp_fun_idem times by (fact comp_fun_idem)
```
```  1331   case True then obtain b B
```
```  1332     where A: "A = insert a B" and "a \<notin> B" by (rule set_insert)
```
```  1333   with assms have "finite B" by auto
```
```  1334   then have "fold times a (insert a B) = fold times (a * a) B"
```
```  1335     using `a \<notin> B` by (rule fold_insert2)
```
```  1336   then show ?thesis
```
```  1337     using `a \<notin> B` `finite B` by (simp add: fold1_eq_fold A)
```
```  1338 qed
```
```  1339
```
```  1340 end
```
```  1341
```
```  1342
```
```  1343 text{* Now the recursion rules for definitions: *}
```
```  1344
```
```  1345 lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
```
```  1346 by simp
```
```  1347
```
```  1348 lemma (in ab_semigroup_mult) fold1_insert_def:
```
```  1349   "\<lbrakk> g = fold1 times; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
```
```  1350 by (simp add:fold1_insert)
```
```  1351
```
```  1352 lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def:
```
```  1353   "\<lbrakk> g = fold1 times; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
```
```  1354 by simp
```
```  1355
```
```  1356 subsubsection{* Determinacy for @{term fold1Set} *}
```
```  1357
```
```  1358 (*Not actually used!!*)
```
```  1359 (*
```
```  1360 context ab_semigroup_mult
```
```  1361 begin
```
```  1362
```
```  1363 lemma fold_graph_permute:
```
```  1364   "[|fold_graph times id b (insert a A) x; a \<notin> A; b \<notin> A|]
```
```  1365    ==> fold_graph times id a (insert b A) x"
```
```  1366 apply (cases "a=b")
```
```  1367 apply (auto dest: fold_graph_permute_diff)
```
```  1368 done
```
```  1369
```
```  1370 lemma fold1Set_determ:
```
```  1371   "fold1Set times A x ==> fold1Set times A y ==> y = x"
```
```  1372 proof (clarify elim!: fold1Set.cases)
```
```  1373   fix A x B y a b
```
```  1374   assume Ax: "fold_graph times id a A x"
```
```  1375   assume By: "fold_graph times id b B y"
```
```  1376   assume anotA:  "a \<notin> A"
```
```  1377   assume bnotB:  "b \<notin> B"
```
```  1378   assume eq: "insert a A = insert b B"
```
```  1379   show "y=x"
```
```  1380   proof cases
```
```  1381     assume same: "a=b"
```
```  1382     hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
```
```  1383     thus ?thesis using Ax By same by (blast intro: fold_graph_determ)
```
```  1384   next
```
```  1385     assume diff: "a\<noteq>b"
```
```  1386     let ?D = "B - {a}"
```
```  1387     have B: "B = insert a ?D" and A: "A = insert b ?D"
```
```  1388      and aB: "a \<in> B" and bA: "b \<in> A"
```
```  1389       using eq anotA bnotB diff by (blast elim!:equalityE)+
```
```  1390     with aB bnotB By
```
```  1391     have "fold_graph times id a (insert b ?D) y"
```
```  1392       by (auto intro: fold_graph_permute simp add: insert_absorb)
```
```  1393     moreover
```
```  1394     have "fold_graph times id a (insert b ?D) x"
```
```  1395       by (simp add: A [symmetric] Ax)
```
```  1396     ultimately show ?thesis by (blast intro: fold_graph_determ)
```
```  1397   qed
```
```  1398 qed
```
```  1399
```
```  1400 lemma fold1Set_equality: "fold1Set times A y ==> fold1 times A = y"
```
```  1401   by (unfold fold1_def) (blast intro: fold1Set_determ)
```
```  1402
```
```  1403 end
```
```  1404 *)
```
```  1405
```
```  1406 declare
```
```  1407   empty_fold_graphE [rule del]  fold_graph.intros [rule del]
```
```  1408   empty_fold1SetE [rule del]  insert_fold1SetE [rule del]
```
```  1409   -- {* No more proofs involve these relations. *}
```
```  1410
```
```  1411 subsubsection {* Lemmas about @{text fold1} *}
```
```  1412
```
```  1413 context ab_semigroup_mult
```
```  1414 begin
```
```  1415
```
```  1416 lemma fold1_Un:
```
```  1417 assumes A: "finite A" "A \<noteq> {}"
```
```  1418 shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
```
```  1419        fold1 times (A Un B) = fold1 times A * fold1 times B"
```
```  1420 using A by (induct rule: finite_ne_induct)
```
```  1421   (simp_all add: fold1_insert mult_assoc)
```
```  1422
```
```  1423 lemma fold1_in:
```
```  1424   assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x,y}"
```
```  1425   shows "fold1 times A \<in> A"
```
```  1426 using A
```
```  1427 proof (induct rule:finite_ne_induct)
```
```  1428   case singleton thus ?case by simp
```
```  1429 next
```
```  1430   case insert thus ?case using elem by (force simp add:fold1_insert)
```
```  1431 qed
```
```  1432
```
```  1433 end
```
```  1434
```
```  1435 lemma (in ab_semigroup_idem_mult) fold1_Un2:
```
```  1436 assumes A: "finite A" "A \<noteq> {}"
```
```  1437 shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
```
```  1438        fold1 times (A Un B) = fold1 times A * fold1 times B"
```
```  1439 using A
```
```  1440 proof(induct rule:finite_ne_induct)
```
```  1441   case singleton thus ?case by simp
```
```  1442 next
```
```  1443   case insert thus ?case by (simp add: mult_assoc)
```
```  1444 qed
```
```  1445
```
```  1446
```
```  1447 subsection {* Locales as mini-packages for fold operations *}
```
```  1448
```
```  1449 subsubsection {* The natural case *}
```
```  1450
```
```  1451 locale folding =
```
```  1452   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
```
```  1453   fixes F :: "'a set \<Rightarrow> 'b \<Rightarrow> 'b"
```
```  1454   assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
```
```  1455   assumes eq_fold: "finite A \<Longrightarrow> F A s = fold f s A"
```
```  1456 begin
```
```  1457
```
```  1458 lemma empty [simp]:
```
```  1459   "F {} = id"
```
```  1460   by (simp add: eq_fold fun_eq_iff)
```
```  1461
```
```  1462 lemma insert [simp]:
```
```  1463   assumes "finite A" and "x \<notin> A"
```
```  1464   shows "F (insert x A) = F A \<circ> f x"
```
```  1465 proof -
```
```  1466   interpret comp_fun_commute f
```
```  1467     by default (insert comp_fun_commute, simp add: fun_eq_iff)
```
```  1468   from fold_insert2 assms
```
```  1469   have "\<And>s. fold f s (insert x A) = fold f (f x s) A" .
```
```  1470   with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
```
```  1471 qed
```
```  1472
```
```  1473 lemma remove:
```
```  1474   assumes "finite A" and "x \<in> A"
```
```  1475   shows "F A = F (A - {x}) \<circ> f x"
```
```  1476 proof -
```
```  1477   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
```
```  1478     by (auto dest: mk_disjoint_insert)
```
```  1479   moreover from `finite A` this have "finite B" by simp
```
```  1480   ultimately show ?thesis by simp
```
```  1481 qed
```
```  1482
```
```  1483 lemma insert_remove:
```
```  1484   assumes "finite A"
```
```  1485   shows "F (insert x A) = F (A - {x}) \<circ> f x"
```
```  1486   using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
```
```  1487
```
```  1488 lemma commute_left_comp:
```
```  1489   "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
```
```  1490   by (simp add: o_assoc comp_fun_commute)
```
```  1491
```
```  1492 lemma comp_fun_commute':
```
```  1493   assumes "finite A"
```
```  1494   shows "f x \<circ> F A = F A \<circ> f x"
```
```  1495   using assms by (induct A)
```
```  1496     (simp, simp del: o_apply add: o_assoc, simp del: o_apply add: comp_assoc comp_fun_commute)
```
```  1497
```
```  1498 lemma commute_left_comp':
```
```  1499   assumes "finite A"
```
```  1500   shows "f x \<circ> (F A \<circ> g) = F A \<circ> (f x \<circ> g)"
```
```  1501   using assms by (simp add: o_assoc comp_fun_commute')
```
```  1502
```
```  1503 lemma comp_fun_commute'':
```
```  1504   assumes "finite A" and "finite B"
```
```  1505   shows "F B \<circ> F A = F A \<circ> F B"
```
```  1506   using assms by (induct A)
```
```  1507     (simp_all add: o_assoc, simp add: comp_assoc comp_fun_commute')
```
```  1508
```
```  1509 lemma commute_left_comp'':
```
```  1510   assumes "finite A" and "finite B"
```
```  1511   shows "F B \<circ> (F A \<circ> g) = F A \<circ> (F B \<circ> g)"
```
```  1512   using assms by (simp add: o_assoc comp_fun_commute'')
```
```  1513
```
```  1514 lemmas comp_fun_commutes = comp_assoc comp_fun_commute commute_left_comp
```
```  1515   comp_fun_commute' commute_left_comp' comp_fun_commute'' commute_left_comp''
```
```  1516
```
```  1517 lemma union_inter:
```
```  1518   assumes "finite A" and "finite B"
```
```  1519   shows "F (A \<union> B) \<circ> F (A \<inter> B) = F A \<circ> F B"
```
```  1520   using assms by (induct A)
```
```  1521     (simp_all del: o_apply add: insert_absorb Int_insert_left comp_fun_commutes,
```
```  1522       simp add: o_assoc)
```
```  1523
```
```  1524 lemma union:
```
```  1525   assumes "finite A" and "finite B"
```
```  1526   and "A \<inter> B = {}"
```
```  1527   shows "F (A \<union> B) = F A \<circ> F B"
```
```  1528 proof -
```
```  1529   from union_inter `finite A` `finite B` have "F (A \<union> B) \<circ> F (A \<inter> B) = F A \<circ> F B" .
```
```  1530   with `A \<inter> B = {}` show ?thesis by simp
```
```  1531 qed
```
```  1532
```
```  1533 end
```
```  1534
```
```  1535
```
```  1536 subsubsection {* The natural case with idempotency *}
```
```  1537
```
```  1538 locale folding_idem = folding +
```
```  1539   assumes idem_comp: "f x \<circ> f x = f x"
```
```  1540 begin
```
```  1541
```
```  1542 lemma idem_left_comp:
```
```  1543   "f x \<circ> (f x \<circ> g) = f x \<circ> g"
```
```  1544   by (simp add: o_assoc idem_comp)
```
```  1545
```
```  1546 lemma in_comp_idem:
```
```  1547   assumes "finite A" and "x \<in> A"
```
```  1548   shows "F A \<circ> f x = F A"
```
```  1549 using assms by (induct A)
```
```  1550   (auto simp add: comp_fun_commutes idem_comp, simp add: commute_left_comp' [symmetric] comp_fun_commute')
```
```  1551
```
```  1552 lemma subset_comp_idem:
```
```  1553   assumes "finite A" and "B \<subseteq> A"
```
```  1554   shows "F A \<circ> F B = F A"
```
```  1555 proof -
```
```  1556   from assms have "finite B" by (blast dest: finite_subset)
```
```  1557   then show ?thesis using `B \<subseteq> A` by (induct B)
```
```  1558     (simp_all add: o_assoc in_comp_idem `finite A`)
```
```  1559 qed
```
```  1560
```
```  1561 declare insert [simp del]
```
```  1562
```
```  1563 lemma insert_idem [simp]:
```
```  1564   assumes "finite A"
```
```  1565   shows "F (insert x A) = F A \<circ> f x"
```
```  1566   using assms by (cases "x \<in> A") (simp_all add: insert in_comp_idem insert_absorb)
```
```  1567
```
```  1568 lemma union_idem:
```
```  1569   assumes "finite A" and "finite B"
```
```  1570   shows "F (A \<union> B) = F A \<circ> F B"
```
```  1571 proof -
```
```  1572   from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
```
```  1573   then have "F (A \<union> B) \<circ> F (A \<inter> B) = F (A \<union> B)" by (rule subset_comp_idem)
```
```  1574   with assms show ?thesis by (simp add: union_inter)
```
```  1575 qed
```
```  1576
```
```  1577 end
```
```  1578
```
```  1579
```
```  1580 subsubsection {* The image case with fixed function *}
```
```  1581
```
```  1582 no_notation times (infixl "*" 70)
```
```  1583 no_notation Groups.one ("1")
```
```  1584
```
```  1585 locale folding_image_simple = comm_monoid +
```
```  1586   fixes g :: "('b \<Rightarrow> 'a)"
```
```  1587   fixes F :: "'b set \<Rightarrow> 'a"
```
```  1588   assumes eq_fold_g: "finite A \<Longrightarrow> F A = fold_image f g 1 A"
```
```  1589 begin
```
```  1590
```
```  1591 lemma empty [simp]:
```
```  1592   "F {} = 1"
```
```  1593   by (simp add: eq_fold_g)
```
```  1594
```
```  1595 lemma insert [simp]:
```
```  1596   assumes "finite A" and "x \<notin> A"
```
```  1597   shows "F (insert x A) = g x * F A"
```
```  1598 proof -
```
```  1599   interpret comp_fun_commute "%x y. (g x) * y"
```
```  1600     by default (simp add: ac_simps fun_eq_iff)
```
```  1601   from assms have "fold_image (op *) g 1 (insert x A) = g x * fold_image (op *) g 1 A"
```
```  1602     by (simp add: fold_image_def)
```
```  1603   with `finite A` show ?thesis by (simp add: eq_fold_g)
```
```  1604 qed
```
```  1605
```
```  1606 lemma remove:
```
```  1607   assumes "finite A" and "x \<in> A"
```
```  1608   shows "F A = g x * F (A - {x})"
```
```  1609 proof -
```
```  1610   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
```
```  1611     by (auto dest: mk_disjoint_insert)
```
```  1612   moreover from `finite A` this have "finite B" by simp
```
```  1613   ultimately show ?thesis by simp
```
```  1614 qed
```
```  1615
```
```  1616 lemma insert_remove:
```
```  1617   assumes "finite A"
```
```  1618   shows "F (insert x A) = g x * F (A - {x})"
```
```  1619   using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
```
```  1620
```
```  1621 lemma neutral:
```
```  1622   assumes "finite A" and "\<forall>x\<in>A. g x = 1"
```
```  1623   shows "F A = 1"
```
```  1624   using assms by (induct A) simp_all
```
```  1625
```
```  1626 lemma union_inter:
```
```  1627   assumes "finite A" and "finite B"
```
```  1628   shows "F (A \<union> B) * F (A \<inter> B) = F A * F B"
```
```  1629 using assms proof (induct A)
```
```  1630   case empty then show ?case by simp
```
```  1631 next
```
```  1632   case (insert x A) then show ?case
```
```  1633     by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
```
```  1634 qed
```
```  1635
```
```  1636 corollary union_inter_neutral:
```
```  1637   assumes "finite A" and "finite B"
```
```  1638   and I0: "\<forall>x \<in> A\<inter>B. g x = 1"
```
```  1639   shows "F (A \<union> B) = F A * F B"
```
```  1640   using assms by (simp add: union_inter [symmetric] neutral)
```
```  1641
```
```  1642 corollary union_disjoint:
```
```  1643   assumes "finite A" and "finite B"
```
```  1644   assumes "A \<inter> B = {}"
```
```  1645   shows "F (A \<union> B) = F A * F B"
```
```  1646   using assms by (simp add: union_inter_neutral)
```
```  1647
```
```  1648 end
```
```  1649
```
```  1650
```
```  1651 subsubsection {* The image case with flexible function *}
```
```  1652
```
```  1653 locale folding_image = comm_monoid +
```
```  1654   fixes F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
```
```  1655   assumes eq_fold: "\<And>g. finite A \<Longrightarrow> F g A = fold_image f g 1 A"
```
```  1656
```
```  1657 sublocale folding_image < folding_image_simple "op *" 1 g "F g" proof
```
```  1658 qed (fact eq_fold)
```
```  1659
```
```  1660 context folding_image
```
```  1661 begin
```
```  1662
```
```  1663 lemma reindex: (* FIXME polymorhism *)
```
```  1664   assumes "finite A" and "inj_on h A"
```
```  1665   shows "F g (h ` A) = F (g \<circ> h) A"
```
```  1666   using assms by (induct A) auto
```
```  1667
```
```  1668 lemma cong:
```
```  1669   assumes "finite A" and "\<And>x. x \<in> A \<Longrightarrow> g x = h x"
```
```  1670   shows "F g A = F h A"
```
```  1671 proof -
```
```  1672   from assms have "ALL C. C <= A --> (ALL x:C. g x = h x) --> F g C = F h C"
```
```  1673   apply - apply (erule finite_induct) apply simp
```
```  1674   apply (simp add: subset_insert_iff, clarify)
```
```  1675   apply (subgoal_tac "finite C")
```
```  1676   prefer 2 apply (blast dest: finite_subset [rotated])
```
```  1677   apply (subgoal_tac "C = insert x (C - {x})")
```
```  1678   prefer 2 apply blast
```
```  1679   apply (erule ssubst)
```
```  1680   apply (drule spec)
```
```  1681   apply (erule (1) notE impE)
```
```  1682   apply (simp add: Ball_def del: insert_Diff_single)
```
```  1683   done
```
```  1684   with assms show ?thesis by simp
```
```  1685 qed
```
```  1686
```
```  1687 lemma UNION_disjoint:
```
```  1688   assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
```
```  1689   and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
```
```  1690   shows "F g (UNION I A) = F (F g \<circ> A) I"
```
```  1691 apply (insert assms)
```
```  1692 apply (induct rule: finite_induct)
```
```  1693 apply simp
```
```  1694 apply atomize
```
```  1695 apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
```
```  1696  prefer 2 apply blast
```
```  1697 apply (subgoal_tac "A x Int UNION Fa A = {}")
```
```  1698  prefer 2 apply blast
```
```  1699 apply (simp add: union_disjoint)
```
```  1700 done
```
```  1701
```
```  1702 lemma distrib:
```
```  1703   assumes "finite A"
```
```  1704   shows "F (\<lambda>x. g x * h x) A = F g A * F h A"
```
```  1705   using assms by (rule finite_induct) (simp_all add: assoc commute left_commute)
```
```  1706
```
```  1707 lemma related:
```
```  1708   assumes Re: "R 1 1"
```
```  1709   and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)"
```
```  1710   and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
```
```  1711   shows "R (F h S) (F g S)"
```
```  1712   using fS by (rule finite_subset_induct) (insert assms, auto)
```
```  1713
```
```  1714 lemma eq_general:
```
```  1715   assumes fS: "finite S"
```
```  1716   and h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y"
```
```  1717   and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x"
```
```  1718   shows "F f1 S = F f2 S'"
```
```  1719 proof-
```
```  1720   from h f12 have hS: "h ` S = S'" by blast
```
```  1721   {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
```
```  1722     from f12 h H  have "x = y" by auto }
```
```  1723   hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
```
```  1724   from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto
```
```  1725   from hS have "F f2 S' = F f2 (h ` S)" by simp
```
```  1726   also have "\<dots> = F (f2 o h) S" using reindex [OF fS hinj, of f2] .
```
```  1727   also have "\<dots> = F f1 S " using th cong [OF fS, of "f2 o h" f1]
```
```  1728     by blast
```
```  1729   finally show ?thesis ..
```
```  1730 qed
```
```  1731
```
```  1732 lemma eq_general_inverses:
```
```  1733   assumes fS: "finite S"
```
```  1734   and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
```
```  1735   and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x"
```
```  1736   shows "F j S = F g T"
```
```  1737   (* metis solves it, but not yet available here *)
```
```  1738   apply (rule eq_general [OF fS, of T h g j])
```
```  1739   apply (rule ballI)
```
```  1740   apply (frule kh)
```
```  1741   apply (rule ex1I[])
```
```  1742   apply blast
```
```  1743   apply clarsimp
```
```  1744   apply (drule hk) apply simp
```
```  1745   apply (rule sym)
```
```  1746   apply (erule conjunct1[OF conjunct2[OF hk]])
```
```  1747   apply (rule ballI)
```
```  1748   apply (drule hk)
```
```  1749   apply blast
```
```  1750   done
```
```  1751
```
```  1752 end
```
```  1753
```
```  1754
```
```  1755 subsubsection {* The image case with fixed function and idempotency *}
```
```  1756
```
```  1757 locale folding_image_simple_idem = folding_image_simple +
```
```  1758   assumes idem: "x * x = x"
```
```  1759
```
```  1760 sublocale folding_image_simple_idem < semilattice: semilattice proof
```
```  1761 qed (fact idem)
```
```  1762
```
```  1763 context folding_image_simple_idem
```
```  1764 begin
```
```  1765
```
```  1766 lemma in_idem:
```
```  1767   assumes "finite A" and "x \<in> A"
```
```  1768   shows "g x * F A = F A"
```
```  1769   using assms by (induct A) (auto simp add: left_commute)
```
```  1770
```
```  1771 lemma subset_idem:
```
```  1772   assumes "finite A" and "B \<subseteq> A"
```
```  1773   shows "F B * F A = F A"
```
```  1774 proof -
```
```  1775   from assms have "finite B" by (blast dest: finite_subset)
```
```  1776   then show ?thesis using `B \<subseteq> A` by (induct B)
```
```  1777     (auto simp add: assoc in_idem `finite A`)
```
```  1778 qed
```
```  1779
```
```  1780 declare insert [simp del]
```
```  1781
```
```  1782 lemma insert_idem [simp]:
```
```  1783   assumes "finite A"
```
```  1784   shows "F (insert x A) = g x * F A"
```
```  1785   using assms by (cases "x \<in> A") (simp_all add: insert in_idem insert_absorb)
```
```  1786
```
```  1787 lemma union_idem:
```
```  1788   assumes "finite A" and "finite B"
```
```  1789   shows "F (A \<union> B) = F A * F B"
```
```  1790 proof -
```
```  1791   from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
```
```  1792   then have "F (A \<inter> B) * F (A \<union> B) = F (A \<union> B)" by (rule subset_idem)
```
```  1793   with assms show ?thesis by (simp add: union_inter [of A B, symmetric] commute)
```
```  1794 qed
```
```  1795
```
```  1796 end
```
```  1797
```
```  1798
```
```  1799 subsubsection {* The image case with flexible function and idempotency *}
```
```  1800
```
```  1801 locale folding_image_idem = folding_image +
```
```  1802   assumes idem: "x * x = x"
```
```  1803
```
```  1804 sublocale folding_image_idem < folding_image_simple_idem "op *" 1 g "F g" proof
```
```  1805 qed (fact idem)
```
```  1806
```
```  1807
```
```  1808 subsubsection {* The neutral-less case *}
```
```  1809
```
```  1810 locale folding_one = abel_semigroup +
```
```  1811   fixes F :: "'a set \<Rightarrow> 'a"
```
```  1812   assumes eq_fold: "finite A \<Longrightarrow> F A = fold1 f A"
```
```  1813 begin
```
```  1814
```
```  1815 lemma singleton [simp]:
```
```  1816   "F {x} = x"
```
```  1817   by (simp add: eq_fold)
```
```  1818
```
```  1819 lemma eq_fold':
```
```  1820   assumes "finite A" and "x \<notin> A"
```
```  1821   shows "F (insert x A) = fold (op *) x A"
```
```  1822 proof -
```
```  1823   interpret ab_semigroup_mult "op *" by default (simp_all add: ac_simps)
```
```  1824   from assms show ?thesis by (simp add: eq_fold fold1_eq_fold)
```
```  1825 qed
```
```  1826
```
```  1827 lemma insert [simp]:
```
```  1828   assumes "finite A" and "x \<notin> A" and "A \<noteq> {}"
```
```  1829   shows "F (insert x A) = x * F A"
```
```  1830 proof -
```
```  1831   from `A \<noteq> {}` obtain b where "b \<in> A" by blast
```
```  1832   then obtain B where *: "A = insert b B" "b \<notin> B" by (blast dest: mk_disjoint_insert)
```
```  1833   with `finite A` have "finite B" by simp
```
```  1834   interpret fold: folding "op *" "\<lambda>a b. fold (op *) b a" proof
```
```  1835   qed (simp_all add: fun_eq_iff ac_simps)
```
```  1836   from `finite B` fold.comp_fun_commute' [of B x]
```
```  1837     have "op * x \<circ> (\<lambda>b. fold op * b B) = (\<lambda>b. fold op * b B) \<circ> op * x" by simp
```
```  1838   then have A: "x * fold op * b B = fold op * (b * x) B" by (simp add: fun_eq_iff commute)
```
```  1839   from `finite B` * fold.insert [of B b]
```
```  1840     have "(\<lambda>x. fold op * x (insert b B)) = (\<lambda>x. fold op * x B) \<circ> op * b" by simp
```
```  1841   then have B: "fold op * x (insert b B) = fold op * (b * x) B" by (simp add: fun_eq_iff)
```
```  1842   from A B assms * show ?thesis by (simp add: eq_fold' del: fold.insert)
```
```  1843 qed
```
```  1844
```
```  1845 lemma remove:
```
```  1846   assumes "finite A" and "x \<in> A"
```
```  1847   shows "F A = (if A - {x} = {} then x else x * F (A - {x}))"
```
```  1848 proof -
```
```  1849   from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
```
```  1850   with assms show ?thesis by simp
```
```  1851 qed
```
```  1852
```
```  1853 lemma insert_remove:
```
```  1854   assumes "finite A"
```
```  1855   shows "F (insert x A) = (if A - {x} = {} then x else x * F (A - {x}))"
```
```  1856   using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
```
```  1857
```
```  1858 lemma union_disjoint:
```
```  1859   assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}" and "A \<inter> B = {}"
```
```  1860   shows "F (A \<union> B) = F A * F B"
```
```  1861   using assms by (induct A rule: finite_ne_induct) (simp_all add: ac_simps)
```
```  1862
```
```  1863 lemma union_inter:
```
```  1864   assumes "finite A" and "finite B" and "A \<inter> B \<noteq> {}"
```
```  1865   shows "F (A \<union> B) * F (A \<inter> B) = F A * F B"
```
```  1866 proof -
```
```  1867   from assms have "A \<noteq> {}" and "B \<noteq> {}" by auto
```
```  1868   from `finite A` `A \<noteq> {}` `A \<inter> B \<noteq> {}` show ?thesis proof (induct A rule: finite_ne_induct)
```
```  1869     case (singleton x) then show ?case by (simp add: insert_absorb ac_simps)
```
```  1870   next
```
```  1871     case (insert x A) show ?case proof (cases "x \<in> B")
```
```  1872       case True then have "B \<noteq> {}" by auto
```
```  1873       with insert True `finite B` show ?thesis by (cases "A \<inter> B = {}")
```
```  1874         (simp_all add: insert_absorb ac_simps union_disjoint)
```
```  1875     next
```
```  1876       case False with insert have "F (A \<union> B) * F (A \<inter> B) = F A * F B" by simp
```
```  1877       moreover from False `finite B` insert have "finite (A \<union> B)" "x \<notin> A \<union> B" "A \<union> B \<noteq> {}"
```
```  1878         by auto
```
```  1879       ultimately show ?thesis using False `finite A` `x \<notin> A` `A \<noteq> {}` by (simp add: assoc)
```
```  1880     qed
```
```  1881   qed
```
```  1882 qed
```
```  1883
```
```  1884 lemma closed:
```
```  1885   assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
```
```  1886   shows "F A \<in> A"
```
```  1887 using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
```
```  1888   case singleton then show ?case by simp
```
```  1889 next
```
```  1890   case insert with elem show ?case by force
```
```  1891 qed
```
```  1892
```
```  1893 end
```
```  1894
```
```  1895
```
```  1896 subsubsection {* The neutral-less case with idempotency *}
```
```  1897
```
```  1898 locale folding_one_idem = folding_one +
```
```  1899   assumes idem: "x * x = x"
```
```  1900
```
```  1901 sublocale folding_one_idem < semilattice: semilattice proof
```
```  1902 qed (fact idem)
```
```  1903
```
```  1904 context folding_one_idem
```
```  1905 begin
```
```  1906
```
```  1907 lemma in_idem:
```
```  1908   assumes "finite A" and "x \<in> A"
```
```  1909   shows "x * F A = F A"
```
```  1910 proof -
```
```  1911   from assms have "A \<noteq> {}" by auto
```
```  1912   with `finite A` show ?thesis using `x \<in> A` by (induct A rule: finite_ne_induct) (auto simp add: ac_simps)
```
```  1913 qed
```
```  1914
```
```  1915 lemma subset_idem:
```
```  1916   assumes "finite A" "B \<noteq> {}" and "B \<subseteq> A"
```
```  1917   shows "F B * F A = F A"
```
```  1918 proof -
```
```  1919   from assms have "finite B" by (blast dest: finite_subset)
```
```  1920   then show ?thesis using `B \<noteq> {}` `B \<subseteq> A` by (induct B rule: finite_ne_induct)
```
```  1921     (simp_all add: assoc in_idem `finite A`)
```
```  1922 qed
```
```  1923
```
```  1924 lemma eq_fold_idem':
```
```  1925   assumes "finite A"
```
```  1926   shows "F (insert a A) = fold (op *) a A"
```
```  1927 proof -
```
```  1928   interpret ab_semigroup_idem_mult "op *" by default (simp_all add: ac_simps)
```
```  1929   from assms show ?thesis by (simp add: eq_fold fold1_eq_fold_idem)
```
```  1930 qed
```
```  1931
```
```  1932 lemma insert_idem [simp]:
```
```  1933   assumes "finite A" and "A \<noteq> {}"
```
```  1934   shows "F (insert x A) = x * F A"
```
```  1935 proof (cases "x \<in> A")
```
```  1936   case False from `finite A` `x \<notin> A` `A \<noteq> {}` show ?thesis by (rule insert)
```
```  1937 next
```
```  1938   case True
```
```  1939   from `finite A` `A \<noteq> {}` show ?thesis by (simp add: in_idem insert_absorb True)
```
```  1940 qed
```
```  1941
```
```  1942 lemma union_idem:
```
```  1943   assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}"
```
```  1944   shows "F (A \<union> B) = F A * F B"
```
```  1945 proof (cases "A \<inter> B = {}")
```
```  1946   case True with assms show ?thesis by (simp add: union_disjoint)
```
```  1947 next
```
```  1948   case False
```
```  1949   from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
```
```  1950   with False have "F (A \<inter> B) * F (A \<union> B) = F (A \<union> B)" by (auto intro: subset_idem)
```
```  1951   with assms False show ?thesis by (simp add: union_inter [of A B, symmetric] commute)
```
```  1952 qed
```
```  1953
```
```  1954 lemma hom_commute:
```
```  1955   assumes hom: "\<And>x y. h (x * y) = h x * h y"
```
```  1956   and N: "finite N" "N \<noteq> {}" shows "h (F N) = F (h ` N)"
```
```  1957 using N proof (induct rule: finite_ne_induct)
```
```  1958   case singleton thus ?case by simp
```
```  1959 next
```
```  1960   case (insert n N)
```
```  1961   then have "h (F (insert n N)) = h (n * F N)" by simp
```
```  1962   also have "\<dots> = h n * h (F N)" by (rule hom)
```
```  1963   also have "h (F N) = F (h ` N)" by(rule insert)
```
```  1964   also have "h n * \<dots> = F (insert (h n) (h ` N))"
```
```  1965     using insert by(simp)
```
```  1966   also have "insert (h n) (h ` N) = h ` insert n N" by simp
```
```  1967   finally show ?case .
```
```  1968 qed
```
```  1969
```
```  1970 end
```
```  1971
```
```  1972 notation times (infixl "*" 70)
```
```  1973 notation Groups.one ("1")
```
```  1974
```
```  1975
```
```  1976 subsection {* Finite cardinality *}
```
```  1977
```
```  1978 text {* This definition, although traditional, is ugly to work with:
```
```  1979 @{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
```
```  1980 But now that we have @{text fold_image} things are easy:
```
```  1981 *}
```
```  1982
```
```  1983 definition card :: "'a set \<Rightarrow> nat" where
```
```  1984   "card A = (if finite A then fold_image (op +) (\<lambda>x. 1) 0 A else 0)"
```
```  1985
```
```  1986 interpretation card: folding_image_simple "op +" 0 "\<lambda>x. 1" card proof
```
```  1987 qed (simp add: card_def)
```
```  1988
```
```  1989 lemma card_infinite [simp]:
```
```  1990   "\<not> finite A \<Longrightarrow> card A = 0"
```
```  1991   by (simp add: card_def)
```
```  1992
```
```  1993 lemma card_empty:
```
```  1994   "card {} = 0"
```
```  1995   by (fact card.empty)
```
```  1996
```
```  1997 lemma card_insert_disjoint:
```
```  1998   "finite A ==> x \<notin> A ==> card (insert x A) = Suc (card A)"
```
```  1999   by simp
```
```  2000
```
```  2001 lemma card_insert_if:
```
```  2002   "finite A ==> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
```
```  2003   by auto (simp add: card.insert_remove card.remove)
```
```  2004
```
```  2005 lemma card_ge_0_finite:
```
```  2006   "card A > 0 \<Longrightarrow> finite A"
```
```  2007   by (rule ccontr) simp
```
```  2008
```
```  2009 lemma card_0_eq [simp, no_atp]:
```
```  2010   "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
```
```  2011   by (auto dest: mk_disjoint_insert)
```
```  2012
```
```  2013 lemma finite_UNIV_card_ge_0:
```
```  2014   "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
```
```  2015   by (rule ccontr) simp
```
```  2016
```
```  2017 lemma card_eq_0_iff:
```
```  2018   "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
```
```  2019   by auto
```
```  2020
```
```  2021 lemma card_gt_0_iff:
```
```  2022   "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
```
```  2023   by (simp add: neq0_conv [symmetric] card_eq_0_iff)
```
```  2024
```
```  2025 lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
```
```  2026 apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
```
```  2027 apply(simp del:insert_Diff_single)
```
```  2028 done
```
```  2029
```
```  2030 lemma card_Diff_singleton:
```
```  2031   "finite A ==> x: A ==> card (A - {x}) = card A - 1"
```
```  2032 by (simp add: card_Suc_Diff1 [symmetric])
```
```  2033
```
```  2034 lemma card_Diff_singleton_if:
```
```  2035   "finite A ==> card (A - {x}) = (if x : A then card A - 1 else card A)"
```
```  2036 by (simp add: card_Diff_singleton)
```
```  2037
```
```  2038 lemma card_Diff_insert[simp]:
```
```  2039 assumes "finite A" and "a:A" and "a ~: B"
```
```  2040 shows "card(A - insert a B) = card(A - B) - 1"
```
```  2041 proof -
```
```  2042   have "A - insert a B = (A - B) - {a}" using assms by blast
```
```  2043   then show ?thesis using assms by(simp add:card_Diff_singleton)
```
```  2044 qed
```
```  2045
```
```  2046 lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
```
```  2047 by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert)
```
```  2048
```
```  2049 lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
```
```  2050 by (simp add: card_insert_if)
```
```  2051
```
```  2052 lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
```
```  2053 by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
```
```  2054
```
```  2055 lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
```
```  2056 using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
```
```  2057
```
```  2058 lemma card_mono:
```
```  2059   assumes "finite B" and "A \<subseteq> B"
```
```  2060   shows "card A \<le> card B"
```
```  2061 proof -
```
```  2062   from assms have "finite A" by (auto intro: finite_subset)
```
```  2063   then show ?thesis using assms proof (induct A arbitrary: B)
```
```  2064     case empty then show ?case by simp
```
```  2065   next
```
```  2066     case (insert x A)
```
```  2067     then have "x \<in> B" by simp
```
```  2068     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
```
```  2069     with insert.hyps have "card A \<le> card (B - {x})" by auto
```
```  2070     with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
```
```  2071   qed
```
```  2072 qed
```
```  2073
```
```  2074 lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
```
```  2075 apply (induct rule: finite_induct)
```
```  2076 apply simp
```
```  2077 apply clarify
```
```  2078 apply (subgoal_tac "finite A & A - {x} <= F")
```
```  2079  prefer 2 apply (blast intro: finite_subset, atomize)
```
```  2080 apply (drule_tac x = "A - {x}" in spec)
```
```  2081 apply (simp add: card_Diff_singleton_if split add: split_if_asm)
```
```  2082 apply (case_tac "card A", auto)
```
```  2083 done
```
```  2084
```
```  2085 lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
```
```  2086 apply (simp add: psubset_eq linorder_not_le [symmetric])
```
```  2087 apply (blast dest: card_seteq)
```
```  2088 done
```
```  2089
```
```  2090 lemma card_Un_Int: "finite A ==> finite B
```
```  2091     ==> card A + card B = card (A Un B) + card (A Int B)"
```
```  2092   by (fact card.union_inter [symmetric])
```
```  2093
```
```  2094 lemma card_Un_disjoint: "finite A ==> finite B
```
```  2095     ==> A Int B = {} ==> card (A Un B) = card A + card B"
```
```  2096   by (fact card.union_disjoint)
```
```  2097
```
```  2098 lemma card_Diff_subset:
```
```  2099   assumes "finite B" and "B \<subseteq> A"
```
```  2100   shows "card (A - B) = card A - card B"
```
```  2101 proof (cases "finite A")
```
```  2102   case False with assms show ?thesis by simp
```
```  2103 next
```
```  2104   case True with assms show ?thesis by (induct B arbitrary: A) simp_all
```
```  2105 qed
```
```  2106
```
```  2107 lemma card_Diff_subset_Int:
```
```  2108   assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
```
```  2109 proof -
```
```  2110   have "A - B = A - A \<inter> B" by auto
```
```  2111   thus ?thesis
```
```  2112     by (simp add: card_Diff_subset AB)
```
```  2113 qed
```
```  2114
```
```  2115 lemma diff_card_le_card_Diff:
```
```  2116 assumes "finite B" shows "card A - card B \<le> card(A - B)"
```
```  2117 proof-
```
```  2118   have "card A - card B \<le> card A - card (A \<inter> B)"
```
```  2119     using card_mono[OF assms Int_lower2, of A] by arith
```
```  2120   also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
```
```  2121   finally show ?thesis .
```
```  2122 qed
```
```  2123
```
```  2124 lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
```
```  2125 apply (rule Suc_less_SucD)
```
```  2126 apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
```
```  2127 done
```
```  2128
```
```  2129 lemma card_Diff2_less:
```
```  2130   "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
```
```  2131 apply (case_tac "x = y")
```
```  2132  apply (simp add: card_Diff1_less del:card_Diff_insert)
```
```  2133 apply (rule less_trans)
```
```  2134  prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
```
```  2135 done
```
```  2136
```
```  2137 lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
```
```  2138 apply (case_tac "x : A")
```
```  2139  apply (simp_all add: card_Diff1_less less_imp_le)
```
```  2140 done
```
```  2141
```
```  2142 lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
```
```  2143 by (erule psubsetI, blast)
```
```  2144
```
```  2145 lemma insert_partition:
```
```  2146   "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
```
```  2147   \<Longrightarrow> x \<inter> \<Union> F = {}"
```
```  2148 by auto
```
```  2149
```
```  2150 lemma finite_psubset_induct[consumes 1, case_names psubset]:
```
```  2151   assumes fin: "finite A"
```
```  2152   and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A"
```
```  2153   shows "P A"
```
```  2154 using fin
```
```  2155 proof (induct A taking: card rule: measure_induct_rule)
```
```  2156   case (less A)
```
```  2157   have fin: "finite A" by fact
```
```  2158   have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
```
```  2159   { fix B
```
```  2160     assume asm: "B \<subset> A"
```
```  2161     from asm have "card B < card A" using psubset_card_mono fin by blast
```
```  2162     moreover
```
```  2163     from asm have "B \<subseteq> A" by auto
```
```  2164     then have "finite B" using fin finite_subset by blast
```
```  2165     ultimately
```
```  2166     have "P B" using ih by simp
```
```  2167   }
```
```  2168   with fin show "P A" using major by blast
```
```  2169 qed
```
```  2170
```
```  2171 text{* main cardinality theorem *}
```
```  2172 lemma card_partition [rule_format]:
```
```  2173   "finite C ==>
```
```  2174      finite (\<Union> C) -->
```
```  2175      (\<forall>c\<in>C. card c = k) -->
```
```  2176      (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
```
```  2177      k * card(C) = card (\<Union> C)"
```
```  2178 apply (erule finite_induct, simp)
```
```  2179 apply (simp add: card_Un_disjoint insert_partition
```
```  2180        finite_subset [of _ "\<Union> (insert x F)"])
```
```  2181 done
```
```  2182
```
```  2183 lemma card_eq_UNIV_imp_eq_UNIV:
```
```  2184   assumes fin: "finite (UNIV :: 'a set)"
```
```  2185   and card: "card A = card (UNIV :: 'a set)"
```
```  2186   shows "A = (UNIV :: 'a set)"
```
```  2187 proof
```
```  2188   show "A \<subseteq> UNIV" by simp
```
```  2189   show "UNIV \<subseteq> A"
```
```  2190   proof
```
```  2191     fix x
```
```  2192     show "x \<in> A"
```
```  2193     proof (rule ccontr)
```
```  2194       assume "x \<notin> A"
```
```  2195       then have "A \<subset> UNIV" by auto
```
```  2196       with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
```
```  2197       with card show False by simp
```
```  2198     qed
```
```  2199   qed
```
```  2200 qed
```
```  2201
```
```  2202 text{*The form of a finite set of given cardinality*}
```
```  2203
```
```  2204 lemma card_eq_SucD:
```
```  2205 assumes "card A = Suc k"
```
```  2206 shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
```
```  2207 proof -
```
```  2208   have fin: "finite A" using assms by (auto intro: ccontr)
```
```  2209   moreover have "card A \<noteq> 0" using assms by auto
```
```  2210   ultimately obtain b where b: "b \<in> A" by auto
```
```  2211   show ?thesis
```
```  2212   proof (intro exI conjI)
```
```  2213     show "A = insert b (A-{b})" using b by blast
```
```  2214     show "b \<notin> A - {b}" by blast
```
```  2215     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
```
```  2216       using assms b fin by(fastforce dest:mk_disjoint_insert)+
```
```  2217   qed
```
```  2218 qed
```
```  2219
```
```  2220 lemma card_Suc_eq:
```
```  2221   "(card A = Suc k) =
```
```  2222    (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
```
```  2223 apply(rule iffI)
```
```  2224  apply(erule card_eq_SucD)
```
```  2225 apply(auto)
```
```  2226 apply(subst card_insert)
```
```  2227  apply(auto intro:ccontr)
```
```  2228 done
```
```  2229
```
```  2230 lemma card_le_Suc_iff: "finite A \<Longrightarrow>
```
```  2231   Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
```
```  2232 by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
```
```  2233   dest: subset_singletonD split: nat.splits if_splits)
```
```  2234
```
```  2235 lemma finite_fun_UNIVD2:
```
```  2236   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
```
```  2237   shows "finite (UNIV :: 'b set)"
```
```  2238 proof -
```
```  2239   from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
```
```  2240     by (rule finite_imageI)
```
```  2241   moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
```
```  2242     by (rule UNIV_eq_I) auto
```
```  2243   ultimately show "finite (UNIV :: 'b set)" by simp
```
```  2244 qed
```
```  2245
```
```  2246 lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
```
```  2247   unfolding UNIV_unit by simp
```
```  2248
```
```  2249 lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
```
```  2250   unfolding UNIV_bool by simp
```
```  2251
```
```  2252
```
```  2253 subsubsection {* Cardinality of image *}
```
```  2254
```
```  2255 lemma card_image_le: "finite A ==> card (f ` A) <= card A"
```
```  2256 apply (induct rule: finite_induct)
```
```  2257  apply simp
```
```  2258 apply (simp add: le_SucI card_insert_if)
```
```  2259 done
```
```  2260
```
```  2261 lemma card_image:
```
```  2262   assumes "inj_on f A"
```
```  2263   shows "card (f ` A) = card A"
```
```  2264 proof (cases "finite A")
```
```  2265   case True then show ?thesis using assms by (induct A) simp_all
```
```  2266 next
```
```  2267   case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
```
```  2268   with False show ?thesis by simp
```
```  2269 qed
```
```  2270
```
```  2271 lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
```
```  2272 by(auto simp: card_image bij_betw_def)
```
```  2273
```
```  2274 lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
```
```  2275 by (simp add: card_seteq card_image)
```
```  2276
```
```  2277 lemma eq_card_imp_inj_on:
```
```  2278   "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
```
```  2279 apply (induct rule:finite_induct)
```
```  2280 apply simp
```
```  2281 apply(frule card_image_le[where f = f])
```
```  2282 apply(simp add:card_insert_if split:if_splits)
```
```  2283 done
```
```  2284
```
```  2285 lemma inj_on_iff_eq_card:
```
```  2286   "finite A ==> inj_on f A = (card(f ` A) = card A)"
```
```  2287 by(blast intro: card_image eq_card_imp_inj_on)
```
```  2288
```
```  2289
```
```  2290 lemma card_inj_on_le:
```
```  2291   "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
```
```  2292 apply (subgoal_tac "finite A")
```
```  2293  apply (force intro: card_mono simp add: card_image [symmetric])
```
```  2294 apply (blast intro: finite_imageD dest: finite_subset)
```
```  2295 done
```
```  2296
```
```  2297 lemma card_bij_eq:
```
```  2298   "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
```
```  2299      finite A; finite B |] ==> card A = card B"
```
```  2300 by (auto intro: le_antisym card_inj_on_le)
```
```  2301
```
```  2302 lemma bij_betw_finite:
```
```  2303   assumes "bij_betw f A B"
```
```  2304   shows "finite A \<longleftrightarrow> finite B"
```
```  2305 using assms unfolding bij_betw_def
```
```  2306 using finite_imageD[of f A] by auto
```
```  2307
```
```  2308
```
```  2309 subsubsection {* Pigeonhole Principles *}
```
```  2310
```
```  2311 lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
```
```  2312 by (auto dest: card_image less_irrefl_nat)
```
```  2313
```
```  2314 lemma pigeonhole_infinite:
```
```  2315 assumes  "~ finite A" and "finite(f`A)"
```
```  2316 shows "EX a0:A. ~finite{a:A. f a = f a0}"
```
```  2317 proof -
```
```  2318   have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
```
```  2319   proof(induct "f`A" arbitrary: A rule: finite_induct)
```
```  2320     case empty thus ?case by simp
```
```  2321   next
```
```  2322     case (insert b F)
```
```  2323     show ?case
```
```  2324     proof cases
```
```  2325       assume "finite{a:A. f a = b}"
```
```  2326       hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
```
```  2327       also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
```
```  2328       finally have "~ finite({a:A. f a \<noteq> b})" .
```
```  2329       from insert(3)[OF _ this]
```
```  2330       show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
```
```  2331     next
```
```  2332       assume 1: "~finite{a:A. f a = b}"
```
```  2333       hence "{a \<in> A. f a = b} \<noteq> {}" by force
```
```  2334       thus ?thesis using 1 by blast
```
```  2335     qed
```
```  2336   qed
```
```  2337   from this[OF assms(2,1)] show ?thesis .
```
```  2338 qed
```
```  2339
```
```  2340 lemma pigeonhole_infinite_rel:
```
```  2341 assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
```
```  2342 shows "EX b:B. ~finite{a:A. R a b}"
```
```  2343 proof -
```
```  2344    let ?F = "%a. {b:B. R a b}"
```
```  2345    from finite_Pow_iff[THEN iffD2, OF `finite B`]
```
```  2346    have "finite(?F ` A)" by(blast intro: rev_finite_subset)
```
```  2347    from pigeonhole_infinite[where f = ?F, OF assms(1) this]
```
```  2348    obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
```
```  2349    obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
```
```  2350    { assume "finite{a:A. R a b0}"
```
```  2351      then have "finite {a\<in>A. ?F a = ?F a0}"
```
```  2352        using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
```
```  2353    }
```
```  2354    with 1 `b0 : B` show ?thesis by blast
```
```  2355 qed
```
```  2356
```
```  2357
```
```  2358 subsubsection {* Cardinality of sums *}
```
```  2359
```
```  2360 lemma card_Plus:
```
```  2361   assumes "finite A" and "finite B"
```
```  2362   shows "card (A <+> B) = card A + card B"
```
```  2363 proof -
```
```  2364   have "Inl`A \<inter> Inr`B = {}" by fast
```
```  2365   with assms show ?thesis
```
```  2366     unfolding Plus_def
```
```  2367     by (simp add: card_Un_disjoint card_image)
```
```  2368 qed
```
```  2369
```
```  2370 lemma card_Plus_conv_if:
```
```  2371   "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
```
```  2372   by (auto simp add: card_Plus)
```
```  2373
```
```  2374
```
```  2375 subsubsection {* Cardinality of the Powerset *}
```
```  2376
```
```  2377 lemma card_Pow: "finite A ==> card (Pow A) = 2 ^ card A"
```
```  2378 apply (induct rule: finite_induct)
```
```  2379  apply (simp_all add: Pow_insert)
```
```  2380 apply (subst card_Un_disjoint, blast)
```
```  2381   apply (blast, blast)
```
```  2382 apply (subgoal_tac "inj_on (insert x) (Pow F)")
```
```  2383  apply (subst mult_2)
```
```  2384  apply (simp add: card_image Pow_insert)
```
```  2385 apply (unfold inj_on_def)
```
```  2386 apply (blast elim!: equalityE)
```
```  2387 done
```
```  2388
```
```  2389 text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
```
```  2390
```
```  2391 lemma dvd_partition:
```
```  2392   "finite (Union C) ==>
```
```  2393     ALL c : C. k dvd card c ==>
```
```  2394     (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
```
```  2395   k dvd card (Union C)"
```
```  2396 apply (frule finite_UnionD)
```
```  2397 apply (rotate_tac -1)
```
```  2398 apply (induct rule: finite_induct)
```
```  2399 apply simp_all
```
```  2400 apply clarify
```
```  2401 apply (subst card_Un_disjoint)
```
```  2402    apply (auto simp add: disjoint_eq_subset_Compl)
```
```  2403 done
```
```  2404
```
```  2405
```
```  2406 subsubsection {* Relating injectivity and surjectivity *}
```
```  2407
```
```  2408 lemma finite_surj_inj: "finite A \<Longrightarrow> A \<subseteq> f ` A \<Longrightarrow> inj_on f A"
```
```  2409 apply(rule eq_card_imp_inj_on, assumption)
```
```  2410 apply(frule finite_imageI)
```
```  2411 apply(drule (1) card_seteq)
```
```  2412  apply(erule card_image_le)
```
```  2413 apply simp
```
```  2414 done
```
```  2415
```
```  2416 lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
```
```  2417 shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
```
```  2418 by (blast intro: finite_surj_inj subset_UNIV)
```
```  2419
```
```  2420 lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
```
```  2421 shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
```
```  2422 by(fastforce simp:surj_def dest!: endo_inj_surj)
```
```  2423
```
```  2424 corollary infinite_UNIV_nat[iff]: "~finite(UNIV::nat set)"
```
```  2425 proof
```
```  2426   assume "finite(UNIV::nat set)"
```
```  2427   with finite_UNIV_inj_surj[of Suc]
```
```  2428   show False by simp (blast dest: Suc_neq_Zero surjD)
```
```  2429 qed
```
```  2430
```
```  2431 (* Often leads to bogus ATP proofs because of reduced type information, hence no_atp *)
```
```  2432 lemma infinite_UNIV_char_0[no_atp]:
```
```  2433   "\<not> finite (UNIV::'a::semiring_char_0 set)"
```
```  2434 proof
```
```  2435   assume "finite (UNIV::'a set)"
```
```  2436   with subset_UNIV have "finite (range of_nat::'a set)"
```
```  2437     by (rule finite_subset)
```
```  2438   moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
```
```  2439     by (simp add: inj_on_def)
```
```  2440   ultimately have "finite (UNIV::nat set)"
```
```  2441     by (rule finite_imageD)
```
```  2442   then show "False"
```
```  2443     by simp
```
```  2444 qed
```
```  2445
```
```  2446 hide_const (open) Finite_Set.fold
```
```  2447
```
```  2448 end
```
```  2449
```