src/HOL/Big_Operators.thy
author Christian Sternagel
Thu Aug 30 15:44:03 2012 +0900 (2012-08-30)
changeset 49093 fdc301f592c4
parent 48893 3db108d14239
child 49660 de49d9b4d7bc
permissions -rw-r--r--
forgot to add lemmas
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@35722
     9
imports Plain
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@35816
    17
locale comm_monoid_big = comm_monoid +
haftmann@35816
    18
  fixes F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@35816
    19
  assumes F_eq: "F g A = (if finite A then fold_image (op *) g 1 A else 1)"
haftmann@35816
    20
haftmann@35816
    21
sublocale comm_monoid_big < folding_image proof
haftmann@35816
    22
qed (simp add: F_eq)
haftmann@35816
    23
haftmann@35816
    24
context comm_monoid_big
haftmann@35816
    25
begin
haftmann@35816
    26
haftmann@35816
    27
lemma infinite [simp]:
haftmann@35816
    28
  "\<not> finite A \<Longrightarrow> F g A = 1"
haftmann@35816
    29
  by (simp add: F_eq)
haftmann@35816
    30
hoelzl@42986
    31
lemma F_cong:
hoelzl@42986
    32
  assumes "A = B" "\<And>x. x \<in> B \<Longrightarrow> h x = g x"
hoelzl@42986
    33
  shows "F h A = F g B"
hoelzl@42986
    34
proof cases
hoelzl@42986
    35
  assume "finite A"
hoelzl@42986
    36
  with assms show ?thesis unfolding `A = B` by (simp cong: cong)
hoelzl@42986
    37
next
hoelzl@42986
    38
  assume "\<not> finite A"
hoelzl@42986
    39
  then show ?thesis unfolding `A = B` by simp
hoelzl@42986
    40
qed
hoelzl@42986
    41
nipkow@48849
    42
lemma strong_F_cong [cong]:
nipkow@48849
    43
  "\<lbrakk> A = B; !!x. x:B =simp=> g x = h x \<rbrakk>
nipkow@48849
    44
   \<Longrightarrow> F (%x. g x) A = F (%x. h x) B"
nipkow@48849
    45
by (rule F_cong) (simp_all add: simp_implies_def)
nipkow@48849
    46
nipkow@48821
    47
lemma F_neutral[simp]: "F (%i. 1) A = 1"
nipkow@48821
    48
by (cases "finite A") (simp_all add: neutral)
nipkow@48821
    49
nipkow@48821
    50
lemma F_neutral': "ALL a:A. g a = 1 \<Longrightarrow> F g A = 1"
nipkow@48849
    51
by simp
nipkow@48849
    52
nipkow@48849
    53
lemma F_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow> F g A = F g (A - B) * F g B"
nipkow@48849
    54
by (metis Diff_partition union_disjoint Diff_disjoint finite_Un inf_commute sup_commute)
nipkow@48849
    55
nipkow@48849
    56
lemma F_mono_neutral_cong_left:
nipkow@48849
    57
  assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1"
nipkow@48849
    58
  and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T"
nipkow@48849
    59
proof-
nipkow@48849
    60
  have eq: "T = S \<union> (T - S)" using `S \<subseteq> T` by blast
nipkow@48849
    61
  have d: "S \<inter> (T - S) = {}" using `S \<subseteq> T` by blast
nipkow@48849
    62
  from `finite T` `S \<subseteq> T` have f: "finite S" "finite (T - S)"
nipkow@48849
    63
    by (auto intro: finite_subset)
nipkow@48849
    64
  show ?thesis using assms(4)
nipkow@48849
    65
    by (simp add: union_disjoint[OF f d, unfolded eq[symmetric]] F_neutral'[OF assms(3)])
nipkow@48849
    66
qed
nipkow@48849
    67
nipkow@48850
    68
lemma F_mono_neutral_cong_right:
nipkow@48850
    69
  "\<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
    70
   \<Longrightarrow> F g T = F h S"
nipkow@48850
    71
by(auto intro!: F_mono_neutral_cong_left[symmetric])
nipkow@48849
    72
nipkow@48849
    73
lemma F_mono_neutral_left:
nipkow@48849
    74
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g S = F g T"
nipkow@48849
    75
by(blast intro: F_mono_neutral_cong_left)
nipkow@48849
    76
nipkow@48850
    77
lemma F_mono_neutral_right:
nipkow@48850
    78
  "\<lbrakk> finite T;  S \<subseteq> T;  \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S"
nipkow@48850
    79
by(blast intro!: F_mono_neutral_left[symmetric])
nipkow@48849
    80
nipkow@48849
    81
lemma F_delta: 
nipkow@48849
    82
  assumes fS: "finite S"
nipkow@48849
    83
  shows "F (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
nipkow@48849
    84
proof-
nipkow@48849
    85
  let ?f = "(\<lambda>k. if k=a then b k else 1)"
nipkow@48849
    86
  { assume a: "a \<notin> S"
nipkow@48849
    87
    hence "\<forall>k\<in>S. ?f k = 1" by simp
nipkow@48849
    88
    hence ?thesis  using a by simp }
nipkow@48849
    89
  moreover
nipkow@48849
    90
  { assume a: "a \<in> S"
nipkow@48849
    91
    let ?A = "S - {a}"
nipkow@48849
    92
    let ?B = "{a}"
nipkow@48849
    93
    have eq: "S = ?A \<union> ?B" using a by blast 
nipkow@48849
    94
    have dj: "?A \<inter> ?B = {}" by simp
nipkow@48849
    95
    from fS have fAB: "finite ?A" "finite ?B" by auto  
nipkow@48849
    96
    have "F ?f S = F ?f ?A * F ?f ?B"
nipkow@48849
    97
      using union_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
nipkow@48849
    98
      by simp
nipkow@48849
    99
    then have ?thesis  using a by simp }
nipkow@48849
   100
  ultimately show ?thesis by blast
nipkow@48849
   101
qed
nipkow@48849
   102
nipkow@48849
   103
lemma F_delta': 
nipkow@48849
   104
  assumes fS: "finite S" shows 
nipkow@48849
   105
  "F (\<lambda>k. if a = k then b k else 1) S = (if a \<in> S then b a else 1)"
nipkow@48849
   106
using F_delta[OF fS, of a b, symmetric] by (auto intro: F_cong)
nipkow@48821
   107
nipkow@48861
   108
lemma F_fun_f: "F (%x. g x * h x) A = (F g A * F h A)"
nipkow@48861
   109
by (cases "finite A") (simp_all add: distrib)
nipkow@48861
   110
nipkow@48893
   111
nipkow@48893
   112
text {* for ad-hoc proofs for @{const fold_image} *}
nipkow@48893
   113
lemma comm_monoid_mult:  "class.comm_monoid_mult (op *) 1"
nipkow@48893
   114
proof qed (auto intro: assoc commute)
nipkow@48893
   115
nipkow@48893
   116
lemma F_Un_neutral:
nipkow@48893
   117
  assumes fS: "finite S" and fT: "finite T"
nipkow@48893
   118
  and I1: "\<forall>x \<in> S\<inter>T. g x = 1"
nipkow@48893
   119
  shows "F g (S \<union> T) = F g S  * F g T"
nipkow@48893
   120
proof -
nipkow@48893
   121
  interpret comm_monoid_mult "op *" 1 by (fact comm_monoid_mult)
nipkow@48893
   122
  show ?thesis
nipkow@48893
   123
  using fS fT
nipkow@48893
   124
  apply (simp add: F_eq)
nipkow@48893
   125
  apply (rule fold_image_Un_one)
nipkow@48893
   126
  using I1 by auto
nipkow@48893
   127
qed
nipkow@48893
   128
hoelzl@42986
   129
lemma If_cases:
hoelzl@42986
   130
  fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
hoelzl@42986
   131
  assumes fA: "finite A"
hoelzl@42986
   132
  shows "F (\<lambda>x. if P x then h x else g x) A =
hoelzl@42986
   133
         F h (A \<inter> {x. P x}) * F g (A \<inter> - {x. P x})"
hoelzl@42986
   134
proof-
hoelzl@42986
   135
  have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}" 
hoelzl@42986
   136
          "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}" 
hoelzl@42986
   137
    by blast+
hoelzl@42986
   138
  from fA 
hoelzl@42986
   139
  have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
hoelzl@42986
   140
  let ?g = "\<lambda>x. if P x then h x else g x"
hoelzl@42986
   141
  from union_disjoint[OF f a(2), of ?g] a(1)
hoelzl@42986
   142
  show ?thesis
hoelzl@42986
   143
    by (subst (1 2) F_cong) simp_all
hoelzl@42986
   144
qed
hoelzl@42986
   145
haftmann@35816
   146
end
haftmann@35816
   147
haftmann@35816
   148
text {* for ad-hoc proofs for @{const fold_image} *}
haftmann@35816
   149
haftmann@35816
   150
lemma (in comm_monoid_add) comm_monoid_mult:
haftmann@36635
   151
  "class.comm_monoid_mult (op +) 0"
haftmann@35816
   152
proof qed (auto intro: add_assoc add_commute)
haftmann@35816
   153
haftmann@35816
   154
notation times (infixl "*" 70)
haftmann@35816
   155
notation Groups.one ("1")
haftmann@35816
   156
haftmann@35816
   157
nipkow@15402
   158
subsection {* Generalized summation over a set *}
nipkow@15402
   159
haftmann@35816
   160
definition (in comm_monoid_add) setsum :: "('b \<Rightarrow> 'a) => 'b set => 'a" where
haftmann@35816
   161
  "setsum f A = (if finite A then fold_image (op +) f 0 A else 0)"
haftmann@26041
   162
haftmann@35816
   163
sublocale comm_monoid_add < setsum!: comm_monoid_big "op +" 0 setsum proof
haftmann@35816
   164
qed (fact setsum_def)
nipkow@15402
   165
wenzelm@19535
   166
abbreviation
wenzelm@21404
   167
  Setsum  ("\<Sum>_" [1000] 999) where
wenzelm@19535
   168
  "\<Sum>A == setsum (%x. x) A"
wenzelm@19535
   169
nipkow@15402
   170
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   171
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   172
nipkow@15402
   173
syntax
paulson@17189
   174
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   175
syntax (xsymbols)
paulson@17189
   176
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   177
syntax (HTML output)
paulson@17189
   178
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   179
nipkow@15402
   180
translations -- {* Beware of argument permutation! *}
nipkow@28853
   181
  "SUM i:A. b" == "CONST setsum (%i. b) A"
nipkow@28853
   182
  "\<Sum>i\<in>A. b" == "CONST setsum (%i. b) A"
nipkow@15402
   183
nipkow@15402
   184
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   185
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   186
nipkow@15402
   187
syntax
paulson@17189
   188
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   189
syntax (xsymbols)
paulson@17189
   190
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   191
syntax (HTML output)
paulson@17189
   192
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   193
nipkow@15402
   194
translations
nipkow@28853
   195
  "SUM x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@28853
   196
  "\<Sum>x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@15402
   197
nipkow@15402
   198
print_translation {*
nipkow@15402
   199
let
wenzelm@35115
   200
  fun setsum_tr' [Abs (x, Tx, t), Const (@{const_syntax Collect}, _) $ Abs (y, Ty, P)] =
wenzelm@35115
   201
        if x <> y then raise Match
wenzelm@35115
   202
        else
wenzelm@35115
   203
          let
wenzelm@42284
   204
            val x' = Syntax_Trans.mark_bound x;
wenzelm@35115
   205
            val t' = subst_bound (x', t);
wenzelm@35115
   206
            val P' = subst_bound (x', P);
wenzelm@42284
   207
          in Syntax.const @{syntax_const "_qsetsum"} $ Syntax_Trans.mark_bound x $ P' $ t' end
wenzelm@35115
   208
    | setsum_tr' _ = raise Match;
wenzelm@35115
   209
in [(@{const_syntax setsum}, setsum_tr')] end
nipkow@15402
   210
*}
nipkow@15402
   211
haftmann@35816
   212
lemma setsum_empty:
haftmann@35816
   213
  "setsum f {} = 0"
haftmann@35816
   214
  by (fact setsum.empty)
nipkow@15402
   215
haftmann@35816
   216
lemma setsum_insert:
nipkow@28853
   217
  "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
haftmann@35816
   218
  by (fact setsum.insert)
haftmann@35816
   219
haftmann@35816
   220
lemma setsum_infinite:
haftmann@35816
   221
  "~ finite A ==> setsum f A = 0"
haftmann@35816
   222
  by (fact setsum.infinite)
nipkow@15402
   223
haftmann@35816
   224
lemma (in comm_monoid_add) setsum_reindex:
haftmann@35816
   225
  assumes "inj_on f B" shows "setsum h (f ` B) = setsum (h \<circ> f) B"
haftmann@35816
   226
proof -
haftmann@35816
   227
  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
nipkow@48849
   228
  from assms show ?thesis by (auto simp add: setsum_def fold_image_reindex o_def dest!:finite_imageD)
haftmann@35816
   229
qed
paulson@15409
   230
nipkow@48849
   231
lemma setsum_reindex_id:
haftmann@35816
   232
  "inj_on f B ==> setsum f B = setsum id (f ` B)"
nipkow@48849
   233
by (simp add: setsum_reindex)
nipkow@15402
   234
nipkow@48849
   235
lemma setsum_reindex_nonzero: 
chaieb@29674
   236
  assumes fS: "finite S"
chaieb@29674
   237
  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"
chaieb@29674
   238
  shows "setsum h (f ` S) = setsum (h o f) S"
chaieb@29674
   239
using nz
chaieb@29674
   240
proof(induct rule: finite_induct[OF fS])
chaieb@29674
   241
  case 1 thus ?case by simp
chaieb@29674
   242
next
chaieb@29674
   243
  case (2 x F) 
nipkow@48849
   244
  { assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
chaieb@29674
   245
    then obtain y where y: "y \<in> F" "f x = f y" by auto 
chaieb@29674
   246
    from "2.hyps" y have xy: "x \<noteq> y" by auto
chaieb@29674
   247
    
chaieb@29674
   248
    from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
chaieb@29674
   249
    have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
chaieb@29674
   250
    also have "\<dots> = setsum (h o f) (insert x F)" 
haftmann@35816
   251
      unfolding setsum.insert[OF `finite F` `x\<notin>F`]
haftmann@35816
   252
      using h0
nipkow@48849
   253
      apply (simp cong del:setsum.strong_F_cong)
chaieb@29674
   254
      apply (rule "2.hyps"(3))
chaieb@29674
   255
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
   256
      apply simp_all
chaieb@29674
   257
      done
nipkow@48849
   258
    finally have ?case . }
chaieb@29674
   259
  moreover
nipkow@48849
   260
  { assume fxF: "f x \<notin> f ` F"
chaieb@29674
   261
    have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)" 
chaieb@29674
   262
      using fxF "2.hyps" by simp 
chaieb@29674
   263
    also have "\<dots> = setsum (h o f) (insert x F)"
haftmann@35816
   264
      unfolding setsum.insert[OF `finite F` `x\<notin>F`]
nipkow@48849
   265
      apply (simp cong del:setsum.strong_F_cong)
haftmann@35816
   266
      apply (rule cong [OF refl [of "op + (h (f x))"]])
chaieb@29674
   267
      apply (rule "2.hyps"(3))
chaieb@29674
   268
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
   269
      apply simp_all
chaieb@29674
   270
      done
nipkow@48849
   271
    finally have ?case . }
chaieb@29674
   272
  ultimately show ?case by blast
chaieb@29674
   273
qed
chaieb@29674
   274
nipkow@48849
   275
lemma setsum_cong:
nipkow@15402
   276
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
nipkow@48849
   277
by (fact setsum.F_cong)
nipkow@15402
   278
nipkow@48849
   279
lemma strong_setsum_cong:
nipkow@16733
   280
  "A = B ==> (!!x. x:B =simp=> f x = g x)
nipkow@16733
   281
   ==> setsum (%x. f x) A = setsum (%x. g x) B"
nipkow@48849
   282
by (fact setsum.strong_F_cong)
berghofe@16632
   283
nipkow@48849
   284
lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A"
nipkow@48849
   285
by (auto intro: setsum_cong)
nipkow@15554
   286
nipkow@48849
   287
lemma setsum_reindex_cong:
nipkow@28853
   288
   "[|inj_on f A; B = f ` A; !!a. a:A \<Longrightarrow> g a = h (f a)|] 
nipkow@28853
   289
    ==> setsum h B = setsum g A"
nipkow@48849
   290
by (simp add: setsum_reindex)
nipkow@15402
   291
nipkow@48821
   292
lemmas setsum_0 = setsum.F_neutral
nipkow@48821
   293
lemmas setsum_0' = setsum.F_neutral'
nipkow@15402
   294
nipkow@48849
   295
lemma setsum_Un_Int: "finite A ==> finite B ==>
nipkow@15402
   296
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
nipkow@15402
   297
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
nipkow@48849
   298
by (fact setsum.union_inter)
nipkow@15402
   299
nipkow@48849
   300
lemma setsum_Un_disjoint: "finite A ==> finite B
nipkow@15402
   301
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
nipkow@48849
   302
by (fact setsum.union_disjoint)
nipkow@48849
   303
nipkow@48849
   304
lemma setsum_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
nipkow@48849
   305
    setsum f A = setsum f (A - B) + setsum f B"
nipkow@48849
   306
by(fact setsum.F_subset_diff)
nipkow@15402
   307
chaieb@29674
   308
lemma setsum_mono_zero_left: 
nipkow@48849
   309
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 0 \<rbrakk> \<Longrightarrow> setsum f S = setsum f T"
nipkow@48849
   310
by(fact setsum.F_mono_neutral_left)
chaieb@29674
   311
nipkow@48849
   312
lemmas setsum_mono_zero_right = setsum.F_mono_neutral_right
chaieb@29674
   313
chaieb@29674
   314
lemma setsum_mono_zero_cong_left: 
nipkow@48849
   315
  "\<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>
nipkow@48849
   316
  \<Longrightarrow> setsum f S = setsum g T"
nipkow@48849
   317
by(fact setsum.F_mono_neutral_cong_left)
chaieb@29674
   318
nipkow@48849
   319
lemmas setsum_mono_zero_cong_right = setsum.F_mono_neutral_cong_right
chaieb@29674
   320
nipkow@48849
   321
lemma setsum_delta: "finite S \<Longrightarrow>
nipkow@48849
   322
  setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
nipkow@48849
   323
by(fact setsum.F_delta)
nipkow@48849
   324
nipkow@48849
   325
lemma setsum_delta': "finite S \<Longrightarrow>
nipkow@48849
   326
  setsum (\<lambda>k. if a = k then b k else 0) S = (if a\<in> S then b a else 0)"
nipkow@48849
   327
by(fact setsum.F_delta')
chaieb@29674
   328
chaieb@30260
   329
lemma setsum_restrict_set:
chaieb@30260
   330
  assumes fA: "finite A"
chaieb@30260
   331
  shows "setsum f (A \<inter> B) = setsum (\<lambda>x. if x \<in> B then f x else 0) A"
chaieb@30260
   332
proof-
chaieb@30260
   333
  from fA have fab: "finite (A \<inter> B)" by auto
chaieb@30260
   334
  have aba: "A \<inter> B \<subseteq> A" by blast
chaieb@30260
   335
  let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
chaieb@30260
   336
  from setsum_mono_zero_left[OF fA aba, of ?g]
chaieb@30260
   337
  show ?thesis by simp
chaieb@30260
   338
qed
chaieb@30260
   339
chaieb@30260
   340
lemma setsum_cases:
chaieb@30260
   341
  assumes fA: "finite A"
hoelzl@35577
   342
  shows "setsum (\<lambda>x. if P x then f x else g x) A =
hoelzl@35577
   343
         setsum f (A \<inter> {x. P x}) + setsum g (A \<inter> - {x. P x})"
hoelzl@42986
   344
  using setsum.If_cases[OF fA] .
chaieb@29674
   345
paulson@15409
   346
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
paulson@15409
   347
  the lhs need not be, since UNION I A could still be finite.*)
haftmann@35816
   348
lemma (in comm_monoid_add) setsum_UN_disjoint:
haftmann@35816
   349
  assumes "finite I" and "ALL i:I. finite (A i)"
haftmann@35816
   350
    and "ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}"
haftmann@35816
   351
  shows "setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
haftmann@35816
   352
proof -
haftmann@35816
   353
  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
wenzelm@41550
   354
  from assms show ?thesis by (simp add: setsum_def fold_image_UN_disjoint)
haftmann@35816
   355
qed
nipkow@15402
   356
paulson@15409
   357
text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
paulson@15409
   358
directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
nipkow@15402
   359
lemma setsum_Union_disjoint:
hoelzl@44937
   360
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}"
hoelzl@44937
   361
  shows "setsum f (Union C) = setsum (setsum f) C"
hoelzl@44937
   362
proof cases
hoelzl@44937
   363
  assume "finite C"
hoelzl@44937
   364
  from setsum_UN_disjoint[OF this assms]
hoelzl@44937
   365
  show ?thesis
hoelzl@44937
   366
    by (simp add: SUP_def)
hoelzl@44937
   367
qed (force dest: finite_UnionD simp add: setsum_def)
nipkow@15402
   368
paulson@15409
   369
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
paulson@15409
   370
  the rhs need not be, since SIGMA A B could still be finite.*)
haftmann@35816
   371
lemma (in comm_monoid_add) setsum_Sigma:
haftmann@35816
   372
  assumes "finite A" and  "ALL x:A. finite (B x)"
haftmann@35816
   373
  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@35816
   374
proof -
haftmann@35816
   375
  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
wenzelm@41550
   376
  from assms show ?thesis by (simp add: setsum_def fold_image_Sigma split_def)
haftmann@35816
   377
qed
nipkow@15402
   378
paulson@15409
   379
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
   380
lemma setsum_cartesian_product: 
paulson@17189
   381
   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
paulson@15409
   382
apply (cases "finite A") 
paulson@15409
   383
 apply (cases "finite B") 
paulson@15409
   384
  apply (simp add: setsum_Sigma)
paulson@15409
   385
 apply (cases "A={}", simp)
nipkow@15543
   386
 apply (simp) 
paulson@15409
   387
apply (auto simp add: setsum_def
paulson@15409
   388
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
   389
done
nipkow@15402
   390
nipkow@48861
   391
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
nipkow@48861
   392
by (fact setsum.F_fun_f)
nipkow@15402
   393
nipkow@48893
   394
lemma setsum_Un_zero:  
nipkow@48893
   395
  "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow>
nipkow@48893
   396
  setsum f (S \<union> T) = setsum f S + setsum f T"
nipkow@48893
   397
by(fact setsum.F_Un_neutral)
nipkow@48893
   398
nipkow@48893
   399
lemma setsum_UNION_zero: 
nipkow@48893
   400
  assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
nipkow@48893
   401
  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
   402
  shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
nipkow@48893
   403
  using fSS f0
nipkow@48893
   404
proof(induct rule: finite_induct[OF fS])
nipkow@48893
   405
  case 1 thus ?case by simp
nipkow@48893
   406
next
nipkow@48893
   407
  case (2 T F)
nipkow@48893
   408
  then have fTF: "finite T" "\<forall>T\<in>F. finite T" "finite F" and TF: "T \<notin> F" 
nipkow@48893
   409
    and H: "setsum f (\<Union> F) = setsum (setsum f) F" by auto
nipkow@48893
   410
  from fTF have fUF: "finite (\<Union>F)" by auto
nipkow@48893
   411
  from "2.prems" TF fTF
nipkow@48893
   412
  show ?case 
nipkow@48893
   413
    by (auto simp add: H[symmetric] intro: setsum_Un_zero[OF fTF(1) fUF, of f])
nipkow@48893
   414
qed
nipkow@48893
   415
nipkow@15402
   416
nipkow@15402
   417
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
   418
nipkow@15402
   419
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
nipkow@28853
   420
apply (case_tac "finite A")
nipkow@28853
   421
 prefer 2 apply (simp add: setsum_def)
nipkow@28853
   422
apply (erule rev_mp)
nipkow@28853
   423
apply (erule finite_induct, auto)
nipkow@28853
   424
done
nipkow@15402
   425
nipkow@15402
   426
lemma setsum_eq_0_iff [simp]:
nipkow@15402
   427
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
nipkow@28853
   428
by (induct set: finite) auto
nipkow@15402
   429
nipkow@30859
   430
lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
nipkow@30859
   431
  (setsum f A = Suc 0) = (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
nipkow@30859
   432
apply(erule finite_induct)
nipkow@30859
   433
apply (auto simp add:add_is_1)
nipkow@30859
   434
done
nipkow@30859
   435
nipkow@30859
   436
lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
nipkow@30859
   437
nipkow@15402
   438
lemma setsum_Un_nat: "finite A ==> finite B ==>
nipkow@28853
   439
  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
   440
  -- {* For the natural numbers, we have subtraction. *}
nipkow@29667
   441
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
nipkow@15402
   442
nipkow@15402
   443
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@28853
   444
  (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@28853
   445
   setsum f A + setsum f B - setsum f (A Int B)"
nipkow@29667
   446
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
nipkow@15402
   447
haftmann@35816
   448
lemma (in comm_monoid_add) setsum_eq_general_reverses:
chaieb@30260
   449
  assumes fS: "finite S" and fT: "finite T"
chaieb@30260
   450
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
chaieb@30260
   451
  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x"
chaieb@30260
   452
  shows "setsum f S = setsum g T"
haftmann@35816
   453
proof -
haftmann@35816
   454
  interpret comm_monoid_mult "op +" 0 by (fact comm_monoid_mult)
haftmann@35816
   455
  show ?thesis
chaieb@30260
   456
  apply (simp add: setsum_def fS fT)
haftmann@35816
   457
  apply (rule fold_image_eq_general_inverses)
haftmann@35816
   458
  apply (rule fS)
chaieb@30260
   459
  apply (erule kh)
chaieb@30260
   460
  apply (erule hk)
chaieb@30260
   461
  done
haftmann@35816
   462
qed
chaieb@30260
   463
nipkow@15402
   464
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
nipkow@28853
   465
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@28853
   466
apply (case_tac "finite A")
nipkow@28853
   467
 prefer 2 apply (simp add: setsum_def)
nipkow@28853
   468
apply (erule finite_induct)
nipkow@28853
   469
 apply (auto simp add: insert_Diff_if)
nipkow@28853
   470
apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@28853
   471
done
nipkow@15402
   472
nipkow@15402
   473
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
   474
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
   475
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@28853
   476
by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@28853
   477
nipkow@28853
   478
lemma setsum_diff1'[rule_format]:
nipkow@28853
   479
  "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
nipkow@28853
   480
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))"])
nipkow@28853
   481
apply (auto simp add: insert_Diff_if add_ac)
nipkow@28853
   482
done
obua@15552
   483
nipkow@31438
   484
lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
nipkow@31438
   485
  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
nipkow@31438
   486
unfolding setsum_diff1'[OF assms] by auto
nipkow@31438
   487
nipkow@15402
   488
(* By Jeremy Siek: *)
nipkow@15402
   489
nipkow@15402
   490
lemma setsum_diff_nat: 
nipkow@28853
   491
assumes "finite B" and "B \<subseteq> A"
nipkow@28853
   492
shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
nipkow@28853
   493
using assms
wenzelm@19535
   494
proof induct
nipkow@15402
   495
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15402
   496
next
nipkow@15402
   497
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15402
   498
    and xFinA: "insert x F \<subseteq> A"
nipkow@15402
   499
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15402
   500
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15402
   501
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15402
   502
    by (simp add: setsum_diff1_nat)
nipkow@15402
   503
  from xFinA have "F \<subseteq> A" by simp
nipkow@15402
   504
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15402
   505
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15402
   506
    by simp
nipkow@15402
   507
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15402
   508
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15402
   509
    by simp
nipkow@15402
   510
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15402
   511
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15402
   512
    by simp
nipkow@15402
   513
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15402
   514
qed
nipkow@15402
   515
nipkow@15402
   516
lemma setsum_diff:
nipkow@15402
   517
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
   518
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
   519
proof -
nipkow@15402
   520
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
   521
  show ?thesis using finiteB le
wenzelm@21575
   522
  proof induct
wenzelm@19535
   523
    case empty
wenzelm@19535
   524
    thus ?case by auto
wenzelm@19535
   525
  next
wenzelm@19535
   526
    case (insert x F)
wenzelm@19535
   527
    thus ?case using le finiteB 
wenzelm@19535
   528
      by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
   529
  qed
wenzelm@19535
   530
qed
nipkow@15402
   531
nipkow@15402
   532
lemma setsum_mono:
haftmann@35028
   533
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, ordered_ab_semigroup_add}))"
nipkow@15402
   534
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
   535
proof (cases "finite K")
nipkow@15402
   536
  case True
nipkow@15402
   537
  thus ?thesis using le
wenzelm@19535
   538
  proof induct
nipkow@15402
   539
    case empty
nipkow@15402
   540
    thus ?case by simp
nipkow@15402
   541
  next
nipkow@15402
   542
    case insert
nipkow@44890
   543
    thus ?case using add_mono by fastforce
nipkow@15402
   544
  qed
nipkow@15402
   545
next
nipkow@15402
   546
  case False
nipkow@15402
   547
  thus ?thesis
nipkow@15402
   548
    by (simp add: setsum_def)
nipkow@15402
   549
qed
nipkow@15402
   550
nipkow@15554
   551
lemma setsum_strict_mono:
haftmann@35028
   552
  fixes f :: "'a \<Rightarrow> 'b::{ordered_cancel_ab_semigroup_add,comm_monoid_add}"
wenzelm@19535
   553
  assumes "finite A"  "A \<noteq> {}"
wenzelm@19535
   554
    and "!!x. x:A \<Longrightarrow> f x < g x"
wenzelm@19535
   555
  shows "setsum f A < setsum g A"
wenzelm@41550
   556
  using assms
nipkow@15554
   557
proof (induct rule: finite_ne_induct)
nipkow@15554
   558
  case singleton thus ?case by simp
nipkow@15554
   559
next
nipkow@15554
   560
  case insert thus ?case by (auto simp: add_strict_mono)
nipkow@15554
   561
qed
nipkow@15554
   562
nipkow@46699
   563
lemma setsum_strict_mono_ex1:
nipkow@46699
   564
fixes f :: "'a \<Rightarrow> 'b::{comm_monoid_add, ordered_cancel_ab_semigroup_add}"
nipkow@46699
   565
assumes "finite A" and "ALL x:A. f x \<le> g x" and "EX a:A. f a < g a"
nipkow@46699
   566
shows "setsum f A < setsum g A"
nipkow@46699
   567
proof-
nipkow@46699
   568
  from assms(3) obtain a where a: "a:A" "f a < g a" by blast
nipkow@46699
   569
  have "setsum f A = setsum f ((A-{a}) \<union> {a})"
nipkow@46699
   570
    by(simp add:insert_absorb[OF `a:A`])
nipkow@46699
   571
  also have "\<dots> = setsum f (A-{a}) + setsum f {a}"
nipkow@46699
   572
    using `finite A` by(subst setsum_Un_disjoint) auto
nipkow@46699
   573
  also have "setsum f (A-{a}) \<le> setsum g (A-{a})"
nipkow@46699
   574
    by(rule setsum_mono)(simp add: assms(2))
nipkow@46699
   575
  also have "setsum f {a} < setsum g {a}" using a by simp
nipkow@46699
   576
  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) \<union> {a})"
nipkow@46699
   577
    using `finite A` by(subst setsum_Un_disjoint[symmetric]) auto
nipkow@46699
   578
  also have "\<dots> = setsum g A" by(simp add:insert_absorb[OF `a:A`])
nipkow@46699
   579
  finally show ?thesis by (metis add_right_mono add_strict_left_mono)
nipkow@46699
   580
qed
nipkow@46699
   581
nipkow@15535
   582
lemma setsum_negf:
wenzelm@19535
   583
  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
nipkow@15535
   584
proof (cases "finite A")
berghofe@22262
   585
  case True thus ?thesis by (induct set: finite) auto
nipkow@15535
   586
next
nipkow@15535
   587
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
   588
qed
nipkow@15402
   589
nipkow@15535
   590
lemma setsum_subtractf:
wenzelm@19535
   591
  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
wenzelm@19535
   592
    setsum f A - setsum g A"
nipkow@15535
   593
proof (cases "finite A")
nipkow@15535
   594
  case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15535
   595
next
nipkow@15535
   596
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
   597
qed
nipkow@15402
   598
nipkow@15535
   599
lemma setsum_nonneg:
haftmann@35028
   600
  assumes nn: "\<forall>x\<in>A. (0::'a::{ordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
wenzelm@19535
   601
  shows "0 \<le> setsum f A"
nipkow@15535
   602
proof (cases "finite A")
nipkow@15535
   603
  case True thus ?thesis using nn
wenzelm@21575
   604
  proof induct
wenzelm@19535
   605
    case empty then show ?case by simp
wenzelm@19535
   606
  next
wenzelm@19535
   607
    case (insert x F)
wenzelm@19535
   608
    then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
wenzelm@19535
   609
    with insert show ?case by simp
wenzelm@19535
   610
  qed
nipkow@15535
   611
next
nipkow@15535
   612
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
   613
qed
nipkow@15402
   614
nipkow@15535
   615
lemma setsum_nonpos:
haftmann@35028
   616
  assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{ordered_ab_semigroup_add,comm_monoid_add})"
wenzelm@19535
   617
  shows "setsum f A \<le> 0"
nipkow@15535
   618
proof (cases "finite A")
nipkow@15535
   619
  case True thus ?thesis using np
wenzelm@21575
   620
  proof induct
wenzelm@19535
   621
    case empty then show ?case by simp
wenzelm@19535
   622
  next
wenzelm@19535
   623
    case (insert x F)
wenzelm@19535
   624
    then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
wenzelm@19535
   625
    with insert show ?case by simp
wenzelm@19535
   626
  qed
nipkow@15535
   627
next
nipkow@15535
   628
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
   629
qed
nipkow@15402
   630
hoelzl@36622
   631
lemma setsum_nonneg_leq_bound:
hoelzl@36622
   632
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
hoelzl@36622
   633
  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
   634
  shows "f i \<le> B"
hoelzl@36622
   635
proof -
hoelzl@36622
   636
  have "0 \<le> (\<Sum> i \<in> s - {i}. f i)" and "0 \<le> f i"
hoelzl@36622
   637
    using assms by (auto intro!: setsum_nonneg)
hoelzl@36622
   638
  moreover
hoelzl@36622
   639
  have "(\<Sum> i \<in> s - {i}. f i) + f i = B"
hoelzl@36622
   640
    using assms by (simp add: setsum_diff1)
hoelzl@36622
   641
  ultimately show ?thesis by auto
hoelzl@36622
   642
qed
hoelzl@36622
   643
hoelzl@36622
   644
lemma setsum_nonneg_0:
hoelzl@36622
   645
  fixes f :: "'a \<Rightarrow> 'b::{ordered_ab_group_add}"
hoelzl@36622
   646
  assumes "finite s" and pos: "\<And> i. i \<in> s \<Longrightarrow> f i \<ge> 0"
hoelzl@36622
   647
  and "(\<Sum> i \<in> s. f i) = 0" and i: "i \<in> s"
hoelzl@36622
   648
  shows "f i = 0"
hoelzl@36622
   649
  using setsum_nonneg_leq_bound[OF assms] pos[OF i] by auto
hoelzl@36622
   650
nipkow@15539
   651
lemma setsum_mono2:
haftmann@36303
   652
fixes f :: "'a \<Rightarrow> 'b :: ordered_comm_monoid_add"
nipkow@15539
   653
assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
nipkow@15539
   654
shows "setsum f A \<le> setsum f B"
nipkow@15539
   655
proof -
nipkow@15539
   656
  have "setsum f A \<le> setsum f A + setsum f (B-A)"
nipkow@15539
   657
    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
nipkow@15539
   658
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15539
   659
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15539
   660
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15539
   661
  finally show ?thesis .
nipkow@15539
   662
qed
nipkow@15542
   663
avigad@16775
   664
lemma setsum_mono3: "finite B ==> A <= B ==> 
avigad@16775
   665
    ALL x: B - A. 
haftmann@35028
   666
      0 <= ((f x)::'a::{comm_monoid_add,ordered_ab_semigroup_add}) ==>
avigad@16775
   667
        setsum f A <= setsum f B"
avigad@16775
   668
  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")
avigad@16775
   669
  apply (erule ssubst)
avigad@16775
   670
  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")
avigad@16775
   671
  apply simp
avigad@16775
   672
  apply (rule add_left_mono)
avigad@16775
   673
  apply (erule setsum_nonneg)
avigad@16775
   674
  apply (subst setsum_Un_disjoint [THEN sym])
avigad@16775
   675
  apply (erule finite_subset, assumption)
avigad@16775
   676
  apply (rule finite_subset)
avigad@16775
   677
  prefer 2
avigad@16775
   678
  apply assumption
haftmann@32698
   679
  apply (auto simp add: sup_absorb2)
avigad@16775
   680
done
avigad@16775
   681
ballarin@19279
   682
lemma setsum_right_distrib: 
huffman@22934
   683
  fixes f :: "'a => ('b::semiring_0)"
nipkow@15402
   684
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
   685
proof (cases "finite A")
nipkow@15402
   686
  case True
nipkow@15402
   687
  thus ?thesis
wenzelm@21575
   688
  proof induct
nipkow@15402
   689
    case empty thus ?case by simp
nipkow@15402
   690
  next
nipkow@15402
   691
    case (insert x A) thus ?case by (simp add: right_distrib)
nipkow@15402
   692
  qed
nipkow@15402
   693
next
nipkow@15402
   694
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
   695
qed
nipkow@15402
   696
ballarin@17149
   697
lemma setsum_left_distrib:
huffman@22934
   698
  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
ballarin@17149
   699
proof (cases "finite A")
ballarin@17149
   700
  case True
ballarin@17149
   701
  then show ?thesis
ballarin@17149
   702
  proof induct
ballarin@17149
   703
    case empty thus ?case by simp
ballarin@17149
   704
  next
ballarin@17149
   705
    case (insert x A) thus ?case by (simp add: left_distrib)
ballarin@17149
   706
  qed
ballarin@17149
   707
next
ballarin@17149
   708
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
   709
qed
ballarin@17149
   710
ballarin@17149
   711
lemma setsum_divide_distrib:
ballarin@17149
   712
  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
ballarin@17149
   713
proof (cases "finite A")
ballarin@17149
   714
  case True
ballarin@17149
   715
  then show ?thesis
ballarin@17149
   716
  proof induct
ballarin@17149
   717
    case empty thus ?case by simp
ballarin@17149
   718
  next
ballarin@17149
   719
    case (insert x A) thus ?case by (simp add: add_divide_distrib)
ballarin@17149
   720
  qed
ballarin@17149
   721
next
ballarin@17149
   722
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
   723
qed
ballarin@17149
   724
nipkow@15535
   725
lemma setsum_abs[iff]: 
haftmann@35028
   726
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15402
   727
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15535
   728
proof (cases "finite A")
nipkow@15535
   729
  case True
nipkow@15535
   730
  thus ?thesis
wenzelm@21575
   731
  proof induct
nipkow@15535
   732
    case empty thus ?case by simp
nipkow@15535
   733
  next
nipkow@15535
   734
    case (insert x A)
nipkow@15535
   735
    thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15535
   736
  qed
nipkow@15402
   737
next
nipkow@15535
   738
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
   739
qed
nipkow@15402
   740
nipkow@15535
   741
lemma setsum_abs_ge_zero[iff]: 
haftmann@35028
   742
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15402
   743
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15535
   744
proof (cases "finite A")
nipkow@15535
   745
  case True
nipkow@15535
   746
  thus ?thesis
wenzelm@21575
   747
  proof induct
nipkow@15535
   748
    case empty thus ?case by simp
nipkow@15535
   749
  next
huffman@36977
   750
    case (insert x A) thus ?case by auto
nipkow@15535
   751
  qed
nipkow@15402
   752
next
nipkow@15535
   753
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
   754
qed
nipkow@15402
   755
nipkow@15539
   756
lemma abs_setsum_abs[simp]: 
haftmann@35028
   757
  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
nipkow@15539
   758
  shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
nipkow@15539
   759
proof (cases "finite A")
nipkow@15539
   760
  case True
nipkow@15539
   761
  thus ?thesis
wenzelm@21575
   762
  proof induct
nipkow@15539
   763
    case empty thus ?case by simp
nipkow@15539
   764
  next
nipkow@15539
   765
    case (insert a A)
nipkow@15539
   766
    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
   767
    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
   768
    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
avigad@16775
   769
      by (simp del: abs_of_nonneg)
nipkow@15539
   770
    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
nipkow@15539
   771
    finally show ?case .
nipkow@15539
   772
  qed
nipkow@15539
   773
next
nipkow@15539
   774
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15539
   775
qed
nipkow@15539
   776
nipkow@31080
   777
lemma setsum_Plus:
nipkow@31080
   778
  fixes A :: "'a set" and B :: "'b set"
nipkow@31080
   779
  assumes fin: "finite A" "finite B"
nipkow@31080
   780
  shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B"
nipkow@31080
   781
proof -
nipkow@31080
   782
  have "A <+> B = Inl ` A \<union> Inr ` B" by auto
nipkow@31080
   783
  moreover from fin have "finite (Inl ` A :: ('a + 'b) set)" "finite (Inr ` B :: ('a + 'b) set)"
nipkow@40786
   784
    by auto
nipkow@31080
   785
  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('a + 'b) set)" by auto
nipkow@31080
   786
  moreover have "inj_on (Inl :: 'a \<Rightarrow> 'a + 'b) A" "inj_on (Inr :: 'b \<Rightarrow> 'a + 'b) B" by(auto intro: inj_onI)
nipkow@31080
   787
  ultimately show ?thesis using fin by(simp add: setsum_Un_disjoint setsum_reindex)
nipkow@31080
   788
qed
nipkow@31080
   789
nipkow@31080
   790
ballarin@17149
   791
text {* Commuting outer and inner summation *}
ballarin@17149
   792
ballarin@17149
   793
lemma setsum_commute:
ballarin@17149
   794
  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
ballarin@17149
   795
proof (simp add: setsum_cartesian_product)
paulson@17189
   796
  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
paulson@17189
   797
    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
ballarin@17149
   798
    (is "?s = _")
ballarin@17149
   799
    apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on)
ballarin@17149
   800
    apply (simp add: split_def)
ballarin@17149
   801
    done
paulson@17189
   802
  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
ballarin@17149
   803
    (is "_ = ?t")
ballarin@17149
   804
    apply (simp add: swap_product)
ballarin@17149
   805
    done
ballarin@17149
   806
  finally show "?s = ?t" .
ballarin@17149
   807
qed
ballarin@17149
   808
ballarin@19279
   809
lemma setsum_product:
huffman@22934
   810
  fixes f :: "'a => ('b::semiring_0)"
ballarin@19279
   811
  shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
ballarin@19279
   812
  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute)
ballarin@19279
   813
nipkow@34223
   814
lemma setsum_mult_setsum_if_inj:
nipkow@34223
   815
fixes f :: "'a => ('b::semiring_0)"
nipkow@34223
   816
shows "inj_on (%(a,b). f a * g b) (A \<times> B) ==>
nipkow@34223
   817
  setsum f A * setsum g B = setsum id {f a * g b|a b. a:A & b:B}"
nipkow@34223
   818
by(auto simp: setsum_product setsum_cartesian_product
nipkow@34223
   819
        intro!:  setsum_reindex_cong[symmetric])
nipkow@34223
   820
haftmann@35722
   821
lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat(card A) * y"
haftmann@35722
   822
apply (cases "finite A")
haftmann@35722
   823
apply (erule finite_induct)
haftmann@35722
   824
apply (auto simp add: algebra_simps)
haftmann@35722
   825
done
haftmann@35722
   826
haftmann@35722
   827
lemma setsum_bounded:
haftmann@35722
   828
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, ordered_ab_semigroup_add})"
haftmann@35722
   829
  shows "setsum f A \<le> of_nat(card A) * K"
haftmann@35722
   830
proof (cases "finite A")
haftmann@35722
   831
  case True
haftmann@35722
   832
  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
haftmann@35722
   833
next
haftmann@35722
   834
  case False thus ?thesis by (simp add: setsum_def)
haftmann@35722
   835
qed
haftmann@35722
   836
haftmann@35722
   837
haftmann@35722
   838
subsubsection {* Cardinality as special case of @{const setsum} *}
haftmann@35722
   839
haftmann@35722
   840
lemma card_eq_setsum:
haftmann@35722
   841
  "card A = setsum (\<lambda>x. 1) A"
haftmann@35722
   842
  by (simp only: card_def setsum_def)
haftmann@35722
   843
haftmann@35722
   844
lemma card_UN_disjoint:
haftmann@46629
   845
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@46629
   846
    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@46629
   847
  shows "card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
haftmann@46629
   848
proof -
haftmann@46629
   849
  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)" by simp
haftmann@46629
   850
  with assms show ?thesis by (simp add: card_eq_setsum setsum_UN_disjoint del: setsum_constant)
haftmann@46629
   851
qed
haftmann@35722
   852
haftmann@35722
   853
lemma card_Union_disjoint:
haftmann@35722
   854
  "finite C ==> (ALL A:C. finite A) ==>
haftmann@35722
   855
   (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {})
haftmann@35722
   856
   ==> card (Union C) = setsum card C"
haftmann@35722
   857
apply (frule card_UN_disjoint [of C id])
hoelzl@44937
   858
apply (simp_all add: SUP_def id_def)
haftmann@35722
   859
done
haftmann@35722
   860
haftmann@35722
   861
text{*The image of a finite set can be expressed using @{term fold_image}.*}
haftmann@35722
   862
lemma image_eq_fold_image:
haftmann@35722
   863
  "finite A ==> f ` A = fold_image (op Un) (%x. {f x}) {} A"
haftmann@35722
   864
proof (induct rule: finite_induct)
haftmann@35722
   865
  case empty then show ?case by simp
haftmann@35722
   866
next
haftmann@35722
   867
  interpret ab_semigroup_mult "op Un"
haftmann@35722
   868
    proof qed auto
haftmann@35722
   869
  case insert 
haftmann@35722
   870
  then show ?case by simp
haftmann@35722
   871
qed
haftmann@35722
   872
haftmann@35722
   873
subsubsection {* Cardinality of products *}
haftmann@35722
   874
haftmann@35722
   875
lemma card_SigmaI [simp]:
haftmann@35722
   876
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
haftmann@35722
   877
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
haftmann@35722
   878
by(simp add: card_eq_setsum setsum_Sigma del:setsum_constant)
haftmann@35722
   879
haftmann@35722
   880
(*
haftmann@35722
   881
lemma SigmaI_insert: "y \<notin> A ==>
haftmann@35722
   882
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
haftmann@35722
   883
  by auto
haftmann@35722
   884
*)
haftmann@35722
   885
haftmann@35722
   886
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
haftmann@35722
   887
  by (cases "finite A \<and> finite B")
haftmann@35722
   888
    (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)
haftmann@35722
   889
haftmann@35722
   890
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
haftmann@35722
   891
by (simp add: card_cartesian_product)
haftmann@35722
   892
ballarin@17149
   893
nipkow@15402
   894
subsection {* Generalized product over a set *}
nipkow@15402
   895
haftmann@35816
   896
definition (in comm_monoid_mult) setprod :: "('b \<Rightarrow> 'a) => 'b set => 'a" where
haftmann@35816
   897
  "setprod f A = (if finite A then fold_image (op *) f 1 A else 1)"
haftmann@35816
   898
huffman@35938
   899
sublocale comm_monoid_mult < setprod!: comm_monoid_big "op *" 1 setprod proof
haftmann@35816
   900
qed (fact setprod_def)
nipkow@15402
   901
wenzelm@19535
   902
abbreviation
wenzelm@21404
   903
  Setprod  ("\<Prod>_" [1000] 999) where
wenzelm@19535
   904
  "\<Prod>A == setprod (%x. x) A"
wenzelm@19535
   905
nipkow@15402
   906
syntax
paulson@17189
   907
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
nipkow@15402
   908
syntax (xsymbols)
paulson@17189
   909
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   910
syntax (HTML output)
paulson@17189
   911
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@16550
   912
nipkow@16550
   913
translations -- {* Beware of argument permutation! *}
nipkow@28853
   914
  "PROD i:A. b" == "CONST setprod (%i. b) A" 
nipkow@28853
   915
  "\<Prod>i\<in>A. b" == "CONST setprod (%i. b) A" 
nipkow@16550
   916
nipkow@16550
   917
text{* Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter
nipkow@16550
   918
 @{text"\<Prod>x|P. e"}. *}
nipkow@16550
   919
nipkow@16550
   920
syntax
paulson@17189
   921
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)
nipkow@16550
   922
syntax (xsymbols)
paulson@17189
   923
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
   924
syntax (HTML output)
paulson@17189
   925
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
   926
nipkow@15402
   927
translations
nipkow@28853
   928
  "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@28853
   929
  "\<Prod>x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@16550
   930
haftmann@35816
   931
lemma setprod_empty: "setprod f {} = 1"
haftmann@35816
   932
  by (fact setprod.empty)
nipkow@15402
   933
haftmann@35816
   934
lemma setprod_insert: "[| finite A; a \<notin> A |] ==>
nipkow@15402
   935
    setprod f (insert a A) = f a * setprod f A"
haftmann@35816
   936
  by (fact setprod.insert)
nipkow@15402
   937
haftmann@35816
   938
lemma setprod_infinite: "~ finite A ==> setprod f A = 1"
haftmann@35816
   939
  by (fact setprod.infinite)
paulson@15409
   940
nipkow@15402
   941
lemma setprod_reindex:
nipkow@28853
   942
   "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
nipkow@48849
   943
by(auto simp: setprod_def fold_image_reindex o_def dest!:finite_imageD)
nipkow@15402
   944
nipkow@15402
   945
lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
nipkow@15402
   946
by (auto simp add: setprod_reindex)
nipkow@15402
   947
nipkow@15402
   948
lemma setprod_cong:
nipkow@15402
   949
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
nipkow@48849
   950
by(fact setprod.F_cong)
nipkow@15402
   951
nipkow@48849
   952
lemma strong_setprod_cong:
berghofe@16632
   953
  "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"
nipkow@48849
   954
by(fact setprod.strong_F_cong)
berghofe@16632
   955
nipkow@15402
   956
lemma setprod_reindex_cong: "inj_on f A ==>
nipkow@15402
   957
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
nipkow@28853
   958
by (frule setprod_reindex, simp)
nipkow@15402
   959
chaieb@29674
   960
lemma strong_setprod_reindex_cong: assumes i: "inj_on f A"
chaieb@29674
   961
  and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x"
chaieb@29674
   962
  shows "setprod h B = setprod g A"
chaieb@29674
   963
proof-
chaieb@29674
   964
    have "setprod h B = setprod (h o f) A"
chaieb@29674
   965
      by (simp add: B setprod_reindex[OF i, of h])
chaieb@29674
   966
    then show ?thesis apply simp
chaieb@29674
   967
      apply (rule setprod_cong)
chaieb@29674
   968
      apply simp
nipkow@30837
   969
      by (simp add: eq)
chaieb@29674
   970
qed
chaieb@29674
   971
nipkow@48893
   972
lemma setprod_Un_one: "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 1 \<rbrakk>
nipkow@48893
   973
  \<Longrightarrow> setprod f (S \<union> T) = setprod f S  * setprod f T"
nipkow@48893
   974
by(fact setprod.F_Un_neutral)
nipkow@15402
   975
nipkow@48821
   976
lemmas setprod_1 = setprod.F_neutral
nipkow@48821
   977
lemmas setprod_1' = setprod.F_neutral'
nipkow@15402
   978
nipkow@15402
   979
nipkow@15402
   980
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
   981
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
nipkow@48849
   982
by (fact setprod.union_inter)
nipkow@15402
   983
nipkow@15402
   984
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
   985
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
nipkow@48849
   986
by (fact setprod.union_disjoint)
nipkow@48849
   987
nipkow@48849
   988
lemma setprod_subset_diff: "\<lbrakk> B \<subseteq> A; finite A \<rbrakk> \<Longrightarrow>
nipkow@48849
   989
    setprod f A = setprod f (A - B) * setprod f B"
nipkow@48849
   990
by(fact setprod.F_subset_diff)
nipkow@15402
   991
nipkow@48849
   992
lemma setprod_mono_one_left:
nipkow@48849
   993
  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. f i = 1 \<rbrakk> \<Longrightarrow> setprod f S = setprod f T"
nipkow@48849
   994
by(fact setprod.F_mono_neutral_left)
nipkow@30837
   995
nipkow@48849
   996
lemmas setprod_mono_one_right = setprod.F_mono_neutral_right
nipkow@30837
   997
nipkow@48849
   998
lemma setprod_mono_one_cong_left: 
nipkow@48849
   999
  "\<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
  1000
  \<Longrightarrow> setprod f S = setprod g T"
nipkow@48849
  1001
by(fact setprod.F_mono_neutral_cong_left)
nipkow@48849
  1002
nipkow@48849
  1003
lemmas setprod_mono_one_cong_right = setprod.F_mono_neutral_cong_right
chaieb@29674
  1004
nipkow@48849
  1005
lemma setprod_delta: "finite S \<Longrightarrow>
nipkow@48849
  1006
  setprod (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
nipkow@48849
  1007
by(fact setprod.F_delta)
chaieb@29674
  1008
nipkow@48849
  1009
lemma setprod_delta': "finite S \<Longrightarrow>
nipkow@48849
  1010
  setprod (\<lambda>k. if a = k then b k else 1) S = (if a\<in> S then b a else 1)"
nipkow@48849
  1011
by(fact setprod.F_delta')
chaieb@29674
  1012
nipkow@15402
  1013
lemma setprod_UN_disjoint:
nipkow@15402
  1014
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1015
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1016
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
wenzelm@41550
  1017
  by (simp add: setprod_def fold_image_UN_disjoint)
nipkow@15402
  1018
nipkow@15402
  1019
lemma setprod_Union_disjoint:
hoelzl@44937
  1020
  assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" 
hoelzl@44937
  1021
  shows "setprod f (Union C) = setprod (setprod f) C"
hoelzl@44937
  1022
proof cases
hoelzl@44937
  1023
  assume "finite C"
hoelzl@44937
  1024
  from setprod_UN_disjoint[OF this assms]
hoelzl@44937
  1025
  show ?thesis
hoelzl@44937
  1026
    by (simp add: SUP_def)
hoelzl@44937
  1027
qed (force dest: finite_UnionD simp add: setprod_def)
nipkow@15402
  1028
nipkow@15402
  1029
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@16550
  1030
    (\<Prod>x\<in>A. (\<Prod>y\<in> B x. f x y)) =
paulson@17189
  1031
    (\<Prod>(x,y)\<in>(SIGMA x:A. B x). f x y)"
wenzelm@41550
  1032
by(simp add:setprod_def fold_image_Sigma split_def)
nipkow@15402
  1033
paulson@15409
  1034
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1035
lemma setprod_cartesian_product: 
paulson@17189
  1036
     "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
paulson@15409
  1037
apply (cases "finite A") 
paulson@15409
  1038
 apply (cases "finite B") 
paulson@15409
  1039
  apply (simp add: setprod_Sigma)
paulson@15409
  1040
 apply (cases "A={}", simp)
nipkow@48849
  1041
 apply (simp) 
paulson@15409
  1042
apply (auto simp add: setprod_def
paulson@15409
  1043
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1044
done
nipkow@15402
  1045
nipkow@48861
  1046
lemma setprod_timesf: "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
nipkow@48861
  1047
by (fact setprod.F_fun_f)
nipkow@15402
  1048
nipkow@15402
  1049
nipkow@15402
  1050
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1051
nipkow@15402
  1052
lemma setprod_eq_1_iff [simp]:
nipkow@28853
  1053
  "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
nipkow@28853
  1054
by (induct set: finite) auto
nipkow@15402
  1055
nipkow@15402
  1056
lemma setprod_zero:
huffman@23277
  1057
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
nipkow@28853
  1058
apply (induct set: finite, force, clarsimp)
nipkow@28853
  1059
apply (erule disjE, auto)
nipkow@28853
  1060
done
nipkow@15402
  1061
nipkow@15402
  1062
lemma setprod_nonneg [rule_format]:
haftmann@35028
  1063
   "(ALL x: A. (0::'a::linordered_semidom) \<le> f x) --> 0 \<le> setprod f A"
huffman@30841
  1064
by (cases "finite A", induct set: finite, simp_all add: mult_nonneg_nonneg)
huffman@30841
  1065
haftmann@35028
  1066
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::linordered_semidom) < f x)
nipkow@28853
  1067
  --> 0 < setprod f A"
huffman@30841
  1068
by (cases "finite A", induct set: finite, simp_all add: mult_pos_pos)
nipkow@15402
  1069
nipkow@30843
  1070
lemma setprod_zero_iff[simp]: "finite A ==> 
nipkow@30843
  1071
  (setprod f A = (0::'a::{comm_semiring_1,no_zero_divisors})) =
nipkow@30843
  1072
  (EX x: A. f x = 0)"
nipkow@30843
  1073
by (erule finite_induct, auto simp:no_zero_divisors)
nipkow@30843
  1074
nipkow@30843
  1075
lemma setprod_pos_nat:
nipkow@30843
  1076
  "finite S ==> (ALL x : S. f x > (0::nat)) ==> setprod f S > 0"
nipkow@30843
  1077
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
nipkow@15402
  1078
nipkow@30863
  1079
lemma setprod_pos_nat_iff[simp]:
nipkow@30863
  1080
  "finite S ==> (setprod f S > 0) = (ALL x : S. f x > (0::nat))"
nipkow@30863
  1081
using setprod_zero_iff by(simp del:neq0_conv add:neq0_conv[symmetric])
nipkow@30863
  1082
nipkow@15402
  1083
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
nipkow@28853
  1084
  (setprod f (A Un B) :: 'a ::{field})
nipkow@28853
  1085
   = setprod f A * setprod f B / setprod f (A Int B)"
nipkow@30843
  1086
by (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1087
nipkow@15402
  1088
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@28853
  1089
  (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@28853
  1090
  (if a:A then setprod f A / f a else setprod f A)"
haftmann@36303
  1091
  by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
  1092
paulson@31906
  1093
lemma setprod_inversef: 
haftmann@36409
  1094
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
paulson@31906
  1095
  shows "finite A ==> setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@28853
  1096
by (erule finite_induct) auto
nipkow@15402
  1097
nipkow@15402
  1098
lemma setprod_dividef:
haftmann@36409
  1099
  fixes f :: "'b \<Rightarrow> 'a::field_inverse_zero"
wenzelm@31916
  1100
  shows "finite A
nipkow@28853
  1101
    ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@28853
  1102
apply (subgoal_tac
nipkow@15402
  1103
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@28853
  1104
apply (erule ssubst)
nipkow@28853
  1105
apply (subst divide_inverse)
nipkow@28853
  1106
apply (subst setprod_timesf)
nipkow@28853
  1107
apply (subst setprod_inversef, assumption+, rule refl)
nipkow@28853
  1108
apply (rule setprod_cong, rule refl)
nipkow@28853
  1109
apply (subst divide_inverse, auto)
nipkow@28853
  1110
done
nipkow@28853
  1111
nipkow@29925
  1112
lemma setprod_dvd_setprod [rule_format]: 
nipkow@29925
  1113
    "(ALL x : A. f x dvd g x) \<longrightarrow> setprod f A dvd setprod g A"
nipkow@29925
  1114
  apply (cases "finite A")
nipkow@29925
  1115
  apply (induct set: finite)
nipkow@29925
  1116
  apply (auto simp add: dvd_def)
nipkow@29925
  1117
  apply (rule_tac x = "k * ka" in exI)
nipkow@29925
  1118
  apply (simp add: algebra_simps)
nipkow@29925
  1119
done
nipkow@29925
  1120
nipkow@29925
  1121
lemma setprod_dvd_setprod_subset:
nipkow@29925
  1122
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> setprod f A dvd setprod f B"
nipkow@29925
  1123
  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")
nipkow@29925
  1124
  apply (unfold dvd_def, blast)
nipkow@29925
  1125
  apply (subst setprod_Un_disjoint [symmetric])
nipkow@29925
  1126
  apply (auto elim: finite_subset intro: setprod_cong)
nipkow@29925
  1127
done
nipkow@29925
  1128
nipkow@29925
  1129
lemma setprod_dvd_setprod_subset2:
nipkow@29925
  1130
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> ALL x : A. (f x::'a::comm_semiring_1) dvd g x \<Longrightarrow> 
nipkow@29925
  1131
      setprod f A dvd setprod g B"
nipkow@29925
  1132
  apply (rule dvd_trans)
nipkow@29925
  1133
  apply (rule setprod_dvd_setprod, erule (1) bspec)
nipkow@29925
  1134
  apply (erule (1) setprod_dvd_setprod_subset)
nipkow@29925
  1135
done
nipkow@29925
  1136
nipkow@29925
  1137
lemma dvd_setprod: "finite A \<Longrightarrow> i:A \<Longrightarrow> 
nipkow@29925
  1138
    (f i ::'a::comm_semiring_1) dvd setprod f A"
nipkow@29925
  1139
by (induct set: finite) (auto intro: dvd_mult)
nipkow@29925
  1140
nipkow@29925
  1141
lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) \<longrightarrow> 
nipkow@29925
  1142
    (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"
nipkow@29925
  1143
  apply (cases "finite A")
nipkow@29925
  1144
  apply (induct set: finite)
nipkow@29925
  1145
  apply auto
nipkow@29925
  1146
done
nipkow@29925
  1147
hoelzl@35171
  1148
lemma setprod_mono:
hoelzl@35171
  1149
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linordered_semidom"
hoelzl@35171
  1150
  assumes "\<forall>i\<in>A. 0 \<le> f i \<and> f i \<le> g i"
hoelzl@35171
  1151
  shows "setprod f A \<le> setprod g A"
hoelzl@35171
  1152
proof (cases "finite A")
hoelzl@35171
  1153
  case True
hoelzl@35171
  1154
  hence ?thesis "setprod f A \<ge> 0" using subset_refl[of A]
hoelzl@35171
  1155
  proof (induct A rule: finite_subset_induct)
hoelzl@35171
  1156
    case (insert a F)
hoelzl@35171
  1157
    thus "setprod f (insert a F) \<le> setprod g (insert a F)" "0 \<le> setprod f (insert a F)"
hoelzl@35171
  1158
      unfolding setprod_insert[OF insert(1,3)]
hoelzl@35171
  1159
      using assms[rule_format,OF insert(2)] insert
hoelzl@35171
  1160
      by (auto intro: mult_mono mult_nonneg_nonneg)
hoelzl@35171
  1161
  qed auto
hoelzl@35171
  1162
  thus ?thesis by simp
hoelzl@35171
  1163
qed auto
hoelzl@35171
  1164
hoelzl@35171
  1165
lemma abs_setprod:
hoelzl@35171
  1166
  fixes f :: "'a \<Rightarrow> 'b\<Colon>{linordered_field,abs}"
hoelzl@35171
  1167
  shows "abs (setprod f A) = setprod (\<lambda>x. abs (f x)) A"
hoelzl@35171
  1168
proof (cases "finite A")
hoelzl@35171
  1169
  case True thus ?thesis
huffman@35216
  1170
    by induct (auto simp add: field_simps abs_mult)
hoelzl@35171
  1171
qed auto
hoelzl@35171
  1172
haftmann@31017
  1173
lemma setprod_constant: "finite A ==> (\<Prod>x\<in> A. (y::'a::{comm_monoid_mult})) = y^(card A)"
nipkow@28853
  1174
apply (erule finite_induct)
huffman@35216
  1175
apply auto
nipkow@28853
  1176
done
nipkow@15402
  1177
chaieb@29674
  1178
lemma setprod_gen_delta:
chaieb@29674
  1179
  assumes fS: "finite S"
haftmann@31017
  1180
  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
  1181
proof-
chaieb@29674
  1182
  let ?f = "(\<lambda>k. if k=a then b k else c)"
chaieb@29674
  1183
  {assume a: "a \<notin> S"
chaieb@29674
  1184
    hence "\<forall> k\<in> S. ?f k = c" by simp
nipkow@48849
  1185
    hence ?thesis  using a setprod_constant[OF fS, of c] by simp }
chaieb@29674
  1186
  moreover 
chaieb@29674
  1187
  {assume a: "a \<in> S"
chaieb@29674
  1188
    let ?A = "S - {a}"
chaieb@29674
  1189
    let ?B = "{a}"
chaieb@29674
  1190
    have eq: "S = ?A \<union> ?B" using a by blast 
chaieb@29674
  1191
    have dj: "?A \<inter> ?B = {}" by simp
chaieb@29674
  1192
    from fS have fAB: "finite ?A" "finite ?B" by auto  
chaieb@29674
  1193
    have fA0:"setprod ?f ?A = setprod (\<lambda>i. c) ?A"
chaieb@29674
  1194
      apply (rule setprod_cong) by auto
chaieb@29674
  1195
    have cA: "card ?A = card S - 1" using fS a by auto
chaieb@29674
  1196
    have fA1: "setprod ?f ?A = c ^ card ?A"  unfolding fA0 apply (rule setprod_constant) using fS by auto
chaieb@29674
  1197
    have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
chaieb@29674
  1198
      using setprod_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
chaieb@29674
  1199
      by simp
chaieb@29674
  1200
    then have ?thesis using a cA
haftmann@36349
  1201
      by (simp add: fA1 field_simps cong add: setprod_cong cong del: if_weak_cong)}
chaieb@29674
  1202
  ultimately show ?thesis by blast
chaieb@29674
  1203
qed
chaieb@29674
  1204
chaieb@29674
  1205
haftmann@35816
  1206
subsection {* Versions of @{const inf} and @{const sup} on non-empty sets *}
haftmann@35816
  1207
haftmann@35816
  1208
no_notation times (infixl "*" 70)
haftmann@35816
  1209
no_notation Groups.one ("1")
haftmann@35816
  1210
haftmann@35816
  1211
locale semilattice_big = semilattice +
haftmann@35816
  1212
  fixes F :: "'a set \<Rightarrow> 'a"
haftmann@35816
  1213
  assumes F_eq: "finite A \<Longrightarrow> F A = fold1 (op *) A"
haftmann@35816
  1214
haftmann@35816
  1215
sublocale semilattice_big < folding_one_idem proof
haftmann@35816
  1216
qed (simp_all add: F_eq)
haftmann@35816
  1217
haftmann@35816
  1218
notation times (infixl "*" 70)
haftmann@35816
  1219
notation Groups.one ("1")
haftmann@22917
  1220
haftmann@35816
  1221
context lattice
haftmann@35816
  1222
begin
haftmann@35816
  1223
haftmann@35816
  1224
definition Inf_fin :: "'a set \<Rightarrow> 'a" ("\<Sqinter>\<^bsub>fin\<^esub>_" [900] 900) where
haftmann@35816
  1225
  "Inf_fin = fold1 inf"
haftmann@35816
  1226
haftmann@35816
  1227
definition Sup_fin :: "'a set \<Rightarrow> 'a" ("\<Squnion>\<^bsub>fin\<^esub>_" [900] 900) where
haftmann@35816
  1228
  "Sup_fin = fold1 sup"
haftmann@35816
  1229
haftmann@35816
  1230
end
haftmann@35816
  1231
haftmann@35816
  1232
sublocale lattice < Inf_fin!: semilattice_big inf Inf_fin proof
haftmann@35816
  1233
qed (simp add: Inf_fin_def)
haftmann@35816
  1234
haftmann@35816
  1235
sublocale lattice < Sup_fin!: semilattice_big sup Sup_fin proof
haftmann@35816
  1236
qed (simp add: Sup_fin_def)
haftmann@22917
  1237
haftmann@35028
  1238
context semilattice_inf
haftmann@26041
  1239
begin
haftmann@26041
  1240
haftmann@36635
  1241
lemma ab_semigroup_idem_mult_inf:
haftmann@36635
  1242
  "class.ab_semigroup_idem_mult inf"
haftmann@35816
  1243
proof qed (rule inf_assoc inf_commute inf_idem)+
haftmann@35816
  1244
haftmann@46033
  1245
lemma fold_inf_insert[simp]: "finite A \<Longrightarrow> Finite_Set.fold inf b (insert a A) = inf a (Finite_Set.fold inf b A)"
haftmann@42871
  1246
by(rule comp_fun_idem.fold_insert_idem[OF ab_semigroup_idem_mult.comp_fun_idem[OF ab_semigroup_idem_mult_inf]])
haftmann@35816
  1247
haftmann@46033
  1248
lemma inf_le_fold_inf: "finite A \<Longrightarrow> ALL a:A. b \<le> a \<Longrightarrow> inf b c \<le> Finite_Set.fold inf c A"
haftmann@35816
  1249
by (induct pred: finite) (auto intro: le_infI1)
haftmann@35816
  1250
haftmann@46033
  1251
lemma fold_inf_le_inf: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> Finite_Set.fold inf b A \<le> inf a b"
haftmann@35816
  1252
proof(induct arbitrary: a pred:finite)
haftmann@35816
  1253
  case empty thus ?case by simp
haftmann@35816
  1254
next
haftmann@35816
  1255
  case (insert x A)
haftmann@35816
  1256
  show ?case
haftmann@35816
  1257
  proof cases
haftmann@35816
  1258
    assume "A = {}" thus ?thesis using insert by simp
haftmann@35816
  1259
  next
haftmann@35816
  1260
    assume "A \<noteq> {}" thus ?thesis using insert by (auto intro: le_infI2)
haftmann@35816
  1261
  qed
haftmann@35816
  1262
qed
haftmann@35816
  1263
haftmann@26041
  1264
lemma below_fold1_iff:
haftmann@26041
  1265
  assumes "finite A" "A \<noteq> {}"
haftmann@26041
  1266
  shows "x \<le> fold1 inf A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
haftmann@26041
  1267
proof -
haftmann@29509
  1268
  interpret ab_semigroup_idem_mult inf
haftmann@26041
  1269
    by (rule ab_semigroup_idem_mult_inf)
haftmann@26041
  1270
  show ?thesis using assms by (induct rule: finite_ne_induct) simp_all
haftmann@26041
  1271
qed
haftmann@26041
  1272
haftmann@26041
  1273
lemma fold1_belowI:
haftmann@26757
  1274
  assumes "finite A"
haftmann@26041
  1275
    and "a \<in> A"
haftmann@26041
  1276
  shows "fold1 inf A \<le> a"
haftmann@26757
  1277
proof -
haftmann@26757
  1278
  from assms have "A \<noteq> {}" by auto
haftmann@26757
  1279
  from `finite A` `A \<noteq> {}` `a \<in> A` show ?thesis
haftmann@26757
  1280
  proof (induct rule: finite_ne_induct)
haftmann@26757
  1281
    case singleton thus ?case by simp
haftmann@26041
  1282
  next
haftmann@29509
  1283
    interpret ab_semigroup_idem_mult inf
haftmann@26757
  1284
      by (rule ab_semigroup_idem_mult_inf)
haftmann@26757
  1285
    case (insert x F)
haftmann@26757
  1286
    from insert(5) have "a = x \<or> a \<in> F" by simp
haftmann@26757
  1287
    thus ?case
haftmann@26757
  1288
    proof
haftmann@26757
  1289
      assume "a = x" thus ?thesis using insert
nipkow@29667
  1290
        by (simp add: mult_ac)
haftmann@26757
  1291
    next
haftmann@26757
  1292
      assume "a \<in> F"
haftmann@26757
  1293
      hence bel: "fold1 inf F \<le> a" by (rule insert)
haftmann@26757
  1294
      have "inf (fold1 inf (insert x F)) a = inf x (inf (fold1 inf F) a)"
nipkow@29667
  1295
        using insert by (simp add: mult_ac)
haftmann@26757
  1296
      also have "inf (fold1 inf F) a = fold1 inf F"
haftmann@26757
  1297
        using bel by (auto intro: antisym)
haftmann@26757
  1298
      also have "inf x \<dots> = fold1 inf (insert x F)"
nipkow@29667
  1299
        using insert by (simp add: mult_ac)
haftmann@26757
  1300
      finally have aux: "inf (fold1 inf (insert x F)) a = fold1 inf (insert x F)" .
haftmann@26757
  1301
      moreover have "inf (fold1 inf (insert x F)) a \<le> a" by simp
haftmann@26757
  1302
      ultimately show ?thesis by simp
haftmann@26757
  1303
    qed
haftmann@26041
  1304
  qed
haftmann@26041
  1305
qed
haftmann@26041
  1306
haftmann@26041
  1307
end
haftmann@26041
  1308
haftmann@35816
  1309
context semilattice_sup
haftmann@22917
  1310
begin
haftmann@22917
  1311
haftmann@36635
  1312
lemma ab_semigroup_idem_mult_sup: "class.ab_semigroup_idem_mult sup"
haftmann@35816
  1313
by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)
haftmann@35816
  1314
haftmann@46033
  1315
lemma fold_sup_insert[simp]: "finite A \<Longrightarrow> Finite_Set.fold sup b (insert a A) = sup a (Finite_Set.fold sup b A)"
haftmann@35816
  1316
by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)
haftmann@22917
  1317
haftmann@46033
  1318
lemma fold_sup_le_sup: "finite A \<Longrightarrow> ALL a:A. a \<le> b \<Longrightarrow> Finite_Set.fold sup c A \<le> sup b c"
haftmann@35816
  1319
by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)
haftmann@35816
  1320
haftmann@46033
  1321
lemma sup_le_fold_sup: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a b \<le> Finite_Set.fold sup b A"
haftmann@35816
  1322
by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)
haftmann@35816
  1323
haftmann@35816
  1324
end
haftmann@35816
  1325
haftmann@35816
  1326
context lattice
haftmann@35816
  1327
begin
haftmann@25062
  1328
wenzelm@31916
  1329
lemma Inf_le_Sup [simp]: "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<Sqinter>\<^bsub>fin\<^esub>A \<le> \<Squnion>\<^bsub>fin\<^esub>A"
haftmann@24342
  1330
apply(unfold Sup_fin_def Inf_fin_def)
nipkow@15500
  1331
apply(subgoal_tac "EX a. a:A")
nipkow@15500
  1332
prefer 2 apply blast
nipkow@15500
  1333
apply(erule exE)
haftmann@22388
  1334
apply(rule order_trans)
haftmann@26757
  1335
apply(erule (1) fold1_belowI)
haftmann@35028
  1336
apply(erule (1) semilattice_inf.fold1_belowI [OF dual_semilattice])
nipkow@15500
  1337
done
nipkow@15500
  1338
haftmann@24342
  1339
lemma sup_Inf_absorb [simp]:
wenzelm@31916
  1340
  "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a (\<Sqinter>\<^bsub>fin\<^esub>A) = a"
nipkow@15512
  1341
apply(subst sup_commute)
haftmann@26041
  1342
apply(simp add: Inf_fin_def sup_absorb2 fold1_belowI)
nipkow@15504
  1343
done
nipkow@15504
  1344
haftmann@24342
  1345
lemma inf_Sup_absorb [simp]:
wenzelm@31916
  1346
  "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> inf a (\<Squnion>\<^bsub>fin\<^esub>A) = a"
haftmann@26041
  1347
by (simp add: Sup_fin_def inf_absorb1
haftmann@35028
  1348
  semilattice_inf.fold1_belowI [OF dual_semilattice])
haftmann@24342
  1349
haftmann@24342
  1350
end
haftmann@24342
  1351
haftmann@24342
  1352
context distrib_lattice
haftmann@24342
  1353
begin
haftmann@24342
  1354
haftmann@24342
  1355
lemma sup_Inf1_distrib:
haftmann@26041
  1356
  assumes "finite A"
haftmann@26041
  1357
    and "A \<noteq> {}"
wenzelm@31916
  1358
  shows "sup x (\<Sqinter>\<^bsub>fin\<^esub>A) = \<Sqinter>\<^bsub>fin\<^esub>{sup x a|a. a \<in> A}"
haftmann@26041
  1359
proof -
haftmann@29509
  1360
  interpret ab_semigroup_idem_mult inf
haftmann@26041
  1361
    by (rule ab_semigroup_idem_mult_inf)
haftmann@26041
  1362
  from assms show ?thesis
haftmann@26041
  1363
    by (simp add: Inf_fin_def image_def
haftmann@26041
  1364
      hom_fold1_commute [where h="sup x", OF sup_inf_distrib1])
berghofe@26792
  1365
        (rule arg_cong [where f="fold1 inf"], blast)
haftmann@26041
  1366
qed
nipkow@18423
  1367
haftmann@24342
  1368
lemma sup_Inf2_distrib:
haftmann@24342
  1369
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
wenzelm@31916
  1370
  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
  1371
using A proof (induct rule: finite_ne_induct)
nipkow@15500
  1372
  case singleton thus ?case
wenzelm@41550
  1373
    by (simp add: sup_Inf1_distrib [OF B])
nipkow@15500
  1374
next
haftmann@29509
  1375
  interpret ab_semigroup_idem_mult inf
haftmann@26041
  1376
    by (rule ab_semigroup_idem_mult_inf)
nipkow@15500
  1377
  case (insert x A)
haftmann@25062
  1378
  have finB: "finite {sup x b |b. b \<in> B}"
haftmann@25062
  1379
    by(rule finite_surj[where f = "sup x", OF B(1)], auto)
haftmann@25062
  1380
  have finAB: "finite {sup a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@15500
  1381
  proof -
haftmann@25062
  1382
    have "{sup a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {sup a b})"
nipkow@15500
  1383
      by blast
berghofe@15517
  1384
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@15500
  1385
  qed
haftmann@25062
  1386
  have ne: "{sup a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
wenzelm@31916
  1387
  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
  1388
    using insert by simp
wenzelm@31916
  1389
  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
  1390
  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
  1391
    using insert by(simp add:sup_Inf1_distrib[OF B])
wenzelm@31916
  1392
  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
  1393
    (is "_ = \<Sqinter>\<^bsub>fin\<^esub>?M")
nipkow@15500
  1394
    using B insert
haftmann@26041
  1395
    by (simp add: Inf_fin_def fold1_Un2 [OF finB _ finAB ne])
haftmann@25062
  1396
  also have "?M = {sup a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@15500
  1397
    by blast
nipkow@15500
  1398
  finally show ?case .
nipkow@15500
  1399
qed
nipkow@15500
  1400
haftmann@24342
  1401
lemma inf_Sup1_distrib:
haftmann@26041
  1402
  assumes "finite A" and "A \<noteq> {}"
wenzelm@31916
  1403
  shows "inf x (\<Squnion>\<^bsub>fin\<^esub>A) = \<Squnion>\<^bsub>fin\<^esub>{inf x a|a. a \<in> A}"
haftmann@26041
  1404
proof -
haftmann@29509
  1405
  interpret ab_semigroup_idem_mult sup
haftmann@26041
  1406
    by (rule ab_semigroup_idem_mult_sup)
haftmann@26041
  1407
  from assms show ?thesis
haftmann@26041
  1408
    by (simp add: Sup_fin_def image_def hom_fold1_commute [where h="inf x", OF inf_sup_distrib1])
berghofe@26792
  1409
      (rule arg_cong [where f="fold1 sup"], blast)
haftmann@26041
  1410
qed
nipkow@18423
  1411
haftmann@24342
  1412
lemma inf_Sup2_distrib:
haftmann@24342
  1413
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
wenzelm@31916
  1414
  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
  1415
using A proof (induct rule: finite_ne_induct)
nipkow@18423
  1416
  case singleton thus ?case
huffman@44921
  1417
    by(simp add: inf_Sup1_distrib [OF B])
nipkow@18423
  1418
next
nipkow@18423
  1419
  case (insert x A)
haftmann@25062
  1420
  have finB: "finite {inf x b |b. b \<in> B}"
haftmann@25062
  1421
    by(rule finite_surj[where f = "%b. inf x b", OF B(1)], auto)
haftmann@25062
  1422
  have finAB: "finite {inf a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@18423
  1423
  proof -
haftmann@25062
  1424
    have "{inf a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {inf a b})"
nipkow@18423
  1425
      by blast
nipkow@18423
  1426
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@18423
  1427
  qed
haftmann@25062
  1428
  have ne: "{inf a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
haftmann@29509
  1429
  interpret ab_semigroup_idem_mult sup
haftmann@26041
  1430
    by (rule ab_semigroup_idem_mult_sup)
wenzelm@31916
  1431
  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
  1432
    using insert by simp
wenzelm@31916
  1433
  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
  1434
  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
  1435
    using insert by(simp add:inf_Sup1_distrib[OF B])
wenzelm@31916
  1436
  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
  1437
    (is "_ = \<Squnion>\<^bsub>fin\<^esub>?M")
nipkow@18423
  1438
    using B insert
haftmann@26041
  1439
    by (simp add: Sup_fin_def fold1_Un2 [OF finB _ finAB ne])
haftmann@25062
  1440
  also have "?M = {inf a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@18423
  1441
    by blast
nipkow@18423
  1442
  finally show ?case .
nipkow@18423
  1443
qed
nipkow@18423
  1444
haftmann@24342
  1445
end
haftmann@24342
  1446
haftmann@35719
  1447
context complete_lattice
haftmann@35719
  1448
begin
haftmann@35719
  1449
haftmann@35719
  1450
lemma Inf_fin_Inf:
haftmann@35719
  1451
  assumes "finite A" and "A \<noteq> {}"
haftmann@35719
  1452
  shows "\<Sqinter>\<^bsub>fin\<^esub>A = Inf A"
haftmann@35719
  1453
proof -
haftmann@35719
  1454
  interpret ab_semigroup_idem_mult inf
haftmann@35719
  1455
    by (rule ab_semigroup_idem_mult_inf)
noschinl@44918
  1456
  from `A \<noteq> {}` obtain b B where "A = {b} \<union> B" by auto
haftmann@35719
  1457
  moreover with `finite A` have "finite B" by simp
noschinl@44918
  1458
  ultimately show ?thesis
noschinl@44918
  1459
    by (simp add: Inf_fin_def fold1_eq_fold_idem inf_Inf_fold_inf [symmetric])
haftmann@35719
  1460
qed
haftmann@35719
  1461
haftmann@35719
  1462
lemma Sup_fin_Sup:
haftmann@35719
  1463
  assumes "finite A" and "A \<noteq> {}"
haftmann@35719
  1464
  shows "\<Squnion>\<^bsub>fin\<^esub>A = Sup A"
haftmann@35719
  1465
proof -
haftmann@35719
  1466
  interpret ab_semigroup_idem_mult sup
haftmann@35719
  1467
    by (rule ab_semigroup_idem_mult_sup)
noschinl@44918
  1468
  from `A \<noteq> {}` obtain b B where "A = {b} \<union> B" by auto
haftmann@35719
  1469
  moreover with `finite A` have "finite B" by simp
haftmann@35719
  1470
  ultimately show ?thesis  
haftmann@35719
  1471
  by (simp add: Sup_fin_def fold1_eq_fold_idem sup_Sup_fold_sup [symmetric])
haftmann@35719
  1472
qed
haftmann@35719
  1473
haftmann@35719
  1474
end
haftmann@35719
  1475
haftmann@22917
  1476
haftmann@35816
  1477
subsection {* Versions of @{const min} and @{const max} on non-empty sets *}
haftmann@35816
  1478
haftmann@35816
  1479
definition (in linorder) Min :: "'a set \<Rightarrow> 'a" where
haftmann@35816
  1480
  "Min = fold1 min"
haftmann@22917
  1481
haftmann@35816
  1482
definition (in linorder) Max :: "'a set \<Rightarrow> 'a" where
haftmann@35816
  1483
  "Max = fold1 max"
haftmann@35816
  1484
haftmann@35816
  1485
sublocale linorder < Min!: semilattice_big min Min proof
haftmann@35816
  1486
qed (simp add: Min_def)
haftmann@35816
  1487
haftmann@35816
  1488
sublocale linorder < Max!: semilattice_big max Max proof
haftmann@35816
  1489
qed (simp add: Max_def)
haftmann@22917
  1490
haftmann@24342
  1491
context linorder
haftmann@22917
  1492
begin
haftmann@22917
  1493
haftmann@35816
  1494
lemmas Min_singleton = Min.singleton
haftmann@35816
  1495
lemmas Max_singleton = Max.singleton
haftmann@35816
  1496
haftmann@35816
  1497
lemma Min_insert:
haftmann@35816
  1498
  assumes "finite A" and "A \<noteq> {}"
haftmann@35816
  1499
  shows "Min (insert x A) = min x (Min A)"
haftmann@35816
  1500
  using assms by simp
haftmann@35816
  1501
haftmann@35816
  1502
lemma Max_insert:
haftmann@35816
  1503
  assumes "finite A" and "A \<noteq> {}"
haftmann@35816
  1504
  shows "Max (insert x A) = max x (Max A)"
haftmann@35816
  1505
  using assms by simp
haftmann@35816
  1506
haftmann@35816
  1507
lemma Min_Un:
haftmann@35816
  1508
  assumes "finite A" and "A \<noteq> {}" and "finite B" and "B \<noteq> {}"
haftmann@35816
  1509
  shows "Min (A \<union> B) = min (Min A) (Min B)"
haftmann@35816
  1510
  using assms by (rule Min.union_idem)
haftmann@35816
  1511
haftmann@35816
  1512
lemma Max_Un:
haftmann@35816
  1513
  assumes "finite A" and "A \<noteq> {}" and "finite B" and "B \<noteq> {}"
haftmann@35816
  1514
  shows "Max (A \<union> B) = max (Max A) (Max B)"
haftmann@35816
  1515
  using assms by (rule Max.union_idem)
haftmann@35816
  1516
haftmann@35816
  1517
lemma hom_Min_commute:
haftmann@35816
  1518
  assumes "\<And>x y. h (min x y) = min (h x) (h y)"
haftmann@35816
  1519
    and "finite N" and "N \<noteq> {}"
haftmann@35816
  1520
  shows "h (Min N) = Min (h ` N)"
haftmann@35816
  1521
  using assms by (rule Min.hom_commute)
haftmann@35816
  1522
haftmann@35816
  1523
lemma hom_Max_commute:
haftmann@35816
  1524
  assumes "\<And>x y. h (max x y) = max (h x) (h y)"
haftmann@35816
  1525
    and "finite N" and "N \<noteq> {}"
haftmann@35816
  1526
  shows "h (Max N) = Max (h ` N)"
haftmann@35816
  1527
  using assms by (rule Max.hom_commute)
haftmann@35816
  1528
haftmann@26041
  1529
lemma ab_semigroup_idem_mult_min:
haftmann@36635
  1530
  "class.ab_semigroup_idem_mult min"
haftmann@28823
  1531
  proof qed (auto simp add: min_def)
haftmann@26041
  1532
haftmann@26041
  1533
lemma ab_semigroup_idem_mult_max:
haftmann@36635
  1534
  "class.ab_semigroup_idem_mult max"
haftmann@28823
  1535
  proof qed (auto simp add: max_def)
haftmann@26041
  1536
haftmann@26041
  1537
lemma max_lattice:
krauss@44845
  1538
  "class.semilattice_inf max (op \<ge>) (op >)"
haftmann@32203
  1539
  by (fact min_max.dual_semilattice)
haftmann@26041
  1540
haftmann@26041
  1541
lemma dual_max:
haftmann@26041
  1542
  "ord.max (op \<ge>) = min"
wenzelm@46904
  1543
  by (auto simp add: ord.max_def min_def fun_eq_iff)
haftmann@26041
  1544
haftmann@26041
  1545
lemma dual_min:
haftmann@26041
  1546
  "ord.min (op \<ge>) = max"
wenzelm@46904
  1547
  by (auto simp add: ord.min_def max_def fun_eq_iff)
haftmann@26041
  1548
haftmann@26041
  1549
lemma strict_below_fold1_iff:
haftmann@26041
  1550
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1551
  shows "x < fold1 min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
haftmann@26041
  1552
proof -
haftmann@29509
  1553
  interpret ab_semigroup_idem_mult min
haftmann@26041
  1554
    by (rule ab_semigroup_idem_mult_min)
haftmann@26041
  1555
  from assms show ?thesis
haftmann@26041
  1556
  by (induct rule: finite_ne_induct)
haftmann@26041
  1557
    (simp_all add: fold1_insert)
haftmann@26041
  1558
qed
haftmann@26041
  1559
haftmann@26041
  1560
lemma fold1_below_iff:
haftmann@26041
  1561
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1562
  shows "fold1 min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
haftmann@26041
  1563
proof -
haftmann@29509
  1564
  interpret ab_semigroup_idem_mult min
haftmann@26041
  1565
    by (rule ab_semigroup_idem_mult_min)
haftmann@26041
  1566
  from assms show ?thesis
haftmann@26041
  1567
  by (induct rule: finite_ne_induct)
haftmann@26041
  1568
    (simp_all add: fold1_insert min_le_iff_disj)
haftmann@26041
  1569
qed
haftmann@26041
  1570
haftmann@26041
  1571
lemma fold1_strict_below_iff:
haftmann@26041
  1572
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1573
  shows "fold1 min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
haftmann@26041
  1574
proof -
haftmann@29509
  1575
  interpret ab_semigroup_idem_mult min
haftmann@26041
  1576
    by (rule ab_semigroup_idem_mult_min)
haftmann@26041
  1577
  from assms show ?thesis
haftmann@26041
  1578
  by (induct rule: finite_ne_induct)
haftmann@26041
  1579
    (simp_all add: fold1_insert min_less_iff_disj)
haftmann@26041
  1580
qed
haftmann@26041
  1581
haftmann@26041
  1582
lemma fold1_antimono:
haftmann@26041
  1583
  assumes "A \<noteq> {}" and "A \<subseteq> B" and "finite B"
haftmann@26041
  1584
  shows "fold1 min B \<le> fold1 min A"
haftmann@26041
  1585
proof cases
haftmann@26041
  1586
  assume "A = B" thus ?thesis by simp
haftmann@26041
  1587
next
haftmann@29509
  1588
  interpret ab_semigroup_idem_mult min
haftmann@26041
  1589
    by (rule ab_semigroup_idem_mult_min)
wenzelm@41550
  1590
  assume neq: "A \<noteq> B"
haftmann@26041
  1591
  have B: "B = A \<union> (B-A)" using `A \<subseteq> B` by blast
haftmann@26041
  1592
  have "fold1 min B = fold1 min (A \<union> (B-A))" by(subst B)(rule refl)
haftmann@26041
  1593
  also have "\<dots> = min (fold1 min A) (fold1 min (B-A))"
haftmann@26041
  1594
  proof -
haftmann@26041
  1595
    have "finite A" by(rule finite_subset[OF `A \<subseteq> B` `finite B`])
wenzelm@41550
  1596
    moreover have "finite(B-A)" by(rule finite_Diff[OF `finite B`])
wenzelm@41550
  1597
    moreover have "(B-A) \<noteq> {}" using assms neq by blast
wenzelm@41550
  1598
    moreover have "A Int (B-A) = {}" using assms by blast
haftmann@26041
  1599
    ultimately show ?thesis using `A \<noteq> {}` by (rule_tac fold1_Un)
haftmann@26041
  1600
  qed
haftmann@26041
  1601
  also have "\<dots> \<le> fold1 min A" by (simp add: min_le_iff_disj)
haftmann@26041
  1602
  finally show ?thesis .
haftmann@26041
  1603
qed
haftmann@26041
  1604
paulson@24427
  1605
lemma Min_in [simp]:
haftmann@26041
  1606
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1607
  shows "Min A \<in> A"
haftmann@26041
  1608
proof -
haftmann@29509
  1609
  interpret ab_semigroup_idem_mult min
haftmann@26041
  1610
    by (rule ab_semigroup_idem_mult_min)
nipkow@44890
  1611
  from assms fold1_in show ?thesis by (fastforce simp: Min_def min_def)
haftmann@26041
  1612
qed
nipkow@15392
  1613
paulson@24427
  1614
lemma Max_in [simp]:
haftmann@26041
  1615
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1616
  shows "Max A \<in> A"
haftmann@26041
  1617
proof -
haftmann@29509
  1618
  interpret ab_semigroup_idem_mult max
haftmann@26041
  1619
    by (rule ab_semigroup_idem_mult_max)
nipkow@44890
  1620
  from assms fold1_in [of A] show ?thesis by (fastforce simp: Max_def max_def)
haftmann@26041
  1621
qed
haftmann@26041
  1622
haftmann@26041
  1623
lemma Min_le [simp]:
haftmann@26757
  1624
  assumes "finite A" and "x \<in> A"
haftmann@26041
  1625
  shows "Min A \<le> x"
haftmann@32203
  1626
  using assms by (simp add: Min_def min_max.fold1_belowI)
haftmann@26041
  1627
haftmann@26041
  1628
lemma Max_ge [simp]:
haftmann@26757
  1629
  assumes "finite A" and "x \<in> A"
haftmann@26041
  1630
  shows "x \<le> Max A"
huffman@44921
  1631
  by (simp add: Max_def semilattice_inf.fold1_belowI [OF max_lattice] assms)
haftmann@26041
  1632
blanchet@35828
  1633
lemma Min_ge_iff [simp, no_atp]:
haftmann@26041
  1634
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1635
  shows "x \<le> Min A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
haftmann@32203
  1636
  using assms by (simp add: Min_def min_max.below_fold1_iff)
haftmann@26041
  1637
blanchet@35828
  1638
lemma Max_le_iff [simp, no_atp]:
haftmann@26041
  1639
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1640
  shows "Max A \<le> x \<longleftrightarrow> (\<forall>a\<in>A. a \<le> x)"
huffman@44921
  1641
  by (simp add: Max_def semilattice_inf.below_fold1_iff [OF max_lattice] assms)
haftmann@26041
  1642
blanchet@35828
  1643
lemma Min_gr_iff [simp, no_atp]:
haftmann@26041
  1644
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1645
  shows "x < Min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
haftmann@32203
  1646
  using assms by (simp add: Min_def strict_below_fold1_iff)
haftmann@26041
  1647
blanchet@35828
  1648
lemma Max_less_iff [simp, no_atp]:
haftmann@26041
  1649
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1650
  shows "Max A < x \<longleftrightarrow> (\<forall>a\<in>A. a < x)"
huffman@44921
  1651
  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
huffman@44921
  1652
    linorder.strict_below_fold1_iff [OF dual_linorder] assms)
nipkow@18493
  1653
blanchet@35828
  1654
lemma Min_le_iff [no_atp]:
haftmann@26041
  1655
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1656
  shows "Min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
haftmann@32203
  1657
  using assms by (simp add: Min_def fold1_below_iff)
nipkow@15497
  1658
blanchet@35828
  1659
lemma Max_ge_iff [no_atp]:
haftmann@26041
  1660
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1661
  shows "x \<le> Max A \<longleftrightarrow> (\<exists>a\<in>A. x \<le> a)"
huffman@44921
  1662
  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
huffman@44921
  1663
    linorder.fold1_below_iff [OF dual_linorder] assms)
haftmann@22917
  1664
blanchet@35828
  1665
lemma Min_less_iff [no_atp]:
haftmann@26041
  1666
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1667
  shows "Min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
haftmann@32203
  1668
  using assms by (simp add: Min_def fold1_strict_below_iff)
haftmann@22917
  1669
blanchet@35828
  1670
lemma Max_gr_iff [no_atp]:
haftmann@26041
  1671
  assumes "finite A" and "A \<noteq> {}"
haftmann@26041
  1672
  shows "x < Max A \<longleftrightarrow> (\<exists>a\<in>A. x < a)"
huffman@44921
  1673
  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
huffman@44921
  1674
    linorder.fold1_strict_below_iff [OF dual_linorder] assms)
haftmann@26041
  1675
haftmann@30325
  1676
lemma Min_eqI:
haftmann@30325
  1677
  assumes "finite A"
haftmann@30325
  1678
  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<ge> x"
haftmann@30325
  1679
    and "x \<in> A"
haftmann@30325
  1680
  shows "Min A = x"
haftmann@30325
  1681
proof (rule antisym)
haftmann@30325
  1682
  from `x \<in> A` have "A \<noteq> {}" by auto
haftmann@30325
  1683
  with assms show "Min A \<ge> x" by simp
haftmann@30325
  1684
next
haftmann@30325
  1685
  from assms show "x \<ge> Min A" by simp
haftmann@30325
  1686
qed
haftmann@30325
  1687
haftmann@30325
  1688
lemma Max_eqI:
haftmann@30325
  1689
  assumes "finite A"
haftmann@30325
  1690
  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
haftmann@30325
  1691
    and "x \<in> A"
haftmann@30325
  1692
  shows "Max A = x"
haftmann@30325
  1693
proof (rule antisym)
haftmann@30325
  1694
  from `x \<in> A` have "A \<noteq> {}" by auto
haftmann@30325
  1695
  with assms show "Max A \<le> x" by simp
haftmann@30325
  1696
next
haftmann@30325
  1697
  from assms show "x \<le> Max A" by simp
haftmann@30325
  1698
qed
haftmann@30325
  1699
haftmann@26041
  1700
lemma Min_antimono:
haftmann@26041
  1701
  assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
haftmann@26041
  1702
  shows "Min N \<le> Min M"
haftmann@32203
  1703
  using assms by (simp add: Min_def fold1_antimono)
haftmann@26041
  1704
haftmann@26041
  1705
lemma Max_mono:
haftmann@26041
  1706
  assumes "M \<subseteq> N" and "M \<noteq> {}" and "finite N"
haftmann@26041
  1707
  shows "Max M \<le> Max N"
huffman@44921
  1708
  by (simp add: Max_def linorder.dual_max [OF dual_linorder]
huffman@44921
  1709
    linorder.fold1_antimono [OF dual_linorder] assms)
haftmann@22917
  1710
nipkow@32006
  1711
lemma finite_linorder_max_induct[consumes 1, case_names empty insert]:
urbanc@36079
  1712
 assumes fin: "finite A"
urbanc@36079
  1713
 and   empty: "P {}" 
urbanc@36079
  1714
 and  insert: "(!!b A. finite A \<Longrightarrow> ALL a:A. a < b \<Longrightarrow> P A \<Longrightarrow> P(insert b A))"
urbanc@36079
  1715
 shows "P A"
urbanc@36079
  1716
using fin empty insert
nipkow@32006
  1717
proof (induct rule: finite_psubset_induct)
urbanc@36079
  1718
  case (psubset A)
urbanc@36079
  1719
  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
  1720
  have fin: "finite A" by fact 
urbanc@36079
  1721
  have empty: "P {}" by fact
urbanc@36079
  1722
  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
  1723
  show "P A"
haftmann@26757
  1724
  proof (cases "A = {}")
urbanc@36079
  1725
    assume "A = {}" 
urbanc@36079
  1726
    then show "P A" using `P {}` by simp
krauss@26748
  1727
  next
urbanc@36079
  1728
    let ?B = "A - {Max A}" 
urbanc@36079
  1729
    let ?A = "insert (Max A) ?B"
urbanc@36079
  1730
    have "finite ?B" using `finite A` by simp
krauss@26748
  1731
    assume "A \<noteq> {}"
krauss@26748
  1732
    with `finite A` have "Max A : A" by auto
urbanc@36079
  1733
    then have A: "?A = A" using insert_Diff_single insert_absorb by auto
urbanc@36079
  1734
    then have "P ?B" using `P {}` step IH[of ?B] by blast
urbanc@36079
  1735
    moreover 
nipkow@44890
  1736
    have "\<forall>a\<in>?B. a < Max A" using Max_ge [OF `finite A`] by fastforce
nipkow@44890
  1737
    ultimately show "P A" using A insert_Diff_single step[OF `finite ?B`] by fastforce
krauss@26748
  1738
  qed
krauss@26748
  1739
qed
krauss@26748
  1740
nipkow@32006
  1741
lemma finite_linorder_min_induct[consumes 1, case_names empty insert]:
nipkow@33434
  1742
 "\<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"
nipkow@32006
  1743
by(rule linorder.finite_linorder_max_induct[OF dual_linorder])
nipkow@32006
  1744
haftmann@22917
  1745
end
haftmann@22917
  1746
haftmann@35028
  1747
context linordered_ab_semigroup_add
haftmann@22917
  1748
begin
haftmann@22917
  1749
haftmann@22917
  1750
lemma add_Min_commute:
haftmann@22917
  1751
  fixes k
haftmann@25062
  1752
  assumes "finite N" and "N \<noteq> {}"
haftmann@25062
  1753
  shows "k + Min N = Min {k + m | m. m \<in> N}"
haftmann@25062
  1754
proof -
haftmann@25062
  1755
  have "\<And>x y. k + min x y = min (k + x) (k + y)"
haftmann@25062
  1756
    by (simp add: min_def not_le)
haftmann@25062
  1757
      (blast intro: antisym less_imp_le add_left_mono)
haftmann@25062
  1758
  with assms show ?thesis
haftmann@25062
  1759
    using hom_Min_commute [of "plus k" N]
haftmann@25062
  1760
    by simp (blast intro: arg_cong [where f = Min])
haftmann@25062
  1761
qed
haftmann@22917
  1762
haftmann@22917
  1763
lemma add_Max_commute:
haftmann@22917
  1764
  fixes k
haftmann@25062
  1765
  assumes "finite N" and "N \<noteq> {}"
haftmann@25062
  1766
  shows "k + Max N = Max {k + m | m. m \<in> N}"
haftmann@25062
  1767
proof -
haftmann@25062
  1768
  have "\<And>x y. k + max x y = max (k + x) (k + y)"
haftmann@25062
  1769
    by (simp add: max_def not_le)
haftmann@25062
  1770
      (blast intro: antisym less_imp_le add_left_mono)
haftmann@25062
  1771
  with assms show ?thesis
haftmann@25062
  1772
    using hom_Max_commute [of "plus k" N]
haftmann@25062
  1773
    by simp (blast intro: arg_cong [where f = Max])
haftmann@25062
  1774
qed
haftmann@22917
  1775
haftmann@22917
  1776
end
haftmann@22917
  1777
haftmann@35034
  1778
context linordered_ab_group_add
haftmann@35034
  1779
begin
haftmann@35034
  1780
haftmann@35034
  1781
lemma minus_Max_eq_Min [simp]:
haftmann@35034
  1782
  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - (Max S) = Min (uminus ` S)"
haftmann@35034
  1783
  by (induct S rule: finite_ne_induct) (simp_all add: minus_max_eq_min)
haftmann@35034
  1784
haftmann@35034
  1785
lemma minus_Min_eq_Max [simp]:
haftmann@35034
  1786
  "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> - (Min S) = Max (uminus ` S)"
haftmann@35034
  1787
  by (induct S rule: finite_ne_induct) (simp_all add: minus_min_eq_max)
haftmann@35034
  1788
haftmann@35034
  1789
end
haftmann@35034
  1790
haftmann@25571
  1791
end