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