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