src/HOL/Finite_Set.thy
author haftmann
Wed, 10 Mar 2010 16:53:27 +0100
changeset 35715 99b6152aedf5
parent 35577 43b93e294522
child 35718 69419a09a7ff
permissions -rw-r--r--
split off theory Big_Operators from theory Finite_Set
     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 Power Option
    10 begin
    11 
    12 subsection {* Definition and basic properties *}
    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 
   175 subsubsection{* Finiteness and set theoretic constructions *}
   176 
   177 lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
   178 by (induct set: finite) simp_all
   179 
   180 lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
   181   -- {* Every subset of a finite set is finite. *}
   182 proof -
   183   assume "finite B"
   184   thus "!!A. A \<subseteq> B ==> finite A"
   185   proof induct
   186     case empty
   187     thus ?case by simp
   188   next
   189     case (insert x F A)
   190     have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" by fact+
   191     show "finite A"
   192     proof cases
   193       assume x: "x \<in> A"
   194       with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
   195       with r have "finite (A - {x})" .
   196       hence "finite (insert x (A - {x}))" ..
   197       also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
   198       finally show ?thesis .
   199     next
   200       show "A \<subseteq> F ==> ?thesis" by fact
   201       assume "x \<notin> A"
   202       with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
   203     qed
   204   qed
   205 qed
   206 
   207 lemma rev_finite_subset: "finite B ==> A \<subseteq> B ==> finite A"
   208 by (rule finite_subset)
   209 
   210 lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
   211 by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
   212 
   213 lemma finite_Collect_disjI[simp]:
   214   "finite{x. P x | Q x} = (finite{x. P x} & finite{x. Q x})"
   215 by(simp add:Collect_disj_eq)
   216 
   217 lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
   218   -- {* The converse obviously fails. *}
   219 by (blast intro: finite_subset)
   220 
   221 lemma finite_Collect_conjI [simp, intro]:
   222   "finite{x. P x} | finite{x. Q x} ==> finite{x. P x & Q x}"
   223   -- {* The converse obviously fails. *}
   224 by(simp add:Collect_conj_eq)
   225 
   226 lemma finite_Collect_le_nat[iff]: "finite{n::nat. n<=k}"
   227 by(simp add: le_eq_less_or_eq)
   228 
   229 lemma finite_insert [simp]: "finite (insert a A) = finite A"
   230   apply (subst insert_is_Un)
   231   apply (simp only: finite_Un, blast)
   232   done
   233 
   234 lemma finite_Union[simp, intro]:
   235  "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
   236 by (induct rule:finite_induct) simp_all
   237 
   238 lemma finite_Inter[intro]: "EX A:M. finite(A) \<Longrightarrow> finite(Inter M)"
   239 by (blast intro: Inter_lower finite_subset)
   240 
   241 lemma finite_INT[intro]: "EX x:I. finite(A x) \<Longrightarrow> finite(INT x:I. A x)"
   242 by (blast intro: INT_lower finite_subset)
   243 
   244 lemma finite_empty_induct:
   245   assumes "finite A"
   246     and "P A"
   247     and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
   248   shows "P {}"
   249 proof -
   250   have "P (A - A)"
   251   proof -
   252     {
   253       fix c b :: "'a set"
   254       assume c: "finite c" and b: "finite b"
   255         and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
   256       have "c \<subseteq> b ==> P (b - c)"
   257         using c
   258       proof induct
   259         case empty
   260         from P1 show ?case by simp
   261       next
   262         case (insert x F)
   263         have "P (b - F - {x})"
   264         proof (rule P2)
   265           from _ b show "finite (b - F)" by (rule finite_subset) blast
   266           from insert show "x \<in> b - F" by simp
   267           from insert show "P (b - F)" by simp
   268         qed
   269         also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
   270         finally show ?case .
   271       qed
   272     }
   273     then show ?thesis by this (simp_all add: assms)
   274   qed
   275   then show ?thesis by simp
   276 qed
   277 
   278 lemma finite_Diff [simp]: "finite A ==> finite (A - B)"
   279 by (rule Diff_subset [THEN finite_subset])
   280 
   281 lemma finite_Diff2 [simp]:
   282   assumes "finite B" shows "finite (A - B) = finite A"
   283 proof -
   284   have "finite A \<longleftrightarrow> finite((A-B) Un (A Int B))" by(simp add: Un_Diff_Int)
   285   also have "\<dots> \<longleftrightarrow> finite(A-B)" using `finite B` by(simp)
   286   finally show ?thesis ..
   287 qed
   288 
   289 lemma finite_compl[simp]:
   290   "finite(A::'a set) \<Longrightarrow> finite(-A) = finite(UNIV::'a set)"
   291 by(simp add:Compl_eq_Diff_UNIV)
   292 
   293 lemma finite_Collect_not[simp]:
   294   "finite{x::'a. P x} \<Longrightarrow> finite{x. ~P x} = finite(UNIV::'a set)"
   295 by(simp add:Collect_neg_eq)
   296 
   297 lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
   298   apply (subst Diff_insert)
   299   apply (case_tac "a : A - B")
   300    apply (rule finite_insert [symmetric, THEN trans])
   301    apply (subst insert_Diff, simp_all)
   302   done
   303 
   304 
   305 text {* Image and Inverse Image over Finite Sets *}
   306 
   307 lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
   308   -- {* The image of a finite set is finite. *}
   309   by (induct set: finite) simp_all
   310 
   311 lemma finite_image_set [simp]:
   312   "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
   313   by (simp add: image_Collect [symmetric])
   314 
   315 lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
   316   apply (frule finite_imageI)
   317   apply (erule finite_subset, assumption)
   318   done
   319 
   320 lemma finite_range_imageI:
   321     "finite (range g) ==> finite (range (%x. f (g x)))"
   322   apply (drule finite_imageI, simp add: range_composition)
   323   done
   324 
   325 lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
   326 proof -
   327   have aux: "!!A. finite (A - {}) = finite A" by simp
   328   fix B :: "'a set"
   329   assume "finite B"
   330   thus "!!A. f`A = B ==> inj_on f A ==> finite A"
   331     apply induct
   332      apply simp
   333     apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
   334      apply clarify
   335      apply (simp (no_asm_use) add: inj_on_def)
   336      apply (blast dest!: aux [THEN iffD1], atomize)
   337     apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
   338     apply (frule subsetD [OF equalityD2 insertI1], clarify)
   339     apply (rule_tac x = xa in bexI)
   340      apply (simp_all add: inj_on_image_set_diff)
   341     done
   342 qed (rule refl)
   343 
   344 
   345 lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
   346   -- {* The inverse image of a singleton under an injective function
   347          is included in a singleton. *}
   348   apply (auto simp add: inj_on_def)
   349   apply (blast intro: the_equality [symmetric])
   350   done
   351 
   352 lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
   353   -- {* The inverse image of a finite set under an injective function
   354          is finite. *}
   355   apply (induct set: finite)
   356    apply simp_all
   357   apply (subst vimage_insert)
   358   apply (simp add: finite_subset [OF inj_vimage_singleton])
   359   done
   360 
   361 lemma finite_vimageD:
   362   assumes fin: "finite (h -` F)" and surj: "surj h"
   363   shows "finite F"
   364 proof -
   365   have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
   366   also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
   367   finally show "finite F" .
   368 qed
   369 
   370 lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
   371   unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
   372 
   373 
   374 text {* The finite UNION of finite sets *}
   375 
   376 lemma finite_UN_I: "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_UN_I 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]:
   433     "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
   434   by (unfold Sigma_def) (blast intro!: finite_UN_I)
   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 setup {* Sign.add_path "finite" *} -- {*FIXME: name tweaking*}
   514 class finite =
   515   assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
   516 setup {* Sign.parent_path *}
   517 hide const finite
   518 
   519 context finite
   520 begin
   521 
   522 lemma finite [simp]: "finite (A \<Colon> 'a set)"
   523   by (rule subset_UNIV finite_UNIV finite_subset)+
   524 
   525 end
   526 
   527 lemma UNIV_unit [noatp]:
   528   "UNIV = {()}" by auto
   529 
   530 instance unit :: finite proof
   531 qed (simp add: UNIV_unit)
   532 
   533 lemma UNIV_bool [noatp]:
   534   "UNIV = {False, True}" by auto
   535 
   536 instance bool :: finite proof
   537 qed (simp add: UNIV_bool)
   538 
   539 instance * :: (finite, finite) finite proof
   540 qed (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
   541 
   542 lemma finite_option_UNIV [simp]:
   543   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   544   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   545 
   546 instance option :: (finite) finite proof
   547 qed (simp add: UNIV_option_conv)
   548 
   549 lemma inj_graph: "inj (%f. {(x, y). y = f x})"
   550   by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq)
   551 
   552 instance "fun" :: (finite, finite) finite
   553 proof
   554   show "finite (UNIV :: ('a => 'b) set)"
   555   proof (rule finite_imageD)
   556     let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
   557     have "range ?graph \<subseteq> Pow UNIV" by simp
   558     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
   559       by (simp only: finite_Pow_iff finite)
   560     ultimately show "finite (range ?graph)"
   561       by (rule finite_subset)
   562     show "inj ?graph" by (rule inj_graph)
   563   qed
   564 qed
   565 
   566 instance "+" :: (finite, finite) finite proof
   567 qed (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
   568 
   569 
   570 subsection {* A fold functional for finite sets *}
   571 
   572 text {* The intended behaviour is
   573 @{text "fold f z {x\<^isub>1, ..., x\<^isub>n} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
   574 if @{text f} is ``left-commutative'':
   575 *}
   576 
   577 locale fun_left_comm =
   578   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
   579   assumes fun_left_comm: "f x (f y z) = f y (f x z)"
   580 begin
   581 
   582 text{* On a functional level it looks much nicer: *}
   583 
   584 lemma fun_comp_comm:  "f x \<circ> f y = f y \<circ> f x"
   585 by (simp add: fun_left_comm expand_fun_eq)
   586 
   587 end
   588 
   589 inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
   590 for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
   591   emptyI [intro]: "fold_graph f z {} z" |
   592   insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
   593       \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
   594 
   595 inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
   596 
   597 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
   598 [code del]: "fold f z A = (THE y. fold_graph f z A y)"
   599 
   600 text{*A tempting alternative for the definiens is
   601 @{term "if finite A then THE y. fold_graph f z A y else e"}.
   602 It allows the removal of finiteness assumptions from the theorems
   603 @{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
   604 The proofs become ugly. It is not worth the effort. (???) *}
   605 
   606 
   607 lemma Diff1_fold_graph:
   608   "fold_graph f z (A - {x}) y \<Longrightarrow> x \<in> A \<Longrightarrow> fold_graph f z A (f x y)"
   609 by (erule insert_Diff [THEN subst], rule fold_graph.intros, auto)
   610 
   611 lemma fold_graph_imp_finite: "fold_graph f z A x \<Longrightarrow> finite A"
   612 by (induct set: fold_graph) auto
   613 
   614 lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
   615 by (induct set: finite) auto
   616 
   617 
   618 subsubsection{*From @{const fold_graph} to @{term fold}*}
   619 
   620 lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
   621   by (auto simp add: less_Suc_eq) 
   622 
   623 lemma insert_image_inj_on_eq:
   624      "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A; 
   625         inj_on h {i. i < Suc m}|] 
   626       ==> A = h ` {i. i < m}"
   627 apply (auto simp add: image_less_Suc inj_on_def)
   628 apply (blast intro: less_trans) 
   629 done
   630 
   631 lemma insert_inj_onE:
   632   assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A" 
   633       and inj_on: "inj_on h {i::nat. i<n}"
   634   shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
   635 proof (cases n)
   636   case 0 thus ?thesis using aA by auto
   637 next
   638   case (Suc m)
   639   have nSuc: "n = Suc m" by fact
   640   have mlessn: "m<n" by (simp add: nSuc)
   641   from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
   642   let ?hm = "Fun.swap k m h"
   643   have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn 
   644     by (simp add: inj_on)
   645   show ?thesis
   646   proof (intro exI conjI)
   647     show "inj_on ?hm {i. i < m}" using inj_hm
   648       by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
   649     show "m<n" by (rule mlessn)
   650     show "A = ?hm ` {i. i < m}" 
   651     proof (rule insert_image_inj_on_eq)
   652       show "inj_on (Fun.swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
   653       show "?hm m \<notin> A" by (simp add: swap_def hkeq anot) 
   654       show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
   655         using aA hkeq nSuc klessn
   656         by (auto simp add: swap_def image_less_Suc fun_upd_image 
   657                            less_Suc_eq inj_on_image_set_diff [OF inj_on])
   658     qed
   659   qed
   660 qed
   661 
   662 context fun_left_comm
   663 begin
   664 
   665 lemma fold_graph_determ_aux:
   666   "A = h`{i::nat. i<n} \<Longrightarrow> inj_on h {i. i<n}
   667    \<Longrightarrow> fold_graph f z A x \<Longrightarrow> fold_graph f z A x'
   668    \<Longrightarrow> x' = x"
   669 proof (induct n arbitrary: A x x' h rule: less_induct)
   670   case (less n)
   671   have IH: "\<And>m h A x x'. m < n \<Longrightarrow> A = h ` {i. i<m}
   672       \<Longrightarrow> inj_on h {i. i<m} \<Longrightarrow> fold_graph f z A x
   673       \<Longrightarrow> fold_graph f z A x' \<Longrightarrow> x' = x" by fact
   674   have Afoldx: "fold_graph f z A x" and Afoldx': "fold_graph f z A x'"
   675     and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" by fact+
   676   show ?case
   677   proof (rule fold_graph.cases [OF Afoldx])
   678     assume "A = {}" and "x = z"
   679     with Afoldx' show "x' = x" by auto
   680   next
   681     fix B b u
   682     assume AbB: "A = insert b B" and x: "x = f b u"
   683       and notinB: "b \<notin> B" and Bu: "fold_graph f z B u"
   684     show "x'=x" 
   685     proof (rule fold_graph.cases [OF Afoldx'])
   686       assume "A = {}" and "x' = z"
   687       with AbB show "x' = x" by blast
   688     next
   689       fix C c v
   690       assume AcC: "A = insert c C" and x': "x' = f c v"
   691         and notinC: "c \<notin> C" and Cv: "fold_graph f z C v"
   692       from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
   693       from insert_inj_onE [OF Beq notinB injh]
   694       obtain hB mB where inj_onB: "inj_on hB {i. i < mB}" 
   695         and Beq: "B = hB ` {i. i < mB}" and lessB: "mB < n" by auto 
   696       from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
   697       from insert_inj_onE [OF Ceq notinC injh]
   698       obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
   699         and Ceq: "C = hC ` {i. i < mC}" and lessC: "mC < n" by auto 
   700       show "x'=x"
   701       proof cases
   702         assume "b=c"
   703         then moreover have "B = C" using AbB AcC notinB notinC by auto
   704         ultimately show ?thesis  using Bu Cv x x' IH [OF lessC Ceq inj_onC]
   705           by auto
   706       next
   707         assume diff: "b \<noteq> c"
   708         let ?D = "B - {c}"
   709         have B: "B = insert c ?D" and C: "C = insert b ?D"
   710           using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
   711         have "finite A" by(rule fold_graph_imp_finite [OF Afoldx])
   712         with AbB have "finite ?D" by simp
   713         then obtain d where Dfoldd: "fold_graph f z ?D d"
   714           using finite_imp_fold_graph by iprover
   715         moreover have cinB: "c \<in> B" using B by auto
   716         ultimately have "fold_graph f z B (f c d)" by(rule Diff1_fold_graph)
   717         hence "f c d = u" by (rule IH [OF lessB Beq inj_onB Bu]) 
   718         moreover have "f b d = v"
   719         proof (rule IH[OF lessC Ceq inj_onC Cv])
   720           show "fold_graph f z C (f b d)" using C notinB Dfoldd by fastsimp
   721         qed
   722         ultimately show ?thesis
   723           using fun_left_comm [of c b] x x' by (auto simp add: o_def)
   724       qed
   725     qed
   726   qed
   727 qed
   728 
   729 lemma fold_graph_determ:
   730   "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
   731 apply (frule fold_graph_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) 
   732 apply (blast intro: fold_graph_determ_aux [rule_format])
   733 done
   734 
   735 lemma fold_equality:
   736   "fold_graph f z A y \<Longrightarrow> fold f z A = y"
   737 by (unfold fold_def) (blast intro: fold_graph_determ)
   738 
   739 text{* The base case for @{text fold}: *}
   740 
   741 lemma (in -) fold_empty [simp]: "fold f z {} = z"
   742 by (unfold fold_def) blast
   743 
   744 text{* The various recursion equations for @{const fold}: *}
   745 
   746 lemma fold_insert_aux: "x \<notin> A
   747   \<Longrightarrow> fold_graph f z (insert x A) v \<longleftrightarrow>
   748       (\<exists>y. fold_graph f z A y \<and> v = f x y)"
   749 apply auto
   750 apply (rule_tac A1 = A and f1 = f in finite_imp_fold_graph [THEN exE])
   751  apply (fastsimp dest: fold_graph_imp_finite)
   752 apply (blast intro: fold_graph_determ)
   753 done
   754 
   755 lemma fold_insert [simp]:
   756   "finite A ==> x \<notin> A ==> fold f z (insert x A) = f x (fold f z A)"
   757 apply (simp add: fold_def fold_insert_aux)
   758 apply (rule the_equality)
   759  apply (auto intro: finite_imp_fold_graph
   760         cong add: conj_cong simp add: fold_def[symmetric] fold_equality)
   761 done
   762 
   763 lemma fold_fun_comm:
   764   "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
   765 proof (induct rule: finite_induct)
   766   case empty then show ?case by simp
   767 next
   768   case (insert y A) then show ?case
   769     by (simp add: fun_left_comm[of x])
   770 qed
   771 
   772 lemma fold_insert2:
   773   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   774 by (simp add: fold_fun_comm)
   775 
   776 lemma fold_rec:
   777 assumes "finite A" and "x \<in> A"
   778 shows "fold f z A = f x (fold f z (A - {x}))"
   779 proof -
   780   have A: "A = insert x (A - {x})" using `x \<in> A` by blast
   781   then have "fold f z A = fold f z (insert x (A - {x}))" by simp
   782   also have "\<dots> = f x (fold f z (A - {x}))"
   783     by (rule fold_insert) (simp add: `finite A`)+
   784   finally show ?thesis .
   785 qed
   786 
   787 lemma fold_insert_remove:
   788   assumes "finite A"
   789   shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
   790 proof -
   791   from `finite A` have "finite (insert x A)" by auto
   792   moreover have "x \<in> insert x A" by auto
   793   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
   794     by (rule fold_rec)
   795   then show ?thesis by simp
   796 qed
   797 
   798 end
   799 
   800 text{* A simplified version for idempotent functions: *}
   801 
   802 locale fun_left_comm_idem = fun_left_comm +
   803   assumes fun_left_idem: "f x (f x z) = f x z"
   804 begin
   805 
   806 text{* The nice version: *}
   807 lemma fun_comp_idem : "f x o f x = f x"
   808 by (simp add: fun_left_idem expand_fun_eq)
   809 
   810 lemma fold_insert_idem:
   811   assumes fin: "finite A"
   812   shows "fold f z (insert x A) = f x (fold f z A)"
   813 proof cases
   814   assume "x \<in> A"
   815   then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
   816   then show ?thesis using assms by (simp add:fun_left_idem)
   817 next
   818   assume "x \<notin> A" then show ?thesis using assms by simp
   819 qed
   820 
   821 declare fold_insert[simp del] fold_insert_idem[simp]
   822 
   823 lemma fold_insert_idem2:
   824   "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   825 by(simp add:fold_fun_comm)
   826 
   827 end
   828 
   829 context ab_semigroup_idem_mult
   830 begin
   831 
   832 lemma fun_left_comm_idem: "fun_left_comm_idem(op *)"
   833 apply unfold_locales
   834  apply (rule mult_left_commute)
   835 apply (rule mult_left_idem)
   836 done
   837 
   838 end
   839 
   840 context semilattice_inf
   841 begin
   842 
   843 lemma ab_semigroup_idem_mult_inf: "ab_semigroup_idem_mult inf"
   844 proof qed (rule inf_assoc inf_commute inf_idem)+
   845 
   846 lemma fold_inf_insert[simp]: "finite A \<Longrightarrow> fold inf b (insert a A) = inf a (fold inf b A)"
   847 by(rule fun_left_comm_idem.fold_insert_idem[OF ab_semigroup_idem_mult.fun_left_comm_idem[OF ab_semigroup_idem_mult_inf]])
   848 
   849 lemma inf_le_fold_inf: "finite A \<Longrightarrow> ALL a:A. b \<le> a \<Longrightarrow> inf b c \<le> fold inf c A"
   850 by (induct pred: finite) (auto intro: le_infI1)
   851 
   852 lemma fold_inf_le_inf: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> fold inf b A \<le> inf a b"
   853 proof(induct arbitrary: a pred:finite)
   854   case empty thus ?case by simp
   855 next
   856   case (insert x A)
   857   show ?case
   858   proof cases
   859     assume "A = {}" thus ?thesis using insert by simp
   860   next
   861     assume "A \<noteq> {}" thus ?thesis using insert by (auto intro: le_infI2)
   862   qed
   863 qed
   864 
   865 end
   866 
   867 context semilattice_sup
   868 begin
   869 
   870 lemma ab_semigroup_idem_mult_sup: "ab_semigroup_idem_mult sup"
   871 by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)
   872 
   873 lemma fold_sup_insert[simp]: "finite A \<Longrightarrow> fold sup b (insert a A) = sup a (fold sup b A)"
   874 by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)
   875 
   876 lemma fold_sup_le_sup: "finite A \<Longrightarrow> ALL a:A. a \<le> b \<Longrightarrow> fold sup c A \<le> sup b c"
   877 by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)
   878 
   879 lemma sup_le_fold_sup: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a b \<le> fold sup b A"
   880 by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)
   881 
   882 end
   883 
   884 
   885 subsubsection{* The derived combinator @{text fold_image} *}
   886 
   887 definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
   888 where "fold_image f g = fold (%x y. f (g x) y)"
   889 
   890 lemma fold_image_empty[simp]: "fold_image f g z {} = z"
   891 by(simp add:fold_image_def)
   892 
   893 context ab_semigroup_mult
   894 begin
   895 
   896 lemma fold_image_insert[simp]:
   897 assumes "finite A" and "a \<notin> A"
   898 shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
   899 proof -
   900   interpret I: fun_left_comm "%x y. (g x) * y"
   901     by unfold_locales (simp add: mult_ac)
   902   show ?thesis using assms by(simp add:fold_image_def)
   903 qed
   904 
   905 (*
   906 lemma fold_commute:
   907   "finite A ==> (!!z. x * (fold times g z A) = fold times g (x * z) A)"
   908   apply (induct set: finite)
   909    apply simp
   910   apply (simp add: mult_left_commute [of x])
   911   done
   912 
   913 lemma fold_nest_Un_Int:
   914   "finite A ==> finite B
   915     ==> fold times g (fold times g z B) A = fold times g (fold times g z (A Int B)) (A Un B)"
   916   apply (induct set: finite)
   917    apply simp
   918   apply (simp add: fold_commute Int_insert_left insert_absorb)
   919   done
   920 
   921 lemma fold_nest_Un_disjoint:
   922   "finite A ==> finite B ==> A Int B = {}
   923     ==> fold times g z (A Un B) = fold times g (fold times g z B) A"
   924   by (simp add: fold_nest_Un_Int)
   925 *)
   926 
   927 lemma fold_image_reindex:
   928 assumes fin: "finite A"
   929 shows "inj_on h A \<Longrightarrow> fold_image times g z (h`A) = fold_image times (g\<circ>h) z A"
   930 using fin by induct auto
   931 
   932 (*
   933 text{*
   934   Fusion theorem, as described in Graham Hutton's paper,
   935   A Tutorial on the Universality and Expressiveness of Fold,
   936   JFP 9:4 (355-372), 1999.
   937 *}
   938 
   939 lemma fold_fusion:
   940   assumes "ab_semigroup_mult g"
   941   assumes fin: "finite A"
   942     and hyp: "\<And>x y. h (g x y) = times x (h y)"
   943   shows "h (fold g j w A) = fold times j (h w) A"
   944 proof -
   945   class_interpret ab_semigroup_mult [g] by fact
   946   show ?thesis using fin hyp by (induct set: finite) simp_all
   947 qed
   948 *)
   949 
   950 lemma fold_image_cong:
   951   "finite A \<Longrightarrow>
   952   (!!x. x:A ==> g x = h x) ==> fold_image times g z A = fold_image times h z A"
   953 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")
   954  apply simp
   955 apply (erule finite_induct, simp)
   956 apply (simp add: subset_insert_iff, clarify)
   957 apply (subgoal_tac "finite C")
   958  prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
   959 apply (subgoal_tac "C = insert x (C - {x})")
   960  prefer 2 apply blast
   961 apply (erule ssubst)
   962 apply (drule spec)
   963 apply (erule (1) notE impE)
   964 apply (simp add: Ball_def del: insert_Diff_single)
   965 done
   966 
   967 end
   968 
   969 context comm_monoid_mult
   970 begin
   971 
   972 lemma fold_image_Un_Int:
   973   "finite A ==> finite B ==>
   974     fold_image times g 1 A * fold_image times g 1 B =
   975     fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
   976 by (induct set: finite) 
   977    (auto simp add: mult_ac insert_absorb Int_insert_left)
   978 
   979 corollary fold_Un_disjoint:
   980   "finite A ==> finite B ==> A Int B = {} ==>
   981    fold_image times g 1 (A Un B) =
   982    fold_image times g 1 A * fold_image times g 1 B"
   983 by (simp add: fold_image_Un_Int)
   984 
   985 lemma fold_image_UN_disjoint:
   986   "\<lbrakk> finite I; ALL i:I. finite (A i);
   987      ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
   988    \<Longrightarrow> fold_image times g 1 (UNION I A) =
   989        fold_image times (%i. fold_image times g 1 (A i)) 1 I"
   990 apply (induct set: finite, simp, atomize)
   991 apply (subgoal_tac "ALL i:F. x \<noteq> i")
   992  prefer 2 apply blast
   993 apply (subgoal_tac "A x Int UNION F A = {}")
   994  prefer 2 apply blast
   995 apply (simp add: fold_Un_disjoint)
   996 done
   997 
   998 lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
   999   fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
  1000   fold_image times (split g) 1 (SIGMA x:A. B x)"
  1001 apply (subst Sigma_def)
  1002 apply (subst fold_image_UN_disjoint, assumption, simp)
  1003  apply blast
  1004 apply (erule fold_image_cong)
  1005 apply (subst fold_image_UN_disjoint, simp, simp)
  1006  apply blast
  1007 apply simp
  1008 done
  1009 
  1010 lemma fold_image_distrib: "finite A \<Longrightarrow>
  1011    fold_image times (%x. g x * h x) 1 A =
  1012    fold_image times g 1 A *  fold_image times h 1 A"
  1013 by (erule finite_induct) (simp_all add: mult_ac)
  1014 
  1015 lemma fold_image_related: 
  1016   assumes Re: "R e e" 
  1017   and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
  1018   and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
  1019   shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
  1020   using fS by (rule finite_subset_induct) (insert assms, auto)
  1021 
  1022 lemma  fold_image_eq_general:
  1023   assumes fS: "finite S"
  1024   and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y" 
  1025   and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
  1026   shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
  1027 proof-
  1028   from h f12 have hS: "h ` S = S'" by auto
  1029   {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
  1030     from f12 h H  have "x = y" by auto }
  1031   hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
  1032   from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
  1033   from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
  1034   also have "\<dots> = fold_image (op *) (f2 o h) e S" 
  1035     using fold_image_reindex[OF fS hinj, of f2 e] .
  1036   also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
  1037     by blast
  1038   finally show ?thesis ..
  1039 qed
  1040 
  1041 lemma fold_image_eq_general_inverses:
  1042   assumes fS: "finite S" 
  1043   and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
  1044   and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
  1045   shows "fold_image (op *) f e S = fold_image (op *) g e T"
  1046   (* metis solves it, but not yet available here *)
  1047   apply (rule fold_image_eq_general[OF fS, of T h g f e])
  1048   apply (rule ballI)
  1049   apply (frule kh)
  1050   apply (rule ex1I[])
  1051   apply blast
  1052   apply clarsimp
  1053   apply (drule hk) apply simp
  1054   apply (rule sym)
  1055   apply (erule conjunct1[OF conjunct2[OF hk]])
  1056   apply (rule ballI)
  1057   apply (drule  hk)
  1058   apply blast
  1059   done
  1060 
  1061 end
  1062 
  1063 
  1064 subsection{* A fold functional for non-empty sets *}
  1065 
  1066 text{* Does not require start value. *}
  1067 
  1068 inductive
  1069   fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
  1070   for f :: "'a => 'a => 'a"
  1071 where
  1072   fold1Set_insertI [intro]:
  1073    "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
  1074 
  1075 definition fold1 :: "('a => 'a => 'a) => 'a set => 'a" where
  1076   "fold1 f A == THE x. fold1Set f A x"
  1077 
  1078 lemma fold1Set_nonempty:
  1079   "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
  1080 by(erule fold1Set.cases, simp_all)
  1081 
  1082 inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
  1083 
  1084 inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
  1085 
  1086 
  1087 lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
  1088 by (blast elim: fold_graph.cases)
  1089 
  1090 lemma fold1_singleton [simp]: "fold1 f {a} = a"
  1091 by (unfold fold1_def) blast
  1092 
  1093 lemma finite_nonempty_imp_fold1Set:
  1094   "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
  1095 apply (induct A rule: finite_induct)
  1096 apply (auto dest: finite_imp_fold_graph [of _ f])
  1097 done
  1098 
  1099 text{*First, some lemmas about @{const fold_graph}.*}
  1100 
  1101 context ab_semigroup_mult
  1102 begin
  1103 
  1104 lemma fun_left_comm: "fun_left_comm(op *)"
  1105 by unfold_locales (simp add: mult_ac)
  1106 
  1107 lemma fold_graph_insert_swap:
  1108 assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
  1109 shows "fold_graph times z (insert b A) (z * y)"
  1110 proof -
  1111   interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
  1112 from assms show ?thesis
  1113 proof (induct rule: fold_graph.induct)
  1114   case emptyI thus ?case by (force simp add: fold_insert_aux mult_commute)
  1115 next
  1116   case (insertI x A y)
  1117     have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
  1118       using insertI by force  --{*how does @{term id} get unfolded?*}
  1119     thus ?case by (simp add: insert_commute mult_ac)
  1120 qed
  1121 qed
  1122 
  1123 lemma fold_graph_permute_diff:
  1124 assumes fold: "fold_graph times b A x"
  1125 shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
  1126 using fold
  1127 proof (induct rule: fold_graph.induct)
  1128   case emptyI thus ?case by simp
  1129 next
  1130   case (insertI x A y)
  1131   have "a = x \<or> a \<in> A" using insertI by simp
  1132   thus ?case
  1133   proof
  1134     assume "a = x"
  1135     with insertI show ?thesis
  1136       by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
  1137   next
  1138     assume ainA: "a \<in> A"
  1139     hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
  1140       using insertI by force
  1141     moreover
  1142     have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
  1143       using ainA insertI by blast
  1144     ultimately show ?thesis by simp
  1145   qed
  1146 qed
  1147 
  1148 lemma fold1_eq_fold:
  1149 assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
  1150 proof -
  1151   interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
  1152   from assms show ?thesis
  1153 apply (simp add: fold1_def fold_def)
  1154 apply (rule the_equality)
  1155 apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
  1156 apply (rule sym, clarify)
  1157 apply (case_tac "Aa=A")
  1158  apply (best intro: fold_graph_determ)
  1159 apply (subgoal_tac "fold_graph times a A x")
  1160  apply (best intro: fold_graph_determ)
  1161 apply (subgoal_tac "insert aa (Aa - {a}) = A")
  1162  prefer 2 apply (blast elim: equalityE)
  1163 apply (auto dest: fold_graph_permute_diff [where a=a])
  1164 done
  1165 qed
  1166 
  1167 lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
  1168 apply safe
  1169  apply simp
  1170  apply (drule_tac x=x in spec)
  1171  apply (drule_tac x="A-{x}" in spec, auto)
  1172 done
  1173 
  1174 lemma fold1_insert:
  1175   assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
  1176   shows "fold1 times (insert x A) = x * fold1 times A"
  1177 proof -
  1178   interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
  1179   from nonempty obtain a A' where "A = insert a A' & a ~: A'"
  1180     by (auto simp add: nonempty_iff)
  1181   with A show ?thesis
  1182     by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
  1183 qed
  1184 
  1185 end
  1186 
  1187 context ab_semigroup_idem_mult
  1188 begin
  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 {* Expressing set operations via @{const fold} *}
  1358 
  1359 lemma (in fun_left_comm) fun_left_comm_apply:
  1360   "fun_left_comm (\<lambda>x. f (g x))"
  1361 proof
  1362 qed (simp_all add: fun_left_comm)
  1363 
  1364 lemma (in fun_left_comm_idem) fun_left_comm_idem_apply:
  1365   "fun_left_comm_idem (\<lambda>x. f (g x))"
  1366   by (rule fun_left_comm_idem.intro, rule fun_left_comm_apply, unfold_locales)
  1367     (simp_all add: fun_left_idem)
  1368 
  1369 lemma fun_left_comm_idem_insert:
  1370   "fun_left_comm_idem insert"
  1371 proof
  1372 qed auto
  1373 
  1374 lemma fun_left_comm_idem_remove:
  1375   "fun_left_comm_idem (\<lambda>x A. A - {x})"
  1376 proof
  1377 qed auto
  1378 
  1379 lemma (in semilattice_inf) fun_left_comm_idem_inf:
  1380   "fun_left_comm_idem inf"
  1381 proof
  1382 qed (auto simp add: inf_left_commute)
  1383 
  1384 lemma (in semilattice_sup) fun_left_comm_idem_sup:
  1385   "fun_left_comm_idem sup"
  1386 proof
  1387 qed (auto simp add: sup_left_commute)
  1388 
  1389 lemma union_fold_insert:
  1390   assumes "finite A"
  1391   shows "A \<union> B = fold insert B A"
  1392 proof -
  1393   interpret fun_left_comm_idem insert by (fact fun_left_comm_idem_insert)
  1394   from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
  1395 qed
  1396 
  1397 lemma minus_fold_remove:
  1398   assumes "finite A"
  1399   shows "B - A = fold (\<lambda>x A. A - {x}) B A"
  1400 proof -
  1401   interpret fun_left_comm_idem "\<lambda>x A. A - {x}" by (fact fun_left_comm_idem_remove)
  1402   from `finite A` show ?thesis by (induct A arbitrary: B) auto
  1403 qed
  1404 
  1405 context complete_lattice
  1406 begin
  1407 
  1408 lemma inf_Inf_fold_inf:
  1409   assumes "finite A"
  1410   shows "inf B (Inf A) = fold inf B A"
  1411 proof -
  1412   interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
  1413   from `finite A` show ?thesis by (induct A arbitrary: B)
  1414     (simp_all add: Inf_empty Inf_insert inf_commute fold_fun_comm)
  1415 qed
  1416 
  1417 lemma sup_Sup_fold_sup:
  1418   assumes "finite A"
  1419   shows "sup B (Sup A) = fold sup B A"
  1420 proof -
  1421   interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
  1422   from `finite A` show ?thesis by (induct A arbitrary: B)
  1423     (simp_all add: Sup_empty Sup_insert sup_commute fold_fun_comm)
  1424 qed
  1425 
  1426 lemma Inf_fold_inf:
  1427   assumes "finite A"
  1428   shows "Inf A = fold inf top A"
  1429   using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
  1430 
  1431 lemma Sup_fold_sup:
  1432   assumes "finite A"
  1433   shows "Sup A = fold sup bot A"
  1434   using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
  1435 
  1436 lemma inf_INFI_fold_inf:
  1437   assumes "finite A"
  1438   shows "inf B (INFI A f) = fold (\<lambda>A. inf (f A)) B A" (is "?inf = ?fold") 
  1439 proof (rule sym)
  1440   interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
  1441   interpret fun_left_comm_idem "\<lambda>A. inf (f A)" by (fact fun_left_comm_idem_apply)
  1442   from `finite A` show "?fold = ?inf"
  1443   by (induct A arbitrary: B)
  1444     (simp_all add: INFI_def Inf_empty Inf_insert inf_left_commute)
  1445 qed
  1446 
  1447 lemma sup_SUPR_fold_sup:
  1448   assumes "finite A"
  1449   shows "sup B (SUPR A f) = fold (\<lambda>A. sup (f A)) B A" (is "?sup = ?fold") 
  1450 proof (rule sym)
  1451   interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
  1452   interpret fun_left_comm_idem "\<lambda>A. sup (f A)" by (fact fun_left_comm_idem_apply)
  1453   from `finite A` show "?fold = ?sup"
  1454   by (induct A arbitrary: B)
  1455     (simp_all add: SUPR_def Sup_empty Sup_insert sup_left_commute)
  1456 qed
  1457 
  1458 lemma INFI_fold_inf:
  1459   assumes "finite A"
  1460   shows "INFI A f = fold (\<lambda>A. inf (f A)) top A"
  1461   using assms inf_INFI_fold_inf [of A top] by simp
  1462 
  1463 lemma SUPR_fold_sup:
  1464   assumes "finite A"
  1465   shows "SUPR A f = fold (\<lambda>A. sup (f A)) bot A"
  1466   using assms sup_SUPR_fold_sup [of A bot] by simp
  1467 
  1468 end
  1469 
  1470 
  1471 subsection {* Locales as mini-packages *}
  1472 
  1473 locale folding =
  1474   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1475   fixes F :: "'a set \<Rightarrow> 'b \<Rightarrow> 'b"
  1476   assumes commute_comp: "f x \<circ> f y = f y \<circ> f x"
  1477   assumes eq_fold: "F A s = Finite_Set.fold f s A"
  1478 begin
  1479 
  1480 lemma fun_left_commute:
  1481   "f x (f y s) = f y (f x s)"
  1482   using commute_comp [of x y] by (simp add: expand_fun_eq)
  1483 
  1484 lemma fun_left_comm:
  1485   "fun_left_comm f"
  1486 proof
  1487 qed (fact fun_left_commute)
  1488 
  1489 lemma empty [simp]:
  1490   "F {} = id"
  1491   by (simp add: eq_fold expand_fun_eq)
  1492 
  1493 lemma insert [simp]:
  1494   assumes "finite A" and "x \<notin> A"
  1495   shows "F (insert x A) = F A \<circ> f x"
  1496 proof -
  1497   interpret fun_left_comm f by (fact fun_left_comm)
  1498   from fold_insert2 assms
  1499   have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
  1500   then show ?thesis by (simp add: eq_fold expand_fun_eq)
  1501 qed
  1502 
  1503 lemma remove:
  1504   assumes "finite A" and "x \<in> A"
  1505   shows "F A = F (A - {x}) \<circ> f x"
  1506 proof -
  1507   from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
  1508     by (auto dest: mk_disjoint_insert)
  1509   moreover from `finite A` this have "finite B" by simp
  1510   ultimately show ?thesis by simp
  1511 qed
  1512 
  1513 lemma insert_remove:
  1514   assumes "finite A"
  1515   shows "F (insert x A) = F (A - {x}) \<circ> f x"
  1516 proof (cases "x \<in> A")
  1517   case True with assms show ?thesis by (simp add: remove insert_absorb)
  1518 next
  1519   case False with assms show ?thesis by simp
  1520 qed
  1521 
  1522 lemma commute_comp':
  1523   assumes "finite A"
  1524   shows "f x \<circ> F A = F A \<circ> f x"
  1525 proof (rule ext)
  1526   fix s
  1527   from assms show "(f x \<circ> F A) s = (F A \<circ> f x) s"
  1528     by (induct A arbitrary: s) (simp_all add: fun_left_commute)
  1529 qed
  1530 
  1531 lemma fun_left_commute':
  1532   assumes "finite A"
  1533   shows "f x (F A s) = F A (f x s)"
  1534   using commute_comp' assms by (simp add: expand_fun_eq)
  1535 
  1536 lemma union:
  1537   assumes "finite A" and "finite B"
  1538   and "A \<inter> B = {}"
  1539   shows "F (A \<union> B) = F A \<circ> F B"
  1540 using `finite A` `A \<inter> B = {}` proof (induct A)
  1541   case empty show ?case by simp
  1542 next
  1543   case (insert x A)
  1544   then have "A \<inter> B = {}" by auto
  1545   with insert(3) have "F (A \<union> B) = F A \<circ> F B" .
  1546   moreover from insert have "x \<notin> B" by simp
  1547   moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
  1548   moreover from `x \<notin> A` `x \<notin> B` have "x \<notin> A \<union> B" by simp
  1549   ultimately show ?case by (simp add: fun_left_commute')
  1550 qed
  1551 
  1552 end
  1553 
  1554 locale folding_idem = folding +
  1555   assumes idem_comp: "f x \<circ> f x = f x"
  1556 begin
  1557 
  1558 declare insert [simp del]
  1559 
  1560 lemma fun_idem:
  1561   "f x (f x s) = f x s"
  1562   using idem_comp [of x] by (simp add: expand_fun_eq)
  1563 
  1564 lemma fun_left_comm_idem:
  1565   "fun_left_comm_idem f"
  1566 proof
  1567 qed (fact fun_left_commute fun_idem)+
  1568 
  1569 lemma insert_idem [simp]:
  1570   assumes "finite A"
  1571   shows "F (insert x A) = F A \<circ> f x"
  1572 proof -
  1573   interpret fun_left_comm_idem f by (fact fun_left_comm_idem)
  1574   from fold_insert_idem2 assms
  1575   have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
  1576   then show ?thesis by (simp add: eq_fold expand_fun_eq)
  1577 qed
  1578 
  1579 lemma union_idem:
  1580   assumes "finite A" and "finite B"
  1581   shows "F (A \<union> B) = F A \<circ> F B"
  1582 using `finite A` proof (induct A)
  1583   case empty show ?case by simp
  1584 next
  1585   case (insert x A)
  1586   from insert(3) have "F (A \<union> B) = F A \<circ> F B" .
  1587   moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
  1588   ultimately show ?case by (simp add: fun_left_commute')
  1589 qed
  1590 
  1591 end
  1592 
  1593 end