src/HOL/Groups_Big.thy
author paulson <lp15@cam.ac.uk>
Mon Apr 09 15:20:11 2018 +0100 (17 months ago)
changeset 67969 83c8cafdebe8
parent 67683 817944aeac3f
child 68361 20375f232f3b
permissions -rw-r--r--
Syntax for the special cases Min(A`I) and Max (A`I)
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
blanchet@66364
    11
  imports 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
lp15@63952
    50
lemma insert_if: "finite A \<Longrightarrow> F g (insert x A) = (if x \<in> A then F g A else g x \<^bold>* F g A)"
lp15@63952
    51
  by (cases "x \<in> A") (simp_all add: insert_absorb)
lp15@63952
    52
wenzelm@63654
    53
lemma neutral: "\<forall>x\<in>A. g x = \<^bold>1 \<Longrightarrow> F g A = \<^bold>1"
wenzelm@63654
    54
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@54744
    55
wenzelm@63654
    56
lemma neutral_const [simp]: "F (\<lambda>_. \<^bold>1) A = \<^bold>1"
haftmann@54744
    57
  by (simp add: neutral)
haftmann@54744
    58
haftmann@54744
    59
lemma union_inter:
haftmann@54744
    60
  assumes "finite A" and "finite B"
haftmann@63290
    61
  shows "F g (A \<union> B) \<^bold>* F g (A \<inter> B) = F g A \<^bold>* F g B"
wenzelm@61799
    62
  \<comment> \<open>The reversed orientation looks more natural, but LOOPS as a simprule!\<close>
wenzelm@63654
    63
  using assms
wenzelm@63654
    64
proof (induct A)
wenzelm@63654
    65
  case empty
wenzelm@63654
    66
  then show ?case by simp
haftmann@54744
    67
next
wenzelm@63654
    68
  case (insert x A)
wenzelm@63654
    69
  then show ?case
wenzelm@63654
    70
    by (auto simp: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
haftmann@54744
    71
qed
haftmann@54744
    72
haftmann@54744
    73
corollary union_inter_neutral:
haftmann@54744
    74
  assumes "finite A" and "finite B"
wenzelm@63654
    75
    and "\<forall>x \<in> A \<inter> B. g x = \<^bold>1"
haftmann@63290
    76
  shows "F g (A \<union> B) = F g A \<^bold>* F g B"
haftmann@54744
    77
  using assms by (simp add: union_inter [symmetric] neutral)
haftmann@54744
    78
haftmann@54744
    79
corollary union_disjoint:
haftmann@54744
    80
  assumes "finite A" and "finite B"
haftmann@54744
    81
  assumes "A \<inter> B = {}"
haftmann@63290
    82
  shows "F g (A \<union> B) = F g A \<^bold>* F g B"
haftmann@54744
    83
  using assms by (simp add: union_inter_neutral)
haftmann@54744
    84
haftmann@57418
    85
lemma union_diff2:
haftmann@57418
    86
  assumes "finite A" and "finite B"
haftmann@63290
    87
  shows "F g (A \<union> B) = F g (A - B) \<^bold>* F g (B - A) \<^bold>* F g (A \<inter> B)"
haftmann@57418
    88
proof -
haftmann@57418
    89
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@57418
    90
    by auto
wenzelm@63654
    91
  with assms show ?thesis
wenzelm@63654
    92
    by simp (subst union_disjoint, auto)+
haftmann@57418
    93
qed
haftmann@57418
    94
haftmann@54744
    95
lemma subset_diff:
haftmann@54744
    96
  assumes "B \<subseteq> A" and "finite A"
haftmann@63290
    97
  shows "F g A = F g (A - B) \<^bold>* F g B"
haftmann@54744
    98
proof -
haftmann@54744
    99
  from assms have "finite (A - B)" by auto
haftmann@54744
   100
  moreover from assms have "finite B" by (rule finite_subset)
haftmann@54744
   101
  moreover from assms have "(A - B) \<inter> B = {}" by auto
haftmann@63290
   102
  ultimately have "F g (A - B \<union> B) = F g (A - B) \<^bold>* F g B" by (rule union_disjoint)
haftmann@54744
   103
  moreover from assms have "A \<union> B = A" by auto
haftmann@54744
   104
  ultimately show ?thesis by simp
haftmann@54744
   105
qed
haftmann@54744
   106
haftmann@56545
   107
lemma setdiff_irrelevant:
haftmann@56545
   108
  assumes "finite A"
haftmann@56545
   109
  shows "F g (A - {x. g x = z}) = F g A"
hoelzl@62376
   110
  using assms by (induct A) (simp_all add: insert_Diff_if)
haftmann@58195
   111
haftmann@56545
   112
lemma not_neutral_contains_not_neutral:
haftmann@63290
   113
  assumes "F g A \<noteq> \<^bold>1"
haftmann@63290
   114
  obtains a where "a \<in> A" and "g a \<noteq> \<^bold>1"
haftmann@56545
   115
proof -
haftmann@63290
   116
  from assms have "\<exists>a\<in>A. g a \<noteq> \<^bold>1"
haftmann@56545
   117
  proof (induct A rule: infinite_finite_induct)
wenzelm@63654
   118
    case infinite
wenzelm@63654
   119
    then show ?case by simp
wenzelm@63654
   120
  next
wenzelm@63654
   121
    case empty
wenzelm@63654
   122
    then show ?case by simp
wenzelm@63654
   123
  next
haftmann@56545
   124
    case (insert a A)
wenzelm@63654
   125
    then show ?case by fastforce
wenzelm@63654
   126
  qed
haftmann@56545
   127
  with that show thesis by blast
haftmann@56545
   128
qed
haftmann@56545
   129
haftmann@54744
   130
lemma reindex:
haftmann@54744
   131
  assumes "inj_on h A"
haftmann@54744
   132
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@54744
   133
proof (cases "finite A")
haftmann@54744
   134
  case True
wenzelm@63654
   135
  with assms show ?thesis
wenzelm@63654
   136
    by (simp add: eq_fold fold_image comp_assoc)
haftmann@54744
   137
next
wenzelm@63654
   138
  case False
wenzelm@63654
   139
  with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
haftmann@54744
   140
  with False show ?thesis by simp
haftmann@54744
   141
qed
haftmann@54744
   142
nipkow@63357
   143
lemma cong [fundef_cong]:
haftmann@54744
   144
  assumes "A = B"
haftmann@54744
   145
  assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x"
haftmann@54744
   146
  shows "F g A = F h B"
wenzelm@60758
   147
  using g_h unfolding \<open>A = B\<close>
hoelzl@57129
   148
  by (induct B rule: infinite_finite_induct) auto
haftmann@54744
   149
haftmann@54744
   150
lemma strong_cong [cong]:
haftmann@54744
   151
  assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x"
haftmann@54744
   152
  shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B"
wenzelm@63654
   153
  by (rule cong) (use assms in \<open>simp_all add: simp_implies_def\<close>)
haftmann@54744
   154
haftmann@57418
   155
lemma reindex_cong:
haftmann@57418
   156
  assumes "inj_on l B"
haftmann@57418
   157
  assumes "A = l ` B"
haftmann@57418
   158
  assumes "\<And>x. x \<in> B \<Longrightarrow> g (l x) = h x"
haftmann@57418
   159
  shows "F g A = F h B"
haftmann@57418
   160
  using assms by (simp add: reindex)
haftmann@57418
   161
haftmann@54744
   162
lemma UNION_disjoint:
haftmann@54744
   163
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
wenzelm@63654
   164
    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@54744
   165
  shows "F g (UNION I A) = F (\<lambda>x. F g (A x)) I"
wenzelm@63654
   166
  apply (insert assms)
wenzelm@63654
   167
  apply (induct rule: finite_induct)
wenzelm@63654
   168
   apply simp
wenzelm@63654
   169
  apply atomize
wenzelm@63654
   170
  apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
wenzelm@63654
   171
   prefer 2 apply blast
wenzelm@63654
   172
  apply (subgoal_tac "A x \<inter> UNION Fa A = {}")
wenzelm@63654
   173
   prefer 2 apply blast
wenzelm@63654
   174
  apply (simp add: union_disjoint)
wenzelm@63654
   175
  done
haftmann@54744
   176
haftmann@54744
   177
lemma Union_disjoint:
haftmann@54744
   178
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
wenzelm@61952
   179
  shows "F g (\<Union>C) = (F \<circ> F) g C"
wenzelm@63654
   180
proof (cases "finite C")
wenzelm@63654
   181
  case True
wenzelm@63654
   182
  from UNION_disjoint [OF this assms] show ?thesis by simp
wenzelm@63654
   183
next
wenzelm@63654
   184
  case False
wenzelm@63654
   185
  then show ?thesis by (auto dest: finite_UnionD intro: infinite)
wenzelm@63654
   186
qed
haftmann@54744
   187
wenzelm@63654
   188
lemma distrib: "F (\<lambda>x. g x \<^bold>* h x) A = F g A \<^bold>* F h A"
wenzelm@63092
   189
  by (induct A rule: infinite_finite_induct) (simp_all add: assoc commute left_commute)
haftmann@54744
   190
haftmann@54744
   191
lemma Sigma:
haftmann@61032
   192
  "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
   193
  apply (subst Sigma_def)
wenzelm@63654
   194
  apply (subst UNION_disjoint)
wenzelm@63654
   195
     apply assumption
wenzelm@63654
   196
    apply simp
wenzelm@63654
   197
   apply blast
wenzelm@63654
   198
  apply (rule cong)
wenzelm@63654
   199
   apply rule
wenzelm@63654
   200
  apply (simp add: fun_eq_iff)
wenzelm@63654
   201
  apply (subst UNION_disjoint)
wenzelm@63654
   202
     apply simp
wenzelm@63654
   203
    apply simp
wenzelm@63654
   204
   apply blast
wenzelm@63654
   205
  apply (simp add: comp_def)
wenzelm@63654
   206
  done
haftmann@54744
   207
hoelzl@62376
   208
lemma related:
haftmann@63290
   209
  assumes Re: "R \<^bold>1 \<^bold>1"
wenzelm@63654
   210
    and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 \<^bold>* y1) (x2 \<^bold>* y2)"
wenzelm@63654
   211
    and fin: "finite S"
wenzelm@63654
   212
    and R_h_g: "\<forall>x\<in>S. R (h x) (g x)"
haftmann@54744
   213
  shows "R (F h S) (F g S)"
wenzelm@63654
   214
  using fin by (rule finite_subset_induct) (use assms in auto)
haftmann@54744
   215
haftmann@54744
   216
lemma mono_neutral_cong_left:
wenzelm@63654
   217
  assumes "finite T"
wenzelm@63654
   218
    and "S \<subseteq> T"
wenzelm@63654
   219
    and "\<forall>i \<in> T - S. h i = \<^bold>1"
wenzelm@63654
   220
    and "\<And>x. x \<in> S \<Longrightarrow> g x = h x"
wenzelm@63654
   221
  shows "F g S = F h T"
haftmann@54744
   222
proof-
wenzelm@60758
   223
  have eq: "T = S \<union> (T - S)" using \<open>S \<subseteq> T\<close> by blast
wenzelm@60758
   224
  have d: "S \<inter> (T - S) = {}" using \<open>S \<subseteq> T\<close> by blast
wenzelm@60758
   225
  from \<open>finite T\<close> \<open>S \<subseteq> T\<close> have f: "finite S" "finite (T - S)"
haftmann@54744
   226
    by (auto intro: finite_subset)
haftmann@54744
   227
  show ?thesis using assms(4)
haftmann@54744
   228
    by (simp add: union_disjoint [OF f d, unfolded eq [symmetric]] neutral [OF assms(3)])
haftmann@54744
   229
qed
haftmann@54744
   230
haftmann@54744
   231
lemma mono_neutral_cong_right:
wenzelm@63654
   232
  "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
   233
    F g T = F h S"
haftmann@54744
   234
  by (auto intro!: mono_neutral_cong_left [symmetric])
haftmann@54744
   235
wenzelm@63654
   236
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
   237
  by (blast intro: mono_neutral_cong_left)
haftmann@54744
   238
wenzelm@63654
   239
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
   240
  by (blast intro!: mono_neutral_left [symmetric])
haftmann@54744
   241
hoelzl@64979
   242
lemma mono_neutral_cong:
hoelzl@64979
   243
  assumes [simp]: "finite T" "finite S"
hoelzl@64979
   244
    and *: "\<And>i. i \<in> T - S \<Longrightarrow> h i = \<^bold>1" "\<And>i. i \<in> S - T \<Longrightarrow> g i = \<^bold>1"
hoelzl@64979
   245
    and gh: "\<And>x. x \<in> S \<inter> T \<Longrightarrow> g x = h x"
hoelzl@64979
   246
 shows "F g S = F h T"
hoelzl@64979
   247
proof-
hoelzl@64979
   248
  have "F g S = F g (S \<inter> T)"
hoelzl@64979
   249
    by(rule mono_neutral_right)(auto intro: *)
hoelzl@64979
   250
  also have "\<dots> = F h (S \<inter> T)" using refl gh by(rule cong)
hoelzl@64979
   251
  also have "\<dots> = F h T"
hoelzl@64979
   252
    by(rule mono_neutral_left)(auto intro: *)
hoelzl@64979
   253
  finally show ?thesis .
hoelzl@64979
   254
qed
hoelzl@64979
   255
hoelzl@57129
   256
lemma reindex_bij_betw: "bij_betw h S T \<Longrightarrow> F (\<lambda>x. g (h x)) S = F g T"
hoelzl@57129
   257
  by (auto simp: bij_betw_def reindex)
hoelzl@57129
   258
hoelzl@57129
   259
lemma reindex_bij_witness:
hoelzl@57129
   260
  assumes witness:
hoelzl@57129
   261
    "\<And>a. a \<in> S \<Longrightarrow> i (j a) = a"
hoelzl@57129
   262
    "\<And>a. a \<in> S \<Longrightarrow> j a \<in> T"
hoelzl@57129
   263
    "\<And>b. b \<in> T \<Longrightarrow> j (i b) = b"
hoelzl@57129
   264
    "\<And>b. b \<in> T \<Longrightarrow> i b \<in> S"
hoelzl@57129
   265
  assumes eq:
hoelzl@57129
   266
    "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a"
hoelzl@57129
   267
  shows "F g S = F h T"
hoelzl@57129
   268
proof -
hoelzl@57129
   269
  have "bij_betw j S T"
hoelzl@57129
   270
    using bij_betw_byWitness[where A=S and f=j and f'=i and A'=T] witness by auto
hoelzl@57129
   271
  moreover have "F g S = F (\<lambda>x. h (j x)) S"
hoelzl@57129
   272
    by (intro cong) (auto simp: eq)
hoelzl@57129
   273
  ultimately show ?thesis
hoelzl@57129
   274
    by (simp add: reindex_bij_betw)
hoelzl@57129
   275
qed
hoelzl@57129
   276
hoelzl@57129
   277
lemma reindex_bij_betw_not_neutral:
hoelzl@57129
   278
  assumes fin: "finite S'" "finite T'"
hoelzl@57129
   279
  assumes bij: "bij_betw h (S - S') (T - T')"
hoelzl@57129
   280
  assumes nn:
hoelzl@57129
   281
    "\<And>a. a \<in> S' \<Longrightarrow> g (h a) = z"
hoelzl@57129
   282
    "\<And>b. b \<in> T' \<Longrightarrow> g b = z"
hoelzl@57129
   283
  shows "F (\<lambda>x. g (h x)) S = F g T"
hoelzl@57129
   284
proof -
hoelzl@57129
   285
  have [simp]: "finite S \<longleftrightarrow> finite T"
hoelzl@57129
   286
    using bij_betw_finite[OF bij] fin by auto
hoelzl@57129
   287
  show ?thesis
wenzelm@63654
   288
  proof (cases "finite S")
wenzelm@63654
   289
    case True
hoelzl@57129
   290
    with nn have "F (\<lambda>x. g (h x)) S = F (\<lambda>x. g (h x)) (S - S')"
hoelzl@57129
   291
      by (intro mono_neutral_cong_right) auto
hoelzl@57129
   292
    also have "\<dots> = F g (T - T')"
hoelzl@57129
   293
      using bij by (rule reindex_bij_betw)
hoelzl@57129
   294
    also have "\<dots> = F g T"
wenzelm@60758
   295
      using nn \<open>finite S\<close> by (intro mono_neutral_cong_left) auto
hoelzl@57129
   296
    finally show ?thesis .
wenzelm@63654
   297
  next
wenzelm@63654
   298
    case False
wenzelm@63654
   299
    then show ?thesis by simp
wenzelm@63654
   300
  qed
hoelzl@57129
   301
qed
hoelzl@57129
   302
haftmann@57418
   303
lemma reindex_nontrivial:
haftmann@57418
   304
  assumes "finite A"
wenzelm@63654
   305
    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
   306
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@57418
   307
proof (subst reindex_bij_betw_not_neutral [symmetric])
haftmann@63290
   308
  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
   309
    using nz by (auto intro!: inj_onI simp: bij_betw_def)
wenzelm@63654
   310
qed (use \<open>finite A\<close> in auto)
haftmann@57418
   311
hoelzl@57129
   312
lemma reindex_bij_witness_not_neutral:
hoelzl@57129
   313
  assumes fin: "finite S'" "finite T'"
hoelzl@57129
   314
  assumes witness:
hoelzl@57129
   315
    "\<And>a. a \<in> S - S' \<Longrightarrow> i (j a) = a"
hoelzl@57129
   316
    "\<And>a. a \<in> S - S' \<Longrightarrow> j a \<in> T - T'"
hoelzl@57129
   317
    "\<And>b. b \<in> T - T' \<Longrightarrow> j (i b) = b"
hoelzl@57129
   318
    "\<And>b. b \<in> T - T' \<Longrightarrow> i b \<in> S - S'"
hoelzl@57129
   319
  assumes nn:
hoelzl@57129
   320
    "\<And>a. a \<in> S' \<Longrightarrow> g a = z"
hoelzl@57129
   321
    "\<And>b. b \<in> T' \<Longrightarrow> h b = z"
hoelzl@57129
   322
  assumes eq:
hoelzl@57129
   323
    "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a"
hoelzl@57129
   324
  shows "F g S = F h T"
hoelzl@57129
   325
proof -
hoelzl@57129
   326
  have bij: "bij_betw j (S - (S' \<inter> S)) (T - (T' \<inter> T))"
hoelzl@57129
   327
    using witness by (intro bij_betw_byWitness[where f'=i]) auto
hoelzl@57129
   328
  have F_eq: "F g S = F (\<lambda>x. h (j x)) S"
hoelzl@57129
   329
    by (intro cong) (auto simp: eq)
hoelzl@57129
   330
  show ?thesis
hoelzl@57129
   331
    unfolding F_eq using fin nn eq
hoelzl@57129
   332
    by (intro reindex_bij_betw_not_neutral[OF _ _ bij]) auto
hoelzl@57129
   333
qed
hoelzl@57129
   334
lp15@67673
   335
lemma delta_remove:
lp15@67673
   336
  assumes fS: "finite S"
lp15@67673
   337
  shows "F (\<lambda>k. if k = a then b k else c k) S = (if a \<in> S then b a \<^bold>* F c (S-{a}) else F c (S-{a}))"
lp15@67673
   338
proof -
lp15@67673
   339
  let ?f = "(\<lambda>k. if k = a then b k else c k)"
lp15@67673
   340
  show ?thesis
lp15@67673
   341
  proof (cases "a \<in> S")
lp15@67673
   342
    case False
lp15@67673
   343
    then have "\<forall>k\<in>S. ?f k = c k" by simp
lp15@67673
   344
    with False show ?thesis by simp
lp15@67673
   345
  next
lp15@67673
   346
    case True
lp15@67673
   347
    let ?A = "S - {a}"
lp15@67673
   348
    let ?B = "{a}"
lp15@67673
   349
    from True have eq: "S = ?A \<union> ?B" by blast
lp15@67673
   350
    have dj: "?A \<inter> ?B = {}" by simp
lp15@67673
   351
    from fS have fAB: "finite ?A" "finite ?B" by auto
lp15@67673
   352
    have "F ?f S = F ?f ?A \<^bold>* F ?f ?B"
lp15@67673
   353
      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]] by simp
lp15@67673
   354
    with True show ?thesis
lp15@67673
   355
      using comm_monoid_set.remove comm_monoid_set_axioms fS by fastforce
lp15@67673
   356
  qed
lp15@67673
   357
qed
lp15@67673
   358
lp15@67683
   359
lemma delta [simp]:
lp15@67683
   360
  assumes fS: "finite S"
lp15@67683
   361
  shows "F (\<lambda>k. if k = a then b k else \<^bold>1) S = (if a \<in> S then b a else \<^bold>1)"
lp15@67683
   362
  by (simp add: delta_remove [OF assms])
lp15@67683
   363
lp15@67683
   364
lemma delta' [simp]:
lp15@67683
   365
  assumes fin: "finite S"
lp15@67683
   366
  shows "F (\<lambda>k. if a = k then b k else \<^bold>1) S = (if a \<in> S then b a else \<^bold>1)"
lp15@67683
   367
  using delta [OF fin, of a b, symmetric] by (auto intro: cong)
lp15@67683
   368
haftmann@54744
   369
lemma If_cases:
haftmann@54744
   370
  fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
wenzelm@63654
   371
  assumes fin: "finite A"
wenzelm@63654
   372
  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
   373
proof -
wenzelm@63654
   374
  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
   375
    by blast+
wenzelm@63654
   376
  from fin have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
haftmann@54744
   377
  let ?g = "\<lambda>x. if P x then h x else g x"
wenzelm@63654
   378
  from union_disjoint [OF f a(2), of ?g] a(1) show ?thesis
haftmann@54744
   379
    by (subst (1 2) cong) simp_all
haftmann@54744
   380
qed
haftmann@54744
   381
wenzelm@63654
   382
lemma cartesian_product: "F (\<lambda>x. F (g x) B) A = F (case_prod g) (A \<times> B)"
wenzelm@63654
   383
  apply (rule sym)
wenzelm@63654
   384
  apply (cases "finite A")
wenzelm@63654
   385
   apply (cases "finite B")
wenzelm@63654
   386
    apply (simp add: Sigma)
wenzelm@63654
   387
   apply (cases "A = {}")
wenzelm@63654
   388
    apply simp
wenzelm@63654
   389
   apply simp
wenzelm@63654
   390
   apply (auto intro: infinite dest: finite_cartesian_productD2)
wenzelm@63654
   391
  apply (cases "B = {}")
wenzelm@63654
   392
   apply (auto intro: infinite dest: finite_cartesian_productD1)
wenzelm@63654
   393
  done
haftmann@54744
   394
haftmann@57418
   395
lemma inter_restrict:
haftmann@57418
   396
  assumes "finite A"
haftmann@63290
   397
  shows "F g (A \<inter> B) = F (\<lambda>x. if x \<in> B then g x else \<^bold>1) A"
haftmann@57418
   398
proof -
haftmann@63290
   399
  let ?g = "\<lambda>x. if x \<in> A \<inter> B then g x else \<^bold>1"
wenzelm@63654
   400
  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
   401
  moreover have "A \<inter> B \<subseteq> A" by blast
wenzelm@63654
   402
  ultimately have "F ?g (A \<inter> B) = F ?g A"
wenzelm@63654
   403
    using \<open>finite A\<close> by (intro mono_neutral_left) auto
haftmann@57418
   404
  then show ?thesis by simp
haftmann@57418
   405
qed
haftmann@57418
   406
haftmann@57418
   407
lemma inter_filter:
haftmann@63290
   408
  "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
   409
  by (simp add: inter_restrict [symmetric, of A "{x. P x}" g, simplified mem_Collect_eq] Int_def)
haftmann@57418
   410
haftmann@57418
   411
lemma Union_comp:
haftmann@57418
   412
  assumes "\<forall>A \<in> B. finite A"
wenzelm@63654
   413
    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
   414
  shows "F g (\<Union>B) = (F \<circ> F) g B"
wenzelm@63654
   415
  using assms
wenzelm@63654
   416
proof (induct B rule: infinite_finite_induct)
haftmann@57418
   417
  case (infinite A)
haftmann@57418
   418
  then have "\<not> finite (\<Union>A)" by (blast dest: finite_UnionD)
haftmann@57418
   419
  with infinite show ?case by simp
haftmann@57418
   420
next
wenzelm@63654
   421
  case empty
wenzelm@63654
   422
  then show ?case by simp
haftmann@57418
   423
next
haftmann@57418
   424
  case (insert A B)
haftmann@57418
   425
  then have "finite A" "finite B" "finite (\<Union>B)" "A \<notin> B"
haftmann@63290
   426
    and "\<forall>x\<in>A \<inter> \<Union>B. g x = \<^bold>1"
wenzelm@63654
   427
    and H: "F g (\<Union>B) = (F \<circ> F) g B" by auto
haftmann@63290
   428
  then have "F g (A \<union> \<Union>B) = F g A \<^bold>* F g (\<Union>B)"
haftmann@57418
   429
    by (simp add: union_inter_neutral)
wenzelm@60758
   430
  with \<open>finite B\<close> \<open>A \<notin> B\<close> show ?case
haftmann@57418
   431
    by (simp add: H)
haftmann@57418
   432
qed
haftmann@57418
   433
haftmann@66804
   434
lemma swap: "F (\<lambda>i. F (g i) B) A = F (\<lambda>j. F (\<lambda>i. g i j) A) B"
haftmann@57418
   435
  unfolding cartesian_product
haftmann@57418
   436
  by (rule reindex_bij_witness [where i = "\<lambda>(i, j). (j, i)" and j = "\<lambda>(i, j). (j, i)"]) auto
haftmann@57418
   437
haftmann@66804
   438
lemma swap_restrict:
haftmann@57418
   439
  "finite A \<Longrightarrow> finite B \<Longrightarrow>
haftmann@57418
   440
    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@66804
   441
  by (simp add: inter_filter) (rule swap)
haftmann@57418
   442
haftmann@57418
   443
lemma Plus:
haftmann@57418
   444
  fixes A :: "'b set" and B :: "'c set"
haftmann@57418
   445
  assumes fin: "finite A" "finite B"
haftmann@63290
   446
  shows "F g (A <+> B) = F (g \<circ> Inl) A \<^bold>* F (g \<circ> Inr) B"
haftmann@57418
   447
proof -
haftmann@57418
   448
  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
wenzelm@63654
   449
  moreover from fin have "finite (Inl ` A)" "finite (Inr ` B)" by auto
wenzelm@63654
   450
  moreover have "Inl ` A \<inter> Inr ` B = {}" by auto
wenzelm@63654
   451
  moreover have "inj_on Inl A" "inj_on Inr B" by (auto intro: inj_onI)
wenzelm@63654
   452
  ultimately show ?thesis
wenzelm@63654
   453
    using fin by (simp add: union_disjoint reindex)
haftmann@57418
   454
qed
haftmann@57418
   455
haftmann@58195
   456
lemma same_carrier:
haftmann@58195
   457
  assumes "finite C"
haftmann@58195
   458
  assumes subset: "A \<subseteq> C" "B \<subseteq> C"
haftmann@63290
   459
  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
   460
  shows "F g A = F h B \<longleftrightarrow> F g C = F h C"
haftmann@58195
   461
proof -
wenzelm@63654
   462
  have "finite A" and "finite B" and "finite (C - A)" and "finite (C - B)"
wenzelm@63654
   463
    using \<open>finite C\<close> subset by (auto elim: finite_subset)
haftmann@58195
   464
  from subset have [simp]: "A - (C - A) = A" by auto
haftmann@58195
   465
  from subset have [simp]: "B - (C - B) = B" by auto
haftmann@58195
   466
  from subset have "C = A \<union> (C - A)" by auto
haftmann@58195
   467
  then have "F g C = F g (A \<union> (C - A))" by simp
haftmann@63290
   468
  also have "\<dots> = F g (A - (C - A)) \<^bold>* F g (C - A - A) \<^bold>* F g (A \<inter> (C - A))"
wenzelm@60758
   469
    using \<open>finite A\<close> \<open>finite (C - A)\<close> by (simp only: union_diff2)
wenzelm@63654
   470
  finally have *: "F g C = F g A" using trivial by simp
haftmann@58195
   471
  from subset have "C = B \<union> (C - B)" by auto
haftmann@58195
   472
  then have "F h C = F h (B \<union> (C - B))" by simp
haftmann@63290
   473
  also have "\<dots> = F h (B - (C - B)) \<^bold>* F h (C - B - B) \<^bold>* F h (B \<inter> (C - B))"
wenzelm@60758
   474
    using \<open>finite B\<close> \<open>finite (C - B)\<close> by (simp only: union_diff2)
wenzelm@63654
   475
  finally have "F h C = F h B"
wenzelm@63654
   476
    using trivial by simp
wenzelm@63654
   477
  with * show ?thesis by simp
haftmann@58195
   478
qed
haftmann@58195
   479
haftmann@58195
   480
lemma same_carrierI:
haftmann@58195
   481
  assumes "finite C"
haftmann@58195
   482
  assumes subset: "A \<subseteq> C" "B \<subseteq> C"
haftmann@63290
   483
  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
   484
  assumes "F g C = F h C"
haftmann@58195
   485
  shows "F g A = F h B"
haftmann@58195
   486
  using assms same_carrier [of C A B] by simp
haftmann@58195
   487
haftmann@54744
   488
end
haftmann@54744
   489
haftmann@54744
   490
wenzelm@60758
   491
subsection \<open>Generalized summation over a set\<close>
haftmann@54744
   492
haftmann@54744
   493
context comm_monoid_add
haftmann@54744
   494
begin
haftmann@54744
   495
nipkow@64267
   496
sublocale sum: comm_monoid_set plus 0
nipkow@64267
   497
  defines sum = sum.F ..
haftmann@54744
   498
nipkow@64267
   499
abbreviation Sum ("\<Sum>_" [1000] 999)
nipkow@64267
   500
  where "\<Sum>A \<equiv> sum (\<lambda>x. x) A"
haftmann@54744
   501
haftmann@54744
   502
end
haftmann@54744
   503
lp15@67268
   504
text \<open>Now: lots of fancy syntax. First, @{term "sum (\<lambda>x. e) A"} is written \<open>\<Sum>x\<in>A. e\<close>.\<close>
haftmann@54744
   505
wenzelm@61955
   506
syntax (ASCII)
lp15@67268
   507
  "_sum" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b::comm_monoid_add"  ("(3SUM (_/:_)./ _)" [0, 51, 10] 10)
haftmann@54744
   508
syntax
lp15@67268
   509
  "_sum" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b::comm_monoid_add"  ("(2\<Sum>(_/\<in>_)./ _)" [0, 51, 10] 10)
wenzelm@61799
   510
translations \<comment> \<open>Beware of argument permutation!\<close>
nipkow@64267
   511
  "\<Sum>i\<in>A. b" \<rightleftharpoons> "CONST sum (\<lambda>i. b) A"
haftmann@54744
   512
wenzelm@61955
   513
text \<open>Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter \<open>\<Sum>x|P. e\<close>.\<close>
haftmann@54744
   514
wenzelm@61955
   515
syntax (ASCII)
nipkow@64267
   516
  "_qsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a"  ("(3SUM _ |/ _./ _)" [0, 0, 10] 10)
haftmann@54744
   517
syntax
nipkow@64267
   518
  "_qsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a"  ("(2\<Sum>_ | (_)./ _)" [0, 0, 10] 10)
haftmann@54744
   519
translations
nipkow@64267
   520
  "\<Sum>x|P. t" => "CONST sum (\<lambda>x. t) {x. P}"
haftmann@54744
   521
wenzelm@60758
   522
print_translation \<open>
haftmann@54744
   523
let
nipkow@64267
   524
  fun sum_tr' [Abs (x, Tx, t), Const (@{const_syntax Collect}, _) $ Abs (y, Ty, P)] =
haftmann@54744
   525
        if x <> y then raise Match
haftmann@54744
   526
        else
haftmann@54744
   527
          let
haftmann@54744
   528
            val x' = Syntax_Trans.mark_bound_body (x, Tx);
haftmann@54744
   529
            val t' = subst_bound (x', t);
haftmann@54744
   530
            val P' = subst_bound (x', P);
haftmann@54744
   531
          in
nipkow@64267
   532
            Syntax.const @{syntax_const "_qsum"} $ Syntax_Trans.mark_bound_abs (x, Tx) $ P' $ t'
haftmann@54744
   533
          end
nipkow@64267
   534
    | sum_tr' _ = raise Match;
nipkow@64267
   535
in [(@{const_syntax sum}, K sum_tr')] end
wenzelm@60758
   536
\<close>
haftmann@54744
   537
wenzelm@63654
   538
(* TODO generalization candidates *)
haftmann@54744
   539
nipkow@64267
   540
lemma (in comm_monoid_add) sum_image_gen:
wenzelm@63654
   541
  assumes fin: "finite S"
nipkow@64267
   542
  shows "sum g S = sum (\<lambda>y. sum g {x. x \<in> S \<and> f x = y}) (f ` S)"
wenzelm@63654
   543
proof -
wenzelm@63654
   544
  have "{y. y\<in> f`S \<and> f x = y} = {f x}" if "x \<in> S" for x
wenzelm@63654
   545
    using that by auto
nipkow@64267
   546
  then have "sum g S = sum (\<lambda>x. sum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
haftmann@57418
   547
    by simp
nipkow@64267
   548
  also have "\<dots> = sum (\<lambda>y. sum g {x. x \<in> S \<and> f x = y}) (f ` S)"
haftmann@66804
   549
    by (rule sum.swap_restrict [OF fin finite_imageI [OF fin]])
haftmann@57418
   550
  finally show ?thesis .
haftmann@54744
   551
qed
haftmann@54744
   552
haftmann@54744
   553
wenzelm@60758
   554
subsubsection \<open>Properties in more restricted classes of structures\<close>
haftmann@54744
   555
nipkow@64267
   556
lemma sum_Un:
nipkow@64267
   557
  "finite A \<Longrightarrow> finite B \<Longrightarrow> sum f (A \<union> B) = sum f A + sum f B - sum f (A \<inter> B)"
wenzelm@63654
   558
  for f :: "'b \<Rightarrow> 'a::ab_group_add"
nipkow@64267
   559
  by (subst sum.union_inter [symmetric]) (auto simp add: algebra_simps)
haftmann@54744
   560
nipkow@64267
   561
lemma sum_Un2:
haftmann@54744
   562
  assumes "finite (A \<union> B)"
nipkow@64267
   563
  shows "sum f (A \<union> B) = sum f (A - B) + sum f (B - A) + sum f (A \<inter> B)"
haftmann@54744
   564
proof -
haftmann@54744
   565
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@54744
   566
    by auto
wenzelm@63654
   567
  with assms show ?thesis
nipkow@64267
   568
    by simp (subst sum.union_disjoint, auto)+
haftmann@54744
   569
qed
haftmann@54744
   570
nipkow@64267
   571
lemma sum_diff1:
wenzelm@63654
   572
  fixes f :: "'b \<Rightarrow> 'a::ab_group_add"
wenzelm@63654
   573
  assumes "finite A"
nipkow@64267
   574
  shows "sum f (A - {a}) = (if a \<in> A then sum f A - f a else sum f A)"
wenzelm@63654
   575
  using assms by induct (auto simp: insert_Diff_if)
haftmann@54744
   576
nipkow@64267
   577
lemma sum_diff:
wenzelm@63654
   578
  fixes f :: "'b \<Rightarrow> 'a::ab_group_add"
wenzelm@63654
   579
  assumes "finite A" "B \<subseteq> A"
nipkow@64267
   580
  shows "sum f (A - B) = sum f A - sum f B"
haftmann@54744
   581
proof -
wenzelm@63654
   582
  from assms(2,1) have "finite B" by (rule finite_subset)
wenzelm@63654
   583
  from this \<open>B \<subseteq> A\<close>
wenzelm@63654
   584
  show ?thesis
haftmann@54744
   585
  proof induct
haftmann@54744
   586
    case empty
wenzelm@63654
   587
    thus ?case by simp
haftmann@54744
   588
  next
haftmann@54744
   589
    case (insert x F)
wenzelm@63654
   590
    with \<open>finite A\<close> \<open>finite B\<close> show ?case
nipkow@64267
   591
      by (simp add: Diff_insert[where a=x and B=F] sum_diff1 insert_absorb)
haftmann@54744
   592
  qed
haftmann@54744
   593
qed
haftmann@54744
   594
nipkow@64267
   595
lemma (in ordered_comm_monoid_add) sum_mono:
wenzelm@63915
   596
  "(\<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
   597
  by (induct K rule: infinite_finite_induct) (use add_mono in auto)
haftmann@54744
   598
nipkow@64267
   599
lemma (in strict_ordered_comm_monoid_add) sum_strict_mono:
wenzelm@63654
   600
  assumes "finite A" "A \<noteq> {}"
wenzelm@63654
   601
    and "\<And>x. x \<in> A \<Longrightarrow> f x < g x"
nipkow@64267
   602
  shows "sum f A < sum g A"
haftmann@54744
   603
  using assms
haftmann@54744
   604
proof (induct rule: finite_ne_induct)
wenzelm@63654
   605
  case singleton
wenzelm@63654
   606
  then show ?case by simp
haftmann@54744
   607
next
wenzelm@63654
   608
  case insert
wenzelm@63654
   609
  then show ?case by (auto simp: add_strict_mono)
haftmann@54744
   610
qed
haftmann@54744
   611
nipkow@64267
   612
lemma sum_strict_mono_ex1:
hoelzl@62376
   613
  fixes f g :: "'i \<Rightarrow> 'a::ordered_cancel_comm_monoid_add"
wenzelm@63654
   614
  assumes "finite A"
wenzelm@63654
   615
    and "\<forall>x\<in>A. f x \<le> g x"
wenzelm@63654
   616
    and "\<exists>a\<in>A. f a < g a"
nipkow@64267
   617
  shows "sum f A < sum g A"
haftmann@54744
   618
proof-
wenzelm@63654
   619
  from assms(3) obtain a where a: "a \<in> A" "f a < g a" by blast
nipkow@64267
   620
  have "sum f A = sum f ((A - {a}) \<union> {a})"
wenzelm@63654
   621
    by(simp add: insert_absorb[OF \<open>a \<in> A\<close>])
nipkow@64267
   622
  also have "\<dots> = sum f (A - {a}) + sum f {a}"
nipkow@64267
   623
    using \<open>finite A\<close> by(subst sum.union_disjoint) auto
nipkow@64267
   624
  also have "sum f (A - {a}) \<le> sum g (A - {a})"
nipkow@64267
   625
    by (rule sum_mono) (simp add: assms(2))
nipkow@64267
   626
  also from a have "sum f {a} < sum g {a}" by simp
nipkow@64267
   627
  also have "sum g (A - {a}) + sum g {a} = sum g((A - {a}) \<union> {a})"
nipkow@64267
   628
    using \<open>finite A\<close> by (subst sum.union_disjoint[symmetric]) auto
nipkow@64267
   629
  also have "\<dots> = sum g A" by (simp add: insert_absorb[OF \<open>a \<in> A\<close>])
wenzelm@63654
   630
  finally show ?thesis
wenzelm@63654
   631
    by (auto simp add: add_right_mono add_strict_left_mono)
haftmann@54744
   632
qed
haftmann@54744
   633
nipkow@64267
   634
lemma sum_mono_inv:
Andreas@63561
   635
  fixes f g :: "'i \<Rightarrow> 'a :: ordered_cancel_comm_monoid_add"
nipkow@64267
   636
  assumes eq: "sum f I = sum g I"
Andreas@63561
   637
  assumes le: "\<And>i. i \<in> I \<Longrightarrow> f i \<le> g i"
Andreas@63561
   638
  assumes i: "i \<in> I"
Andreas@63561
   639
  assumes I: "finite I"
Andreas@63561
   640
  shows "f i = g i"
wenzelm@63654
   641
proof (rule ccontr)
wenzelm@63654
   642
  assume "\<not> ?thesis"
Andreas@63561
   643
  with le[OF i] have "f i < g i" by simp
wenzelm@63654
   644
  with i have "\<exists>i\<in>I. f i < g i" ..
nipkow@64267
   645
  from sum_strict_mono_ex1[OF I _ this] le have "sum f I < sum g I"
wenzelm@63654
   646
    by blast
Andreas@63561
   647
  with eq show False by simp
Andreas@63561
   648
qed
Andreas@63561
   649
nipkow@64267
   650
lemma member_le_sum:
lp15@63938
   651
  fixes f :: "_ \<Rightarrow> 'b::{semiring_1, ordered_comm_monoid_add}"
lp15@66112
   652
  assumes "i \<in> A"
lp15@66112
   653
    and le: "\<And>x. x \<in> A - {i} \<Longrightarrow> 0 \<le> f x"
lp15@63938
   654
    and "finite A"
nipkow@64267
   655
  shows "f i \<le> sum f A"
lp15@63938
   656
proof -
nipkow@64267
   657
  have "f i \<le> sum f (A \<inter> {i})"
lp15@63938
   658
    by (simp add: assms)
lp15@63938
   659
  also have "... = (\<Sum>x\<in>A. if x \<in> {i} then f x else 0)"
nipkow@64267
   660
    using assms sum.inter_restrict by blast
nipkow@64267
   661
  also have "... \<le> sum f A"
nipkow@64267
   662
    apply (rule sum_mono)
lp15@63938
   663
    apply (auto simp: le)
lp15@63938
   664
    done
lp15@63938
   665
  finally show ?thesis .
lp15@63938
   666
qed
lp15@63938
   667
nipkow@64267
   668
lemma sum_negf: "(\<Sum>x\<in>A. - f x) = - (\<Sum>x\<in>A. f x)"
wenzelm@63654
   669
  for f :: "'b \<Rightarrow> 'a::ab_group_add"
wenzelm@63915
   670
  by (induct A rule: infinite_finite_induct) auto
haftmann@54744
   671
nipkow@64267
   672
lemma sum_subtractf: "(\<Sum>x\<in>A. f x - g x) = (\<Sum>x\<in>A. f x) - (\<Sum>x\<in>A. g x)"
wenzelm@63654
   673
  for f g :: "'b \<Rightarrow>'a::ab_group_add"
nipkow@64267
   674
  using sum.distrib [of f "- g" A] by (simp add: sum_negf)
haftmann@54744
   675
nipkow@64267
   676
lemma sum_subtractf_nat:
wenzelm@63654
   677
  "(\<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
   678
  for f g :: "'a \<Rightarrow> nat"
nipkow@64267
   679
  by (induct A rule: infinite_finite_induct) (auto simp: sum_mono)
hoelzl@59416
   680
wenzelm@63654
   681
context ordered_comm_monoid_add
wenzelm@63654
   682
begin
wenzelm@63654
   683
lp15@65680
   684
lemma sum_nonneg: "(\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> sum f A"
wenzelm@63915
   685
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   686
  case infinite
wenzelm@63915
   687
  then show ?case by simp
haftmann@54744
   688
next
wenzelm@63915
   689
  case empty
wenzelm@63915
   690
  then show ?case by simp
wenzelm@63915
   691
next
wenzelm@63915
   692
  case (insert x F)
nipkow@64267
   693
  then have "0 + 0 \<le> f x + sum f F" by (blast intro: add_mono)
wenzelm@63915
   694
  with insert show ?case by simp
haftmann@54744
   695
qed
haftmann@54744
   696
lp15@65680
   697
lemma sum_nonpos: "(\<And>x. x \<in> A \<Longrightarrow> f x \<le> 0) \<Longrightarrow> sum f A \<le> 0"
wenzelm@63915
   698
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   699
  case infinite
wenzelm@63915
   700
  then show ?case by simp
haftmann@54744
   701
next
wenzelm@63915
   702
  case empty
wenzelm@63915
   703
  then show ?case by simp
wenzelm@63915
   704
next
wenzelm@63915
   705
  case (insert x F)
nipkow@64267
   706
  then have "f x + sum f F \<le> 0 + 0" by (blast intro: add_mono)
wenzelm@63915
   707
  with insert show ?case by simp
haftmann@54744
   708
qed
haftmann@54744
   709
nipkow@64267
   710
lemma sum_nonneg_eq_0_iff:
lp15@65680
   711
  "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> sum f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
nipkow@64267
   712
  by (induct set: finite) (simp_all add: add_nonneg_eq_0_iff sum_nonneg)
hoelzl@62376
   713
nipkow@64267
   714
lemma sum_nonneg_0:
hoelzl@62376
   715
  "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"
nipkow@64267
   716
  by (simp add: sum_nonneg_eq_0_iff)
hoelzl@62376
   717
nipkow@64267
   718
lemma sum_nonneg_leq_bound:
haftmann@54744
   719
  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
   720
  shows "f i \<le> B"
haftmann@54744
   721
proof -
wenzelm@63654
   722
  from assms have "f i \<le> f i + (\<Sum>i \<in> s - {i}. f i)"
nipkow@64267
   723
    by (intro add_increasing2 sum_nonneg) auto
hoelzl@62376
   724
  also have "\<dots> = B"
nipkow@64267
   725
    using sum.remove[of s i f] assms by simp
hoelzl@62376
   726
  finally show ?thesis by auto
haftmann@54744
   727
qed
haftmann@54744
   728
nipkow@64267
   729
lemma sum_mono2:
wenzelm@63654
   730
  assumes fin: "finite B"
wenzelm@63654
   731
    and sub: "A \<subseteq> B"
wenzelm@63654
   732
    and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
nipkow@64267
   733
  shows "sum f A \<le> sum f B"
haftmann@54744
   734
proof -
nipkow@64267
   735
  have "sum f A \<le> sum f A + sum f (B-A)"
lp15@65680
   736
    by (auto intro: add_increasing2 [OF sum_nonneg] nn)
nipkow@64267
   737
  also from fin finite_subset[OF sub fin] have "\<dots> = sum f (A \<union> (B-A))"
nipkow@64267
   738
    by (simp add: sum.union_disjoint del: Un_Diff_cancel)
wenzelm@63654
   739
  also from sub have "A \<union> (B-A) = B" by blast
haftmann@54744
   740
  finally show ?thesis .
haftmann@54744
   741
qed
haftmann@54744
   742
nipkow@64267
   743
lemma sum_le_included:
haftmann@57418
   744
  assumes "finite s" "finite t"
haftmann@57418
   745
  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)"
nipkow@64267
   746
  shows "sum f s \<le> sum g t"
haftmann@57418
   747
proof -
nipkow@64267
   748
  have "sum f s \<le> sum (\<lambda>y. sum g {x. x\<in>t \<and> i x = y}) s"
nipkow@64267
   749
  proof (rule sum_mono)
wenzelm@63654
   750
    fix y
wenzelm@63654
   751
    assume "y \<in> s"
haftmann@57418
   752
    with assms obtain z where z: "z \<in> t" "y = i z" "f y \<le> g z" by auto
nipkow@64267
   753
    with assms show "f y \<le> sum g {x \<in> t. i x = y}" (is "?A y \<le> ?B y")
nipkow@64267
   754
      using order_trans[of "?A (i z)" "sum g {z}" "?B (i z)", intro]
nipkow@64267
   755
      by (auto intro!: sum_mono2)
haftmann@57418
   756
  qed
nipkow@64267
   757
  also have "\<dots> \<le> sum (\<lambda>y. sum g {x. x\<in>t \<and> i x = y}) (i ` t)"
nipkow@64267
   758
    using assms(2-4) by (auto intro!: sum_mono2 sum_nonneg)
nipkow@64267
   759
  also have "\<dots> \<le> sum g t"
nipkow@64267
   760
    using assms by (auto simp: sum_image_gen[symmetric])
haftmann@57418
   761
  finally show ?thesis .
haftmann@57418
   762
qed
haftmann@57418
   763
wenzelm@63654
   764
end
wenzelm@63654
   765
nipkow@64267
   766
lemma (in canonically_ordered_monoid_add) sum_eq_0_iff [simp]:
nipkow@64267
   767
  "finite F \<Longrightarrow> (sum f F = 0) = (\<forall>a\<in>F. f a = 0)"
nipkow@64267
   768
  by (intro ballI sum_nonneg_eq_0_iff zero_le)
hoelzl@62376
   769
haftmann@66936
   770
context semiring_0
haftmann@66936
   771
begin
haftmann@66936
   772
haftmann@66936
   773
lemma sum_distrib_left: "r * sum f A = (\<Sum>n\<in>A. r * f n)"
haftmann@66936
   774
  by (induct A rule: infinite_finite_induct) (simp_all add: algebra_simps)
haftmann@54744
   775
nipkow@64267
   776
lemma sum_distrib_right: "sum f A * r = (\<Sum>n\<in>A. f n * r)"
haftmann@66936
   777
  by (induct A rule: infinite_finite_induct) (simp_all add: algebra_simps)
haftmann@66936
   778
haftmann@66936
   779
end
wenzelm@63654
   780
nipkow@64267
   781
lemma sum_divide_distrib: "sum f A / r = (\<Sum>n\<in>A. f n / r)"
wenzelm@63654
   782
  for r :: "'a::field"
wenzelm@63915
   783
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   784
  case infinite
wenzelm@63915
   785
  then show ?case by simp
haftmann@54744
   786
next
wenzelm@63915
   787
  case empty
wenzelm@63915
   788
  then show ?case by simp
wenzelm@63915
   789
next
wenzelm@63915
   790
  case insert
wenzelm@63915
   791
  then show ?case by (simp add: add_divide_distrib)
haftmann@54744
   792
qed
haftmann@54744
   793
nipkow@64267
   794
lemma sum_abs[iff]: "\<bar>sum f A\<bar> \<le> sum (\<lambda>i. \<bar>f i\<bar>) A"
wenzelm@63654
   795
  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
wenzelm@63915
   796
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   797
  case infinite
wenzelm@63915
   798
  then show ?case by simp
wenzelm@63654
   799
next
wenzelm@63915
   800
  case empty
wenzelm@63915
   801
  then show ?case by simp
wenzelm@63915
   802
next
wenzelm@63915
   803
  case insert
wenzelm@63915
   804
  then show ?case by (auto intro: abs_triangle_ineq order_trans)
wenzelm@63654
   805
qed
wenzelm@63654
   806
nipkow@64267
   807
lemma sum_abs_ge_zero[iff]: "0 \<le> sum (\<lambda>i. \<bar>f i\<bar>) A"
wenzelm@63654
   808
  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
nipkow@64267
   809
  by (simp add: sum_nonneg)
wenzelm@63654
   810
nipkow@64267
   811
lemma abs_sum_abs[simp]: "\<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar> = (\<Sum>a\<in>A. \<bar>f a\<bar>)"
wenzelm@63654
   812
  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
wenzelm@63915
   813
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   814
  case infinite
wenzelm@63915
   815
  then show ?case by simp
wenzelm@63915
   816
next
wenzelm@63915
   817
  case empty
wenzelm@63915
   818
  then show ?case by simp
haftmann@54744
   819
next
wenzelm@63915
   820
  case (insert a A)
wenzelm@63915
   821
  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
   822
  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
   823
  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
   824
  also from insert have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" by simp
wenzelm@63915
   825
  finally show ?case .
haftmann@54744
   826
qed
haftmann@54744
   827
nipkow@64267
   828
lemma sum_diff1_ring:
wenzelm@63654
   829
  fixes f :: "'b \<Rightarrow> 'a::ring"
wenzelm@63654
   830
  assumes "finite A" "a \<in> A"
nipkow@64267
   831
  shows "sum f (A - {a}) = sum f A - (f a)"
nipkow@64267
   832
  unfolding sum.remove [OF assms] by auto
haftmann@54744
   833
nipkow@64267
   834
lemma sum_product:
wenzelm@63654
   835
  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
nipkow@64267
   836
  shows "sum f A * sum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
haftmann@66804
   837
  by (simp add: sum_distrib_left sum_distrib_right) (rule sum.swap)
haftmann@54744
   838
nipkow@64267
   839
lemma sum_mult_sum_if_inj:
wenzelm@63654
   840
  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
wenzelm@63654
   841
  shows "inj_on (\<lambda>(a, b). f a * g b) (A \<times> B) \<Longrightarrow>
nipkow@64267
   842
    sum f A * sum g B = sum id {f a * g b |a b. a \<in> A \<and> b \<in> B}"
nipkow@64267
   843
  by(auto simp: sum_product sum.cartesian_product intro!: sum.reindex_cong[symmetric])
haftmann@54744
   844
nipkow@64267
   845
lemma sum_SucD: "sum f A = Suc n \<Longrightarrow> \<exists>a\<in>A. 0 < f a"
wenzelm@63915
   846
  by (induct A rule: infinite_finite_induct) auto
haftmann@54744
   847
nipkow@64267
   848
lemma sum_eq_Suc0_iff:
nipkow@64267
   849
  "finite A \<Longrightarrow> sum 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
   850
  by (induct A rule: finite_induct) (auto simp add: add_is_1)
haftmann@54744
   851
nipkow@64267
   852
lemmas sum_eq_1_iff = sum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
haftmann@54744
   853
nipkow@64267
   854
lemma sum_Un_nat:
nipkow@64267
   855
  "finite A \<Longrightarrow> finite B \<Longrightarrow> sum f (A \<union> B) = sum f A + sum f B - sum f (A \<inter> B)"
wenzelm@63654
   856
  for f :: "'a \<Rightarrow> nat"
wenzelm@61799
   857
  \<comment> \<open>For the natural numbers, we have subtraction.\<close>
nipkow@64267
   858
  by (subst sum.union_inter [symmetric]) (auto simp: algebra_simps)
haftmann@54744
   859
nipkow@64267
   860
lemma sum_diff1_nat: "sum f (A - {a}) = (if a \<in> A then sum f A - f a else sum f A)"
wenzelm@63654
   861
  for f :: "'a \<Rightarrow> nat"
wenzelm@63915
   862
proof (induct A rule: infinite_finite_induct)
wenzelm@63915
   863
  case infinite
wenzelm@63915
   864
  then show ?case by simp
wenzelm@63915
   865
next
wenzelm@63915
   866
  case empty
wenzelm@63915
   867
  then show ?case by simp
wenzelm@63915
   868
next
wenzelm@63915
   869
  case insert
wenzelm@63915
   870
  then show ?case
wenzelm@63915
   871
    apply (auto simp: insert_Diff_if)
wenzelm@63654
   872
    apply (drule mk_disjoint_insert)
wenzelm@63654
   873
    apply auto
wenzelm@63654
   874
    done
wenzelm@63654
   875
qed
haftmann@54744
   876
nipkow@64267
   877
lemma sum_diff_nat:
wenzelm@63654
   878
  fixes f :: "'a \<Rightarrow> nat"
wenzelm@63654
   879
  assumes "finite B" and "B \<subseteq> A"
nipkow@64267
   880
  shows "sum f (A - B) = sum f A - sum f B"
wenzelm@63654
   881
  using assms
haftmann@54744
   882
proof induct
wenzelm@63654
   883
  case empty
wenzelm@63654
   884
  then show ?case by simp
haftmann@54744
   885
next
wenzelm@63654
   886
  case (insert x F)
nipkow@64267
   887
  note IH = \<open>F \<subseteq> A \<Longrightarrow> sum f (A - F) = sum f A - sum f F\<close>
wenzelm@63654
   888
  from \<open>x \<notin> F\<close> \<open>insert x F \<subseteq> A\<close> have "x \<in> A - F" by simp
nipkow@64267
   889
  then have A: "sum f ((A - F) - {x}) = sum f (A - F) - f x"
nipkow@64267
   890
    by (simp add: sum_diff1_nat)
wenzelm@63654
   891
  from \<open>insert x F \<subseteq> A\<close> have "F \<subseteq> A" by simp
nipkow@64267
   892
  with IH have "sum f (A - F) = sum f A - sum f F" by simp
nipkow@64267
   893
  with A have B: "sum f ((A - F) - {x}) = sum f A - sum f F - f x"
haftmann@54744
   894
    by simp
wenzelm@63654
   895
  from \<open>x \<notin> F\<close> have "A - insert x F = (A - F) - {x}" by auto
nipkow@64267
   896
  with B have C: "sum f (A - insert x F) = sum f A - sum f F - f x"
haftmann@54744
   897
    by simp
nipkow@64267
   898
  from \<open>finite F\<close> \<open>x \<notin> F\<close> have "sum f (insert x F) = sum f F + f x"
wenzelm@63654
   899
    by simp
nipkow@64267
   900
  with C have "sum f (A - insert x F) = sum f A - sum f (insert x F)"
haftmann@54744
   901
    by simp
wenzelm@63654
   902
  then show ?case by simp
haftmann@54744
   903
qed
haftmann@54744
   904
nipkow@64267
   905
lemma sum_comp_morphism:
nipkow@64267
   906
  "h 0 = 0 \<Longrightarrow> (\<And>x y. h (x + y) = h x + h y) \<Longrightarrow> sum (h \<circ> g) A = h (sum g A)"
wenzelm@63915
   907
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@54744
   908
nipkow@64267
   909
lemma (in comm_semiring_1) dvd_sum: "(\<And>a. a \<in> A \<Longrightarrow> d dvd f a) \<Longrightarrow> d dvd sum f A"
haftmann@59010
   910
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@59010
   911
nipkow@64267
   912
lemma (in ordered_comm_monoid_add) sum_pos:
nipkow@64267
   913
  "finite I \<Longrightarrow> I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> 0 < f i) \<Longrightarrow> 0 < sum f I"
hoelzl@62377
   914
  by (induct I rule: finite_ne_induct) (auto intro: add_pos_pos)
hoelzl@62377
   915
nipkow@64267
   916
lemma (in ordered_comm_monoid_add) sum_pos2:
hoelzl@62377
   917
  assumes I: "finite I" "i \<in> I" "0 < f i" "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
nipkow@64267
   918
  shows "0 < sum f I"
lp15@60974
   919
proof -
nipkow@64267
   920
  have "0 < f i + sum f (I - {i})"
nipkow@64267
   921
    using assms by (intro add_pos_nonneg sum_nonneg) auto
nipkow@64267
   922
  also have "\<dots> = sum f I"
nipkow@64267
   923
    using assms by (simp add: sum.remove)
lp15@60974
   924
  finally show ?thesis .
lp15@60974
   925
qed
lp15@60974
   926
nipkow@64267
   927
lemma sum_cong_Suc:
eberlm@61524
   928
  assumes "0 \<notin> A" "\<And>x. Suc x \<in> A \<Longrightarrow> f (Suc x) = g (Suc x)"
nipkow@64267
   929
  shows "sum f A = sum g A"
nipkow@64267
   930
proof (rule sum.cong)
wenzelm@63654
   931
  fix x
wenzelm@63654
   932
  assume "x \<in> A"
wenzelm@63654
   933
  with assms(1) show "f x = g x"
wenzelm@63654
   934
    by (cases x) (auto intro!: assms(2))
eberlm@61524
   935
qed simp_all
eberlm@61524
   936
haftmann@54744
   937
nipkow@64267
   938
subsubsection \<open>Cardinality as special case of @{const sum}\<close>
haftmann@54744
   939
nipkow@64267
   940
lemma card_eq_sum: "card A = sum (\<lambda>x. 1) A"
haftmann@54744
   941
proof -
haftmann@54744
   942
  have "plus \<circ> (\<lambda>_. Suc 0) = (\<lambda>_. Suc)"
haftmann@54744
   943
    by (simp add: fun_eq_iff)
haftmann@54744
   944
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) = Finite_Set.fold (\<lambda>_. Suc)"
haftmann@54744
   945
    by (rule arg_cong)
haftmann@54744
   946
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) 0 A = Finite_Set.fold (\<lambda>_. Suc) 0 A"
haftmann@54744
   947
    by (blast intro: fun_cong)
wenzelm@63654
   948
  then show ?thesis
nipkow@64267
   949
    by (simp add: card.eq_fold sum.eq_fold)
haftmann@54744
   950
qed
haftmann@54744
   951
haftmann@66936
   952
context semiring_1
haftmann@66936
   953
begin
haftmann@66936
   954
haftmann@66936
   955
lemma sum_constant [simp]:
haftmann@66936
   956
  "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
haftmann@66936
   957
  by (induct A rule: infinite_finite_induct) (simp_all add: algebra_simps)
haftmann@66936
   958
haftmann@66936
   959
end
haftmann@54744
   960
nipkow@64267
   961
lemma sum_Suc: "sum (\<lambda>x. Suc(f x)) A = sum f A + card A"
nipkow@64267
   962
  using sum.distrib[of f "\<lambda>_. 1" A] by simp
nipkow@58349
   963
nipkow@64267
   964
lemma sum_bounded_above:
wenzelm@63654
   965
  fixes K :: "'a::{semiring_1,ordered_comm_monoid_add}"
wenzelm@63654
   966
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> K"
nipkow@64267
   967
  shows "sum f A \<le> of_nat (card A) * K"
haftmann@54744
   968
proof (cases "finite A")
haftmann@54744
   969
  case True
wenzelm@63654
   970
  then show ?thesis
nipkow@64267
   971
    using le sum_mono[where K=A and g = "\<lambda>x. K"] by simp
haftmann@54744
   972
next
wenzelm@63654
   973
  case False
wenzelm@63654
   974
  then show ?thesis by simp
haftmann@54744
   975
qed
haftmann@54744
   976
nipkow@64267
   977
lemma sum_bounded_above_strict:
wenzelm@63654
   978
  fixes K :: "'a::{ordered_cancel_comm_monoid_add,semiring_1}"
wenzelm@63654
   979
  assumes "\<And>i. i\<in>A \<Longrightarrow> f i < K" "card A > 0"
nipkow@64267
   980
  shows "sum f A < of_nat (card A) * K"
nipkow@64267
   981
  using assms sum_strict_mono[where A=A and g = "\<lambda>x. K"]
wenzelm@63654
   982
  by (simp add: card_gt_0_iff)
lp15@60974
   983
nipkow@64267
   984
lemma sum_bounded_below:
wenzelm@63654
   985
  fixes K :: "'a::{semiring_1,ordered_comm_monoid_add}"
wenzelm@63654
   986
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> K \<le> f i"
nipkow@64267
   987
  shows "of_nat (card A) * K \<le> sum f A"
lp15@60974
   988
proof (cases "finite A")
lp15@60974
   989
  case True
wenzelm@63654
   990
  then show ?thesis
nipkow@64267
   991
    using le sum_mono[where K=A and f = "\<lambda>x. K"] by simp
lp15@60974
   992
next
wenzelm@63654
   993
  case False
wenzelm@63654
   994
  then show ?thesis by simp
lp15@60974
   995
qed
lp15@60974
   996
haftmann@54744
   997
lemma card_UN_disjoint:
haftmann@54744
   998
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@54744
   999
    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@54744
  1000
  shows "card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
haftmann@54744
  1001
proof -
wenzelm@63654
  1002
  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)"
wenzelm@63654
  1003
    by simp
wenzelm@63654
  1004
  with assms show ?thesis
nipkow@64267
  1005
    by (simp add: card_eq_sum sum.UNION_disjoint del: sum_constant)
haftmann@54744
  1006
qed
haftmann@54744
  1007
haftmann@54744
  1008
lemma card_Union_disjoint:
wenzelm@63654
  1009
  "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>
nipkow@64267
  1010
    card (\<Union>C) = sum card C"
wenzelm@63654
  1011
  by (frule card_UN_disjoint [of C id]) simp_all
haftmann@54744
  1012
nipkow@64267
  1013
lemma sum_multicount_gen:
haftmann@57418
  1014
  assumes "finite s" "finite t" "\<forall>j\<in>t. (card {i\<in>s. R i j} = k j)"
nipkow@64267
  1015
  shows "sum (\<lambda>i. (card {j\<in>t. R i j})) s = sum k t"
wenzelm@63654
  1016
    (is "?l = ?r")
haftmann@57418
  1017
proof-
nipkow@64267
  1018
  have "?l = sum (\<lambda>i. sum (\<lambda>x.1) {j\<in>t. R i j}) s"
wenzelm@63654
  1019
    by auto
wenzelm@63654
  1020
  also have "\<dots> = ?r"
haftmann@66804
  1021
    unfolding sum.swap_restrict [OF assms(1-2)]
haftmann@57418
  1022
    using assms(3) by auto
haftmann@57418
  1023
  finally show ?thesis .
haftmann@57418
  1024
qed
haftmann@57418
  1025
nipkow@64267
  1026
lemma sum_multicount:
haftmann@57418
  1027
  assumes "finite S" "finite T" "\<forall>j\<in>T. (card {i\<in>S. R i j} = k)"
nipkow@64267
  1028
  shows "sum (\<lambda>i. card {j\<in>T. R i j}) S = k * card T" (is "?l = ?r")
haftmann@57418
  1029
proof-
nipkow@64267
  1030
  have "?l = sum (\<lambda>i. k) T"
nipkow@64267
  1031
    by (rule sum_multicount_gen) (auto simp: assms)
haftmann@57512
  1032
  also have "\<dots> = ?r" by (simp add: mult.commute)
haftmann@57418
  1033
  finally show ?thesis by auto
haftmann@57418
  1034
qed
haftmann@57418
  1035
bulwahn@67511
  1036
lemma sum_card_image:
bulwahn@67511
  1037
  assumes "finite A"
bulwahn@67511
  1038
  assumes "\<forall>s\<in>A. \<forall>t\<in>A. s \<noteq> t \<longrightarrow> (f s) \<inter> (f t) = {}"
bulwahn@67511
  1039
  shows "sum card (f ` A) = sum (\<lambda>a. card (f a)) A"
bulwahn@67511
  1040
using assms
bulwahn@67511
  1041
proof (induct A)
bulwahn@67511
  1042
  case empty
bulwahn@67511
  1043
  from this show ?case by simp
bulwahn@67511
  1044
next
bulwahn@67511
  1045
  case (insert a A)
bulwahn@67511
  1046
  show ?case
bulwahn@67511
  1047
  proof cases
bulwahn@67511
  1048
    assume "f a = {}"
bulwahn@67511
  1049
    from this insert show ?case
bulwahn@67511
  1050
      by (subst sum.mono_neutral_right[where S="f ` A"]) auto
bulwahn@67511
  1051
  next
bulwahn@67511
  1052
    assume "f a \<noteq> {}"
bulwahn@67511
  1053
    from this have "sum card (insert (f a) (f ` A)) = card (f a) + sum card (f ` A)"
bulwahn@67511
  1054
      using insert by (subst sum.insert) auto
bulwahn@67511
  1055
    from this insert show ?case by simp
bulwahn@67511
  1056
  qed
bulwahn@67511
  1057
qed
bulwahn@67511
  1058
bulwahn@67511
  1059
lemma card_Union_image:
bulwahn@67511
  1060
  assumes "finite S"
bulwahn@67511
  1061
  assumes "\<forall>s\<in>f ` S. finite s"
bulwahn@67511
  1062
  assumes "\<forall>s\<in>S. \<forall>t\<in>S. s \<noteq> t \<longrightarrow> (f s) \<inter> (f t) = {}"
bulwahn@67511
  1063
  shows "card (\<Union>(f ` S)) = sum (\<lambda>x. card (f x)) S"
bulwahn@67511
  1064
proof -
bulwahn@67511
  1065
  have "\<forall>A\<in>f ` S. \<forall>B\<in>f ` S. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
bulwahn@67511
  1066
    using assms(3) by (metis image_iff)
bulwahn@67511
  1067
  from this have "card (\<Union>(f ` S)) = sum card (f ` S)"
bulwahn@67511
  1068
    using assms(1, 2) by (subst card_Union_disjoint) auto
bulwahn@67511
  1069
  also have "... = sum (\<lambda>x. card (f x)) S"
bulwahn@67511
  1070
    using assms(1, 3) by (auto simp add: sum_card_image)
bulwahn@67511
  1071
  finally show ?thesis .
bulwahn@67511
  1072
qed
wenzelm@63654
  1073
wenzelm@60758
  1074
subsubsection \<open>Cardinality of products\<close>
haftmann@54744
  1075
haftmann@54744
  1076
lemma card_SigmaI [simp]:
wenzelm@63654
  1077
  "finite A \<Longrightarrow> \<forall>a\<in>A. finite (B a) \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
nipkow@64267
  1078
  by (simp add: card_eq_sum sum.Sigma del: sum_constant)
haftmann@54744
  1079
haftmann@54744
  1080
(*
haftmann@54744
  1081
lemma SigmaI_insert: "y \<notin> A ==>
wenzelm@61943
  1082
  (SIGMA x:(insert y A). B x) = (({y} \<times> (B y)) \<union> (SIGMA x: A. B x))"
haftmann@54744
  1083
  by auto
haftmann@54744
  1084
*)
haftmann@54744
  1085
wenzelm@63654
  1086
lemma card_cartesian_product: "card (A \<times> B) = card A * card B"
haftmann@54744
  1087
  by (cases "finite A \<and> finite B")
haftmann@54744
  1088
    (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)
haftmann@54744
  1089
wenzelm@63654
  1090
lemma card_cartesian_product_singleton:  "card ({x} \<times> A) = card A"
wenzelm@63654
  1091
  by (simp add: card_cartesian_product)
haftmann@54744
  1092
haftmann@54744
  1093
wenzelm@60758
  1094
subsection \<open>Generalized product over a set\<close>
haftmann@54744
  1095
haftmann@54744
  1096
context comm_monoid_mult
haftmann@54744
  1097
begin
haftmann@54744
  1098
nipkow@64272
  1099
sublocale prod: comm_monoid_set times 1
nipkow@64272
  1100
  defines prod = prod.F ..
haftmann@54744
  1101
nipkow@64272
  1102
abbreviation Prod ("\<Prod>_" [1000] 999)
nipkow@64272
  1103
  where "\<Prod>A \<equiv> prod (\<lambda>x. x) A"
haftmann@54744
  1104
haftmann@54744
  1105
end
haftmann@54744
  1106
wenzelm@61955
  1107
syntax (ASCII)
lp15@67268
  1108
  "_prod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(4PROD (_/:_)./ _)" [0, 51, 10] 10)
wenzelm@61955
  1109
syntax
lp15@67268
  1110
  "_prod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(2\<Prod>(_/\<in>_)./ _)" [0, 51, 10] 10)
wenzelm@61799
  1111
translations \<comment> \<open>Beware of argument permutation!\<close>
nipkow@64272
  1112
  "\<Prod>i\<in>A. b" == "CONST prod (\<lambda>i. b) A"
haftmann@54744
  1113
wenzelm@61955
  1114
text \<open>Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter \<open>\<Prod>x|P. e\<close>.\<close>
haftmann@54744
  1115
wenzelm@61955
  1116
syntax (ASCII)
nipkow@64272
  1117
  "_qprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a"  ("(4PROD _ |/ _./ _)" [0, 0, 10] 10)
haftmann@54744
  1118
syntax
nipkow@64272
  1119
  "_qprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a"  ("(2\<Prod>_ | (_)./ _)" [0, 0, 10] 10)
haftmann@54744
  1120
translations
nipkow@64272
  1121
  "\<Prod>x|P. t" => "CONST prod (\<lambda>x. t) {x. P}"
haftmann@54744
  1122
haftmann@59010
  1123
context comm_monoid_mult
haftmann@59010
  1124
begin
haftmann@59010
  1125
nipkow@64272
  1126
lemma prod_dvd_prod: "(\<And>a. a \<in> A \<Longrightarrow> f a dvd g a) \<Longrightarrow> prod f A dvd prod g A"
haftmann@59010
  1127
proof (induct A rule: infinite_finite_induct)
wenzelm@63654
  1128
  case infinite
wenzelm@63654
  1129
  then show ?case by (auto intro: dvdI)
wenzelm@63654
  1130
next
wenzelm@63654
  1131
  case empty
wenzelm@63654
  1132
  then show ?case by (auto intro: dvdI)
haftmann@59010
  1133
next
wenzelm@63654
  1134
  case (insert a A)
nipkow@64272
  1135
  then have "f a dvd g a" and "prod f A dvd prod g A"
wenzelm@63654
  1136
    by simp_all
nipkow@64272
  1137
  then obtain r s where "g a = f a * r" and "prod g A = prod f A * s"
wenzelm@63654
  1138
    by (auto elim!: dvdE)
nipkow@64272
  1139
  then have "g a * prod g A = f a * prod f A * (r * s)"
wenzelm@63654
  1140
    by (simp add: ac_simps)
wenzelm@63654
  1141
  with insert.hyps show ?case
wenzelm@63654
  1142
    by (auto intro: dvdI)
haftmann@59010
  1143
qed
haftmann@59010
  1144
nipkow@64272
  1145
lemma prod_dvd_prod_subset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> prod f A dvd prod f B"
nipkow@64272
  1146
  by (auto simp add: prod.subset_diff ac_simps intro: dvdI)
haftmann@59010
  1147
haftmann@59010
  1148
end
haftmann@59010
  1149
haftmann@54744
  1150
wenzelm@60758
  1151
subsubsection \<open>Properties in more restricted classes of structures\<close>
haftmann@54744
  1152
lp15@65687
  1153
context linordered_nonzero_semiring
lp15@65687
  1154
begin
lp15@65687
  1155
  
lp15@65687
  1156
lemma prod_ge_1: "(\<And>x. x \<in> A \<Longrightarrow> 1 \<le> f x) \<Longrightarrow> 1 \<le> prod f A"
lp15@65687
  1157
proof (induct A rule: infinite_finite_induct)
lp15@65687
  1158
  case infinite
lp15@65687
  1159
  then show ?case by simp
lp15@65687
  1160
next
lp15@65687
  1161
  case empty
lp15@65687
  1162
  then show ?case by simp
lp15@65687
  1163
next
lp15@65687
  1164
  case (insert x F)
lp15@65687
  1165
  have "1 * 1 \<le> f x * prod f F"
lp15@65687
  1166
    by (rule mult_mono') (use insert in auto)
lp15@65687
  1167
  with insert show ?case by simp
lp15@65687
  1168
qed
lp15@65687
  1169
lp15@65687
  1170
lemma prod_le_1:
lp15@65687
  1171
  fixes f :: "'b \<Rightarrow> 'a"
lp15@65687
  1172
  assumes "\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x \<and> f x \<le> 1"
lp15@65687
  1173
  shows "prod f A \<le> 1"
lp15@65687
  1174
    using assms
lp15@65687
  1175
proof (induct A rule: infinite_finite_induct)
lp15@65687
  1176
  case infinite
lp15@65687
  1177
  then show ?case by simp
lp15@65687
  1178
next
lp15@65687
  1179
  case empty
lp15@65687
  1180
  then show ?case by simp
lp15@65687
  1181
next
lp15@65687
  1182
  case (insert x F)
lp15@65687
  1183
  then show ?case by (force simp: mult.commute intro: dest: mult_le_one)
lp15@65687
  1184
qed
lp15@65687
  1185
lp15@65687
  1186
end
lp15@65687
  1187
haftmann@59010
  1188
context comm_semiring_1
haftmann@59010
  1189
begin
haftmann@54744
  1190
nipkow@64272
  1191
lemma dvd_prod_eqI [intro]:
haftmann@59010
  1192
  assumes "finite A" and "a \<in> A" and "b = f a"
nipkow@64272
  1193
  shows "b dvd prod f A"
haftmann@59010
  1194
proof -
nipkow@64272
  1195
  from \<open>finite A\<close> have "prod f (insert a (A - {a})) = f a * prod f (A - {a})"
nipkow@64272
  1196
    by (intro prod.insert) auto
wenzelm@63654
  1197
  also from \<open>a \<in> A\<close> have "insert a (A - {a}) = A"
wenzelm@63654
  1198
    by blast
nipkow@64272
  1199
  finally have "prod f A = f a * prod f (A - {a})" .
wenzelm@63654
  1200
  with \<open>b = f a\<close> show ?thesis
wenzelm@63654
  1201
    by simp
haftmann@59010
  1202
qed
haftmann@54744
  1203
nipkow@64272
  1204
lemma dvd_prodI [intro]: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> f a dvd prod f A"
wenzelm@63654
  1205
  by auto
haftmann@54744
  1206
nipkow@64272
  1207
lemma prod_zero:
haftmann@59010
  1208
  assumes "finite A" and "\<exists>a\<in>A. f a = 0"
nipkow@64272
  1209
  shows "prod f A = 0"
wenzelm@63654
  1210
  using assms
wenzelm@63654
  1211
proof (induct A)
wenzelm@63654
  1212
  case empty
wenzelm@63654
  1213
  then show ?case by simp
haftmann@59010
  1214
next
haftmann@59010
  1215
  case (insert a A)
haftmann@59010
  1216
  then have "f a = 0 \<or> (\<exists>a\<in>A. f a = 0)" by simp
nipkow@64272
  1217
  then have "f a * prod f A = 0" by rule (simp_all add: insert)
haftmann@59010
  1218
  with insert show ?case by simp
haftmann@59010
  1219
qed
haftmann@54744
  1220
nipkow@64272
  1221
lemma prod_dvd_prod_subset2:
haftmann@59010
  1222
  assumes "finite B" and "A \<subseteq> B" and "\<And>a. a \<in> A \<Longrightarrow> f a dvd g a"
nipkow@64272
  1223
  shows "prod f A dvd prod g B"
haftmann@59010
  1224
proof -
nipkow@64272
  1225
  from assms have "prod f A dvd prod g A"
nipkow@64272
  1226
    by (auto intro: prod_dvd_prod)
nipkow@64272
  1227
  moreover from assms have "prod g A dvd prod g B"
nipkow@64272
  1228
    by (auto intro: prod_dvd_prod_subset)
haftmann@59010
  1229
  ultimately show ?thesis by (rule dvd_trans)
haftmann@59010
  1230
qed
haftmann@59010
  1231
haftmann@59010
  1232
end
haftmann@59010
  1233
nipkow@64272
  1234
lemma (in semidom) prod_zero_iff [simp]:
haftmann@63924
  1235
  fixes f :: "'b \<Rightarrow> 'a"
haftmann@59010
  1236
  assumes "finite A"
nipkow@64272
  1237
  shows "prod f A = 0 \<longleftrightarrow> (\<exists>a\<in>A. f a = 0)"
haftmann@59010
  1238
  using assms by (induct A) (auto simp: no_zero_divisors)
haftmann@59010
  1239
nipkow@64272
  1240
lemma (in semidom_divide) prod_diff1:
haftmann@60353
  1241
  assumes "finite A" and "f a \<noteq> 0"
nipkow@64272
  1242
  shows "prod f (A - {a}) = (if a \<in> A then prod f A div f a else prod f A)"
haftmann@60353
  1243
proof (cases "a \<notin> A")
wenzelm@63654
  1244
  case True
wenzelm@63654
  1245
  then show ?thesis by simp
haftmann@60353
  1246
next
wenzelm@63654
  1247
  case False
wenzelm@63654
  1248
  with assms show ?thesis
wenzelm@63654
  1249
  proof induct
wenzelm@63654
  1250
    case empty
wenzelm@63654
  1251
    then show ?case by simp
haftmann@60353
  1252
  next
haftmann@60353
  1253
    case (insert b B)
haftmann@60353
  1254
    then show ?case
haftmann@60353
  1255
    proof (cases "a = b")
wenzelm@63654
  1256
      case True
wenzelm@63654
  1257
      with insert show ?thesis by simp
haftmann@60353
  1258
    next
wenzelm@63654
  1259
      case False
wenzelm@63654
  1260
      with insert have "a \<in> B" by simp
wenzelm@63040
  1261
      define C where "C = B - {a}"
wenzelm@63654
  1262
      with \<open>finite B\<close> \<open>a \<in> B\<close> have "B = insert a C" "finite C" "a \<notin> C"
wenzelm@63654
  1263
        by auto
wenzelm@63654
  1264
      with insert show ?thesis
wenzelm@63654
  1265
        by (auto simp add: insert_commute ac_simps)
haftmann@60353
  1266
    qed
haftmann@60353
  1267
  qed
haftmann@60353
  1268
qed
haftmann@54744
  1269
nipkow@64267
  1270
lemma sum_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
  1271
  for c :: "nat \<Rightarrow> 'a::division_ring"
wenzelm@63654
  1272
  by (induct A rule: infinite_finite_induct) auto
haftmann@62481
  1273
nipkow@64267
  1274
lemma sum_zero_power' [simp]:
wenzelm@63654
  1275
  "(\<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
  1276
  for c :: "nat \<Rightarrow> 'a::field"
nipkow@64267
  1277
  using sum_zero_power [of "\<lambda>i. c i / d i" A] by auto
haftmann@62481
  1278
lp15@65680
  1279
lemma (in field) prod_inversef: "prod (inverse \<circ> f) A = inverse (prod f A)"
lp15@65680
  1280
 proof (cases "finite A")
lp15@65680
  1281
   case True
lp15@65680
  1282
   then show ?thesis
lp15@65680
  1283
     by (induct A rule: finite_induct) simp_all
lp15@65680
  1284
 next
lp15@65680
  1285
   case False
lp15@65680
  1286
   then show ?thesis
lp15@65680
  1287
     by auto
lp15@65680
  1288
 qed
haftmann@59010
  1289
lp15@65680
  1290
lemma (in field) prod_dividef: "(\<Prod>x\<in>A. f x / g x) = prod f A / prod g A"
lp15@65680
  1291
  using prod_inversef [of g A] by (simp add: divide_inverse prod.distrib)
haftmann@54744
  1292
nipkow@64272
  1293
lemma prod_Un:
haftmann@59010
  1294
  fixes f :: "'b \<Rightarrow> 'a :: field"
haftmann@59010
  1295
  assumes "finite A" and "finite B"
wenzelm@63654
  1296
    and "\<forall>x\<in>A \<inter> B. f x \<noteq> 0"
nipkow@64272
  1297
  shows "prod f (A \<union> B) = prod f A * prod f B / prod f (A \<inter> B)"
haftmann@59010
  1298
proof -
nipkow@64272
  1299
  from assms have "prod f A * prod f B = prod f (A \<union> B) * prod f (A \<inter> B)"
nipkow@64272
  1300
    by (simp add: prod.union_inter [symmetric, of A B])
wenzelm@63654
  1301
  with assms show ?thesis
wenzelm@63654
  1302
    by simp
haftmann@59010
  1303
qed
haftmann@54744
  1304
nipkow@64272
  1305
lemma (in linordered_semidom) prod_nonneg: "(\<forall>a\<in>A. 0 \<le> f a) \<Longrightarrow> 0 \<le> prod f A"
haftmann@59010
  1306
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@59010
  1307
nipkow@64272
  1308
lemma (in linordered_semidom) prod_pos: "(\<forall>a\<in>A. 0 < f a) \<Longrightarrow> 0 < prod f A"
haftmann@59010
  1309
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@59010
  1310
nipkow@64272
  1311
lemma (in linordered_semidom) prod_mono:
lp15@67673
  1312
  "(\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i \<and> f i \<le> g i) \<Longrightarrow> prod f A \<le> prod g A"
lp15@67673
  1313
  by (induct A rule: infinite_finite_induct) (force intro!: prod_nonneg mult_mono)+
haftmann@54744
  1314
nipkow@64272
  1315
lemma (in linordered_semidom) prod_mono_strict:
lp15@67673
  1316
  assumes "finite A" "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i \<and> f i < g i" "A \<noteq> {}"
nipkow@64272
  1317
  shows "prod f A < prod g A"
wenzelm@63654
  1318
  using assms
wenzelm@63654
  1319
proof (induct A rule: finite_induct)
wenzelm@63654
  1320
  case empty
wenzelm@63654
  1321
  then show ?case by simp
wenzelm@63654
  1322
next
wenzelm@63654
  1323
  case insert
nipkow@64272
  1324
  then show ?case by (force intro: mult_strict_mono' prod_nonneg)
wenzelm@63654
  1325
qed
lp15@60974
  1326
nipkow@64272
  1327
lemma (in linordered_field) abs_prod: "\<bar>prod f A\<bar> = (\<Prod>x\<in>A. \<bar>f x\<bar>)"
haftmann@59010
  1328
  by (induct A rule: infinite_finite_induct) (simp_all add: abs_mult)
haftmann@54744
  1329
nipkow@64272
  1330
lemma prod_eq_1_iff [simp]: "finite A \<Longrightarrow> prod f A = 1 \<longleftrightarrow> (\<forall>a\<in>A. f a = 1)"
wenzelm@63654
  1331
  for f :: "'a \<Rightarrow> nat"
haftmann@59010
  1332
  by (induct A rule: finite_induct) simp_all
haftmann@54744
  1333
nipkow@64272
  1334
lemma prod_pos_nat_iff [simp]: "finite A \<Longrightarrow> prod f A > 0 \<longleftrightarrow> (\<forall>a\<in>A. f a > 0)"
wenzelm@63654
  1335
  for f :: "'a \<Rightarrow> nat"
nipkow@64272
  1336
  using prod_zero_iff by (simp del: neq0_conv add: zero_less_iff_neq_zero)
haftmann@54744
  1337
lp15@67969
  1338
lemma prod_constant [simp]: "(\<Prod>x\<in> A. y) = y ^ card A"
wenzelm@63654
  1339
  for y :: "'a::comm_monoid_mult"
haftmann@62481
  1340
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@62481
  1341
nipkow@64272
  1342
lemma prod_power_distrib: "prod f A ^ n = prod (\<lambda>x. (f x) ^ n) A"
wenzelm@63654
  1343
  for f :: "'a \<Rightarrow> 'b::comm_semiring_1"
wenzelm@63654
  1344
  by (induct A rule: infinite_finite_induct) (auto simp add: power_mult_distrib)
haftmann@62481
  1345
nipkow@64267
  1346
lemma power_sum: "c ^ (\<Sum>a\<in>A. f a) = (\<Prod>a\<in>A. c ^ f a)"
haftmann@62481
  1347
  by (induct A rule: infinite_finite_induct) (simp_all add: power_add)
haftmann@62481
  1348
nipkow@64272
  1349
lemma prod_gen_delta:
wenzelm@63654
  1350
  fixes b :: "'b \<Rightarrow> 'a::comm_monoid_mult"
wenzelm@63654
  1351
  assumes fin: "finite S"
nipkow@64272
  1352
  shows "prod (\<lambda>k. if k = a then b k else c) S =
wenzelm@63654
  1353
    (if a \<in> S then b a * c ^ (card S - 1) else c ^ card S)"
wenzelm@63654
  1354
proof -
haftmann@62481
  1355
  let ?f = "(\<lambda>k. if k=a then b k else c)"
wenzelm@63654
  1356
  show ?thesis
wenzelm@63654
  1357
  proof (cases "a \<in> S")
wenzelm@63654
  1358
    case False
wenzelm@63654
  1359
    then have "\<forall> k\<in> S. ?f k = c" by simp
nipkow@64272
  1360
    with False show ?thesis by (simp add: prod_constant)
wenzelm@63654
  1361
  next
wenzelm@63654
  1362
    case True
haftmann@62481
  1363
    let ?A = "S - {a}"
haftmann@62481
  1364
    let ?B = "{a}"
wenzelm@63654
  1365
    from True have eq: "S = ?A \<union> ?B" by blast
wenzelm@63654
  1366
    have disjoint: "?A \<inter> ?B = {}" by simp
wenzelm@63654
  1367
    from fin have fin': "finite ?A" "finite ?B" by auto
nipkow@64272
  1368
    have f_A0: "prod ?f ?A = prod (\<lambda>i. c) ?A"
nipkow@64272
  1369
      by (rule prod.cong) auto
wenzelm@63654
  1370
    from fin True have card_A: "card ?A = card S - 1" by auto
nipkow@64272
  1371
    have f_A1: "prod ?f ?A = c ^ card ?A"
nipkow@64272
  1372
      unfolding f_A0 by (rule prod_constant)
nipkow@64272
  1373
    have "prod ?f ?A * prod ?f ?B = prod ?f S"
nipkow@64272
  1374
      using prod.union_disjoint[OF fin' disjoint, of ?f, unfolded eq[symmetric]]
haftmann@62481
  1375
      by simp
wenzelm@63654
  1376
    with True card_A show ?thesis
nipkow@64272
  1377
      by (simp add: f_A1 field_simps cong add: prod.cong cong del: if_weak_cong)
wenzelm@63654
  1378
  qed
haftmann@62481
  1379
qed
haftmann@62481
  1380
nipkow@64267
  1381
lemma sum_image_le:
lp15@63952
  1382
  fixes g :: "'a \<Rightarrow> 'b::ordered_ab_group_add"
lp15@63952
  1383
  assumes "finite I" "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> g(f i)"
nipkow@64267
  1384
    shows "sum g (f ` I) \<le> sum (g \<circ> f) I"
lp15@63952
  1385
  using assms
lp15@63952
  1386
proof induction
lp15@63952
  1387
  case empty
lp15@63952
  1388
  then show ?case by auto
lp15@63952
  1389
next
lp15@63952
  1390
  case (insert x F) then
nipkow@64267
  1391
  have "sum g (f ` insert x F) = sum g (insert (f x) (f ` F))" by simp
nipkow@64267
  1392
  also have "\<dots> \<le> g (f x) + sum g (f ` F)"
nipkow@64267
  1393
    by (simp add: insert sum.insert_if)
nipkow@64267
  1394
  also have "\<dots>  \<le> sum (g \<circ> f) (insert x F)"
lp15@63952
  1395
    using insert by auto
lp15@63952
  1396
  finally show ?case .
lp15@63952
  1397
qed
lp15@63952
  1398
 
haftmann@54744
  1399
end