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