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