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