src/HOL/Big_Operators.thy
author hoelzl
Tue Mar 26 12:20:58 2013 +0100 (2013-03-26)
changeset 51526 155263089e7b
parent 51489 f738e6dbd844
child 51540 eea5c4ca4a0e
permissions -rw-r--r--
move SEQ.thy and Lim.thy to Limits.thy
haftmann@35719
     1
(*  Title:      HOL/Big_Operators.thy
wenzelm@12396
     2
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
avigad@16775
     3
                with contributions by Jeremy Avigad
wenzelm@12396
     4
*)
wenzelm@12396
     5
haftmann@35719
     6
header {* Big operators and finite (non-empty) sets *}
haftmann@26041
     7
haftmann@35719
     8
theory Big_Operators
haftmann@51489
     9
imports Finite_Set Option Metis
haftmann@26041
    10
begin
haftmann@26041
    11
haftmann@35816
    12
subsection {* Generic monoid operation over a set *}
haftmann@35816
    13
haftmann@35816
    14
no_notation times (infixl "*" 70)
haftmann@35816
    15
no_notation Groups.one ("1")
haftmann@35816
    16
haftmann@51489
    17
locale comm_monoid_set = comm_monoid
haftmann@51489
    18
begin
haftmann@35816
    19
haftmann@51489
    20
definition F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@51489
    21
where
haftmann@51489
    22
  eq_fold: "F g A = Finite_Set.fold (f \<circ> g) 1 A"
haftmann@35816
    23
haftmann@35816
    24
lemma infinite [simp]:
haftmann@35816
    25
  "\<not> finite A \<Longrightarrow> F g A = 1"
haftmann@51489
    26
  by (simp add: eq_fold)
haftmann@51489
    27
haftmann@51489
    28
lemma empty [simp]:
haftmann@51489
    29
  "F g {} = 1"
haftmann@51489
    30
  by (simp add: eq_fold)
haftmann@51489
    31
haftmann@51489
    32
lemma insert [simp]:
haftmann@51489
    33
  assumes "finite A" and "x \<notin> A"
haftmann@51489
    34
  shows "F g (insert x A) = g x * F g A"
haftmann@51489
    35
proof -
haftmann@51489
    36
  interpret comp_fun_commute f
haftmann@51489
    37
    by default (simp add: fun_eq_iff left_commute)
haftmann@51489
    38
  interpret comp_fun_commute "f \<circ> g"
haftmann@51489
    39
    by (rule comp_comp_fun_commute)
haftmann@51489
    40
  from assms show ?thesis by (simp add: eq_fold)
haftmann@51489
    41
qed
haftmann@51489
    42
haftmann@51489
    43
lemma remove:
haftmann@51489
    44
  assumes "finite A" and "x \<in> A"
haftmann@51489
    45
  shows "F g A = g x * F g (A - {x})"
haftmann@51489
    46
proof -
haftmann@51489
    47
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@51489
    48
    by (auto dest: mk_disjoint_insert)
haftmann@51489
    49
  moreover from `finite A` this have "finite B" by simp
haftmann@51489
    50
  ultimately show ?thesis by simp
haftmann@51489
    51
qed
haftmann@51489
    52
haftmann@51489
    53
lemma insert_remove:
haftmann@51489
    54
  assumes "finite A"
haftmann@51489
    55
  shows "F g (insert x A) = g x * F g (A - {x})"
haftmann@51489
    56
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@51489
    57
haftmann@51489
    58
lemma neutral:
haftmann@51489
    59
  assumes "\<forall>x\<in>A. g x = 1"
haftmann@51489
    60
  shows "F g A = 1"
haftmann@51489
    61
proof (cases "finite A")
haftmann@51489
    62
  case True from `finite A` assms show ?thesis by (induct A) simp_all
haftmann@51489
    63
next
haftmann@51489
    64
  case False then show ?thesis by simp
haftmann@51489
    65
qed
haftmann@35816
    66
haftmann@51489
    67
lemma neutral_const [simp]:
haftmann@51489
    68
  "F (\<lambda>_. 1) A = 1"
haftmann@51489
    69
  by (simp add: neutral)
haftmann@51489
    70
haftmann@51489
    71
lemma union_inter:
haftmann@51489
    72
  assumes "finite A" and "finite B"
haftmann@51489
    73
  shows "F g (A \<union> B) * F g (A \<inter> B) = F g A * F g B"
haftmann@51489
    74
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
haftmann@51489
    75
using assms proof (induct A)
haftmann@51489
    76
  case empty then show ?case by simp
hoelzl@42986
    77
next
haftmann@51489
    78
  case (insert x A) then show ?case
haftmann@51489
    79
    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
haftmann@51489
    80
qed
haftmann@51489
    81
haftmann@51489
    82
corollary union_inter_neutral:
haftmann@51489
    83
  assumes "finite A" and "finite B"
haftmann@51489
    84
  and I0: "\<forall>x \<in> A \<inter> B. g x = 1"
haftmann@51489
    85
  shows "F g (A \<union> B) = F g A * F g B"
haftmann@51489
    86
  using assms by (simp add: union_inter [symmetric] neutral)
haftmann@51489
    87
haftmann@51489
    88
corollary union_disjoint:
haftmann@51489
    89
  assumes "finite A" and "finite B"
haftmann@51489
    90
  assumes "A \<inter> B = {}"
haftmann@51489
    91
  shows "F g (A \<union> B) = F g A * F g B"
haftmann@51489
    92
  using assms by (simp add: union_inter_neutral)
haftmann@51489
    93
haftmann@51489
    94
lemma subset_diff:
haftmann@51489
    95
  "B \<subseteq> A \<Longrightarrow> finite A \<Longrightarrow> F g A = F g (A - B) * F g B"
haftmann@51489
    96
  by (metis Diff_partition union_disjoint Diff_disjoint finite_Un inf_commute sup_commute)
haftmann@51489
    97
haftmann@51489
    98
lemma reindex:
haftmann@51489
    99
  assumes "inj_on h A"
haftmann@51489
   100
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@51489
   101
proof (cases "finite A")
haftmann@51489
   102
  case True
haftmann@51489
   103
  interpret comp_fun_commute f
haftmann@51489
   104
    by default (simp add: fun_eq_iff left_commute)
haftmann@51489
   105
  interpret comp_fun_commute "f \<circ> g"
haftmann@51489
   106
    by (rule comp_comp_fun_commute)
haftmann@51489
   107
  from assms `finite A` show ?thesis by (simp add: eq_fold fold_image comp_assoc)
haftmann@51489
   108
next
haftmann@51489
   109
  case False with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
haftmann@51489
   110
  with False show ?thesis by simp
hoelzl@42986
   111
qed
hoelzl@42986
   112
haftmann@51489
   113
lemma cong:
haftmann@51489
   114
  assumes "A = B"
haftmann@51489
   115
  assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x"
haftmann@51489
   116
  shows "F g A = F h B"
haftmann@51489
   117
proof (cases "finite A")
haftmann@51489
   118
  case True
haftmann@51489
   119
  then have "\<And>C. C \<subseteq> A \<longrightarrow> (\<forall>x\<in>C. g x = h x) \<longrightarrow> F g C = F h C"
haftmann@51489
   120
  proof induct
haftmann@51489
   121
    case empty then show ?case by simp
haftmann@51489
   122
  next
haftmann@51489
   123
    case (insert x F) then show ?case apply -
haftmann@51489
   124
    apply (simp add: subset_insert_iff, clarify)
haftmann@51489
   125
    apply (subgoal_tac "finite C")
haftmann@51489
   126
      prefer 2 apply (blast dest: finite_subset [rotated])
haftmann@51489
   127
    apply (subgoal_tac "C = insert x (C - {x})")
haftmann@51489
   128
      prefer 2 apply blast
haftmann@51489
   129
    apply (erule ssubst)
haftmann@51489
   130
    apply (simp add: Ball_def del: insert_Diff_single)
haftmann@51489
   131
    done
haftmann@51489
   132
  qed
haftmann@51489
   133
  with `A = B` g_h show ?thesis by simp
haftmann@51489
   134
next
haftmann@51489
   135
  case False
haftmann@51489
   136
  with `A = B` show ?thesis by simp
haftmann@51489
   137
qed
nipkow@48849
   138
haftmann@51489
   139
lemma strong_cong [cong]:
haftmann@51489
   140
  assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x"
haftmann@51489
   141
  shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B"
haftmann@51489
   142
  by (rule cong) (insert assms, simp_all add: simp_implies_def)
haftmann@51489
   143
haftmann@51489
   144
lemma UNION_disjoint:
haftmann@51489
   145
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@51489
   146
  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@51489
   147
  shows "F g (UNION I A) = F (\<lambda>x. F g (A x)) I"
haftmann@51489
   148
apply (insert assms)
haftmann@51489
   149
apply (induct rule: finite_induct)
haftmann@51489
   150
apply simp
haftmann@51489
   151
apply atomize
haftmann@51489
   152
apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
haftmann@51489
   153
 prefer 2 apply blast
haftmann@51489
   154
apply (subgoal_tac "A x Int UNION Fa A = {}")
haftmann@51489
   155
 prefer 2 apply blast
haftmann@51489
   156
apply (simp add: union_disjoint)
haftmann@51489
   157
done
haftmann@51489
   158
haftmann@51489
   159
lemma Union_disjoint:
haftmann@51489
   160
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
haftmann@51489
   161
  shows "F g (Union C) = F (F g) C"
haftmann@51489
   162
proof cases
haftmann@51489
   163
  assume "finite C"
haftmann@51489
   164
  from UNION_disjoint [OF this assms]
haftmann@51489
   165
  show ?thesis
haftmann@51489
   166
    by (simp add: SUP_def)
haftmann@51489
   167
qed (auto dest: finite_UnionD intro: infinite)
nipkow@48821
   168
haftmann@51489
   169
lemma distrib:
haftmann@51489
   170
  "F (\<lambda>x. g x * h x) A = F g A * F h A"
haftmann@51489
   171
proof (cases "finite A")
haftmann@51489
   172
  case False then show ?thesis by simp
haftmann@51489
   173
next
haftmann@51489
   174
  case True then show ?thesis by (rule finite_induct) (simp_all add: assoc commute left_commute)
haftmann@51489
   175
qed
haftmann@51489
   176
haftmann@51489
   177
lemma Sigma:
haftmann@51489
   178
  "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@51489
   179
apply (subst Sigma_def)
haftmann@51489
   180
apply (subst UNION_disjoint, assumption, simp)
haftmann@51489
   181
 apply blast
haftmann@51489
   182
apply (rule cong)
haftmann@51489
   183
apply rule
haftmann@51489
   184
apply (simp add: fun_eq_iff)
haftmann@51489
   185
apply (subst UNION_disjoint, simp, simp)
haftmann@51489
   186
 apply blast
haftmann@51489
   187
apply (simp add: comp_def)
haftmann@51489
   188
done
haftmann@51489
   189
haftmann@51489
   190
lemma related: 
haftmann@51489
   191
  assumes Re: "R 1 1" 
haftmann@51489
   192
  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
haftmann@51489
   193
  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
haftmann@51489
   194
  shows "R (F h S) (F g S)"
haftmann@51489
   195
  using fS by (rule finite_subset_induct) (insert assms, auto)
nipkow@48849
   196
haftmann@51489
   197
lemma eq_general:
haftmann@51489
   198
  assumes h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y" 
haftmann@51489
   199
  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x"
haftmann@51489
   200
  shows "F f1 S = F f2 S'"
haftmann@51489
   201
proof-
haftmann@51489
   202
  from h f12 have hS: "h ` S = S'" by blast
haftmann@51489
   203
  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
haftmann@51489
   204
    from f12 h H  have "x = y" by auto }
haftmann@51489
   205
  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
haftmann@51489
   206
  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
haftmann@51489
   207
  from hS have "F f2 S' = F f2 (h ` S)" by simp
haftmann@51489
   208
  also have "\<dots> = F (f2 o h) S" using reindex [OF hinj, of f2] .
haftmann@51489
   209
  also have "\<dots> = F f1 S " using th cong [of _ _ "f2 o h" f1]
haftmann@51489
   210
    by blast
haftmann@51489
   211
  finally show ?thesis ..
haftmann@51489
   212
qed
nipkow@48849
   213
haftmann@51489
   214
lemma eq_general_reverses:
haftmann@51489
   215
  assumes kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
haftmann@51489
   216
  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x"
haftmann@51489
   217
  shows "F j S = F g T"
haftmann@51489
   218
  (* metis solves it, but not yet available here *)
haftmann@51489
   219
  apply (rule eq_general [of T S h g j])
haftmann@51489
   220
  apply (rule ballI)
haftmann@51489
   221
  apply (frule kh)
haftmann@51489
   222
  apply (rule ex1I[])
haftmann@51489
   223
  apply blast
haftmann@51489
   224
  apply clarsimp
haftmann@51489
   225
  apply (drule hk) apply simp
haftmann@51489
   226
  apply (rule sym)
haftmann@51489
   227
  apply (erule conjunct1[OF conjunct2[OF hk]])
haftmann@51489
   228
  apply (rule ballI)
haftmann@51489
   229
  apply (drule hk)
haftmann@51489
   230
  apply blast
haftmann@51489
   231
  done
haftmann@51489
   232
haftmann@51489
   233
lemma mono_neutral_cong_left:
nipkow@48849
   234
  assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1"
nipkow@48849
   235
  and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T"
nipkow@48849
   236
proof-
nipkow@48849
   237
  have eq: "T = S \<union> (T - S)" using `S \<subseteq> T` by blast
nipkow@48849
   238
  have d: "S \<inter> (T - S) = {}" using `S \<subseteq> T` by blast
nipkow@48849
   239
  from `finite T` `S \<subseteq> T` have f: "finite S" "finite (T - S)"
nipkow@48849
   240
    by (auto intro: finite_subset)
nipkow@48849
   241
  show ?thesis using assms(4)
haftmann@51489
   242
    by (simp add: union_disjoint [OF f d, unfolded eq [symmetric]] neutral [OF assms(3)])
nipkow@48849
   243
qed
nipkow@48849
   244
haftmann@51489
   245
lemma mono_neutral_cong_right:
nipkow@48850
   246
  "\<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>
nipkow@48850
   247
   \<Longrightarrow> F g T = F h S"
haftmann@51489
   248
  by (auto intro!: mono_neutral_cong_left [symmetric])
nipkow@48849
   249
haftmann@51489
   250
lemma mono_neutral_left:
nipkow@48849
   251
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g S = F g T"
haftmann@51489
   252
  by (blast intro: mono_neutral_cong_left)
nipkow@48849
   253
haftmann@51489
   254
lemma mono_neutral_right:
nipkow@48850
   255
  "\<lbrakk> finite T;  S \<subseteq> T;  \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S"
haftmann@51489
   256
  by (blast intro!: mono_neutral_left [symmetric])
nipkow@48849
   257
haftmann@51489
   258
lemma delta: 
nipkow@48849
   259
  assumes fS: "finite S"
haftmann@51489
   260
  shows "F (\<lambda>k. if k = a then b k else 1) S = (if a \<in> S then b a else 1)"
nipkow@48849
   261
proof-
nipkow@48849
   262
  let ?f = "(\<lambda>k. if k=a then b k else 1)"
nipkow@48849
   263
  { assume a: "a \<notin> S"
nipkow@48849
   264
    hence "\<forall>k\<in>S. ?f k = 1" by simp
nipkow@48849
   265
    hence ?thesis  using a by simp }
nipkow@48849
   266
  moreover
nipkow@48849
   267
  { assume a: "a \<in> S"
nipkow@48849
   268
    let ?A = "S - {a}"
nipkow@48849
   269
    let ?B = "{a}"
nipkow@48849
   270
    have eq: "S = ?A \<union> ?B" using a by blast 
nipkow@48849
   271
    have dj: "?A \<inter> ?B = {}" by simp
nipkow@48849
   272
    from fS have fAB: "finite ?A" "finite ?B" by auto  
nipkow@48849
   273
    have "F ?f S = F ?f ?A * F ?f ?B"
haftmann@51489
   274
      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]]
nipkow@48849
   275
      by simp
haftmann@51489
   276
    then have ?thesis using a by simp }
nipkow@48849
   277
  ultimately show ?thesis by blast
nipkow@48849
   278
qed
nipkow@48849
   279
haftmann@51489
   280
lemma delta': 
haftmann@51489
   281
  assumes fS: "finite S"
haftmann@51489
   282
  shows "F (\<lambda>k. if a = k then b k else 1) S = (if a \<in> S then b a else 1)"
haftmann@51489
   283
  using delta [OF fS, of a b, symmetric] by (auto intro: cong)
nipkow@48893
   284
hoelzl@42986
   285
lemma If_cases:
hoelzl@42986
   286
  fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
hoelzl@42986
   287
  assumes fA: "finite A"
hoelzl@42986
   288
  shows "F (\<lambda>x. if P x then h x else g x) A =
haftmann@51489
   289
    F h (A \<inter> {x. P x}) * F g (A \<inter> - {x. P x})"
haftmann@51489
   290
proof -
hoelzl@42986
   291
  have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}" 
hoelzl@42986
   292
          "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}" 
hoelzl@42986
   293
    by blast+
hoelzl@42986
   294
  from fA 
hoelzl@42986
   295
  have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
hoelzl@42986
   296
  let ?g = "\<lambda>x. if P x then h x else g x"
haftmann@51489
   297
  from union_disjoint [OF f a(2), of ?g] a(1)
hoelzl@42986
   298
  show ?thesis
haftmann@51489
   299
    by (subst (1 2) cong) simp_all
hoelzl@42986
   300
qed
hoelzl@42986
   301
haftmann@51489
   302
lemma cartesian_product:
haftmann@51489
   303
   "F (\<lambda>x. F (g x) B) A = F (split g) (A <*> B)"
haftmann@51489
   304
apply (rule sym)
haftmann@51489
   305
apply (cases "finite A") 
haftmann@51489
   306
 apply (cases "finite B") 
haftmann@51489
   307
  apply (simp add: Sigma)
haftmann@51489
   308
 apply (cases "A={}", simp)
haftmann@51489
   309
 apply simp
haftmann@51489
   310
apply (auto intro: infinite dest: finite_cartesian_productD2)
haftmann@51489
   311
apply (cases "B = {}") apply (auto intro: infinite dest: finite_cartesian_productD1)
haftmann@51489
   312
done
haftmann@51489
   313
haftmann@35816
   314
end
haftmann@35816
   315
haftmann@35816
   316
notation times (infixl "*" 70)
haftmann@35816
   317
notation Groups.one ("1")
haftmann@35816
   318
haftmann@35816
   319
nipkow@15402
   320
subsection {* Generalized summation over a set *}
nipkow@15402
   321
haftmann@51489
   322
definition (in comm_monoid_add) setsum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@51489
   323
where
haftmann@51489
   324
  "setsum = comm_monoid_set.F plus 0"
haftmann@26041
   325
haftmann@51489
   326
sublocale comm_monoid_add < setsum!: comm_monoid_set plus 0
haftmann@51489
   327
where
haftmann@51489
   328
  "setsum.F = setsum"
haftmann@51489
   329
proof -
haftmann@51489
   330
  show "comm_monoid_set plus 0" ..
haftmann@51489
   331
  then interpret setsum!: comm_monoid_set plus 0 .
haftmann@51489
   332
  show "setsum.F = setsum"
haftmann@51489
   333
    by (simp only: setsum_def)
haftmann@51489
   334
qed
nipkow@15402
   335
wenzelm@19535
   336
abbreviation
haftmann@51489
   337
  Setsum ("\<Sum>_" [1000] 999) where
haftmann@51489
   338
  "\<Sum>A \<equiv> setsum (%x. x) A"
wenzelm@19535
   339
nipkow@15402
   340
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   341
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   342
nipkow@15402
   343
syntax
paulson@17189
   344
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   345
syntax (xsymbols)
paulson@17189
   346
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   347
syntax (HTML output)
paulson@17189
   348
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   349
nipkow@15402
   350
translations -- {* Beware of argument permutation! *}
nipkow@28853
   351
  "SUM i:A. b" == "CONST setsum (%i. b) A"
nipkow@28853
   352
  "\<Sum>i\<in>A. b" == "CONST setsum (%i. b) A"
nipkow@15402
   353
nipkow@15402
   354
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   355
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   356
nipkow@15402
   357
syntax
paulson@17189
   358
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   359
syntax (xsymbols)
paulson@17189
   360
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   361
syntax (HTML output)
paulson@17189
   362
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   363
nipkow@15402
   364
translations
nipkow@28853
   365
  "SUM x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@28853
   366
  "\<Sum>x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@15402
   367
nipkow@15402
   368
print_translation {*
nipkow@15402
   369
let
wenzelm@35115
   370
  fun setsum_tr' [Abs (x, Tx, t), Const (@{const_syntax Collect}, _) $ Abs (y, Ty, P)] =
wenzelm@35115
   371
        if x <> y then raise Match
wenzelm@35115
   372
        else
wenzelm@35115
   373
          let
wenzelm@49660
   374
            val x' = Syntax_Trans.mark_bound_body (x, Tx);
wenzelm@35115
   375
            val t' = subst_bound (x', t);
wenzelm@35115
   376
            val P' = subst_bound (x', P);
wenzelm@49660
   377
          in
wenzelm@49660
   378
            Syntax.const @{syntax_const "_qsetsum"} $ Syntax_Trans.mark_bound_abs (x, Tx) $ P' $ t'
wenzelm@49660
   379
          end
wenzelm@35115
   380
    | setsum_tr' _ = raise Match;
wenzelm@35115
   381
in [(@{const_syntax setsum}, setsum_tr')] end
nipkow@15402
   382
*}
nipkow@15402
   383
haftmann@51489
   384
text {* TODO These are candidates for generalization *}
nipkow@15402
   385
haftmann@51489
   386
context comm_monoid_add
haftmann@51489
   387
begin
nipkow@15402
   388
haftmann@51489
   389
lemma setsum_reindex_id: 
haftmann@35816
   390
  "inj_on f B ==> setsum f B = setsum id (f ` B)"
haftmann@51489
   391
  by (simp add: setsum.reindex)
nipkow@15402
   392
haftmann@51489
   393
lemma setsum_reindex_nonzero:
chaieb@29674
   394
  assumes fS: "finite S"
haftmann@51489
   395
  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@51489
   396
  shows "setsum h (f ` S) = setsum (h \<circ> f) S"
haftmann@51489
   397
using nz proof (induct rule: finite_induct [OF fS])
chaieb@29674
   398
  case 1 thus ?case by simp
chaieb@29674
   399
next
chaieb@29674
   400
  case (2 x F) 
nipkow@48849
   401
  { assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
chaieb@29674
   402
    then obtain y where y: "y \<in> F" "f x = f y" by auto 
chaieb@29674
   403
    from "2.hyps" y have xy: "x \<noteq> y" by auto
haftmann@51489
   404
    from "2.prems" [of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
chaieb@29674
   405
    have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
chaieb@29674
   406
    also have "\<dots> = setsum (h o f) (insert x F)" 
haftmann@35816
   407
      unfolding setsum.insert[OF `finite F` `x\<notin>F`]
haftmann@35816
   408
      using h0
haftmann@51489
   409
      apply (simp cong del: setsum.strong_cong)
chaieb@29674
   410
      apply (rule "2.hyps"(3))
chaieb@29674
   411
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
   412
      apply simp_all
chaieb@29674
   413
      done
nipkow@48849
   414
    finally have ?case . }
chaieb@29674
   415
  moreover
nipkow@48849
   416
  { assume fxF: "f x \<notin> f ` F"
chaieb@29674
   417
    have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)" 
chaieb@29674
   418
      using fxF "2.hyps" by simp 
chaieb@29674
   419
    also have "\<dots> = setsum (h o f) (insert x F)"
haftmann@35816
   420
      unfolding setsum.insert[OF `finite F` `x\<notin>F`]
haftmann@51489
   421
      apply (simp cong del: setsum.strong_cong)
haftmann@35816
   422
      apply (rule cong [OF refl [of "op + (h (f x))"]])
chaieb@29674
   423
      apply (rule "2.hyps"(3))
chaieb@29674
   424
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
   425
      apply simp_all
chaieb@29674
   426
      done
nipkow@48849
   427
    finally have ?case . }
chaieb@29674
   428
  ultimately show ?case by blast
chaieb@29674
   429
qed
chaieb@29674
   430
haftmann@51489
   431
lemma setsum_cong2:
haftmann@51489
   432
  "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> setsum f A = setsum g A"
haftmann@51489
   433
  by (auto intro: setsum.cong)
nipkow@15554
   434
nipkow@48849
   435
lemma setsum_reindex_cong:
nipkow@28853
   436
   "[|inj_on f A; B = f ` A; !!a. a:A \<Longrightarrow> g a = h (f a)|] 
nipkow@28853
   437
    ==> setsum h B = setsum g A"
haftmann@51489
   438
  by (simp add: setsum.reindex)
chaieb@29674
   439
chaieb@30260
   440
lemma setsum_restrict_set:
chaieb@30260
   441
  assumes fA: "finite A"
chaieb@30260
   442
  shows "setsum f (A \<inter> B) = setsum (\<lambda>x. if x \<in> B then f x else 0) A"
chaieb@30260
   443
proof-
chaieb@30260
   444
  from fA have fab: "finite (A \<inter> B)" by auto
chaieb@30260
   445
  have aba: "A \<inter> B \<subseteq> A" by blast
chaieb@30260
   446
  let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
haftmann@51489
   447
  from setsum.mono_neutral_left [OF fA aba, of ?g]
chaieb@30260
   448
  show ?thesis by simp
chaieb@30260
   449
qed
chaieb@30260
   450
nipkow@15402
   451
lemma setsum_Union_disjoint:
hoelzl@44937
   452
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}"
hoelzl@44937
   453
  shows "setsum f (Union C) = setsum (setsum f) C"
haftmann@51489
   454
  using assms by (fact setsum.Union_disjoint)
nipkow@15402
   455
haftmann@51489
   456
lemma setsum_cartesian_product:
haftmann@51489
   457
  "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
haftmann@51489
   458
  by (fact setsum.cartesian_product)
nipkow@15402
   459
haftmann@51489
   460
lemma setsum_UNION_zero:
nipkow@48893
   461
  assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
nipkow@48893
   462
  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"
nipkow@48893
   463
  shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
nipkow@48893
   464
  using fSS f0
nipkow@48893
   465
proof(induct rule: finite_induct[OF fS])
nipkow@48893
   466
  case 1 thus ?case by simp
nipkow@48893
   467
next
nipkow@48893
   468
  case (2 T F)
nipkow@48893
   469
  then have fTF: "finite T" "\<forall>T\<in>F. finite T" "finite F" and TF: "T \<notin> F" 
nipkow@48893
   470
    and H: "setsum f (\<Union> F) = setsum (setsum f) F" by auto
nipkow@48893
   471
  from fTF have fUF: "finite (\<Union>F)" by auto
nipkow@48893
   472
  from "2.prems" TF fTF
nipkow@48893
   473
  show ?case 
haftmann@51489
   474
    by (auto simp add: H [symmetric] intro: setsum.union_inter_neutral [OF fTF(1) fUF, of f])
haftmann@51489
   475
qed
haftmann@51489
   476
haftmann@51489
   477
text {* Commuting outer and inner summation *}
haftmann@51489
   478
haftmann@51489
   479
lemma setsum_commute:
haftmann@51489
   480
  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
haftmann@51489
   481
proof (simp add: setsum_cartesian_product)
haftmann@51489
   482
  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
haftmann@51489
   483
    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
haftmann@51489
   484
    (is "?s = _")
haftmann@51489
   485
    apply (simp add: setsum.reindex [where h = "%(i, j). (j, i)"] swap_inj_on)
haftmann@51489
   486
    apply (simp add: split_def)
haftmann@51489
   487
    done
haftmann@51489
   488
  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
haftmann@51489
   489
    (is "_ = ?t")
haftmann@51489
   490
    apply (simp add: swap_product)
haftmann@51489
   491
    done
haftmann@51489
   492
  finally show "?s = ?t" .
haftmann@51489
   493
qed
haftmann@51489
   494
haftmann@51489
   495
lemma setsum_Plus:
haftmann@51489
   496
  fixes A :: "'a set" and B :: "'b set"
haftmann@51489
   497
  assumes fin: "finite A" "finite B"
haftmann@51489
   498
  shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B"
haftmann@51489
   499
proof -
haftmann@51489
   500
  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
haftmann@51489
   501
  moreover from fin have "finite (Inl ` A :: ('a + 'b) set)" "finite (Inr ` B :: ('a + 'b) set)"
haftmann@51489
   502
    by auto
haftmann@51489
   503
  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('a + 'b) set)" by auto
haftmann@51489
   504
  moreover have "inj_on (Inl :: 'a \<Rightarrow> 'a + 'b) A" "inj_on (Inr :: 'b \<Rightarrow> 'a + 'b) B" by(auto intro: inj_onI)
haftmann@51489
   505
  ultimately show ?thesis using fin by(simp add: setsum.union_disjoint setsum.reindex)
nipkow@48893
   506
qed
nipkow@48893
   507
haftmann@51489
   508
end
haftmann@51489
   509
haftmann@51489
   510
text {* TODO These are legacy *}
haftmann@51489
   511
haftmann@51489
   512
lemma setsum_empty:
haftmann@51489
   513
  "setsum f {} = 0"
haftmann@51489
   514
  by (fact setsum.empty)
haftmann@51489
   515
haftmann@51489
   516
lemma setsum_insert:
haftmann@51489
   517
  "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
haftmann@51489
   518
  by (fact setsum.insert)
haftmann@51489
   519
haftmann@51489
   520
lemma setsum_infinite:
haftmann@51489
   521
  "~ finite A ==> setsum f A = 0"
haftmann@51489
   522
  by (fact setsum.infinite)
haftmann@51489
   523
haftmann@51489
   524
lemma setsum_reindex:
haftmann@51489
   525
  "inj_on f B \<Longrightarrow> setsum h (f ` B) = setsum (h \<circ> f) B"
haftmann@51489
   526
  by (fact setsum.reindex)
haftmann@51489
   527
haftmann@51489
   528
lemma setsum_cong:
haftmann@51489
   529
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
haftmann@51489
   530
  by (fact setsum.cong)
haftmann@51489
   531
haftmann@51489
   532
lemma strong_setsum_cong:
haftmann@51489
   533
  "A = B ==> (!!x. x:B =simp=> f x = g x)
haftmann@51489
   534
   ==> setsum (%x. f x) A = setsum (%x. g x) B"
haftmann@51489
   535
  by (fact setsum.strong_cong)
haftmann@51489
   536
haftmann@51489
   537
lemmas setsum_0 = setsum.neutral_const
haftmann@51489
   538
lemmas setsum_0' = setsum.neutral
haftmann@51489
   539
haftmann@51489
   540
lemma setsum_Un_Int: "finite A ==> finite B ==>
haftmann@51489
   541
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
haftmann@51489
   542
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
haftmann@51489
   543
  by (fact setsum.union_inter)
haftmann@51489
   544
haftmann@51489
   545
lemma setsum_Un_disjoint: "finite A ==> finite B
haftmann@51489
   546
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
haftmann@51489
   547
  by (fact setsum.union_disjoint)
haftmann@51489
   548
haftmann@51489
   549
lemma setsum_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
haftmann@51489
   550
    setsum f A = setsum f (A - B) + setsum f B"
haftmann@51489
   551
  by (fact setsum.subset_diff)
haftmann@51489
   552
haftmann@51489
   553
lemma setsum_mono_zero_left: 
haftmann@51489
   554
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 0 \<rbrakk> \<Longrightarrow> setsum f S = setsum f T"
haftmann@51489
   555
  by (fact setsum.mono_neutral_left)
haftmann@51489
   556
haftmann@51489
   557
lemmas setsum_mono_zero_right = setsum.mono_neutral_right
haftmann@51489
   558
haftmann@51489
   559
lemma setsum_mono_zero_cong_left: 
haftmann@51489
   560
  "\<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@51489
   561
  \<Longrightarrow> setsum f S = setsum g T"
haftmann@51489
   562
  by (fact setsum.mono_neutral_cong_left)
haftmann@51489
   563
haftmann@51489
   564
lemmas setsum_mono_zero_cong_right = setsum.mono_neutral_cong_right
haftmann@51489
   565
haftmann@51489
   566
lemma setsum_delta: "finite S \<Longrightarrow>
haftmann@51489
   567
  setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
haftmann@51489
   568
  by (fact setsum.delta)
haftmann@51489
   569
haftmann@51489
   570
lemma setsum_delta': "finite S \<Longrightarrow>
haftmann@51489
   571
  setsum (\<lambda>k. if a = k then b k else 0) S = (if a\<in> S then b a else 0)"
haftmann@51489
   572
  by (fact setsum.delta')
haftmann@51489
   573
haftmann@51489
   574
lemma setsum_cases:
haftmann@51489
   575
  assumes "finite A"
haftmann@51489
   576
  shows "setsum (\<lambda>x. if P x then f x else g x) A =
haftmann@51489
   577
         setsum f (A \<inter> {x. P x}) + setsum g (A \<inter> - {x. P x})"
haftmann@51489
   578
  using assms by (fact setsum.If_cases)
haftmann@51489
   579
haftmann@51489
   580
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
haftmann@51489
   581
  the lhs need not be, since UNION I A could still be finite.*)
haftmann@51489
   582
lemma setsum_UN_disjoint:
haftmann@51489
   583
  assumes "finite I" and "ALL i:I. finite (A i)"
haftmann@51489
   584
    and "ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}"
haftmann@51489
   585
  shows "setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
haftmann@51489
   586
  using assms by (fact setsum.UNION_disjoint)
haftmann@51489
   587
haftmann@51489
   588
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
haftmann@51489
   589
  the rhs need not be, since SIGMA A B could still be finite.*)
haftmann@51489
   590
lemma setsum_Sigma:
haftmann@51489
   591
  assumes "finite A" and  "ALL x:A. finite (B x)"
haftmann@51489
   592
  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@51489
   593
  using assms by (fact setsum.Sigma)
haftmann@51489
   594
haftmann@51489
   595
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
haftmann@51489
   596
  by (fact setsum.distrib)
haftmann@51489
   597
haftmann@51489
   598
lemma setsum_Un_zero:  
haftmann@51489
   599
  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow>
haftmann@51489
   600
  setsum f (S \<union> T) = setsum f S + setsum f T"
haftmann@51489
   601
  by (fact setsum.union_inter_neutral)
haftmann@51489
   602
haftmann@51489
   603
lemma setsum_eq_general_reverses:
haftmann@51489
   604
  assumes fS: "finite S" and fT: "finite T"
haftmann@51489
   605
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
haftmann@51489
   606
  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x"
haftmann@51489
   607
  shows "setsum f S = setsum g T"
haftmann@51489
   608
  using kh hk by (fact setsum.eq_general_reverses)
haftmann@51489
   609
nipkow@15402
   610
nipkow@15402
   611
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
   612
nipkow@15402
   613
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@28853
   614
  (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@28853
   615
   setsum f A + setsum f B - setsum f (A Int B)"
nipkow@29667
   616
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
nipkow@15402
   617
haftmann@49715
   618
lemma setsum_Un2:
haftmann@49715
   619
  assumes "finite (A \<union> B)"
haftmann@49715
   620
  shows "setsum f (A \<union> B) = setsum f (A - B) + setsum f (B - A) + setsum f (A \<inter> B)"
haftmann@49715
   621
proof -
haftmann@49715
   622
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@49715
   623
    by auto
haftmann@49715
   624
  with assms show ?thesis by simp (subst setsum_Un_disjoint, auto)+
haftmann@49715
   625
qed
haftmann@49715
   626
nipkow@15402
   627
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
   628
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
   629
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@28853
   630
by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@28853
   631
nipkow@15402
   632
lemma setsum_diff:
nipkow@15402
   633
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
   634
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
   635
proof -
nipkow@15402
   636
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
   637
  show ?thesis using finiteB le
wenzelm@21575
   638
  proof induct
wenzelm@19535
   639
    case empty
wenzelm@19535
   640
    thus ?case by auto
wenzelm@19535
   641
  next
wenzelm@19535
   642
    case (insert x F)
wenzelm@19535
   643
    thus ?case using le finiteB 
wenzelm@19535
   644
      by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
   645
  qed
wenzelm@19535
   646
qed
nipkow@15402
   647
nipkow@15402
   648
lemma setsum_mono:
haftmann@35028
   649
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, ordered_ab_semigroup_add}))"
nipkow@15402
   650
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
   651
proof (cases "finite K")
nipkow@15402
   652
  case True
nipkow@15402
   653
  thus ?thesis using le
wenzelm@19535
   654
  proof induct
nipkow@15402
   655
    case empty
nipkow@15402
   656
    thus ?case by simp
nipkow@15402
   657
  next
nipkow@15402
   658
    case insert
nipkow@44890
   659
    thus ?case using add_mono by fastforce
nipkow@15402
   660
  qed
nipkow@15402
   661
next
haftmann@51489
   662
  case False then show ?thesis by simp
nipkow@15402
   663
qed
nipkow@15402
   664
nipkow@15554
   665
lemma setsum_strict_mono:
haftmann@35028
   666
  fixes f :: "'a \<Rightarrow> 'b::{ordered_cancel_ab_semigroup_add,comm_monoid_add}"
wenzelm@19535
   667
  assumes "finite A"  "A \<noteq> {}"
wenzelm@19535
   668
    and "!!x. x:A \<Longrightarrow> f x < g x"
wenzelm@19535
   669
  shows "setsum f A < setsum g A"
wenzelm@41550
   670
  using assms
nipkow@15554
   671
proof (induct rule: finite_ne_induct)
nipkow@15554
   672
  case singleton thus ?case by simp
nipkow@15554
   673
next
nipkow@15554
   674
  case insert thus ?case by (auto simp: add_strict_mono)
nipkow@15554
   675
qed
nipkow@15554
   676
nipkow@46699
   677
lemma setsum_strict_mono_ex1:
nipkow@46699
   678
fixes f :: "'a \<Rightarrow> 'b::{comm_monoid_add, ordered_cancel_ab_semigroup_add}"
nipkow@46699
   679
assumes "finite A" and "ALL x:A. f x \<le> g x" and "EX a:A. f a < g a"
nipkow@46699
   680
shows "setsum f A < setsum g A"
nipkow@46699
   681
proof-
nipkow@46699
   682
  from assms(3) obtain a where a: "a:A" "f a < g a" by blast
nipkow@46699
   683
  have "setsum f A = setsum f ((A-{a}) \<union> {a})"
nipkow@46699
   684
    by(simp add:insert_absorb[OF `a:A`])
nipkow@46699
   685
  also have "\<dots> = setsum f (A-{a}) + setsum f {a}"
nipkow@46699
   686
    using `finite A` by(subst setsum_Un_disjoint) auto
nipkow@46699
   687
  also have "setsum f (A-{a}) \<le> setsum g (A-{a})"
nipkow@46699
   688
    by(rule setsum_mono)(simp add: assms(2))
nipkow@46699
   689
  also have "setsum f {a} < setsum g {a}" using a by simp
nipkow@46699
   690
  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) \<union> {a})"
nipkow@46699
   691
    using `finite A` by(subst setsum_Un_disjoint[symmetric]) auto
nipkow@46699
   692
  also have "\<dots> = setsum g A" by(simp add:insert_absorb[OF `a:A`])
nipkow@46699
   693
  finally show ?thesis by (metis add_right_mono add_strict_left_mono)
nipkow@46699
   694
qed
nipkow@46699
   695
nipkow@15535
   696
lemma setsum_negf:
wenzelm@19535
   697
  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
nipkow@15535
   698
proof (cases "finite A")
berghofe@22262
   699
  case True thus ?thesis by (induct set: finite) auto
nipkow@15535
   700
next
haftmann@51489
   701
  case False thus ?thesis by simp
nipkow@15535
   702
qed
nipkow@15402
   703
nipkow@15535
   704
lemma setsum_subtractf:
wenzelm@19535
   705
  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
wenzelm@19535
   706
    setsum f A - setsum g A"
nipkow@15535
   707
proof (cases "finite A")
nipkow@15535
   708
  case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15535
   709
next
haftmann@51489
   710
  case False thus ?thesis by simp
nipkow@15535
   711
qed
nipkow@15402
   712
nipkow@15535
   713
lemma setsum_nonneg:
haftmann@35028
   714
  assumes nn: "\<forall>x\<in>A. (0::'a::{ordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
wenzelm@19535
   715
  shows "0 \<le> setsum f A"
nipkow@15535
   716
proof (cases "finite A")
nipkow@15535
   717
  case True thus ?thesis using nn
wenzelm@21575
   718
  proof induct
wenzelm@19535
   719
    case empty then show ?case by simp
wenzelm@19535
   720
  next
wenzelm@19535
   721
    case (insert x F)
wenzelm@19535
   722
    then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
wenzelm@19535
   723
    with insert show ?case by simp
wenzelm@19535
   724
  qed
nipkow@15535
   725
next
haftmann@51489
   726
  case False thus ?thesis by simp
nipkow@15535
   727
qed
nipkow@15402
   728
nipkow@15535
   729
lemma setsum_nonpos:
haftmann@35028
   730
  assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{ordered_ab_semigroup_add,comm_monoid_add})"
wenzelm@19535
   731
  shows "setsum f A \<le> 0"
nipkow@15535
   732
proof (cases "finite A")
nipkow@15535
   733
  case True thus ?thesis using np
wenzelm@21575
   734
  proof induct
wenzelm@19535
   735
    case empty then show ?case by simp
wenzelm@19535
   736
  next
wenzelm@19535
   737
    case (insert x F)
wenzelm@19535
   738
    then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
wenzelm@19535
   739
    with insert show ?case by simp
wenzelm@19535
   740
  qed
nipkow@15535
   741
next
haftmann@51489
   742
  case False thus ?thesis by simp
nipkow@15535
   743
qed
nipkow@15402
   744
hoelzl@36622
   745
lemma setsum_nonneg_leq_bound:
hoelzl@36622
   746
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
hoelzl@36622
   747
  assumes "finite s" "\<And>i. i \<in> s \<Longrightarrow> f i \<ge> 0" "(\<Sum>i \<in> s. f i) = B" "i \<in> s"
hoelzl@36622
   748
  shows "f i \<le> B"
hoelzl@36622
   749
proof -
hoelzl@36622
   750
  have "0 \<le> (\<Sum> i \<in> s - {i}. f i)" and "0 \<le> f i"
hoelzl@36622
   751
    using assms by (auto intro!: setsum_nonneg)
hoelzl@36622
   752
  moreover
hoelzl@36622
   753
  have "(\<Sum> i \<in> s - {i}. f i) + f i = B"
hoelzl@36622
   754
    using assms by (simp add: setsum_diff1)
hoelzl@36622
   755
  ultimately show ?thesis by auto
hoelzl@36622
   756
qed
hoelzl@36622
   757
hoelzl@36622
   758
lemma setsum_nonneg_0:
hoelzl@36622
   759
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
hoelzl@36622
   760
  assumes "finite s" and pos: "\<And> i. i \<in> s \<Longrightarrow> f i \<ge> 0"
hoelzl@36622
   761
  and "(\<Sum> i \<in> s. f i) = 0" and i: "i \<in> s"
hoelzl@36622
   762
  shows "f i = 0"
hoelzl@36622
   763
  using setsum_nonneg_leq_bound[OF assms] pos[OF i] by auto
hoelzl@36622
   764
nipkow@15539
   765
lemma setsum_mono2:
haftmann@36303
   766
fixes f :: "'a \<Rightarrow> 'b :: ordered_comm_monoid_add"
nipkow@15539
   767
assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
nipkow@15539
   768
shows "setsum f A \<le> setsum f B"
nipkow@15539
   769
proof -
nipkow@15539
   770
  have "setsum f A \<le> setsum f A + setsum f (B-A)"
nipkow@15539
   771
    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
nipkow@15539
   772
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15539
   773
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15539
   774
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15539
   775
  finally show ?thesis .
nipkow@15539
   776
qed
nipkow@15542
   777
avigad@16775
   778
lemma setsum_mono3: "finite B ==> A <= B ==> 
avigad@16775
   779
    ALL x: B - A. 
haftmann@35028
   780
      0 <= ((f x)::'a::{comm_monoid_add,ordered_ab_semigroup_add}) ==>
avigad@16775
   781
        setsum f A <= setsum f B"
avigad@16775
   782
  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")
avigad@16775
   783
  apply (erule ssubst)
avigad@16775
   784
  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")
avigad@16775
   785
  apply simp
avigad@16775
   786
  apply (rule add_left_mono)
avigad@16775
   787
  apply (erule setsum_nonneg)
avigad@16775
   788
  apply (subst setsum_Un_disjoint [THEN sym])
avigad@16775
   789
  apply (erule finite_subset, assumption)
avigad@16775
   790
  apply (rule finite_subset)
avigad@16775
   791
  prefer 2
avigad@16775
   792
  apply assumption
haftmann@32698
   793
  apply (auto simp add: sup_absorb2)
avigad@16775
   794
done
avigad@16775
   795
ballarin@19279
   796
lemma setsum_right_distrib: 
huffman@22934
   797
  fixes f :: "'a => ('b::semiring_0)"
nipkow@15402
   798
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
   799
proof (cases "finite A")
nipkow@15402
   800
  case True
nipkow@15402
   801
  thus ?thesis
wenzelm@21575
   802
  proof induct
nipkow@15402
   803
    case empty thus ?case by simp
nipkow@15402
   804
  next
webertj@49962
   805
    case (insert x A) thus ?case by (simp add: distrib_left)
nipkow@15402
   806
  qed
nipkow@15402
   807
next
haftmann@51489
   808
  case False thus ?thesis by simp
nipkow@15402
   809
qed
nipkow@15402
   810
ballarin@17149
   811
lemma setsum_left_distrib:
huffman@22934
   812
  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
ballarin@17149
   813
proof (cases "finite A")
ballarin@17149
   814
  case True
ballarin@17149
   815
  then show ?thesis
ballarin@17149
   816
  proof induct
ballarin@17149
   817
    case empty thus ?case by simp
ballarin@17149
   818
  next
webertj@49962
   819
    case (insert x A) thus ?case by (simp add: distrib_right)
ballarin@17149
   820
  qed
ballarin@17149
   821
next
haftmann@51489
   822
  case False thus ?thesis by simp
ballarin@17149
   823
qed
ballarin@17149
   824
ballarin@17149
   825
lemma setsum_divide_distrib:
ballarin@17149
   826
  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
ballarin@17149
   827
proof (cases "finite A")
ballarin@17149
   828
  case True
ballarin@17149
   829
  then show ?thesis
ballarin@17149
   830
  proof induct
ballarin@17149
   831
    case empty thus ?case by simp
ballarin@17149
   832
  next
ballarin@17149
   833
    case (insert x A) thus ?case by (simp add: add_divide_distrib)
ballarin@17149
   834
  qed
ballarin@17149
   835
next
haftmann@51489
   836
  case False thus ?thesis by simp
ballarin@17149
   837
qed
ballarin@17149
   838
nipkow@15535
   839
lemma setsum_abs[iff]: 
haftmann@35028
   840
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15402
   841
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15535
   842
proof (cases "finite A")
nipkow@15535
   843
  case True
nipkow@15535
   844
  thus ?thesis
wenzelm@21575
   845
  proof induct
nipkow@15535
   846
    case empty thus ?case by simp
nipkow@15535
   847
  next
nipkow@15535
   848
    case (insert x A)
nipkow@15535
   849
    thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15535
   850
  qed
nipkow@15402
   851
next
haftmann@51489
   852
  case False thus ?thesis by simp
nipkow@15402
   853
qed
nipkow@15402
   854
nipkow@15535
   855
lemma setsum_abs_ge_zero[iff]: 
haftmann@35028
   856
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15402
   857
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15535
   858
proof (cases "finite A")
nipkow@15535
   859
  case True
nipkow@15535
   860
  thus ?thesis
wenzelm@21575
   861
  proof induct
nipkow@15535
   862
    case empty thus ?case by simp
nipkow@15535
   863
  next
huffman@36977
   864
    case (insert x A) thus ?case by auto
nipkow@15535
   865
  qed
nipkow@15402
   866
next
haftmann@51489
   867
  case False thus ?thesis by simp
nipkow@15402
   868
qed
nipkow@15402
   869
nipkow@15539
   870
lemma abs_setsum_abs[simp]: 
haftmann@35028
   871
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15539
   872
  shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
nipkow@15539
   873
proof (cases "finite A")
nipkow@15539
   874
  case True
nipkow@15539
   875
  thus ?thesis
wenzelm@21575
   876
  proof induct
nipkow@15539
   877
    case empty thus ?case by simp
nipkow@15539
   878
  next
nipkow@15539
   879
    case (insert a A)
nipkow@15539
   880
    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
nipkow@15539
   881
    also have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>"  using insert by simp
avigad@16775
   882
    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
avigad@16775
   883
      by (simp del: abs_of_nonneg)
nipkow@15539
   884
    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
nipkow@15539
   885
    finally show ?case .
nipkow@15539
   886
  qed
nipkow@15539
   887
next
haftmann@51489
   888
  case False thus ?thesis by simp
nipkow@31080
   889
qed
nipkow@31080
   890
haftmann@51489
   891
lemma setsum_diff1'[rule_format]:
haftmann@51489
   892
  "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
haftmann@51489
   893
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@51489
   894
apply (auto simp add: insert_Diff_if add_ac)
haftmann@51489
   895
done
ballarin@17149
   896
haftmann@51489
   897
lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
haftmann@51489
   898
  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
haftmann@51489
   899
unfolding setsum_diff1'[OF assms] by auto
ballarin@17149
   900
ballarin@19279
   901
lemma setsum_product:
huffman@22934
   902
  fixes f :: "'a => ('b::semiring_0)"
ballarin@19279
   903
  shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
ballarin@19279
   904
  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute)
ballarin@19279
   905
nipkow@34223
   906
lemma setsum_mult_setsum_if_inj:
nipkow@34223
   907
fixes f :: "'a => ('b::semiring_0)"
nipkow@34223
   908
shows "inj_on (%(a,b). f a * g b) (A \<times> B) ==>
nipkow@34223
   909
  setsum f A * setsum g B = setsum id {f a * g b|a b. a:A & b:B}"
nipkow@34223
   910
by(auto simp: setsum_product setsum_cartesian_product
nipkow@34223
   911
        intro!:  setsum_reindex_cong[symmetric])
nipkow@34223
   912
haftmann@51489
   913
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
haftmann@51489
   914
apply (case_tac "finite A")
haftmann@51489
   915
 prefer 2 apply simp
haftmann@51489
   916
apply (erule rev_mp)
haftmann@51489
   917
apply (erule finite_induct, auto)
haftmann@51489
   918
done
haftmann@51489
   919
haftmann@51489
   920
lemma setsum_eq_0_iff [simp]:
haftmann@51489
   921
  "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
haftmann@51489
   922
  by (induct set: finite) auto
haftmann@51489
   923
haftmann@51489
   924
lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
haftmann@51489
   925
  setsum f A = Suc 0 \<longleftrightarrow> (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
haftmann@51489
   926
apply(erule finite_induct)
haftmann@51489
   927
apply (auto simp add:add_is_1)
haftmann@51489
   928
done
haftmann@51489
   929
haftmann@51489
   930
lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
haftmann@51489
   931
haftmann@51489
   932
lemma setsum_Un_nat: "finite A ==> finite B ==>
haftmann@51489
   933
  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
haftmann@51489
   934
  -- {* For the natural numbers, we have subtraction. *}
haftmann@51489
   935
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
haftmann@51489
   936
haftmann@51489
   937
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
haftmann@51489
   938
  (if a:A then setsum f A - f a else setsum f A)"
haftmann@51489
   939
apply (case_tac "finite A")
haftmann@51489
   940
 prefer 2 apply simp
haftmann@51489
   941
apply (erule finite_induct)
haftmann@51489
   942
 apply (auto simp add: insert_Diff_if)
haftmann@51489
   943
apply (drule_tac a = a in mk_disjoint_insert, auto)
haftmann@51489
   944
done
haftmann@51489
   945
haftmann@51489
   946
lemma setsum_diff_nat: 
haftmann@51489
   947
assumes "finite B" and "B \<subseteq> A"
haftmann@51489
   948
shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
haftmann@51489
   949
using assms
haftmann@51489
   950
proof induct
haftmann@51489
   951
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
haftmann@51489
   952
next
haftmann@51489
   953
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
haftmann@51489
   954
    and xFinA: "insert x F \<subseteq> A"
haftmann@51489
   955
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
haftmann@51489
   956
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
haftmann@51489
   957
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
haftmann@51489
   958
    by (simp add: setsum_diff1_nat)
haftmann@51489
   959
  from xFinA have "F \<subseteq> A" by simp
haftmann@51489
   960
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
haftmann@51489
   961
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
haftmann@51489
   962
    by simp
haftmann@51489
   963
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
haftmann@51489
   964
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
haftmann@51489
   965
    by simp
haftmann@51489
   966
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
haftmann@51489
   967
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
haftmann@51489
   968
    by simp
haftmann@51489
   969
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
haftmann@51489
   970
qed
haftmann@51489
   971
haftmann@51489
   972
haftmann@51489
   973
subsubsection {* Cardinality as special case of @{const setsum} *}
haftmann@51489
   974
haftmann@51489
   975
lemma card_eq_setsum:
haftmann@51489
   976
  "card A = setsum (\<lambda>x. 1) A"
haftmann@51489
   977
proof -
haftmann@51489
   978
  have "plus \<circ> (\<lambda>_. Suc 0) = (\<lambda>_. Suc)"
haftmann@51489
   979
    by (simp add: fun_eq_iff)
haftmann@51489
   980
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) = Finite_Set.fold (\<lambda>_. Suc)"
haftmann@51489
   981
    by (rule arg_cong)
haftmann@51489
   982
  then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) 0 A = Finite_Set.fold (\<lambda>_. Suc) 0 A"
haftmann@51489
   983
    by (blast intro: fun_cong)
haftmann@51489
   984
  then show ?thesis by (simp add: card.eq_fold setsum.eq_fold)
haftmann@51489
   985
qed
haftmann@51489
   986
haftmann@51489
   987
lemma setsum_constant [simp]:
haftmann@51489
   988
  "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
haftmann@35722
   989
apply (cases "finite A")
haftmann@35722
   990
apply (erule finite_induct)
haftmann@35722
   991
apply (auto simp add: algebra_simps)
haftmann@35722
   992
done
haftmann@35722
   993
haftmann@35722
   994
lemma setsum_bounded:
haftmann@35722
   995
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, ordered_ab_semigroup_add})"
haftmann@51489
   996
  shows "setsum f A \<le> of_nat (card A) * K"
haftmann@35722
   997
proof (cases "finite A")
haftmann@35722
   998
  case True
haftmann@35722
   999
  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
haftmann@35722
  1000
next
haftmann@51489
  1001
  case False thus ?thesis by simp
haftmann@35722
  1002
qed
haftmann@35722
  1003
haftmann@35722
  1004
lemma card_UN_disjoint:
haftmann@46629
  1005
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@46629
  1006
    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@46629
  1007
  shows "card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
haftmann@46629
  1008
proof -
haftmann@46629
  1009
  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)" by simp
haftmann@46629
  1010
  with assms show ?thesis by (simp add: card_eq_setsum setsum_UN_disjoint del: setsum_constant)
haftmann@46629
  1011
qed
haftmann@35722
  1012
haftmann@35722
  1013
lemma card_Union_disjoint:
haftmann@35722
  1014
  "finite C ==> (ALL A:C. finite A) ==>
haftmann@35722
  1015
   (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {})
haftmann@35722
  1016
   ==> card (Union C) = setsum card C"
haftmann@35722
  1017
apply (frule card_UN_disjoint [of C id])
hoelzl@44937
  1018
apply (simp_all add: SUP_def id_def)
haftmann@35722
  1019
done
haftmann@35722
  1020
haftmann@35722
  1021
haftmann@35722
  1022
subsubsection {* Cardinality of products *}
haftmann@35722
  1023
haftmann@35722
  1024
lemma card_SigmaI [simp]:
haftmann@35722
  1025
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
haftmann@35722
  1026
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
haftmann@35722
  1027
by(simp add: card_eq_setsum setsum_Sigma del:setsum_constant)
haftmann@35722
  1028
haftmann@35722
  1029
(*
haftmann@35722
  1030
lemma SigmaI_insert: "y \<notin> A ==>
haftmann@35722
  1031
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
haftmann@35722
  1032
  by auto
haftmann@35722
  1033
*)
haftmann@35722
  1034
haftmann@35722
  1035
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
haftmann@35722
  1036
  by (cases "finite A \<and> finite B")
haftmann@35722
  1037
    (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)
haftmann@35722
  1038
haftmann@35722
  1039
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
haftmann@35722
  1040
by (simp add: card_cartesian_product)
haftmann@35722
  1041
ballarin@17149
  1042
nipkow@15402
  1043
subsection {* Generalized product over a set *}
nipkow@15402
  1044
haftmann@51489
  1045
definition (in comm_monoid_mult) setprod :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@51489
  1046
where
haftmann@51489
  1047
  "setprod = comm_monoid_set.F times 1"
haftmann@35816
  1048
haftmann@51489
  1049
sublocale comm_monoid_mult < setprod!: comm_monoid_set times 1
haftmann@51489
  1050
where
haftmann@51489
  1051
  "setprod.F = setprod"
haftmann@51489
  1052
proof -
haftmann@51489
  1053
  show "comm_monoid_set times 1" ..
haftmann@51489
  1054
  then interpret setprod!: comm_monoid_set times 1 .
haftmann@51489
  1055
  show "setprod.F = setprod"
haftmann@51489
  1056
    by (simp only: setprod_def)
haftmann@51489
  1057
qed
nipkow@15402
  1058
wenzelm@19535
  1059
abbreviation
haftmann@51489
  1060
  Setprod ("\<Prod>_" [1000] 999) where
haftmann@51489
  1061
  "\<Prod>A \<equiv> setprod (\<lambda>x. x) A"
wenzelm@19535
  1062
nipkow@15402
  1063
syntax
paulson@17189
  1064
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
nipkow@15402
  1065
syntax (xsymbols)
paulson@17189
  1066
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1067
syntax (HTML output)
paulson@17189
  1068
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@16550
  1069
nipkow@16550
  1070
translations -- {* Beware of argument permutation! *}
nipkow@28853
  1071
  "PROD i:A. b" == "CONST setprod (%i. b) A" 
nipkow@28853
  1072
  "\<Prod>i\<in>A. b" == "CONST setprod (%i. b) A" 
nipkow@16550
  1073
nipkow@16550
  1074
text{* Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter
nipkow@16550
  1075
 @{text"\<Prod>x|P. e"}. *}
nipkow@16550
  1076
nipkow@16550
  1077
syntax
paulson@17189
  1078
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)
nipkow@16550
  1079
syntax (xsymbols)
paulson@17189
  1080
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1081
syntax (HTML output)
paulson@17189
  1082
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1083
nipkow@15402
  1084
translations
nipkow@28853
  1085
  "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@28853
  1086
  "\<Prod>x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@16550
  1087
haftmann@51489
  1088
text {* TODO These are candidates for generalization *}
haftmann@51489
  1089
haftmann@51489
  1090
context comm_monoid_mult
haftmann@51489
  1091
begin
haftmann@51489
  1092
haftmann@51489
  1093
lemma setprod_reindex_id:
haftmann@51489
  1094
  "inj_on f B ==> setprod f B = setprod id (f ` B)"
haftmann@51489
  1095
  by (auto simp add: setprod.reindex)
haftmann@51489
  1096
haftmann@51489
  1097
lemma setprod_reindex_cong:
haftmann@51489
  1098
  "inj_on f A ==> B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
haftmann@51489
  1099
  by (frule setprod.reindex, simp)
haftmann@51489
  1100
haftmann@51489
  1101
lemma strong_setprod_reindex_cong:
haftmann@51489
  1102
  assumes i: "inj_on f A"
haftmann@51489
  1103
  and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x"
haftmann@51489
  1104
  shows "setprod h B = setprod g A"
haftmann@51489
  1105
proof-
haftmann@51489
  1106
  have "setprod h B = setprod (h o f) A"
haftmann@51489
  1107
    by (simp add: B setprod.reindex [OF i, of h])
haftmann@51489
  1108
  then show ?thesis apply simp
haftmann@51489
  1109
    apply (rule setprod.cong)
haftmann@51489
  1110
    apply simp
haftmann@51489
  1111
    by (simp add: eq)
haftmann@51489
  1112
qed
haftmann@51489
  1113
haftmann@51489
  1114
lemma setprod_Union_disjoint:
haftmann@51489
  1115
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" 
haftmann@51489
  1116
  shows "setprod f (Union C) = setprod (setprod f) C"
haftmann@51489
  1117
  using assms by (fact setprod.Union_disjoint)
haftmann@51489
  1118
haftmann@51489
  1119
text{*Here we can eliminate the finiteness assumptions, by cases.*}
haftmann@51489
  1120
lemma setprod_cartesian_product:
haftmann@51489
  1121
  "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
haftmann@51489
  1122
  by (fact setprod.cartesian_product)
haftmann@51489
  1123
haftmann@51489
  1124
lemma setprod_Un2:
haftmann@51489
  1125
  assumes "finite (A \<union> B)"
haftmann@51489
  1126
  shows "setprod f (A \<union> B) = setprod f (A - B) * setprod f (B - A) * setprod f (A \<inter> B)"
haftmann@51489
  1127
proof -
haftmann@51489
  1128
  have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
haftmann@51489
  1129
    by auto
haftmann@51489
  1130
  with assms show ?thesis by simp (subst setprod.union_disjoint, auto)+
haftmann@51489
  1131
qed
haftmann@51489
  1132
haftmann@51489
  1133
end
haftmann@51489
  1134
haftmann@51489
  1135
text {* TODO These are legacy *}
haftmann@51489
  1136
haftmann@35816
  1137
lemma setprod_empty: "setprod f {} = 1"
haftmann@35816
  1138
  by (fact setprod.empty)
nipkow@15402
  1139
haftmann@35816
  1140
lemma setprod_insert: "[| finite A; a \<notin> A |] ==>
nipkow@15402
  1141
    setprod f (insert a A) = f a * setprod f A"
haftmann@35816
  1142
  by (fact setprod.insert)
nipkow@15402
  1143
haftmann@35816
  1144
lemma setprod_infinite: "~ finite A ==> setprod f A = 1"
haftmann@35816
  1145
  by (fact setprod.infinite)
paulson@15409
  1146
nipkow@15402
  1147
lemma setprod_reindex:
haftmann@51489
  1148
  "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
haftmann@51489
  1149
  by (fact setprod.reindex)
nipkow@15402
  1150
nipkow@15402
  1151
lemma setprod_cong:
nipkow@15402
  1152
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
haftmann@51489
  1153
  by (fact setprod.cong)
nipkow@15402
  1154
nipkow@48849
  1155
lemma strong_setprod_cong:
berghofe@16632
  1156
  "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"
haftmann@51489
  1157
  by (fact setprod.strong_cong)
nipkow@15402
  1158
haftmann@51489
  1159
lemma setprod_Un_one:
haftmann@51489
  1160
  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 1 \<rbrakk>
haftmann@51489
  1161
  \<Longrightarrow> setprod f (S \<union> T) = setprod f S  * setprod f T"
haftmann@51489
  1162
  by (fact setprod.union_inter_neutral)
chaieb@29674
  1163
haftmann@51489
  1164
lemmas setprod_1 = setprod.neutral_const
haftmann@51489
  1165
lemmas setprod_1' = setprod.neutral
nipkow@15402
  1166
nipkow@15402
  1167
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
  1168
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
haftmann@51489
  1169
  by (fact setprod.union_inter)
nipkow@15402
  1170
nipkow@15402
  1171
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1172
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
haftmann@51489
  1173
  by (fact setprod.union_disjoint)
nipkow@48849
  1174
nipkow@48849
  1175
lemma setprod_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
nipkow@48849
  1176
    setprod f A = setprod f (A - B) * setprod f B"
haftmann@51489
  1177
  by (fact setprod.subset_diff)
nipkow@15402
  1178
nipkow@48849
  1179
lemma setprod_mono_one_left:
nipkow@48849
  1180
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 1 \<rbrakk> \<Longrightarrow> setprod f S = setprod f T"
haftmann@51489
  1181
  by (fact setprod.mono_neutral_left)
nipkow@30837
  1182
haftmann@51489
  1183
lemmas setprod_mono_one_right = setprod.mono_neutral_right
nipkow@30837
  1184
nipkow@48849
  1185
lemma setprod_mono_one_cong_left: 
nipkow@48849
  1186
  "\<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>
nipkow@48849
  1187
  \<Longrightarrow> setprod f S = setprod g T"
haftmann@51489
  1188
  by (fact setprod.mono_neutral_cong_left)
nipkow@48849
  1189
haftmann@51489
  1190
lemmas setprod_mono_one_cong_right = setprod.mono_neutral_cong_right
chaieb@29674
  1191
nipkow@48849
  1192
lemma setprod_delta: "finite S \<Longrightarrow>
nipkow@48849
  1193
  setprod (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
haftmann@51489
  1194
  by (fact setprod.delta)
chaieb@29674
  1195
nipkow@48849
  1196
lemma setprod_delta': "finite S \<Longrightarrow>
nipkow@48849
  1197
  setprod (\<lambda>k. if a = k then b k else 1) S = (if a\<in> S then b a else 1)"
haftmann@51489
  1198
  by (fact setprod.delta')
chaieb@29674
  1199
nipkow@15402
  1200
lemma setprod_UN_disjoint:
nipkow@15402
  1201
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1202
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1203
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
haftmann@51489
  1204
  by (fact setprod.UNION_disjoint)
nipkow@15402
  1205
nipkow@15402
  1206
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@16550
  1207
    (\<Prod>x\<in>A. (\<Prod>y\<in> B x. f x y)) =
paulson@17189
  1208
    (\<Prod>(x,y)\<in>(SIGMA x:A. B x). f x y)"
haftmann@51489
  1209
  by (fact setprod.Sigma)
nipkow@15402
  1210
haftmann@51489
  1211
lemma setprod_timesf: "setprod (\<lambda>x. f x * g x) A = setprod f A * setprod g A"
haftmann@51489
  1212
  by (fact setprod.distrib)
nipkow@15402
  1213
nipkow@15402
  1214
nipkow@15402
  1215
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1216
nipkow@15402
  1217
lemma setprod_zero:
huffman@23277
  1218
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
nipkow@28853
  1219
apply (induct set: finite, force, clarsimp)
nipkow@28853
  1220
apply (erule disjE, auto)
nipkow@28853
  1221
done
nipkow@15402
  1222
haftmann@51489
  1223
lemma setprod_zero_iff[simp]: "finite A ==> 
haftmann@51489
  1224
  (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =
haftmann@51489
  1225
  (EX x: A. f x = 0)"
haftmann@51489
  1226
by (erule finite_induct, auto simp:no_zero_divisors)
haftmann@51489
  1227
haftmann@51489
  1228
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
haftmann@51489
  1229
  (setprod f (A Un B) :: 'a ::{field})
haftmann@51489
  1230
   = setprod f A * setprod f B / setprod f (A Int B)"
haftmann@51489
  1231
by (subst setprod_Un_Int [symmetric], auto)
haftmann@51489
  1232
nipkow@15402
  1233
lemma setprod_nonneg [rule_format]:
haftmann@35028
  1234
   "(ALL x: A. (0::'a::linordered_semidom) \<le> f x) --> 0 \<le> setprod f A"
huffman@30841
  1235
by (cases "finite A", induct set: finite, simp_all add: mult_nonneg_nonneg)
huffman@30841
  1236
haftmann@35028
  1237
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::linordered_semidom) < f x)
nipkow@28853
  1238
  --> 0 < setprod f A"
huffman@30841
  1239
by (cases "finite A", induct set: finite, simp_all add: mult_pos_pos)
nipkow@15402
  1240
nipkow@15402
  1241
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@28853
  1242
  (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@28853
  1243
  (if a:A then setprod f A / f a else setprod f A)"
haftmann@36303
  1244
  by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
  1245
paulson@31906
  1246
lemma setprod_inversef: 
haftmann@36409
  1247
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
paulson@31906
  1248
  shows "finite A ==> setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@28853
  1249
by (erule finite_induct) auto
nipkow@15402
  1250
nipkow@15402
  1251
lemma setprod_dividef:
haftmann@36409
  1252
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
wenzelm@31916
  1253
  shows "finite A
nipkow@28853
  1254
    ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@28853
  1255
apply (subgoal_tac
nipkow@15402
  1256
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@28853
  1257
apply (erule ssubst)
nipkow@28853
  1258
apply (subst divide_inverse)
nipkow@28853
  1259
apply (subst setprod_timesf)
nipkow@28853
  1260
apply (subst setprod_inversef, assumption+, rule refl)
nipkow@28853
  1261
apply (rule setprod_cong, rule refl)
nipkow@28853
  1262
apply (subst divide_inverse, auto)
nipkow@28853
  1263
done
nipkow@28853
  1264
nipkow@29925
  1265
lemma setprod_dvd_setprod [rule_format]: 
nipkow@29925
  1266
    "(ALL x : A. f x dvd g x) \<longrightarrow> setprod f A dvd setprod g A"
nipkow@29925
  1267
  apply (cases "finite A")
nipkow@29925
  1268
  apply (induct set: finite)
nipkow@29925
  1269
  apply (auto simp add: dvd_def)
nipkow@29925
  1270
  apply (rule_tac x = "k * ka" in exI)
nipkow@29925
  1271
  apply (simp add: algebra_simps)
nipkow@29925
  1272
done
nipkow@29925
  1273
nipkow@29925
  1274
lemma setprod_dvd_setprod_subset:
nipkow@29925
  1275
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> setprod f A dvd setprod f B"
nipkow@29925
  1276
  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")
nipkow@29925
  1277
  apply (unfold dvd_def, blast)
nipkow@29925
  1278
  apply (subst setprod_Un_disjoint [symmetric])
nipkow@29925
  1279
  apply (auto elim: finite_subset intro: setprod_cong)
nipkow@29925
  1280
done
nipkow@29925
  1281
nipkow@29925
  1282
lemma setprod_dvd_setprod_subset2:
nipkow@29925
  1283
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> ALL x : A. (f x::'a::comm_semiring_1) dvd g x \<Longrightarrow> 
nipkow@29925
  1284
      setprod f A dvd setprod g B"
nipkow@29925
  1285
  apply (rule dvd_trans)
nipkow@29925
  1286
  apply (rule setprod_dvd_setprod, erule (1) bspec)
nipkow@29925
  1287
  apply (erule (1) setprod_dvd_setprod_subset)
nipkow@29925
  1288
done
nipkow@29925
  1289
nipkow@29925
  1290
lemma dvd_setprod: "finite A \<Longrightarrow> i:A \<Longrightarrow> 
nipkow@29925
  1291
    (f i ::'a::comm_semiring_1) dvd setprod f A"
nipkow@29925
  1292
by (induct set: finite) (auto intro: dvd_mult)
nipkow@29925
  1293
nipkow@29925
  1294
lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) \<longrightarrow> 
nipkow@29925
  1295
    (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"
nipkow@29925
  1296
  apply (cases "finite A")
nipkow@29925
  1297
  apply (induct set: finite)
nipkow@29925
  1298
  apply auto
nipkow@29925
  1299
done
nipkow@29925
  1300
hoelzl@35171
  1301
lemma setprod_mono:
hoelzl@35171
  1302
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linordered_semidom"
hoelzl@35171
  1303
  assumes "\<forall>i\<in>A. 0 \<le> f i \<and> f i \<le> g i"
hoelzl@35171
  1304
  shows "setprod f A \<le> setprod g A"
hoelzl@35171
  1305
proof (cases "finite A")
hoelzl@35171
  1306
  case True
hoelzl@35171
  1307
  hence ?thesis "setprod f A \<ge> 0" using subset_refl[of A]
hoelzl@35171
  1308
  proof (induct A rule: finite_subset_induct)
hoelzl@35171
  1309
    case (insert a F)
hoelzl@35171
  1310
    thus "setprod f (insert a F) \<le> setprod g (insert a F)" "0 \<le> setprod f (insert a F)"
hoelzl@35171
  1311
      unfolding setprod_insert[OF insert(1,3)]
hoelzl@35171
  1312
      using assms[rule_format,OF insert(2)] insert
hoelzl@35171
  1313
      by (auto intro: mult_mono mult_nonneg_nonneg)
hoelzl@35171
  1314
  qed auto
hoelzl@35171
  1315
  thus ?thesis by simp
hoelzl@35171
  1316
qed auto
hoelzl@35171
  1317
hoelzl@35171
  1318
lemma abs_setprod:
hoelzl@35171
  1319
  fixes f :: "'a \<Rightarrow> 'b\<Colon>{linordered_field,abs}"
hoelzl@35171
  1320
  shows "abs (setprod f A) = setprod (\<lambda>x. abs (f x)) A"
hoelzl@35171
  1321
proof (cases "finite A")
hoelzl@35171
  1322
  case True thus ?thesis
huffman@35216
  1323
    by induct (auto simp add: field_simps abs_mult)
hoelzl@35171
  1324
qed auto
hoelzl@35171
  1325
haftmann@31017
  1326
lemma setprod_constant: "finite A ==> (\<Prod>x\<in> A. (y::'a::{comm_monoid_mult})) = y^(card A)"
nipkow@28853
  1327
apply (erule finite_induct)
huffman@35216
  1328
apply auto
nipkow@28853
  1329
done
nipkow@15402
  1330
chaieb@29674
  1331
lemma setprod_gen_delta:
chaieb@29674
  1332
  assumes fS: "finite S"
haftmann@51489
  1333
  shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::comm_monoid_mult) * c^ (card S - 1) else c^ card S)"
chaieb@29674
  1334
proof-
chaieb@29674
  1335
  let ?f = "(\<lambda>k. if k=a then b k else c)"
chaieb@29674
  1336
  {assume a: "a \<notin> S"
chaieb@29674
  1337
    hence "\<forall> k\<in> S. ?f k = c" by simp
nipkow@48849
  1338
    hence ?thesis  using a setprod_constant[OF fS, of c] by simp }
chaieb@29674
  1339
  moreover 
chaieb@29674
  1340
  {assume a: "a \<in> S"
chaieb@29674
  1341
    let ?A = "S - {a}"
chaieb@29674
  1342
    let ?B = "{a}"
chaieb@29674
  1343
    have eq: "S = ?A \<union> ?B" using a by blast 
chaieb@29674
  1344
    have dj: "?A \<inter> ?B = {}" by simp
chaieb@29674
  1345
    from fS have fAB: "finite ?A" "finite ?B" by auto  
chaieb@29674
  1346
    have fA0:"setprod ?f ?A = setprod (\<lambda>i. c) ?A"
chaieb@29674
  1347
      apply (rule setprod_cong) by auto
chaieb@29674
  1348
    have cA: "card ?A = card S - 1" using fS a by auto
chaieb@29674
  1349
    have fA1: "setprod ?f ?A = c ^ card ?A"  unfolding fA0 apply (rule setprod_constant) using fS by auto
chaieb@29674
  1350
    have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
chaieb@29674
  1351
      using setprod_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
chaieb@29674
  1352
      by simp
chaieb@29674
  1353
    then have ?thesis using a cA
haftmann@36349
  1354
      by (simp add: fA1 field_simps cong add: setprod_cong cong del: if_weak_cong)}
chaieb@29674
  1355
  ultimately show ?thesis by blast
chaieb@29674
  1356
qed
chaieb@29674
  1357
haftmann@51489
  1358
lemma setprod_eq_1_iff [simp]:
haftmann@51489
  1359
  "finite F ==> setprod f F = 1 \<longleftrightarrow> (ALL a:F. f a = (1::nat))"
haftmann@51489
  1360
  by (induct set: finite) auto
chaieb@29674
  1361
haftmann@51489
  1362
lemma setprod_pos_nat:
haftmann@51489
  1363
  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"
haftmann@51489
  1364
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
haftmann@51489
  1365
haftmann@51489
  1366
lemma setprod_pos_nat_iff[simp]:
haftmann@51489
  1367
  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"
haftmann@51489
  1368
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
haftmann@51489
  1369
haftmann@51489
  1370
haftmann@51489
  1371
subsection {* Generic lattice operations over a set *}
haftmann@35816
  1372
haftmann@35816
  1373
no_notation times (infixl "*" 70)
haftmann@35816
  1374
no_notation Groups.one ("1")
haftmann@35816
  1375
haftmann@51489
  1376
haftmann@51489
  1377
subsubsection {* Without neutral element *}
haftmann@51489
  1378
haftmann@51489
  1379
locale semilattice_set = semilattice
haftmann@51489
  1380
begin
haftmann@51489
  1381
haftmann@51489
  1382
definition F :: "'a set \<Rightarrow> 'a"
haftmann@51489
  1383
where
haftmann@51489
  1384
  eq_fold': "F A = the (Finite_Set.fold (\<lambda>x y. Some (case y of None \<Rightarrow> x | Some z \<Rightarrow> f x z)) None A)"
haftmann@51489
  1385
haftmann@51489
  1386
lemma eq_fold:
haftmann@51489
  1387
  assumes "finite A"
haftmann@51489
  1388
  shows "F (insert x A) = Finite_Set.fold f x A"
haftmann@51489
  1389
proof (rule sym)
haftmann@51489
  1390
  let ?f = "\<lambda>x y. Some (case y of None \<Rightarrow> x | Some z \<Rightarrow> f x z)"
haftmann@51489
  1391
  interpret comp_fun_idem f
haftmann@51489
  1392
    by default (simp_all add: fun_eq_iff left_commute)
haftmann@51489
  1393
  interpret comp_fun_idem "?f"
haftmann@51489
  1394
    by default (simp_all add: fun_eq_iff commute left_commute split: option.split)
haftmann@51489
  1395
  from assms show "Finite_Set.fold f x A = F (insert x A)"
haftmann@51489
  1396
  proof induct
haftmann@51489
  1397
    case empty then show ?case by (simp add: eq_fold')
haftmann@51489
  1398
  next
haftmann@51489
  1399
    case (insert y B) then show ?case by (simp add: insert_commute [of x] eq_fold')
haftmann@51489
  1400
  qed
haftmann@51489
  1401
qed
haftmann@51489
  1402
haftmann@51489
  1403
lemma singleton [simp]:
haftmann@51489
  1404
  "F {x} = x"
haftmann@51489
  1405
  by (simp add: eq_fold)
haftmann@51489
  1406
haftmann@51489
  1407
lemma insert_not_elem:
haftmann@51489
  1408
  assumes "finite A" and "x \<notin> A" and "A \<noteq> {}"
haftmann@51489
  1409
  shows "F (insert x A) = x * F A"
haftmann@51489
  1410
proof -
haftmann@51489
  1411
  interpret comp_fun_idem f
haftmann@51489
  1412
    by default (simp_all add: fun_eq_iff left_commute)
haftmann@51489
  1413
  from `A \<noteq> {}` obtain b where "b \<in> A" by blast
haftmann@51489
  1414
  then obtain B where *: "A = insert b B" "b \<notin> B" by (blast dest: mk_disjoint_insert)
haftmann@51489
  1415
  with `finite A` and `x \<notin> A`
haftmann@51489
  1416
    have "finite (insert x B)" and "b \<notin> insert x B" by auto
haftmann@51489
  1417
  then have "F (insert b (insert x B)) = x * F (insert b B)"
haftmann@51489
  1418
    by (simp add: eq_fold)
haftmann@51489
  1419
  then show ?thesis by (simp add: * insert_commute)
haftmann@51489
  1420
qed
haftmann@51489
  1421
haftmann@51489
  1422
lemma subsumption:
haftmann@51489
  1423
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1424
  shows "x * F A = F A"
haftmann@51489
  1425
proof -
haftmann@51489
  1426
  from assms have "A \<noteq> {}" by auto
haftmann@51489
  1427
  with `finite A` show ?thesis using `x \<in> A`
haftmann@51489
  1428
    by (induct A rule: finite_ne_induct) (auto simp add: ac_simps insert_not_elem)
haftmann@51489
  1429
qed
haftmann@51489
  1430
haftmann@51489
  1431
lemma insert [simp]:
haftmann@51489
  1432
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1433
  shows "F (insert x A) = x * F A"
haftmann@51489
  1434
  using assms by (cases "x \<in> A") (simp_all add: insert_absorb subsumption insert_not_elem)
haftmann@51489
  1435
haftmann@51489
  1436
lemma union:
haftmann@51489
  1437
  assumes "finite A" "A \<noteq> {}" and "finite B" "B \<noteq> {}"
haftmann@51489
  1438
  shows "F (A \<union> B) = F A * F B"
haftmann@51489
  1439
  using assms by (induct A rule: finite_ne_induct) (simp_all add: ac_simps)
haftmann@51489
  1440
haftmann@51489
  1441
lemma remove:
haftmann@51489
  1442
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1443
  shows "F A = (if A - {x} = {} then x else x * F (A - {x}))"
haftmann@51489
  1444
proof -
haftmann@51489
  1445
  from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
haftmann@51489
  1446
  with assms show ?thesis by simp
haftmann@51489
  1447
qed
haftmann@51489
  1448
haftmann@51489
  1449
lemma insert_remove:
haftmann@51489
  1450
  assumes "finite A"
haftmann@51489
  1451
  shows "F (insert x A) = (if A - {x} = {} then x else x * F (A - {x}))"
haftmann@51489
  1452
  using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
haftmann@51489
  1453
haftmann@51489
  1454
lemma subset:
haftmann@51489
  1455
  assumes "finite A" "B \<noteq> {}" and "B \<subseteq> A"
haftmann@51489
  1456
  shows "F B * F A = F A"
haftmann@51489
  1457
proof -
haftmann@51489
  1458
  from assms have "A \<noteq> {}" and "finite B" by (auto dest: finite_subset)
haftmann@51489
  1459
  with assms show ?thesis by (simp add: union [symmetric] Un_absorb1)
haftmann@51489
  1460
qed
haftmann@51489
  1461
haftmann@51489
  1462
lemma closed:
haftmann@51489
  1463
  assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
haftmann@51489
  1464
  shows "F A \<in> A"
haftmann@51489
  1465
using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
haftmann@51489
  1466
  case singleton then show ?case by simp
haftmann@51489
  1467
next
haftmann@51489
  1468
  case insert with elem show ?case by force
haftmann@51489
  1469
qed
haftmann@51489
  1470
haftmann@51489
  1471
lemma hom_commute:
haftmann@51489
  1472
  assumes hom: "\<And>x y. h (x * y) = h x * h y"
haftmann@51489
  1473
  and N: "finite N" "N \<noteq> {}"
haftmann@51489
  1474
  shows "h (F N) = F (h ` N)"
haftmann@51489
  1475
using N proof (induct rule: finite_ne_induct)
haftmann@51489
  1476
  case singleton thus ?case by simp
haftmann@51489
  1477
next
haftmann@51489
  1478
  case (insert n N)
haftmann@51489
  1479
  then have "h (F (insert n N)) = h (n * F N)" by simp
haftmann@51489
  1480
  also have "\<dots> = h n * h (F N)" by (rule hom)
haftmann@51489
  1481
  also have "h (F N) = F (h ` N)" by (rule insert)
haftmann@51489
  1482
  also have "h n * \<dots> = F (insert (h n) (h ` N))"
haftmann@51489
  1483
    using insert by simp
haftmann@51489
  1484
  also have "insert (h n) (h ` N) = h ` insert n N" by simp
haftmann@51489
  1485
  finally show ?case .
haftmann@51489
  1486
qed
haftmann@51489
  1487
haftmann@51489
  1488
end
haftmann@51489
  1489
haftmann@51489
  1490
locale semilattice_order_set = semilattice_order + semilattice_set
haftmann@51489
  1491
begin
haftmann@51489
  1492
haftmann@51489
  1493
lemma bounded_iff:
haftmann@51489
  1494
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1495
  shows "x \<preceq> F A \<longleftrightarrow> (\<forall>a\<in>A. x \<preceq> a)"
haftmann@51489
  1496
  using assms by (induct rule: finite_ne_induct) (simp_all add: bounded_iff)
haftmann@51489
  1497
haftmann@51489
  1498
lemma boundedI:
haftmann@51489
  1499
  assumes "finite A"
haftmann@51489
  1500
  assumes "A \<noteq> {}"
haftmann@51489
  1501
  assumes "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
haftmann@51489
  1502
  shows "x \<preceq> F A"
haftmann@51489
  1503
  using assms by (simp add: bounded_iff)
haftmann@51489
  1504
haftmann@51489
  1505
lemma boundedE:
haftmann@51489
  1506
  assumes "finite A" and "A \<noteq> {}" and "x \<preceq> F A"
haftmann@51489
  1507
  obtains "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
haftmann@51489
  1508
  using assms by (simp add: bounded_iff)
haftmann@35816
  1509
haftmann@51489
  1510
lemma coboundedI:
haftmann@51489
  1511
  assumes "finite A"
haftmann@51489
  1512
    and "a \<in> A"
haftmann@51489
  1513
  shows "F A \<preceq> a"
haftmann@51489
  1514
proof -
haftmann@51489
  1515
  from assms have "A \<noteq> {}" by auto
haftmann@51489
  1516
  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
haftmann@51489
  1517
  proof (induct rule: finite_ne_induct)
haftmann@51489
  1518
    case singleton thus ?case by (simp add: refl)
haftmann@51489
  1519
  next
haftmann@51489
  1520
    case (insert x B)
haftmann@51489
  1521
    from insert have "a = x \<or> a \<in> B" by simp
haftmann@51489
  1522
    then show ?case using insert by (auto intro: coboundedI2)
haftmann@51489
  1523
  qed
haftmann@51489
  1524
qed
haftmann@51489
  1525
haftmann@51489
  1526
lemma antimono:
haftmann@51489
  1527
  assumes "A \<subseteq> B" and "A \<noteq> {}" and "finite B"
haftmann@51489
  1528
  shows "F B \<preceq> F A"
haftmann@51489
  1529
proof (cases "A = B")
haftmann@51489
  1530
  case True then show ?thesis by (simp add: refl)
haftmann@51489
  1531
next
haftmann@51489
  1532
  case False
haftmann@51489
  1533
  have B: "B = A \<union> (B - A)" using `A \<subseteq> B` by blast
haftmann@51489
  1534
  then have "F B = F (A \<union> (B - A))" by simp
haftmann@51489
  1535
  also have "\<dots> = F A * F (B - A)" using False assms by (subst union) (auto intro: finite_subset)
haftmann@51489
  1536
  also have "\<dots> \<preceq> F A" by simp
haftmann@51489
  1537
  finally show ?thesis .
haftmann@51489
  1538
qed
haftmann@51489
  1539
haftmann@51489
  1540
end
haftmann@51489
  1541
haftmann@51489
  1542
haftmann@51489
  1543
subsubsection {* With neutral element *}
haftmann@51489
  1544
haftmann@51489
  1545
locale semilattice_neutr_set = semilattice_neutr
haftmann@51489
  1546
begin
haftmann@51489
  1547
haftmann@51489
  1548
definition F :: "'a set \<Rightarrow> 'a"
haftmann@51489
  1549
where
haftmann@51489
  1550
  eq_fold: "F A = Finite_Set.fold f 1 A"
haftmann@51489
  1551
haftmann@51489
  1552
lemma infinite [simp]:
haftmann@51489
  1553
  "\<not> finite A \<Longrightarrow> F A = 1"
haftmann@51489
  1554
  by (simp add: eq_fold)
haftmann@51489
  1555
haftmann@51489
  1556
lemma empty [simp]:
haftmann@51489
  1557
  "F {} = 1"
haftmann@51489
  1558
  by (simp add: eq_fold)
haftmann@51489
  1559
haftmann@51489
  1560
lemma insert [simp]:
haftmann@51489
  1561
  assumes "finite A"
haftmann@51489
  1562
  shows "F (insert x A) = x * F A"
haftmann@51489
  1563
proof -
haftmann@51489
  1564
  interpret comp_fun_idem f
haftmann@51489
  1565
    by default (simp_all add: fun_eq_iff left_commute)
haftmann@51489
  1566
  from assms show ?thesis by (simp add: eq_fold)
haftmann@51489
  1567
qed
haftmann@51489
  1568
haftmann@51489
  1569
lemma subsumption:
haftmann@51489
  1570
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1571
  shows "x * F A = F A"
haftmann@51489
  1572
proof -
haftmann@51489
  1573
  from assms have "A \<noteq> {}" by auto
haftmann@51489
  1574
  with `finite A` show ?thesis using `x \<in> A`
haftmann@51489
  1575
    by (induct A rule: finite_ne_induct) (auto simp add: ac_simps)
haftmann@51489
  1576
qed
haftmann@51489
  1577
haftmann@51489
  1578
lemma union:
haftmann@51489
  1579
  assumes "finite A" and "finite B"
haftmann@51489
  1580
  shows "F (A \<union> B) = F A * F B"
haftmann@51489
  1581
  using assms by (induct A) (simp_all add: ac_simps)
haftmann@51489
  1582
haftmann@51489
  1583
lemma remove:
haftmann@51489
  1584
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1585
  shows "F A = x * F (A - {x})"
haftmann@51489
  1586
proof -
haftmann@51489
  1587
  from assms obtain B where "A = insert x B" and "x \<notin> B" by (blast dest: mk_disjoint_insert)
haftmann@51489
  1588
  with assms show ?thesis by simp
haftmann@51489
  1589
qed
haftmann@51489
  1590
haftmann@51489
  1591
lemma insert_remove:
haftmann@51489
  1592
  assumes "finite A"
haftmann@51489
  1593
  shows "F (insert x A) = x * F (A - {x})"
haftmann@51489
  1594
  using assms by (cases "x \<in> A") (simp_all add: insert_absorb remove)
haftmann@51489
  1595
haftmann@51489
  1596
lemma subset:
haftmann@51489
  1597
  assumes "finite A" and "B \<subseteq> A"
haftmann@51489
  1598
  shows "F B * F A = F A"
haftmann@51489
  1599
proof -
haftmann@51489
  1600
  from assms have "finite B" by (auto dest: finite_subset)
haftmann@51489
  1601
  with assms show ?thesis by (simp add: union [symmetric] Un_absorb1)
haftmann@51489
  1602
qed
haftmann@51489
  1603
haftmann@51489
  1604
lemma closed:
haftmann@51489
  1605
  assumes "finite A" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x, y}"
haftmann@51489
  1606
  shows "F A \<in> A"
haftmann@51489
  1607
using `finite A` `A \<noteq> {}` proof (induct rule: finite_ne_induct)
haftmann@51489
  1608
  case singleton then show ?case by simp
haftmann@51489
  1609
next
haftmann@51489
  1610
  case insert with elem show ?case by force
haftmann@51489
  1611
qed
haftmann@51489
  1612
haftmann@51489
  1613
end
haftmann@51489
  1614
haftmann@51489
  1615
locale semilattice_order_neutr_set = semilattice_neutr_order + semilattice_neutr_set
haftmann@51489
  1616
begin
haftmann@51489
  1617
haftmann@51489
  1618
lemma bounded_iff:
haftmann@51489
  1619
  assumes "finite A"
haftmann@51489
  1620
  shows "x \<preceq> F A \<longleftrightarrow> (\<forall>a\<in>A. x \<preceq> a)"
haftmann@51489
  1621
  using assms by (induct A) (simp_all add: bounded_iff)
haftmann@51489
  1622
haftmann@51489
  1623
lemma boundedI:
haftmann@51489
  1624
  assumes "finite A"
haftmann@51489
  1625
  assumes "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
haftmann@51489
  1626
  shows "x \<preceq> F A"
haftmann@51489
  1627
  using assms by (simp add: bounded_iff)
haftmann@51489
  1628
haftmann@51489
  1629
lemma boundedE:
haftmann@51489
  1630
  assumes "finite A" and "x \<preceq> F A"
haftmann@51489
  1631
  obtains "\<And>a. a \<in> A \<Longrightarrow> x \<preceq> a"
haftmann@51489
  1632
  using assms by (simp add: bounded_iff)
haftmann@51489
  1633
haftmann@51489
  1634
lemma coboundedI:
haftmann@51489
  1635
  assumes "finite A"
haftmann@51489
  1636
    and "a \<in> A"
haftmann@51489
  1637
  shows "F A \<preceq> a"
haftmann@51489
  1638
proof -
haftmann@51489
  1639
  from assms have "A \<noteq> {}" by auto
haftmann@51489
  1640
  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
haftmann@51489
  1641
  proof (induct rule: finite_ne_induct)
haftmann@51489
  1642
    case singleton thus ?case by (simp add: refl)
haftmann@51489
  1643
  next
haftmann@51489
  1644
    case (insert x B)
haftmann@51489
  1645
    from insert have "a = x \<or> a \<in> B" by simp
haftmann@51489
  1646
    then show ?case using insert by (auto intro: coboundedI2)
haftmann@51489
  1647
  qed
haftmann@51489
  1648
qed
haftmann@51489
  1649
haftmann@51489
  1650
lemma antimono:
haftmann@51489
  1651
  assumes "A \<subseteq> B" and "finite B"
haftmann@51489
  1652
  shows "F B \<preceq> F A"
haftmann@51489
  1653
proof (cases "A = B")
haftmann@51489
  1654
  case True then show ?thesis by (simp add: refl)
haftmann@51489
  1655
next
haftmann@51489
  1656
  case False
haftmann@51489
  1657
  have B: "B = A \<union> (B - A)" using `A \<subseteq> B` by blast
haftmann@51489
  1658
  then have "F B = F (A \<union> (B - A))" by simp
haftmann@51489
  1659
  also have "\<dots> = F A * F (B - A)" using False assms by (subst union) (auto intro: finite_subset)
haftmann@51489
  1660
  also have "\<dots> \<preceq> F A" by simp
haftmann@51489
  1661
  finally show ?thesis .
haftmann@51489
  1662
qed
haftmann@51489
  1663
haftmann@51489
  1664
end
haftmann@35816
  1665
haftmann@35816
  1666
notation times (infixl "*" 70)
haftmann@35816
  1667
notation Groups.one ("1")
haftmann@22917
  1668
haftmann@35816
  1669
haftmann@51489
  1670
subsection {* Lattice operations on finite sets *}
haftmann@35816
  1671
haftmann@51489
  1672
text {*
haftmann@51489
  1673
  For historic reasons, there is the sublocale dependency from @{class distrib_lattice}
haftmann@51489
  1674
  to @{class linorder}.  This is badly designed: both should depend on a common abstract
haftmann@51489
  1675
  distributive lattice rather than having this non-subclass dependecy between two
haftmann@51489
  1676
  classes.  But for the moment we have to live with it.  This forces us to setup
haftmann@51489
  1677
  this sublocale dependency simultaneously with the lattice operations on finite
haftmann@51489
  1678
  sets, to avoid garbage.
haftmann@51489
  1679
*}
haftmann@22917
  1680
haftmann@51489
  1681
definition (in semilattice_inf) Inf_fin :: "'a set \<Rightarrow> 'a" ("\<Sqinter>\<^bsub>fin\<^esub>_" [900] 900)
haftmann@51489
  1682
where
haftmann@51489
  1683
  "Inf_fin = semilattice_set.F inf"
haftmann@26041
  1684
haftmann@51489
  1685
definition (in semilattice_sup) Sup_fin :: "'a set \<Rightarrow> 'a" ("\<Squnion>\<^bsub>fin\<^esub>_" [900] 900)
haftmann@51489
  1686
where
haftmann@51489
  1687
  "Sup_fin = semilattice_set.F sup"
haftmann@35816
  1688
haftmann@51489
  1689
definition (in linorder) Min :: "'a set \<Rightarrow> 'a"
haftmann@51489
  1690
where
haftmann@51489
  1691
  "Min = semilattice_set.F min"
haftmann@35816
  1692
haftmann@51489
  1693
definition (in linorder) Max :: "'a set \<Rightarrow> 'a"
haftmann@51489
  1694
where
haftmann@51489
  1695
  "Max = semilattice_set.F max"
haftmann@51489
  1696
haftmann@51489
  1697
text {* An aside: @{const min}/@{const max} on linear orders as special case of @{const inf}/@{const sup} *}
haftmann@35816
  1698
haftmann@51489
  1699
sublocale linorder < min_max!: distrib_lattice min less_eq less max
haftmann@51489
  1700
where
haftmann@51489
  1701
  "semilattice_inf.Inf_fin min = Min"
haftmann@51489
  1702
  and "semilattice_sup.Sup_fin max = Max"
haftmann@26041
  1703
proof -
haftmann@51489
  1704
  show "class.distrib_lattice min less_eq less max"
haftmann@51489
  1705
  proof
haftmann@51489
  1706
    fix x y z
haftmann@51489
  1707
    show "max x (min y z) = min (max x y) (max x z)"
haftmann@51489
  1708
      by (auto simp add: min_def max_def)
haftmann@51489
  1709
  qed (auto simp add: min_def max_def not_le less_imp_le)
haftmann@51489
  1710
  then interpret min_max!: distrib_lattice min less_eq less max .
haftmann@51489
  1711
  show "semilattice_inf.Inf_fin min = Min"
haftmann@51489
  1712
    by (simp only: min_max.Inf_fin_def Min_def)
haftmann@51489
  1713
  show "semilattice_sup.Sup_fin max = Max"
haftmann@51489
  1714
    by (simp only: min_max.Sup_fin_def Max_def)
haftmann@26041
  1715
qed
haftmann@26041
  1716
haftmann@51489
  1717
lemma inf_min: "inf = (min \<Colon> 'a\<Colon>{semilattice_inf, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
haftmann@51489
  1718
  by (rule ext)+ (auto intro: antisym)
haftmann@51489
  1719
haftmann@51489
  1720
lemma sup_max: "sup = (max \<Colon> 'a\<Colon>{semilattice_sup, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
haftmann@51489
  1721
  by (rule ext)+ (auto intro: antisym)
haftmann@51489
  1722
haftmann@51489
  1723
lemmas le_maxI1 = min_max.sup_ge1
haftmann@51489
  1724
lemmas le_maxI2 = min_max.sup_ge2
haftmann@51489
  1725
 
haftmann@51489
  1726
lemmas min_ac = min_max.inf_assoc min_max.inf_commute
haftmann@51489
  1727
  min_max.inf.left_commute
haftmann@51489
  1728
haftmann@51489
  1729
lemmas max_ac = min_max.sup_assoc min_max.sup_commute
haftmann@51489
  1730
  min_max.sup.left_commute
haftmann@51489
  1731
haftmann@51489
  1732
haftmann@51489
  1733
text {* Lattice operations proper *}
haftmann@51489
  1734
haftmann@51489
  1735
sublocale semilattice_inf < Inf_fin!: semilattice_order_set inf less_eq less
haftmann@51489
  1736
where
haftmann@51489
  1737
  "Inf_fin.F = Inf_fin"
haftmann@26757
  1738
proof -
haftmann@51489
  1739
  show "semilattice_order_set inf less_eq less" ..
haftmann@51489
  1740
  then interpret Inf_fin!: semilattice_order_set inf less_eq less.
haftmann@51489
  1741
  show "Inf_fin.F = Inf_fin"
haftmann@51489
  1742
    by (fact Inf_fin_def [symmetric])
haftmann@26041
  1743
qed
haftmann@26041
  1744
haftmann@51489
  1745
sublocale semilattice_sup < Sup_fin!: semilattice_order_set sup greater_eq greater
haftmann@51489
  1746
where
haftmann@51489
  1747
  "Sup_fin.F = Sup_fin"
haftmann@51489
  1748
proof -
haftmann@51489
  1749
  show "semilattice_order_set sup greater_eq greater" ..
haftmann@51489
  1750
  then interpret Sup_fin!: semilattice_order_set sup greater_eq greater .
haftmann@51489
  1751
  show "Sup_fin.F = Sup_fin"
haftmann@51489
  1752
    by (fact Sup_fin_def [symmetric])
haftmann@51489
  1753
qed
haftmann@35816
  1754
haftmann@51489
  1755
haftmann@51489
  1756
subsection {* Infimum and Supremum over non-empty sets *}
haftmann@22917
  1757
haftmann@51489
  1758
text {*
haftmann@51489
  1759
  After this non-regular bootstrap, things continue canonically.
haftmann@51489
  1760
*}
haftmann@35816
  1761
haftmann@35816
  1762
context lattice
haftmann@35816
  1763
begin
haftmann@25062
  1764
wenzelm@31916
  1765
lemma Inf_le_Sup [simp]: "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<Sqinter>\<^bsub>fin\<^esub>A \<le> \<Squnion>\<^bsub>fin\<^esub>A"
nipkow@15500
  1766
apply(subgoal_tac "EX a. a:A")
nipkow@15500
  1767
prefer 2 apply blast
nipkow@15500
  1768
apply(erule exE)
haftmann@22388
  1769
apply(rule order_trans)
haftmann@51489
  1770
apply(erule (1) Inf_fin.coboundedI)
haftmann@51489
  1771
apply(erule (1) Sup_fin.coboundedI)
nipkow@15500
  1772
done
nipkow@15500
  1773
haftmann@24342
  1774
lemma sup_Inf_absorb [simp]:
wenzelm@31916
  1775
  "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a (\<Sqinter>\<^bsub>fin\<^esub>A) = a"
nipkow@15512
  1776
apply(subst sup_commute)
haftmann@51489
  1777
apply(simp add: sup_absorb2 Inf_fin.coboundedI)
nipkow@15504
  1778
done
nipkow@15504
  1779
haftmann@24342
  1780
lemma inf_Sup_absorb [simp]:
wenzelm@31916
  1781
  "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> inf a (\<Squnion>\<^bsub>fin\<^esub>A) = a"
haftmann@51489
  1782
by (simp add: inf_absorb1 Sup_fin.coboundedI)
haftmann@24342
  1783
haftmann@24342
  1784
end
haftmann@24342
  1785
haftmann@24342
  1786
context distrib_lattice
haftmann@24342
  1787
begin
haftmann@24342
  1788
haftmann@24342
  1789
lemma sup_Inf1_distrib:
haftmann@26041
  1790
  assumes "finite A"
haftmann@26041
  1791
    and "A \<noteq> {}"
wenzelm@31916
  1792
  shows "sup x (\<Sqinter>\<^bsub>fin\<^esub>A) = \<Sqinter>\<^bsub>fin\<^esub>{sup x a|a. a \<in> A}"
haftmann@51489
  1793
using assms by (simp add: image_def Inf_fin.hom_commute [where h="sup x", OF sup_inf_distrib1])
haftmann@51489
  1794
  (rule arg_cong [where f="Inf_fin"], blast)
nipkow@18423
  1795
haftmann@24342
  1796
lemma sup_Inf2_distrib:
haftmann@24342
  1797
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
wenzelm@31916
  1798
  shows "sup (\<Sqinter>\<^bsub>fin\<^esub>A) (\<Sqinter>\<^bsub>fin\<^esub>B) = \<Sqinter>\<^bsub>fin\<^esub>{sup a b|a b. a \<in> A \<and> b \<in> B}"
haftmann@24342
  1799
using A proof (induct rule: finite_ne_induct)
haftmann@51489
  1800
  case singleton then show ?case
wenzelm@41550
  1801
    by (simp add: sup_Inf1_distrib [OF B])
nipkow@15500
  1802
next
nipkow@15500
  1803
  case (insert x A)
haftmann@25062
  1804
  have finB: "finite {sup x b |b. b \<in> B}"
haftmann@51489
  1805
    by (rule finite_surj [where f = "sup x", OF B(1)], auto)
haftmann@25062
  1806
  have finAB: "finite {sup a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@15500
  1807
  proof -
haftmann@25062
  1808
    have "{sup a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {sup a b})"
nipkow@15500
  1809
      by blast
berghofe@15517
  1810
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@15500
  1811
  qed
haftmann@25062
  1812
  have ne: "{sup a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
wenzelm@31916
  1813
  have "sup (\<Sqinter>\<^bsub>fin\<^esub>(insert x A)) (\<Sqinter>\<^bsub>fin\<^esub>B) = sup (inf x (\<Sqinter>\<^bsub>fin\<^esub>A)) (\<Sqinter>\<^bsub>fin\<^esub>B)"
wenzelm@41550
  1814
    using insert by simp
wenzelm@31916
  1815
  also have "\<dots> = inf (sup x (\<Sqinter>\<^bsub>fin\<^esub>B)) (sup (\<Sqinter>\<^bsub>fin\<^esub>A) (\<Sqinter>\<^bsub>fin\<^esub>B))" by(rule sup_inf_distrib2)
wenzelm@31916
  1816
  also have "\<dots> = inf (\<Sqinter>\<^bsub>fin\<^esub>{sup x b|b. b \<in> B}) (\<Sqinter>\<^bsub>fin\<^esub>{sup a b|a b. a \<in> A \<and> b \<in> B})"
nipkow@15500
  1817
    using insert by(simp add:sup_Inf1_distrib[OF B])
wenzelm@31916
  1818
  also have "\<dots> = \<Sqinter>\<^bsub>fin\<^esub>({sup x b |b. b \<in> B} \<union> {sup a b |a b. a \<in> A \<and> b \<in> B})"
wenzelm@31916
  1819
    (is "_ = \<Sqinter>\<^bsub>fin\<^esub>?M")
nipkow@15500
  1820
    using B insert
haftmann@51489
  1821
    by (simp add: Inf_fin.union [OF finB _ finAB ne])
haftmann@25062
  1822
  also have "?M = {sup a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@15500
  1823
    by blast
nipkow@15500
  1824
  finally show ?case .
nipkow@15500
  1825
qed
nipkow@15500
  1826
haftmann@24342
  1827
lemma inf_Sup1_distrib:
haftmann@26041
  1828
  assumes "finite A" and "A \<noteq> {}"
wenzelm@31916
  1829
  shows "inf x (\<Squnion>\<^bsub>fin\<^esub>A) = \<Squnion>\<^bsub>fin\<^esub>{inf x a|a. a \<in> A}"
haftmann@51489
  1830
using assms by (simp add: image_def Sup_fin.hom_commute [where h="inf x", OF inf_sup_distrib1])
haftmann@51489
  1831
  (rule arg_cong [where f="Sup_fin"], blast)
nipkow@18423
  1832
haftmann@24342
  1833
lemma inf_Sup2_distrib:
haftmann@24342
  1834
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
wenzelm@31916
  1835
  shows "inf (\<Squnion>\<^bsub>fin\<^esub>A) (\<Squnion>\<^bsub>fin\<^esub>B) = \<Squnion>\<^bsub>fin\<^esub>{inf a b|a b. a \<in> A \<and> b \<in> B}"
haftmann@24342
  1836
using A proof (induct rule: finite_ne_induct)
nipkow@18423
  1837
  case singleton thus ?case
huffman@44921
  1838
    by(simp add: inf_Sup1_distrib [OF B])
nipkow@18423
  1839
next
nipkow@18423
  1840
  case (insert x A)
haftmann@25062
  1841
  have finB: "finite {inf x b |b. b \<in> B}"
haftmann@25062
  1842
    by(rule finite_surj[where f = "%b. inf x b", OF B(1)], auto)
haftmann@25062
  1843
  have finAB: "finite {inf a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@18423
  1844
  proof -
haftmann@25062
  1845
    have "{inf a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {inf a b})"
nipkow@18423
  1846
      by blast
nipkow@18423
  1847
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@18423
  1848
  qed
haftmann@25062
  1849
  have ne: "{inf a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
wenzelm@31916
  1850
  have "inf (\<Squnion>\<^bsub>fin\<^esub>(insert x A)) (\<Squnion>\<^bsub>fin\<^esub>B) = inf (sup x (\<Squnion>\<^bsub>fin\<^esub>A)) (\<Squnion>\<^bsub>fin\<^esub>B)"
wenzelm@41550
  1851
    using insert by simp
wenzelm@31916
  1852
  also have "\<dots> = sup (inf x (\<Squnion>\<^bsub>fin\<^esub>B)) (inf (\<Squnion>\<^bsub>fin\<^esub>A) (\<Squnion>\<^bsub>fin\<^esub>B))" by(rule inf_sup_distrib2)
wenzelm@31916
  1853
  also have "\<dots> = sup (\<Squnion>\<^bsub>fin\<^esub>{inf x b|b. b \<in> B}) (\<Squnion>\<^bsub>fin\<^esub>{inf a b|a b. a \<in> A \<and> b \<in> B})"
nipkow@18423
  1854
    using insert by(simp add:inf_Sup1_distrib[OF B])
wenzelm@31916
  1855
  also have "\<dots> = \<Squnion>\<^bsub>fin\<^esub>({inf x b |b. b \<in> B} \<union> {inf a b |a b. a \<in> A \<and> b \<in> B})"
wenzelm@31916
  1856
    (is "_ = \<Squnion>\<^bsub>fin\<^esub>?M")
nipkow@18423
  1857
    using B insert
haftmann@51489
  1858
    by (simp add: Sup_fin.union [OF finB _ finAB ne])
haftmann@25062
  1859
  also have "?M = {inf a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@18423
  1860
    by blast
nipkow@18423
  1861
  finally show ?case .
nipkow@18423
  1862
qed
nipkow@18423
  1863
haftmann@24342
  1864
end
haftmann@24342
  1865
haftmann@35719
  1866
context complete_lattice
haftmann@35719
  1867
begin
haftmann@35719
  1868
haftmann@35719
  1869
lemma Inf_fin_Inf:
haftmann@35719
  1870
  assumes "finite A" and "A \<noteq> {}"
haftmann@35719
  1871
  shows "\<Sqinter>\<^bsub>fin\<^esub>A = Inf A"
haftmann@35719
  1872
proof -
haftmann@51489
  1873
  from assms obtain b B where "A = insert b B" and "finite B" by auto
haftmann@51489
  1874
  then show ?thesis
haftmann@51489
  1875
    by (simp add: Inf_fin.eq_fold inf_Inf_fold_inf inf.commute [of b])
haftmann@35719
  1876
qed
haftmann@35719
  1877
haftmann@35719
  1878
lemma Sup_fin_Sup:
haftmann@35719
  1879
  assumes "finite A" and "A \<noteq> {}"
haftmann@35719
  1880
  shows "\<Squnion>\<^bsub>fin\<^esub>A = Sup A"
haftmann@35719
  1881
proof -
haftmann@51489
  1882
  from assms obtain b B where "A = insert b B" and "finite B" by auto
haftmann@51489
  1883
  then show ?thesis
haftmann@51489
  1884
    by (simp add: Sup_fin.eq_fold sup_Sup_fold_sup sup.commute [of b])
haftmann@35719
  1885
qed
haftmann@35719
  1886
haftmann@35719
  1887
end
haftmann@35719
  1888
haftmann@22917
  1889
haftmann@51489
  1890
subsection {* Minimum and Maximum over non-empty sets *}
haftmann@22917
  1891
haftmann@51489
  1892
text {*
haftmann@51489
  1893
  This case is already setup by the @{text min_max} sublocale dependency from above.  But note
haftmann@51489
  1894
  that this yields irregular prefixes, e.g.~@{text min_max.Inf_fin.insert} instead
haftmann@51489
  1895
  of @{text Max.insert}.
haftmann@51489
  1896
*}
haftmann@22917
  1897
haftmann@24342
  1898
context linorder
haftmann@22917
  1899
begin
haftmann@22917
  1900
haftmann@26041
  1901
lemma dual_min:
haftmann@51489
  1902
  "ord.min greater_eq = max"
wenzelm@46904
  1903
  by (auto simp add: ord.min_def max_def fun_eq_iff)
haftmann@26041
  1904
haftmann@51489
  1905
lemma dual_max:
haftmann@51489
  1906
  "ord.max greater_eq = min"
haftmann@51489
  1907
  by (auto simp add: ord.max_def min_def fun_eq_iff)
haftmann@51489
  1908
haftmann@51489
  1909
lemma dual_Min:
haftmann@51489
  1910
  "linorder.Min greater_eq = Max"
haftmann@26041
  1911
proof -
haftmann@51489
  1912
  interpret dual!: linorder greater_eq greater by (fact dual_linorder)
haftmann@51489
  1913
  show ?thesis by (simp add: dual.Min_def dual_min Max_def)
haftmann@26041
  1914
qed
haftmann@26041
  1915
haftmann@51489
  1916
lemma dual_Max:
haftmann@51489
  1917
  "linorder.Max greater_eq = Min"
haftmann@26041
  1918
proof -
haftmann@51489
  1919
  interpret dual!: linorder greater_eq greater by (fact dual_linorder)
haftmann@51489
  1920
  show ?thesis by (simp add: dual.Max_def dual_max Min_def)
haftmann@26041
  1921
qed
haftmann@26041
  1922
haftmann@51489
  1923
lemmas Min_singleton = min_max.Inf_fin.singleton
haftmann@51489
  1924
lemmas Max_singleton = min_max.Sup_fin.singleton
haftmann@51489
  1925
lemmas Min_insert = min_max.Inf_fin.insert
haftmann@51489
  1926
lemmas Max_insert = min_max.Sup_fin.insert
haftmann@51489
  1927
lemmas Min_Un = min_max.Inf_fin.union
haftmann@51489
  1928
lemmas Max_Un = min_max.Sup_fin.union
haftmann@51489
  1929
lemmas hom_Min_commute = min_max.Inf_fin.hom_commute
haftmann@51489
  1930
lemmas hom_Max_commute = min_max.Sup_fin.hom_commute
haftmann@26041
  1931
paulson@24427
  1932
lemma Min_in [simp]:
haftmann@26041
  1933
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1934
  shows "Min A \<in> A"
haftmann@51489
  1935
  using assms by (auto simp add: min_def min_max.Inf_fin.closed)
nipkow@15392
  1936
paulson@24427
  1937
lemma Max_in [simp]:
haftmann@26041
  1938
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1939
  shows "Max A \<in> A"
haftmann@51489
  1940
  using assms by (auto simp add: max_def min_max.Sup_fin.closed)
haftmann@26041
  1941
haftmann@26041
  1942
lemma Min_le [simp]:
haftmann@26757
  1943
  assumes "finite A" and "x \<in> A"
haftmann@26041
  1944
  shows "Min A \<le> x"
haftmann@51489
  1945
  using assms by (fact min_max.Inf_fin.coboundedI)
haftmann@26041
  1946
haftmann@26041
  1947
lemma Max_ge [simp]:
haftmann@26757
  1948
  assumes "finite A" and "x \<in> A"
haftmann@26041
  1949
  shows "x \<le> Max A"
haftmann@51489
  1950
  using assms by (fact min_max.Sup_fin.coboundedI)
haftmann@26041
  1951
haftmann@30325
  1952
lemma Min_eqI:
haftmann@30325
  1953
  assumes "finite A"
haftmann@30325
  1954
  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<ge> x"
haftmann@30325
  1955
    and "x \<in> A"
haftmann@30325
  1956
  shows "Min A = x"
haftmann@30325
  1957
proof (rule antisym)
haftmann@30325
  1958
  from `x \<in> A` have "A \<noteq> {}" by auto
haftmann@30325
  1959
  with assms show "Min A \<ge> x" by simp
haftmann@30325
  1960
next
haftmann@30325
  1961
  from assms show "x \<ge> Min A" by simp
haftmann@30325
  1962
qed
haftmann@30325
  1963
haftmann@30325
  1964
lemma Max_eqI:
haftmann@30325
  1965
  assumes "finite A"
haftmann@30325
  1966
  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
haftmann@30325
  1967
    and "x \<in> A"
haftmann@30325
  1968
  shows "Max A = x"
haftmann@30325
  1969
proof (rule antisym)
haftmann@30325
  1970
  from `x \<in> A` have "A \<noteq> {}" by auto
haftmann@30325
  1971
  with assms show "Max A \<le> x" by simp
haftmann@30325
  1972
next
haftmann@30325
  1973
  from assms show "x \<le> Max A" by simp
haftmann@30325
  1974
qed
haftmann@30325
  1975
haftmann@51489
  1976
lemma Min_ge_iff [simp, no_atp]:
haftmann@51489
  1977
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1978
  shows "x \<le> Min A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
haftmann@51489
  1979
  using assms by (fact min_max.Inf_fin.bounded_iff)
haftmann@51489
  1980
haftmann@51489
  1981
lemma Max_le_iff [simp, no_atp]:
haftmann@51489
  1982
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1983
  shows "Max A \<le> x \<longleftrightarrow> (\<forall>a\<in>A. a \<le> x)"
haftmann@51489
  1984
  using assms by (fact min_max.Sup_fin.bounded_iff)
haftmann@51489
  1985
haftmann@51489
  1986
lemma Min_gr_iff [simp, no_atp]:
haftmann@51489
  1987
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1988
  shows "x < Min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
haftmann@51489
  1989
  using assms by (induct rule: finite_ne_induct) simp_all
haftmann@51489
  1990
haftmann@51489
  1991
lemma Max_less_iff [simp, no_atp]:
haftmann@51489
  1992
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1993
  shows "Max A < x \<longleftrightarrow> (\<forall>a\<in>A. a < x)"
haftmann@51489
  1994
  using assms by (induct rule: finite_ne_induct) simp_all
haftmann@51489
  1995
haftmann@51489
  1996
lemma Min_le_iff [no_atp]:
haftmann@51489
  1997
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  1998
  shows "Min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
haftmann@51489
  1999
  using assms by (induct rule: finite_ne_induct) (simp_all add: min_le_iff_disj)
haftmann@51489
  2000
haftmann@51489
  2001
lemma Max_ge_iff [no_atp]:
haftmann@51489
  2002
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  2003
  shows "x \<le> Max A \<longleftrightarrow> (\<exists>a\<in>A. x \<le> a)"
haftmann@51489
  2004
  using assms by (induct rule: finite_ne_induct) (simp_all add: le_max_iff_disj)
haftmann@51489
  2005
haftmann@51489
  2006
lemma Min_less_iff [no_atp]:
haftmann@51489
  2007
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  2008
  shows "Min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
haftmann@51489
  2009
  using assms by (induct rule: finite_ne_induct) (simp_all add: min_less_iff_disj)
haftmann@51489
  2010
haftmann@51489
  2011
lemma Max_gr_iff [no_atp]:
haftmann@51489
  2012
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  2013
  shows "x < Max A \<longleftrightarrow> (\<exists>a\<in>A. x < a)"
haftmann@51489
  2014
  using assms by (induct rule: finite_ne_induct) (simp_all add: less_max_iff_disj)
haftmann@51489
  2015
haftmann@26041
  2016
lemma Min_antimono:
haftmann@26041
  2017
  assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
haftmann@26041
  2018
  shows "Min N \<le> Min M"
haftmann@51489
  2019
  using assms by (fact min_max.Inf_fin.antimono)
haftmann@26041
  2020
haftmann@26041
  2021
lemma Max_mono:
haftmann@26041
  2022
  assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
haftmann@26041
  2023
  shows "Max M \<le> Max N"
haftmann@51489
  2024
  using assms by (fact min_max.Sup_fin.antimono)
haftmann@51489
  2025
haftmann@51489
  2026
lemma mono_Min_commute:
haftmann@51489
  2027
  assumes "mono f"
haftmann@51489
  2028
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  2029
  shows "f (Min A) = Min (f ` A)"
haftmann@51489
  2030
proof (rule linorder_class.Min_eqI [symmetric])
haftmann@51489
  2031
  from `finite A` show "finite (f ` A)" by simp
haftmann@51489
  2032
  from assms show "f (Min A) \<in> f ` A" by simp
haftmann@51489
  2033
  fix x
haftmann@51489
  2034
  assume "x \<in> f ` A"
haftmann@51489
  2035
  then obtain y where "y \<in> A" and "x = f y" ..
haftmann@51489
  2036
  with assms have "Min A \<le> y" by auto
haftmann@51489
  2037
  with `mono f` have "f (Min A) \<le> f y" by (rule monoE)
haftmann@51489
  2038
  with `x = f y` show "f (Min A) \<le> x" by simp
haftmann@51489
  2039
qed
haftmann@22917
  2040
haftmann@51489
  2041
lemma mono_Max_commute:
haftmann@51489
  2042
  assumes "mono f"
haftmann@51489
  2043
  assumes "finite A" and "A \<noteq> {}"
haftmann@51489
  2044
  shows "f (Max A) = Max (f ` A)"
haftmann@51489
  2045
proof (rule linorder_class.Max_eqI [symmetric])
haftmann@51489
  2046
  from `finite A` show "finite (f ` A)" by simp
haftmann@51489
  2047
  from assms show "f (Max A) \<in> f ` A" by simp
haftmann@51489
  2048
  fix x
haftmann@51489
  2049
  assume "x \<in> f ` A"
haftmann@51489
  2050
  then obtain y where "y \<in> A" and "x = f y" ..
haftmann@51489
  2051
  with assms have "y \<le> Max A" by auto
haftmann@51489
  2052
  with `mono f` have "f y \<le> f (Max A)" by (rule monoE)
haftmann@51489
  2053
  with `x = f y` show "x \<le> f (Max A)" by simp
haftmann@51489
  2054
qed
haftmann@51489
  2055
haftmann@51489
  2056
lemma finite_linorder_max_induct [consumes 1, case_names empty insert]:
haftmann@51489
  2057
  assumes fin: "finite A"
haftmann@51489
  2058
  and empty: "P {}" 
haftmann@51489
  2059
  and insert: "\<And>b A. finite A \<Longrightarrow> \<forall>a\<in>A. a < b \<Longrightarrow> P A \<Longrightarrow> P (insert b A)"
haftmann@51489
  2060
  shows "P A"
urbanc@36079
  2061
using fin empty insert
nipkow@32006
  2062
proof (induct rule: finite_psubset_induct)
urbanc@36079
  2063
  case (psubset A)
urbanc@36079
  2064
  have IH: "\<And>B. \<lbrakk>B < A; P {}; (\<And>A b. \<lbrakk>finite A; \<forall>a\<in>A. a<b; P A\<rbrakk> \<Longrightarrow> P (insert b A))\<rbrakk> \<Longrightarrow> P B" by fact 
urbanc@36079
  2065
  have fin: "finite A" by fact 
urbanc@36079
  2066
  have empty: "P {}" by fact
urbanc@36079
  2067
  have step: "\<And>b A. \<lbrakk>finite A; \<forall>a\<in>A. a < b; P A\<rbrakk> \<Longrightarrow> P (insert b A)" by fact
krauss@26748
  2068
  show "P A"
haftmann@26757
  2069
  proof (cases "A = {}")
urbanc@36079
  2070
    assume "A = {}" 
urbanc@36079
  2071
    then show "P A" using `P {}` by simp
krauss@26748
  2072
  next
urbanc@36079
  2073
    let ?B = "A - {Max A}" 
urbanc@36079
  2074
    let ?A = "insert (Max A) ?B"
urbanc@36079
  2075
    have "finite ?B" using `finite A` by simp
krauss@26748
  2076
    assume "A \<noteq> {}"
krauss@26748
  2077
    with `finite A` have "Max A : A" by auto
urbanc@36079
  2078
    then have A: "?A = A" using insert_Diff_single insert_absorb by auto
haftmann@51489
  2079
    then have "P ?B" using `P {}` step IH [of ?B] by blast
urbanc@36079
  2080
    moreover 
nipkow@44890
  2081
    have "\<forall>a\<in>?B. a < Max A" using Max_ge [OF `finite A`] by fastforce
haftmann@51489
  2082
    ultimately show "P A" using A insert_Diff_single step [OF `finite ?B`] by fastforce
krauss@26748
  2083
  qed
krauss@26748
  2084
qed
krauss@26748
  2085
haftmann@51489
  2086
lemma finite_linorder_min_induct [consumes 1, case_names empty insert]:
haftmann@51489
  2087
  "\<lbrakk>finite A; P {}; \<And>b A. \<lbrakk>finite A; \<forall>a\<in>A. b < a; P A\<rbrakk> \<Longrightarrow> P (insert b A)\<rbrakk> \<Longrightarrow> P A"
haftmann@51489
  2088
  by (rule linorder.finite_linorder_max_induct [OF dual_linorder])
nipkow@32006
  2089
haftmann@22917
  2090
end
haftmann@22917
  2091
haftmann@35028
  2092
context linordered_ab_semigroup_add
haftmann@22917
  2093
begin
haftmann@22917
  2094
haftmann@22917
  2095
lemma add_Min_commute:
haftmann@22917
  2096
  fixes k
haftmann@25062
  2097
  assumes "finite N" and "N \<noteq> {}"
haftmann@25062
  2098
  shows "k + Min N = Min {k + m | m. m \<in> N}"
haftmann@25062
  2099
proof -
haftmann@25062
  2100
  have "\<And>x y. k + min x y = min (k + x) (k + y)"
haftmann@25062
  2101
    by (simp add: min_def not_le)
haftmann@25062
  2102
      (blast intro: antisym less_imp_le add_left_mono)
haftmann@25062
  2103
  with assms show ?thesis
haftmann@25062
  2104
    using hom_Min_commute [of "plus k" N]
haftmann@25062
  2105
    by simp (blast intro: arg_cong [where f = Min])
haftmann@25062
  2106
qed
haftmann@22917
  2107
haftmann@22917
  2108
lemma add_Max_commute:
haftmann@22917
  2109
  fixes k
haftmann@25062
  2110
  assumes "finite N" and "N \<noteq> {}"
haftmann@25062
  2111
  shows "k + Max N = Max {k + m | m. m \<in> N}"
haftmann@25062
  2112
proof -
haftmann@25062
  2113
  have "\<And>x y. k + max x y = max (k + x) (k + y)"
haftmann@25062
  2114
    by (simp add: max_def not_le)
haftmann@25062
  2115
      (blast intro: antisym less_imp_le add_left_mono)
haftmann@25062
  2116
  with assms show ?thesis
haftmann@25062
  2117
    using hom_Max_commute [of "plus k" N]
haftmann@25062
  2118
    by simp (blast intro: arg_cong [where f = Max])
haftmann@25062
  2119
qed
haftmann@22917
  2120
haftmann@22917
  2121
end
haftmann@22917
  2122
haftmann@35034
  2123
context linordered_ab_group_add
haftmann@35034
  2124
begin
haftmann@35034
  2125
haftmann@35034
  2126
lemma minus_Max_eq_Min [simp]:
haftmann@51489
  2127
  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - Max S = Min (uminus ` S)"
haftmann@35034
  2128
  by (induct S rule: finite_ne_induct) (simp_all add: minus_max_eq_min)
haftmann@35034
  2129
haftmann@35034
  2130
lemma minus_Min_eq_Max [simp]:
haftmann@51489
  2131
  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - Min S = Max (uminus ` S)"
haftmann@35034
  2132
  by (induct S rule: finite_ne_induct) (simp_all add: minus_min_eq_max)
haftmann@35034
  2133
haftmann@35034
  2134
end
haftmann@35034
  2135
haftmann@25571
  2136
end
haftmann@51263
  2137