fundamental revision of big operators on sets
authorhaftmann
Sat Mar 23 20:50:39 2013 +0100 (2013-03-23)
changeset 51489f738e6dbd844
parent 51488 3c886fe611b8
child 51490 7edcc0618dae
fundamental revision of big operators on sets
CONTRIBUTORS
NEWS
src/Doc/Main/Main_Doc.thy
src/HOL/Algebra/poly/UnivPoly2.thy
src/HOL/BNF/More_BNFs.thy
src/HOL/Big_Operators.thy
src/HOL/Complete_Lattices.thy
src/HOL/Finite_Set.thy
src/HOL/GCD.thy
src/HOL/HOLCF/Compact_Basis.thy
src/HOL/HOLCF/ConvexPD.thy
src/HOL/HOLCF/LowerPD.thy
src/HOL/HOLCF/UpperPD.thy
src/HOL/Lattices.thy
src/HOL/Library/Finite_Lattice.thy
src/HOL/Library/Formal_Power_Series.thy
src/HOL/Library/Function_Algebras.thy
src/HOL/Library/Nat_Bijection.thy
src/HOL/Library/Permutations.thy
src/HOL/Library/RBT_Set.thy
src/HOL/Library/Saturated.thy
src/HOL/List.thy
src/HOL/MacLaurin.thy
src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Determinants.thy
src/HOL/Multivariate_Analysis/Integration.thy
src/HOL/Number_Theory/UniqueFactorization.thy
src/HOL/Old_Number_Theory/Finite2.thy
src/HOL/Old_Number_Theory/Pocklington.thy
src/HOL/Probability/Fin_Map.thy
     1.1 --- a/CONTRIBUTORS	Sat Mar 23 17:11:06 2013 +0100
     1.2 +++ b/CONTRIBUTORS	Sat Mar 23 20:50:39 2013 +0100
     1.3 @@ -7,6 +7,9 @@
     1.4  --------------------------------------
     1.5  
     1.6  * March 2013: Florian Haftmann, TUM
     1.7 +  Reform of "big operators" on sets.
     1.8 +
     1.9 +* March 2013: Florian Haftmann, TUM
    1.10    Algebraic locale hierarchy for orderings and (semi)lattices.
    1.11  
    1.12  * Feb. 2013: Florian Haftmann, TUM
     2.1 --- a/NEWS	Sat Mar 23 17:11:06 2013 +0100
     2.2 +++ b/NEWS	Sat Mar 23 20:50:39 2013 +0100
     2.3 @@ -33,6 +33,28 @@
     2.4  
     2.5  *** HOL ***
     2.6  
     2.7 +* Revised devices for recursive definitions over finite sets:
     2.8 +  - Only one fundamental fold combinator on finite set remains:
     2.9 +    Finite_Set.fold :: ('a => 'b => 'b) => 'b => 'a set => 'b
    2.10 +    This is now identity on infinite sets.
    2.11 +  - Locales (Ā»mini packagesĀ«) for fundamental definitions with
    2.12 +    Finite_Set.fold: folding, folding_idem.
    2.13 +  - Locales comm_monoid_set, semilattice_order_set and
    2.14 +    semilattice_neutr_order_set for big operators on sets.
    2.15 +    See theory Big_Operators for canonical examples.
    2.16 +    Note that foundational constants comm_monoid_set.F and
    2.17 +    semilattice_set.F correspond to former combinators fold_image
    2.18 +    and fold1 respectively.  These are now gone.  You may use
    2.19 +    those foundational counstants as substitutes, but it is
    2.20 +    preferable to interpret the above locales accordingly. 
    2.21 +  - Dropped class ab_semigroup_idem_mult (special case of lattice,
    2.22 +    no longer needed in connection with Finite_Set.fold etc.)
    2.23 +  - Fact renames:
    2.24 +      card.union_inter ~> card_Un_Int [symmetric]
    2.25 +      card.union_disjoint ~> card_Un_disjoint
    2.26 +
    2.27 +INCOMPATIBILITY.
    2.28 +
    2.29  * Locale hierarchy for abstract orderings and (semi)lattices.
    2.30  
    2.31  * Discontinued theory src/HOL/Library/Eval_Witness.
     3.1 --- a/src/Doc/Main/Main_Doc.thy	Sat Mar 23 17:11:06 2013 +0100
     3.2 +++ b/src/Doc/Main/Main_Doc.thy	Sat Mar 23 20:50:39 2013 +0100
     3.3 @@ -406,7 +406,6 @@
     3.4  @{const Finite_Set.finite} & @{term_type_only Finite_Set.finite "'a set\<Rightarrow>bool"}\\
     3.5  @{const Finite_Set.card} & @{term_type_only Finite_Set.card "'a set => nat"}\\
     3.6  @{const Finite_Set.fold} & @{term_type_only Finite_Set.fold "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"}\\
     3.7 -@{const Finite_Set.fold_image} & @{typ "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"}\\
     3.8  @{const Big_Operators.setsum} & @{term_type_only Big_Operators.setsum "('a => 'b) => 'a set => 'b::comm_monoid_add"}\\
     3.9  @{const Big_Operators.setprod} & @{term_type_only Big_Operators.setprod "('a => 'b) => 'a set => 'b::comm_monoid_mult"}\\
    3.10  \end{supertabular}
     4.1 --- a/src/HOL/Algebra/poly/UnivPoly2.thy	Sat Mar 23 17:11:06 2013 +0100
     4.2 +++ b/src/HOL/Algebra/poly/UnivPoly2.thy	Sat Mar 23 20:50:39 2013 +0100
     4.3 @@ -687,7 +687,7 @@
     4.4  proof (cases "finite A")
     4.5    case True then show ?thesis by induct auto
     4.6  next
     4.7 -  case False then show ?thesis by (simp add: setsum_def)
     4.8 +  case False then show ?thesis by simp
     4.9  qed
    4.10  (* Instance of a more general result!!! *)
    4.11  
     5.1 --- a/src/HOL/BNF/More_BNFs.thy	Sat Mar 23 17:11:06 2013 +0100
     5.2 +++ b/src/HOL/BNF/More_BNFs.thy	Sat Mar 23 20:50:39 2013 +0100
     5.3 @@ -548,16 +548,16 @@
     5.4  unfolding mcard_def by auto
     5.5  
     5.6  lemma mcard_Plus[simp]: "mcard (M + N) = mcard M + mcard N"
     5.7 -proof-
     5.8 +proof -
     5.9    have "setsum (count M) {a. 0 < count M a + count N a} =
    5.10          setsum (count M) {a. a \<in># M}"
    5.11 -  apply(rule setsum_mono_zero_cong_right) by auto
    5.12 +  apply (rule setsum_mono_zero_cong_right) by auto
    5.13    moreover
    5.14    have "setsum (count N) {a. 0 < count M a + count N a} =
    5.15          setsum (count N) {a. a \<in># N}"
    5.16 -  apply(rule setsum_mono_zero_cong_right) by auto
    5.17 +  apply (rule setsum_mono_zero_cong_right) by auto
    5.18    ultimately show ?thesis
    5.19 -  unfolding mcard_def count_union[THEN ext] comm_monoid_add_class.setsum.F_fun_f by simp
    5.20 +  unfolding mcard_def count_union [THEN ext] by (simp add: setsum.distrib)
    5.21  qed
    5.22  
    5.23  lemma setsum_gt_0_iff:
    5.24 @@ -1207,7 +1207,7 @@
    5.25    have "setsum L {aa. f aa = a \<and> 0 < L aa} = setsum L {aa. f aa = a \<and> 0 < K aa + L aa}"
    5.26    apply(rule setsum_mono_zero_cong_left) using C by auto
    5.27    ultimately show ?thesis
    5.28 -  unfolding mmap_def unfolding comm_monoid_add_class.setsum.F_fun_f by auto
    5.29 +  unfolding mmap_def by (auto simp add: setsum.distrib)
    5.30  qed
    5.31  
    5.32  lemma multiset_map_Plus[simp]:
    5.33 @@ -1265,10 +1265,10 @@
    5.34    have "setsum (\<lambda> x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
    5.35    unfolding comp_def ..
    5.36    also have "... = (\<Sum>x\<in> A ` ?B. setsum (count M) x)"
    5.37 -  unfolding comm_monoid_add_class.setsum_reindex[OF i, symmetric] ..
    5.38 +  unfolding setsum.reindex [OF i, symmetric] ..
    5.39    also have "... = setsum (count M) (\<Union>x\<in>A ` {b. 0 < setsum (count M) (A b)}. x)"
    5.40    (is "_ = setsum (count M) ?J")
    5.41 -  apply(rule comm_monoid_add_class.setsum_UN_disjoint[symmetric])
    5.42 +  apply(rule setsum.UNION_disjoint[symmetric])
    5.43    using 0 fin unfolding A_def by (auto intro!: finite_imageI)
    5.44    also have "?J = {a. a \<in># M}" unfolding AB unfolding A_def by auto
    5.45    finally have "setsum (\<lambda> x. setsum (count M) (A x)) ?B =
     6.1 --- a/src/HOL/Big_Operators.thy	Sat Mar 23 17:11:06 2013 +0100
     6.2 +++ b/src/HOL/Big_Operators.thy	Sat Mar 23 20:50:39 2013 +0100
     6.3 @@ -6,7 +6,7 @@
     6.4  header {* Big operators and finite (non-empty) sets *}
     6.5  
     6.6  theory Big_Operators
     6.7 -imports Finite_Set Metis
     6.8 +imports Finite_Set Option Metis
     6.9  begin
    6.10  
    6.11  subsection {* Generic monoid operation over a set *}
    6.12 @@ -14,46 +14,223 @@
    6.13  no_notation times (infixl "*" 70)
    6.14  no_notation Groups.one ("1")
    6.15  
    6.16 -locale comm_monoid_big = comm_monoid +
    6.17 -  fixes F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    6.18 -  assumes F_eq: "F g A = (if finite A then fold_image (op *) g 1 A else 1)"
    6.19 +locale comm_monoid_set = comm_monoid
    6.20 +begin
    6.21  
    6.22 -sublocale comm_monoid_big < folding_image proof
    6.23 -qed (simp add: F_eq)
    6.24 -
    6.25 -context comm_monoid_big
    6.26 -begin
    6.27 +definition F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    6.28 +where
    6.29 +  eq_fold: "F g A = Finite_Set.fold (f \<circ> g) 1 A"
    6.30  
    6.31  lemma infinite [simp]:
    6.32    "\<not> finite A \<Longrightarrow> F g A = 1"
    6.33 -  by (simp add: F_eq)
    6.34 +  by (simp add: eq_fold)
    6.35 +
    6.36 +lemma empty [simp]:
    6.37 +  "F g {} = 1"
    6.38 +  by (simp add: eq_fold)
    6.39 +
    6.40 +lemma insert [simp]:
    6.41 +  assumes "finite A" and "x \<notin> A"
    6.42 +  shows "F g (insert x A) = g x * F g A"
    6.43 +proof -
    6.44 +  interpret comp_fun_commute f
    6.45 +    by default (simp add: fun_eq_iff left_commute)
    6.46 +  interpret comp_fun_commute "f \<circ> g"
    6.47 +    by (rule comp_comp_fun_commute)
    6.48 +  from assms show ?thesis by (simp add: eq_fold)
    6.49 +qed
    6.50 +
    6.51 +lemma remove:
    6.52 +  assumes "finite A" and "x \<in> A"
    6.53 +  shows "F g A = g x * F g (A - {x})"
    6.54 +proof -
    6.55 +  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
    6.56 +    by (auto dest: mk_disjoint_insert)
    6.57 +  moreover from `finite A` this have "finite B" by simp
    6.58 +  ultimately show ?thesis by simp
    6.59 +qed
    6.60 +
    6.61 +lemma insert_remove:
    6.62 +  assumes "finite A"
    6.63 +  shows "F g (insert x A) = g x * F g (A - {x})"
    6.64 +  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
    6.65 +
    6.66 +lemma neutral:
    6.67 +  assumes "\<forall>x\<in>A. g x = 1"
    6.68 +  shows "F g A = 1"
    6.69 +proof (cases "finite A")
    6.70 +  case True from `finite A` assms show ?thesis by (induct A) simp_all
    6.71 +next
    6.72 +  case False then show ?thesis by simp
    6.73 +qed
    6.74  
    6.75 -lemma F_cong:
    6.76 -  assumes "A = B" "\<And>x. x \<in> B \<Longrightarrow> h x = g x"
    6.77 -  shows "F h A = F g B"
    6.78 -proof cases
    6.79 -  assume "finite A"
    6.80 -  with assms show ?thesis unfolding `A = B` by (simp cong: cong)
    6.81 +lemma neutral_const [simp]:
    6.82 +  "F (\<lambda>_. 1) A = 1"
    6.83 +  by (simp add: neutral)
    6.84 +
    6.85 +lemma union_inter:
    6.86 +  assumes "finite A" and "finite B"
    6.87 +  shows "F g (A \<union> B) * F g (A \<inter> B) = F g A * F g B"
    6.88 +  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
    6.89 +using assms proof (induct A)
    6.90 +  case empty then show ?case by simp
    6.91  next
    6.92 -  assume "\<not> finite A"
    6.93 -  then show ?thesis unfolding `A = B` by simp
    6.94 +  case (insert x A) then show ?case
    6.95 +    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
    6.96 +qed
    6.97 +
    6.98 +corollary union_inter_neutral:
    6.99 +  assumes "finite A" and "finite B"
   6.100 +  and I0: "\<forall>x \<in> A \<inter> B. g x = 1"
   6.101 +  shows "F g (A \<union> B) = F g A * F g B"
   6.102 +  using assms by (simp add: union_inter [symmetric] neutral)
   6.103 +
   6.104 +corollary union_disjoint:
   6.105 +  assumes "finite A" and "finite B"
   6.106 +  assumes "A \<inter> B = {}"
   6.107 +  shows "F g (A \<union> B) = F g A * F g B"
   6.108 +  using assms by (simp add: union_inter_neutral)
   6.109 +
   6.110 +lemma subset_diff:
   6.111 +  "B \<subseteq> A \<Longrightarrow> finite A \<Longrightarrow> F g A = F g (A - B) * F g B"
   6.112 +  by (metis Diff_partition union_disjoint Diff_disjoint finite_Un inf_commute sup_commute)
   6.113 +
   6.114 +lemma reindex:
   6.115 +  assumes "inj_on h A"
   6.116 +  shows "F g (h ` A) = F (g \<circ> h) A"
   6.117 +proof (cases "finite A")
   6.118 +  case True
   6.119 +  interpret comp_fun_commute f
   6.120 +    by default (simp add: fun_eq_iff left_commute)
   6.121 +  interpret comp_fun_commute "f \<circ> g"
   6.122 +    by (rule comp_comp_fun_commute)
   6.123 +  from assms `finite A` show ?thesis by (simp add: eq_fold fold_image comp_assoc)
   6.124 +next
   6.125 +  case False with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
   6.126 +  with False show ?thesis by simp
   6.127  qed
   6.128  
   6.129 -lemma strong_F_cong [cong]:
   6.130 -  "\<lbrakk> A = B; !!x. x:B =simp=> g x = h x \<rbrakk>
   6.131 -   \<Longrightarrow> F (%x. g x) A = F (%x. h x) B"
   6.132 -by (rule F_cong) (simp_all add: simp_implies_def)
   6.133 +lemma cong:
   6.134 +  assumes "A = B"
   6.135 +  assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x"
   6.136 +  shows "F g A = F h B"
   6.137 +proof (cases "finite A")
   6.138 +  case True
   6.139 +  then have "\<And>C. C \<subseteq> A \<longrightarrow> (\<forall>x\<in>C. g x = h x) \<longrightarrow> F g C = F h C"
   6.140 +  proof induct
   6.141 +    case empty then show ?case by simp
   6.142 +  next
   6.143 +    case (insert x F) then show ?case apply -
   6.144 +    apply (simp add: subset_insert_iff, clarify)
   6.145 +    apply (subgoal_tac "finite C")
   6.146 +      prefer 2 apply (blast dest: finite_subset [rotated])
   6.147 +    apply (subgoal_tac "C = insert x (C - {x})")
   6.148 +      prefer 2 apply blast
   6.149 +    apply (erule ssubst)
   6.150 +    apply (simp add: Ball_def del: insert_Diff_single)
   6.151 +    done
   6.152 +  qed
   6.153 +  with `A = B` g_h show ?thesis by simp
   6.154 +next
   6.155 +  case False
   6.156 +  with `A = B` show ?thesis by simp
   6.157 +qed
   6.158  
   6.159 -lemma F_neutral[simp]: "F (%i. 1) A = 1"
   6.160 -by (cases "finite A") (simp_all add: neutral)
   6.161 +lemma strong_cong [cong]:
   6.162 +  assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x"
   6.163 +  shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B"
   6.164 +  by (rule cong) (insert assms, simp_all add: simp_implies_def)
   6.165 +
   6.166 +lemma UNION_disjoint:
   6.167 +  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
   6.168 +  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
   6.169 +  shows "F g (UNION I A) = F (\<lambda>x. F g (A x)) I"
   6.170 +apply (insert assms)
   6.171 +apply (induct rule: finite_induct)
   6.172 +apply simp
   6.173 +apply atomize
   6.174 +apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
   6.175 + prefer 2 apply blast
   6.176 +apply (subgoal_tac "A x Int UNION Fa A = {}")
   6.177 + prefer 2 apply blast
   6.178 +apply (simp add: union_disjoint)
   6.179 +done
   6.180 +
   6.181 +lemma Union_disjoint:
   6.182 +  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
   6.183 +  shows "F g (Union C) = F (F g) C"
   6.184 +proof cases
   6.185 +  assume "finite C"
   6.186 +  from UNION_disjoint [OF this assms]
   6.187 +  show ?thesis
   6.188 +    by (simp add: SUP_def)
   6.189 +qed (auto dest: finite_UnionD intro: infinite)
   6.190  
   6.191 -lemma F_neutral': "ALL a:A. g a = 1 \<Longrightarrow> F g A = 1"
   6.192 -by simp
   6.193 +lemma distrib:
   6.194 +  "F (\<lambda>x. g x * h x) A = F g A * F h A"
   6.195 +proof (cases "finite A")
   6.196 +  case False then show ?thesis by simp
   6.197 +next
   6.198 +  case True then show ?thesis by (rule finite_induct) (simp_all add: assoc commute left_commute)
   6.199 +qed
   6.200 +
   6.201 +lemma Sigma:
   6.202 +  "finite A \<Longrightarrow> \<forall>x\<in>A. finite (B x) \<Longrightarrow> F (\<lambda>x. F (g x) (B x)) A = F (split g) (SIGMA x:A. B x)"
   6.203 +apply (subst Sigma_def)
   6.204 +apply (subst UNION_disjoint, assumption, simp)
   6.205 + apply blast
   6.206 +apply (rule cong)
   6.207 +apply rule
   6.208 +apply (simp add: fun_eq_iff)
   6.209 +apply (subst UNION_disjoint, simp, simp)
   6.210 + apply blast
   6.211 +apply (simp add: comp_def)
   6.212 +done
   6.213 +
   6.214 +lemma related: 
   6.215 +  assumes Re: "R 1 1" 
   6.216 +  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
   6.217 +  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
   6.218 +  shows "R (F h S) (F g S)"
   6.219 +  using fS by (rule finite_subset_induct) (insert assms, auto)
   6.220  
   6.221 -lemma F_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow> F g A = F g (A - B) * F g B"
   6.222 -by (metis Diff_partition union_disjoint Diff_disjoint finite_Un inf_commute sup_commute)
   6.223 +lemma eq_general:
   6.224 +  assumes h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y" 
   6.225 +  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x"
   6.226 +  shows "F f1 S = F f2 S'"
   6.227 +proof-
   6.228 +  from h f12 have hS: "h ` S = S'" by blast
   6.229 +  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
   6.230 +    from f12 h H  have "x = y" by auto }
   6.231 +  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
   6.232 +  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
   6.233 +  from hS have "F f2 S' = F f2 (h ` S)" by simp
   6.234 +  also have "\<dots> = F (f2 o h) S" using reindex [OF hinj, of f2] .
   6.235 +  also have "\<dots> = F f1 S " using th cong [of _ _ "f2 o h" f1]
   6.236 +    by blast
   6.237 +  finally show ?thesis ..
   6.238 +qed
   6.239  
   6.240 -lemma F_mono_neutral_cong_left:
   6.241 +lemma eq_general_reverses:
   6.242 +  assumes kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
   6.243 +  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x"
   6.244 +  shows "F j S = F g T"
   6.245 +  (* metis solves it, but not yet available here *)
   6.246 +  apply (rule eq_general [of T S h g j])
   6.247 +  apply (rule ballI)
   6.248 +  apply (frule kh)
   6.249 +  apply (rule ex1I[])
   6.250 +  apply blast
   6.251 +  apply clarsimp
   6.252 +  apply (drule hk) apply simp
   6.253 +  apply (rule sym)
   6.254 +  apply (erule conjunct1[OF conjunct2[OF hk]])
   6.255 +  apply (rule ballI)
   6.256 +  apply (drule hk)
   6.257 +  apply blast
   6.258 +  done
   6.259 +
   6.260 +lemma mono_neutral_cong_left:
   6.261    assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1"
   6.262    and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T"
   6.263  proof-
   6.264 @@ -62,25 +239,25 @@
   6.265    from `finite T` `S \<subseteq> T` have f: "finite S" "finite (T - S)"
   6.266      by (auto intro: finite_subset)
   6.267    show ?thesis using assms(4)
   6.268 -    by (simp add: union_disjoint[OF f d, unfolded eq[symmetric]] F_neutral'[OF assms(3)])
   6.269 +    by (simp add: union_disjoint [OF f d, unfolded eq [symmetric]] neutral [OF assms(3)])
   6.270  qed
   6.271  
   6.272 -lemma F_mono_neutral_cong_right:
   6.273 +lemma mono_neutral_cong_right:
   6.274    "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1; \<And>x. x \<in> S \<Longrightarrow> g x = h x \<rbrakk>
   6.275     \<Longrightarrow> F g T = F h S"
   6.276 -by(auto intro!: F_mono_neutral_cong_left[symmetric])
   6.277 +  by (auto intro!: mono_neutral_cong_left [symmetric])
   6.278  
   6.279 -lemma F_mono_neutral_left:
   6.280 +lemma mono_neutral_left:
   6.281    "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g S = F g T"
   6.282 -by(blast intro: F_mono_neutral_cong_left)
   6.283 +  by (blast intro: mono_neutral_cong_left)
   6.284  
   6.285 -lemma F_mono_neutral_right:
   6.286 +lemma mono_neutral_right:
   6.287    "\<lbrakk> finite T;  S \<subseteq> T;  \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S"
   6.288 -by(blast intro!: F_mono_neutral_left[symmetric])
   6.289 +  by (blast intro!: mono_neutral_left [symmetric])
   6.290  
   6.291 -lemma F_delta: 
   6.292 +lemma delta: 
   6.293    assumes fS: "finite S"
   6.294 -  shows "F (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
   6.295 +  shows "F (\<lambda>k. if k = a then b k else 1) S = (if a \<in> S then b a else 1)"
   6.296  proof-
   6.297    let ?f = "(\<lambda>k. if k=a then b k else 1)"
   6.298    { assume a: "a \<notin> S"
   6.299 @@ -94,78 +271,71 @@
   6.300      have dj: "?A \<inter> ?B = {}" by simp
   6.301      from fS have fAB: "finite ?A" "finite ?B" by auto  
   6.302      have "F ?f S = F ?f ?A * F ?f ?B"
   6.303 -      using union_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
   6.304 +      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]]
   6.305        by simp
   6.306 -    then have ?thesis  using a by simp }
   6.307 +    then have ?thesis using a by simp }
   6.308    ultimately show ?thesis by blast
   6.309  qed
   6.310  
   6.311 -lemma F_delta': 
   6.312 -  assumes fS: "finite S" shows 
   6.313 -  "F (\<lambda>k. if a = k then b k else 1) S = (if a \<in> S then b a else 1)"
   6.314 -using F_delta[OF fS, of a b, symmetric] by (auto intro: F_cong)
   6.315 -
   6.316 -lemma F_fun_f: "F (%x. g x * h x) A = (F g A * F h A)"
   6.317 -by (cases "finite A") (simp_all add: distrib)
   6.318 -
   6.319 -
   6.320 -text {* for ad-hoc proofs for @{const fold_image} *}
   6.321 -lemma comm_monoid_mult:  "class.comm_monoid_mult (op *) 1"
   6.322 -proof qed (auto intro: assoc commute)
   6.323 -
   6.324 -lemma F_Un_neutral:
   6.325 -  assumes fS: "finite S" and fT: "finite T"
   6.326 -  and I1: "\<forall>x \<in> S\<inter>T. g x = 1"
   6.327 -  shows "F g (S \<union> T) = F g S  * F g T"
   6.328 -proof -
   6.329 -  interpret comm_monoid_mult "op *" 1 by (fact comm_monoid_mult)
   6.330 -  show ?thesis
   6.331 -  using fS fT
   6.332 -  apply (simp add: F_eq)
   6.333 -  apply (rule fold_image_Un_one)
   6.334 -  using I1 by auto
   6.335 -qed
   6.336 +lemma delta': 
   6.337 +  assumes fS: "finite S"
   6.338 +  shows "F (\<lambda>k. if a = k then b k else 1) S = (if a \<in> S then b a else 1)"
   6.339 +  using delta [OF fS, of a b, symmetric] by (auto intro: cong)
   6.340  
   6.341  lemma If_cases:
   6.342    fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
   6.343    assumes fA: "finite A"
   6.344    shows "F (\<lambda>x. if P x then h x else g x) A =
   6.345 -         F h (A \<inter> {x. P x}) * F g (A \<inter> - {x. P x})"
   6.346 -proof-
   6.347 +    F h (A \<inter> {x. P x}) * F g (A \<inter> - {x. P x})"
   6.348 +proof -
   6.349    have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}" 
   6.350            "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}" 
   6.351      by blast+
   6.352    from fA 
   6.353    have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
   6.354    let ?g = "\<lambda>x. if P x then h x else g x"
   6.355 -  from union_disjoint[OF f a(2), of ?g] a(1)
   6.356 +  from union_disjoint [OF f a(2), of ?g] a(1)
   6.357    show ?thesis
   6.358 -    by (subst (1 2) F_cong) simp_all
   6.359 +    by (subst (1 2) cong) simp_all
   6.360  qed
   6.361  
   6.362 +lemma cartesian_product:
   6.363 +   "F (\<lambda>x. F (g x) B) A = F (split g) (A <*> B)"
   6.364 +apply (rule sym)
   6.365 +apply (cases "finite A") 
   6.366 + apply (cases "finite B") 
   6.367 +  apply (simp add: Sigma)
   6.368 + apply (cases "A={}", simp)
   6.369 + apply simp
   6.370 +apply (auto intro: infinite dest: finite_cartesian_productD2)
   6.371 +apply (cases "B = {}") apply (auto intro: infinite dest: finite_cartesian_productD1)
   6.372 +done
   6.373 +
   6.374  end
   6.375  
   6.376 -text {* for ad-hoc proofs for @{const fold_image} *}
   6.377 -
   6.378 -lemma (in comm_monoid_add) comm_monoid_mult:
   6.379 -  "class.comm_monoid_mult (op +) 0"
   6.380 -proof qed (auto intro: add_assoc add_commute)
   6.381 -
   6.382  notation times (infixl "*" 70)
   6.383  notation Groups.one ("1")
   6.384  
   6.385  
   6.386  subsection {* Generalized summation over a set *}
   6.387  
   6.388 -definition (in comm_monoid_add) setsum :: "('b \<Rightarrow> 'a) => 'b set => 'a" where
   6.389 -  "setsum f A = (if finite A then fold_image (op +) f 0 A else 0)"
   6.390 +definition (in comm_monoid_add) setsum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
   6.391 +where
   6.392 +  "setsum = comm_monoid_set.F plus 0"
   6.393  
   6.394 -sublocale comm_monoid_add < setsum!: comm_monoid_big "op +" 0 setsum proof
   6.395 -qed (fact setsum_def)
   6.396 +sublocale comm_monoid_add < setsum!: comm_monoid_set plus 0
   6.397 +where
   6.398 +  "setsum.F = setsum"
   6.399 +proof -
   6.400 +  show "comm_monoid_set plus 0" ..
   6.401 +  then interpret setsum!: comm_monoid_set plus 0 .
   6.402 +  show "setsum.F = setsum"
   6.403 +    by (simp only: setsum_def)
   6.404 +qed
   6.405  
   6.406  abbreviation
   6.407 -  Setsum  ("\<Sum>_" [1000] 999) where
   6.408 -  "\<Sum>A == setsum (%x. x) A"
   6.409 +  Setsum ("\<Sum>_" [1000] 999) where
   6.410 +  "\<Sum>A \<equiv> setsum (%x. x) A"
   6.411  
   6.412  text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
   6.413  written @{text"\<Sum>x\<in>A. e"}. *}
   6.414 @@ -211,48 +381,32 @@
   6.415  in [(@{const_syntax setsum}, setsum_tr')] end
   6.416  *}
   6.417  
   6.418 -lemma setsum_empty:
   6.419 -  "setsum f {} = 0"
   6.420 -  by (fact setsum.empty)
   6.421 +text {* TODO These are candidates for generalization *}
   6.422  
   6.423 -lemma setsum_insert:
   6.424 -  "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
   6.425 -  by (fact setsum.insert)
   6.426 -
   6.427 -lemma setsum_infinite:
   6.428 -  "~ finite A ==> setsum f A = 0"
   6.429 -  by (fact setsum.infinite)
   6.430 +context comm_monoid_add
   6.431 +begin
   6.432  
   6.433 -lemma (in comm_monoid_add) setsum_reindex:
   6.434 -  assumes "inj_on f B" shows "setsum h (f ` B) = setsum (h \<circ> f) B"
   6.435 -proof -
   6.436 -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
   6.437 -  from assms show ?thesis by (auto simp add: setsum_def fold_image_reindex o_def dest!:finite_imageD)
   6.438 -qed
   6.439 -
   6.440 -lemma setsum_reindex_id:
   6.441 +lemma setsum_reindex_id: 
   6.442    "inj_on f B ==> setsum f B = setsum id (f ` B)"
   6.443 -by (simp add: setsum_reindex)
   6.444 +  by (simp add: setsum.reindex)
   6.445  
   6.446 -lemma setsum_reindex_nonzero: 
   6.447 +lemma setsum_reindex_nonzero:
   6.448    assumes fS: "finite S"
   6.449 -  and nz: "\<And> x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x = f y \<Longrightarrow> h (f x) = 0"
   6.450 -  shows "setsum h (f ` S) = setsum (h o f) S"
   6.451 -using nz
   6.452 -proof(induct rule: finite_induct[OF fS])
   6.453 +  and nz: "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x = f y \<Longrightarrow> h (f x) = 0"
   6.454 +  shows "setsum h (f ` S) = setsum (h \<circ> f) S"
   6.455 +using nz proof (induct rule: finite_induct [OF fS])
   6.456    case 1 thus ?case by simp
   6.457  next
   6.458    case (2 x F) 
   6.459    { assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
   6.460      then obtain y where y: "y \<in> F" "f x = f y" by auto 
   6.461      from "2.hyps" y have xy: "x \<noteq> y" by auto
   6.462 -    
   6.463 -    from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
   6.464 +    from "2.prems" [of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
   6.465      have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
   6.466      also have "\<dots> = setsum (h o f) (insert x F)" 
   6.467        unfolding setsum.insert[OF `finite F` `x\<notin>F`]
   6.468        using h0
   6.469 -      apply (simp cong del:setsum.strong_F_cong)
   6.470 +      apply (simp cong del: setsum.strong_cong)
   6.471        apply (rule "2.hyps"(3))
   6.472        apply (rule_tac y="y" in  "2.prems")
   6.473        apply simp_all
   6.474 @@ -264,7 +418,7 @@
   6.475        using fxF "2.hyps" by simp 
   6.476      also have "\<dots> = setsum (h o f) (insert x F)"
   6.477        unfolding setsum.insert[OF `finite F` `x\<notin>F`]
   6.478 -      apply (simp cong del:setsum.strong_F_cong)
   6.479 +      apply (simp cong del: setsum.strong_cong)
   6.480        apply (rule cong [OF refl [of "op + (h (f x))"]])
   6.481        apply (rule "2.hyps"(3))
   6.482        apply (rule_tac y="y" in  "2.prems")
   6.483 @@ -274,59 +428,14 @@
   6.484    ultimately show ?case by blast
   6.485  qed
   6.486  
   6.487 -lemma setsum_cong:
   6.488 -  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   6.489 -by (fact setsum.F_cong)
   6.490 -
   6.491 -lemma strong_setsum_cong:
   6.492 -  "A = B ==> (!!x. x:B =simp=> f x = g x)
   6.493 -   ==> setsum (%x. f x) A = setsum (%x. g x) B"
   6.494 -by (fact setsum.strong_F_cong)
   6.495 -
   6.496 -lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A"
   6.497 -by (auto intro: setsum_cong)
   6.498 +lemma setsum_cong2:
   6.499 +  "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> setsum f A = setsum g A"
   6.500 +  by (auto intro: setsum.cong)
   6.501  
   6.502  lemma setsum_reindex_cong:
   6.503     "[|inj_on f A; B = f ` A; !!a. a:A \<Longrightarrow> g a = h (f a)|] 
   6.504      ==> setsum h B = setsum g A"
   6.505 -by (simp add: setsum_reindex)
   6.506 -
   6.507 -lemmas setsum_0 = setsum.F_neutral
   6.508 -lemmas setsum_0' = setsum.F_neutral'
   6.509 -
   6.510 -lemma setsum_Un_Int: "finite A ==> finite B ==>
   6.511 -  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
   6.512 -  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
   6.513 -by (fact setsum.union_inter)
   6.514 -
   6.515 -lemma setsum_Un_disjoint: "finite A ==> finite B
   6.516 -  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
   6.517 -by (fact setsum.union_disjoint)
   6.518 -
   6.519 -lemma setsum_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
   6.520 -    setsum f A = setsum f (A - B) + setsum f B"
   6.521 -by(fact setsum.F_subset_diff)
   6.522 -
   6.523 -lemma setsum_mono_zero_left: 
   6.524 -  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 0 \<rbrakk> \<Longrightarrow> setsum f S = setsum f T"
   6.525 -by(fact setsum.F_mono_neutral_left)
   6.526 -
   6.527 -lemmas setsum_mono_zero_right = setsum.F_mono_neutral_right
   6.528 -
   6.529 -lemma setsum_mono_zero_cong_left: 
   6.530 -  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 0; \<And>x. x \<in> S \<Longrightarrow> f x = g x \<rbrakk>
   6.531 -  \<Longrightarrow> setsum f S = setsum g T"
   6.532 -by(fact setsum.F_mono_neutral_cong_left)
   6.533 -
   6.534 -lemmas setsum_mono_zero_cong_right = setsum.F_mono_neutral_cong_right
   6.535 -
   6.536 -lemma setsum_delta: "finite S \<Longrightarrow>
   6.537 -  setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
   6.538 -by(fact setsum.F_delta)
   6.539 -
   6.540 -lemma setsum_delta': "finite S \<Longrightarrow>
   6.541 -  setsum (\<lambda>k. if a = k then b k else 0) S = (if a\<in> S then b a else 0)"
   6.542 -by(fact setsum.F_delta')
   6.543 +  by (simp add: setsum.reindex)
   6.544  
   6.545  lemma setsum_restrict_set:
   6.546    assumes fA: "finite A"
   6.547 @@ -335,70 +444,20 @@
   6.548    from fA have fab: "finite (A \<inter> B)" by auto
   6.549    have aba: "A \<inter> B \<subseteq> A" by blast
   6.550    let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
   6.551 -  from setsum_mono_zero_left[OF fA aba, of ?g]
   6.552 +  from setsum.mono_neutral_left [OF fA aba, of ?g]
   6.553    show ?thesis by simp
   6.554  qed
   6.555  
   6.556 -lemma setsum_cases:
   6.557 -  assumes fA: "finite A"
   6.558 -  shows "setsum (\<lambda>x. if P x then f x else g x) A =
   6.559 -         setsum f (A \<inter> {x. P x}) + setsum g (A \<inter> - {x. P x})"
   6.560 -  using setsum.If_cases[OF fA] .
   6.561 -
   6.562 -(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
   6.563 -  the lhs need not be, since UNION I A could still be finite.*)
   6.564 -lemma (in comm_monoid_add) setsum_UN_disjoint:
   6.565 -  assumes "finite I" and "ALL i:I. finite (A i)"
   6.566 -    and "ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}"
   6.567 -  shows "setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
   6.568 -proof -
   6.569 -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
   6.570 -  from assms show ?thesis by (simp add: setsum_def fold_image_UN_disjoint)
   6.571 -qed
   6.572 -
   6.573 -text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
   6.574 -directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
   6.575  lemma setsum_Union_disjoint:
   6.576    assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}"
   6.577    shows "setsum f (Union C) = setsum (setsum f) C"
   6.578 -proof cases
   6.579 -  assume "finite C"
   6.580 -  from setsum_UN_disjoint[OF this assms]
   6.581 -  show ?thesis
   6.582 -    by (simp add: SUP_def)
   6.583 -qed (force dest: finite_UnionD simp add: setsum_def)
   6.584 -
   6.585 -(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
   6.586 -  the rhs need not be, since SIGMA A B could still be finite.*)
   6.587 -lemma (in comm_monoid_add) setsum_Sigma:
   6.588 -  assumes "finite A" and  "ALL x:A. finite (B x)"
   6.589 -  shows "(\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) = (\<Sum>(x,y)\<in>(SIGMA x:A. B x). f x y)"
   6.590 -proof -
   6.591 -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
   6.592 -  from assms show ?thesis by (simp add: setsum_def fold_image_Sigma split_def)
   6.593 -qed
   6.594 +  using assms by (fact setsum.Union_disjoint)
   6.595  
   6.596 -text{*Here we can eliminate the finiteness assumptions, by cases.*}
   6.597 -lemma setsum_cartesian_product: 
   6.598 -   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
   6.599 -apply (cases "finite A") 
   6.600 - apply (cases "finite B") 
   6.601 -  apply (simp add: setsum_Sigma)
   6.602 - apply (cases "A={}", simp)
   6.603 - apply (simp) 
   6.604 -apply (auto simp add: setsum_def
   6.605 -            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
   6.606 -done
   6.607 +lemma setsum_cartesian_product:
   6.608 +  "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
   6.609 +  by (fact setsum.cartesian_product)
   6.610  
   6.611 -lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
   6.612 -by (fact setsum.F_fun_f)
   6.613 -
   6.614 -lemma setsum_Un_zero:  
   6.615 -  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow>
   6.616 -  setsum f (S \<union> T) = setsum f S + setsum f T"
   6.617 -by(fact setsum.F_Un_neutral)
   6.618 -
   6.619 -lemma setsum_UNION_zero: 
   6.620 +lemma setsum_UNION_zero:
   6.621    assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
   6.622    and f0: "\<And>T1 T2 x. T1\<in>S \<Longrightarrow> T2\<in>S \<Longrightarrow> T1 \<noteq> T2 \<Longrightarrow> x \<in> T1 \<Longrightarrow> x \<in> T2 \<Longrightarrow> f x = 0"
   6.623    shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
   6.624 @@ -412,36 +471,145 @@
   6.625    from fTF have fUF: "finite (\<Union>F)" by auto
   6.626    from "2.prems" TF fTF
   6.627    show ?case 
   6.628 -    by (auto simp add: H[symmetric] intro: setsum_Un_zero[OF fTF(1) fUF, of f])
   6.629 +    by (auto simp add: H [symmetric] intro: setsum.union_inter_neutral [OF fTF(1) fUF, of f])
   6.630 +qed
   6.631 +
   6.632 +text {* Commuting outer and inner summation *}
   6.633 +
   6.634 +lemma setsum_commute:
   6.635 +  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
   6.636 +proof (simp add: setsum_cartesian_product)
   6.637 +  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
   6.638 +    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
   6.639 +    (is "?s = _")
   6.640 +    apply (simp add: setsum.reindex [where h = "%(i, j). (j, i)"] swap_inj_on)
   6.641 +    apply (simp add: split_def)
   6.642 +    done
   6.643 +  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
   6.644 +    (is "_ = ?t")
   6.645 +    apply (simp add: swap_product)
   6.646 +    done
   6.647 +  finally show "?s = ?t" .
   6.648 +qed
   6.649 +
   6.650 +lemma setsum_Plus:
   6.651 +  fixes A :: "'a set" and B :: "'b set"
   6.652 +  assumes fin: "finite A" "finite B"
   6.653 +  shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B"
   6.654 +proof -
   6.655 +  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
   6.656 +  moreover from fin have "finite (Inl ` A :: ('a + 'b) set)" "finite (Inr ` B :: ('a + 'b) set)"
   6.657 +    by auto
   6.658 +  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('a + 'b) set)" by auto
   6.659 +  moreover have "inj_on (Inl :: 'a \<Rightarrow> 'a + 'b) A" "inj_on (Inr :: 'b \<Rightarrow> 'a + 'b) B" by(auto intro: inj_onI)
   6.660 +  ultimately show ?thesis using fin by(simp add: setsum.union_disjoint setsum.reindex)
   6.661  qed
   6.662  
   6.663 +end
   6.664 +
   6.665 +text {* TODO These are legacy *}
   6.666 +
   6.667 +lemma setsum_empty:
   6.668 +  "setsum f {} = 0"
   6.669 +  by (fact setsum.empty)
   6.670 +
   6.671 +lemma setsum_insert:
   6.672 +  "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
   6.673 +  by (fact setsum.insert)
   6.674 +
   6.675 +lemma setsum_infinite:
   6.676 +  "~ finite A ==> setsum f A = 0"
   6.677 +  by (fact setsum.infinite)
   6.678 +
   6.679 +lemma setsum_reindex:
   6.680 +  "inj_on f B \<Longrightarrow> setsum h (f ` B) = setsum (h \<circ> f) B"
   6.681 +  by (fact setsum.reindex)
   6.682 +
   6.683 +lemma setsum_cong:
   6.684 +  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
   6.685 +  by (fact setsum.cong)
   6.686 +
   6.687 +lemma strong_setsum_cong:
   6.688 +  "A = B ==> (!!x. x:B =simp=> f x = g x)
   6.689 +   ==> setsum (%x. f x) A = setsum (%x. g x) B"
   6.690 +  by (fact setsum.strong_cong)
   6.691 +
   6.692 +lemmas setsum_0 = setsum.neutral_const
   6.693 +lemmas setsum_0' = setsum.neutral
   6.694 +
   6.695 +lemma setsum_Un_Int: "finite A ==> finite B ==>
   6.696 +  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
   6.697 +  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
   6.698 +  by (fact setsum.union_inter)
   6.699 +
   6.700 +lemma setsum_Un_disjoint: "finite A ==> finite B
   6.701 +  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
   6.702 +  by (fact setsum.union_disjoint)
   6.703 +
   6.704 +lemma setsum_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
   6.705 +    setsum f A = setsum f (A - B) + setsum f B"
   6.706 +  by (fact setsum.subset_diff)
   6.707 +
   6.708 +lemma setsum_mono_zero_left: 
   6.709 +  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 0 \<rbrakk> \<Longrightarrow> setsum f S = setsum f T"
   6.710 +  by (fact setsum.mono_neutral_left)
   6.711 +
   6.712 +lemmas setsum_mono_zero_right = setsum.mono_neutral_right
   6.713 +
   6.714 +lemma setsum_mono_zero_cong_left: 
   6.715 +  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 0; \<And>x. x \<in> S \<Longrightarrow> f x = g x \<rbrakk>
   6.716 +  \<Longrightarrow> setsum f S = setsum g T"
   6.717 +  by (fact setsum.mono_neutral_cong_left)
   6.718 +
   6.719 +lemmas setsum_mono_zero_cong_right = setsum.mono_neutral_cong_right
   6.720 +
   6.721 +lemma setsum_delta: "finite S \<Longrightarrow>
   6.722 +  setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
   6.723 +  by (fact setsum.delta)
   6.724 +
   6.725 +lemma setsum_delta': "finite S \<Longrightarrow>
   6.726 +  setsum (\<lambda>k. if a = k then b k else 0) S = (if a\<in> S then b a else 0)"
   6.727 +  by (fact setsum.delta')
   6.728 +
   6.729 +lemma setsum_cases:
   6.730 +  assumes "finite A"
   6.731 +  shows "setsum (\<lambda>x. if P x then f x else g x) A =
   6.732 +         setsum f (A \<inter> {x. P x}) + setsum g (A \<inter> - {x. P x})"
   6.733 +  using assms by (fact setsum.If_cases)
   6.734 +
   6.735 +(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
   6.736 +  the lhs need not be, since UNION I A could still be finite.*)
   6.737 +lemma setsum_UN_disjoint:
   6.738 +  assumes "finite I" and "ALL i:I. finite (A i)"
   6.739 +    and "ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}"
   6.740 +  shows "setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
   6.741 +  using assms by (fact setsum.UNION_disjoint)
   6.742 +
   6.743 +(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
   6.744 +  the rhs need not be, since SIGMA A B could still be finite.*)
   6.745 +lemma setsum_Sigma:
   6.746 +  assumes "finite A" and  "ALL x:A. finite (B x)"
   6.747 +  shows "(\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) = (\<Sum>(x,y)\<in>(SIGMA x:A. B x). f x y)"
   6.748 +  using assms by (fact setsum.Sigma)
   6.749 +
   6.750 +lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
   6.751 +  by (fact setsum.distrib)
   6.752 +
   6.753 +lemma setsum_Un_zero:  
   6.754 +  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow>
   6.755 +  setsum f (S \<union> T) = setsum f S + setsum f T"
   6.756 +  by (fact setsum.union_inter_neutral)
   6.757 +
   6.758 +lemma setsum_eq_general_reverses:
   6.759 +  assumes fS: "finite S" and fT: "finite T"
   6.760 +  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
   6.761 +  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x"
   6.762 +  shows "setsum f S = setsum g T"
   6.763 +  using kh hk by (fact setsum.eq_general_reverses)
   6.764 +
   6.765  
   6.766  subsubsection {* Properties in more restricted classes of structures *}
   6.767  
   6.768 -lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
   6.769 -apply (case_tac "finite A")
   6.770 - prefer 2 apply (simp add: setsum_def)
   6.771 -apply (erule rev_mp)
   6.772 -apply (erule finite_induct, auto)
   6.773 -done
   6.774 -
   6.775 -lemma setsum_eq_0_iff [simp]:
   6.776 -    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
   6.777 -by (induct set: finite) auto
   6.778 -
   6.779 -lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
   6.780 -  (setsum f A = Suc 0) = (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
   6.781 -apply(erule finite_induct)
   6.782 -apply (auto simp add:add_is_1)
   6.783 -done
   6.784 -
   6.785 -lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
   6.786 -
   6.787 -lemma setsum_Un_nat: "finite A ==> finite B ==>
   6.788 -  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
   6.789 -  -- {* For the natural numbers, we have subtraction. *}
   6.790 -by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
   6.791 -
   6.792  lemma setsum_Un: "finite A ==> finite B ==>
   6.793    (setsum f (A Un B) :: 'a :: ab_group_add) =
   6.794     setsum f A + setsum f B - setsum f (A Int B)"
   6.795 @@ -456,74 +624,11 @@
   6.796    with assms show ?thesis by simp (subst setsum_Un_disjoint, auto)+
   6.797  qed
   6.798  
   6.799 -lemma (in comm_monoid_add) setsum_eq_general_reverses:
   6.800 -  assumes fS: "finite S" and fT: "finite T"
   6.801 -  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
   6.802 -  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x"
   6.803 -  shows "setsum f S = setsum g T"
   6.804 -proof -
   6.805 -  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
   6.806 -  show ?thesis
   6.807 -  apply (simp add: setsum_def fS fT)
   6.808 -  apply (rule fold_image_eq_general_inverses)
   6.809 -  apply (rule fS)
   6.810 -  apply (erule kh)
   6.811 -  apply (erule hk)
   6.812 -  done
   6.813 -qed
   6.814 -
   6.815 -lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
   6.816 -  (if a:A then setsum f A - f a else setsum f A)"
   6.817 -apply (case_tac "finite A")
   6.818 - prefer 2 apply (simp add: setsum_def)
   6.819 -apply (erule finite_induct)
   6.820 - apply (auto simp add: insert_Diff_if)
   6.821 -apply (drule_tac a = a in mk_disjoint_insert, auto)
   6.822 -done
   6.823 -
   6.824  lemma setsum_diff1: "finite A \<Longrightarrow>
   6.825    (setsum f (A - {a}) :: ('a::ab_group_add)) =
   6.826    (if a:A then setsum f A - f a else setsum f A)"
   6.827  by (erule finite_induct) (auto simp add: insert_Diff_if)
   6.828  
   6.829 -lemma setsum_diff1'[rule_format]:
   6.830 -  "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
   6.831 -apply (erule finite_induct[where F=A and P="% A. (a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x))"])
   6.832 -apply (auto simp add: insert_Diff_if add_ac)
   6.833 -done
   6.834 -
   6.835 -lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
   6.836 -  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
   6.837 -unfolding setsum_diff1'[OF assms] by auto
   6.838 -
   6.839 -(* By Jeremy Siek: *)
   6.840 -
   6.841 -lemma setsum_diff_nat: 
   6.842 -assumes "finite B" and "B \<subseteq> A"
   6.843 -shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
   6.844 -using assms
   6.845 -proof induct
   6.846 -  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
   6.847 -next
   6.848 -  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
   6.849 -    and xFinA: "insert x F \<subseteq> A"
   6.850 -    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
   6.851 -  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
   6.852 -  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
   6.853 -    by (simp add: setsum_diff1_nat)
   6.854 -  from xFinA have "F \<subseteq> A" by simp
   6.855 -  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
   6.856 -  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
   6.857 -    by simp
   6.858 -  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
   6.859 -  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
   6.860 -    by simp
   6.861 -  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
   6.862 -  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
   6.863 -    by simp
   6.864 -  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
   6.865 -qed
   6.866 -
   6.867  lemma setsum_diff:
   6.868    assumes le: "finite A" "B \<subseteq> A"
   6.869    shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
   6.870 @@ -554,9 +659,7 @@
   6.871      thus ?case using add_mono by fastforce
   6.872    qed
   6.873  next
   6.874 -  case False
   6.875 -  thus ?thesis
   6.876 -    by (simp add: setsum_def)
   6.877 +  case False then show ?thesis by simp
   6.878  qed
   6.879  
   6.880  lemma setsum_strict_mono:
   6.881 @@ -595,7 +698,7 @@
   6.882  proof (cases "finite A")
   6.883    case True thus ?thesis by (induct set: finite) auto
   6.884  next
   6.885 -  case False thus ?thesis by (simp add: setsum_def)
   6.886 +  case False thus ?thesis by simp
   6.887  qed
   6.888  
   6.889  lemma setsum_subtractf:
   6.890 @@ -604,7 +707,7 @@
   6.891  proof (cases "finite A")
   6.892    case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
   6.893  next
   6.894 -  case False thus ?thesis by (simp add: setsum_def)
   6.895 +  case False thus ?thesis by simp
   6.896  qed
   6.897  
   6.898  lemma setsum_nonneg:
   6.899 @@ -620,7 +723,7 @@
   6.900      with insert show ?case by simp
   6.901    qed
   6.902  next
   6.903 -  case False thus ?thesis by (simp add: setsum_def)
   6.904 +  case False thus ?thesis by simp
   6.905  qed
   6.906  
   6.907  lemma setsum_nonpos:
   6.908 @@ -636,7 +739,7 @@
   6.909      with insert show ?case by simp
   6.910    qed
   6.911  next
   6.912 -  case False thus ?thesis by (simp add: setsum_def)
   6.913 +  case False thus ?thesis by simp
   6.914  qed
   6.915  
   6.916  lemma setsum_nonneg_leq_bound:
   6.917 @@ -702,7 +805,7 @@
   6.918      case (insert x A) thus ?case by (simp add: distrib_left)
   6.919    qed
   6.920  next
   6.921 -  case False thus ?thesis by (simp add: setsum_def)
   6.922 +  case False thus ?thesis by simp
   6.923  qed
   6.924  
   6.925  lemma setsum_left_distrib:
   6.926 @@ -716,7 +819,7 @@
   6.927      case (insert x A) thus ?case by (simp add: distrib_right)
   6.928    qed
   6.929  next
   6.930 -  case False thus ?thesis by (simp add: setsum_def)
   6.931 +  case False thus ?thesis by simp
   6.932  qed
   6.933  
   6.934  lemma setsum_divide_distrib:
   6.935 @@ -730,7 +833,7 @@
   6.936      case (insert x A) thus ?case by (simp add: add_divide_distrib)
   6.937    qed
   6.938  next
   6.939 -  case False thus ?thesis by (simp add: setsum_def)
   6.940 +  case False thus ?thesis by simp
   6.941  qed
   6.942  
   6.943  lemma setsum_abs[iff]: 
   6.944 @@ -746,7 +849,7 @@
   6.945      thus ?case by (auto intro: abs_triangle_ineq order_trans)
   6.946    qed
   6.947  next
   6.948 -  case False thus ?thesis by (simp add: setsum_def)
   6.949 +  case False thus ?thesis by simp
   6.950  qed
   6.951  
   6.952  lemma setsum_abs_ge_zero[iff]: 
   6.953 @@ -761,7 +864,7 @@
   6.954      case (insert x A) thus ?case by auto
   6.955    qed
   6.956  next
   6.957 -  case False thus ?thesis by (simp add: setsum_def)
   6.958 +  case False thus ?thesis by simp
   6.959  qed
   6.960  
   6.961  lemma abs_setsum_abs[simp]: 
   6.962 @@ -782,40 +885,18 @@
   6.963      finally show ?case .
   6.964    qed
   6.965  next
   6.966 -  case False thus ?thesis by (simp add: setsum_def)
   6.967 -qed
   6.968 -
   6.969 -lemma setsum_Plus:
   6.970 -  fixes A :: "'a set" and B :: "'b set"
   6.971 -  assumes fin: "finite A" "finite B"
   6.972 -  shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B"
   6.973 -proof -
   6.974 -  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
   6.975 -  moreover from fin have "finite (Inl ` A :: ('a + 'b) set)" "finite (Inr ` B :: ('a + 'b) set)"
   6.976 -    by auto
   6.977 -  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('a + 'b) set)" by auto
   6.978 -  moreover have "inj_on (Inl :: 'a \<Rightarrow> 'a + 'b) A" "inj_on (Inr :: 'b \<Rightarrow> 'a + 'b) B" by(auto intro: inj_onI)
   6.979 -  ultimately show ?thesis using fin by(simp add: setsum_Un_disjoint setsum_reindex)
   6.980 +  case False thus ?thesis by simp
   6.981  qed
   6.982  
   6.983 -
   6.984 -text {* Commuting outer and inner summation *}
   6.985 +lemma setsum_diff1'[rule_format]:
   6.986 +  "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
   6.987 +apply (erule finite_induct[where F=A and P="% A. (a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x))"])
   6.988 +apply (auto simp add: insert_Diff_if add_ac)
   6.989 +done
   6.990  
   6.991 -lemma setsum_commute:
   6.992 -  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
   6.993 -proof (simp add: setsum_cartesian_product)
   6.994 -  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
   6.995 -    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
   6.996 -    (is "?s = _")
   6.997 -    apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on)
   6.998 -    apply (simp add: split_def)
   6.999 -    done
  6.1000 -  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
  6.1001 -    (is "_ = ?t")
  6.1002 -    apply (simp add: swap_product)
  6.1003 -    done
  6.1004 -  finally show "?s = ?t" .
  6.1005 -qed
  6.1006 +lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
  6.1007 +  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
  6.1008 +unfolding setsum_diff1'[OF assms] by auto
  6.1009  
  6.1010  lemma setsum_product:
  6.1011    fixes f :: "'a => ('b::semiring_0)"
  6.1012 @@ -829,7 +910,82 @@
  6.1013  by(auto simp: setsum_product setsum_cartesian_product
  6.1014          intro!:  setsum_reindex_cong[symmetric])
  6.1015  
  6.1016 -lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat(card A) * y"
  6.1017 +lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
  6.1018 +apply (case_tac "finite A")
  6.1019 + prefer 2 apply simp
  6.1020 +apply (erule rev_mp)
  6.1021 +apply (erule finite_induct, auto)
  6.1022 +done
  6.1023 +
  6.1024 +lemma setsum_eq_0_iff [simp]:
  6.1025 +  "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
  6.1026 +  by (induct set: finite) auto
  6.1027 +
  6.1028 +lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
  6.1029 +  setsum f A = Suc 0 \<longleftrightarrow> (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
  6.1030 +apply(erule finite_induct)
  6.1031 +apply (auto simp add:add_is_1)
  6.1032 +done
  6.1033 +
  6.1034 +lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
  6.1035 +
  6.1036 +lemma setsum_Un_nat: "finite A ==> finite B ==>
  6.1037 +  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
  6.1038 +  -- {* For the natural numbers, we have subtraction. *}
  6.1039 +by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
  6.1040 +
  6.1041 +lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
  6.1042 +  (if a:A then setsum f A - f a else setsum f A)"
  6.1043 +apply (case_tac "finite A")
  6.1044 + prefer 2 apply simp
  6.1045 +apply (erule finite_induct)
  6.1046 + apply (auto simp add: insert_Diff_if)
  6.1047 +apply (drule_tac a = a in mk_disjoint_insert, auto)
  6.1048 +done
  6.1049 +
  6.1050 +lemma setsum_diff_nat: 
  6.1051 +assumes "finite B" and "B \<subseteq> A"
  6.1052 +shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
  6.1053 +using assms
  6.1054 +proof induct
  6.1055 +  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
  6.1056 +next
  6.1057 +  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
  6.1058 +    and xFinA: "insert x F \<subseteq> A"
  6.1059 +    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
  6.1060 +  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
  6.1061 +  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
  6.1062 +    by (simp add: setsum_diff1_nat)
  6.1063 +  from xFinA have "F \<subseteq> A" by simp
  6.1064 +  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
  6.1065 +  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
  6.1066 +    by simp
  6.1067 +  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
  6.1068 +  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
  6.1069 +    by simp
  6.1070 +  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
  6.1071 +  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
  6.1072 +    by simp
  6.1073 +  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
  6.1074 +qed
  6.1075 +
  6.1076 +
  6.1077 +subsubsection {* Cardinality as special case of @{const setsum} *}
  6.1078 +
  6.1079 +lemma card_eq_setsum:
  6.1080 +  "card A = setsum (\<lambda>x. 1) A"
  6.1081 +proof -
  6.1082 +  have "plus \<circ> (\<lambda>_. Suc 0) = (\<lambda>_. Suc)"
  6.1083 +    by (simp add: fun_eq_iff)
  6.1084 +  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) = Finite_Set.fold (\<lambda>_. Suc)"
  6.1085 +    by (rule arg_cong)
  6.1086 +  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) 0 A = Finite_Set.fold (\<lambda>_. Suc) 0 A"
  6.1087 +    by (blast intro: fun_cong)
  6.1088 +  then show ?thesis by (simp add: card.eq_fold setsum.eq_fold)
  6.1089 +qed
  6.1090 +
  6.1091 +lemma setsum_constant [simp]:
  6.1092 +  "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
  6.1093  apply (cases "finite A")
  6.1094  apply (erule finite_induct)
  6.1095  apply (auto simp add: algebra_simps)
  6.1096 @@ -837,21 +993,14 @@
  6.1097  
  6.1098  lemma setsum_bounded:
  6.1099    assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, ordered_ab_semigroup_add})"
  6.1100 -  shows "setsum f A \<le> of_nat(card A) * K"
  6.1101 +  shows "setsum f A \<le> of_nat (card A) * K"
  6.1102  proof (cases "finite A")
  6.1103    case True
  6.1104    thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
  6.1105  next
  6.1106 -  case False thus ?thesis by (simp add: setsum_def)
  6.1107 +  case False thus ?thesis by simp
  6.1108  qed
  6.1109  
  6.1110 -
  6.1111 -subsubsection {* Cardinality as special case of @{const setsum} *}
  6.1112 -
  6.1113 -lemma card_eq_setsum:
  6.1114 -  "card A = setsum (\<lambda>x. 1) A"
  6.1115 -  by (simp only: card_def setsum_def)
  6.1116 -
  6.1117  lemma card_UN_disjoint:
  6.1118    assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
  6.1119      and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
  6.1120 @@ -869,17 +1018,6 @@
  6.1121  apply (simp_all add: SUP_def id_def)
  6.1122  done
  6.1123  
  6.1124 -text{*The image of a finite set can be expressed using @{term fold_image}.*}
  6.1125 -lemma image_eq_fold_image:
  6.1126 -  "finite A ==> f ` A = fold_image (op Un) (%x. {f x}) {} A"
  6.1127 -proof (induct rule: finite_induct)
  6.1128 -  case empty then show ?case by simp
  6.1129 -next
  6.1130 -  interpret ab_semigroup_mult "op Un"
  6.1131 -    proof qed auto
  6.1132 -  case insert 
  6.1133 -  then show ?case by simp
  6.1134 -qed
  6.1135  
  6.1136  subsubsection {* Cardinality of products *}
  6.1137  
  6.1138 @@ -904,15 +1042,23 @@
  6.1139  
  6.1140  subsection {* Generalized product over a set *}
  6.1141  
  6.1142 -definition (in comm_monoid_mult) setprod :: "('b \<Rightarrow> 'a) => 'b set => 'a" where
  6.1143 -  "setprod f A = (if finite A then fold_image (op *) f 1 A else 1)"
  6.1144 +definition (in comm_monoid_mult) setprod :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
  6.1145 +where
  6.1146 +  "setprod = comm_monoid_set.F times 1"
  6.1147  
  6.1148 -sublocale comm_monoid_mult < setprod!: comm_monoid_big "op *" 1 setprod proof
  6.1149 -qed (fact setprod_def)
  6.1150 +sublocale comm_monoid_mult < setprod!: comm_monoid_set times 1
  6.1151 +where
  6.1152 +  "setprod.F = setprod"
  6.1153 +proof -
  6.1154 +  show "comm_monoid_set times 1" ..
  6.1155 +  then interpret setprod!: comm_monoid_set times 1 .
  6.1156 +  show "setprod.F = setprod"
  6.1157 +    by (simp only: setprod_def)
  6.1158 +qed
  6.1159  
  6.1160  abbreviation
  6.1161 -  Setprod  ("\<Prod>_" [1000] 999) where
  6.1162 -  "\<Prod>A == setprod (%x. x) A"
  6.1163 +  Setprod ("\<Prod>_" [1000] 999) where
  6.1164 +  "\<Prod>A \<equiv> setprod (\<lambda>x. x) A"
  6.1165  
  6.1166  syntax
  6.1167    "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
  6.1168 @@ -939,6 +1085,55 @@
  6.1169    "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"
  6.1170    "\<Prod>x|P. t" => "CONST setprod (%x. t) {x. P}"
  6.1171  
  6.1172 +text {* TODO These are candidates for generalization *}
  6.1173 +
  6.1174 +context comm_monoid_mult
  6.1175 +begin
  6.1176 +
  6.1177 +lemma setprod_reindex_id:
  6.1178 +  "inj_on f B ==> setprod f B = setprod id (f ` B)"
  6.1179 +  by (auto simp add: setprod.reindex)
  6.1180 +
  6.1181 +lemma setprod_reindex_cong:
  6.1182 +  "inj_on f A ==> B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
  6.1183 +  by (frule setprod.reindex, simp)
  6.1184 +
  6.1185 +lemma strong_setprod_reindex_cong:
  6.1186 +  assumes i: "inj_on f A"
  6.1187 +  and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x"
  6.1188 +  shows "setprod h B = setprod g A"
  6.1189 +proof-
  6.1190 +  have "setprod h B = setprod (h o f) A"
  6.1191 +    by (simp add: B setprod.reindex [OF i, of h])
  6.1192 +  then show ?thesis apply simp
  6.1193 +    apply (rule setprod.cong)
  6.1194 +    apply simp
  6.1195 +    by (simp add: eq)
  6.1196 +qed
  6.1197 +
  6.1198 +lemma setprod_Union_disjoint:
  6.1199 +  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" 
  6.1200 +  shows "setprod f (Union C) = setprod (setprod f) C"
  6.1201 +  using assms by (fact setprod.Union_disjoint)
  6.1202 +
  6.1203 +text{*Here we can eliminate the finiteness assumptions, by cases.*}
  6.1204 +lemma setprod_cartesian_product:
  6.1205 +  "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
  6.1206 +  by (fact setprod.cartesian_product)
  6.1207 +
  6.1208 +lemma setprod_Un2:
  6.1209 +  assumes "finite (A \<union> B)"
  6.1210 +  shows "setprod f (A \<union> B) = setprod f (A - B) * setprod f (B - A) * setprod f (A \<inter> B)"
  6.1211 +proof -
  6.1212 +  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
  6.1213 +    by auto
  6.1214 +  with assms show ?thesis by simp (subst setprod.union_disjoint, auto)+
  6.1215 +qed
  6.1216 +
  6.1217 +end
  6.1218 +
  6.1219 +text {* TODO These are legacy *}
  6.1220 +
  6.1221  lemma setprod_empty: "setprod f {} = 1"
  6.1222    by (fact setprod.empty)
  6.1223  
  6.1224 @@ -950,126 +1145,91 @@
  6.1225    by (fact setprod.infinite)
  6.1226  
  6.1227  lemma setprod_reindex:
  6.1228 -   "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
  6.1229 -by(auto simp: setprod_def fold_image_reindex o_def dest!:finite_imageD)
  6.1230 -
  6.1231 -lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
  6.1232 -by (auto simp add: setprod_reindex)
  6.1233 +  "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
  6.1234 +  by (fact setprod.reindex)
  6.1235  
  6.1236  lemma setprod_cong:
  6.1237    "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
  6.1238 -by(fact setprod.F_cong)
  6.1239 +  by (fact setprod.cong)
  6.1240  
  6.1241  lemma strong_setprod_cong:
  6.1242    "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"
  6.1243 -by(fact setprod.strong_F_cong)
  6.1244 -
  6.1245 -lemma setprod_reindex_cong: "inj_on f A ==>
  6.1246 -    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
  6.1247 -by (frule setprod_reindex, simp)
  6.1248 +  by (fact setprod.strong_cong)
  6.1249  
  6.1250 -lemma strong_setprod_reindex_cong: assumes i: "inj_on f A"
  6.1251 -  and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x"
  6.1252 -  shows "setprod h B = setprod g A"
  6.1253 -proof-
  6.1254 -    have "setprod h B = setprod (h o f) A"
  6.1255 -      by (simp add: B setprod_reindex[OF i, of h])
  6.1256 -    then show ?thesis apply simp
  6.1257 -      apply (rule setprod_cong)
  6.1258 -      apply simp
  6.1259 -      by (simp add: eq)
  6.1260 -qed
  6.1261 +lemma setprod_Un_one:
  6.1262 +  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 1 \<rbrakk>
  6.1263 +  \<Longrightarrow> setprod f (S \<union> T) = setprod f S  * setprod f T"
  6.1264 +  by (fact setprod.union_inter_neutral)
  6.1265  
  6.1266 -lemma setprod_Un_one: "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 1 \<rbrakk>
  6.1267 -  \<Longrightarrow> setprod f (S \<union> T) = setprod f S  * setprod f T"
  6.1268 -by(fact setprod.F_Un_neutral)
  6.1269 -
  6.1270 -lemmas setprod_1 = setprod.F_neutral
  6.1271 -lemmas setprod_1' = setprod.F_neutral'
  6.1272 -
  6.1273 +lemmas setprod_1 = setprod.neutral_const
  6.1274 +lemmas setprod_1' = setprod.neutral
  6.1275  
  6.1276  lemma setprod_Un_Int: "finite A ==> finite B
  6.1277      ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
  6.1278 -by (fact setprod.union_inter)
  6.1279 +  by (fact setprod.union_inter)
  6.1280  
  6.1281  lemma setprod_Un_disjoint: "finite A ==> finite B
  6.1282    ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
  6.1283 -by (fact setprod.union_disjoint)
  6.1284 +  by (fact setprod.union_disjoint)
  6.1285  
  6.1286  lemma setprod_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
  6.1287      setprod f A = setprod f (A - B) * setprod f B"
  6.1288 -by(fact setprod.F_subset_diff)
  6.1289 +  by (fact setprod.subset_diff)
  6.1290  
  6.1291  lemma setprod_mono_one_left:
  6.1292    "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 1 \<rbrakk> \<Longrightarrow> setprod f S = setprod f T"
  6.1293 -by(fact setprod.F_mono_neutral_left)
  6.1294 +  by (fact setprod.mono_neutral_left)
  6.1295  
  6.1296 -lemmas setprod_mono_one_right = setprod.F_mono_neutral_right
  6.1297 +lemmas setprod_mono_one_right = setprod.mono_neutral_right
  6.1298  
  6.1299  lemma setprod_mono_one_cong_left: 
  6.1300    "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1; \<And>x. x \<in> S \<Longrightarrow> f x = g x \<rbrakk>
  6.1301    \<Longrightarrow> setprod f S = setprod g T"
  6.1302 -by(fact setprod.F_mono_neutral_cong_left)
  6.1303 +  by (fact setprod.mono_neutral_cong_left)
  6.1304  
  6.1305 -lemmas setprod_mono_one_cong_right = setprod.F_mono_neutral_cong_right
  6.1306 +lemmas setprod_mono_one_cong_right = setprod.mono_neutral_cong_right
  6.1307  
  6.1308  lemma setprod_delta: "finite S \<Longrightarrow>
  6.1309    setprod (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
  6.1310 -by(fact setprod.F_delta)
  6.1311 +  by (fact setprod.delta)
  6.1312  
  6.1313  lemma setprod_delta': "finite S \<Longrightarrow>
  6.1314    setprod (\<lambda>k. if a = k then b k else 1) S = (if a\<in> S then b a else 1)"
  6.1315 -by(fact setprod.F_delta')
  6.1316 +  by (fact setprod.delta')
  6.1317  
  6.1318  lemma setprod_UN_disjoint:
  6.1319      "finite I ==> (ALL i:I. finite (A i)) ==>
  6.1320          (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
  6.1321        setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
  6.1322 -  by (simp add: setprod_def fold_image_UN_disjoint)
  6.1323 -
  6.1324 -lemma setprod_Union_disjoint:
  6.1325 -  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" 
  6.1326 -  shows "setprod f (Union C) = setprod (setprod f) C"
  6.1327 -proof cases
  6.1328 -  assume "finite C"
  6.1329 -  from setprod_UN_disjoint[OF this assms]
  6.1330 -  show ?thesis
  6.1331 -    by (simp add: SUP_def)
  6.1332 -qed (force dest: finite_UnionD simp add: setprod_def)
  6.1333 +  by (fact setprod.UNION_disjoint)
  6.1334  
  6.1335  lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
  6.1336      (\<Prod>x\<in>A. (\<Prod>y\<in> B x. f x y)) =
  6.1337      (\<Prod>(x,y)\<in>(SIGMA x:A. B x). f x y)"
  6.1338 -by(simp add:setprod_def fold_image_Sigma split_def)
  6.1339 +  by (fact setprod.Sigma)
  6.1340  
  6.1341 -text{*Here we can eliminate the finiteness assumptions, by cases.*}
  6.1342 -lemma setprod_cartesian_product: 
  6.1343 -     "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
  6.1344 -apply (cases "finite A") 
  6.1345 - apply (cases "finite B") 
  6.1346 -  apply (simp add: setprod_Sigma)
  6.1347 - apply (cases "A={}", simp)
  6.1348 - apply (simp) 
  6.1349 -apply (auto simp add: setprod_def
  6.1350 -            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
  6.1351 -done
  6.1352 -
  6.1353 -lemma setprod_timesf: "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
  6.1354 -by (fact setprod.F_fun_f)
  6.1355 +lemma setprod_timesf: "setprod (\<lambda>x. f x * g x) A = setprod f A * setprod g A"
  6.1356 +  by (fact setprod.distrib)
  6.1357  
  6.1358  
  6.1359  subsubsection {* Properties in more restricted classes of structures *}
  6.1360  
  6.1361 -lemma setprod_eq_1_iff [simp]:
  6.1362 -  "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
  6.1363 -by (induct set: finite) auto
  6.1364 -
  6.1365  lemma setprod_zero:
  6.1366       "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
  6.1367  apply (induct set: finite, force, clarsimp)
  6.1368  apply (erule disjE, auto)
  6.1369  done
  6.1370  
  6.1371 +lemma setprod_zero_iff[simp]: "finite A ==> 
  6.1372 +  (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =
  6.1373 +  (EX x: A. f x = 0)"
  6.1374 +by (erule finite_induct, auto simp:no_zero_divisors)
  6.1375 +
  6.1376 +lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
  6.1377 +  (setprod f (A Un B) :: 'a ::{field})
  6.1378 +   = setprod f A * setprod f B / setprod f (A Int B)"
  6.1379 +by (subst setprod_Un_Int [symmetric], auto)
  6.1380 +
  6.1381  lemma setprod_nonneg [rule_format]:
  6.1382     "(ALL x: A. (0::'a::linordered_semidom) \<le> f x) --> 0 \<le> setprod f A"
  6.1383  by (cases "finite A", induct set: finite, simp_all add: mult_nonneg_nonneg)
  6.1384 @@ -1078,33 +1238,6 @@
  6.1385    --> 0 < setprod f A"
  6.1386  by (cases "finite A", induct set: finite, simp_all add: mult_pos_pos)
  6.1387  
  6.1388 -lemma setprod_zero_iff[simp]: "finite A ==> 
  6.1389 -  (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =
  6.1390 -  (EX x: A. f x = 0)"
  6.1391 -by (erule finite_induct, auto simp:no_zero_divisors)
  6.1392 -
  6.1393 -lemma setprod_pos_nat:
  6.1394 -  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"
  6.1395 -using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
  6.1396 -
  6.1397 -lemma setprod_pos_nat_iff[simp]:
  6.1398 -  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"
  6.1399 -using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
  6.1400 -
  6.1401 -lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
  6.1402 -  (setprod f (A Un B) :: 'a ::{field})
  6.1403 -   = setprod f A * setprod f B / setprod f (A Int B)"
  6.1404 -by (subst setprod_Un_Int [symmetric], auto)
  6.1405 -
  6.1406 -lemma setprod_Un2:
  6.1407 -  assumes "finite (A \<union> B)"
  6.1408 -  shows "setprod f (A \<union> B) = setprod f (A - B) * setprod f (B - A) * setprod f (A \<inter> B)"
  6.1409 -proof -
  6.1410 -  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
  6.1411 -    by auto
  6.1412 -  with assms show ?thesis by simp (subst setprod_Un_disjoint, auto)+
  6.1413 -qed
  6.1414 -
  6.1415  lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
  6.1416    (setprod f (A - {a}) :: 'a :: {field}) =
  6.1417    (if a:A then setprod f A / f a else setprod f A)"
  6.1418 @@ -1197,7 +1330,7 @@
  6.1419  
  6.1420  lemma setprod_gen_delta:
  6.1421    assumes fS: "finite S"
  6.1422 -  shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::{comm_monoid_mult}) * c^ (card S - 1) else c^ card S)"
  6.1423 +  shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::comm_monoid_mult) * c^ (card S - 1) else c^ card S)"
  6.1424  proof-
  6.1425    let ?f = "(\<lambda>k. if k=a then b k else c)"
  6.1426    {assume a: "a \<notin> S"
  6.1427 @@ -1222,150 +1355,431 @@
  6.1428    ultimately show ?thesis by blast
  6.1429  qed
  6.1430  
  6.1431 +lemma setprod_eq_1_iff [simp]:
  6.1432 +  "finite F ==> setprod f F = 1 \<longleftrightarrow> (ALL a:F. f a = (1::nat))"
  6.1433 +  by (induct set: finite) auto
  6.1434  
  6.1435 -subsection {* Versions of @{const inf} and @{const sup} on non-empty sets *}
  6.1436 +lemma setprod_pos_nat:
  6.1437 +  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"
  6.1438 +using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
  6.1439 +
  6.1440 +lemma setprod_pos_nat_iff[simp]:
  6.1441 +  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"
  6.1442 +using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
  6.1443 +
  6.1444 +
  6.1445 +subsection {* Generic lattice operations over a set *}
  6.1446  
  6.1447  no_notation times (infixl "*" 70)
  6.1448  no_notation Groups.one ("1")
  6.1449  
  6.1450 -locale semilattice_big = semilattice +
  6.1451 -  fixes F :: "'a set \<Rightarrow> 'a"
  6.1452 -  assumes F_eq: "finite A \<Longrightarrow> F A = fold1 (op *) A"
  6.1453 +
  6.1454 +subsubsection {* Without neutral element *}
  6.1455 +
  6.1456 +locale semilattice_set = semilattice
  6.1457 +begin
  6.1458 +
  6.1459 +definition F :: "'a set \<Rightarrow> 'a"
  6.1460 +where
  6.1461 +  eq_fold': "F A = the (Finite_Set.fold (\<lambda>x y. Some (case y of None \<Rightarrow> x | Some z \<Rightarrow> f x z)) None A)"
  6.1462 +
  6.1463 +lemma eq_fold:
  6.1464 +  assumes "finite A"
  6.1465 +  shows "F (insert x A) = Finite_Set.fold f x A"
  6.1466 +proof (rule sym)
  6.1467 +  let ?f = "\<lambda>x y. Some (case y of None \<Rightarrow> x | Some z \<Rightarrow> f x z)"
  6.1468 +  interpret comp_fun_idem f
  6.1469 +    by default (simp_all add: fun_eq_iff left_commute)
  6.1470 +  interpret comp_fun_idem "?f"
  6.1471 +    by default (simp_all add: fun_eq_iff commute left_commute split: option.split)
  6.1472 +  from assms show "Finite_Set.fold f x A = F (insert x A)"
  6.1473 +  proof induct
  6.1474 +    case empty then show ?case by (simp add: eq_fold')
  6.1475 +  next
  6.1476 +    case (insert y B) then show ?case by (simp add: insert_commute [of x] eq_fold')
  6.1477 +  qed
  6.1478 +qed
  6.1479 +
  6.1480 +lemma singleton [simp]:
  6.1481 +  "F {x} = x"
  6.1482 +  by (simp add: eq_fold)
  6.1483 +
  6.1484 +lemma insert_not_elem:
  6.1485 +  assumes "finite A" and "x \<notin> A" and "A \<noteq> {}"
  6.1486 +  shows "F (insert x A) = x * F A"
  6.1487 +proof -
  6.1488 +  interpret comp_fun_idem f
  6.1489 +    by default (simp_all add: fun_eq_iff left_commute)
  6.1490 +  from `A \<noteq> {}` obtain b where "b \<in> A" by blast
  6.1491 +  then obtain B where *: "A = insert b B" "b \<notin> B" by (blast dest: mk_disjoint_insert)
  6.1492 +  with `finite A` and `x \<notin> A`
  6.1493 +    have "finite (insert x B)" and "b \<notin> insert x B" by auto
  6.1494 +  then have "F (insert b (insert x B)) = x * F (insert b B)"
  6.1495 +    by (simp add: eq_fold)
  6.1496 +  then show ?thesis by (simp add: * insert_commute)
  6.1497 +qed
  6.1498 +
  6.1499 +lemma subsumption:
  6.1500 +  assumes "finite A" and "x \<in> A"
  6.1501 +  shows "x * F A = F A"
  6.1502 +proof -
  6.1503 +  from assms have "A \<noteq> {}" by auto
  6.1504 +  with `finite A` show ?thesis using `x \<in> A`
  6.1505 +    by (induct A rule: finite_ne_induct) (auto simp add: ac_simps insert_not_elem)
  6.1506 +qed
  6.1507 +
  6.1508 +lemma insert [simp]:
  6.1509 +  assumes "finite A" and "A \<noteq> {}"
  6.1510 +  shows "F (insert x A) = x * F A"
  6.1511 +  using assms by (cases "x \<in> A") (simp_all add: insert_absorb subsumption insert_not_elem)
  6.1512 +
  6.1513 +lemma union:
  6.1514 +  assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}"
  6.1515 +  shows "F (A \<union> B) = F A * F B"
  6.1516 +  using assms by (induct A rule: finite_ne_induct) (simp_all add: ac_simps)
  6.1517 +
  6.1518 +lemma remove:
  6.1519 +  assumes "finite A" and "x \<in> A"
  6.1520 +  shows "F A = (if A - {x} = {} then x else x * F (A - {x}))"
  6.1521 +proof -
  6.1522 +  from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
  6.1523 +  with assms show ?thesis by simp
  6.1524 +qed
  6.1525 +
  6.1526 +lemma insert_remove:
  6.1527 +  assumes "finite A"
  6.1528 +  shows "F (insert x A) = (if A - {x} = {} then x else x * F (A - {x}))"
  6.1529 +  using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
  6.1530 +
  6.1531 +lemma subset:
  6.1532 +  assumes "finite A" "B \<noteq> {}" and "B \<subseteq> A"
  6.1533 +  shows "F B * F A = F A"
  6.1534 +proof -
  6.1535 +  from assms have "A \<noteq> {}" and "finite B" by (auto dest: finite_subset)
  6.1536 +  with assms show ?thesis by (simp add: union [symmetric] Un_absorb1)
  6.1537 +qed
  6.1538 +
  6.1539 +lemma closed:
  6.1540 +  assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
  6.1541 +  shows "F A \<in> A"
  6.1542 +using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
  6.1543 +  case singleton then show ?case by simp
  6.1544 +next
  6.1545 +  case insert with elem show ?case by force
  6.1546 +qed
  6.1547 +
  6.1548 +lemma hom_commute:
  6.1549 +  assumes hom: "\<And>x y. h (x * y) = h x * h y"
  6.1550 +  and N: "finite N" "N \<noteq> {}"
  6.1551 +  shows "h (F N) = F (h ` N)"
  6.1552 +using N proof (induct rule: finite_ne_induct)
  6.1553 +  case singleton thus ?case by simp
  6.1554 +next
  6.1555 +  case (insert n N)
  6.1556 +  then have "h (F (insert n N)) = h (n * F N)" by simp
  6.1557 +  also have "\<dots> = h n * h (F N)" by (rule hom)
  6.1558 +  also have "h (F N) = F (h ` N)" by (rule insert)
  6.1559 +  also have "h n * \<dots> = F (insert (h n) (h ` N))"
  6.1560 +    using insert by simp
  6.1561 +  also have "insert (h n) (h ` N) = h ` insert n N" by simp
  6.1562 +  finally show ?case .
  6.1563 +qed
  6.1564 +
  6.1565 +end
  6.1566 +
  6.1567 +locale semilattice_order_set = semilattice_order + semilattice_set
  6.1568 +begin
  6.1569 +
  6.1570 +lemma bounded_iff:
  6.1571 +  assumes "finite A" and "A \<noteq> {}"
  6.1572 +  shows "x \<preceq> F A \<longleftrightarrow> (\<forall>a\<in>A. x \<preceq> a)"
  6.1573 +  using assms by (induct rule: finite_ne_induct) (simp_all add: bounded_iff)
  6.1574 +
  6.1575 +lemma boundedI:
  6.1576 +  assumes "finite A"
  6.1577 +  assumes "A \<noteq> {}"
  6.1578 +  assumes "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
  6.1579 +  shows "x \<preceq> F A"
  6.1580 +  using assms by (simp add: bounded_iff)
  6.1581 +
  6.1582 +lemma boundedE:
  6.1583 +  assumes "finite A" and "A \<noteq> {}" and "x \<preceq> F A"
  6.1584 +  obtains "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
  6.1585 +  using assms by (simp add: bounded_iff)
  6.1586  
  6.1587 -sublocale semilattice_big < folding_one_idem proof
  6.1588 -qed (simp_all add: F_eq)
  6.1589 +lemma coboundedI:
  6.1590 +  assumes "finite A"
  6.1591 +    and "a \<in> A"
  6.1592 +  shows "F A \<preceq> a"
  6.1593 +proof -
  6.1594 +  from assms have "A \<noteq> {}" by auto
  6.1595 +  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
  6.1596 +  proof (induct rule: finite_ne_induct)
  6.1597 +    case singleton thus ?case by (simp add: refl)
  6.1598 +  next
  6.1599 +    case (insert x B)
  6.1600 +    from insert have "a = x \<or> a \<in> B" by simp
  6.1601 +    then show ?case using insert by (auto intro: coboundedI2)
  6.1602 +  qed
  6.1603 +qed
  6.1604 +
  6.1605 +lemma antimono:
  6.1606 +  assumes "A \<subseteq> B" and "A \<noteq> {}" and "finite B"
  6.1607 +  shows "F B \<preceq> F A"
  6.1608 +proof (cases "A = B")
  6.1609 +  case True then show ?thesis by (simp add: refl)
  6.1610 +next
  6.1611 +  case False
  6.1612 +  have B: "B = A \<union> (B - A)" using `A \<subseteq> B` by blast
  6.1613 +  then have "F B = F (A \<union> (B - A))" by simp
  6.1614 +  also have "\<dots> = F A * F (B - A)" using False assms by (subst union) (auto intro: finite_subset)
  6.1615 +  also have "\<dots> \<preceq> F A" by simp
  6.1616 +  finally show ?thesis .
  6.1617 +qed
  6.1618 +
  6.1619 +end
  6.1620 +
  6.1621 +
  6.1622 +subsubsection {* With neutral element *}
  6.1623 +
  6.1624 +locale semilattice_neutr_set = semilattice_neutr
  6.1625 +begin
  6.1626 +
  6.1627 +definition F :: "'a set \<Rightarrow> 'a"
  6.1628 +where
  6.1629 +  eq_fold: "F A = Finite_Set.fold f 1 A"
  6.1630 +
  6.1631 +lemma infinite [simp]:
  6.1632 +  "\<not> finite A \<Longrightarrow> F A = 1"
  6.1633 +  by (simp add: eq_fold)
  6.1634 +
  6.1635 +lemma empty [simp]:
  6.1636 +  "F {} = 1"
  6.1637 +  by (simp add: eq_fold)
  6.1638 +
  6.1639 +lemma insert [simp]:
  6.1640 +  assumes "finite A"
  6.1641 +  shows "F (insert x A) = x * F A"
  6.1642 +proof -
  6.1643 +  interpret comp_fun_idem f
  6.1644 +    by default (simp_all add: fun_eq_iff left_commute)
  6.1645 +  from assms show ?thesis by (simp add: eq_fold)
  6.1646 +qed
  6.1647 +
  6.1648 +lemma subsumption:
  6.1649 +  assumes "finite A" and "x \<in> A"
  6.1650 +  shows "x * F A = F A"
  6.1651 +proof -
  6.1652 +  from assms have "A \<noteq> {}" by auto
  6.1653 +  with `finite A` show ?thesis using `x \<in> A`
  6.1654 +    by (induct A rule: finite_ne_induct) (auto simp add: ac_simps)
  6.1655 +qed
  6.1656 +
  6.1657 +lemma union:
  6.1658 +  assumes "finite A" and "finite B"
  6.1659 +  shows "F (A \<union> B) = F A * F B"
  6.1660 +  using assms by (induct A) (simp_all add: ac_simps)
  6.1661 +
  6.1662 +lemma remove:
  6.1663 +  assumes "finite A" and "x \<in> A"
  6.1664 +  shows "F A = x * F (A - {x})"
  6.1665 +proof -
  6.1666 +  from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
  6.1667 +  with assms show ?thesis by simp
  6.1668 +qed
  6.1669 +
  6.1670 +lemma insert_remove:
  6.1671 +  assumes "finite A"
  6.1672 +  shows "F (insert x A) = x * F (A - {x})"
  6.1673 +  using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
  6.1674 +
  6.1675 +lemma subset:
  6.1676 +  assumes "finite A" and "B \<subseteq> A"
  6.1677 +  shows "F B * F A = F A"
  6.1678 +proof -
  6.1679 +  from assms have "finite B" by (auto dest: finite_subset)
  6.1680 +  with assms show ?thesis by (simp add: union [symmetric] Un_absorb1)
  6.1681 +qed
  6.1682 +
  6.1683 +lemma closed:
  6.1684 +  assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
  6.1685 +  shows "F A \<in> A"
  6.1686 +using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
  6.1687 +  case singleton then show ?case by simp
  6.1688 +next
  6.1689 +  case insert with elem show ?case by force
  6.1690 +qed
  6.1691 +
  6.1692 +end
  6.1693 +
  6.1694 +locale semilattice_order_neutr_set = semilattice_neutr_order + semilattice_neutr_set
  6.1695 +begin
  6.1696 +
  6.1697 +lemma bounded_iff:
  6.1698 +  assumes "finite A"
  6.1699 +  shows "x \<preceq> F A \<longleftrightarrow> (\<forall>a\<in>A. x \<preceq> a)"
  6.1700 +  using assms by (induct A) (simp_all add: bounded_iff)
  6.1701 +
  6.1702 +lemma boundedI:
  6.1703 +  assumes "finite A"
  6.1704 +  assumes "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
  6.1705 +  shows "x \<preceq> F A"
  6.1706 +  using assms by (simp add: bounded_iff)
  6.1707 +
  6.1708 +lemma boundedE:
  6.1709 +  assumes "finite A" and "x \<preceq> F A"
  6.1710 +  obtains "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
  6.1711 +  using assms by (simp add: bounded_iff)
  6.1712 +
  6.1713 +lemma coboundedI:
  6.1714 +  assumes "finite A"
  6.1715 +    and "a \<in> A"
  6.1716 +  shows "F A \<preceq> a"
  6.1717 +proof -
  6.1718 +  from assms have "A \<noteq> {}" by auto
  6.1719 +  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
  6.1720 +  proof (induct rule: finite_ne_induct)
  6.1721 +    case singleton thus ?case by (simp add: refl)
  6.1722 +  next
  6.1723 +    case (insert x B)
  6.1724 +    from insert have "a = x \<or> a \<in> B" by simp
  6.1725 +    then show ?case using insert by (auto intro: coboundedI2)
  6.1726 +  qed
  6.1727 +qed
  6.1728 +
  6.1729 +lemma antimono:
  6.1730 +  assumes "A \<subseteq> B" and "finite B"
  6.1731 +  shows "F B \<preceq> F A"
  6.1732 +proof (cases "A = B")
  6.1733 +  case True then show ?thesis by (simp add: refl)
  6.1734 +next
  6.1735 +  case False
  6.1736 +  have B: "B = A \<union> (B - A)" using `A \<subseteq> B` by blast
  6.1737 +  then have "F B = F (A \<union> (B - A))" by simp
  6.1738 +  also have "\<dots> = F A * F (B - A)" using False assms by (subst union) (auto intro: finite_subset)
  6.1739 +  also have "\<dots> \<preceq> F A" by simp
  6.1740 +  finally show ?thesis .
  6.1741 +qed
  6.1742 +
  6.1743 +end
  6.1744  
  6.1745  notation times (infixl "*" 70)
  6.1746  notation Groups.one ("1")
  6.1747  
  6.1748 -context lattice
  6.1749 -begin
  6.1750  
  6.1751 -definition Inf_fin :: "'a set \<Rightarrow> 'a" ("\<Sqinter>\<^bsub>fin\<^esub>_" [900] 900) where
  6.1752 -  "Inf_fin = fold1 inf"
  6.1753 -
  6.1754 -definition Sup_fin :: "'a set \<Rightarrow> 'a" ("\<Squnion>\<^bsub>fin\<^esub>_" [900] 900) where
  6.1755 -  "Sup_fin = fold1 sup"
  6.1756 -
  6.1757 -end
  6.1758 +subsection {* Lattice operations on finite sets *}
  6.1759  
  6.1760 -sublocale lattice < Inf_fin!: semilattice_big inf Inf_fin proof
  6.1761 -qed (simp add: Inf_fin_def)
  6.1762 -
  6.1763 -sublocale lattice < Sup_fin!: semilattice_big sup Sup_fin proof
  6.1764 -qed (simp add: Sup_fin_def)
  6.1765 +text {*
  6.1766 +  For historic reasons, there is the sublocale dependency from @{class distrib_lattice}
  6.1767 +  to @{class linorder}.  This is badly designed: both should depend on a common abstract
  6.1768 +  distributive lattice rather than having this non-subclass dependecy between two
  6.1769 +  classes.  But for the moment we have to live with it.  This forces us to setup
  6.1770 +  this sublocale dependency simultaneously with the lattice operations on finite
  6.1771 +  sets, to avoid garbage.
  6.1772 +*}
  6.1773  
  6.1774 -context semilattice_inf
  6.1775 -begin
  6.1776 +definition (in semilattice_inf) Inf_fin :: "'a set \<Rightarrow> 'a" ("\<Sqinter>\<^bsub>fin\<^esub>_" [900] 900)
  6.1777 +where
  6.1778 +  "Inf_fin = semilattice_set.F inf"
  6.1779  
  6.1780 -lemma ab_semigroup_idem_mult_inf:
  6.1781 -  "class.ab_semigroup_idem_mult inf"
  6.1782 -proof qed (rule inf_assoc inf_commute inf_idem)+
  6.1783 +definition (in semilattice_sup) Sup_fin :: "'a set \<Rightarrow> 'a" ("\<Squnion>\<^bsub>fin\<^esub>_" [900] 900)
  6.1784 +where
  6.1785 +  "Sup_fin = semilattice_set.F sup"
  6.1786  
  6.1787 -lemma fold_inf_insert[simp]: "finite A \<Longrightarrow> Finite_Set.fold inf b (insert a A) = inf a (Finite_Set.fold inf b A)"
  6.1788 -by(rule comp_fun_idem.fold_insert_idem[OF ab_semigroup_idem_mult.comp_fun_idem[OF ab_semigroup_idem_mult_inf]])
  6.1789 +definition (in linorder) Min :: "'a set \<Rightarrow> 'a"
  6.1790 +where
  6.1791 +  "Min = semilattice_set.F min"
  6.1792  
  6.1793 -lemma inf_le_fold_inf: "finite A \<Longrightarrow> ALL a:A. b \<le> a \<Longrightarrow> inf b c \<le> Finite_Set.fold inf c A"
  6.1794 -by (induct pred: finite) (auto intro: le_infI1)
  6.1795 +definition (in linorder) Max :: "'a set \<Rightarrow> 'a"
  6.1796 +where
  6.1797 +  "Max = semilattice_set.F max"
  6.1798 +
  6.1799 +text {* An aside: @{const min}/@{const max} on linear orders as special case of @{const inf}/@{const sup} *}
  6.1800  
  6.1801 -lemma fold_inf_le_inf: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> Finite_Set.fold inf b A \<le> inf a b"
  6.1802 -proof(induct arbitrary: a pred:finite)
  6.1803 -  case empty thus ?case by simp
  6.1804 -next
  6.1805 -  case (insert x A)
  6.1806 -  show ?case
  6.1807 -  proof cases
  6.1808 -    assume "A = {}" thus ?thesis using insert by simp
  6.1809 -  next
  6.1810 -    assume "A \<noteq> {}" thus ?thesis using insert by (auto intro: le_infI2)
  6.1811 -  qed
  6.1812 -qed
  6.1813 -
  6.1814 -lemma below_fold1_iff:
  6.1815 -  assumes "finite A" "A \<noteq> {}"
  6.1816 -  shows "x \<le> fold1 inf A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
  6.1817 +sublocale linorder < min_max!: distrib_lattice min less_eq less max
  6.1818 +where
  6.1819 +  "semilattice_inf.Inf_fin min = Min"
  6.1820 +  and "semilattice_sup.Sup_fin max = Max"
  6.1821  proof -
  6.1822 -  interpret ab_semigroup_idem_mult inf
  6.1823 -    by (rule ab_semigroup_idem_mult_inf)
  6.1824 -  show ?thesis using assms by (induct rule: finite_ne_induct) simp_all
  6.1825 +  show "class.distrib_lattice min less_eq less max"
  6.1826 +  proof
  6.1827 +    fix x y z
  6.1828 +    show "max x (min y z) = min (max x y) (max x z)"
  6.1829 +      by (auto simp add: min_def max_def)
  6.1830 +  qed (auto simp add: min_def max_def not_le less_imp_le)
  6.1831 +  then interpret min_max!: distrib_lattice min less_eq less max .
  6.1832 +  show "semilattice_inf.Inf_fin min = Min"
  6.1833 +    by (simp only: min_max.Inf_fin_def Min_def)
  6.1834 +  show "semilattice_sup.Sup_fin max = Max"
  6.1835 +    by (simp only: min_max.Sup_fin_def Max_def)
  6.1836  qed
  6.1837  
  6.1838 -lemma fold1_belowI:
  6.1839 -  assumes "finite A"
  6.1840 -    and "a \<in> A"
  6.1841 -  shows "fold1 inf A \<le> a"
  6.1842 +lemma inf_min: "inf = (min \<Colon> 'a\<Colon>{semilattice_inf, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
  6.1843 +  by (rule ext)+ (auto intro: antisym)
  6.1844 +
  6.1845 +lemma sup_max: "sup = (max \<Colon> 'a\<Colon>{semilattice_sup, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
  6.1846 +  by (rule ext)+ (auto intro: antisym)
  6.1847 +
  6.1848 +lemmas le_maxI1 = min_max.sup_ge1
  6.1849 +lemmas le_maxI2 = min_max.sup_ge2
  6.1850 + 
  6.1851 +lemmas min_ac = min_max.inf_assoc min_max.inf_commute
  6.1852 +  min_max.inf.left_commute
  6.1853 +
  6.1854 +lemmas max_ac = min_max.sup_assoc min_max.sup_commute
  6.1855 +  min_max.sup.left_commute
  6.1856 +
  6.1857 +
  6.1858 +text {* Lattice operations proper *}
  6.1859 +
  6.1860 +sublocale semilattice_inf < Inf_fin!: semilattice_order_set inf less_eq less
  6.1861 +where
  6.1862 +  "Inf_fin.F = Inf_fin"
  6.1863  proof -
  6.1864 -  from assms have "A \<noteq> {}" by auto
  6.1865 -  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
  6.1866 -  proof (induct rule: finite_ne_induct)
  6.1867 -    case singleton thus ?case by simp
  6.1868 -  next
  6.1869 -    interpret ab_semigroup_idem_mult inf
  6.1870 -      by (rule ab_semigroup_idem_mult_inf)
  6.1871 -    case (insert x F)
  6.1872 -    from insert(5) have "a = x \<or> a \<in> F" by simp
  6.1873 -    thus ?case
  6.1874 -    proof
  6.1875 -      assume "a = x" thus ?thesis using insert
  6.1876 -        by (simp add: mult_ac)
  6.1877 -    next
  6.1878 -      assume "a \<in> F"
  6.1879 -      hence bel: "fold1 inf F \<le> a" by (rule insert)
  6.1880 -      have "inf (fold1 inf (insert x F)) a = inf x (inf (fold1 inf F) a)"
  6.1881 -        using insert by (simp add: mult_ac)
  6.1882 -      also have "inf (fold1 inf F) a = fold1 inf F"
  6.1883 -        using bel by (auto intro: antisym)
  6.1884 -      also have "inf x \<dots> = fold1 inf (insert x F)"
  6.1885 -        using insert by (simp add: mult_ac)
  6.1886 -      finally have aux: "inf (fold1 inf (insert x F)) a = fold1 inf (insert x F)" .
  6.1887 -      moreover have "inf (fold1 inf (insert x F)) a \<le> a" by simp
  6.1888 -      ultimately show ?thesis by simp
  6.1889 -    qed
  6.1890 -  qed
  6.1891 +  show "semilattice_order_set inf less_eq less" ..
  6.1892 +  then interpret Inf_fin!: semilattice_order_set inf less_eq less.
  6.1893 +  show "Inf_fin.F = Inf_fin"
  6.1894 +    by (fact Inf_fin_def [symmetric])
  6.1895  qed
  6.1896  
  6.1897 -end
  6.1898 -
  6.1899 -context semilattice_sup
  6.1900 -begin
  6.1901 -
  6.1902 -lemma ab_semigroup_idem_mult_sup: "class.ab_semigroup_idem_mult sup"
  6.1903 -by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)
  6.1904 +sublocale semilattice_sup < Sup_fin!: semilattice_order_set sup greater_eq greater
  6.1905 +where
  6.1906 +  "Sup_fin.F = Sup_fin"
  6.1907 +proof -
  6.1908 +  show "semilattice_order_set sup greater_eq greater" ..
  6.1909 +  then interpret Sup_fin!: semilattice_order_set sup greater_eq greater .
  6.1910 +  show "Sup_fin.F = Sup_fin"
  6.1911 +    by (fact Sup_fin_def [symmetric])
  6.1912 +qed
  6.1913  
  6.1914 -lemma fold_sup_insert[simp]: "finite A \<Longrightarrow> Finite_Set.fold sup b (insert a A) = sup a (Finite_Set.fold sup b A)"
  6.1915 -by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)
  6.1916 +
  6.1917 +subsection {* Infimum and Supremum over non-empty sets *}
  6.1918  
  6.1919 -lemma fold_sup_le_sup: "finite A \<Longrightarrow> ALL a:A. a \<le> b \<Longrightarrow> Finite_Set.fold sup c A \<le> sup b c"
  6.1920 -by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)
  6.1921 -
  6.1922 -lemma sup_le_fold_sup: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a b \<le> Finite_Set.fold sup b A"
  6.1923 -by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)
  6.1924 -
  6.1925 -end
  6.1926 +text {*
  6.1927 +  After this non-regular bootstrap, things continue canonically.
  6.1928 +*}
  6.1929  
  6.1930  context lattice
  6.1931  begin
  6.1932  
  6.1933  lemma Inf_le_Sup [simp]: "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<Sqinter>\<^bsub>fin\<^esub>A \<le> \<Squnion>\<^bsub>fin\<^esub>A"
  6.1934 -apply(unfold Sup_fin_def Inf_fin_def)
  6.1935  apply(subgoal_tac "EX a. a:A")
  6.1936  prefer 2 apply blast
  6.1937  apply(erule exE)
  6.1938  apply(rule order_trans)
  6.1939 -apply(erule (1) fold1_belowI)
  6.1940 -apply(erule (1) semilattice_inf.fold1_belowI [OF dual_semilattice])
  6.1941 +apply(erule (1) Inf_fin.coboundedI)
  6.1942 +apply(erule (1) Sup_fin.coboundedI)
  6.1943  done
  6.1944  
  6.1945  lemma sup_Inf_absorb [simp]:
  6.1946    "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a (\<Sqinter>\<^bsub>fin\<^esub>A) = a"
  6.1947  apply(subst sup_commute)
  6.1948 -apply(simp add: Inf_fin_def sup_absorb2 fold1_belowI)
  6.1949 +apply(simp add: sup_absorb2 Inf_fin.coboundedI)
  6.1950  done
  6.1951  
  6.1952  lemma inf_Sup_absorb [simp]:
  6.1953    "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> inf a (\<Squnion>\<^bsub>fin\<^esub>A) = a"
  6.1954 -by (simp add: Sup_fin_def inf_absorb1
  6.1955 -  semilattice_inf.fold1_belowI [OF dual_semilattice])
  6.1956 +by (simp add: inf_absorb1 Sup_fin.coboundedI)
  6.1957  
  6.1958  end
  6.1959  
  6.1960 @@ -1376,27 +1790,19 @@
  6.1961    assumes "finite A"
  6.1962      and "A \<noteq> {}"
  6.1963    shows "sup x (\<Sqinter>\<^bsub>fin\<^esub>A) = \<Sqinter>\<^bsub>fin\<^esub>{sup x a|a. a \<in> A}"
  6.1964 -proof -
  6.1965 -  interpret ab_semigroup_idem_mult inf
  6.1966 -    by (rule ab_semigroup_idem_mult_inf)
  6.1967 -  from assms show ?thesis
  6.1968 -    by (simp add: Inf_fin_def image_def
  6.1969 -      hom_fold1_commute [where h="sup x", OF sup_inf_distrib1])
  6.1970 -        (rule arg_cong [where f="fold1 inf"], blast)
  6.1971 -qed
  6.1972 +using assms by (simp add: image_def Inf_fin.hom_commute [where h="sup x", OF sup_inf_distrib1])
  6.1973 +  (rule arg_cong [where f="Inf_fin"], blast)
  6.1974  
  6.1975  lemma sup_Inf2_distrib:
  6.1976    assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
  6.1977    shows "sup (\<Sqinter>\<^bsub>fin\<^esub>A) (\<Sqinter>\<^bsub>fin\<^esub>B) = \<Sqinter>\<^bsub>fin\<^esub>{sup a b|a b. a \<in> A \<and> b \<in> B}"
  6.1978  using A proof (induct rule: finite_ne_induct)
  6.1979 -  case singleton thus ?case
  6.1980 +  case singleton then show ?case
  6.1981      by (simp add: sup_Inf1_distrib [OF B])
  6.1982  next
  6.1983 -  interpret ab_semigroup_idem_mult inf
  6.1984 -    by (rule ab_semigroup_idem_mult_inf)
  6.1985    case (insert x A)
  6.1986    have finB: "finite {sup x b |b. b \<in> B}"
  6.1987 -    by(rule finite_surj[where f = "sup x", OF B(1)], auto)
  6.1988 +    by (rule finite_surj [where f = "sup x", OF B(1)], auto)
  6.1989    have finAB: "finite {sup a b |a b. a \<in> A \<and> b \<in> B}"
  6.1990    proof -
  6.1991      have "{sup a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {sup a b})"
  6.1992 @@ -1412,7 +1818,7 @@
  6.1993    also have "\<dots> = \<Sqinter>\<^bsub>fin\<^esub>({sup x b |b. b \<in> B} \<union> {sup a b |a b. a \<in> A \<and> b \<in> B})"
  6.1994      (is "_ = \<Sqinter>\<^bsub>fin\<^esub>?M")
  6.1995      using B insert
  6.1996 -    by (simp add: Inf_fin_def fold1_Un2 [OF finB _ finAB ne])
  6.1997 +    by (simp add: Inf_fin.union [OF finB _ finAB ne])
  6.1998    also have "?M = {sup a b |a b. a \<in> insert x A \<and> b \<in> B}"
  6.1999      by blast
  6.2000    finally show ?case .
  6.2001 @@ -1421,13 +1827,8 @@
  6.2002  lemma inf_Sup1_distrib:
  6.2003    assumes "finite A" and "A \<noteq> {}"
  6.2004    shows "inf x (\<Squnion>\<^bsub>fin\<^esub>A) = \<Squnion>\<^bsub>fin\<^esub>{inf x a|a. a \<in> A}"
  6.2005 -proof -
  6.2006 -  interpret ab_semigroup_idem_mult sup
  6.2007 -    by (rule ab_semigroup_idem_mult_sup)
  6.2008 -  from assms show ?thesis
  6.2009 -    by (simp add: Sup_fin_def image_def hom_fold1_commute [where h="inf x", OF inf_sup_distrib1])
  6.2010 -      (rule arg_cong [where f="fold1 sup"], blast)
  6.2011 -qed
  6.2012 +using assms by (simp add: image_def Sup_fin.hom_commute [where h="inf x", OF inf_sup_distrib1])
  6.2013 +  (rule arg_cong [where f="Sup_fin"], blast)
  6.2014  
  6.2015  lemma inf_Sup2_distrib:
  6.2016    assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
  6.2017 @@ -1446,8 +1847,6 @@
  6.2018      thus ?thesis by(simp add: insert(1) B(1))
  6.2019    qed
  6.2020    have ne: "{inf a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
  6.2021 -  interpret ab_semigroup_idem_mult sup
  6.2022 -    by (rule ab_semigroup_idem_mult_sup)
  6.2023    have "inf (\<Squnion>\<^bsub>fin\<^esub>(insert x A)) (\<Squnion>\<^bsub>fin\<^esub>B) = inf (sup x (\<Squnion>\<^bsub>fin\<^esub>A)) (\<Squnion>\<^bsub>fin\<^esub>B)"
  6.2024      using insert by simp
  6.2025    also have "\<dots> = sup (inf x (\<Squnion>\<^bsub>fin\<^esub>B)) (inf (\<Squnion>\<^bsub>fin\<^esub>A) (\<Squnion>\<^bsub>fin\<^esub>B))" by(rule inf_sup_distrib2)
  6.2026 @@ -1456,7 +1855,7 @@
  6.2027    also have "\<dots> = \<Squnion>\<^bsub>fin\<^esub>({inf x b |b. b \<in> B} \<union> {inf a b |a b. a \<in> A \<and> b \<in> B})"
  6.2028      (is "_ = \<Squnion>\<^bsub>fin\<^esub>?M")
  6.2029      using B insert
  6.2030 -    by (simp add: Sup_fin_def fold1_Un2 [OF finB _ finAB ne])
  6.2031 +    by (simp add: Sup_fin.union [OF finB _ finAB ne])
  6.2032    also have "?M = {inf a b |a b. a \<in> insert x A \<and> b \<in> B}"
  6.2033      by blast
  6.2034    finally show ?case .
  6.2035 @@ -1471,227 +1870,84 @@
  6.2036    assumes "finite A" and "A \<noteq> {}"
  6.2037    shows "\<Sqinter>\<^bsub>fin\<^esub>A = Inf A"
  6.2038  proof -
  6.2039 -  interpret ab_semigroup_idem_mult inf
  6.2040 -    by (rule ab_semigroup_idem_mult_inf)
  6.2041 -  from `A \<noteq> {}` obtain b B where "A = {b} \<union> B" by auto
  6.2042 -  moreover with `finite A` have "finite B" by simp
  6.2043 -  ultimately show ?thesis
  6.2044 -    by (simp add: Inf_fin_def fold1_eq_fold_idem inf_Inf_fold_inf [symmetric])
  6.2045 +  from assms obtain b B where "A = insert b B" and "finite B" by auto
  6.2046 +  then show ?thesis
  6.2047 +    by (simp add: Inf_fin.eq_fold inf_Inf_fold_inf inf.commute [of b])
  6.2048  qed
  6.2049  
  6.2050  lemma Sup_fin_Sup:
  6.2051    assumes "finite A" and "A \<noteq> {}"
  6.2052    shows "\<Squnion>\<^bsub>fin\<^esub>A = Sup A"
  6.2053  proof -
  6.2054 -  interpret ab_semigroup_idem_mult sup
  6.2055 -    by (rule ab_semigroup_idem_mult_sup)
  6.2056 -  from `A \<noteq> {}` obtain b B where "A = {b} \<union> B" by auto
  6.2057 -  moreover with `finite A` have "finite B" by simp
  6.2058 -  ultimately show ?thesis  
  6.2059 -  by (simp add: Sup_fin_def fold1_eq_fold_idem sup_Sup_fold_sup [symmetric])
  6.2060 +  from assms obtain b B where "A = insert b B" and "finite B" by auto
  6.2061 +  then show ?thesis
  6.2062 +    by (simp add: Sup_fin.eq_fold sup_Sup_fold_sup sup.commute [of b])
  6.2063  qed
  6.2064  
  6.2065  end
  6.2066  
  6.2067  
  6.2068 -subsection {* Versions of @{const min} and @{const max} on non-empty sets *}
  6.2069 -
  6.2070 -definition (in linorder) Min :: "'a set \<Rightarrow> 'a" where
  6.2071 -  "Min = fold1 min"
  6.2072 +subsection {* Minimum and Maximum over non-empty sets *}
  6.2073  
  6.2074 -definition (in linorder) Max :: "'a set \<Rightarrow> 'a" where
  6.2075 -  "Max = fold1 max"
  6.2076 -
  6.2077 -sublocale linorder < Min!: semilattice_big min Min proof
  6.2078 -qed (simp add: Min_def)
  6.2079 -
  6.2080 -sublocale linorder < Max!: semilattice_big max Max proof
  6.2081 -qed (simp add: Max_def)
  6.2082 +text {*
  6.2083 +  This case is already setup by the @{text min_max} sublocale dependency from above.  But note
  6.2084 +  that this yields irregular prefixes, e.g.~@{text min_max.Inf_fin.insert} instead
  6.2085 +  of @{text Max.insert}.
  6.2086 +*}
  6.2087  
  6.2088  context linorder
  6.2089  begin
  6.2090  
  6.2091 -lemmas Min_singleton = Min.singleton
  6.2092 -lemmas Max_singleton = Max.singleton
  6.2093 -
  6.2094 -lemma Min_insert:
  6.2095 -  assumes "finite A" and "A \<noteq> {}"
  6.2096 -  shows "Min (insert x A) = min x (Min A)"
  6.2097 -  using assms by simp
  6.2098 -
  6.2099 -lemma Max_insert:
  6.2100 -  assumes "finite A" and "A \<noteq> {}"
  6.2101 -  shows "Max (insert x A) = max x (Max A)"
  6.2102 -  using assms by simp
  6.2103 -
  6.2104 -lemma Min_Un:
  6.2105 -  assumes "finite A" and "A \<noteq> {}" and "finite B" and "B \<noteq> {}"
  6.2106 -  shows "Min (A \<union> B) = min (Min A) (Min B)"
  6.2107 -  using assms by (rule Min.union_idem)
  6.2108 -
  6.2109 -lemma Max_Un:
  6.2110 -  assumes "finite A" and "A \<noteq> {}" and "finite B" and "B \<noteq> {}"
  6.2111 -  shows "Max (A \<union> B) = max (Max A) (Max B)"
  6.2112 -  using assms by (rule Max.union_idem)
  6.2113 -
  6.2114 -lemma hom_Min_commute:
  6.2115 -  assumes "\<And>x y. h (min x y) = min (h x) (h y)"
  6.2116 -    and "finite N" and "N \<noteq> {}"
  6.2117 -  shows "h (Min N) = Min (h ` N)"
  6.2118 -  using assms by (rule Min.hom_commute)
  6.2119 -
  6.2120 -lemma hom_Max_commute:
  6.2121 -  assumes "\<And>x y. h (max x y) = max (h x) (h y)"
  6.2122 -    and "finite N" and "N \<noteq> {}"
  6.2123 -  shows "h (Max N) = Max (h ` N)"
  6.2124 -  using assms by (rule Max.hom_commute)
  6.2125 -
  6.2126 -lemma ab_semigroup_idem_mult_min:
  6.2127 -  "class.ab_semigroup_idem_mult min"
  6.2128 -  proof qed (auto simp add: min_def)
  6.2129 -
  6.2130 -lemma ab_semigroup_idem_mult_max:
  6.2131 -  "class.ab_semigroup_idem_mult max"
  6.2132 -  proof qed (auto simp add: max_def)
  6.2133 -
  6.2134 -lemma max_lattice:
  6.2135 -  "class.semilattice_inf max (op \<ge>) (op >)"
  6.2136 -  by (fact min_max.dual_semilattice)
  6.2137 -
  6.2138 -lemma dual_max:
  6.2139 -  "ord.max (op \<ge>) = min"
  6.2140 -  by (auto simp add: ord.max_def min_def fun_eq_iff)
  6.2141 -
  6.2142  lemma dual_min:
  6.2143 -  "ord.min (op \<ge>) = max"
  6.2144 +  "ord.min greater_eq = max"
  6.2145    by (auto simp add: ord.min_def max_def fun_eq_iff)
  6.2146  
  6.2147 -lemma strict_below_fold1_iff:
  6.2148 -  assumes "finite A" and "A \<noteq> {}"
  6.2149 -  shows "x < fold1 min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
  6.2150 +lemma dual_max:
  6.2151 +  "ord.max greater_eq = min"
  6.2152 +  by (auto simp add: ord.max_def min_def fun_eq_iff)
  6.2153 +
  6.2154 +lemma dual_Min:
  6.2155 +  "linorder.Min greater_eq = Max"
  6.2156  proof -
  6.2157 -  interpret ab_semigroup_idem_mult min
  6.2158 -    by (rule ab_semigroup_idem_mult_min)
  6.2159 -  from assms show ?thesis
  6.2160 -  by (induct rule: finite_ne_induct)
  6.2161 -    (simp_all add: fold1_insert)
  6.2162 -qed
  6.2163 -
  6.2164 -lemma fold1_below_iff:
  6.2165 -  assumes "finite A" and "A \<noteq> {}"
  6.2166 -  shows "fold1 min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
  6.2167 -proof -
  6.2168 -  interpret ab_semigroup_idem_mult min
  6.2169 -    by (rule ab_semigroup_idem_mult_min)
  6.2170 -  from assms show ?thesis
  6.2171 -  by (induct rule: finite_ne_induct)
  6.2172 -    (simp_all add: fold1_insert min_le_iff_disj)
  6.2173 +  interpret dual!: linorder greater_eq greater by (fact dual_linorder)
  6.2174 +  show ?thesis by (simp add: dual.Min_def dual_min Max_def)
  6.2175  qed
  6.2176  
  6.2177 -lemma fold1_strict_below_iff:
  6.2178 -  assumes "finite A" and "A \<noteq> {}"
  6.2179 -  shows "fold1 min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
  6.2180 +lemma dual_Max:
  6.2181 +  "linorder.Max greater_eq = Min"
  6.2182  proof -
  6.2183 -  interpret ab_semigroup_idem_mult min
  6.2184 -    by (rule ab_semigroup_idem_mult_min)
  6.2185 -  from assms show ?thesis
  6.2186 -  by (induct rule: finite_ne_induct)
  6.2187 -    (simp_all add: fold1_insert min_less_iff_disj)
  6.2188 +  interpret dual!: linorder greater_eq greater by (fact dual_linorder)
  6.2189 +  show ?thesis by (simp add: dual.Max_def dual_max Min_def)
  6.2190  qed
  6.2191  
  6.2192 -lemma fold1_antimono:
  6.2193 -  assumes "A \<noteq> {}" and "A \<subseteq> B" and "finite B"
  6.2194 -  shows "fold1 min B \<le> fold1 min A"
  6.2195 -proof cases
  6.2196 -  assume "A = B" thus ?thesis by simp
  6.2197 -next
  6.2198 -  interpret ab_semigroup_idem_mult min
  6.2199 -    by (rule ab_semigroup_idem_mult_min)
  6.2200 -  assume neq: "A \<noteq> B"
  6.2201 -  have B: "B = A \<union> (B-A)" using `A \<subseteq> B` by blast
  6.2202 -  have "fold1 min B = fold1 min (A \<union> (B-A))" by(subst B)(rule refl)
  6.2203 -  also have "\<dots> = min (fold1 min A) (fold1 min (B-A))"
  6.2204 -  proof -
  6.2205 -    have "finite A" by(rule finite_subset[OF `A \<subseteq> B` `finite B`])
  6.2206 -    moreover have "finite(B-A)" by(rule finite_Diff[OF `finite B`])
  6.2207 -    moreover have "(B-A) \<noteq> {}" using assms neq by blast
  6.2208 -    moreover have "A Int (B-A) = {}" using assms by blast
  6.2209 -    ultimately show ?thesis using `A \<noteq> {}` by (rule_tac fold1_Un)
  6.2210 -  qed
  6.2211 -  also have "\<dots> \<le> fold1 min A" by (simp add: min_le_iff_disj)
  6.2212 -  finally show ?thesis .
  6.2213 -qed
  6.2214 +lemmas Min_singleton = min_max.Inf_fin.singleton
  6.2215 +lemmas Max_singleton = min_max.Sup_fin.singleton
  6.2216 +lemmas Min_insert = min_max.Inf_fin.insert
  6.2217 +lemmas Max_insert = min_max.Sup_fin.insert
  6.2218 +lemmas Min_Un = min_max.Inf_fin.union
  6.2219 +lemmas Max_Un = min_max.Sup_fin.union
  6.2220 +lemmas hom_Min_commute = min_max.Inf_fin.hom_commute
  6.2221 +lemmas hom_Max_commute = min_max.Sup_fin.hom_commute
  6.2222  
  6.2223  lemma Min_in [simp]:
  6.2224    assumes "finite A" and "A \<noteq> {}"
  6.2225    shows "Min A \<in> A"
  6.2226 -proof -
  6.2227 -  interpret ab_semigroup_idem_mult min
  6.2228 -    by (rule ab_semigroup_idem_mult_min)
  6.2229 -  from assms fold1_in show ?thesis by (fastforce simp: Min_def min_def)
  6.2230 -qed
  6.2231 +  using assms by (auto simp add: min_def min_max.Inf_fin.closed)
  6.2232  
  6.2233  lemma Max_in [simp]:
  6.2234    assumes "finite A" and "A \<noteq> {}"
  6.2235    shows "Max A \<in> A"
  6.2236 -proof -
  6.2237 -  interpret ab_semigroup_idem_mult max
  6.2238 -    by (rule ab_semigroup_idem_mult_max)
  6.2239 -  from assms fold1_in [of A] show ?thesis by (fastforce simp: Max_def max_def)
  6.2240 -qed
  6.2241 +  using assms by (auto simp add: max_def min_max.Sup_fin.closed)
  6.2242  
  6.2243  lemma Min_le [simp]:
  6.2244    assumes "finite A" and "x \<in> A"
  6.2245    shows "Min A \<le> x"
  6.2246 -  using assms by (simp add: Min_def min_max.fold1_belowI)
  6.2247 +  using assms by (fact min_max.Inf_fin.coboundedI)
  6.2248  
  6.2249  lemma Max_ge [simp]:
  6.2250    assumes "finite A" and "x \<in> A"
  6.2251    shows "x \<le> Max A"
  6.2252 -  by (simp add: Max_def semilattice_inf.fold1_belowI [OF max_lattice] assms)
  6.2253 -
  6.2254 -lemma Min_ge_iff [simp, no_atp]:
  6.2255 -  assumes "finite A" and "A \<noteq> {}"
  6.2256 -  shows "x \<le> Min A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
  6.2257 -  using assms by (simp add: Min_def min_max.below_fold1_iff)
  6.2258 -
  6.2259 -lemma Max_le_iff [simp, no_atp]:
  6.2260 -  assumes "finite A" and "A \<noteq> {}"
  6.2261 -  shows "Max A \<le> x \<longleftrightarrow> (\<forall>a\<in>A. a \<le> x)"
  6.2262 -  by (simp add: Max_def semilattice_inf.below_fold1_iff [OF max_lattice] assms)
  6.2263 -
  6.2264 -lemma Min_gr_iff [simp, no_atp]:
  6.2265 -  assumes "finite A" and "A \<noteq> {}"
  6.2266 -  shows "x < Min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
  6.2267 -  using assms by (simp add: Min_def strict_below_fold1_iff)
  6.2268 -
  6.2269 -lemma Max_less_iff [simp, no_atp]:
  6.2270 -  assumes "finite A" and "A \<noteq> {}"
  6.2271 -  shows "Max A < x \<longleftrightarrow> (\<forall>a\<in>A. a < x)"
  6.2272 -  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
  6.2273 -    linorder.strict_below_fold1_iff [OF dual_linorder] assms)
  6.2274 -
  6.2275 -lemma Min_le_iff [no_atp]:
  6.2276 -  assumes "finite A" and "A \<noteq> {}"
  6.2277 -  shows "Min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
  6.2278 -  using assms by (simp add: Min_def fold1_below_iff)
  6.2279 -
  6.2280 -lemma Max_ge_iff [no_atp]:
  6.2281 -  assumes "finite A" and "A \<noteq> {}"
  6.2282 -  shows "x \<le> Max A \<longleftrightarrow> (\<exists>a\<in>A. x \<le> a)"
  6.2283 -  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
  6.2284 -    linorder.fold1_below_iff [OF dual_linorder] assms)
  6.2285 -
  6.2286 -lemma Min_less_iff [no_atp]:
  6.2287 -  assumes "finite A" and "A \<noteq> {}"
  6.2288 -  shows "Min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
  6.2289 -  using assms by (simp add: Min_def fold1_strict_below_iff)
  6.2290 -
  6.2291 -lemma Max_gr_iff [no_atp]:
  6.2292 -  assumes "finite A" and "A \<noteq> {}"
  6.2293 -  shows "x < Max A \<longleftrightarrow> (\<exists>a\<in>A. x < a)"
  6.2294 -  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
  6.2295 -    linorder.fold1_strict_below_iff [OF dual_linorder] assms)
  6.2296 +  using assms by (fact min_max.Sup_fin.coboundedI)
  6.2297  
  6.2298  lemma Min_eqI:
  6.2299    assumes "finite A"
  6.2300 @@ -1717,22 +1973,91 @@
  6.2301    from assms show "x \<le> Max A" by simp
  6.2302  qed
  6.2303  
  6.2304 +lemma Min_ge_iff [simp, no_atp]:
  6.2305 +  assumes "finite A" and "A \<noteq> {}"
  6.2306 +  shows "x \<le> Min A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
  6.2307 +  using assms by (fact min_max.Inf_fin.bounded_iff)
  6.2308 +
  6.2309 +lemma Max_le_iff [simp, no_atp]:
  6.2310 +  assumes "finite A" and "A \<noteq> {}"
  6.2311 +  shows "Max A \<le> x \<longleftrightarrow> (\<forall>a\<in>A. a \<le> x)"
  6.2312 +  using assms by (fact min_max.Sup_fin.bounded_iff)
  6.2313 +
  6.2314 +lemma Min_gr_iff [simp, no_atp]:
  6.2315 +  assumes "finite A" and "A \<noteq> {}"
  6.2316 +  shows "x < Min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
  6.2317 +  using assms by (induct rule: finite_ne_induct) simp_all
  6.2318 +
  6.2319 +lemma Max_less_iff [simp, no_atp]:
  6.2320 +  assumes "finite A" and "A \<noteq> {}"
  6.2321 +  shows "Max A < x \<longleftrightarrow> (\<forall>a\<in>A. a < x)"
  6.2322 +  using assms by (induct rule: finite_ne_induct) simp_all
  6.2323 +
  6.2324 +lemma Min_le_iff [no_atp]:
  6.2325 +  assumes "finite A" and "A \<noteq> {}"
  6.2326 +  shows "Min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
  6.2327 +  using assms by (induct rule: finite_ne_induct) (simp_all add: min_le_iff_disj)
  6.2328 +
  6.2329 +lemma Max_ge_iff [no_atp]:
  6.2330 +  assumes "finite A" and "A \<noteq> {}"
  6.2331 +  shows "x \<le> Max A \<longleftrightarrow> (\<exists>a\<in>A. x \<le> a)"
  6.2332 +  using assms by (induct rule: finite_ne_induct) (simp_all add: le_max_iff_disj)
  6.2333 +
  6.2334 +lemma Min_less_iff [no_atp]:
  6.2335 +  assumes "finite A" and "A \<noteq> {}"
  6.2336 +  shows "Min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
  6.2337 +  using assms by (induct rule: finite_ne_induct) (simp_all add: min_less_iff_disj)
  6.2338 +
  6.2339 +lemma Max_gr_iff [no_atp]:
  6.2340 +  assumes "finite A" and "A \<noteq> {}"
  6.2341 +  shows "x < Max A \<longleftrightarrow> (\<exists>a\<in>A. x < a)"
  6.2342 +  using assms by (induct rule: finite_ne_induct) (simp_all add: less_max_iff_disj)
  6.2343 +
  6.2344  lemma Min_antimono:
  6.2345    assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
  6.2346    shows "Min N \<le> Min M"
  6.2347 -  using assms by (simp add: Min_def fold1_antimono)
  6.2348 +  using assms by (fact min_max.Inf_fin.antimono)
  6.2349  
  6.2350  lemma Max_mono:
  6.2351    assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
  6.2352    shows "Max M \<le> Max N"
  6.2353 -  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
  6.2354 -    linorder.fold1_antimono [OF dual_linorder] assms)
  6.2355 +  using assms by (fact min_max.Sup_fin.antimono)
  6.2356 +
  6.2357 +lemma mono_Min_commute:
  6.2358 +  assumes "mono f"
  6.2359 +  assumes "finite A" and "A \<noteq> {}"
  6.2360 +  shows "f (Min A) = Min (f ` A)"
  6.2361 +proof (rule linorder_class.Min_eqI [symmetric])
  6.2362 +  from `finite A` show "finite (f ` A)" by simp
  6.2363 +  from assms show "f (Min A) \<in> f ` A" by simp
  6.2364 +  fix x
  6.2365 +  assume "x \<in> f ` A"
  6.2366 +  then obtain y where "y \<in> A" and "x = f y" ..
  6.2367 +  with assms have "Min A \<le> y" by auto
  6.2368 +  with `mono f` have "f (Min A) \<le> f y" by (rule monoE)
  6.2369 +  with `x = f y` show "f (Min A) \<le> x" by simp
  6.2370 +qed
  6.2371  
  6.2372 -lemma finite_linorder_max_induct[consumes 1, case_names empty insert]:
  6.2373 - assumes fin: "finite A"
  6.2374 - and   empty: "P {}" 
  6.2375 - and  insert: "(!!b A. finite A \<Longrightarrow> ALL a:A. a < b \<Longrightarrow> P A \<Longrightarrow> P(insert b A))"
  6.2376 - shows "P A"
  6.2377 +lemma mono_Max_commute:
  6.2378 +  assumes "mono f"
  6.2379 +  assumes "finite A" and "A \<noteq> {}"
  6.2380 +  shows "f (Max A) = Max (f ` A)"
  6.2381 +proof (rule linorder_class.Max_eqI [symmetric])
  6.2382 +  from `finite A` show "finite (f ` A)" by simp
  6.2383 +  from assms show "f (Max A) \<in> f ` A" by simp
  6.2384 +  fix x
  6.2385 +  assume "x \<in> f ` A"
  6.2386 +  then obtain y where "y \<in> A" and "x = f y" ..
  6.2387 +  with assms have "y \<le> Max A" by auto
  6.2388 +  with `mono f` have "f y \<le> f (Max A)" by (rule monoE)
  6.2389 +  with `x = f y` show "x \<le> f (Max A)" by simp
  6.2390 +qed
  6.2391 +
  6.2392 +lemma finite_linorder_max_induct [consumes 1, case_names empty insert]:
  6.2393 +  assumes fin: "finite A"
  6.2394 +  and empty: "P {}" 
  6.2395 +  and insert: "\<And>b A. finite A \<Longrightarrow> \<forall>a\<in>A. a < b \<Longrightarrow> P A \<Longrightarrow> P (insert b A)"
  6.2396 +  shows "P A"
  6.2397  using fin empty insert
  6.2398  proof (induct rule: finite_psubset_induct)
  6.2399    case (psubset A)
  6.2400 @@ -1751,16 +2076,16 @@
  6.2401      assume "A \<noteq> {}"
  6.2402      with `finite A` have "Max A : A" by auto
  6.2403      then have A: "?A = A" using insert_Diff_single insert_absorb by auto
  6.2404 -    then have "P ?B" using `P {}` step IH[of ?B] by blast
  6.2405 +    then have "P ?B" using `P {}` step IH [of ?B] by blast
  6.2406      moreover 
  6.2407      have "\<forall>a\<in>?B. a < Max A" using Max_ge [OF `finite A`] by fastforce
  6.2408 -    ultimately show "P A" using A insert_Diff_single step[OF `finite ?B`] by fastforce
  6.2409 +    ultimately show "P A" using A insert_Diff_single step [OF `finite ?B`] by fastforce
  6.2410    qed
  6.2411  qed
  6.2412  
  6.2413 -lemma finite_linorder_min_induct[consumes 1, case_names empty insert]:
  6.2414 - "\<lbrakk>finite A; P {}; \<And>b A. \<lbrakk>finite A; \<forall>a\<in>A. b < a; P A\<rbrakk> \<Longrightarrow> P (insert b A)\<rbrakk> \<Longrightarrow> P A"
  6.2415 -by(rule linorder.finite_linorder_max_induct[OF dual_linorder])
  6.2416 +lemma finite_linorder_min_induct [consumes 1, case_names empty insert]:
  6.2417 +  "\<lbrakk>finite A; P {}; \<And>b A. \<lbrakk>finite A; \<forall>a\<in>A. b < a; P A\<rbrakk> \<Longrightarrow> P (insert b A)\<rbrakk> \<Longrightarrow> P A"
  6.2418 +  by (rule linorder.finite_linorder_max_induct [OF dual_linorder])
  6.2419  
  6.2420  end
  6.2421  
  6.2422 @@ -1799,29 +2124,14 @@
  6.2423  begin
  6.2424  
  6.2425  lemma minus_Max_eq_Min [simp]:
  6.2426 -  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - (Max S) = Min (uminus ` S)"
  6.2427 +  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - Max S = Min (uminus ` S)"
  6.2428    by (induct S rule: finite_ne_induct) (simp_all add: minus_max_eq_min)
  6.2429  
  6.2430  lemma minus_Min_eq_Max [simp]:
  6.2431 -  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - (Min S) = Max (uminus ` S)"
  6.2432 +  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - Min S = Max (uminus ` S)"
  6.2433    by (induct S rule: finite_ne_induct) (simp_all add: minus_min_eq_max)
  6.2434  
  6.2435  end
  6.2436  
  6.2437 -lemma (in linorder) mono_Max_commute:
  6.2438 -  assumes "mono f"
  6.2439 -  assumes "finite A" and "A \<noteq> {}"
  6.2440 -  shows "f (Max A) = Max (f ` A)"
  6.2441 -proof (rule linorder_class.Max_eqI [symmetric])
  6.2442 -  from `finite A` show "finite (f ` A)" by simp
  6.2443 -  from assms show "f (Max A) \<in> f ` A" by simp
  6.2444 -  fix x
  6.2445 -  assume "x \<in> f ` A"
  6.2446 -  then obtain y where "y \<in> A" and "x = f y" ..
  6.2447 -  with assms have "y \<le> Max A" by auto
  6.2448 -  with `mono f` have "f y \<le> f (Max A)" by (rule monoE)
  6.2449 -  with `x = f y` show "x \<le> f (Max A)" by simp
  6.2450 -qed (* FIXME augment also dual rule mono_Min_commute *)
  6.2451 -
  6.2452  end
  6.2453  
     7.1 --- a/src/HOL/Complete_Lattices.thy	Sat Mar 23 17:11:06 2013 +0100
     7.2 +++ b/src/HOL/Complete_Lattices.thy	Sat Mar 23 20:50:39 2013 +0100
     7.3 @@ -514,10 +514,10 @@
     7.4    by (rule class.complete_linorder.intro, rule dual_complete_lattice, rule dual_linorder)
     7.5  
     7.6  lemma complete_linorder_inf_min: "inf = min"
     7.7 -  by (rule ext)+ (auto intro: antisym)
     7.8 +  by (rule ext)+ (auto intro: antisym simp add: min_def)
     7.9  
    7.10  lemma complete_linorder_sup_max: "sup = max"
    7.11 -  by (rule ext)+ (auto intro: antisym)
    7.12 +  by (rule ext)+ (auto intro: antisym simp add: max_def)
    7.13  
    7.14  lemma Inf_less_iff:
    7.15    "\<Sqinter>S \<sqsubset> a \<longleftrightarrow> (\<exists>x\<in>S. x \<sqsubset> a)"
     8.1 --- a/src/HOL/Finite_Set.thy	Sat Mar 23 17:11:06 2013 +0100
     8.2 +++ b/src/HOL/Finite_Set.thy	Sat Mar 23 20:50:39 2013 +0100
     8.3 @@ -564,9 +564,13 @@
     8.4    assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
     8.5  begin
     8.6  
     8.7 -lemma fun_left_comm: "f x (f y z) = f y (f x z)"
     8.8 +lemma fun_left_comm: "f y (f x z) = f x (f y z)"
     8.9    using comp_fun_commute by (simp add: fun_eq_iff)
    8.10  
    8.11 +lemma commute_left_comp:
    8.12 +  "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
    8.13 +  by (simp add: o_assoc comp_fun_commute)
    8.14 +
    8.15  end
    8.16  
    8.17  inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
    8.18 @@ -578,7 +582,7 @@
    8.19  inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
    8.20  
    8.21  definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
    8.22 -  "fold f z A = (THE y. fold_graph f z A y)"
    8.23 +  "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
    8.24  
    8.25  text{*A tempting alternative for the definiens is
    8.26  @{term "if finite A then THE y. fold_graph f z A y else e"}.
    8.27 @@ -595,6 +599,11 @@
    8.28  context comp_fun_commute
    8.29  begin
    8.30  
    8.31 +lemma fold_graph_finite:
    8.32 +  assumes "fold_graph f z A y"
    8.33 +  shows "finite A"
    8.34 +  using assms by induct simp_all
    8.35 +
    8.36  lemma fold_graph_insertE_aux:
    8.37    "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'"
    8.38  proof (induct set: fold_graph)
    8.39 @@ -632,7 +641,7 @@
    8.40  
    8.41  lemma fold_equality:
    8.42    "fold_graph f z A y \<Longrightarrow> fold f z A = y"
    8.43 -by (unfold fold_def) (blast intro: fold_graph_determ)
    8.44 +  by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
    8.45  
    8.46  lemma fold_graph_fold:
    8.47    assumes "finite A"
    8.48 @@ -642,13 +651,19 @@
    8.49    moreover note fold_graph_determ
    8.50    ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
    8.51    then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
    8.52 -  then show ?thesis by (unfold fold_def)
    8.53 +  with assms show ?thesis by (simp add: fold_def)
    8.54  qed
    8.55  
    8.56 -text{* The base case for @{text fold}: *}
    8.57 +text {* The base case for @{text fold}: *}
    8.58  
    8.59 -lemma (in -) fold_empty [simp]: "fold f z {} = z"
    8.60 -by (unfold fold_def) blast
    8.61 +lemma (in -) fold_infinite [simp]:
    8.62 +  assumes "\<not> finite A"
    8.63 +  shows "fold f z A = z"
    8.64 +  using assms by (auto simp add: fold_def)
    8.65 +
    8.66 +lemma (in -) fold_empty [simp]:
    8.67 +  "fold f z {} = z"
    8.68 +  by (auto simp add: fold_def)
    8.69  
    8.70  text{* The various recursion equations for @{const fold}: *}
    8.71  
    8.72 @@ -656,22 +671,27 @@
    8.73    assumes "finite A" and "x \<notin> A"
    8.74    shows "fold f z (insert x A) = f x (fold f z A)"
    8.75  proof (rule fold_equality)
    8.76 +  fix z
    8.77    from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
    8.78 -  with `x \<notin> A`show "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
    8.79 +  with `x \<notin> A` have "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
    8.80 +  then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
    8.81  qed
    8.82  
    8.83 -lemma fold_fun_comm:
    8.84 +declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
    8.85 +  -- {* No more proofs involve these. *}
    8.86 +
    8.87 +lemma fold_fun_left_comm:
    8.88    "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
    8.89  proof (induct rule: finite_induct)
    8.90    case empty then show ?case by simp
    8.91  next
    8.92    case (insert y A) then show ?case
    8.93 -    by (simp add: fun_left_comm[of x])
    8.94 +    by (simp add: fun_left_comm [of x])
    8.95  qed
    8.96  
    8.97  lemma fold_insert2:
    8.98 -  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
    8.99 -by (simp add: fold_fun_comm)
   8.100 +  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
   8.101 +  by (simp add: fold_fun_left_comm)
   8.102  
   8.103  lemma fold_rec:
   8.104    assumes "finite A" and "x \<in> A"
   8.105 @@ -699,23 +719,23 @@
   8.106  
   8.107  lemma fold_image:
   8.108    assumes "finite A" and "inj_on g A"
   8.109 -  shows "fold f x (g ` A) = fold (f \<circ> g) x A"
   8.110 +  shows "fold f z (g ` A) = fold (f \<circ> g) z A"
   8.111  using assms
   8.112  proof induction
   8.113    case (insert a F)
   8.114      interpret comp_fun_commute "\<lambda>x. f (g x)" by default (simp add: comp_fun_commute)
   8.115      from insert show ?case by auto
   8.116 -qed (simp)
   8.117 +qed simp
   8.118  
   8.119  end
   8.120  
   8.121  lemma fold_cong:
   8.122    assumes "comp_fun_commute f" "comp_fun_commute g"
   8.123    assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
   8.124 -    and "A = B" and "s = t"
   8.125 -  shows "Finite_Set.fold f s A = Finite_Set.fold g t B"
   8.126 +    and "s = t" and "A = B"
   8.127 +  shows "fold f s A = fold g t B"
   8.128  proof -
   8.129 -  have "Finite_Set.fold f s A = Finite_Set.fold g s A"  
   8.130 +  have "fold f s A = fold g s A"  
   8.131    using `finite A` cong proof (induct A)
   8.132      case empty then show ?case by simp
   8.133    next
   8.134 @@ -728,10 +748,10 @@
   8.135  qed
   8.136  
   8.137  
   8.138 -text{* A simplified version for idempotent functions: *}
   8.139 +text {* A simplified version for idempotent functions: *}
   8.140  
   8.141  locale comp_fun_idem = comp_fun_commute +
   8.142 -  assumes comp_fun_idem: "f x o f x = f x"
   8.143 +  assumes comp_fun_idem: "f x \<circ> f x = f x"
   8.144  begin
   8.145  
   8.146  lemma fun_left_idem: "f x (f x z) = f x z"
   8.147 @@ -739,20 +759,20 @@
   8.148  
   8.149  lemma fold_insert_idem:
   8.150    assumes fin: "finite A"
   8.151 -  shows "fold f z (insert x A) = f x (fold f z A)"
   8.152 +  shows "fold f z (insert x A)  = f x (fold f z A)"
   8.153  proof cases
   8.154    assume "x \<in> A"
   8.155    then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
   8.156 -  then show ?thesis using assms by (simp add:fun_left_idem)
   8.157 +  then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
   8.158  next
   8.159    assume "x \<notin> A" then show ?thesis using assms by simp
   8.160  qed
   8.161  
   8.162 -declare fold_insert[simp del] fold_insert_idem[simp]
   8.163 +declare fold_insert [simp del] fold_insert_idem [simp]
   8.164  
   8.165  lemma fold_insert_idem2:
   8.166    "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
   8.167 -by(simp add:fold_fun_comm)
   8.168 +  by (simp add: fold_fun_left_comm)
   8.169  
   8.170  end
   8.171  
   8.172 @@ -810,6 +830,11 @@
   8.173  
   8.174  subsubsection {* Expressing set operations via @{const fold} *}
   8.175  
   8.176 +lemma comp_fun_commute_const:
   8.177 +  "comp_fun_commute (\<lambda>_. f)"
   8.178 +proof
   8.179 +qed rule
   8.180 +
   8.181  lemma comp_fun_idem_insert:
   8.182    "comp_fun_idem insert"
   8.183  proof
   8.184 @@ -847,7 +872,8 @@
   8.185    then show ?thesis ..
   8.186  qed
   8.187  
   8.188 -lemma comp_fun_commute_filter_fold: "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
   8.189 +lemma comp_fun_commute_filter_fold:
   8.190 +  "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
   8.191  proof - 
   8.192    interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
   8.193    show ?thesis by default (auto simp: fun_eq_iff)
   8.194 @@ -916,13 +942,13 @@
   8.195  
   8.196  lemma comp_fun_commute_product_fold: 
   8.197    assumes "finite B"
   8.198 -  shows "comp_fun_commute (\<lambda>x A. fold (\<lambda>y. Set.insert (x, y)) A B)" 
   8.199 +  shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
   8.200  by default (auto simp: fold_union_pair[symmetric] assms)
   8.201  
   8.202  lemma product_fold:
   8.203    assumes "finite A"
   8.204    assumes "finite B"
   8.205 -  shows "A \<times> B = fold (\<lambda>x A. fold (\<lambda>y. Set.insert (x, y)) A B) {} A"
   8.206 +  shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
   8.207  using assms unfolding Sigma_def 
   8.208  by (induct A) 
   8.209    (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
   8.210 @@ -933,20 +959,20 @@
   8.211  
   8.212  lemma inf_Inf_fold_inf:
   8.213    assumes "finite A"
   8.214 -  shows "inf B (Inf A) = fold inf B A"
   8.215 +  shows "inf (Inf A) B = fold inf B A"
   8.216  proof -
   8.217    interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
   8.218 -  from `finite A` show ?thesis by (induct A arbitrary: B)
   8.219 -    (simp_all add: inf_commute fold_fun_comm)
   8.220 +  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
   8.221 +    (simp_all add: inf_commute fun_eq_iff)
   8.222  qed
   8.223  
   8.224  lemma sup_Sup_fold_sup:
   8.225    assumes "finite A"
   8.226 -  shows "sup B (Sup A) = fold sup B A"
   8.227 +  shows "sup (Sup A) B = fold sup B A"
   8.228  proof -
   8.229    interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
   8.230 -  from `finite A` show ?thesis by (induct A arbitrary: B)
   8.231 -    (simp_all add: sup_commute fold_fun_comm)
   8.232 +  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
   8.233 +    (simp_all add: sup_commute fun_eq_iff)
   8.234  qed
   8.235  
   8.236  lemma Inf_fold_inf:
   8.237 @@ -994,503 +1020,42 @@
   8.238  end
   8.239  
   8.240  
   8.241 -subsection {* The derived combinator @{text fold_image} *}
   8.242 -
   8.243 -definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
   8.244 -  where "fold_image f g = fold (\<lambda>x y. f (g x) y)"
   8.245 -
   8.246 -lemma fold_image_empty[simp]: "fold_image f g z {} = z"
   8.247 -  by (simp add:fold_image_def)
   8.248 -
   8.249 -context ab_semigroup_mult
   8.250 -begin
   8.251 -
   8.252 -lemma fold_image_insert[simp]:
   8.253 -  assumes "finite A" and "a \<notin> A"
   8.254 -  shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
   8.255 -proof -
   8.256 -  interpret comp_fun_commute "%x y. (g x) * y"
   8.257 -    by default (simp add: fun_eq_iff mult_ac)
   8.258 -  from assms show ?thesis by (simp add: fold_image_def)
   8.259 -qed
   8.260 -
   8.261 -lemma fold_image_reindex:
   8.262 -  assumes "finite A"
   8.263 -  shows "inj_on h A \<Longrightarrow> fold_image times g z (h ` A) = fold_image times (g \<circ> h) z A"
   8.264 -  using assms by induct auto
   8.265 -
   8.266 -lemma fold_image_cong:
   8.267 -  assumes "finite A" and g_h: "\<And>x. x\<in>A \<Longrightarrow> g x = h x"
   8.268 -  shows "fold_image times g z A = fold_image times h z A"
   8.269 -proof -
   8.270 -  from `finite A`
   8.271 -  have "\<And>C. C <= A --> (ALL x:C. g x = h x) --> fold_image times g z C = fold_image times h z C"
   8.272 -  proof (induct arbitrary: C)
   8.273 -    case empty then show ?case by simp
   8.274 -  next
   8.275 -    case (insert x F) then show ?case apply -
   8.276 -    apply (simp add: subset_insert_iff, clarify)
   8.277 -    apply (subgoal_tac "finite C")
   8.278 -      prefer 2 apply (blast dest: finite_subset [rotated])
   8.279 -    apply (subgoal_tac "C = insert x (C - {x})")
   8.280 -      prefer 2 apply blast
   8.281 -    apply (erule ssubst)
   8.282 -    apply (simp add: Ball_def del: insert_Diff_single)
   8.283 -    done
   8.284 -  qed
   8.285 -  with g_h show ?thesis by simp
   8.286 -qed
   8.287 -
   8.288 -end
   8.289 -
   8.290 -context comm_monoid_mult
   8.291 -begin
   8.292 -
   8.293 -lemma fold_image_1:
   8.294 -  "finite S \<Longrightarrow> (\<forall>x\<in>S. f x = 1) \<Longrightarrow> fold_image op * f 1 S = 1"
   8.295 -  apply (induct rule: finite_induct)
   8.296 -  apply simp by auto
   8.297 -
   8.298 -lemma fold_image_Un_Int:
   8.299 -  "finite A ==> finite B ==>
   8.300 -    fold_image times g 1 A * fold_image times g 1 B =
   8.301 -    fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
   8.302 -  apply (induct rule: finite_induct)
   8.303 -by (induct set: finite) 
   8.304 -   (auto simp add: mult_ac insert_absorb Int_insert_left)
   8.305 -
   8.306 -lemma fold_image_Un_one:
   8.307 -  assumes fS: "finite S" and fT: "finite T"
   8.308 -  and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
   8.309 -  shows "fold_image (op *) f 1 (S \<union> T) = fold_image (op *) f 1 S * fold_image (op *) f 1 T"
   8.310 -proof-
   8.311 -  have "fold_image op * f 1 (S \<inter> T) = 1" 
   8.312 -    apply (rule fold_image_1)
   8.313 -    using fS fT I0 by auto 
   8.314 -  with fold_image_Un_Int[OF fS fT] show ?thesis by simp
   8.315 -qed
   8.316 -
   8.317 -corollary fold_Un_disjoint:
   8.318 -  "finite A ==> finite B ==> A Int B = {} ==>
   8.319 -   fold_image times g 1 (A Un B) =
   8.320 -   fold_image times g 1 A * fold_image times g 1 B"
   8.321 -by (simp add: fold_image_Un_Int)
   8.322 -
   8.323 -lemma fold_image_UN_disjoint:
   8.324 -  "\<lbrakk> finite I; ALL i:I. finite (A i);
   8.325 -     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
   8.326 -   \<Longrightarrow> fold_image times g 1 (UNION I A) =
   8.327 -       fold_image times (%i. fold_image times g 1 (A i)) 1 I"
   8.328 -apply (induct rule: finite_induct)
   8.329 -apply simp
   8.330 -apply atomize
   8.331 -apply (subgoal_tac "ALL i:F. x \<noteq> i")
   8.332 - prefer 2 apply blast
   8.333 -apply (subgoal_tac "A x Int UNION F A = {}")
   8.334 - prefer 2 apply blast
   8.335 -apply (simp add: fold_Un_disjoint)
   8.336 -done
   8.337 -
   8.338 -lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
   8.339 -  fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
   8.340 -  fold_image times (split g) 1 (SIGMA x:A. B x)"
   8.341 -apply (subst Sigma_def)
   8.342 -apply (subst fold_image_UN_disjoint, assumption, simp)
   8.343 - apply blast
   8.344 -apply (erule fold_image_cong)
   8.345 -apply (subst fold_image_UN_disjoint, simp, simp)
   8.346 - apply blast
   8.347 -apply simp
   8.348 -done
   8.349 -
   8.350 -lemma fold_image_distrib: "finite A \<Longrightarrow>
   8.351 -   fold_image times (%x. g x * h x) 1 A =
   8.352 -   fold_image times g 1 A *  fold_image times h 1 A"
   8.353 -by (erule finite_induct) (simp_all add: mult_ac)
   8.354 -
   8.355 -lemma fold_image_related: 
   8.356 -  assumes Re: "R e e" 
   8.357 -  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
   8.358 -  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
   8.359 -  shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
   8.360 -  using fS by (rule finite_subset_induct) (insert assms, auto)
   8.361 -
   8.362 -lemma  fold_image_eq_general:
   8.363 -  assumes fS: "finite S"
   8.364 -  and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y" 
   8.365 -  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
   8.366 -  shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
   8.367 -proof-
   8.368 -  from h f12 have hS: "h ` S = S'" by auto
   8.369 -  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
   8.370 -    from f12 h H  have "x = y" by auto }
   8.371 -  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
   8.372 -  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
   8.373 -  from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
   8.374 -  also have "\<dots> = fold_image (op *) (f2 o h) e S" 
   8.375 -    using fold_image_reindex[OF fS hinj, of f2 e] .
   8.376 -  also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
   8.377 -    by blast
   8.378 -  finally show ?thesis ..
   8.379 -qed
   8.380 -
   8.381 -lemma fold_image_eq_general_inverses:
   8.382 -  assumes fS: "finite S" 
   8.383 -  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
   8.384 -  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
   8.385 -  shows "fold_image (op *) f e S = fold_image (op *) g e T"
   8.386 -  (* metis solves it, but not yet available here *)
   8.387 -  apply (rule fold_image_eq_general[OF fS, of T h g f e])
   8.388 -  apply (rule ballI)
   8.389 -  apply (frule kh)
   8.390 -  apply (rule ex1I[])
   8.391 -  apply blast
   8.392 -  apply clarsimp
   8.393 -  apply (drule hk) apply simp
   8.394 -  apply (rule sym)
   8.395 -  apply (erule conjunct1[OF conjunct2[OF hk]])
   8.396 -  apply (rule ballI)
   8.397 -  apply (drule  hk)
   8.398 -  apply blast
   8.399 -  done
   8.400 -
   8.401 -end
   8.402 -
   8.403 -
   8.404 -subsection {* A fold functional for non-empty sets *}
   8.405 -
   8.406 -text{* Does not require start value. *}
   8.407 -
   8.408 -inductive
   8.409 -  fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
   8.410 -  for f :: "'a => 'a => 'a"
   8.411 -where
   8.412 -  fold1Set_insertI [intro]:
   8.413 -   "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
   8.414 -
   8.415 -definition fold1 :: "('a => 'a => 'a) => 'a set => 'a" where
   8.416 -  "fold1 f A == THE x. fold1Set f A x"
   8.417 -
   8.418 -lemma fold1Set_nonempty:
   8.419 -  "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
   8.420 -by(erule fold1Set.cases, simp_all)
   8.421 -
   8.422 -inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
   8.423 -
   8.424 -inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
   8.425 -
   8.426 -
   8.427 -lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
   8.428 -by (blast elim: fold_graph.cases)
   8.429 -
   8.430 -lemma fold1_singleton [simp]: "fold1 f {a} = a"
   8.431 -by (unfold fold1_def) blast
   8.432 -
   8.433 -lemma finite_nonempty_imp_fold1Set:
   8.434 -  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
   8.435 -apply (induct A rule: finite_induct)
   8.436 -apply (auto dest: finite_imp_fold_graph [of _ f])
   8.437 -done
   8.438 -
   8.439 -text{*First, some lemmas about @{const fold_graph}.*}
   8.440 -
   8.441 -context ab_semigroup_mult
   8.442 -begin
   8.443 -
   8.444 -lemma comp_fun_commute: "comp_fun_commute (op *)"
   8.445 -  by default (simp add: fun_eq_iff mult_ac)
   8.446 -
   8.447 -lemma fold_graph_insert_swap:
   8.448 -assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
   8.449 -shows "fold_graph times z (insert b A) (z * y)"
   8.450 -proof -
   8.451 -  interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
   8.452 -from assms show ?thesis
   8.453 -proof (induct rule: fold_graph.induct)
   8.454 -  case emptyI show ?case by (subst mult_commute [of z b], fast)
   8.455 -next
   8.456 -  case (insertI x A y)
   8.457 -    have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
   8.458 -      using insertI by force  --{*how does @{term id} get unfolded?*}
   8.459 -    thus ?case by (simp add: insert_commute mult_ac)
   8.460 -qed
   8.461 -qed
   8.462 -
   8.463 -lemma fold_graph_permute_diff:
   8.464 -assumes fold: "fold_graph times b A x"
   8.465 -shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
   8.466 -using fold
   8.467 -proof (induct rule: fold_graph.induct)
   8.468 -  case emptyI thus ?case by simp
   8.469 -next
   8.470 -  case (insertI x A y)
   8.471 -  have "a = x \<or> a \<in> A" using insertI by simp
   8.472 -  thus ?case
   8.473 -  proof
   8.474 -    assume "a = x"
   8.475 -    with insertI show ?thesis
   8.476 -      by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
   8.477 -  next
   8.478 -    assume ainA: "a \<in> A"
   8.479 -    hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
   8.480 -      using insertI by force
   8.481 -    moreover
   8.482 -    have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
   8.483 -      using ainA insertI by blast
   8.484 -    ultimately show ?thesis by simp
   8.485 -  qed
   8.486 -qed
   8.487 -
   8.488 -lemma fold1_eq_fold:
   8.489 -assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
   8.490 -proof -
   8.491 -  interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
   8.492 -  from assms show ?thesis
   8.493 -apply (simp add: fold1_def fold_def)
   8.494 -apply (rule the_equality)
   8.495 -apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
   8.496 -apply (rule sym, clarify)
   8.497 -apply (case_tac "Aa=A")
   8.498 - apply (best intro: fold_graph_determ)
   8.499 -apply (subgoal_tac "fold_graph times a A x")
   8.500 - apply (best intro: fold_graph_determ)
   8.501 -apply (subgoal_tac "insert aa (Aa - {a}) = A")
   8.502 - prefer 2 apply (blast elim: equalityE)
   8.503 -apply (auto dest: fold_graph_permute_diff [where a=a])
   8.504 -done
   8.505 -qed
   8.506 -
   8.507 -lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
   8.508 -apply safe
   8.509 - apply simp
   8.510 - apply (drule_tac x=x in spec)
   8.511 - apply (drule_tac x="A-{x}" in spec, auto)
   8.512 -done
   8.513 -
   8.514 -lemma fold1_insert:
   8.515 -  assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
   8.516 -  shows "fold1 times (insert x A) = x * fold1 times A"
   8.517 -proof -
   8.518 -  interpret comp_fun_commute "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule comp_fun_commute)
   8.519 -  from nonempty obtain a A' where "A = insert a A' & a ~: A'"
   8.520 -    by (auto simp add: nonempty_iff)
   8.521 -  with A show ?thesis
   8.522 -    by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
   8.523 -qed
   8.524 -
   8.525 -end
   8.526 -
   8.527 -class ab_semigroup_idem_mult = ab_semigroup_mult +
   8.528 -  assumes mult_idem: "x * x = x"
   8.529 -
   8.530 -sublocale ab_semigroup_idem_mult < times!: semilattice times proof
   8.531 -qed (fact mult_idem)
   8.532 -
   8.533 -context ab_semigroup_idem_mult
   8.534 -begin
   8.535 - 
   8.536 -lemmas mult_left_idem = times.left_idem
   8.537 -
   8.538 -lemma comp_fun_idem: "comp_fun_idem (op *)"
   8.539 -  by default (simp_all add: fun_eq_iff mult_left_commute)
   8.540 -
   8.541 -lemma fold1_insert_idem [simp]:
   8.542 -  assumes nonempty: "A \<noteq> {}" and A: "finite A" 
   8.543 -  shows "fold1 times (insert x A) = x * fold1 times A"
   8.544 -proof -
   8.545 -  interpret comp_fun_idem "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a"
   8.546 -    by (rule comp_fun_idem)
   8.547 -  from nonempty obtain a A' where A': "A = insert a A' & a ~: A'"
   8.548 -    by (auto simp add: nonempty_iff)
   8.549 -  show ?thesis
   8.550 -  proof cases
   8.551 -    assume a: "a = x"
   8.552 -    show ?thesis
   8.553 -    proof cases
   8.554 -      assume "A' = {}"
   8.555 -      with A' a show ?thesis by simp
   8.556 -    next
   8.557 -      assume "A' \<noteq> {}"
   8.558 -      with A A' a show ?thesis
   8.559 -        by (simp add: fold1_insert mult_assoc [symmetric])
   8.560 -    qed
   8.561 -  next
   8.562 -    assume "a \<noteq> x"
   8.563 -    with A A' show ?thesis
   8.564 -      by (simp add: insert_commute fold1_eq_fold)
   8.565 -  qed
   8.566 -qed
   8.567 -
   8.568 -lemma hom_fold1_commute:
   8.569 -assumes hom: "!!x y. h (x * y) = h x * h y"
   8.570 -and N: "finite N" "N \<noteq> {}" shows "h (fold1 times N) = fold1 times (h ` N)"
   8.571 -using N
   8.572 -proof (induct rule: finite_ne_induct)
   8.573 -  case singleton thus ?case by simp
   8.574 -next
   8.575 -  case (insert n N)
   8.576 -  then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp
   8.577 -  also have "\<dots> = h n * h (fold1 times N)" by(rule hom)
   8.578 -  also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert)
   8.579 -  also have "times (h n) \<dots> = fold1 times (insert (h n) (h ` N))"
   8.580 -    using insert by(simp)
   8.581 -  also have "insert (h n) (h ` N) = h ` insert n N" by simp
   8.582 -  finally show ?case .
   8.583 -qed
   8.584 -
   8.585 -lemma fold1_eq_fold_idem:
   8.586 -  assumes "finite A"
   8.587 -  shows "fold1 times (insert a A) = fold times a A"
   8.588 -proof (cases "a \<in> A")
   8.589 -  case False
   8.590 -  with assms show ?thesis by (simp add: fold1_eq_fold)
   8.591 -next
   8.592 -  interpret comp_fun_idem times by (fact comp_fun_idem)
   8.593 -  case True then obtain b B
   8.594 -    where A: "A = insert a B" and "a \<notin> B" by (rule set_insert)
   8.595 -  with assms have "finite B" by auto
   8.596 -  then have "fold times a (insert a B) = fold times (a * a) B"
   8.597 -    using `a \<notin> B` by (rule fold_insert2)
   8.598 -  then show ?thesis
   8.599 -    using `a \<notin> B` `finite B` by (simp add: fold1_eq_fold A)
   8.600 -qed
   8.601 -
   8.602 -end
   8.603 -
   8.604 -
   8.605 -text{* Now the recursion rules for definitions: *}
   8.606 -
   8.607 -lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
   8.608 -by simp
   8.609 -
   8.610 -lemma (in ab_semigroup_mult) fold1_insert_def:
   8.611 -  "\<lbrakk> g = fold1 times; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
   8.612 -by (simp add:fold1_insert)
   8.613 -
   8.614 -lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def:
   8.615 -  "\<lbrakk> g = fold1 times; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
   8.616 -by simp
   8.617 -
   8.618 -subsubsection{* Determinacy for @{term fold1Set} *}
   8.619 -
   8.620 -(*Not actually used!!*)
   8.621 -(*
   8.622 -context ab_semigroup_mult
   8.623 -begin
   8.624 -
   8.625 -lemma fold_graph_permute:
   8.626 -  "[|fold_graph times id b (insert a A) x; a \<notin> A; b \<notin> A|]
   8.627 -   ==> fold_graph times id a (insert b A) x"
   8.628 -apply (cases "a=b") 
   8.629 -apply (auto dest: fold_graph_permute_diff) 
   8.630 -done
   8.631 -
   8.632 -lemma fold1Set_determ:
   8.633 -  "fold1Set times A x ==> fold1Set times A y ==> y = x"
   8.634 -proof (clarify elim!: fold1Set.cases)
   8.635 -  fix A x B y a b
   8.636 -  assume Ax: "fold_graph times id a A x"
   8.637 -  assume By: "fold_graph times id b B y"
   8.638 -  assume anotA:  "a \<notin> A"
   8.639 -  assume bnotB:  "b \<notin> B"
   8.640 -  assume eq: "insert a A = insert b B"
   8.641 -  show "y=x"
   8.642 -  proof cases
   8.643 -    assume same: "a=b"
   8.644 -    hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
   8.645 -    thus ?thesis using Ax By same by (blast intro: fold_graph_determ)
   8.646 -  next
   8.647 -    assume diff: "a\<noteq>b"
   8.648 -    let ?D = "B - {a}"
   8.649 -    have B: "B = insert a ?D" and A: "A = insert b ?D"
   8.650 -     and aB: "a \<in> B" and bA: "b \<in> A"
   8.651 -      using eq anotA bnotB diff by (blast elim!:equalityE)+
   8.652 -    with aB bnotB By
   8.653 -    have "fold_graph times id a (insert b ?D) y" 
   8.654 -      by (auto intro: fold_graph_permute simp add: insert_absorb)
   8.655 -    moreover
   8.656 -    have "fold_graph times id a (insert b ?D) x"
   8.657 -      by (simp add: A [symmetric] Ax) 
   8.658 -    ultimately show ?thesis by (blast intro: fold_graph_determ) 
   8.659 -  qed
   8.660 -qed
   8.661 -
   8.662 -lemma fold1Set_equality: "fold1Set times A y ==> fold1 times A = y"
   8.663 -  by (unfold fold1_def) (blast intro: fold1Set_determ)
   8.664 -
   8.665 -end
   8.666 -*)
   8.667 -
   8.668 -declare
   8.669 -  empty_fold_graphE [rule del]  fold_graph.intros [rule del]
   8.670 -  empty_fold1SetE [rule del]  insert_fold1SetE [rule del]
   8.671 -  -- {* No more proofs involve these relations. *}
   8.672 -
   8.673 -subsubsection {* Lemmas about @{text fold1} *}
   8.674 -
   8.675 -context ab_semigroup_mult
   8.676 -begin
   8.677 -
   8.678 -lemma fold1_Un:
   8.679 -assumes A: "finite A" "A \<noteq> {}"
   8.680 -shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
   8.681 -       fold1 times (A Un B) = fold1 times A * fold1 times B"
   8.682 -using A by (induct rule: finite_ne_induct)
   8.683 -  (simp_all add: fold1_insert mult_assoc)
   8.684 -
   8.685 -lemma fold1_in:
   8.686 -  assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x,y}"
   8.687 -  shows "fold1 times A \<in> A"
   8.688 -using A
   8.689 -proof (induct rule:finite_ne_induct)
   8.690 -  case singleton thus ?case by simp
   8.691 -next
   8.692 -  case insert thus ?case using elem by (force simp add:fold1_insert)
   8.693 -qed
   8.694 -
   8.695 -end
   8.696 -
   8.697 -lemma (in ab_semigroup_idem_mult) fold1_Un2:
   8.698 -assumes A: "finite A" "A \<noteq> {}"
   8.699 -shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
   8.700 -       fold1 times (A Un B) = fold1 times A * fold1 times B"
   8.701 -using A
   8.702 -proof(induct rule:finite_ne_induct)
   8.703 -  case singleton thus ?case by simp
   8.704 -next
   8.705 -  case insert thus ?case by (simp add: mult_assoc)
   8.706 -qed
   8.707 -
   8.708 -
   8.709  subsection {* Locales as mini-packages for fold operations *}
   8.710  
   8.711  subsubsection {* The natural case *}
   8.712  
   8.713  locale folding =
   8.714    fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
   8.715 -  fixes F :: "'a set \<Rightarrow> 'b \<Rightarrow> 'b"
   8.716 +  fixes z :: "'b"
   8.717    assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
   8.718 -  assumes eq_fold: "finite A \<Longrightarrow> F A s = fold f s A"
   8.719  begin
   8.720  
   8.721 +definition F :: "'a set \<Rightarrow> 'b"
   8.722 +where
   8.723 +  eq_fold: "F A = fold f z A"
   8.724 +
   8.725  lemma empty [simp]:
   8.726 -  "F {} = id"
   8.727 -  by (simp add: eq_fold fun_eq_iff)
   8.728 +  "F {} = z"
   8.729 +  by (simp add: eq_fold)
   8.730  
   8.731 +lemma infinite [simp]:
   8.732 +  "\<not> finite A \<Longrightarrow> F A = z"
   8.733 +  by (simp add: eq_fold)
   8.734 + 
   8.735  lemma insert [simp]:
   8.736    assumes "finite A" and "x \<notin> A"
   8.737 -  shows "F (insert x A) = F A \<circ> f x"
   8.738 +  shows "F (insert x A) = f x (F A)"
   8.739  proof -
   8.740    interpret comp_fun_commute f
   8.741      by default (insert comp_fun_commute, simp add: fun_eq_iff)
   8.742 -  from fold_insert2 assms
   8.743 -  have "\<And>s. fold f s (insert x A) = fold f (f x s) A" .
   8.744 +  from fold_insert assms
   8.745 +  have "fold f z (insert x A) = f x (fold f z A)" by simp
   8.746    with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
   8.747  qed
   8.748 -
   8.749 + 
   8.750  lemma remove:
   8.751    assumes "finite A" and "x \<in> A"
   8.752 -  shows "F A = F (A - {x}) \<circ> f x"
   8.753 +  shows "F A = f x (F (A - {x}))"
   8.754  proof -
   8.755    from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
   8.756      by (auto dest: mk_disjoint_insert)
   8.757 @@ -1500,524 +1065,69 @@
   8.758  
   8.759  lemma insert_remove:
   8.760    assumes "finite A"
   8.761 -  shows "F (insert x A) = F (A - {x}) \<circ> f x"
   8.762 +  shows "F (insert x A) = f x (F (A - {x}))"
   8.763    using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
   8.764  
   8.765 -lemma commute_left_comp:
   8.766 -  "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
   8.767 -  by (simp add: o_assoc comp_fun_commute)
   8.768 -
   8.769 -lemma comp_fun_commute':
   8.770 -  assumes "finite A"
   8.771 -  shows "f x \<circ> F A = F A \<circ> f x"
   8.772 -  using assms by (induct A)
   8.773 -    (simp, simp del: o_apply add: o_assoc, simp del: o_apply add: comp_assoc comp_fun_commute)
   8.774 -
   8.775 -lemma commute_left_comp':
   8.776 -  assumes "finite A"
   8.777 -  shows "f x \<circ> (F A \<circ> g) = F A \<circ> (f x \<circ> g)"
   8.778 -  using assms by (simp add: o_assoc comp_fun_commute')
   8.779 -
   8.780 -lemma comp_fun_commute'':
   8.781 -  assumes "finite A" and "finite B"
   8.782 -  shows "F B \<circ> F A = F A \<circ> F B"
   8.783 -  using assms by (induct A)
   8.784 -    (simp_all add: o_assoc, simp add: comp_assoc comp_fun_commute')
   8.785 -
   8.786 -lemma commute_left_comp'':
   8.787 -  assumes "finite A" and "finite B"
   8.788 -  shows "F B \<circ> (F A \<circ> g) = F A \<circ> (F B \<circ> g)"
   8.789 -  using assms by (simp add: o_assoc comp_fun_commute'')
   8.790 -
   8.791 -lemmas comp_fun_commutes = comp_assoc comp_fun_commute commute_left_comp
   8.792 -  comp_fun_commute' commute_left_comp' comp_fun_commute'' commute_left_comp''
   8.793 -
   8.794 -lemma union_inter:
   8.795 -  assumes "finite A" and "finite B"
   8.796 -  shows "F (A \<union> B) \<circ> F (A \<inter> B) = F A \<circ> F B"
   8.797 -  using assms by (induct A)
   8.798 -    (simp_all del: o_apply add: insert_absorb Int_insert_left comp_fun_commutes,
   8.799 -      simp add: o_assoc)
   8.800 -
   8.801 -lemma union:
   8.802 -  assumes "finite A" and "finite B"
   8.803 -  and "A \<inter> B = {}"
   8.804 -  shows "F (A \<union> B) = F A \<circ> F B"
   8.805 -proof -
   8.806 -  from union_inter `finite A` `finite B` have "F (A \<union> B) \<circ> F (A \<inter> B) = F A \<circ> F B" .
   8.807 -  with `A \<inter> B = {}` show ?thesis by simp
   8.808 -qed
   8.809 -
   8.810  end
   8.811  
   8.812  
   8.813 -subsubsection {* The natural case with idempotency *}
   8.814 +subsubsection {* With idempotency *}
   8.815  
   8.816  locale folding_idem = folding +
   8.817 -  assumes idem_comp: "f x \<circ> f x = f x"
   8.818 +  assumes comp_fun_idem: "f x \<circ> f x = f x"
   8.819  begin
   8.820  
   8.821 -lemma idem_left_comp:
   8.822 -  "f x \<circ> (f x \<circ> g) = f x \<circ> g"
   8.823 -  by (simp add: o_assoc idem_comp)
   8.824 -
   8.825 -lemma in_comp_idem:
   8.826 -  assumes "finite A" and "x \<in> A"
   8.827 -  shows "F A \<circ> f x = F A"
   8.828 -using assms by (induct A)
   8.829 -  (auto simp add: comp_fun_commutes idem_comp, simp add: commute_left_comp' [symmetric] comp_fun_commute')
   8.830 -
   8.831 -lemma subset_comp_idem:
   8.832 -  assumes "finite A" and "B \<subseteq> A"
   8.833 -  shows "F A \<circ> F B = F A"
   8.834 -proof -
   8.835 -  from assms have "finite B" by (blast dest: finite_subset)
   8.836 -  then show ?thesis using `B \<subseteq> A` by (induct B)
   8.837 -    (simp_all add: o_assoc in_comp_idem `finite A`)
   8.838 -qed
   8.839 -
   8.840  declare insert [simp del]
   8.841  
   8.842  lemma insert_idem [simp]:
   8.843    assumes "finite A"
   8.844 -  shows "F (insert x A) = F A \<circ> f x"
   8.845 -  using assms by (cases "x \<in> A") (simp_all add: insert in_comp_idem insert_absorb)
   8.846 -
   8.847 -lemma union_idem:
   8.848 -  assumes "finite A" and "finite B"
   8.849 -  shows "F (A \<union> B) = F A \<circ> F B"
   8.850 +  shows "F (insert x A) = f x (F A)"
   8.851  proof -
   8.852 -  from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
   8.853 -  then have "F (A \<union> B) \<circ> F (A \<inter> B) = F (A \<union> B)" by (rule subset_comp_idem)
   8.854 -  with assms show ?thesis by (simp add: union_inter)
   8.855 +  interpret comp_fun_idem f
   8.856 +    by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
   8.857 +  from fold_insert_idem assms
   8.858 +  have "fold f z (insert x A) = f x (fold f z A)" by simp
   8.859 +  with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
   8.860  qed
   8.861  
   8.862  end
   8.863  
   8.864  
   8.865 -subsubsection {* The image case with fixed function *}
   8.866 -
   8.867 -no_notation times (infixl "*" 70)
   8.868 -no_notation Groups.one ("1")
   8.869 -
   8.870 -locale folding_image_simple = comm_monoid +
   8.871 -  fixes g :: "('b \<Rightarrow> 'a)"
   8.872 -  fixes F :: "'b set \<Rightarrow> 'a"
   8.873 -  assumes eq_fold_g: "finite A \<Longrightarrow> F A = fold_image f g 1 A"
   8.874 -begin
   8.875 -
   8.876 -lemma empty [simp]:
   8.877 -  "F {} = 1"
   8.878 -  by (simp add: eq_fold_g)
   8.879 -
   8.880 -lemma insert [simp]:
   8.881 -  assumes "finite A" and "x \<notin> A"
   8.882 -  shows "F (insert x A) = g x * F A"
   8.883 -proof -
   8.884 -  interpret comp_fun_commute "%x y. (g x) * y"
   8.885 -    by default (simp add: ac_simps fun_eq_iff)
   8.886 -  from assms have "fold_image (op *) g 1 (insert x A) = g x * fold_image (op *) g 1 A"
   8.887 -    by (simp add: fold_image_def)
   8.888 -  with `finite A` show ?thesis by (simp add: eq_fold_g)
   8.889 -qed
   8.890 -
   8.891 -lemma remove:
   8.892 -  assumes "finite A" and "x \<in> A"
   8.893 -  shows "F A = g x * F (A - {x})"
   8.894 -proof -
   8.895 -  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
   8.896 -    by (auto dest: mk_disjoint_insert)
   8.897 -  moreover from `finite A` this have "finite B" by simp
   8.898 -  ultimately show ?thesis by simp
   8.899 -qed
   8.900 -
   8.901 -lemma insert_remove:
   8.902 -  assumes "finite A"
   8.903 -  shows "F (insert x A) = g x * F (A - {x})"
   8.904 -  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
   8.905 -
   8.906 -lemma neutral:
   8.907 -  assumes "finite A" and "\<forall>x\<in>A. g x = 1"
   8.908 -  shows "F A = 1"
   8.909 -  using assms by (induct A) simp_all
   8.910 -
   8.911 -lemma union_inter:
   8.912 -  assumes "finite A" and "finite B"
   8.913 -  shows "F (A \<union> B) * F (A \<inter> B) = F A * F B"
   8.914 -using assms proof (induct A)
   8.915 -  case empty then show ?case by simp
   8.916 -next
   8.917 -  case (insert x A) then show ?case
   8.918 -    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
   8.919 -qed
   8.920 -
   8.921 -corollary union_inter_neutral:
   8.922 -  assumes "finite A" and "finite B"
   8.923 -  and I0: "\<forall>x \<in> A\<inter>B. g x = 1"
   8.924 -  shows "F (A \<union> B) = F A * F B"
   8.925 -  using assms by (simp add: union_inter [symmetric] neutral)
   8.926 -
   8.927 -corollary union_disjoint:
   8.928 -  assumes "finite A" and "finite B"
   8.929 -  assumes "A \<inter> B = {}"
   8.930 -  shows "F (A \<union> B) = F A * F B"
   8.931 -  using assms by (simp add: union_inter_neutral)
   8.932 -
   8.933 -end
   8.934 -
   8.935 -
   8.936 -subsubsection {* The image case with flexible function *}
   8.937 -
   8.938 -locale folding_image = comm_monoid +
   8.939 -  fixes F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
   8.940 -  assumes eq_fold: "\<And>g. finite A \<Longrightarrow> F g A = fold_image f g 1 A"
   8.941 -
   8.942 -sublocale folding_image < folding_image_simple "op *" 1 g "F g" proof
   8.943 -qed (fact eq_fold)
   8.944 -
   8.945 -context folding_image
   8.946 -begin
   8.947 -
   8.948 -lemma reindex: (* FIXME polymorhism *)
   8.949 -  assumes "finite A" and "inj_on h A"
   8.950 -  shows "F g (h ` A) = F (g \<circ> h) A"
   8.951 -  using assms by (induct A) auto
   8.952 -
   8.953 -lemma cong:
   8.954 -  assumes "finite A" and "\<And>x. x \<in> A \<Longrightarrow> g x = h x"
   8.955 -  shows "F g A = F h A"
   8.956 -proof -
   8.957 -  from assms have "ALL C. C <= A --> (ALL x:C. g x = h x) --> F g C = F h C"
   8.958 -  apply - apply (erule finite_induct) apply simp
   8.959 -  apply (simp add: subset_insert_iff, clarify)
   8.960 -  apply (subgoal_tac "finite C")
   8.961 -  prefer 2 apply (blast dest: finite_subset [rotated])
   8.962 -  apply (subgoal_tac "C = insert x (C - {x})")
   8.963 -  prefer 2 apply blast
   8.964 -  apply (erule ssubst)
   8.965 -  apply (drule spec)
   8.966 -  apply (erule (1) notE impE)
   8.967 -  apply (simp add: Ball_def del: insert_Diff_single)
   8.968 -  done
   8.969 -  with assms show ?thesis by simp
   8.970 -qed
   8.971 -
   8.972 -lemma UNION_disjoint:
   8.973 -  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
   8.974 -  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
   8.975 -  shows "F g (UNION I A) = F (F g \<circ> A) I"
   8.976 -apply (insert assms)
   8.977 -apply (induct rule: finite_induct)
   8.978 -apply simp
   8.979 -apply atomize
   8.980 -apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
   8.981 - prefer 2 apply blast
   8.982 -apply (subgoal_tac "A x Int UNION Fa A = {}")
   8.983 - prefer 2 apply blast
   8.984 -apply (simp add: union_disjoint)
   8.985 -done
   8.986 -
   8.987 -lemma distrib:
   8.988 -  assumes "finite A"
   8.989 -  shows "F (\<lambda>x. g x * h x) A = F g A * F h A"
   8.990 -  using assms by (rule finite_induct) (simp_all add: assoc commute left_commute)
   8.991 -
   8.992 -lemma related: 
   8.993 -  assumes Re: "R 1 1" 
   8.994 -  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
   8.995 -  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
   8.996 -  shows "R (F h S) (F g S)"
   8.997 -  using fS by (rule finite_subset_induct) (insert assms, auto)
   8.998 -
   8.999 -lemma eq_general:
  8.1000 -  assumes fS: "finite S"
  8.1001 -  and h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y" 
  8.1002 -  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x"
  8.1003 -  shows "F f1 S = F f2 S'"
  8.1004 -proof-
  8.1005 -  from h f12 have hS: "h ` S = S'" by blast
  8.1006 -  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
  8.1007 -    from f12 h H  have "x = y" by auto }
  8.1008 -  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
  8.1009 -  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
  8.1010 -  from hS have "F f2 S' = F f2 (h ` S)" by simp
  8.1011 -  also have "\<dots> = F (f2 o h) S" using reindex [OF fS hinj, of f2] .
  8.1012 -  also have "\<dots> = F f1 S " using th cong [OF fS, of "f2 o h" f1]
  8.1013 -    by blast
  8.1014 -  finally show ?thesis ..
  8.1015 -qed
  8.1016 -
  8.1017 -lemma eq_general_inverses:
  8.1018 -  assumes fS: "finite S" 
  8.1019 -  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
  8.1020 -  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x"
  8.1021 -  shows "F j S = F g T"
  8.1022 -  (* metis solves it, but not yet available here *)
  8.1023 -  apply (rule eq_general [OF fS, of T h g j])
  8.1024 -  apply (rule ballI)
  8.1025 -  apply (frule kh)
  8.1026 -  apply (rule ex1I[])
  8.1027 -  apply blast
  8.1028 -  apply clarsimp
  8.1029 -  apply (drule hk) apply simp
  8.1030 -  apply (rule sym)
  8.1031 -  apply (erule conjunct1[OF conjunct2[OF hk]])
  8.1032 -  apply (rule ballI)
  8.1033 -  apply (drule hk)
  8.1034 -  apply blast
  8.1035 -  done
  8.1036 -
  8.1037 -end
  8.1038 -
  8.1039 -
  8.1040 -subsubsection {* The image case with fixed function and idempotency *}
  8.1041 -
  8.1042 -locale folding_image_simple_idem = folding_image_simple +
  8.1043 -  assumes idem: "x * x = x"
  8.1044 -
  8.1045 -sublocale folding_image_simple_idem < semilattice: semilattice proof
  8.1046 -qed (fact idem)
  8.1047 -
  8.1048 -context folding_image_simple_idem
  8.1049 -begin
  8.1050 -
  8.1051 -lemma in_idem:
  8.1052 -  assumes "finite A" and "x \<in> A"
  8.1053 -  shows "g x * F A = F A"
  8.1054 -  using assms by (induct A) (auto simp add: left_commute)
  8.1055 -
  8.1056 -lemma subset_idem:
  8.1057 -  assumes "finite A" and "B \<subseteq> A"
  8.1058 -  shows "F B * F A = F A"
  8.1059 -proof -
  8.1060 -  from assms have "finite B" by (blast dest: finite_subset)
  8.1061 -  then show ?thesis using `B \<subseteq> A` by (induct B)
  8.1062 -    (auto simp add: assoc in_idem `finite A`)
  8.1063 -qed
  8.1064 -
  8.1065 -declare insert [simp del]
  8.1066 -
  8.1067 -lemma insert_idem [simp]:
  8.1068 -  assumes "finite A"
  8.1069 -  shows "F (insert x A) = g x * F A"
  8.1070 -  using assms by (cases "x \<in> A") (simp_all add: insert in_idem insert_absorb)
  8.1071 -
  8.1072 -lemma union_idem:
  8.1073 -  assumes "finite A" and "finite B"
  8.1074 -  shows "F (A \<union> B) = F A * F B"
  8.1075 -proof -
  8.1076 -  from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
  8.1077 -  then have "F (A \<inter> B) * F (A \<union> B) = F (A \<union> B)" by (rule subset_idem)
  8.1078 -  with assms show ?thesis by (simp add: union_inter [of A B, symmetric] commute)
  8.1079 -qed
  8.1080 -
  8.1081 -end
  8.1082 -
  8.1083 -
  8.1084 -subsubsection {* The image case with flexible function and idempotency *}
  8.1085 -
  8.1086 -locale folding_image_idem = folding_image +
  8.1087 -  assumes idem: "x * x = x"
  8.1088 -
  8.1089 -sublocale folding_image_idem < folding_image_simple_idem "op *" 1 g "F g" proof
  8.1090 -qed (fact idem)
  8.1091 -
  8.1092 -
  8.1093 -subsubsection {* The neutral-less case *}
  8.1094 -
  8.1095 -locale folding_one = abel_semigroup +
  8.1096 -  fixes F :: "'a set \<Rightarrow> 'a"
  8.1097 -  assumes eq_fold: "finite A \<Longrightarrow> F A = fold1 f A"
  8.1098 -begin
  8.1099 -
  8.1100 -lemma singleton [simp]:
  8.1101 -  "F {x} = x"
  8.1102 -  by (simp add: eq_fold)
  8.1103 -
  8.1104 -lemma eq_fold':
  8.1105 -  assumes "finite A" and "x \<notin> A"
  8.1106 -  shows "F (insert x A) = fold (op *) x A"
  8.1107 -proof -
  8.1108 -  interpret ab_semigroup_mult "op *" by default (simp_all add: ac_simps)
  8.1109 -  from assms show ?thesis by (simp add: eq_fold fold1_eq_fold)
  8.1110 -qed
  8.1111 -
  8.1112 -lemma insert [simp]:
  8.1113 -  assumes "finite A" and "x \<notin> A" and "A \<noteq> {}"
  8.1114 -  shows "F (insert x A) = x * F A"
  8.1115 -proof -
  8.1116 -  from `A \<noteq> {}` obtain b where "b \<in> A" by blast
  8.1117 -  then obtain B where *: "A = insert b B" "b \<notin> B" by (blast dest: mk_disjoint_insert)
  8.1118 -  with `finite A` have "finite B" by simp
  8.1119 -  interpret fold: folding "op *" "\<lambda>a b. fold (op *) b a" proof
  8.1120 -  qed (simp_all add: fun_eq_iff ac_simps)
  8.1121 -  from `finite B` fold.comp_fun_commute' [of B x]
  8.1122 -    have "op * x \<circ> (\<lambda>b. fold op * b B) = (\<lambda>b. fold op * b B) \<circ> op * x" by simp
  8.1123 -  then have A: "x * fold op * b B = fold op * (b * x) B" by (simp add: fun_eq_iff commute)
  8.1124 -  from `finite B` * fold.insert [of B b]
  8.1125 -    have "(\<lambda>x. fold op * x (insert b B)) = (\<lambda>x. fold op * x B) \<circ> op * b" by simp
  8.1126 -  then have B: "fold op * x (insert b B) = fold op * (b * x) B" by (simp add: fun_eq_iff)
  8.1127 -  from A B assms * show ?thesis by (simp add: eq_fold' del: fold.insert)
  8.1128 -qed
  8.1129 -
  8.1130 -lemma remove:
  8.1131 -  assumes "finite A" and "x \<in> A"
  8.1132 -  shows "F A = (if A - {x} = {} then x else x * F (A - {x}))"
  8.1133 -proof -
  8.1134 -  from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
  8.1135 -  with assms show ?thesis by simp
  8.1136 -qed
  8.1137 -
  8.1138 -lemma insert_remove:
  8.1139 -  assumes "finite A"
  8.1140 -  shows "F (insert x A) = (if A - {x} = {} then x else x * F (A - {x}))"
  8.1141 -  using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
  8.1142 -
  8.1143 -lemma union_disjoint:
  8.1144 -  assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}" and "A \<inter> B = {}"
  8.1145 -  shows "F (A \<union> B) = F A * F B"
  8.1146 -  using assms by (induct A rule: finite_ne_induct) (simp_all add: ac_simps)
  8.1147 -
  8.1148 -lemma union_inter:
  8.1149 -  assumes "finite A" and "finite B" and "A \<inter> B \<noteq> {}"
  8.1150 -  shows "F (A \<union> B) * F (A \<inter> B) = F A * F B"
  8.1151 -proof -
  8.1152 -  from assms have "A \<noteq> {}" and "B \<noteq> {}" by auto
  8.1153 -  from `finite A` `A \<noteq> {}` `A \<inter> B \<noteq> {}` show ?thesis proof (induct A rule: finite_ne_induct)
  8.1154 -    case (singleton x) then show ?case by (simp add: insert_absorb ac_simps)
  8.1155 -  next
  8.1156 -    case (insert x A) show ?case proof (cases "x \<in> B")
  8.1157 -      case True then have "B \<noteq> {}" by auto
  8.1158 -      with insert True `finite B` show ?thesis by (cases "A \<inter> B = {}")
  8.1159 -        (simp_all add: insert_absorb ac_simps union_disjoint)
  8.1160 -    next
  8.1161 -      case False with insert have "F (A \<union> B) * F (A \<inter> B) = F A * F B" by simp
  8.1162 -      moreover from False `finite B` insert have "finite (A \<union> B)" "x \<notin> A \<union> B" "A \<union> B \<noteq> {}"
  8.1163 -        by auto
  8.1164 -      ultimately show ?thesis using False `finite A` `x \<notin> A` `A \<noteq> {}` by (simp add: assoc)
  8.1165 -    qed
  8.1166 -  qed
  8.1167 -qed
  8.1168 -
  8.1169 -lemma closed:
  8.1170 -  assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
  8.1171 -  shows "F A \<in> A"
  8.1172 -using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
  8.1173 -  case singleton then show ?case by simp
  8.1174 -next
  8.1175 -  case insert with elem show ?case by force
  8.1176 -qed
  8.1177 -
  8.1178 -end
  8.1179 -
  8.1180 -
  8.1181 -subsubsection {* The neutral-less case with idempotency *}
  8.1182 -
  8.1183 -locale folding_one_idem = folding_one +
  8.1184 -  assumes idem: "x * x = x"
  8.1185 -
  8.1186 -sublocale folding_one_idem < semilattice: semilattice proof
  8.1187 -qed (fact idem)
  8.1188 -
  8.1189 -context folding_one_idem
  8.1190 -begin
  8.1191 -
  8.1192 -lemma in_idem:
  8.1193 -  assumes "finite A" and "x \<in> A"
  8.1194 -  shows "x * F A = F A"
  8.1195 -proof -
  8.1196 -  from assms have "A \<noteq> {}" by auto
  8.1197 -  with `finite A` show ?thesis using `x \<in> A` by (induct A rule: finite_ne_induct) (auto simp add: ac_simps)
  8.1198 -qed
  8.1199 -
  8.1200 -lemma subset_idem:
  8.1201 -  assumes "finite A" "B \<noteq> {}" and "B \<subseteq> A"
  8.1202 -  shows "F B * F A = F A"
  8.1203 -proof -
  8.1204 -  from assms have "finite B" by (blast dest: finite_subset)
  8.1205 -  then show ?thesis using `B \<noteq> {}` `B \<subseteq> A` by (induct B rule: finite_ne_induct)
  8.1206 -    (simp_all add: assoc in_idem `finite A`)
  8.1207 -qed
  8.1208 -
  8.1209 -lemma eq_fold_idem':
  8.1210 -  assumes "finite A"
  8.1211 -  shows "F (insert a A) = fold (op *) a A"
  8.1212 -proof -
  8.1213 -  interpret ab_semigroup_idem_mult "op *" by default (simp_all add: ac_simps)
  8.1214 -  from assms show ?thesis by (simp add: eq_fold fold1_eq_fold_idem)
  8.1215 -qed
  8.1216 -
  8.1217 -lemma insert_idem [simp]:
  8.1218 -  assumes "finite A" and "A \<noteq> {}"
  8.1219 -  shows "F (insert x A) = x * F A"
  8.1220 -proof (cases "x \<in> A")
  8.1221 -  case False from `finite A` `x \<notin> A` `A \<noteq> {}` show ?thesis by (rule insert)
  8.1222 -next
  8.1223 -  case True
  8.1224 -  from `finite A` `A \<noteq> {}` show ?thesis by (simp add: in_idem insert_absorb True)
  8.1225 -qed
  8.1226 -  
  8.1227 -lemma union_idem:
  8.1228 -  assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}"
  8.1229 -  shows "F (A \<union> B) = F A * F B"
  8.1230 -proof (cases "A \<inter> B = {}")
  8.1231 -  case True with assms show ?thesis by (simp add: union_disjoint)
  8.1232 -next
  8.1233 -  case False
  8.1234 -  from assms have "finite (A \<union> B)" and "A \<inter> B \<subseteq> A \<union> B" by auto
  8.1235 -  with False have "F (A \<inter> B) * F (A \<union> B) = F (A \<union> B)" by (auto intro: subset_idem)
  8.1236 -  with assms False show ?thesis by (simp add: union_inter [of A B, symmetric] commute)
  8.1237 -qed
  8.1238 -
  8.1239 -lemma hom_commute:
  8.1240 -  assumes hom: "\<And>x y. h (x * y) = h x * h y"
  8.1241 -  and N: "finite N" "N \<noteq> {}" shows "h (F N) = F (h ` N)"
  8.1242 -using N proof (induct rule: finite_ne_induct)
  8.1243 -  case singleton thus ?case by simp
  8.1244 -next
  8.1245 -  case (insert n N)
  8.1246 -  then have "h (F (insert n N)) = h (n * F N)" by simp
  8.1247 -  also have "\<dots> = h n * h (F N)" by (rule hom)
  8.1248 -  also have "h (F N) = F (h ` N)" by(rule insert)
  8.1249 -  also have "h n * \<dots> = F (insert (h n) (h ` N))"
  8.1250 -    using insert by(simp)
  8.1251 -  also have "insert (h n) (h ` N) = h ` insert n N" by simp
  8.1252 -  finally show ?case .
  8.1253 -qed
  8.1254 -
  8.1255 -end
  8.1256 -
  8.1257 -notation times (infixl "*" 70)
  8.1258 -notation Groups.one ("1")
  8.1259 -
  8.1260 -
  8.1261  subsection {* Finite cardinality *}
  8.1262  
  8.1263 -text {* This definition, although traditional, is ugly to work with:
  8.1264 -@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
  8.1265 -But now that we have @{text fold_image} things are easy:
  8.1266 +text {*
  8.1267 +  The traditional definition
  8.1268 +  @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
  8.1269 +  is ugly to work with.
  8.1270 +  But now that we have @{const fold} things are easy:
  8.1271  *}
  8.1272  
  8.1273  definition card :: "'a set \<Rightarrow> nat" where
  8.1274 -  "card A = (if finite A then fold_image (op +) (\<lambda>x. 1) 0 A else 0)"
  8.1275 +  "card = folding.F (\<lambda>_. Suc) 0"
  8.1276  
  8.1277 -interpretation card: folding_image_simple "op +" 0 "\<lambda>x. 1" card proof
  8.1278 -qed (simp add: card_def)
  8.1279 +interpretation card!: folding "\<lambda>_. Suc" 0
  8.1280 +where
  8.1281 +  "card.F = card"
  8.1282 +proof -
  8.1283 +  show "folding (\<lambda>_. Suc)" by default rule
  8.1284 +  then interpret card!: folding "\<lambda>_. Suc" 0 .
  8.1285 +  show "card.F = card" by (simp only: card_def)
  8.1286 +qed
  8.1287  
  8.1288 -lemma card_infinite [simp]:
  8.1289 +lemma card_infinite:
  8.1290    "\<not> finite A \<Longrightarrow> card A = 0"
  8.1291 -  by (simp add: card_def)
  8.1292 +  by (fact card.infinite)
  8.1293  
  8.1294  lemma card_empty:
  8.1295    "card {} = 0"
  8.1296    by (fact card.empty)
  8.1297  
  8.1298  lemma card_insert_disjoint:
  8.1299 -  "finite A ==> x \<notin> A ==> card (insert x A) = Suc (card A)"
  8.1300 -  by simp
  8.1301 +  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
  8.1302 +  by (fact card.insert)
  8.1303  
  8.1304  lemma card_insert_if:
  8.1305 -  "finite A ==> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
  8.1306 +  "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
  8.1307    by auto (simp add: card.insert_remove card.remove)
  8.1308  
  8.1309  lemma card_ge_0_finite:
  8.1310 @@ -2040,29 +1150,30 @@
  8.1311    "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
  8.1312    by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
  8.1313  
  8.1314 -lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
  8.1315 +lemma card_Suc_Diff1:
  8.1316 +  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
  8.1317  apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
  8.1318  apply(simp del:insert_Diff_single)
  8.1319  done
  8.1320  
  8.1321  lemma card_Diff_singleton:
  8.1322 -  "finite A ==> x: A ==> card (A - {x}) = card A - 1"
  8.1323 -by (simp add: card_Suc_Diff1 [symmetric])
  8.1324 +  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
  8.1325 +  by (simp add: card_Suc_Diff1 [symmetric])
  8.1326  
  8.1327  lemma card_Diff_singleton_if:
  8.1328 -  "finite A ==> card (A - {x}) = (if x : A then card A - 1 else card A)"
  8.1329 -by (simp add: card_Diff_singleton)
  8.1330 +  "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
  8.1331 +  by (simp add: card_Diff_singleton)
  8.1332  
  8.1333  lemma card_Diff_insert[simp]:
  8.1334 -assumes "finite A" and "a:A" and "a ~: B"
  8.1335 -shows "card(A - insert a B) = card(A - B) - 1"
  8.1336 +  assumes "finite A" and "a \<in> A" and "a \<notin> B"
  8.1337 +  shows "card (A - insert a B) = card (A - B) - 1"
  8.1338  proof -
  8.1339    have "A - insert a B = (A - B) - {a}" using assms by blast
  8.1340 -  then show ?thesis using assms by(simp add:card_Diff_singleton)
  8.1341 +  then show ?thesis using assms by(simp add: card_Diff_singleton)
  8.1342  qed
  8.1343  
  8.1344  lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
  8.1345 -by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert)
  8.1346 +  by (fact card.insert_remove)
  8.1347  
  8.1348  lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
  8.1349  by (simp add: card_insert_if)
  8.1350 @@ -2105,13 +1216,21 @@
  8.1351  apply (blast dest: card_seteq)
  8.1352  done
  8.1353  
  8.1354 -lemma card_Un_Int: "finite A ==> finite B
  8.1355 -    ==> card A + card B = card (A Un B) + card (A Int B)"
  8.1356 -  by (fact card.union_inter [symmetric])
  8.1357 +lemma card_Un_Int:
  8.1358 +  assumes "finite A" and "finite B"
  8.1359 +  shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
  8.1360 +using assms proof (induct A)
  8.1361 +  case empty then show ?case by simp
  8.1362 +next
  8.1363 + case (insert x A) then show ?case
  8.1364 +    by (auto simp add: insert_absorb Int_insert_left)
  8.1365 +qed
  8.1366  
  8.1367 -lemma card_Un_disjoint: "finite A ==> finite B
  8.1368 -    ==> A Int B = {} ==> card (A Un B) = card A + card B"
  8.1369 -  by (fact card.union_disjoint)
  8.1370 +lemma card_Un_disjoint:
  8.1371 +  assumes "finite A" and "finite B"
  8.1372 +  assumes "A \<inter> B = {}"
  8.1373 +  shows "card (A \<union> B) = card A + card B"
  8.1374 +using assms card_Un_Int [of A B] by simp
  8.1375  
  8.1376  lemma card_Diff_subset:
  8.1377    assumes "finite B" and "B \<subseteq> A"
  8.1378 @@ -2241,7 +1360,7 @@
  8.1379  apply(rule iffI)
  8.1380   apply(erule card_eq_SucD)
  8.1381  apply(auto)
  8.1382 -apply(subst card_insert)
  8.1383 +apply(subst card.insert)
  8.1384   apply(auto intro:ccontr)
  8.1385  done
  8.1386  
  8.1387 @@ -2439,25 +1558,26 @@
  8.1388  shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
  8.1389  by(fastforce simp:surj_def dest!: endo_inj_surj)
  8.1390  
  8.1391 -corollary infinite_UNIV_nat[iff]: "~finite(UNIV::nat set)"
  8.1392 +corollary infinite_UNIV_nat [iff]:
  8.1393 +  "\<not> finite (UNIV :: nat set)"
  8.1394  proof
  8.1395 -  assume "finite(UNIV::nat set)"
  8.1396 -  with finite_UNIV_inj_surj[of Suc]
  8.1397 +  assume "finite (UNIV :: nat set)"
  8.1398 +  with finite_UNIV_inj_surj [of Suc]
  8.1399    show False by simp (blast dest: Suc_neq_Zero surjD)
  8.1400  qed
  8.1401  
  8.1402  (* Often leads to bogus ATP proofs because of reduced type information, hence no_atp *)
  8.1403 -lemma infinite_UNIV_char_0[no_atp]:
  8.1404 -  "\<not> finite (UNIV::'a::semiring_char_0 set)"
  8.1405 +lemma infinite_UNIV_char_0 [no_atp]:
  8.1406 +  "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
  8.1407  proof
  8.1408 -  assume "finite (UNIV::'a set)"
  8.1409 -  with subset_UNIV have "finite (range of_nat::'a set)"
  8.1410 +  assume "finite (UNIV :: 'a set)"
  8.1411 +  with subset_UNIV have "finite (range of_nat :: 'a set)"
  8.1412      by (rule finite_subset)
  8.1413 -  moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
  8.1414 +  moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
  8.1415      by (simp add: inj_on_def)
  8.1416 -  ultimately have "finite (UNIV::nat set)"
  8.1417 +  ultimately have "finite (UNIV :: nat set)"
  8.1418      by (rule finite_imageD)
  8.1419 -  then show "False"
  8.1420 +  then show False
  8.1421      by simp
  8.1422  qed
  8.1423  
     9.1 --- a/src/HOL/GCD.thy	Sat Mar 23 17:11:06 2013 +0100
     9.2 +++ b/src/HOL/GCD.thy	Sat Mar 23 20:50:39 2013 +0100
     9.3 @@ -1462,6 +1462,10 @@
     9.4  
     9.5  subsection {* The complete divisibility lattice *}
     9.6  
     9.7 +lemma semilattice_neutr_set_lcm_one_nat:
     9.8 +  "semilattice_neutr_set lcm (1::nat)"
     9.9 +  by default simp_all
    9.10 +
    9.11  interpretation gcd_semilattice_nat: semilattice_inf gcd "op dvd" "(%m n::nat. m dvd n & ~ n dvd m)"
    9.12  proof
    9.13    case goal3 thus ?case by(metis gcd_unique_nat)
    9.14 @@ -1486,33 +1490,62 @@
    9.15  begin
    9.16  
    9.17  definition
    9.18 -  "Lcm (M::nat set) = (if finite M then Finite_Set.fold lcm 1 M else 0)"
    9.19 +  "Lcm (M::nat set) = (if finite M then semilattice_neutr_set.F lcm 1 M else 0)"
    9.20 +
    9.21 +lemma Lcm_nat_infinite:
    9.22 +  "\<not> finite M \<Longrightarrow> Lcm M = (0::nat)"
    9.23 +  by (simp add: Lcm_nat_def)
    9.24 +
    9.25 +lemma Lcm_nat_empty:
    9.26 +  "Lcm {} = (1::nat)"
    9.27 +proof -
    9.28 +  interpret semilattice_neutr_set lcm "1::nat"
    9.29 +    by (fact semilattice_neutr_set_lcm_one_nat)
    9.30 +  show ?thesis by (simp add: Lcm_nat_def)
    9.31 +qed
    9.32 +
    9.33 +lemma Lcm_nat_insert:
    9.34 +  "Lcm (insert n M) = lcm (n::nat) (Lcm M)"
    9.35 +proof (cases "finite M")
    9.36 +  interpret semilattice_neutr_set lcm "1::nat"
    9.37 +    by (fact semilattice_neutr_set_lcm_one_nat)
    9.38 +  case True
    9.39 +  then show ?thesis by (simp add: Lcm_nat_def)
    9.40 +next
    9.41 +  case False then show ?thesis by (simp add: Lcm_nat_infinite)
    9.42 +qed
    9.43  
    9.44  definition
    9.45    "Gcd (M::nat set) = Lcm {d. \<forall>m\<in>M. d dvd m}"
    9.46  
    9.47  instance ..
    9.48 +
    9.49  end
    9.50  
    9.51  lemma dvd_Lcm_nat [simp]:
    9.52 -  fixes M :: "nat set" assumes "m \<in> M" shows "m dvd Lcm M"
    9.53 -  using lcm_semilattice_nat.sup_le_fold_sup[OF _ assms, of 1]
    9.54 -  by (simp add: Lcm_nat_def)
    9.55 +  fixes M :: "nat set"
    9.56 +  assumes "m \<in> M"
    9.57 +  shows "m dvd Lcm M"
    9.58 +proof (cases "finite M")
    9.59 +  case False then show ?thesis by (simp add: Lcm_nat_infinite)
    9.60 +next
    9.61 +  case True then show ?thesis using assms by (induct M) (auto simp add: Lcm_nat_insert)
    9.62 +qed
    9.63  
    9.64  lemma Lcm_dvd_nat [simp]:
    9.65 -  fixes M :: "nat set" assumes "\<forall>m\<in>M. m dvd n" shows "Lcm M dvd n"
    9.66 +  fixes M :: "nat set"
    9.67 +  assumes "\<forall>m\<in>M. m dvd n"
    9.68 +  shows "Lcm M dvd n"
    9.69  proof (cases "n = 0")
    9.70    assume "n \<noteq> 0"
    9.71    hence "finite {d. d dvd n}" by (rule finite_divisors_nat)
    9.72    moreover have "M \<subseteq> {d. d dvd n}" using assms by fast
    9.73    ultimately have "finite M" by (rule rev_finite_subset)
    9.74 -  thus ?thesis
    9.75 -    using lcm_semilattice_nat.fold_sup_le_sup [OF _ assms, of 1]
    9.76 -    by (simp add: Lcm_nat_def)
    9.77 +  then show ?thesis using assms by (induct M) (simp_all add: Lcm_nat_empty Lcm_nat_insert)
    9.78  qed simp
    9.79  
    9.80  interpretation gcd_lcm_complete_lattice_nat:
    9.81 -  complete_lattice Gcd Lcm gcd "op dvd" "%m n::nat. m dvd n & ~ n dvd m" lcm 1 0
    9.82 +  complete_lattice Gcd Lcm gcd "op dvd" "\<lambda>m n::nat. m dvd n \<and> \<not> n dvd m" lcm 1 0
    9.83  proof
    9.84    case goal1 show ?case by simp
    9.85  next
    10.1 --- a/src/HOL/HOLCF/Compact_Basis.thy	Sat Mar 23 17:11:06 2013 +0100
    10.2 +++ b/src/HOL/HOLCF/Compact_Basis.thy	Sat Mar 23 20:50:39 2013 +0100
    10.3 @@ -96,20 +96,23 @@
    10.4  definition
    10.5    fold_pd ::
    10.6      "('a compact_basis \<Rightarrow> 'b::type) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a pd_basis \<Rightarrow> 'b"
    10.7 -  where "fold_pd g f t = fold1 f (g ` Rep_pd_basis t)"
    10.8 +  where "fold_pd g f t = semilattice_set.F f (g ` Rep_pd_basis t)"
    10.9  
   10.10  lemma fold_pd_PDUnit:
   10.11 -  assumes "class.ab_semigroup_idem_mult f"
   10.12 +  assumes "semilattice f"
   10.13    shows "fold_pd g f (PDUnit x) = g x"
   10.14 -unfolding fold_pd_def Rep_PDUnit by simp
   10.15 +proof -
   10.16 +  from assms interpret semilattice_set f by (rule semilattice_set.intro)
   10.17 +  show ?thesis by (simp add: fold_pd_def Rep_PDUnit)
   10.18 +qed
   10.19  
   10.20  lemma fold_pd_PDPlus:
   10.21 -  assumes "class.ab_semigroup_idem_mult f"
   10.22 +  assumes "semilattice f"
   10.23    shows "fold_pd g f (PDPlus t u) = f (fold_pd g f t) (fold_pd g f u)"
   10.24  proof -
   10.25 -  interpret ab_semigroup_idem_mult f by fact
   10.26 -  show ?thesis unfolding fold_pd_def Rep_PDPlus
   10.27 -    by (simp add: image_Un fold1_Un2)
   10.28 +  from assms interpret semilattice_set f by (rule semilattice_set.intro)
   10.29 +  show ?thesis by (simp add: image_Un fold_pd_def Rep_PDPlus union)
   10.30  qed
   10.31  
   10.32  end
   10.33 +
    11.1 --- a/src/HOL/HOLCF/ConvexPD.thy	Sat Mar 23 17:11:06 2013 +0100
    11.2 +++ b/src/HOL/HOLCF/ConvexPD.thy	Sat Mar 23 20:50:39 2013 +0100
    11.3 @@ -316,7 +316,7 @@
    11.4      (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<natural> y\<cdot>f)"
    11.5  
    11.6  lemma ACI_convex_bind:
    11.7 -  "class.ab_semigroup_idem_mult (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<natural> y\<cdot>f)"
    11.8 +  "semilattice (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<natural> y\<cdot>f)"
    11.9  apply unfold_locales
   11.10  apply (simp add: convex_plus_assoc)
   11.11  apply (simp add: convex_plus_commute)
    12.1 --- a/src/HOL/HOLCF/LowerPD.thy	Sat Mar 23 17:11:06 2013 +0100
    12.2 +++ b/src/HOL/HOLCF/LowerPD.thy	Sat Mar 23 20:50:39 2013 +0100
    12.3 @@ -309,7 +309,7 @@
    12.4      (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<flat> y\<cdot>f)"
    12.5  
    12.6  lemma ACI_lower_bind:
    12.7 -  "class.ab_semigroup_idem_mult (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<flat> y\<cdot>f)"
    12.8 +  "semilattice (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<flat> y\<cdot>f)"
    12.9  apply unfold_locales
   12.10  apply (simp add: lower_plus_assoc)
   12.11  apply (simp add: lower_plus_commute)
    13.1 --- a/src/HOL/HOLCF/UpperPD.thy	Sat Mar 23 17:11:06 2013 +0100
    13.2 +++ b/src/HOL/HOLCF/UpperPD.thy	Sat Mar 23 20:50:39 2013 +0100
    13.3 @@ -302,7 +302,7 @@
    13.4      (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<sharp> y\<cdot>f)"
    13.5  
    13.6  lemma ACI_upper_bind:
    13.7 -  "class.ab_semigroup_idem_mult (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<sharp> y\<cdot>f)"
    13.8 +  "semilattice (\<lambda>x y. \<Lambda> f. x\<cdot>f \<union>\<sharp> y\<cdot>f)"
    13.9  apply unfold_locales
   13.10  apply (simp add: upper_plus_assoc)
   13.11  apply (simp add: upper_plus_commute)
    14.1 --- a/src/HOL/Lattices.thy	Sat Mar 23 17:11:06 2013 +0100
    14.2 +++ b/src/HOL/Lattices.thy	Sat Mar 23 20:50:39 2013 +0100
    14.3 @@ -716,32 +716,6 @@
    14.4  qed
    14.5  
    14.6  
    14.7 -subsection {* @{const min}/@{const max} on linear orders as
    14.8 -  special case of @{const inf}/@{const sup} *}
    14.9 -
   14.10 -sublocale linorder < min_max!: distrib_lattice min less_eq less max
   14.11 -proof
   14.12 -  fix x y z
   14.13 -  show "max x (min y z) = min (max x y) (max x z)"
   14.14 -    by (auto simp add: min_def max_def)
   14.15 -qed (auto simp add: min_def max_def not_le less_imp_le)
   14.16 -
   14.17 -lemma inf_min: "inf = (min \<Colon> 'a\<Colon>{semilattice_inf, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
   14.18 -  by (rule ext)+ (auto intro: antisym)
   14.19 -
   14.20 -lemma sup_max: "sup = (max \<Colon> 'a\<Colon>{semilattice_sup, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
   14.21 -  by (rule ext)+ (auto intro: antisym)
   14.22 -
   14.23 -lemmas le_maxI1 = min_max.sup_ge1
   14.24 -lemmas le_maxI2 = min_max.sup_ge2
   14.25 - 
   14.26 -lemmas min_ac = min_max.inf_assoc min_max.inf_commute
   14.27 -  min_max.inf.left_commute
   14.28 -
   14.29 -lemmas max_ac = min_max.sup_assoc min_max.sup_commute
   14.30 -  min_max.sup.left_commute
   14.31 -
   14.32 -
   14.33  subsection {* Lattice on @{typ bool} *}
   14.34  
   14.35  instantiation bool :: boolean_algebra
    15.1 --- a/src/HOL/Library/Finite_Lattice.thy	Sat Mar 23 17:11:06 2013 +0100
    15.2 +++ b/src/HOL/Library/Finite_Lattice.thy	Sat Mar 23 20:50:39 2013 +0100
    15.3 @@ -39,6 +39,30 @@
    15.4  by (metis finite_UNIV inf_Sup_absorb inf_top_left iso_tuple_UNIV_I)
    15.5  -- "Derived definition of @{const top}."
    15.6  
    15.7 +lemma finite_lattice_complete_Inf_empty:
    15.8 +  "Inf {} = (top :: 'a::finite_lattice_complete)"
    15.9 +  by (simp add: Inf_def)
   15.10 +
   15.11 +lemma finite_lattice_complete_Sup_empty:
   15.12 +  "Sup {} = (bot :: 'a::finite_lattice_complete)"
   15.13 +  by (simp add: Sup_def)
   15.14 +
   15.15 +lemma finite_lattice_complete_Inf_insert:
   15.16 +  fixes A :: "'a::finite_lattice_complete set"
   15.17 +  shows "Inf (insert x A) = inf x (Inf A)"
   15.18 +proof -
   15.19 +  interpret comp_fun_idem "inf :: 'a \<Rightarrow> _" by (fact comp_fun_idem_inf)
   15.20 +  show ?thesis by (simp add: Inf_def)
   15.21 +qed
   15.22 +
   15.23 +lemma finite_lattice_complete_Sup_insert:
   15.24 +  fixes A :: "'a::finite_lattice_complete set"
   15.25 +  shows "Sup (insert x A) = sup x (Sup A)"
   15.26 +proof -
   15.27 +  interpret comp_fun_idem "sup :: 'a \<Rightarrow> _" by (fact comp_fun_idem_sup)
   15.28 +  show ?thesis by (simp add: Sup_def)
   15.29 +qed
   15.30 +
   15.31  text {* The definitional assumptions
   15.32  on the operators @{const Inf} and @{const Sup}
   15.33  of class @{class finite_lattice_complete}
   15.34 @@ -47,19 +71,19 @@
   15.35  
   15.36  lemma finite_lattice_complete_Inf_lower:
   15.37    "(x::'a::finite_lattice_complete) \<in> A \<Longrightarrow> Inf A \<le> x"
   15.38 -unfolding Inf_def by (metis finite_code le_inf_iff fold_inf_le_inf)
   15.39 +  using finite [of A] by (induct A) (auto simp add: finite_lattice_complete_Inf_insert intro: le_infI2)
   15.40  
   15.41  lemma finite_lattice_complete_Inf_greatest:
   15.42    "\<forall>x::'a::finite_lattice_complete \<in> A. z \<le> x \<Longrightarrow> z \<le> Inf A"
   15.43 -unfolding Inf_def by (metis finite_code inf_le_fold_inf inf_top_right)
   15.44 +  using finite [of A] by (induct A) (auto simp add: finite_lattice_complete_Inf_empty finite_lattice_complete_Inf_insert)
   15.45  
   15.46  lemma finite_lattice_complete_Sup_upper:
   15.47    "(x::'a::finite_lattice_complete) \<in> A \<Longrightarrow> Sup A \<ge> x"
   15.48 -unfolding Sup_def by (metis finite_code le_sup_iff sup_le_fold_sup)
   15.49 +  using finite [of A] by (induct A) (auto simp add: finite_lattice_complete_Sup_insert intro: le_supI2)
   15.50  
   15.51  lemma finite_lattice_complete_Sup_least:
   15.52    "\<forall>x::'a::finite_lattice_complete \<in> A. z \<ge> x \<Longrightarrow> z \<ge> Sup A"
   15.53 -unfolding Sup_def by (metis finite_code fold_sup_le_sup sup_bot_right)
   15.54 +  using finite [of A] by (induct A) (auto simp add: finite_lattice_complete_Sup_empty finite_lattice_complete_Sup_insert)
   15.55  
   15.56  instance finite_lattice_complete \<subseteq> complete_lattice
   15.57  proof
    16.1 --- a/src/HOL/Library/Formal_Power_Series.thy	Sat Mar 23 17:11:06 2013 +0100
    16.2 +++ b/src/HOL/Library/Formal_Power_Series.thy	Sat Mar 23 20:50:39 2013 +0100
    16.3 @@ -1022,7 +1022,7 @@
    16.4          also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))" by (simp add: fps_mult_nth)
    16.5          also have "\<dots> = 0" apply (rule setsum_0')
    16.6            apply auto
    16.7 -          apply (case_tac "aa = m")
    16.8 +          apply (case_tac "x = m")
    16.9            using a0
   16.10            apply simp
   16.11            apply (rule H[rule_format])
   16.12 @@ -2270,10 +2270,10 @@
   16.13    unfolding fps_mult_nth
   16.14    apply (rule setsum_0')
   16.15    apply (clarsimp simp add: not_le)
   16.16 -  apply (case_tac "aaa < aa")
   16.17 +  apply (case_tac "x < aa")
   16.18    apply (rule startsby_zero_power_prefix[OF c0, rule_format])
   16.19    apply simp
   16.20 -  apply (subgoal_tac "n - aaa < ba")
   16.21 +  apply (subgoal_tac "n - x < ba")
   16.22    apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
   16.23    apply simp
   16.24    apply arith
    17.1 --- a/src/HOL/Library/Function_Algebras.thy	Sat Mar 23 17:11:06 2013 +0100
    17.2 +++ b/src/HOL/Library/Function_Algebras.thy	Sat Mar 23 20:50:39 2013 +0100
    17.3 @@ -97,9 +97,6 @@
    17.4  instance "fun" :: (type, ab_semigroup_mult) ab_semigroup_mult
    17.5    by default (simp add: fun_eq_iff mult.commute)
    17.6  
    17.7 -instance "fun" :: (type, ab_semigroup_idem_mult) ab_semigroup_idem_mult
    17.8 -  by default (simp add: fun_eq_iff)
    17.9 -
   17.10  instance "fun" :: (type, monoid_mult) monoid_mult
   17.11    by default (simp_all add: fun_eq_iff)
   17.12  
    18.1 --- a/src/HOL/Library/Nat_Bijection.thy	Sat Mar 23 17:11:06 2013 +0100
    18.2 +++ b/src/HOL/Library/Nat_Bijection.thy	Sat Mar 23 20:50:39 2013 +0100
    18.3 @@ -377,7 +377,7 @@
    18.4        by (metis finite_set_decode set_decode_inverse)
    18.5    thus ?thesis using assms
    18.6      apply auto
    18.7 -    apply (simp add: set_encode_def nat_add_commute setsum.F_subset_diff)
    18.8 +    apply (simp add: set_encode_def nat_add_commute setsum.subset_diff)
    18.9      done
   18.10    qed
   18.11    thus ?thesis
   18.12 @@ -385,3 +385,4 @@
   18.13  qed
   18.14  
   18.15  end
   18.16 +
    19.1 --- a/src/HOL/Library/Permutations.thy	Sat Mar 23 17:11:06 2013 +0100
    19.2 +++ b/src/HOL/Library/Permutations.thy	Sat Mar 23 20:50:39 2013 +0100
    19.3 @@ -216,36 +216,36 @@
    19.4  (* Permutations of index set for iterated operations.                        *)
    19.5  (* ------------------------------------------------------------------------- *)
    19.6  
    19.7 -lemma (in ab_semigroup_mult) fold_image_permute: assumes fS: "finite S" and pS: "p permutes S"
    19.8 -  shows "fold_image times f z S = fold_image times (f o p) z S"
    19.9 -  using fold_image_reindex[OF fS subset_inj_on[OF permutes_inj[OF pS], of S, simplified], of f z]
   19.10 -  unfolding permutes_image[OF pS] .
   19.11 -lemma (in ab_semigroup_add) fold_image_permute: assumes fS: "finite S" and pS: "p permutes S"
   19.12 -  shows "fold_image plus f z S = fold_image plus (f o p) z S"
   19.13 -proof-
   19.14 -  interpret ab_semigroup_mult plus apply unfold_locales apply (simp add: add_assoc)
   19.15 -    apply (simp add: add_commute) done
   19.16 -  from fold_image_reindex[OF fS subset_inj_on[OF permutes_inj[OF pS], of S, simplified], of f z]
   19.17 -  show ?thesis
   19.18 -  unfolding permutes_image[OF pS] .
   19.19 +lemma (in comm_monoid_set) permute:
   19.20 +  assumes "p permutes S"
   19.21 +  shows "F g S = F (g o p) S"
   19.22 +proof -
   19.23 +  from `p permutes S` have "inj p" by (rule permutes_inj)
   19.24 +  then have "inj_on p S" by (auto intro: subset_inj_on)
   19.25 +  then have "F g (p ` S) = F (g o p) S" by (rule reindex)
   19.26 +  moreover from `p permutes S` have "p ` S = S" by (rule permutes_image)
   19.27 +  ultimately show ?thesis by simp
   19.28  qed
   19.29  
   19.30 -lemma setsum_permute: assumes pS: "p permutes S"
   19.31 +lemma setsum_permute:
   19.32 +  assumes "p permutes S"
   19.33    shows "setsum f S = setsum (f o p) S"
   19.34 -  unfolding setsum_def using fold_image_permute[of S p f 0] pS by clarsimp
   19.35 +  using assms by (fact setsum.permute)
   19.36  
   19.37 -lemma setsum_permute_natseg:assumes pS: "p permutes {m .. n}"
   19.38 +lemma setsum_permute_natseg:
   19.39 +  assumes pS: "p permutes {m .. n}"
   19.40    shows "setsum f {m .. n} = setsum (f o p) {m .. n}"
   19.41 -  using setsum_permute[OF pS, of f ] pS by blast
   19.42 +  using setsum_permute [OF pS, of f ] pS by blast
   19.43  
   19.44 -lemma setprod_permute: assumes pS: "p permutes S"
   19.45 +lemma setprod_permute:
   19.46 +  assumes "p permutes S"
   19.47    shows "setprod f S = setprod (f o p) S"
   19.48 -  unfolding setprod_def
   19.49 -  using ab_semigroup_mult_class.fold_image_permute[of S p f 1] pS by clarsimp
   19.50 +  using assms by (fact setprod.permute)
   19.51  
   19.52 -lemma setprod_permute_natseg:assumes pS: "p permutes {m .. n}"
   19.53 +lemma setprod_permute_natseg:
   19.54 +  assumes pS: "p permutes {m .. n}"
   19.55    shows "setprod f {m .. n} = setprod (f o p) {m .. n}"
   19.56 -  using setprod_permute[OF pS, of f ] pS by blast
   19.57 +  using setprod_permute [OF pS, of f ] pS by blast
   19.58  
   19.59  (* ------------------------------------------------------------------------- *)
   19.60  (* Various combinations of transpositions with 2, 1 and 0 common elements.   *)
   19.61 @@ -835,7 +835,6 @@
   19.62          by (simp add: o_def)
   19.63        with bc have "b = c \<and> p = q" by blast
   19.64      }
   19.65 -
   19.66      then show "inj_on ?f (insert a S \<times> ?P)"
   19.67        unfolding inj_on_def
   19.68        apply clarify by metis
   19.69 @@ -843,3 +842,4 @@
   19.70  qed
   19.71  
   19.72  end
   19.73 +
    20.1 --- a/src/HOL/Library/RBT_Set.thy	Sat Mar 23 17:11:06 2013 +0100
    20.2 +++ b/src/HOL/Library/RBT_Set.thy	Sat Mar 23 20:50:39 2013 +0100
    20.3 @@ -316,11 +316,10 @@
    20.4    assumes "is_rbt t"
    20.5    shows "rbt_min t = rbt_min_opt t"
    20.6  proof -
    20.7 -  interpret ab_semigroup_idem_mult "(min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_min
    20.8 -    unfolding class.ab_semigroup_idem_mult_def by blast
    20.9 -  show ?thesis
   20.10 -    by (simp add: Min_eqI rbt_min_opt_is_min rbt_min_opt_in_set assms Min_def[symmetric]
   20.11 -      non_empty_rbt_keys fold1_set_fold[symmetric] rbt_min_def rbt_fold1_keys_def)
   20.12 +  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
   20.13 +  with assms show ?thesis
   20.14 +    by (simp add: rbt_min_def rbt_fold1_keys_def rbt_min_opt_is_min
   20.15 +      min_max.Inf_fin.set_eq_fold [symmetric] Min_eqI rbt_min_opt_in_set)
   20.16  qed
   20.17  
   20.18  (* maximum *)
   20.19 @@ -337,12 +336,7 @@
   20.20    fixes xs :: "('a :: linorder) list"
   20.21    assumes "xs \<noteq> []"
   20.22    shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
   20.23 -proof -
   20.24 -  interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
   20.25 -    unfolding class.ab_semigroup_idem_mult_def by blast
   20.26 -  show ?thesis
   20.27 -  using assms by (auto simp add: fold1_set_fold[symmetric])
   20.28 -qed
   20.29 +  using assms by (simp add: min_max.Sup_fin.set_eq_fold [symmetric])
   20.30  
   20.31  lemma rbt_max_simps:
   20.32    assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
   20.33 @@ -416,11 +410,10 @@
   20.34    assumes "is_rbt t"
   20.35    shows "rbt_max t = rbt_max_opt t"
   20.36  proof -
   20.37 -  interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
   20.38 -    unfolding class.ab_semigroup_idem_mult_def by blast
   20.39 -  show ?thesis
   20.40 -    by (simp add: Max_eqI rbt_max_opt_is_max rbt_max_opt_in_set assms Max_def[symmetric]
   20.41 -      non_empty_rbt_keys fold1_set_fold[symmetric] rbt_max_def rbt_fold1_keys_def)
   20.42 +  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
   20.43 +  with assms show ?thesis
   20.44 +    by (simp add: rbt_max_def rbt_fold1_keys_def rbt_max_opt_is_max
   20.45 +      min_max.Sup_fin.set_eq_fold [symmetric] Max_eqI rbt_max_opt_in_set)
   20.46  qed
   20.47  
   20.48  
   20.49 @@ -434,13 +427,13 @@
   20.50    by transfer (simp add: rbt_fold1_keys_def)
   20.51  
   20.52  lemma finite_fold1_fold1_keys:
   20.53 -  assumes "class.ab_semigroup_mult f"
   20.54 -  assumes "\<not> (is_empty t)"
   20.55 -  shows "Finite_Set.fold1 f (Set t) = fold1_keys f t"
   20.56 +  assumes "semilattice f"
   20.57 +  assumes "\<not> is_empty t"
   20.58 +  shows "semilattice_set.F f (Set t) = fold1_keys f t"
   20.59  proof -
   20.60 -  interpret ab_semigroup_mult f by fact
   20.61 +  from `semilattice f` interpret semilattice_set f by (rule semilattice_set.intro)
   20.62    show ?thesis using assms 
   20.63 -    by (auto simp: fold1_keys_def_alt set_keys fold_def_alt fold1_distinct_set_fold non_empty_keys)
   20.64 +    by (auto simp: fold1_keys_def_alt set_keys fold_def_alt non_empty_keys set_eq_fold [symmetric])
   20.65  qed
   20.66  
   20.67  (* minimum *)
   20.68 @@ -658,14 +651,14 @@
   20.69  
   20.70  lemma card_Set [code]:
   20.71    "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
   20.72 -by (auto simp add: card_def fold_image_def intro!: finite_fold_fold_keys) (default, simp) 
   20.73 +  by (auto simp add: card.eq_fold intro: finite_fold_fold_keys comp_fun_commute_const)
   20.74  
   20.75  lemma setsum_Set [code]:
   20.76    "setsum f (Set xs) = fold_keys (plus o f) xs 0"
   20.77  proof -
   20.78    have "comp_fun_commute (\<lambda>x. op + (f x))" by default (auto simp: add_ac)
   20.79    then show ?thesis 
   20.80 -    by (auto simp add: setsum_def fold_image_def finite_fold_fold_keys o_def)
   20.81 +    by (auto simp add: setsum.eq_fold finite_fold_fold_keys o_def)
   20.82  qed
   20.83  
   20.84  definition not_a_singleton_tree  where [code del]: "not_a_singleton_tree x y = x y"
   20.85 @@ -743,11 +736,10 @@
   20.86  lemma Min_fin_set_fold [code]:
   20.87    "Min (Set t) = (if is_empty t then not_non_empty_tree Min (Set t) else r_min_opt t)"
   20.88  proof -
   20.89 -  have *:"(class.ab_semigroup_mult (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_min
   20.90 -    unfolding class.ab_semigroup_idem_mult_def by blast
   20.91 +  have *: "semilattice (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
   20.92 +  with finite_fold1_fold1_keys [OF *, folded Min_def]
   20.93    show ?thesis
   20.94 -    by (auto simp add: Min_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_min_alt_def 
   20.95 -      r_min_eq_r_min_opt[symmetric])  
   20.96 +    by (simp add: not_non_empty_tree_def r_min_alt_def r_min_eq_r_min_opt [symmetric])  
   20.97  qed
   20.98  
   20.99  lemma Inf_fin_set_fold [code]:
  20.100 @@ -781,11 +773,10 @@
  20.101  lemma Max_fin_set_fold [code]:
  20.102    "Max (Set t) = (if is_empty t then not_non_empty_tree Max (Set t) else r_max_opt t)"
  20.103  proof -
  20.104 -  have *:"(class.ab_semigroup_mult (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_max
  20.105 -    unfolding class.ab_semigroup_idem_mult_def by blast
  20.106 +  have *: "semilattice (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
  20.107 +  with finite_fold1_fold1_keys [OF *, folded Max_def]
  20.108    show ?thesis
  20.109 -    by (auto simp add: Max_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_max_alt_def 
  20.110 -      r_max_eq_r_max_opt[symmetric])  
  20.111 +    by (simp add: not_non_empty_tree_def r_max_alt_def r_max_eq_r_max_opt [symmetric])  
  20.112  qed
  20.113  
  20.114  lemma Sup_fin_set_fold [code]:
    21.1 --- a/src/HOL/Library/Saturated.thy	Sat Mar 23 17:11:06 2013 +0100
    21.2 +++ b/src/HOL/Library/Saturated.thy	Sat Mar 23 20:50:39 2013 +0100
    21.3 @@ -207,47 +207,65 @@
    21.4  
    21.5  end
    21.6  
    21.7 -instantiation sat :: (len) complete_lattice
    21.8 +instantiation sat :: (len) "{Inf, Sup}"
    21.9  begin
   21.10  
   21.11  definition
   21.12 -  "Inf (A :: 'a sat set) = Finite_Set.fold min top A"
   21.13 +  "Inf = (semilattice_neutr_set.F min top :: 'a sat set \<Rightarrow> 'a sat)"
   21.14  
   21.15  definition
   21.16 -  "Sup (A :: 'a sat set) = Finite_Set.fold max bot A"
   21.17 +  "Sup = (semilattice_neutr_set.F max bot :: 'a sat set \<Rightarrow> 'a sat)"
   21.18 +
   21.19 +instance ..
   21.20 +
   21.21 +end
   21.22  
   21.23 -instance proof
   21.24 +interpretation Inf_sat!: semilattice_neutr_set min "top :: 'a::len sat"
   21.25 +where
   21.26 +  "semilattice_neutr_set.F min (top :: 'a sat) = Inf"
   21.27 +proof -
   21.28 +  show "semilattice_neutr_set min (top :: 'a sat)" by default (simp add: min_def)
   21.29 +  show "semilattice_neutr_set.F min (top :: 'a sat) = Inf" by (simp add: Inf_sat_def)
   21.30 +qed
   21.31 +
   21.32 +interpretation Sup_sat!: semilattice_neutr_set max "bot :: 'a::len sat"
   21.33 +where
   21.34 +  "semilattice_neutr_set.F max (bot :: 'a sat) = Sup"
   21.35 +proof -
   21.36 +  show "semilattice_neutr_set max (bot :: 'a sat)" by default (simp add: max_def bot.extremum_unique)
   21.37 +  show "semilattice_neutr_set.F max (bot :: 'a sat) = Sup" by (simp add: Sup_sat_def)
   21.38 +qed
   21.39 +
   21.40 +instance sat :: (len) complete_lattice
   21.41 +proof 
   21.42    fix x :: "'a sat"
   21.43    fix A :: "'a sat set"
   21.44    note finite
   21.45    moreover assume "x \<in> A"
   21.46 -  ultimately have "Finite_Set.fold min top A \<le> min x top" by (rule min_max.fold_inf_le_inf)
   21.47 -  then show "Inf A \<le> x" by (simp add: Inf_sat_def)
   21.48 +  ultimately show "Inf A \<le> x"
   21.49 +    by (induct A) (auto intro: min_max.le_infI2)
   21.50  next
   21.51    fix z :: "'a sat"
   21.52    fix A :: "'a sat set"
   21.53    note finite
   21.54    moreover assume z: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
   21.55 -  ultimately have "min z top \<le> Finite_Set.fold min top A" by (blast intro: min_max.inf_le_fold_inf)
   21.56 -  then show "z \<le> Inf A" by (simp add: Inf_sat_def min_def)
   21.57 +  ultimately show "z \<le> Inf A" by (induct A) simp_all
   21.58  next
   21.59    fix x :: "'a sat"
   21.60    fix A :: "'a sat set"
   21.61    note finite
   21.62    moreover assume "x \<in> A"
   21.63 -  ultimately have "max x bot \<le> Finite_Set.fold max bot A" by (rule min_max.sup_le_fold_sup)
   21.64 -  then show "x \<le> Sup A" by (simp add: Sup_sat_def)
   21.65 +  ultimately show "x \<le> Sup A"
   21.66 +    by (induct A) (auto intro: min_max.le_supI2)
   21.67  next
   21.68    fix z :: "'a sat"
   21.69    fix A :: "'a sat set"
   21.70    note finite
   21.71    moreover assume z: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
   21.72 -  ultimately have "Finite_Set.fold max bot A \<le> max z bot" by (blast intro: min_max.fold_sup_le_sup)
   21.73 -  then show "Sup A \<le> z" by (simp add: Sup_sat_def max_def bot_unique)
   21.74 +  ultimately show "Sup A \<le> z" by (induct A) auto
   21.75  qed
   21.76  
   21.77 -end
   21.78 -
   21.79  hide_const (open) sat_of_nat
   21.80  
   21.81  end
   21.82 +
    22.1 --- a/src/HOL/List.thy	Sat Mar 23 17:11:06 2013 +0100
    22.2 +++ b/src/HOL/List.thy	Sat Mar 23 20:50:39 2013 +0100
    22.3 @@ -2734,51 +2734,11 @@
    22.4  
    22.5  lemma (in comp_fun_commute) fold_set_fold_remdups:
    22.6    "Finite_Set.fold f y (set xs) = fold f (remdups xs) y"
    22.7 -  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm insert_absorb)
    22.8 -
    22.9 -lemma (in ab_semigroup_mult) fold1_distinct_set_fold:
   22.10 -  assumes "xs \<noteq> []"
   22.11 -  assumes d: "distinct xs"
   22.12 -  shows "Finite_Set.fold1 times (set xs) = List.fold times (tl xs) (hd xs)"
   22.13 -proof -
   22.14 -  interpret comp_fun_commute times by (fact comp_fun_commute)
   22.15 -  from assms obtain y ys where xs: "xs = y # ys"
   22.16 -    by (cases xs) auto
   22.17 -  then have *: "y \<notin> set ys" using assms by simp
   22.18 -  from xs d have **: "remdups ys = ys"  by safe (induct ys, auto)
   22.19 -  show ?thesis
   22.20 -  proof (cases "set ys = {}")
   22.21 -    case True with xs show ?thesis by simp
   22.22 -  next
   22.23 -    case False
   22.24 -    then have "fold1 times (Set.insert y (set ys)) = Finite_Set.fold times y (set ys)"
   22.25 -      by (simp_all add: fold1_eq_fold *)
   22.26 -    with xs show ?thesis
   22.27 -      by (simp add: fold_set_fold_remdups **)
   22.28 -  qed
   22.29 -qed
   22.30 +  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm insert_absorb)
   22.31  
   22.32  lemma (in comp_fun_idem) fold_set_fold:
   22.33    "Finite_Set.fold f y (set xs) = fold f xs y"
   22.34 -  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm)
   22.35 -
   22.36 -lemma (in ab_semigroup_idem_mult) fold1_set_fold:
   22.37 -  assumes "xs \<noteq> []"
   22.38 -  shows "Finite_Set.fold1 times (set xs) = fold times (tl xs) (hd xs)"
   22.39 -proof -
   22.40 -  interpret comp_fun_idem times by (fact comp_fun_idem)
   22.41 -  from assms obtain y ys where xs: "xs = y # ys"
   22.42 -    by (cases xs) auto
   22.43 -  show ?thesis
   22.44 -  proof (cases "set ys = {}")
   22.45 -    case True with xs show ?thesis by simp
   22.46 -  next
   22.47 -    case False
   22.48 -    then have "fold1 times (insert y (set ys)) = Finite_Set.fold times y (set ys)"
   22.49 -      by (simp only: finite_set fold1_eq_fold_idem)
   22.50 -    with xs show ?thesis by (simp add: fold_set_fold mult_commute)
   22.51 -  qed
   22.52 -qed
   22.53 +  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm)
   22.54  
   22.55  lemma union_set_fold [code]:
   22.56    "set xs \<union> A = fold Set.insert xs A"
   22.57 @@ -2813,49 +2773,18 @@
   22.58    "A \<inter> List.coset xs = fold Set.remove xs A"
   22.59    by (simp add: Diff_eq [symmetric] minus_set_fold)
   22.60  
   22.61 -lemma (in lattice) Inf_fin_set_fold:
   22.62 -  "Inf_fin (set (x # xs)) = fold inf xs x"
   22.63 +lemma (in semilattice_set) set_eq_fold:
   22.64 +  "F (set (x # xs)) = fold f xs x"
   22.65  proof -
   22.66 -  interpret ab_semigroup_idem_mult "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
   22.67 -    by (fact ab_semigroup_idem_mult_inf)
   22.68 -  show ?thesis
   22.69 -    by (simp add: Inf_fin_def fold1_set_fold del: set.simps)
   22.70 -qed
   22.71 -
   22.72 -declare Inf_fin_set_fold [code]
   22.73 -
   22.74 -lemma (in lattice) Sup_fin_set_fold:
   22.75 -  "Sup_fin (set (x # xs)) = fold sup xs x"
   22.76 -proof -
   22.77 -  interpret ab_semigroup_idem_mult "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
   22.78 -    by (fact ab_semigroup_idem_mult_sup)
   22.79 -  show ?thesis
   22.80 -    by (simp add: Sup_fin_def fold1_set_fold del: set.simps)
   22.81 +  interpret comp_fun_idem f
   22.82 +    by default (simp_all add: fun_eq_iff left_commute)
   22.83 +  show ?thesis by (simp add: eq_fold fold_set_fold)
   22.84  qed
   22.85  
   22.86 -declare Sup_fin_set_fold [code]
   22.87 -
   22.88 -lemma (in linorder) Min_fin_set_fold:
   22.89 -  "Min (set (x # xs)) = fold min xs x"
   22.90 -proof -
   22.91 -  interpret ab_semigroup_idem_mult "min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
   22.92 -    by (fact ab_semigroup_idem_mult_min)
   22.93 -  show ?thesis
   22.94 -    by (simp add: Min_def fold1_set_fold del: set.simps)
   22.95 -qed
   22.96 -
   22.97 -declare Min_fin_set_fold [code]
   22.98 -
   22.99 -lemma (in linorder) Max_fin_set_fold:
  22.100 -  "Max (set (x # xs)) = fold max xs x"
  22.101 -proof -
  22.102 -  interpret ab_semigroup_idem_mult "max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
  22.103 -    by (fact ab_semigroup_idem_mult_max)
  22.104 -  show ?thesis
  22.105 -    by (simp add: Max_def fold1_set_fold del: set.simps)
  22.106 -qed
  22.107 -
  22.108 -declare Max_fin_set_fold [code]
  22.109 +declare Inf_fin.set_eq_fold [code]
  22.110 +declare Sup_fin.set_eq_fold [code]
  22.111 +declare min_max.Inf_fin.set_eq_fold [code]
  22.112 +declare min_max.Sup_fin.set_eq_fold [code]
  22.113  
  22.114  lemma (in complete_lattice) Inf_set_fold:
  22.115    "Inf (set xs) = fold inf xs top"
  22.116 @@ -4915,24 +4844,36 @@
  22.117  sets to lists but one should convert in the other direction (via
  22.118  @{const set}). *}
  22.119  
  22.120 +subsubsection {* @{text sorted_list_of_set} *}
  22.121 +
  22.122 +text{* This function maps (finite) linearly ordered sets to sorted
  22.123 +lists. Warning: in most cases it is not a good idea to convert from
  22.124 +sets to lists but one should convert in the other direction (via
  22.125 +@{const set}). *}
  22.126 +
  22.127 +definition (in linorder) sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where
  22.128 +  "sorted_list_of_set = folding.F insort []"
  22.129 +
  22.130 +sublocale linorder < sorted_list_of_set!: folding insort Nil
  22.131 +where
  22.132 +  "folding.F insort [] = sorted_list_of_set"
  22.133 +proof -
  22.134 +  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
  22.135 +  show "folding insort" by default (fact comp_fun_commute)
  22.136 +  show "folding.F insort [] = sorted_list_of_set" by (simp only: sorted_list_of_set_def)
  22.137 +qed
  22.138 +
  22.139  context linorder
  22.140  begin
  22.141  
  22.142 -definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where
  22.143 -"sorted_list_of_set = Finite_Set.fold insort []"
  22.144 -
  22.145 -lemma sorted_list_of_set_empty [simp]:
  22.146 +lemma sorted_list_of_set_empty:
  22.147    "sorted_list_of_set {} = []"
  22.148 -  by (simp add: sorted_list_of_set_def)
  22.149 +  by (fact sorted_list_of_set.empty)
  22.150  
  22.151  lemma sorted_list_of_set_insert [simp]:
  22.152    assumes "finite A"
  22.153    shows "sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))"
  22.154 -proof -
  22.155 -  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
  22.156 -  from assms show ?thesis
  22.157 -    by (simp add: sorted_list_of_set_def fold_insert_remove)
  22.158 -qed
  22.159 +  using assms by (fact sorted_list_of_set.insert_remove)
  22.160  
  22.161  lemma sorted_list_of_set [simp]:
  22.162    "finite A \<Longrightarrow> set (sorted_list_of_set A) = A \<and> sorted (sorted_list_of_set A) 
  22.163 @@ -4943,7 +4884,7 @@
  22.164    "sorted_list_of_set (set xs) = sort (remdups xs)"
  22.165  proof -
  22.166    interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
  22.167 -  show ?thesis by (simp add: sorted_list_of_set_def sort_conv_fold fold_set_fold_remdups)
  22.168 +  show ?thesis by (simp add: sorted_list_of_set.eq_fold sort_conv_fold fold_set_fold_remdups)
  22.169  qed
  22.170  
  22.171  lemma sorted_list_of_set_remove:
    23.1 --- a/src/HOL/MacLaurin.thy	Sat Mar 23 17:11:06 2013 +0100
    23.2 +++ b/src/HOL/MacLaurin.thy	Sat Mar 23 20:50:39 2013 +0100
    23.3 @@ -428,7 +428,7 @@
    23.4  apply (simp (no_asm))
    23.5  apply (simp (no_asm) add: sin_expansion_lemma)
    23.6  apply (force intro!: DERIV_intros)
    23.7 -apply (subst (asm) setsum_0', clarify, case_tac "a", simp, simp)
    23.8 +apply (subst (asm) setsum_0', clarify, case_tac "x", simp, simp)
    23.9  apply (cases n, simp, simp)
   23.10  apply (rule ccontr, simp)
   23.11  apply (drule_tac x = x in spec, simp)
    24.1 --- a/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Sat Mar 23 17:11:06 2013 +0100
    24.2 +++ b/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Sat Mar 23 20:50:39 2013 +0100
    24.3 @@ -188,9 +188,6 @@
    24.4  instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
    24.5    by default (vector mult_commute)
    24.6  
    24.7 -instance vec :: (ab_semigroup_idem_mult, finite) ab_semigroup_idem_mult
    24.8 -  by default (vector mult_idem)
    24.9 -
   24.10  instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
   24.11    by default vector
   24.12  
    25.1 --- a/src/HOL/Multivariate_Analysis/Determinants.thy	Sat Mar 23 17:11:06 2013 +0100
    25.2 +++ b/src/HOL/Multivariate_Analysis/Determinants.thy	Sat Mar 23 20:50:39 2013 +0100
    25.3 @@ -103,18 +103,7 @@
    25.4  lemma setprod_permute:
    25.5    assumes p: "p permutes S"
    25.6    shows "setprod f S = setprod (f o p) S"
    25.7 -proof-
    25.8 -  {assume "\<not> finite S" hence ?thesis by simp}
    25.9 -  moreover
   25.10 -  {assume fS: "finite S"
   25.11 -    then have ?thesis
   25.12 -      apply (simp add: setprod_def cong del:strong_setprod_cong)
   25.13 -      apply (rule ab_semigroup_mult.fold_image_permute)
   25.14 -      apply (auto simp add: p)
   25.15 -      apply unfold_locales
   25.16 -      done}
   25.17 -  ultimately show ?thesis by blast
   25.18 -qed
   25.19 +  using assms by (fact setprod.permute)
   25.20  
   25.21  lemma setproduct_permute_nat_interval: "p permutes {m::nat .. n} ==> setprod f {m..n} = setprod (f o p) {m..n}"
   25.22    by (blast intro!: setprod_permute)
    26.1 --- a/src/HOL/Multivariate_Analysis/Integration.thy	Sat Mar 23 17:11:06 2013 +0100
    26.2 +++ b/src/HOL/Multivariate_Analysis/Integration.thy	Sat Mar 23 20:50:39 2013 +0100
    26.3 @@ -2744,12 +2744,15 @@
    26.4  
    26.5  subsection {* Additivity of content. *}
    26.6  
    26.7 -lemma setsum_iterate:assumes "finite s" shows "setsum f s = iterate op + s f"
    26.8 -proof- have *:"setsum f s = setsum f (support op + f s)"
    26.9 -    apply(rule setsum_mono_zero_right)
   26.10 +lemma setsum_iterate:
   26.11 +  assumes "finite s" shows "setsum f s = iterate op + s f"
   26.12 +proof -
   26.13 +  have *: "setsum f s = setsum f (support op + f s)"
   26.14 +    apply (rule setsum_mono_zero_right)
   26.15      unfolding support_def neutral_monoid using assms by auto
   26.16 -  thus ?thesis unfolding * setsum_def iterate_def fold_image_def fold'_def
   26.17 -    unfolding neutral_monoid . qed
   26.18 +  then show ?thesis unfolding * iterate_def fold'_def setsum.eq_fold
   26.19 +    unfolding neutral_monoid by (simp add: comp_def)
   26.20 +qed
   26.21  
   26.22  lemma additive_content_division: assumes "d division_of {a..b}"
   26.23    shows "setsum content d = content({a..b})"
    27.1 --- a/src/HOL/Number_Theory/UniqueFactorization.thy	Sat Mar 23 17:11:06 2013 +0100
    27.2 +++ b/src/HOL/Number_Theory/UniqueFactorization.thy	Sat Mar 23 20:50:39 2013 +0100
    27.3 @@ -36,36 +36,71 @@
    27.4     "ALL i :# M. P i"? 
    27.5  *)
    27.6  
    27.7 +no_notation times (infixl "*" 70)
    27.8 +no_notation Groups.one ("1")
    27.9 +
   27.10 +locale comm_monoid_mset = comm_monoid
   27.11 +begin
   27.12 +
   27.13 +definition F :: "'a multiset \<Rightarrow> 'a"
   27.14 +where
   27.15 +  eq_fold: "F M = Multiset.fold f 1 M"
   27.16 +
   27.17 +lemma empty [simp]:
   27.18 +  "F {#} = 1"
   27.19 +  by (simp add: eq_fold)
   27.20 +
   27.21 +lemma singleton [simp]:
   27.22 +  "F {#x#} = x"
   27.23 +proof -
   27.24 +  interpret comp_fun_commute
   27.25 +    by default (simp add: fun_eq_iff left_commute)
   27.26 +  show ?thesis by (simp add: eq_fold)
   27.27 +qed
   27.28 +
   27.29 +lemma union [simp]:
   27.30 +  "F (M + N) = F M * F N"
   27.31 +proof -
   27.32 +  interpret comp_fun_commute f
   27.33 +    by default (simp add: fun_eq_iff left_commute)
   27.34 +  show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
   27.35 +qed
   27.36 +
   27.37 +end
   27.38 +
   27.39 +notation times (infixl "*" 70)
   27.40 +notation Groups.one ("1")
   27.41 +
   27.42 +definition (in comm_monoid_mult) msetprod :: "'a multiset \<Rightarrow> 'a"
   27.43 +where
   27.44 +  "msetprod = comm_monoid_mset.F times 1"
   27.45 +
   27.46 +sublocale comm_monoid_mult < msetprod!: comm_monoid_mset times 1
   27.47 +where
   27.48 +  "comm_monoid_mset.F times 1 = msetprod"
   27.49 +proof -
   27.50 +  show "comm_monoid_mset times 1" ..
   27.51 +  from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" by rule
   27.52 +qed
   27.53 +
   27.54  context comm_monoid_mult
   27.55  begin
   27.56  
   27.57 -definition msetprod :: "'a multiset \<Rightarrow> 'a"
   27.58 -where
   27.59 -  "msetprod M = Multiset.fold times 1 M"
   27.60 +lemma msetprod_empty:
   27.61 +  "msetprod {#} = 1"
   27.62 +  by (fact msetprod.empty)
   27.63  
   27.64 -lemma msetprod_empty [simp]:
   27.65 -  "msetprod {#} = 1"
   27.66 -  by (simp add: msetprod_def)
   27.67 -
   27.68 -lemma msetprod_singleton [simp]:
   27.69 +lemma msetprod_singleton:
   27.70    "msetprod {#x#} = x"
   27.71 -proof -
   27.72 -  interpret comp_fun_commute times
   27.73 -    by (fact comp_fun_commute)
   27.74 -  show ?thesis by (simp add: msetprod_def)
   27.75 -qed
   27.76 +  by (fact msetprod.singleton)
   27.77  
   27.78 -lemma msetprod_Un [simp]:
   27.79 +lemma msetprod_Un:
   27.80    "msetprod (A + B) = msetprod A * msetprod B" 
   27.81 -proof -
   27.82 -  interpret comp_fun_commute times
   27.83 -    by (fact comp_fun_commute)
   27.84 -  show ?thesis by (induct B) (simp_all add: msetprod_def mult_ac)
   27.85 -qed
   27.86 +  by (fact msetprod.union)
   27.87  
   27.88  lemma msetprod_multiplicity:
   27.89    "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)"
   27.90 -  by (simp add: msetprod_def setprod_def Multiset.fold_def fold_image_def funpow_times_power)
   27.91 +  by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
   27.92  
   27.93  abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
   27.94  where
   27.95 @@ -111,8 +146,7 @@
   27.96      by arith
   27.97    moreover {
   27.98      assume "n = 1"
   27.99 -    then have "(ALL p : set_of {#}. prime p) & n = (PROD i :# {#}. i)"
  27.100 -        by (auto simp add: msetprod_def)
  27.101 +    then have "(ALL p : set_of {#}. prime p) & n = (PROD i :# {#}. i)" by auto
  27.102    } moreover {
  27.103      assume "n > 1" and "prime n"
  27.104      then have "(ALL p : set_of {# n #}. prime p) & n = (PROD i :# {# n #}. i)"
    28.1 --- a/src/HOL/Old_Number_Theory/Finite2.thy	Sat Mar 23 17:11:06 2013 +0100
    28.2 +++ b/src/HOL/Old_Number_Theory/Finite2.thy	Sat Mar 23 20:50:39 2013 +0100
    28.3 @@ -23,7 +23,7 @@
    28.4    assume "finite S"
    28.5    thus ?thesis using a by induct (simp_all add: zcong_zadd)
    28.6  next
    28.7 -  assume "infinite S" thus ?thesis by(simp add:setsum_def)
    28.8 +  assume "infinite S" thus ?thesis by simp
    28.9  qed
   28.10  
   28.11  lemma setprod_same_function_zcong:
   28.12 @@ -33,7 +33,7 @@
   28.13    assume "finite S"
   28.14    thus ?thesis using a by induct (simp_all add: zcong_zmult)
   28.15  next
   28.16 -  assume "infinite S" thus ?thesis by(simp add:setprod_def)
   28.17 +  assume "infinite S" thus ?thesis by simp
   28.18  qed
   28.19  
   28.20  lemma setsum_const: "finite X ==> setsum (%x. (c :: int)) X = c * int(card X)"
    29.1 --- a/src/HOL/Old_Number_Theory/Pocklington.thy	Sat Mar 23 17:11:06 2013 +0100
    29.2 +++ b/src/HOL/Old_Number_Theory/Pocklington.thy	Sat Mar 23 20:50:39 2013 +0100
    29.3 @@ -566,7 +566,7 @@
    29.4      [x1 = x2] (mod n) \<and> [y1 = y2] (mod n) \<longrightarrow> [x1 * y1 = x2 * y2] (mod n)"
    29.5      by blast
    29.6    have th4:"\<forall>x\<in>S. [a x mod n = a x] (mod n)" by (simp add: modeq_def)
    29.7 -  from fold_image_related[where h="(\<lambda>m. a(m) mod n)" and g=a, OF th1 th3 fS, OF th4] show ?thesis unfolding setprod_def by (simp add: fS)
    29.8 +  from setprod.related [where h="(\<lambda>m. a(m) mod n)" and g=a, OF th1 th3 fS, OF th4] show ?thesis by (simp add: fS)
    29.9  qed
   29.10  
   29.11  lemma nproduct_cmul:
   29.12 @@ -577,7 +577,7 @@
   29.13  lemma coprime_nproduct:
   29.14    assumes fS: "finite S" and Sn: "\<forall>x\<in>S. coprime n (a x)"
   29.15    shows "coprime n (setprod a S)"
   29.16 -  using fS unfolding setprod_def by (rule finite_subset_induct)
   29.17 +  using fS by (rule finite_subset_induct)
   29.18      (insert Sn, auto simp add: coprime_mul)
   29.19  
   29.20  lemma fermat_little: assumes an: "coprime a n"
   29.21 @@ -607,12 +607,8 @@
   29.22        hence hS: "?h ` ?S = ?S"by (auto simp add: image_iff)
   29.23        have "a\<noteq>0" using an n1 nz apply- apply (rule ccontr) by simp
   29.24        hence inj: "inj_on (op * a) ?S" unfolding inj_on_def by simp
   29.25 -
   29.26 -      have eq0: "fold_image op * (?h \<circ> op * a) 1 {m. coprime m n \<and> m < n} =
   29.27 -     fold_image op * (\<lambda>m. m) 1 {m. coprime m n \<and> m < n}"
   29.28 -      proof (rule fold_image_eq_general[where h="?h o (op * a)"])
   29.29 -        show "finite ?S" using fS .
   29.30 -      next
   29.31 +      have eq0: "setprod (?h \<circ> op * a) {m. coprime m n \<and> m < n} = setprod (\<lambda>m. m) {m. coprime m n \<and> m < n}"
   29.32 +      proof (rule setprod.eq_general [where h="?h o (op * a)"])
   29.33          {fix y assume yS: "y \<in> ?S" hence y: "coprime y n" "y < n" by simp_all
   29.34            from cong_solve_unique[OF an nz, of y]
   29.35            obtain x where x:"x < n" "[a * x = y] (mod n)" "\<forall>z. z < n \<and> [a * z = y] (mod n) \<longrightarrow> z=x" by blast
    30.1 --- a/src/HOL/Probability/Fin_Map.thy	Sat Mar 23 17:11:06 2013 +0100
    30.2 +++ b/src/HOL/Probability/Fin_Map.thy	Sat Mar 23 20:50:39 2013 +0100
    30.3 @@ -406,7 +406,7 @@
    30.4  next
    30.5    fix P Q::"'a \<Rightarrow>\<^isub>F 'b"
    30.6    have Max_eq_iff: "\<And>A m. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> (Max A = m) = (m \<in> A \<and> (\<forall>a\<in>A. a \<le> m))"
    30.7 -    by (metis Max.in_idem Max_in max_def min_max.sup.commute order_refl)
    30.8 +    by (auto intro: Max_in Max_eqI)
    30.9    show "dist P Q = 0 \<longleftrightarrow> P = Q"
   30.10      by (auto simp: finmap_eq_iff dist_finmap_def Max_ge_iff finite_proj_diag Max_eq_iff
   30.11        intro!: Max_eqI image_eqI[where x=undefined])