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