src/HOL/Groups_Big.thy
author wenzelm
Sun Nov 02 18:21:45 2014 +0100 (2014-11-02)
changeset 58889 5b7a9633cfa8
parent 58437 8d124c73c37a
child 59010 ec2b4270a502
permissions -rw-r--r--
modernized header uniformly as section;
haftmann@54744
     1
(*  Title:      HOL/Groups_Big.thy
haftmann@54744
     2
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
haftmann@54744
     3
                with contributions by Jeremy Avigad
haftmann@54744
     4
*)
haftmann@54744
     5
wenzelm@58889
     6
section {* Big sum and product over finite (non-empty) sets *}
haftmann@54744
     7
haftmann@54744
     8
theory Groups_Big
haftmann@54744
     9
imports Finite_Set
haftmann@54744
    10
begin
haftmann@54744
    11
haftmann@54744
    12
subsection {* Generic monoid operation over a set *}
haftmann@54744
    13
haftmann@54744
    14
no_notation times (infixl "*" 70)
haftmann@54744
    15
no_notation Groups.one ("1")
haftmann@54744
    16
haftmann@54744
    17
locale comm_monoid_set = comm_monoid
haftmann@54744
    18
begin
haftmann@54744
    19
haftmann@54744
    20
interpretation comp_fun_commute f
haftmann@54744
    21
  by default (simp add: fun_eq_iff left_commute)
haftmann@54744
    22
haftmann@54745
    23
interpretation comp?: comp_fun_commute "f \<circ> g"
haftmann@54745
    24
  by (fact comp_comp_fun_commute)
haftmann@54744
    25
haftmann@54744
    26
definition F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@54744
    27
where
haftmann@54744
    28
  eq_fold: "F g A = Finite_Set.fold (f \<circ> g) 1 A"
haftmann@54744
    29
haftmann@54744
    30
lemma infinite [simp]:
haftmann@54744
    31
  "\<not> finite A \<Longrightarrow> F g A = 1"
haftmann@54744
    32
  by (simp add: eq_fold)
haftmann@54744
    33
haftmann@54744
    34
lemma empty [simp]:
haftmann@54744
    35
  "F g {} = 1"
haftmann@54744
    36
  by (simp add: eq_fold)
haftmann@54744
    37
haftmann@54744
    38
lemma insert [simp]:
haftmann@54744
    39
  assumes "finite A" and "x \<notin> A"
haftmann@54744
    40
  shows "F g (insert x A) = g x * F g A"
haftmann@54744
    41
  using assms by (simp add: eq_fold)
haftmann@54744
    42
haftmann@54744
    43
lemma remove:
haftmann@54744
    44
  assumes "finite A" and "x \<in> A"
haftmann@54744
    45
  shows "F g A = g x * F g (A - {x})"
haftmann@54744
    46
proof -
haftmann@54744
    47
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@54744
    48
    by (auto dest: mk_disjoint_insert)
haftmann@54744
    49
  moreover from `finite A` A have "finite B" by simp
haftmann@54744
    50
  ultimately show ?thesis by simp
haftmann@54744
    51
qed
haftmann@54744
    52
haftmann@54744
    53
lemma insert_remove:
haftmann@54744
    54
  assumes "finite A"
haftmann@54744
    55
  shows "F g (insert x A) = g x * F g (A - {x})"
haftmann@54744
    56
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@54744
    57
haftmann@54744
    58
lemma neutral:
haftmann@54744
    59
  assumes "\<forall>x\<in>A. g x = 1"
haftmann@54744
    60
  shows "F g A = 1"
haftmann@54744
    61
  using assms by (induct A rule: infinite_finite_induct) simp_all
haftmann@54744
    62
haftmann@54744
    63
lemma neutral_const [simp]:
haftmann@54744
    64
  "F (\<lambda>_. 1) A = 1"
haftmann@54744
    65
  by (simp add: neutral)
haftmann@54744
    66
haftmann@54744
    67
lemma union_inter:
haftmann@54744
    68
  assumes "finite A" and "finite B"
haftmann@54744
    69
  shows "F g (A \<union> B) * F g (A \<inter> B) = F g A * F g B"
haftmann@54744
    70
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
haftmann@54744
    71
using assms proof (induct A)
haftmann@54744
    72
  case empty then show ?case by simp
haftmann@54744
    73
next
haftmann@54744
    74
  case (insert x A) then show ?case
haftmann@54744
    75
    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
haftmann@54744
    76
qed
haftmann@54744
    77
haftmann@54744
    78
corollary union_inter_neutral:
haftmann@54744
    79
  assumes "finite A" and "finite B"
haftmann@54744
    80
  and I0: "\<forall>x \<in> A \<inter> B. g x = 1"
haftmann@54744
    81
  shows "F g (A \<union> B) = F g A * F g B"
haftmann@54744
    82
  using assms by (simp add: union_inter [symmetric] neutral)
haftmann@54744
    83
haftmann@54744
    84
corollary union_disjoint:
haftmann@54744
    85
  assumes "finite A" and "finite B"
haftmann@54744
    86
  assumes "A \<inter> B = {}"
haftmann@54744
    87
  shows "F g (A \<union> B) = F g A * F g B"
haftmann@54744
    88
  using assms by (simp add: union_inter_neutral)
haftmann@54744
    89
haftmann@57418
    90
lemma union_diff2:
haftmann@57418
    91
  assumes "finite A" and "finite B"
haftmann@57418
    92
  shows "F g (A \<union> B) = F g (A - B) * F g (B - A) * F g (A \<inter> B)"
haftmann@57418
    93
proof -
haftmann@57418
    94
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@57418
    95
    by auto
haftmann@57418
    96
  with assms show ?thesis by simp (subst union_disjoint, auto)+
haftmann@57418
    97
qed
haftmann@57418
    98
haftmann@54744
    99
lemma subset_diff:
haftmann@54744
   100
  assumes "B \<subseteq> A" and "finite A"
haftmann@54744
   101
  shows "F g A = F g (A - B) * F g B"
haftmann@54744
   102
proof -
haftmann@54744
   103
  from assms have "finite (A - B)" by auto
haftmann@54744
   104
  moreover from assms have "finite B" by (rule finite_subset)
haftmann@54744
   105
  moreover from assms have "(A - B) \<inter> B = {}" by auto
haftmann@54744
   106
  ultimately have "F g (A - B \<union> B) = F g (A - B) * F g B" by (rule union_disjoint)
haftmann@54744
   107
  moreover from assms have "A \<union> B = A" by auto
haftmann@54744
   108
  ultimately show ?thesis by simp
haftmann@54744
   109
qed
haftmann@54744
   110
haftmann@56545
   111
lemma setdiff_irrelevant:
haftmann@56545
   112
  assumes "finite A"
haftmann@56545
   113
  shows "F g (A - {x. g x = z}) = F g A"
haftmann@56545
   114
  using assms by (induct A) (simp_all add: insert_Diff_if) 
haftmann@58195
   115
haftmann@56545
   116
lemma not_neutral_contains_not_neutral:
haftmann@56545
   117
  assumes "F g A \<noteq> z"
haftmann@56545
   118
  obtains a where "a \<in> A" and "g a \<noteq> z"
haftmann@56545
   119
proof -
haftmann@56545
   120
  from assms have "\<exists>a\<in>A. g a \<noteq> z"
haftmann@56545
   121
  proof (induct A rule: infinite_finite_induct)
haftmann@56545
   122
    case (insert a A)
haftmann@56545
   123
    then show ?case by simp (rule, simp)
haftmann@56545
   124
  qed simp_all
haftmann@56545
   125
  with that show thesis by blast
haftmann@56545
   126
qed
haftmann@56545
   127
haftmann@54744
   128
lemma reindex:
haftmann@54744
   129
  assumes "inj_on h A"
haftmann@54744
   130
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@54744
   131
proof (cases "finite A")
haftmann@54744
   132
  case True
haftmann@54744
   133
  with assms show ?thesis by (simp add: eq_fold fold_image comp_assoc)
haftmann@54744
   134
next
haftmann@54744
   135
  case False with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
haftmann@54744
   136
  with False show ?thesis by simp
haftmann@54744
   137
qed
haftmann@54744
   138
haftmann@54744
   139
lemma cong:
haftmann@54744
   140
  assumes "A = B"
haftmann@54744
   141
  assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x"
haftmann@54744
   142
  shows "F g A = F h B"
hoelzl@57129
   143
  using g_h unfolding `A = B`
hoelzl@57129
   144
  by (induct B rule: infinite_finite_induct) auto
haftmann@54744
   145
haftmann@54744
   146
lemma strong_cong [cong]:
haftmann@54744
   147
  assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x"
haftmann@54744
   148
  shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B"
haftmann@54744
   149
  by (rule cong) (insert assms, simp_all add: simp_implies_def)
haftmann@54744
   150
haftmann@57418
   151
lemma reindex_cong:
haftmann@57418
   152
  assumes "inj_on l B"
haftmann@57418
   153
  assumes "A = l ` B"
haftmann@57418
   154
  assumes "\<And>x. x \<in> B \<Longrightarrow> g (l x) = h x"
haftmann@57418
   155
  shows "F g A = F h B"
haftmann@57418
   156
  using assms by (simp add: reindex)
haftmann@57418
   157
haftmann@54744
   158
lemma UNION_disjoint:
haftmann@54744
   159
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@54744
   160
  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@54744
   161
  shows "F g (UNION I A) = F (\<lambda>x. F g (A x)) I"
haftmann@54744
   162
apply (insert assms)
haftmann@54744
   163
apply (induct rule: finite_induct)
haftmann@54744
   164
apply simp
haftmann@54744
   165
apply atomize
haftmann@54744
   166
apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
haftmann@54744
   167
 prefer 2 apply blast
haftmann@54744
   168
apply (subgoal_tac "A x Int UNION Fa A = {}")
haftmann@54744
   169
 prefer 2 apply blast
haftmann@54744
   170
apply (simp add: union_disjoint)
haftmann@54744
   171
done
haftmann@54744
   172
haftmann@54744
   173
lemma Union_disjoint:
haftmann@54744
   174
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
haftmann@57418
   175
  shows "F g (Union C) = (F \<circ> F) g C"
haftmann@54744
   176
proof cases
haftmann@54744
   177
  assume "finite C"
haftmann@54744
   178
  from UNION_disjoint [OF this assms]
haftmann@56166
   179
  show ?thesis by simp
haftmann@54744
   180
qed (auto dest: finite_UnionD intro: infinite)
haftmann@54744
   181
haftmann@54744
   182
lemma distrib:
haftmann@54744
   183
  "F (\<lambda>x. g x * h x) A = F g A * F h A"
haftmann@54744
   184
  using assms by (induct A rule: infinite_finite_induct) (simp_all add: assoc commute left_commute)
haftmann@54744
   185
haftmann@54744
   186
lemma Sigma:
haftmann@54744
   187
  "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)"
haftmann@54744
   188
apply (subst Sigma_def)
haftmann@54744
   189
apply (subst UNION_disjoint, assumption, simp)
haftmann@54744
   190
 apply blast
haftmann@54744
   191
apply (rule cong)
haftmann@54744
   192
apply rule
haftmann@54744
   193
apply (simp add: fun_eq_iff)
haftmann@54744
   194
apply (subst UNION_disjoint, simp, simp)
haftmann@54744
   195
 apply blast
haftmann@54744
   196
apply (simp add: comp_def)
haftmann@54744
   197
done
haftmann@54744
   198
haftmann@54744
   199
lemma related: 
haftmann@54744
   200
  assumes Re: "R 1 1" 
haftmann@54744
   201
  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
haftmann@54744
   202
  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
haftmann@54744
   203
  shows "R (F h S) (F g S)"
haftmann@54744
   204
  using fS by (rule finite_subset_induct) (insert assms, auto)
haftmann@54744
   205
haftmann@54744
   206
lemma mono_neutral_cong_left:
haftmann@54744
   207
  assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1"
haftmann@54744
   208
  and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T"
haftmann@54744
   209
proof-
haftmann@54744
   210
  have eq: "T = S \<union> (T - S)" using `S \<subseteq> T` by blast
haftmann@54744
   211
  have d: "S \<inter> (T - S) = {}" using `S \<subseteq> T` by blast
haftmann@54744
   212
  from `finite T` `S \<subseteq> T` have f: "finite S" "finite (T - S)"
haftmann@54744
   213
    by (auto intro: finite_subset)
haftmann@54744
   214
  show ?thesis using assms(4)
haftmann@54744
   215
    by (simp add: union_disjoint [OF f d, unfolded eq [symmetric]] neutral [OF assms(3)])
haftmann@54744
   216
qed
haftmann@54744
   217
haftmann@54744
   218
lemma mono_neutral_cong_right:
haftmann@54744
   219
  "\<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>
haftmann@54744
   220
   \<Longrightarrow> F g T = F h S"
haftmann@54744
   221
  by (auto intro!: mono_neutral_cong_left [symmetric])
haftmann@54744
   222
haftmann@54744
   223
lemma mono_neutral_left:
haftmann@54744
   224
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g S = F g T"
haftmann@54744
   225
  by (blast intro: mono_neutral_cong_left)
haftmann@54744
   226
haftmann@54744
   227
lemma mono_neutral_right:
haftmann@54744
   228
  "\<lbrakk> finite T;  S \<subseteq> T;  \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S"
haftmann@54744
   229
  by (blast intro!: mono_neutral_left [symmetric])
haftmann@54744
   230
hoelzl@57129
   231
lemma reindex_bij_betw: "bij_betw h S T \<Longrightarrow> F (\<lambda>x. g (h x)) S = F g T"
hoelzl@57129
   232
  by (auto simp: bij_betw_def reindex)
hoelzl@57129
   233
hoelzl@57129
   234
lemma reindex_bij_witness:
hoelzl@57129
   235
  assumes witness:
hoelzl@57129
   236
    "\<And>a. a \<in> S \<Longrightarrow> i (j a) = a"
hoelzl@57129
   237
    "\<And>a. a \<in> S \<Longrightarrow> j a \<in> T"
hoelzl@57129
   238
    "\<And>b. b \<in> T \<Longrightarrow> j (i b) = b"
hoelzl@57129
   239
    "\<And>b. b \<in> T \<Longrightarrow> i b \<in> S"
hoelzl@57129
   240
  assumes eq:
hoelzl@57129
   241
    "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a"
hoelzl@57129
   242
  shows "F g S = F h T"
hoelzl@57129
   243
proof -
hoelzl@57129
   244
  have "bij_betw j S T"
hoelzl@57129
   245
    using bij_betw_byWitness[where A=S and f=j and f'=i and A'=T] witness by auto
hoelzl@57129
   246
  moreover have "F g S = F (\<lambda>x. h (j x)) S"
hoelzl@57129
   247
    by (intro cong) (auto simp: eq)
hoelzl@57129
   248
  ultimately show ?thesis
hoelzl@57129
   249
    by (simp add: reindex_bij_betw)
hoelzl@57129
   250
qed
hoelzl@57129
   251
hoelzl@57129
   252
lemma reindex_bij_betw_not_neutral:
hoelzl@57129
   253
  assumes fin: "finite S'" "finite T'"
hoelzl@57129
   254
  assumes bij: "bij_betw h (S - S') (T - T')"
hoelzl@57129
   255
  assumes nn:
hoelzl@57129
   256
    "\<And>a. a \<in> S' \<Longrightarrow> g (h a) = z"
hoelzl@57129
   257
    "\<And>b. b \<in> T' \<Longrightarrow> g b = z"
hoelzl@57129
   258
  shows "F (\<lambda>x. g (h x)) S = F g T"
hoelzl@57129
   259
proof -
hoelzl@57129
   260
  have [simp]: "finite S \<longleftrightarrow> finite T"
hoelzl@57129
   261
    using bij_betw_finite[OF bij] fin by auto
hoelzl@57129
   262
hoelzl@57129
   263
  show ?thesis
hoelzl@57129
   264
  proof cases
hoelzl@57129
   265
    assume "finite S"
hoelzl@57129
   266
    with nn have "F (\<lambda>x. g (h x)) S = F (\<lambda>x. g (h x)) (S - S')"
hoelzl@57129
   267
      by (intro mono_neutral_cong_right) auto
hoelzl@57129
   268
    also have "\<dots> = F g (T - T')"
hoelzl@57129
   269
      using bij by (rule reindex_bij_betw)
hoelzl@57129
   270
    also have "\<dots> = F g T"
hoelzl@57129
   271
      using nn `finite S` by (intro mono_neutral_cong_left) auto
hoelzl@57129
   272
    finally show ?thesis .
hoelzl@57129
   273
  qed simp
hoelzl@57129
   274
qed
hoelzl@57129
   275
haftmann@57418
   276
lemma reindex_nontrivial:
haftmann@57418
   277
  assumes "finite A"
haftmann@57418
   278
  and nz: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> h x = h y \<Longrightarrow> g (h x) = 1"
haftmann@57418
   279
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@57418
   280
proof (subst reindex_bij_betw_not_neutral [symmetric])
haftmann@57418
   281
  show "bij_betw h (A - {x \<in> A. (g \<circ> h) x = 1}) (h ` A - h ` {x \<in> A. (g \<circ> h) x = 1})"
haftmann@57418
   282
    using nz by (auto intro!: inj_onI simp: bij_betw_def)
haftmann@57418
   283
qed (insert `finite A`, auto)
haftmann@57418
   284
hoelzl@57129
   285
lemma reindex_bij_witness_not_neutral:
hoelzl@57129
   286
  assumes fin: "finite S'" "finite T'"
hoelzl@57129
   287
  assumes witness:
hoelzl@57129
   288
    "\<And>a. a \<in> S - S' \<Longrightarrow> i (j a) = a"
hoelzl@57129
   289
    "\<And>a. a \<in> S - S' \<Longrightarrow> j a \<in> T - T'"
hoelzl@57129
   290
    "\<And>b. b \<in> T - T' \<Longrightarrow> j (i b) = b"
hoelzl@57129
   291
    "\<And>b. b \<in> T - T' \<Longrightarrow> i b \<in> S - S'"
hoelzl@57129
   292
  assumes nn:
hoelzl@57129
   293
    "\<And>a. a \<in> S' \<Longrightarrow> g a = z"
hoelzl@57129
   294
    "\<And>b. b \<in> T' \<Longrightarrow> h b = z"
hoelzl@57129
   295
  assumes eq:
hoelzl@57129
   296
    "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a"
hoelzl@57129
   297
  shows "F g S = F h T"
hoelzl@57129
   298
proof -
hoelzl@57129
   299
  have bij: "bij_betw j (S - (S' \<inter> S)) (T - (T' \<inter> T))"
hoelzl@57129
   300
    using witness by (intro bij_betw_byWitness[where f'=i]) auto
hoelzl@57129
   301
  have F_eq: "F g S = F (\<lambda>x. h (j x)) S"
hoelzl@57129
   302
    by (intro cong) (auto simp: eq)
hoelzl@57129
   303
  show ?thesis
hoelzl@57129
   304
    unfolding F_eq using fin nn eq
hoelzl@57129
   305
    by (intro reindex_bij_betw_not_neutral[OF _ _ bij]) auto
hoelzl@57129
   306
qed
hoelzl@57129
   307
haftmann@54744
   308
lemma delta: 
haftmann@54744
   309
  assumes fS: "finite S"
haftmann@54744
   310
  shows "F (\<lambda>k. if k = a then b k else 1) S = (if a \<in> S then b a else 1)"
haftmann@54744
   311
proof-
haftmann@54744
   312
  let ?f = "(\<lambda>k. if k=a then b k else 1)"
haftmann@54744
   313
  { assume a: "a \<notin> S"
haftmann@54744
   314
    hence "\<forall>k\<in>S. ?f k = 1" by simp
haftmann@54744
   315
    hence ?thesis  using a by simp }
haftmann@54744
   316
  moreover
haftmann@54744
   317
  { assume a: "a \<in> S"
haftmann@54744
   318
    let ?A = "S - {a}"
haftmann@54744
   319
    let ?B = "{a}"
haftmann@54744
   320
    have eq: "S = ?A \<union> ?B" using a by blast 
haftmann@54744
   321
    have dj: "?A \<inter> ?B = {}" by simp
haftmann@54744
   322
    from fS have fAB: "finite ?A" "finite ?B" by auto  
haftmann@54744
   323
    have "F ?f S = F ?f ?A * F ?f ?B"
haftmann@54744
   324
      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]]
haftmann@54744
   325
      by simp
haftmann@54744
   326
    then have ?thesis using a by simp }
haftmann@54744
   327
  ultimately show ?thesis by blast
haftmann@54744
   328
qed
haftmann@54744
   329
haftmann@54744
   330
lemma delta': 
haftmann@54744
   331
  assumes fS: "finite S"
haftmann@54744
   332
  shows "F (\<lambda>k. if a = k then b k else 1) S = (if a \<in> S then b a else 1)"
haftmann@54744
   333
  using delta [OF fS, of a b, symmetric] by (auto intro: cong)
haftmann@54744
   334
haftmann@54744
   335
lemma If_cases:
haftmann@54744
   336
  fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
haftmann@54744
   337
  assumes fA: "finite A"
haftmann@54744
   338
  shows "F (\<lambda>x. if P x then h x else g x) A =
haftmann@54744
   339
    F h (A \<inter> {x. P x}) * F g (A \<inter> - {x. P x})"
haftmann@54744
   340
proof -
haftmann@54744
   341
  have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}" 
haftmann@54744
   342
          "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}" 
haftmann@54744
   343
    by blast+
haftmann@54744
   344
  from fA 
haftmann@54744
   345
  have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
haftmann@54744
   346
  let ?g = "\<lambda>x. if P x then h x else g x"
haftmann@54744
   347
  from union_disjoint [OF f a(2), of ?g] a(1)
haftmann@54744
   348
  show ?thesis
haftmann@54744
   349
    by (subst (1 2) cong) simp_all
haftmann@54744
   350
qed
haftmann@54744
   351
haftmann@54744
   352
lemma cartesian_product:
haftmann@54744
   353
   "F (\<lambda>x. F (g x) B) A = F (split g) (A <*> B)"
haftmann@54744
   354
apply (rule sym)
haftmann@54744
   355
apply (cases "finite A") 
haftmann@54744
   356
 apply (cases "finite B") 
haftmann@54744
   357
  apply (simp add: Sigma)
haftmann@54744
   358
 apply (cases "A={}", simp)
haftmann@54744
   359
 apply simp
haftmann@54744
   360
apply (auto intro: infinite dest: finite_cartesian_productD2)
haftmann@54744
   361
apply (cases "B = {}") apply (auto intro: infinite dest: finite_cartesian_productD1)
haftmann@54744
   362
done
haftmann@54744
   363
haftmann@57418
   364
lemma inter_restrict:
haftmann@57418
   365
  assumes "finite A"
haftmann@57418
   366
  shows "F g (A \<inter> B) = F (\<lambda>x. if x \<in> B then g x else 1) A"
haftmann@57418
   367
proof -
haftmann@57418
   368
  let ?g = "\<lambda>x. if x \<in> A \<inter> B then g x else 1"
haftmann@57418
   369
  have "\<forall>i\<in>A - A \<inter> B. (if i \<in> A \<inter> B then g i else 1) = 1"
haftmann@57418
   370
   by simp
haftmann@57418
   371
  moreover have "A \<inter> B \<subseteq> A" by blast
haftmann@57418
   372
  ultimately have "F ?g (A \<inter> B) = F ?g A" using `finite A`
haftmann@57418
   373
    by (intro mono_neutral_left) auto
haftmann@57418
   374
  then show ?thesis by simp
haftmann@57418
   375
qed
haftmann@57418
   376
haftmann@57418
   377
lemma inter_filter:
haftmann@57418
   378
  "finite A \<Longrightarrow> F g {x \<in> A. P x} = F (\<lambda>x. if P x then g x else 1) A"
haftmann@57418
   379
  by (simp add: inter_restrict [symmetric, of A "{x. P x}" g, simplified mem_Collect_eq] Int_def)
haftmann@57418
   380
haftmann@57418
   381
lemma Union_comp:
haftmann@57418
   382
  assumes "\<forall>A \<in> B. finite A"
haftmann@57418
   383
    and "\<And>A1 A2 x. A1 \<in> B \<Longrightarrow> A2 \<in> B  \<Longrightarrow> A1 \<noteq> A2 \<Longrightarrow> x \<in> A1 \<Longrightarrow> x \<in> A2 \<Longrightarrow> g x = 1"
haftmann@57418
   384
  shows "F g (\<Union>B) = (F \<circ> F) g B"
haftmann@57418
   385
using assms proof (induct B rule: infinite_finite_induct)
haftmann@57418
   386
  case (infinite A)
haftmann@57418
   387
  then have "\<not> finite (\<Union>A)" by (blast dest: finite_UnionD)
haftmann@57418
   388
  with infinite show ?case by simp
haftmann@57418
   389
next
haftmann@57418
   390
  case empty then show ?case by simp
haftmann@57418
   391
next
haftmann@57418
   392
  case (insert A B)
haftmann@57418
   393
  then have "finite A" "finite B" "finite (\<Union>B)" "A \<notin> B"
haftmann@57418
   394
    and "\<forall>x\<in>A \<inter> \<Union>B. g x = 1"
haftmann@57418
   395
    and H: "F g (\<Union>B) = (F o F) g B" by auto
haftmann@57418
   396
  then have "F g (A \<union> \<Union>B) = F g A * F g (\<Union>B)"
haftmann@57418
   397
    by (simp add: union_inter_neutral)
haftmann@57418
   398
  with `finite B` `A \<notin> B` show ?case
haftmann@57418
   399
    by (simp add: H)
haftmann@57418
   400
qed
haftmann@57418
   401
haftmann@57418
   402
lemma commute:
haftmann@57418
   403
  "F (\<lambda>i. F (g i) B) A = F (\<lambda>j. F (\<lambda>i. g i j) A) B"
haftmann@57418
   404
  unfolding cartesian_product
haftmann@57418
   405
  by (rule reindex_bij_witness [where i = "\<lambda>(i, j). (j, i)" and j = "\<lambda>(i, j). (j, i)"]) auto
haftmann@57418
   406
haftmann@57418
   407
lemma commute_restrict:
haftmann@57418
   408
  "finite A \<Longrightarrow> finite B \<Longrightarrow>
haftmann@57418
   409
    F (\<lambda>x. F (g x) {y. y \<in> B \<and> R x y}) A = F (\<lambda>y. F (\<lambda>x. g x y) {x. x \<in> A \<and> R x y}) B"
haftmann@57418
   410
  by (simp add: inter_filter) (rule commute)
haftmann@57418
   411
haftmann@57418
   412
lemma Plus:
haftmann@57418
   413
  fixes A :: "'b set" and B :: "'c set"
haftmann@57418
   414
  assumes fin: "finite A" "finite B"
haftmann@57418
   415
  shows "F g (A <+> B) = F (g \<circ> Inl) A * F (g \<circ> Inr) B"
haftmann@57418
   416
proof -
haftmann@57418
   417
  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
haftmann@57418
   418
  moreover from fin have "finite (Inl ` A :: ('b + 'c) set)" "finite (Inr ` B :: ('b + 'c) set)"
haftmann@57418
   419
    by auto
haftmann@57418
   420
  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('b + 'c) set)" by auto
haftmann@57418
   421
  moreover have "inj_on (Inl :: 'b \<Rightarrow> 'b + 'c) A" "inj_on (Inr :: 'c \<Rightarrow> 'b + 'c) B"
haftmann@57418
   422
    by (auto intro: inj_onI)
haftmann@57418
   423
  ultimately show ?thesis using fin
haftmann@57418
   424
    by (simp add: union_disjoint reindex)
haftmann@57418
   425
qed
haftmann@57418
   426
haftmann@58195
   427
lemma same_carrier:
haftmann@58195
   428
  assumes "finite C"
haftmann@58195
   429
  assumes subset: "A \<subseteq> C" "B \<subseteq> C"
haftmann@58195
   430
  assumes trivial: "\<And>a. a \<in> C - A \<Longrightarrow> g a = 1" "\<And>b. b \<in> C - B \<Longrightarrow> h b = 1"
haftmann@58195
   431
  shows "F g A = F h B \<longleftrightarrow> F g C = F h C"
haftmann@58195
   432
proof -
haftmann@58195
   433
  from `finite C` subset have
haftmann@58195
   434
    "finite A" and "finite B" and "finite (C - A)" and "finite (C - B)"
haftmann@58195
   435
    by (auto elim: finite_subset)
haftmann@58195
   436
  from subset have [simp]: "A - (C - A) = A" by auto
haftmann@58195
   437
  from subset have [simp]: "B - (C - B) = B" by auto
haftmann@58195
   438
  from subset have "C = A \<union> (C - A)" by auto
haftmann@58195
   439
  then have "F g C = F g (A \<union> (C - A))" by simp
haftmann@58195
   440
  also have "\<dots> = F g (A - (C - A)) * F g (C - A - A) * F g (A \<inter> (C - A))"
haftmann@58195
   441
    using `finite A` `finite (C - A)` by (simp only: union_diff2)
haftmann@58195
   442
  finally have P: "F g C = F g A" using trivial by simp
haftmann@58195
   443
  from subset have "C = B \<union> (C - B)" by auto
haftmann@58195
   444
  then have "F h C = F h (B \<union> (C - B))" by simp
haftmann@58195
   445
  also have "\<dots> = F h (B - (C - B)) * F h (C - B - B) * F h (B \<inter> (C - B))"
haftmann@58195
   446
    using `finite B` `finite (C - B)` by (simp only: union_diff2)
haftmann@58195
   447
  finally have Q: "F h C = F h B" using trivial by simp
haftmann@58195
   448
  from P Q show ?thesis by simp
haftmann@58195
   449
qed
haftmann@58195
   450
haftmann@58195
   451
lemma same_carrierI:
haftmann@58195
   452
  assumes "finite C"
haftmann@58195
   453
  assumes subset: "A \<subseteq> C" "B \<subseteq> C"
haftmann@58195
   454
  assumes trivial: "\<And>a. a \<in> C - A \<Longrightarrow> g a = 1" "\<And>b. b \<in> C - B \<Longrightarrow> h b = 1"
haftmann@58195
   455
  assumes "F g C = F h C"
haftmann@58195
   456
  shows "F g A = F h B"
haftmann@58195
   457
  using assms same_carrier [of C A B] by simp
haftmann@58195
   458
haftmann@54744
   459
end
haftmann@54744
   460
haftmann@54744
   461
notation times (infixl "*" 70)
haftmann@54744
   462
notation Groups.one ("1")
haftmann@54744
   463
haftmann@54744
   464
haftmann@54744
   465
subsection {* Generalized summation over a set *}
haftmann@54744
   466
haftmann@54744
   467
context comm_monoid_add
haftmann@54744
   468
begin
haftmann@54744
   469
haftmann@54744
   470
definition setsum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@54744
   471
where
haftmann@54744
   472
  "setsum = comm_monoid_set.F plus 0"
haftmann@54744
   473
haftmann@54744
   474
sublocale setsum!: comm_monoid_set plus 0
haftmann@54744
   475
where
haftmann@54744
   476
  "comm_monoid_set.F plus 0 = setsum"
haftmann@54744
   477
proof -
haftmann@54744
   478
  show "comm_monoid_set plus 0" ..
haftmann@54744
   479
  then interpret setsum!: comm_monoid_set plus 0 .
haftmann@54744
   480
  from setsum_def show "comm_monoid_set.F plus 0 = setsum" by rule
haftmann@54744
   481
qed
haftmann@54744
   482
haftmann@54744
   483
abbreviation
haftmann@54744
   484
  Setsum ("\<Sum>_" [1000] 999) where
haftmann@54744
   485
  "\<Sum>A \<equiv> setsum (%x. x) A"
haftmann@54744
   486
haftmann@54744
   487
end
haftmann@54744
   488
haftmann@54744
   489
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
haftmann@54744
   490
written @{text"\<Sum>x\<in>A. e"}. *}
haftmann@54744
   491
haftmann@54744
   492
syntax
haftmann@54744
   493
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
haftmann@54744
   494
syntax (xsymbols)
haftmann@54744
   495
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
haftmann@54744
   496
syntax (HTML output)
haftmann@54744
   497
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
haftmann@54744
   498
haftmann@54744
   499
translations -- {* Beware of argument permutation! *}
haftmann@54744
   500
  "SUM i:A. b" == "CONST setsum (%i. b) A"
haftmann@54744
   501
  "\<Sum>i\<in>A. b" == "CONST setsum (%i. b) A"
haftmann@54744
   502
haftmann@54744
   503
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
haftmann@54744
   504
 @{text"\<Sum>x|P. e"}. *}
haftmann@54744
   505
haftmann@54744
   506
syntax
haftmann@54744
   507
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
haftmann@54744
   508
syntax (xsymbols)
haftmann@54744
   509
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
haftmann@54744
   510
syntax (HTML output)
haftmann@54744
   511
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
haftmann@54744
   512
haftmann@54744
   513
translations
haftmann@54744
   514
  "SUM x|P. t" => "CONST setsum (%x. t) {x. P}"
haftmann@54744
   515
  "\<Sum>x|P. t" => "CONST setsum (%x. t) {x. P}"
haftmann@54744
   516
haftmann@54744
   517
print_translation {*
haftmann@54744
   518
let
haftmann@54744
   519
  fun setsum_tr' [Abs (x, Tx, t), Const (@{const_syntax Collect}, _) $ Abs (y, Ty, P)] =
haftmann@54744
   520
        if x <> y then raise Match
haftmann@54744
   521
        else
haftmann@54744
   522
          let
haftmann@54744
   523
            val x' = Syntax_Trans.mark_bound_body (x, Tx);
haftmann@54744
   524
            val t' = subst_bound (x', t);
haftmann@54744
   525
            val P' = subst_bound (x', P);
haftmann@54744
   526
          in
haftmann@54744
   527
            Syntax.const @{syntax_const "_qsetsum"} $ Syntax_Trans.mark_bound_abs (x, Tx) $ P' $ t'
haftmann@54744
   528
          end
haftmann@54744
   529
    | setsum_tr' _ = raise Match;
haftmann@54744
   530
in [(@{const_syntax setsum}, K setsum_tr')] end
haftmann@54744
   531
*}
haftmann@54744
   532
haftmann@57418
   533
text {* TODO generalization candidates *}
haftmann@54744
   534
haftmann@57418
   535
lemma setsum_image_gen:
haftmann@57418
   536
  assumes fS: "finite S"
haftmann@57418
   537
  shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
haftmann@57418
   538
proof-
haftmann@57418
   539
  { fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto }
haftmann@57418
   540
  hence "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
haftmann@57418
   541
    by simp
haftmann@57418
   542
  also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
haftmann@57418
   543
    by (rule setsum.commute_restrict [OF fS finite_imageI [OF fS]])
haftmann@57418
   544
  finally show ?thesis .
haftmann@54744
   545
qed
haftmann@54744
   546
haftmann@54744
   547
haftmann@54744
   548
subsubsection {* Properties in more restricted classes of structures *}
haftmann@54744
   549
haftmann@54744
   550
lemma setsum_Un: "finite A ==> finite B ==>
haftmann@54744
   551
  (setsum f (A Un B) :: 'a :: ab_group_add) =
haftmann@54744
   552
   setsum f A + setsum f B - setsum f (A Int B)"
haftmann@57418
   553
by (subst setsum.union_inter [symmetric], auto simp add: algebra_simps)
haftmann@54744
   554
haftmann@54744
   555
lemma setsum_Un2:
haftmann@54744
   556
  assumes "finite (A \<union> B)"
haftmann@54744
   557
  shows "setsum f (A \<union> B) = setsum f (A - B) + setsum f (B - A) + setsum f (A \<inter> B)"
haftmann@54744
   558
proof -
haftmann@54744
   559
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@54744
   560
    by auto
haftmann@57418
   561
  with assms show ?thesis by simp (subst setsum.union_disjoint, auto)+
haftmann@54744
   562
qed
haftmann@54744
   563
haftmann@54744
   564
lemma setsum_diff1: "finite A \<Longrightarrow>
haftmann@54744
   565
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
haftmann@54744
   566
  (if a:A then setsum f A - f a else setsum f A)"
haftmann@54744
   567
by (erule finite_induct) (auto simp add: insert_Diff_if)
haftmann@54744
   568
haftmann@54744
   569
lemma setsum_diff:
haftmann@54744
   570
  assumes le: "finite A" "B \<subseteq> A"
haftmann@54744
   571
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
haftmann@54744
   572
proof -
haftmann@54744
   573
  from le have finiteB: "finite B" using finite_subset by auto
haftmann@54744
   574
  show ?thesis using finiteB le
haftmann@54744
   575
  proof induct
haftmann@54744
   576
    case empty
haftmann@54744
   577
    thus ?case by auto
haftmann@54744
   578
  next
haftmann@54744
   579
    case (insert x F)
haftmann@54744
   580
    thus ?case using le finiteB 
haftmann@54744
   581
      by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
haftmann@54744
   582
  qed
haftmann@54744
   583
qed
haftmann@54744
   584
haftmann@54744
   585
lemma setsum_mono:
haftmann@54744
   586
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, ordered_ab_semigroup_add}))"
haftmann@54744
   587
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
haftmann@54744
   588
proof (cases "finite K")
haftmann@54744
   589
  case True
haftmann@54744
   590
  thus ?thesis using le
haftmann@54744
   591
  proof induct
haftmann@54744
   592
    case empty
haftmann@54744
   593
    thus ?case by simp
haftmann@54744
   594
  next
haftmann@54744
   595
    case insert
haftmann@54744
   596
    thus ?case using add_mono by fastforce
haftmann@54744
   597
  qed
haftmann@54744
   598
next
haftmann@54744
   599
  case False then show ?thesis by simp
haftmann@54744
   600
qed
haftmann@54744
   601
haftmann@54744
   602
lemma setsum_strict_mono:
haftmann@54744
   603
  fixes f :: "'a \<Rightarrow> 'b::{ordered_cancel_ab_semigroup_add,comm_monoid_add}"
haftmann@54744
   604
  assumes "finite A"  "A \<noteq> {}"
haftmann@54744
   605
    and "!!x. x:A \<Longrightarrow> f x < g x"
haftmann@54744
   606
  shows "setsum f A < setsum g A"
haftmann@54744
   607
  using assms
haftmann@54744
   608
proof (induct rule: finite_ne_induct)
haftmann@54744
   609
  case singleton thus ?case by simp
haftmann@54744
   610
next
haftmann@54744
   611
  case insert thus ?case by (auto simp: add_strict_mono)
haftmann@54744
   612
qed
haftmann@54744
   613
haftmann@54744
   614
lemma setsum_strict_mono_ex1:
haftmann@54744
   615
fixes f :: "'a \<Rightarrow> 'b::{comm_monoid_add, ordered_cancel_ab_semigroup_add}"
haftmann@54744
   616
assumes "finite A" and "ALL x:A. f x \<le> g x" and "EX a:A. f a < g a"
haftmann@54744
   617
shows "setsum f A < setsum g A"
haftmann@54744
   618
proof-
haftmann@54744
   619
  from assms(3) obtain a where a: "a:A" "f a < g a" by blast
haftmann@54744
   620
  have "setsum f A = setsum f ((A-{a}) \<union> {a})"
haftmann@54744
   621
    by(simp add:insert_absorb[OF `a:A`])
haftmann@54744
   622
  also have "\<dots> = setsum f (A-{a}) + setsum f {a}"
haftmann@57418
   623
    using `finite A` by(subst setsum.union_disjoint) auto
haftmann@54744
   624
  also have "setsum f (A-{a}) \<le> setsum g (A-{a})"
haftmann@54744
   625
    by(rule setsum_mono)(simp add: assms(2))
haftmann@54744
   626
  also have "setsum f {a} < setsum g {a}" using a by simp
haftmann@54744
   627
  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) \<union> {a})"
haftmann@57418
   628
    using `finite A` by(subst setsum.union_disjoint[symmetric]) auto
haftmann@54744
   629
  also have "\<dots> = setsum g A" by(simp add:insert_absorb[OF `a:A`])
haftmann@54744
   630
  finally show ?thesis by (auto simp add: add_right_mono add_strict_left_mono)
haftmann@54744
   631
qed
haftmann@54744
   632
haftmann@54744
   633
lemma setsum_negf:
haftmann@54744
   634
  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
haftmann@54744
   635
proof (cases "finite A")
haftmann@54744
   636
  case True thus ?thesis by (induct set: finite) auto
haftmann@54744
   637
next
haftmann@54744
   638
  case False thus ?thesis by simp
haftmann@54744
   639
qed
haftmann@54744
   640
haftmann@54744
   641
lemma setsum_subtractf:
haftmann@54744
   642
  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
haftmann@54744
   643
    setsum f A - setsum g A"
haftmann@57418
   644
  using setsum.distrib [of f "- g" A] by (simp add: setsum_negf)
haftmann@54744
   645
haftmann@54744
   646
lemma setsum_nonneg:
haftmann@54744
   647
  assumes nn: "\<forall>x\<in>A. (0::'a::{ordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
haftmann@54744
   648
  shows "0 \<le> setsum f A"
haftmann@54744
   649
proof (cases "finite A")
haftmann@54744
   650
  case True thus ?thesis using nn
haftmann@54744
   651
  proof induct
haftmann@54744
   652
    case empty then show ?case by simp
haftmann@54744
   653
  next
haftmann@54744
   654
    case (insert x F)
haftmann@54744
   655
    then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
haftmann@54744
   656
    with insert show ?case by simp
haftmann@54744
   657
  qed
haftmann@54744
   658
next
haftmann@54744
   659
  case False thus ?thesis by simp
haftmann@54744
   660
qed
haftmann@54744
   661
haftmann@54744
   662
lemma setsum_nonpos:
haftmann@54744
   663
  assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{ordered_ab_semigroup_add,comm_monoid_add})"
haftmann@54744
   664
  shows "setsum f A \<le> 0"
haftmann@54744
   665
proof (cases "finite A")
haftmann@54744
   666
  case True thus ?thesis using np
haftmann@54744
   667
  proof induct
haftmann@54744
   668
    case empty then show ?case by simp
haftmann@54744
   669
  next
haftmann@54744
   670
    case (insert x F)
haftmann@54744
   671
    then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
haftmann@54744
   672
    with insert show ?case by simp
haftmann@54744
   673
  qed
haftmann@54744
   674
next
haftmann@54744
   675
  case False thus ?thesis by simp
haftmann@54744
   676
qed
haftmann@54744
   677
haftmann@54744
   678
lemma setsum_nonneg_leq_bound:
haftmann@54744
   679
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
haftmann@54744
   680
  assumes "finite s" "\<And>i. i \<in> s \<Longrightarrow> f i \<ge> 0" "(\<Sum>i \<in> s. f i) = B" "i \<in> s"
haftmann@54744
   681
  shows "f i \<le> B"
haftmann@54744
   682
proof -
haftmann@54744
   683
  have "0 \<le> (\<Sum> i \<in> s - {i}. f i)" and "0 \<le> f i"
haftmann@54744
   684
    using assms by (auto intro!: setsum_nonneg)
haftmann@54744
   685
  moreover
haftmann@54744
   686
  have "(\<Sum> i \<in> s - {i}. f i) + f i = B"
haftmann@54744
   687
    using assms by (simp add: setsum_diff1)
haftmann@54744
   688
  ultimately show ?thesis by auto
haftmann@54744
   689
qed
haftmann@54744
   690
haftmann@54744
   691
lemma setsum_nonneg_0:
haftmann@54744
   692
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
haftmann@54744
   693
  assumes "finite s" and pos: "\<And> i. i \<in> s \<Longrightarrow> f i \<ge> 0"
haftmann@54744
   694
  and "(\<Sum> i \<in> s. f i) = 0" and i: "i \<in> s"
haftmann@54744
   695
  shows "f i = 0"
haftmann@54744
   696
  using setsum_nonneg_leq_bound[OF assms] pos[OF i] by auto
haftmann@54744
   697
haftmann@54744
   698
lemma setsum_mono2:
haftmann@54744
   699
fixes f :: "'a \<Rightarrow> 'b :: ordered_comm_monoid_add"
haftmann@54744
   700
assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
haftmann@54744
   701
shows "setsum f A \<le> setsum f B"
haftmann@54744
   702
proof -
haftmann@54744
   703
  have "setsum f A \<le> setsum f A + setsum f (B-A)"
haftmann@54744
   704
    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
haftmann@54744
   705
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
haftmann@57418
   706
    by (simp add: setsum.union_disjoint del:Un_Diff_cancel)
haftmann@54744
   707
  also have "A \<union> (B-A) = B" using sub by blast
haftmann@54744
   708
  finally show ?thesis .
haftmann@54744
   709
qed
haftmann@54744
   710
haftmann@57418
   711
lemma setsum_le_included:
haftmann@57418
   712
  fixes f :: "'a \<Rightarrow> 'b::ordered_comm_monoid_add"
haftmann@57418
   713
  assumes "finite s" "finite t"
haftmann@57418
   714
  and "\<forall>y\<in>t. 0 \<le> g y" "(\<forall>x\<in>s. \<exists>y\<in>t. i y = x \<and> f x \<le> g y)"
haftmann@57418
   715
  shows "setsum f s \<le> setsum g t"
haftmann@57418
   716
proof -
haftmann@57418
   717
  have "setsum f s \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) s"
haftmann@57418
   718
  proof (rule setsum_mono)
haftmann@57418
   719
    fix y assume "y \<in> s"
haftmann@57418
   720
    with assms obtain z where z: "z \<in> t" "y = i z" "f y \<le> g z" by auto
haftmann@57418
   721
    with assms show "f y \<le> setsum g {x \<in> t. i x = y}" (is "?A y \<le> ?B y")
haftmann@57418
   722
      using order_trans[of "?A (i z)" "setsum g {z}" "?B (i z)", intro]
haftmann@57418
   723
      by (auto intro!: setsum_mono2)
haftmann@57418
   724
  qed
haftmann@57418
   725
  also have "... \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) (i ` t)"
haftmann@57418
   726
    using assms(2-4) by (auto intro!: setsum_mono2 setsum_nonneg)
haftmann@57418
   727
  also have "... \<le> setsum g t"
haftmann@57418
   728
    using assms by (auto simp: setsum_image_gen[symmetric])
haftmann@57418
   729
  finally show ?thesis .
haftmann@57418
   730
qed
haftmann@57418
   731
haftmann@54744
   732
lemma setsum_mono3: "finite B ==> A <= B ==> 
haftmann@54744
   733
    ALL x: B - A. 
haftmann@54744
   734
      0 <= ((f x)::'a::{comm_monoid_add,ordered_ab_semigroup_add}) ==>
haftmann@54744
   735
        setsum f A <= setsum f B"
haftmann@54744
   736
  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")
haftmann@54744
   737
  apply (erule ssubst)
haftmann@54744
   738
  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")
haftmann@54744
   739
  apply simp
haftmann@54744
   740
  apply (rule add_left_mono)
haftmann@54744
   741
  apply (erule setsum_nonneg)
haftmann@57418
   742
  apply (subst setsum.union_disjoint [THEN sym])
haftmann@54744
   743
  apply (erule finite_subset, assumption)
haftmann@54744
   744
  apply (rule finite_subset)
haftmann@54744
   745
  prefer 2
haftmann@54744
   746
  apply assumption
haftmann@54744
   747
  apply (auto simp add: sup_absorb2)
haftmann@54744
   748
done
haftmann@54744
   749
haftmann@54744
   750
lemma setsum_right_distrib: 
haftmann@54744
   751
  fixes f :: "'a => ('b::semiring_0)"
haftmann@54744
   752
  shows "r * setsum f A = setsum (%n. r * f n) A"
haftmann@54744
   753
proof (cases "finite A")
haftmann@54744
   754
  case True
haftmann@54744
   755
  thus ?thesis
haftmann@54744
   756
  proof induct
haftmann@54744
   757
    case empty thus ?case by simp
haftmann@54744
   758
  next
haftmann@54744
   759
    case (insert x A) thus ?case by (simp add: distrib_left)
haftmann@54744
   760
  qed
haftmann@54744
   761
next
haftmann@54744
   762
  case False thus ?thesis by simp
haftmann@54744
   763
qed
haftmann@54744
   764
haftmann@54744
   765
lemma setsum_left_distrib:
haftmann@54744
   766
  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
haftmann@54744
   767
proof (cases "finite A")
haftmann@54744
   768
  case True
haftmann@54744
   769
  then show ?thesis
haftmann@54744
   770
  proof induct
haftmann@54744
   771
    case empty thus ?case by simp
haftmann@54744
   772
  next
haftmann@54744
   773
    case (insert x A) thus ?case by (simp add: distrib_right)
haftmann@54744
   774
  qed
haftmann@54744
   775
next
haftmann@54744
   776
  case False thus ?thesis by simp
haftmann@54744
   777
qed
haftmann@54744
   778
haftmann@54744
   779
lemma setsum_divide_distrib:
haftmann@54744
   780
  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
haftmann@54744
   781
proof (cases "finite A")
haftmann@54744
   782
  case True
haftmann@54744
   783
  then show ?thesis
haftmann@54744
   784
  proof induct
haftmann@54744
   785
    case empty thus ?case by simp
haftmann@54744
   786
  next
haftmann@54744
   787
    case (insert x A) thus ?case by (simp add: add_divide_distrib)
haftmann@54744
   788
  qed
haftmann@54744
   789
next
haftmann@54744
   790
  case False thus ?thesis by simp
haftmann@54744
   791
qed
haftmann@54744
   792
haftmann@54744
   793
lemma setsum_abs[iff]: 
haftmann@54744
   794
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
haftmann@54744
   795
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
haftmann@54744
   796
proof (cases "finite A")
haftmann@54744
   797
  case True
haftmann@54744
   798
  thus ?thesis
haftmann@54744
   799
  proof induct
haftmann@54744
   800
    case empty thus ?case by simp
haftmann@54744
   801
  next
haftmann@54744
   802
    case (insert x A)
haftmann@54744
   803
    thus ?case by (auto intro: abs_triangle_ineq order_trans)
haftmann@54744
   804
  qed
haftmann@54744
   805
next
haftmann@54744
   806
  case False thus ?thesis by simp
haftmann@54744
   807
qed
haftmann@54744
   808
haftmann@54744
   809
lemma setsum_abs_ge_zero[iff]: 
haftmann@54744
   810
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
haftmann@54744
   811
  shows "0 \<le> setsum (%i. abs(f i)) A"
haftmann@54744
   812
proof (cases "finite A")
haftmann@54744
   813
  case True
haftmann@54744
   814
  thus ?thesis
haftmann@54744
   815
  proof induct
haftmann@54744
   816
    case empty thus ?case by simp
haftmann@54744
   817
  next
haftmann@54744
   818
    case (insert x A) thus ?case by auto
haftmann@54744
   819
  qed
haftmann@54744
   820
next
haftmann@54744
   821
  case False thus ?thesis by simp
haftmann@54744
   822
qed
haftmann@54744
   823
haftmann@54744
   824
lemma abs_setsum_abs[simp]: 
haftmann@54744
   825
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
haftmann@54744
   826
  shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
haftmann@54744
   827
proof (cases "finite A")
haftmann@54744
   828
  case True
haftmann@54744
   829
  thus ?thesis
haftmann@54744
   830
  proof induct
haftmann@54744
   831
    case empty thus ?case by simp
haftmann@54744
   832
  next
haftmann@54744
   833
    case (insert a A)
haftmann@54744
   834
    hence "\<bar>\<Sum>a\<in>insert a A. \<bar>f a\<bar>\<bar> = \<bar>\<bar>f a\<bar> + (\<Sum>a\<in>A. \<bar>f a\<bar>)\<bar>" by simp
haftmann@54744
   835
    also have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>"  using insert by simp
haftmann@54744
   836
    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
haftmann@54744
   837
      by (simp del: abs_of_nonneg)
haftmann@54744
   838
    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
haftmann@54744
   839
    finally show ?case .
haftmann@54744
   840
  qed
haftmann@54744
   841
next
haftmann@54744
   842
  case False thus ?thesis by simp
haftmann@54744
   843
qed
haftmann@54744
   844
haftmann@54744
   845
lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
haftmann@54744
   846
  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
haftmann@57418
   847
  unfolding setsum.remove [OF assms] by auto
haftmann@54744
   848
haftmann@54744
   849
lemma setsum_product:
haftmann@54744
   850
  fixes f :: "'a => ('b::semiring_0)"
haftmann@54744
   851
  shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
haftmann@57418
   852
  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum.commute)
haftmann@54744
   853
haftmann@54744
   854
lemma setsum_mult_setsum_if_inj:
haftmann@54744
   855
fixes f :: "'a => ('b::semiring_0)"
haftmann@54744
   856
shows "inj_on (%(a,b). f a * g b) (A \<times> B) ==>
haftmann@54744
   857
  setsum f A * setsum g B = setsum id {f a * g b|a b. a:A & b:B}"
haftmann@57418
   858
by(auto simp: setsum_product setsum.cartesian_product
haftmann@57418
   859
        intro!:  setsum.reindex_cong[symmetric])
haftmann@54744
   860
haftmann@54744
   861
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
haftmann@54744
   862
apply (case_tac "finite A")
haftmann@54744
   863
 prefer 2 apply simp
haftmann@54744
   864
apply (erule rev_mp)
haftmann@54744
   865
apply (erule finite_induct, auto)
haftmann@54744
   866
done
haftmann@54744
   867
haftmann@54744
   868
lemma setsum_eq_0_iff [simp]:
haftmann@54744
   869
  "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
haftmann@54744
   870
  by (induct set: finite) auto
haftmann@54744
   871
haftmann@54744
   872
lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
haftmann@54744
   873
  setsum f A = Suc 0 \<longleftrightarrow> (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
haftmann@54744
   874
apply(erule finite_induct)
haftmann@54744
   875
apply (auto simp add:add_is_1)
haftmann@54744
   876
done
haftmann@54744
   877
haftmann@54744
   878
lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
haftmann@54744
   879
haftmann@54744
   880
lemma setsum_Un_nat: "finite A ==> finite B ==>
haftmann@54744
   881
  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
haftmann@54744
   882
  -- {* For the natural numbers, we have subtraction. *}
haftmann@57418
   883
by (subst setsum.union_inter [symmetric], auto simp add: algebra_simps)
haftmann@54744
   884
haftmann@54744
   885
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
haftmann@54744
   886
  (if a:A then setsum f A - f a else setsum f A)"
haftmann@54744
   887
apply (case_tac "finite A")
haftmann@54744
   888
 prefer 2 apply simp
haftmann@54744
   889
apply (erule finite_induct)
haftmann@54744
   890
 apply (auto simp add: insert_Diff_if)
haftmann@54744
   891
apply (drule_tac a = a in mk_disjoint_insert, auto)
haftmann@54744
   892
done
haftmann@54744
   893
haftmann@54744
   894
lemma setsum_diff_nat: 
haftmann@54744
   895
assumes "finite B" and "B \<subseteq> A"
haftmann@54744
   896
shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
haftmann@54744
   897
using assms
haftmann@54744
   898
proof induct
haftmann@54744
   899
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
haftmann@54744
   900
next
haftmann@54744
   901
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
haftmann@54744
   902
    and xFinA: "insert x F \<subseteq> A"
haftmann@54744
   903
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
haftmann@54744
   904
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
haftmann@54744
   905
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
haftmann@54744
   906
    by (simp add: setsum_diff1_nat)
haftmann@54744
   907
  from xFinA have "F \<subseteq> A" by simp
haftmann@54744
   908
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
haftmann@54744
   909
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
haftmann@54744
   910
    by simp
haftmann@54744
   911
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
haftmann@54744
   912
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
haftmann@54744
   913
    by simp
haftmann@54744
   914
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
haftmann@54744
   915
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
haftmann@54744
   916
    by simp
haftmann@54744
   917
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
haftmann@54744
   918
qed
haftmann@54744
   919
haftmann@54744
   920
lemma setsum_comp_morphism:
haftmann@54744
   921
  assumes "h 0 = 0" and "\<And>x y. h (x + y) = h x + h y"
haftmann@54744
   922
  shows "setsum (h \<circ> g) A = h (setsum g A)"
haftmann@54744
   923
proof (cases "finite A")
haftmann@54744
   924
  case False then show ?thesis by (simp add: assms)
haftmann@54744
   925
next
haftmann@54744
   926
  case True then show ?thesis by (induct A) (simp_all add: assms)
haftmann@54744
   927
qed
haftmann@54744
   928
haftmann@54744
   929
haftmann@54744
   930
subsubsection {* Cardinality as special case of @{const setsum} *}
haftmann@54744
   931
haftmann@54744
   932
lemma card_eq_setsum:
haftmann@54744
   933
  "card A = setsum (\<lambda>x. 1) A"
haftmann@54744
   934
proof -
haftmann@54744
   935
  have "plus \<circ> (\<lambda>_. Suc 0) = (\<lambda>_. Suc)"
haftmann@54744
   936
    by (simp add: fun_eq_iff)
haftmann@54744
   937
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) = Finite_Set.fold (\<lambda>_. Suc)"
haftmann@54744
   938
    by (rule arg_cong)
haftmann@54744
   939
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) 0 A = Finite_Set.fold (\<lambda>_. Suc) 0 A"
haftmann@54744
   940
    by (blast intro: fun_cong)
haftmann@54744
   941
  then show ?thesis by (simp add: card.eq_fold setsum.eq_fold)
haftmann@54744
   942
qed
haftmann@54744
   943
haftmann@54744
   944
lemma setsum_constant [simp]:
haftmann@54744
   945
  "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
haftmann@54744
   946
apply (cases "finite A")
haftmann@54744
   947
apply (erule finite_induct)
haftmann@54744
   948
apply (auto simp add: algebra_simps)
haftmann@54744
   949
done
haftmann@54744
   950
nipkow@58349
   951
lemma setsum_Suc: "setsum (%x. Suc(f x)) A = setsum f A + card A"
nipkow@58349
   952
using setsum.distrib[of f "%_. 1" A] by(simp)
nipkow@58349
   953
haftmann@54744
   954
lemma setsum_bounded:
haftmann@54744
   955
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, ordered_ab_semigroup_add})"
haftmann@54744
   956
  shows "setsum f A \<le> of_nat (card A) * K"
haftmann@54744
   957
proof (cases "finite A")
haftmann@54744
   958
  case True
haftmann@54744
   959
  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
haftmann@54744
   960
next
haftmann@54744
   961
  case False thus ?thesis by simp
haftmann@54744
   962
qed
haftmann@54744
   963
haftmann@54744
   964
lemma card_UN_disjoint:
haftmann@54744
   965
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@54744
   966
    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@54744
   967
  shows "card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
haftmann@54744
   968
proof -
haftmann@54744
   969
  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)" by simp
haftmann@57418
   970
  with assms show ?thesis by (simp add: card_eq_setsum setsum.UNION_disjoint del: setsum_constant)
haftmann@54744
   971
qed
haftmann@54744
   972
haftmann@54744
   973
lemma card_Union_disjoint:
haftmann@54744
   974
  "finite C ==> (ALL A:C. finite A) ==>
haftmann@54744
   975
   (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {})
haftmann@54744
   976
   ==> card (Union C) = setsum card C"
haftmann@54744
   977
apply (frule card_UN_disjoint [of C id])
haftmann@56166
   978
apply simp_all
haftmann@54744
   979
done
haftmann@54744
   980
haftmann@57418
   981
lemma setsum_multicount_gen:
haftmann@57418
   982
  assumes "finite s" "finite t" "\<forall>j\<in>t. (card {i\<in>s. R i j} = k j)"
haftmann@57418
   983
  shows "setsum (\<lambda>i. (card {j\<in>t. R i j})) s = setsum k t" (is "?l = ?r")
haftmann@57418
   984
proof-
haftmann@57418
   985
  have "?l = setsum (\<lambda>i. setsum (\<lambda>x.1) {j\<in>t. R i j}) s" by auto
haftmann@57418
   986
  also have "\<dots> = ?r" unfolding setsum.commute_restrict [OF assms(1-2)]
haftmann@57418
   987
    using assms(3) by auto
haftmann@57418
   988
  finally show ?thesis .
haftmann@57418
   989
qed
haftmann@57418
   990
haftmann@57418
   991
lemma setsum_multicount:
haftmann@57418
   992
  assumes "finite S" "finite T" "\<forall>j\<in>T. (card {i\<in>S. R i j} = k)"
haftmann@57418
   993
  shows "setsum (\<lambda>i. card {j\<in>T. R i j}) S = k * card T" (is "?l = ?r")
haftmann@57418
   994
proof-
haftmann@57418
   995
  have "?l = setsum (\<lambda>i. k) T" by (rule setsum_multicount_gen) (auto simp: assms)
haftmann@57512
   996
  also have "\<dots> = ?r" by (simp add: mult.commute)
haftmann@57418
   997
  finally show ?thesis by auto
haftmann@57418
   998
qed
haftmann@57418
   999
haftmann@58437
  1000
lemma (in ordered_comm_monoid_add) setsum_pos: 
haftmann@58437
  1001
  "finite I \<Longrightarrow> I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> 0 < f i) \<Longrightarrow> 0 < setsum f I"
haftmann@58437
  1002
  by (induct I rule: finite_ne_induct) (auto intro: add_pos_pos)
haftmann@58437
  1003
haftmann@54744
  1004
haftmann@54744
  1005
subsubsection {* Cardinality of products *}
haftmann@54744
  1006
haftmann@54744
  1007
lemma card_SigmaI [simp]:
haftmann@54744
  1008
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
haftmann@54744
  1009
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
haftmann@57418
  1010
by(simp add: card_eq_setsum setsum.Sigma del:setsum_constant)
haftmann@54744
  1011
haftmann@54744
  1012
(*
haftmann@54744
  1013
lemma SigmaI_insert: "y \<notin> A ==>
haftmann@54744
  1014
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
haftmann@54744
  1015
  by auto
haftmann@54744
  1016
*)
haftmann@54744
  1017
haftmann@54744
  1018
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
haftmann@54744
  1019
  by (cases "finite A \<and> finite B")
haftmann@54744
  1020
    (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)
haftmann@54744
  1021
haftmann@54744
  1022
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
haftmann@54744
  1023
by (simp add: card_cartesian_product)
haftmann@54744
  1024
haftmann@54744
  1025
haftmann@54744
  1026
subsection {* Generalized product over a set *}
haftmann@54744
  1027
haftmann@54744
  1028
context comm_monoid_mult
haftmann@54744
  1029
begin
haftmann@54744
  1030
haftmann@54744
  1031
definition setprod :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@54744
  1032
where
haftmann@54744
  1033
  "setprod = comm_monoid_set.F times 1"
haftmann@54744
  1034
haftmann@54744
  1035
sublocale setprod!: comm_monoid_set times 1
haftmann@54744
  1036
where
haftmann@54744
  1037
  "comm_monoid_set.F times 1 = setprod"
haftmann@54744
  1038
proof -
haftmann@54744
  1039
  show "comm_monoid_set times 1" ..
haftmann@54744
  1040
  then interpret setprod!: comm_monoid_set times 1 .
haftmann@54744
  1041
  from setprod_def show "comm_monoid_set.F times 1 = setprod" by rule
haftmann@54744
  1042
qed
haftmann@54744
  1043
haftmann@54744
  1044
abbreviation
haftmann@54744
  1045
  Setprod ("\<Prod>_" [1000] 999) where
haftmann@54744
  1046
  "\<Prod>A \<equiv> setprod (\<lambda>x. x) A"
haftmann@54744
  1047
haftmann@54744
  1048
end
haftmann@54744
  1049
haftmann@54744
  1050
syntax
haftmann@54744
  1051
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
haftmann@54744
  1052
syntax (xsymbols)
haftmann@54744
  1053
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
haftmann@54744
  1054
syntax (HTML output)
haftmann@54744
  1055
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
haftmann@54744
  1056
haftmann@54744
  1057
translations -- {* Beware of argument permutation! *}
haftmann@54744
  1058
  "PROD i:A. b" == "CONST setprod (%i. b) A" 
haftmann@54744
  1059
  "\<Prod>i\<in>A. b" == "CONST setprod (%i. b) A" 
haftmann@54744
  1060
haftmann@54744
  1061
text{* Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter
haftmann@54744
  1062
 @{text"\<Prod>x|P. e"}. *}
haftmann@54744
  1063
haftmann@54744
  1064
syntax
haftmann@54744
  1065
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)
haftmann@54744
  1066
syntax (xsymbols)
haftmann@54744
  1067
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
haftmann@54744
  1068
syntax (HTML output)
haftmann@54744
  1069
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
haftmann@54744
  1070
haftmann@54744
  1071
translations
haftmann@54744
  1072
  "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"
haftmann@54744
  1073
  "\<Prod>x|P. t" => "CONST setprod (%x. t) {x. P}"
haftmann@54744
  1074
haftmann@54744
  1075
haftmann@54744
  1076
subsubsection {* Properties in more restricted classes of structures *}
haftmann@54744
  1077
haftmann@54744
  1078
lemma setprod_zero:
haftmann@54744
  1079
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
haftmann@54744
  1080
apply (induct set: finite, force, clarsimp)
haftmann@54744
  1081
apply (erule disjE, auto)
haftmann@54744
  1082
done
haftmann@54744
  1083
haftmann@54744
  1084
lemma setprod_zero_iff[simp]: "finite A ==> 
haftmann@54744
  1085
  (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =
haftmann@54744
  1086
  (EX x: A. f x = 0)"
haftmann@54744
  1087
by (erule finite_induct, auto simp:no_zero_divisors)
haftmann@54744
  1088
haftmann@54744
  1089
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
haftmann@54744
  1090
  (setprod f (A Un B) :: 'a ::{field})
haftmann@54744
  1091
   = setprod f A * setprod f B / setprod f (A Int B)"
haftmann@57418
  1092
by (subst setprod.union_inter [symmetric], auto)
haftmann@54744
  1093
haftmann@54744
  1094
lemma setprod_nonneg [rule_format]:
haftmann@54744
  1095
   "(ALL x: A. (0::'a::linordered_semidom) \<le> f x) --> 0 \<le> setprod f A"
nipkow@56536
  1096
by (cases "finite A", induct set: finite, simp_all)
haftmann@54744
  1097
haftmann@54744
  1098
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::linordered_semidom) < f x)
haftmann@54744
  1099
  --> 0 < setprod f A"
nipkow@56544
  1100
by (cases "finite A", induct set: finite, simp_all)
haftmann@54744
  1101
haftmann@54744
  1102
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
haftmann@54744
  1103
  (setprod f (A - {a}) :: 'a :: {field}) =
haftmann@54744
  1104
  (if a:A then setprod f A / f a else setprod f A)"
haftmann@54744
  1105
  by (erule finite_induct) (auto simp add: insert_Diff_if)
haftmann@54744
  1106
haftmann@54744
  1107
lemma setprod_inversef: 
haftmann@54744
  1108
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
haftmann@54744
  1109
  shows "finite A ==> setprod (inverse \<circ> f) A = inverse (setprod f A)"
haftmann@54744
  1110
by (erule finite_induct) auto
haftmann@54744
  1111
haftmann@54744
  1112
lemma setprod_dividef:
haftmann@54744
  1113
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
haftmann@54744
  1114
  shows "finite A
haftmann@54744
  1115
    ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
haftmann@54744
  1116
apply (subgoal_tac
haftmann@54744
  1117
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
haftmann@54744
  1118
apply (erule ssubst)
haftmann@54744
  1119
apply (subst divide_inverse)
haftmann@57418
  1120
apply (subst setprod.distrib)
haftmann@54744
  1121
apply (subst setprod_inversef, assumption+, rule refl)
haftmann@57418
  1122
apply (rule setprod.cong, rule refl)
haftmann@54744
  1123
apply (subst divide_inverse, auto)
haftmann@54744
  1124
done
haftmann@54744
  1125
haftmann@54744
  1126
lemma setprod_dvd_setprod [rule_format]: 
haftmann@54744
  1127
    "(ALL x : A. f x dvd g x) \<longrightarrow> setprod f A dvd setprod g A"
haftmann@54744
  1128
  apply (cases "finite A")
haftmann@54744
  1129
  apply (induct set: finite)
haftmann@54744
  1130
  apply (auto simp add: dvd_def)
haftmann@54744
  1131
  apply (rule_tac x = "k * ka" in exI)
haftmann@54744
  1132
  apply (simp add: algebra_simps)
haftmann@54744
  1133
done
haftmann@54744
  1134
haftmann@54744
  1135
lemma setprod_dvd_setprod_subset:
haftmann@54744
  1136
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> setprod f A dvd setprod f B"
haftmann@54744
  1137
  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")
haftmann@54744
  1138
  apply (unfold dvd_def, blast)
haftmann@57418
  1139
  apply (subst setprod.union_disjoint [symmetric])
haftmann@57418
  1140
  apply (auto elim: finite_subset intro: setprod.cong)
haftmann@54744
  1141
done
haftmann@54744
  1142
haftmann@54744
  1143
lemma setprod_dvd_setprod_subset2:
haftmann@54744
  1144
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> ALL x : A. (f x::'a::comm_semiring_1) dvd g x \<Longrightarrow> 
haftmann@54744
  1145
      setprod f A dvd setprod g B"
haftmann@54744
  1146
  apply (rule dvd_trans)
haftmann@54744
  1147
  apply (rule setprod_dvd_setprod, erule (1) bspec)
haftmann@54744
  1148
  apply (erule (1) setprod_dvd_setprod_subset)
haftmann@54744
  1149
done
haftmann@54744
  1150
haftmann@54744
  1151
lemma dvd_setprod: "finite A \<Longrightarrow> i:A \<Longrightarrow> 
haftmann@54744
  1152
    (f i ::'a::comm_semiring_1) dvd setprod f A"
haftmann@54744
  1153
by (induct set: finite) (auto intro: dvd_mult)
haftmann@54744
  1154
haftmann@54744
  1155
lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) \<longrightarrow> 
haftmann@54744
  1156
    (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"
haftmann@54744
  1157
  apply (cases "finite A")
haftmann@54744
  1158
  apply (induct set: finite)
haftmann@54744
  1159
  apply auto
haftmann@54744
  1160
done
haftmann@54744
  1161
haftmann@54744
  1162
lemma setprod_mono:
haftmann@54744
  1163
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linordered_semidom"
haftmann@54744
  1164
  assumes "\<forall>i\<in>A. 0 \<le> f i \<and> f i \<le> g i"
haftmann@54744
  1165
  shows "setprod f A \<le> setprod g A"
haftmann@54744
  1166
proof (cases "finite A")
haftmann@54744
  1167
  case True
haftmann@54744
  1168
  hence ?thesis "setprod f A \<ge> 0" using subset_refl[of A]
haftmann@54744
  1169
  proof (induct A rule: finite_subset_induct)
haftmann@54744
  1170
    case (insert a F)
haftmann@54744
  1171
    thus "setprod f (insert a F) \<le> setprod g (insert a F)" "0 \<le> setprod f (insert a F)"
haftmann@57418
  1172
      unfolding setprod.insert[OF insert(1,3)]
haftmann@54744
  1173
      using assms[rule_format,OF insert(2)] insert
nipkow@56536
  1174
      by (auto intro: mult_mono)
haftmann@54744
  1175
  qed auto
haftmann@54744
  1176
  thus ?thesis by simp
haftmann@54744
  1177
qed auto
haftmann@54744
  1178
haftmann@54744
  1179
lemma abs_setprod:
haftmann@54744
  1180
  fixes f :: "'a \<Rightarrow> 'b\<Colon>{linordered_field,abs}"
haftmann@54744
  1181
  shows "abs (setprod f A) = setprod (\<lambda>x. abs (f x)) A"
haftmann@54744
  1182
proof (cases "finite A")
haftmann@54744
  1183
  case True thus ?thesis
haftmann@54744
  1184
    by induct (auto simp add: field_simps abs_mult)
haftmann@54744
  1185
qed auto
haftmann@54744
  1186
haftmann@54744
  1187
lemma setprod_eq_1_iff [simp]:
haftmann@54744
  1188
  "finite F ==> setprod f F = 1 \<longleftrightarrow> (ALL a:F. f a = (1::nat))"
haftmann@54744
  1189
  by (induct set: finite) auto
haftmann@54744
  1190
haftmann@54744
  1191
lemma setprod_pos_nat:
haftmann@54744
  1192
  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"
haftmann@54744
  1193
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
haftmann@54744
  1194
haftmann@54744
  1195
lemma setprod_pos_nat_iff[simp]:
haftmann@54744
  1196
  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"
haftmann@54744
  1197
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
haftmann@54744
  1198
haftmann@54744
  1199
end