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