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