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