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