src/HOL/Finite_Set.thy
author huffman
Wed Apr 01 11:31:24 2009 -0700 (2009-04-01)
changeset 30841 0813afc97522
parent 30729 461ee3e49ad3
child 30842 d007dee0c372
permissions -rw-r--r--
generalized setprod_nonneg and setprod_pos to ordered_semidom, simplified proofs
wenzelm@12396
     1
(*  Title:      HOL/Finite_Set.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
wenzelm@12396
     6
header {* Finite sets *}
wenzelm@12396
     7
nipkow@15131
     8
theory Finite_Set
haftmann@29609
     9
imports Nat Product_Type Power
nipkow@15131
    10
begin
wenzelm@12396
    11
nipkow@15392
    12
subsection {* Definition and basic properties *}
wenzelm@12396
    13
berghofe@23736
    14
inductive finite :: "'a set => bool"
berghofe@22262
    15
  where
berghofe@22262
    16
    emptyI [simp, intro!]: "finite {}"
berghofe@22262
    17
  | insertI [simp, intro!]: "finite A ==> finite (insert a A)"
wenzelm@12396
    18
nipkow@13737
    19
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    20
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    21
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    22
proof -
haftmann@28823
    23
  from assms have "A \<noteq> UNIV" by blast
wenzelm@14661
    24
  thus ?thesis by blast
wenzelm@14661
    25
qed
wenzelm@12396
    26
berghofe@22262
    27
lemma finite_induct [case_names empty insert, induct set: finite]:
wenzelm@12396
    28
  "finite F ==>
nipkow@15327
    29
    P {} ==> (!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    30
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    31
proof -
wenzelm@13421
    32
  assume "P {}" and
nipkow@15327
    33
    insert: "!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    34
  assume "finite F"
wenzelm@12396
    35
  thus "P F"
wenzelm@12396
    36
  proof induct
wenzelm@23389
    37
    show "P {}" by fact
nipkow@15327
    38
    fix x F assume F: "finite F" and P: "P F"
wenzelm@12396
    39
    show "P (insert x F)"
wenzelm@12396
    40
    proof cases
wenzelm@12396
    41
      assume "x \<in> F"
wenzelm@12396
    42
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    43
      with P show ?thesis by (simp only:)
wenzelm@12396
    44
    next
wenzelm@12396
    45
      assume "x \<notin> F"
wenzelm@12396
    46
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    47
    qed
wenzelm@12396
    48
  qed
wenzelm@12396
    49
qed
wenzelm@12396
    50
nipkow@15484
    51
lemma finite_ne_induct[case_names singleton insert, consumes 2]:
nipkow@15484
    52
assumes fin: "finite F" shows "F \<noteq> {} \<Longrightarrow>
nipkow@15484
    53
 \<lbrakk> \<And>x. P{x};
nipkow@15484
    54
   \<And>x F. \<lbrakk> finite F; F \<noteq> {}; x \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert x F) \<rbrakk>
nipkow@15484
    55
 \<Longrightarrow> P F"
nipkow@15484
    56
using fin
nipkow@15484
    57
proof induct
nipkow@15484
    58
  case empty thus ?case by simp
nipkow@15484
    59
next
nipkow@15484
    60
  case (insert x F)
nipkow@15484
    61
  show ?case
nipkow@15484
    62
  proof cases
wenzelm@23389
    63
    assume "F = {}"
wenzelm@23389
    64
    thus ?thesis using `P {x}` by simp
nipkow@15484
    65
  next
wenzelm@23389
    66
    assume "F \<noteq> {}"
wenzelm@23389
    67
    thus ?thesis using insert by blast
nipkow@15484
    68
  qed
nipkow@15484
    69
qed
nipkow@15484
    70
wenzelm@12396
    71
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@23389
    72
  assumes "finite F" and "F \<subseteq> A"
wenzelm@23389
    73
    and empty: "P {}"
wenzelm@23389
    74
    and insert: "!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@23389
    75
  shows "P F"
wenzelm@12396
    76
proof -
wenzelm@23389
    77
  from `finite F` and `F \<subseteq> A`
wenzelm@23389
    78
  show ?thesis
wenzelm@12396
    79
  proof induct
wenzelm@23389
    80
    show "P {}" by fact
wenzelm@23389
    81
  next
wenzelm@23389
    82
    fix x F
wenzelm@23389
    83
    assume "finite F" and "x \<notin> F" and
wenzelm@23389
    84
      P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    85
    show "P (insert x F)"
wenzelm@12396
    86
    proof (rule insert)
wenzelm@12396
    87
      from i show "x \<in> A" by blast
wenzelm@12396
    88
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    89
      with P show "P F" .
wenzelm@23389
    90
      show "finite F" by fact
wenzelm@23389
    91
      show "x \<notin> F" by fact
wenzelm@12396
    92
    qed
wenzelm@12396
    93
  qed
wenzelm@12396
    94
qed
wenzelm@12396
    95
nipkow@29923
    96
text{* A finite choice principle. Does not need the SOME choice operator. *}
nipkow@29923
    97
lemma finite_set_choice:
nipkow@29923
    98
  "finite A \<Longrightarrow> ALL x:A. (EX y. P x y) \<Longrightarrow> EX f. ALL x:A. P x (f x)"
nipkow@29923
    99
proof (induct set: finite)
nipkow@29923
   100
  case empty thus ?case by simp
nipkow@29923
   101
next
nipkow@29923
   102
  case (insert a A)
nipkow@29923
   103
  then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
nipkow@29923
   104
  show ?case (is "EX f. ?P f")
nipkow@29923
   105
  proof
nipkow@29923
   106
    show "?P(%x. if x = a then b else f x)" using f ab by auto
nipkow@29923
   107
  qed
nipkow@29923
   108
qed
nipkow@29923
   109
haftmann@23878
   110
nipkow@15392
   111
text{* Finite sets are the images of initial segments of natural numbers: *}
nipkow@15392
   112
paulson@15510
   113
lemma finite_imp_nat_seg_image_inj_on:
paulson@15510
   114
  assumes fin: "finite A" 
paulson@15510
   115
  shows "\<exists> (n::nat) f. A = f ` {i. i<n} & inj_on f {i. i<n}"
nipkow@15392
   116
using fin
nipkow@15392
   117
proof induct
nipkow@15392
   118
  case empty
paulson@15510
   119
  show ?case  
paulson@15510
   120
  proof show "\<exists>f. {} = f ` {i::nat. i < 0} & inj_on f {i. i<0}" by simp 
paulson@15510
   121
  qed
nipkow@15392
   122
next
nipkow@15392
   123
  case (insert a A)
wenzelm@23389
   124
  have notinA: "a \<notin> A" by fact
paulson@15510
   125
  from insert.hyps obtain n f
paulson@15510
   126
    where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
paulson@15510
   127
  hence "insert a A = f(n:=a) ` {i. i < Suc n}"
paulson@15510
   128
        "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
paulson@15510
   129
    by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
nipkow@15392
   130
  thus ?case by blast
nipkow@15392
   131
qed
nipkow@15392
   132
nipkow@15392
   133
lemma nat_seg_image_imp_finite:
nipkow@15392
   134
  "!!f A. A = f ` {i::nat. i<n} \<Longrightarrow> finite A"
nipkow@15392
   135
proof (induct n)
nipkow@15392
   136
  case 0 thus ?case by simp
nipkow@15392
   137
next
nipkow@15392
   138
  case (Suc n)
nipkow@15392
   139
  let ?B = "f ` {i. i < n}"
nipkow@15392
   140
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
nipkow@15392
   141
  show ?case
nipkow@15392
   142
  proof cases
nipkow@15392
   143
    assume "\<exists>k<n. f n = f k"
nipkow@15392
   144
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   145
    thus ?thesis using finB by simp
nipkow@15392
   146
  next
nipkow@15392
   147
    assume "\<not>(\<exists> k<n. f n = f k)"
nipkow@15392
   148
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   149
    thus ?thesis using finB by simp
nipkow@15392
   150
  qed
nipkow@15392
   151
qed
nipkow@15392
   152
nipkow@15392
   153
lemma finite_conv_nat_seg_image:
nipkow@15392
   154
  "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
paulson@15510
   155
by(blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
nipkow@15392
   156
nipkow@29920
   157
lemma finite_Collect_less_nat[iff]: "finite{n::nat. n<k}"
nipkow@29920
   158
by(fastsimp simp: finite_conv_nat_seg_image)
nipkow@29920
   159
haftmann@26441
   160
nipkow@15392
   161
subsubsection{* Finiteness and set theoretic constructions *}
nipkow@15392
   162
wenzelm@12396
   163
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
nipkow@29901
   164
by (induct set: finite) simp_all
wenzelm@12396
   165
wenzelm@12396
   166
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
   167
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
   168
proof -
wenzelm@12396
   169
  assume "finite B"
wenzelm@12396
   170
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
   171
  proof induct
wenzelm@12396
   172
    case empty
wenzelm@12396
   173
    thus ?case by simp
wenzelm@12396
   174
  next
nipkow@15327
   175
    case (insert x F A)
wenzelm@23389
   176
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" by fact+
wenzelm@12396
   177
    show "finite A"
wenzelm@12396
   178
    proof cases
wenzelm@12396
   179
      assume x: "x \<in> A"
wenzelm@12396
   180
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   181
      with r have "finite (A - {x})" .
wenzelm@12396
   182
      hence "finite (insert x (A - {x}))" ..
wenzelm@23389
   183
      also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
wenzelm@12396
   184
      finally show ?thesis .
wenzelm@12396
   185
    next
wenzelm@23389
   186
      show "A \<subseteq> F ==> ?thesis" by fact
wenzelm@12396
   187
      assume "x \<notin> A"
wenzelm@12396
   188
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   189
    qed
wenzelm@12396
   190
  qed
wenzelm@12396
   191
qed
wenzelm@12396
   192
wenzelm@12396
   193
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
nipkow@29901
   194
by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
nipkow@29901
   195
nipkow@29916
   196
lemma finite_Collect_disjI[simp]:
nipkow@29901
   197
  "finite{x. P x | Q x} = (finite{x. P x} & finite{x. Q x})"
nipkow@29901
   198
by(simp add:Collect_disj_eq)
wenzelm@12396
   199
wenzelm@12396
   200
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   201
  -- {* The converse obviously fails. *}
nipkow@29901
   202
by (blast intro: finite_subset)
nipkow@29901
   203
nipkow@29916
   204
lemma finite_Collect_conjI [simp, intro]:
nipkow@29901
   205
  "finite{x. P x} | finite{x. Q x} ==> finite{x. P x & Q x}"
nipkow@29901
   206
  -- {* The converse obviously fails. *}
nipkow@29901
   207
by(simp add:Collect_conj_eq)
wenzelm@12396
   208
nipkow@29920
   209
lemma finite_Collect_le_nat[iff]: "finite{n::nat. n<=k}"
nipkow@29920
   210
by(simp add: le_eq_less_or_eq)
nipkow@29920
   211
wenzelm@12396
   212
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   213
  apply (subst insert_is_Un)
paulson@14208
   214
  apply (simp only: finite_Un, blast)
wenzelm@12396
   215
  done
wenzelm@12396
   216
nipkow@15281
   217
lemma finite_Union[simp, intro]:
nipkow@15281
   218
 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
nipkow@15281
   219
by (induct rule:finite_induct) simp_all
nipkow@15281
   220
wenzelm@12396
   221
lemma finite_empty_induct:
wenzelm@23389
   222
  assumes "finite A"
wenzelm@23389
   223
    and "P A"
wenzelm@23389
   224
    and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@23389
   225
  shows "P {}"
wenzelm@12396
   226
proof -
wenzelm@12396
   227
  have "P (A - A)"
wenzelm@12396
   228
  proof -
wenzelm@23389
   229
    {
wenzelm@23389
   230
      fix c b :: "'a set"
wenzelm@23389
   231
      assume c: "finite c" and b: "finite b"
wenzelm@23389
   232
	and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@23389
   233
      have "c \<subseteq> b ==> P (b - c)"
wenzelm@23389
   234
	using c
wenzelm@23389
   235
      proof induct
wenzelm@23389
   236
	case empty
wenzelm@23389
   237
	from P1 show ?case by simp
wenzelm@23389
   238
      next
wenzelm@23389
   239
	case (insert x F)
wenzelm@23389
   240
	have "P (b - F - {x})"
wenzelm@23389
   241
	proof (rule P2)
wenzelm@23389
   242
          from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@23389
   243
          from insert show "x \<in> b - F" by simp
wenzelm@23389
   244
          from insert show "P (b - F)" by simp
wenzelm@23389
   245
	qed
wenzelm@23389
   246
	also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@23389
   247
	finally show ?case .
wenzelm@12396
   248
      qed
wenzelm@23389
   249
    }
wenzelm@23389
   250
    then show ?thesis by this (simp_all add: assms)
wenzelm@12396
   251
  qed
wenzelm@23389
   252
  then show ?thesis by simp
wenzelm@12396
   253
qed
wenzelm@12396
   254
nipkow@29901
   255
lemma finite_Diff [simp]: "finite A ==> finite (A - B)"
nipkow@29901
   256
by (rule Diff_subset [THEN finite_subset])
nipkow@29901
   257
nipkow@29901
   258
lemma finite_Diff2 [simp]:
nipkow@29901
   259
  assumes "finite B" shows "finite (A - B) = finite A"
nipkow@29901
   260
proof -
nipkow@29901
   261
  have "finite A \<longleftrightarrow> finite((A-B) Un (A Int B))" by(simp add: Un_Diff_Int)
nipkow@29901
   262
  also have "\<dots> \<longleftrightarrow> finite(A-B)" using `finite B` by(simp)
nipkow@29901
   263
  finally show ?thesis ..
nipkow@29901
   264
qed
nipkow@29901
   265
nipkow@29901
   266
lemma finite_compl[simp]:
nipkow@29901
   267
  "finite(A::'a set) \<Longrightarrow> finite(-A) = finite(UNIV::'a set)"
nipkow@29901
   268
by(simp add:Compl_eq_Diff_UNIV)
wenzelm@12396
   269
nipkow@29916
   270
lemma finite_Collect_not[simp]:
nipkow@29903
   271
  "finite{x::'a. P x} \<Longrightarrow> finite{x. ~P x} = finite(UNIV::'a set)"
nipkow@29903
   272
by(simp add:Collect_neg_eq)
nipkow@29903
   273
wenzelm@12396
   274
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   275
  apply (subst Diff_insert)
wenzelm@12396
   276
  apply (case_tac "a : A - B")
wenzelm@12396
   277
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   278
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   279
  done
wenzelm@12396
   280
wenzelm@12396
   281
nipkow@15392
   282
text {* Image and Inverse Image over Finite Sets *}
paulson@13825
   283
paulson@13825
   284
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   285
  -- {* The image of a finite set is finite. *}
berghofe@22262
   286
  by (induct set: finite) simp_all
paulson@13825
   287
paulson@14430
   288
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   289
  apply (frule finite_imageI)
paulson@14430
   290
  apply (erule finite_subset, assumption)
paulson@14430
   291
  done
paulson@14430
   292
paulson@13825
   293
lemma finite_range_imageI:
paulson@13825
   294
    "finite (range g) ==> finite (range (%x. f (g x)))"
huffman@27418
   295
  apply (drule finite_imageI, simp add: range_composition)
paulson@13825
   296
  done
paulson@13825
   297
wenzelm@12396
   298
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   299
proof -
wenzelm@12396
   300
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   301
  fix B :: "'a set"
wenzelm@12396
   302
  assume "finite B"
wenzelm@12396
   303
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   304
    apply induct
wenzelm@12396
   305
     apply simp
wenzelm@12396
   306
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   307
     apply clarify
wenzelm@12396
   308
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   309
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   310
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   311
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   312
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   313
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   314
    done
wenzelm@12396
   315
qed (rule refl)
wenzelm@12396
   316
wenzelm@12396
   317
paulson@13825
   318
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   319
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   320
         is included in a singleton. *}
paulson@14430
   321
  apply (auto simp add: inj_on_def)
paulson@14430
   322
  apply (blast intro: the_equality [symmetric])
paulson@13825
   323
  done
paulson@13825
   324
paulson@13825
   325
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   326
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   327
         is finite. *}
berghofe@22262
   328
  apply (induct set: finite)
wenzelm@21575
   329
   apply simp_all
paulson@14430
   330
  apply (subst vimage_insert)
paulson@14430
   331
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
paulson@13825
   332
  done
paulson@13825
   333
paulson@13825
   334
nipkow@15392
   335
text {* The finite UNION of finite sets *}
wenzelm@12396
   336
wenzelm@12396
   337
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
berghofe@22262
   338
  by (induct set: finite) simp_all
wenzelm@12396
   339
wenzelm@12396
   340
text {*
wenzelm@12396
   341
  Strengthen RHS to
paulson@14430
   342
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   343
wenzelm@12396
   344
  We'd need to prove
paulson@14430
   345
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   346
  by induction. *}
wenzelm@12396
   347
nipkow@29918
   348
lemma finite_UN [simp]:
nipkow@29918
   349
  "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
nipkow@29918
   350
by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   351
nipkow@29920
   352
lemma finite_Collect_bex[simp]: "finite A \<Longrightarrow>
nipkow@29920
   353
  finite{x. EX y:A. Q x y} = (ALL y:A. finite{x. Q x y})"
nipkow@29920
   354
apply(subgoal_tac "{x. EX y:A. Q x y} = UNION A (%y. {x. Q x y})")
nipkow@29920
   355
 apply auto
nipkow@29920
   356
done
nipkow@29920
   357
nipkow@29920
   358
lemma finite_Collect_bounded_ex[simp]: "finite{y. P y} \<Longrightarrow>
nipkow@29920
   359
  finite{x. EX y. P y & Q x y} = (ALL y. P y \<longrightarrow> finite{x. Q x y})"
nipkow@29920
   360
apply(subgoal_tac "{x. EX y. P y & Q x y} = UNION {y. P y} (%y. {x. Q x y})")
nipkow@29920
   361
 apply auto
nipkow@29920
   362
done
nipkow@29920
   363
nipkow@29920
   364
nipkow@17022
   365
lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)"
nipkow@17022
   366
by (simp add: Plus_def)
nipkow@17022
   367
nipkow@15392
   368
text {* Sigma of finite sets *}
wenzelm@12396
   369
wenzelm@12396
   370
lemma finite_SigmaI [simp]:
wenzelm@12396
   371
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   372
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   373
nipkow@15402
   374
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
nipkow@15402
   375
    finite (A <*> B)"
nipkow@15402
   376
  by (rule finite_SigmaI)
nipkow@15402
   377
wenzelm@12396
   378
lemma finite_Prod_UNIV:
wenzelm@12396
   379
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   380
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   381
   apply (erule ssubst)
paulson@14208
   382
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   383
  done
wenzelm@12396
   384
paulson@15409
   385
lemma finite_cartesian_productD1:
paulson@15409
   386
     "[| finite (A <*> B); B \<noteq> {} |] ==> finite A"
paulson@15409
   387
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   388
apply (drule_tac x=n in spec) 
paulson@15409
   389
apply (drule_tac x="fst o f" in spec) 
paulson@15409
   390
apply (auto simp add: o_def) 
paulson@15409
   391
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   392
apply (drule equalityD1) 
paulson@15409
   393
apply (rename_tac y x)
paulson@15409
   394
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   395
 prefer 2 apply force
paulson@15409
   396
apply clarify
paulson@15409
   397
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   398
done
paulson@15409
   399
paulson@15409
   400
lemma finite_cartesian_productD2:
paulson@15409
   401
     "[| finite (A <*> B); A \<noteq> {} |] ==> finite B"
paulson@15409
   402
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   403
apply (drule_tac x=n in spec) 
paulson@15409
   404
apply (drule_tac x="snd o f" in spec) 
paulson@15409
   405
apply (auto simp add: o_def) 
paulson@15409
   406
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   407
apply (drule equalityD1)
paulson@15409
   408
apply (rename_tac x y)
paulson@15409
   409
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   410
 prefer 2 apply force
paulson@15409
   411
apply clarify
paulson@15409
   412
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   413
done
paulson@15409
   414
paulson@15409
   415
nipkow@15392
   416
text {* The powerset of a finite set *}
wenzelm@12396
   417
wenzelm@12396
   418
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   419
proof
wenzelm@12396
   420
  assume "finite (Pow A)"
wenzelm@12396
   421
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   422
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   423
next
wenzelm@12396
   424
  assume "finite A"
wenzelm@12396
   425
  thus "finite (Pow A)"
wenzelm@12396
   426
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   427
qed
wenzelm@12396
   428
nipkow@29916
   429
lemma finite_Collect_subsets[simp,intro]: "finite A \<Longrightarrow> finite{B. B \<subseteq> A}"
nipkow@29916
   430
by(simp add: Pow_def[symmetric])
nipkow@15392
   431
nipkow@29918
   432
nipkow@15392
   433
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
nipkow@15392
   434
by(blast intro: finite_subset[OF subset_Pow_Union])
nipkow@15392
   435
nipkow@15392
   436
haftmann@26441
   437
subsection {* Class @{text finite}  *}
haftmann@26041
   438
haftmann@26041
   439
setup {* Sign.add_path "finite" *} -- {*FIXME: name tweaking*}
haftmann@29797
   440
class finite =
haftmann@26041
   441
  assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
haftmann@26041
   442
setup {* Sign.parent_path *}
haftmann@26041
   443
hide const finite
haftmann@26041
   444
huffman@27430
   445
context finite
huffman@27430
   446
begin
huffman@27430
   447
huffman@27430
   448
lemma finite [simp]: "finite (A \<Colon> 'a set)"
haftmann@26441
   449
  by (rule subset_UNIV finite_UNIV finite_subset)+
haftmann@26041
   450
huffman@27430
   451
end
huffman@27430
   452
haftmann@26146
   453
lemma UNIV_unit [noatp]:
haftmann@26041
   454
  "UNIV = {()}" by auto
haftmann@26041
   455
haftmann@26146
   456
instance unit :: finite
haftmann@26146
   457
  by default (simp add: UNIV_unit)
haftmann@26146
   458
haftmann@26146
   459
lemma UNIV_bool [noatp]:
haftmann@26041
   460
  "UNIV = {False, True}" by auto
haftmann@26041
   461
haftmann@26146
   462
instance bool :: finite
haftmann@26146
   463
  by default (simp add: UNIV_bool)
haftmann@26146
   464
haftmann@26146
   465
instance * :: (finite, finite) finite
haftmann@26146
   466
  by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
haftmann@26146
   467
haftmann@26041
   468
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
haftmann@26041
   469
  by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq)
haftmann@26041
   470
haftmann@26146
   471
instance "fun" :: (finite, finite) finite
haftmann@26146
   472
proof
haftmann@26041
   473
  show "finite (UNIV :: ('a => 'b) set)"
haftmann@26041
   474
  proof (rule finite_imageD)
haftmann@26041
   475
    let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
berghofe@26792
   476
    have "range ?graph \<subseteq> Pow UNIV" by simp
berghofe@26792
   477
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
berghofe@26792
   478
      by (simp only: finite_Pow_iff finite)
berghofe@26792
   479
    ultimately show "finite (range ?graph)"
berghofe@26792
   480
      by (rule finite_subset)
haftmann@26041
   481
    show "inj ?graph" by (rule inj_graph)
haftmann@26041
   482
  qed
haftmann@26041
   483
qed
haftmann@26041
   484
haftmann@27981
   485
instance "+" :: (finite, finite) finite
haftmann@27981
   486
  by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
haftmann@27981
   487
haftmann@26041
   488
nipkow@15392
   489
subsection {* A fold functional for finite sets *}
nipkow@15392
   490
nipkow@15392
   491
text {* The intended behaviour is
haftmann@29966
   492
@{text "fold f z {x\<^isub>1, ..., x\<^isub>n} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
nipkow@28853
   493
if @{text f} is ``left-commutative'':
nipkow@15392
   494
*}
nipkow@15392
   495
nipkow@28853
   496
locale fun_left_comm =
nipkow@28853
   497
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@28853
   498
  assumes fun_left_comm: "f x (f y z) = f y (f x z)"
nipkow@28853
   499
begin
nipkow@28853
   500
nipkow@28853
   501
text{* On a functional level it looks much nicer: *}
nipkow@28853
   502
nipkow@28853
   503
lemma fun_comp_comm:  "f x \<circ> f y = f y \<circ> f x"
nipkow@28853
   504
by (simp add: fun_left_comm expand_fun_eq)
nipkow@28853
   505
nipkow@28853
   506
end
nipkow@28853
   507
nipkow@28853
   508
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
nipkow@28853
   509
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
nipkow@28853
   510
  emptyI [intro]: "fold_graph f z {} z" |
nipkow@28853
   511
  insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
nipkow@28853
   512
      \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
nipkow@28853
   513
nipkow@28853
   514
inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
nipkow@28853
   515
nipkow@28853
   516
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
nipkow@28853
   517
[code del]: "fold f z A = (THE y. fold_graph f z A y)"
nipkow@15392
   518
paulson@15498
   519
text{*A tempting alternative for the definiens is
nipkow@28853
   520
@{term "if finite A then THE y. fold_graph f z A y else e"}.
paulson@15498
   521
It allows the removal of finiteness assumptions from the theorems
nipkow@28853
   522
@{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
nipkow@28853
   523
The proofs become ugly. It is not worth the effort. (???) *}
nipkow@28853
   524
nipkow@28853
   525
nipkow@28853
   526
lemma Diff1_fold_graph:
nipkow@28853
   527
  "fold_graph f z (A - {x}) y \<Longrightarrow> x \<in> A \<Longrightarrow> fold_graph f z A (f x y)"
nipkow@28853
   528
by (erule insert_Diff [THEN subst], rule fold_graph.intros, auto)
nipkow@28853
   529
nipkow@28853
   530
lemma fold_graph_imp_finite: "fold_graph f z A x \<Longrightarrow> finite A"
nipkow@28853
   531
by (induct set: fold_graph) auto
nipkow@28853
   532
nipkow@28853
   533
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
nipkow@28853
   534
by (induct set: finite) auto
nipkow@28853
   535
nipkow@28853
   536
nipkow@28853
   537
subsubsection{*From @{const fold_graph} to @{term fold}*}
nipkow@15392
   538
paulson@15510
   539
lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
wenzelm@19868
   540
  by (auto simp add: less_Suc_eq) 
paulson@15510
   541
paulson@15510
   542
lemma insert_image_inj_on_eq:
paulson@15510
   543
     "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A; 
paulson@15510
   544
        inj_on h {i. i < Suc m}|] 
paulson@15510
   545
      ==> A = h ` {i. i < m}"
paulson@15510
   546
apply (auto simp add: image_less_Suc inj_on_def)
paulson@15510
   547
apply (blast intro: less_trans) 
paulson@15510
   548
done
paulson@15510
   549
paulson@15510
   550
lemma insert_inj_onE:
paulson@15510
   551
  assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A" 
paulson@15510
   552
      and inj_on: "inj_on h {i::nat. i<n}"
paulson@15510
   553
  shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
paulson@15510
   554
proof (cases n)
paulson@15510
   555
  case 0 thus ?thesis using aA by auto
paulson@15510
   556
next
paulson@15510
   557
  case (Suc m)
wenzelm@23389
   558
  have nSuc: "n = Suc m" by fact
paulson@15510
   559
  have mlessn: "m<n" by (simp add: nSuc)
paulson@15532
   560
  from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
nipkow@27165
   561
  let ?hm = "Fun.swap k m h"
paulson@15520
   562
  have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn 
paulson@15520
   563
    by (simp add: inj_on_swap_iff inj_on)
paulson@15510
   564
  show ?thesis
paulson@15520
   565
  proof (intro exI conjI)
paulson@15520
   566
    show "inj_on ?hm {i. i < m}" using inj_hm
paulson@15510
   567
      by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
paulson@15520
   568
    show "m<n" by (rule mlessn)
paulson@15520
   569
    show "A = ?hm ` {i. i < m}" 
paulson@15520
   570
    proof (rule insert_image_inj_on_eq)
nipkow@27165
   571
      show "inj_on (Fun.swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
paulson@15520
   572
      show "?hm m \<notin> A" by (simp add: swap_def hkeq anot) 
paulson@15520
   573
      show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
paulson@15520
   574
	using aA hkeq nSuc klessn
paulson@15520
   575
	by (auto simp add: swap_def image_less_Suc fun_upd_image 
paulson@15520
   576
			   less_Suc_eq inj_on_image_set_diff [OF inj_on])
nipkow@15479
   577
    qed
nipkow@15479
   578
  qed
nipkow@15479
   579
qed
nipkow@15479
   580
nipkow@28853
   581
context fun_left_comm
haftmann@26041
   582
begin
haftmann@26041
   583
nipkow@28853
   584
lemma fold_graph_determ_aux:
nipkow@28853
   585
  "A = h`{i::nat. i<n} \<Longrightarrow> inj_on h {i. i<n}
nipkow@28853
   586
   \<Longrightarrow> fold_graph f z A x \<Longrightarrow> fold_graph f z A x'
nipkow@15392
   587
   \<Longrightarrow> x' = x"
nipkow@28853
   588
proof (induct n arbitrary: A x x' h rule: less_induct)
paulson@15510
   589
  case (less n)
nipkow@28853
   590
  have IH: "\<And>m h A x x'. m < n \<Longrightarrow> A = h ` {i. i<m}
nipkow@28853
   591
      \<Longrightarrow> inj_on h {i. i<m} \<Longrightarrow> fold_graph f z A x
nipkow@28853
   592
      \<Longrightarrow> fold_graph f z A x' \<Longrightarrow> x' = x" by fact
nipkow@28853
   593
  have Afoldx: "fold_graph f z A x" and Afoldx': "fold_graph f z A x'"
nipkow@28853
   594
    and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" by fact+
nipkow@28853
   595
  show ?case
nipkow@28853
   596
  proof (rule fold_graph.cases [OF Afoldx])
nipkow@28853
   597
    assume "A = {}" and "x = z"
nipkow@28853
   598
    with Afoldx' show "x' = x" by auto
nipkow@28853
   599
  next
nipkow@28853
   600
    fix B b u
nipkow@28853
   601
    assume AbB: "A = insert b B" and x: "x = f b u"
nipkow@28853
   602
      and notinB: "b \<notin> B" and Bu: "fold_graph f z B u"
nipkow@28853
   603
    show "x'=x" 
nipkow@28853
   604
    proof (rule fold_graph.cases [OF Afoldx'])
nipkow@28853
   605
      assume "A = {}" and "x' = z"
nipkow@28853
   606
      with AbB show "x' = x" by blast
nipkow@15392
   607
    next
nipkow@28853
   608
      fix C c v
nipkow@28853
   609
      assume AcC: "A = insert c C" and x': "x' = f c v"
nipkow@28853
   610
        and notinC: "c \<notin> C" and Cv: "fold_graph f z C v"
nipkow@28853
   611
      from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
nipkow@28853
   612
      from insert_inj_onE [OF Beq notinB injh]
nipkow@28853
   613
      obtain hB mB where inj_onB: "inj_on hB {i. i < mB}" 
nipkow@28853
   614
        and Beq: "B = hB ` {i. i < mB}" and lessB: "mB < n" by auto 
nipkow@28853
   615
      from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
nipkow@28853
   616
      from insert_inj_onE [OF Ceq notinC injh]
nipkow@28853
   617
      obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
nipkow@28853
   618
        and Ceq: "C = hC ` {i. i < mC}" and lessC: "mC < n" by auto 
nipkow@28853
   619
      show "x'=x"
nipkow@28853
   620
      proof cases
nipkow@28853
   621
        assume "b=c"
nipkow@28853
   622
	then moreover have "B = C" using AbB AcC notinB notinC by auto
nipkow@28853
   623
	ultimately show ?thesis  using Bu Cv x x' IH [OF lessC Ceq inj_onC]
nipkow@28853
   624
          by auto
nipkow@15392
   625
      next
nipkow@28853
   626
	assume diff: "b \<noteq> c"
nipkow@28853
   627
	let ?D = "B - {c}"
nipkow@28853
   628
	have B: "B = insert c ?D" and C: "C = insert b ?D"
nipkow@28853
   629
	  using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
nipkow@28853
   630
	have "finite A" by(rule fold_graph_imp_finite [OF Afoldx])
nipkow@28853
   631
	with AbB have "finite ?D" by simp
nipkow@28853
   632
	then obtain d where Dfoldd: "fold_graph f z ?D d"
nipkow@28853
   633
	  using finite_imp_fold_graph by iprover
nipkow@28853
   634
	moreover have cinB: "c \<in> B" using B by auto
nipkow@28853
   635
	ultimately have "fold_graph f z B (f c d)" by(rule Diff1_fold_graph)
nipkow@28853
   636
	hence "f c d = u" by (rule IH [OF lessB Beq inj_onB Bu]) 
nipkow@28853
   637
        moreover have "f b d = v"
nipkow@28853
   638
	proof (rule IH[OF lessC Ceq inj_onC Cv])
nipkow@28853
   639
	  show "fold_graph f z C (f b d)" using C notinB Dfoldd by fastsimp
nipkow@15392
   640
	qed
nipkow@28853
   641
	ultimately show ?thesis
nipkow@28853
   642
          using fun_left_comm [of c b] x x' by (auto simp add: o_def)
nipkow@15392
   643
      qed
nipkow@15392
   644
    qed
nipkow@15392
   645
  qed
nipkow@28853
   646
qed
nipkow@28853
   647
nipkow@28853
   648
lemma fold_graph_determ:
nipkow@28853
   649
  "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
nipkow@28853
   650
apply (frule fold_graph_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) 
nipkow@28853
   651
apply (blast intro: fold_graph_determ_aux [rule_format])
nipkow@15392
   652
done
nipkow@15392
   653
nipkow@28853
   654
lemma fold_equality:
nipkow@28853
   655
  "fold_graph f z A y \<Longrightarrow> fold f z A = y"
nipkow@28853
   656
by (unfold fold_def) (blast intro: fold_graph_determ)
nipkow@15392
   657
nipkow@15392
   658
text{* The base case for @{text fold}: *}
nipkow@15392
   659
nipkow@28853
   660
lemma (in -) fold_empty [simp]: "fold f z {} = z"
nipkow@28853
   661
by (unfold fold_def) blast
nipkow@28853
   662
nipkow@28853
   663
text{* The various recursion equations for @{const fold}: *}
nipkow@28853
   664
nipkow@28853
   665
lemma fold_insert_aux: "x \<notin> A
nipkow@28853
   666
  \<Longrightarrow> fold_graph f z (insert x A) v \<longleftrightarrow>
nipkow@28853
   667
      (\<exists>y. fold_graph f z A y \<and> v = f x y)"
nipkow@28853
   668
apply auto
nipkow@28853
   669
apply (rule_tac A1 = A and f1 = f in finite_imp_fold_graph [THEN exE])
nipkow@28853
   670
 apply (fastsimp dest: fold_graph_imp_finite)
nipkow@28853
   671
apply (blast intro: fold_graph_determ)
nipkow@28853
   672
done
nipkow@15392
   673
haftmann@26041
   674
lemma fold_insert [simp]:
nipkow@28853
   675
  "finite A ==> x \<notin> A ==> fold f z (insert x A) = f x (fold f z A)"
nipkow@28853
   676
apply (simp add: fold_def fold_insert_aux)
nipkow@28853
   677
apply (rule the_equality)
nipkow@28853
   678
 apply (auto intro: finite_imp_fold_graph
nipkow@28853
   679
        cong add: conj_cong simp add: fold_def[symmetric] fold_equality)
nipkow@28853
   680
done
nipkow@28853
   681
nipkow@28853
   682
lemma fold_fun_comm:
nipkow@28853
   683
  "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
nipkow@28853
   684
proof (induct rule: finite_induct)
nipkow@28853
   685
  case empty then show ?case by simp
nipkow@28853
   686
next
nipkow@28853
   687
  case (insert y A) then show ?case
nipkow@28853
   688
    by (simp add: fun_left_comm[of x])
nipkow@28853
   689
qed
nipkow@28853
   690
nipkow@28853
   691
lemma fold_insert2:
nipkow@28853
   692
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
nipkow@28853
   693
by (simp add: fold_insert fold_fun_comm)
nipkow@15392
   694
haftmann@26041
   695
lemma fold_rec:
nipkow@28853
   696
assumes "finite A" and "x \<in> A"
nipkow@28853
   697
shows "fold f z A = f x (fold f z (A - {x}))"
nipkow@28853
   698
proof -
nipkow@28853
   699
  have A: "A = insert x (A - {x})" using `x \<in> A` by blast
nipkow@28853
   700
  then have "fold f z A = fold f z (insert x (A - {x}))" by simp
nipkow@28853
   701
  also have "\<dots> = f x (fold f z (A - {x}))"
nipkow@28853
   702
    by (rule fold_insert) (simp add: `finite A`)+
nipkow@15535
   703
  finally show ?thesis .
nipkow@15535
   704
qed
nipkow@15535
   705
nipkow@28853
   706
lemma fold_insert_remove:
nipkow@28853
   707
  assumes "finite A"
nipkow@28853
   708
  shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
nipkow@28853
   709
proof -
nipkow@28853
   710
  from `finite A` have "finite (insert x A)" by auto
nipkow@28853
   711
  moreover have "x \<in> insert x A" by auto
nipkow@28853
   712
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
nipkow@28853
   713
    by (rule fold_rec)
nipkow@28853
   714
  then show ?thesis by simp
nipkow@28853
   715
qed
nipkow@28853
   716
haftmann@26041
   717
end
nipkow@15392
   718
nipkow@15480
   719
text{* A simplified version for idempotent functions: *}
nipkow@15480
   720
nipkow@28853
   721
locale fun_left_comm_idem = fun_left_comm +
nipkow@28853
   722
  assumes fun_left_idem: "f x (f x z) = f x z"
haftmann@26041
   723
begin
haftmann@26041
   724
nipkow@28853
   725
text{* The nice version: *}
nipkow@28853
   726
lemma fun_comp_idem : "f x o f x = f x"
nipkow@28853
   727
by (simp add: fun_left_idem expand_fun_eq)
nipkow@28853
   728
haftmann@26041
   729
lemma fold_insert_idem:
nipkow@28853
   730
  assumes fin: "finite A"
nipkow@28853
   731
  shows "fold f z (insert x A) = f x (fold f z A)"
nipkow@15480
   732
proof cases
nipkow@28853
   733
  assume "x \<in> A"
nipkow@28853
   734
  then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
nipkow@28853
   735
  then show ?thesis using assms by (simp add:fun_left_idem)
nipkow@15480
   736
next
nipkow@28853
   737
  assume "x \<notin> A" then show ?thesis using assms by simp
nipkow@15480
   738
qed
nipkow@15480
   739
nipkow@28853
   740
declare fold_insert[simp del] fold_insert_idem[simp]
nipkow@28853
   741
nipkow@28853
   742
lemma fold_insert_idem2:
nipkow@28853
   743
  "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
nipkow@28853
   744
by(simp add:fold_fun_comm)
nipkow@15484
   745
haftmann@26041
   746
end
haftmann@26041
   747
nipkow@28853
   748
subsubsection{* The derived combinator @{text fold_image} *}
nipkow@28853
   749
nipkow@28853
   750
definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
nipkow@28853
   751
where "fold_image f g = fold (%x y. f (g x) y)"
nipkow@28853
   752
nipkow@28853
   753
lemma fold_image_empty[simp]: "fold_image f g z {} = z"
nipkow@28853
   754
by(simp add:fold_image_def)
nipkow@15392
   755
haftmann@26041
   756
context ab_semigroup_mult
haftmann@26041
   757
begin
haftmann@26041
   758
nipkow@28853
   759
lemma fold_image_insert[simp]:
nipkow@28853
   760
assumes "finite A" and "a \<notin> A"
nipkow@28853
   761
shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
nipkow@28853
   762
proof -
ballarin@29223
   763
  interpret I: fun_left_comm "%x y. (g x) * y"
nipkow@28853
   764
    by unfold_locales (simp add: mult_ac)
nipkow@28853
   765
  show ?thesis using assms by(simp add:fold_image_def I.fold_insert)
nipkow@28853
   766
qed
nipkow@28853
   767
nipkow@28853
   768
(*
haftmann@26041
   769
lemma fold_commute:
haftmann@26041
   770
  "finite A ==> (!!z. x * (fold times g z A) = fold times g (x * z) A)"
berghofe@22262
   771
  apply (induct set: finite)
wenzelm@21575
   772
   apply simp
haftmann@26041
   773
  apply (simp add: mult_left_commute [of x])
nipkow@15392
   774
  done
nipkow@15392
   775
haftmann@26041
   776
lemma fold_nest_Un_Int:
nipkow@15392
   777
  "finite A ==> finite B
haftmann@26041
   778
    ==> fold times g (fold times g z B) A = fold times g (fold times g z (A Int B)) (A Un B)"
berghofe@22262
   779
  apply (induct set: finite)
wenzelm@21575
   780
   apply simp
nipkow@15392
   781
  apply (simp add: fold_commute Int_insert_left insert_absorb)
nipkow@15392
   782
  done
nipkow@15392
   783
haftmann@26041
   784
lemma fold_nest_Un_disjoint:
nipkow@15392
   785
  "finite A ==> finite B ==> A Int B = {}
haftmann@26041
   786
    ==> fold times g z (A Un B) = fold times g (fold times g z B) A"
nipkow@15392
   787
  by (simp add: fold_nest_Un_Int)
nipkow@28853
   788
*)
nipkow@28853
   789
nipkow@28853
   790
lemma fold_image_reindex:
paulson@15487
   791
assumes fin: "finite A"
nipkow@28853
   792
shows "inj_on h A \<Longrightarrow> fold_image times g z (h`A) = fold_image times (g\<circ>h) z A"
paulson@15506
   793
using fin apply induct
nipkow@15392
   794
 apply simp
nipkow@15392
   795
apply simp
nipkow@15392
   796
done
nipkow@15392
   797
nipkow@28853
   798
(*
haftmann@26041
   799
text{*
haftmann@26041
   800
  Fusion theorem, as described in Graham Hutton's paper,
haftmann@26041
   801
  A Tutorial on the Universality and Expressiveness of Fold,
haftmann@26041
   802
  JFP 9:4 (355-372), 1999.
haftmann@26041
   803
*}
haftmann@26041
   804
haftmann@26041
   805
lemma fold_fusion:
ballarin@27611
   806
  assumes "ab_semigroup_mult g"
haftmann@26041
   807
  assumes fin: "finite A"
haftmann@26041
   808
    and hyp: "\<And>x y. h (g x y) = times x (h y)"
haftmann@26041
   809
  shows "h (fold g j w A) = fold times j (h w) A"
ballarin@27611
   810
proof -
ballarin@29223
   811
  class_interpret ab_semigroup_mult [g] by fact
ballarin@27611
   812
  show ?thesis using fin hyp by (induct set: finite) simp_all
ballarin@27611
   813
qed
nipkow@28853
   814
*)
nipkow@28853
   815
nipkow@28853
   816
lemma fold_image_cong:
nipkow@28853
   817
  "finite A \<Longrightarrow>
nipkow@28853
   818
  (!!x. x:A ==> g x = h x) ==> fold_image times g z A = fold_image times h z A"
nipkow@28853
   819
apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold_image times g z C = fold_image times h z C")
nipkow@28853
   820
 apply simp
nipkow@28853
   821
apply (erule finite_induct, simp)
nipkow@28853
   822
apply (simp add: subset_insert_iff, clarify)
nipkow@28853
   823
apply (subgoal_tac "finite C")
nipkow@28853
   824
 prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
nipkow@28853
   825
apply (subgoal_tac "C = insert x (C - {x})")
nipkow@28853
   826
 prefer 2 apply blast
nipkow@28853
   827
apply (erule ssubst)
nipkow@28853
   828
apply (drule spec)
nipkow@28853
   829
apply (erule (1) notE impE)
nipkow@28853
   830
apply (simp add: Ball_def del: insert_Diff_single)
nipkow@28853
   831
done
nipkow@15392
   832
haftmann@26041
   833
end
haftmann@26041
   834
haftmann@26041
   835
context comm_monoid_mult
haftmann@26041
   836
begin
haftmann@26041
   837
nipkow@28853
   838
lemma fold_image_Un_Int:
haftmann@26041
   839
  "finite A ==> finite B ==>
nipkow@28853
   840
    fold_image times g 1 A * fold_image times g 1 B =
nipkow@28853
   841
    fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
nipkow@28853
   842
by (induct set: finite) 
nipkow@28853
   843
   (auto simp add: mult_ac insert_absorb Int_insert_left)
haftmann@26041
   844
haftmann@26041
   845
corollary fold_Un_disjoint:
haftmann@26041
   846
  "finite A ==> finite B ==> A Int B = {} ==>
nipkow@28853
   847
   fold_image times g 1 (A Un B) =
nipkow@28853
   848
   fold_image times g 1 A * fold_image times g 1 B"
nipkow@28853
   849
by (simp add: fold_image_Un_Int)
nipkow@28853
   850
nipkow@28853
   851
lemma fold_image_UN_disjoint:
haftmann@26041
   852
  "\<lbrakk> finite I; ALL i:I. finite (A i);
haftmann@26041
   853
     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
nipkow@28853
   854
   \<Longrightarrow> fold_image times g 1 (UNION I A) =
nipkow@28853
   855
       fold_image times (%i. fold_image times g 1 (A i)) 1 I"
nipkow@28853
   856
apply (induct set: finite, simp, atomize)
nipkow@28853
   857
apply (subgoal_tac "ALL i:F. x \<noteq> i")
nipkow@28853
   858
 prefer 2 apply blast
nipkow@28853
   859
apply (subgoal_tac "A x Int UNION F A = {}")
nipkow@28853
   860
 prefer 2 apply blast
nipkow@28853
   861
apply (simp add: fold_Un_disjoint)
nipkow@28853
   862
done
nipkow@28853
   863
nipkow@28853
   864
lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@28853
   865
  fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
nipkow@28853
   866
  fold_image times (split g) 1 (SIGMA x:A. B x)"
nipkow@15392
   867
apply (subst Sigma_def)
nipkow@28853
   868
apply (subst fold_image_UN_disjoint, assumption, simp)
nipkow@15392
   869
 apply blast
nipkow@28853
   870
apply (erule fold_image_cong)
nipkow@28853
   871
apply (subst fold_image_UN_disjoint, simp, simp)
nipkow@15392
   872
 apply blast
paulson@15506
   873
apply simp
nipkow@15392
   874
done
nipkow@15392
   875
nipkow@28853
   876
lemma fold_image_distrib: "finite A \<Longrightarrow>
nipkow@28853
   877
   fold_image times (%x. g x * h x) 1 A =
nipkow@28853
   878
   fold_image times g 1 A *  fold_image times h 1 A"
nipkow@28853
   879
by (erule finite_induct) (simp_all add: mult_ac)
haftmann@26041
   880
chaieb@30260
   881
lemma fold_image_related: 
chaieb@30260
   882
  assumes Re: "R e e" 
chaieb@30260
   883
  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
chaieb@30260
   884
  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
chaieb@30260
   885
  shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
chaieb@30260
   886
  using fS by (rule finite_subset_induct) (insert assms, auto)
chaieb@30260
   887
chaieb@30260
   888
lemma  fold_image_eq_general:
chaieb@30260
   889
  assumes fS: "finite S"
chaieb@30260
   890
  and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y" 
chaieb@30260
   891
  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
chaieb@30260
   892
  shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
chaieb@30260
   893
proof-
chaieb@30260
   894
  from h f12 have hS: "h ` S = S'" by auto
chaieb@30260
   895
  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
chaieb@30260
   896
    from f12 h H  have "x = y" by auto }
chaieb@30260
   897
  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
chaieb@30260
   898
  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
chaieb@30260
   899
  from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
chaieb@30260
   900
  also have "\<dots> = fold_image (op *) (f2 o h) e S" 
chaieb@30260
   901
    using fold_image_reindex[OF fS hinj, of f2 e] .
chaieb@30260
   902
  also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
chaieb@30260
   903
    by blast
chaieb@30260
   904
  finally show ?thesis ..
chaieb@30260
   905
qed
chaieb@30260
   906
chaieb@30260
   907
lemma fold_image_eq_general_inverses:
chaieb@30260
   908
  assumes fS: "finite S" 
chaieb@30260
   909
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
chaieb@30260
   910
  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
   911
  shows "fold_image (op *) f e S = fold_image (op *) g e T"
chaieb@30260
   912
  (* metis solves it, but not yet available here *)
chaieb@30260
   913
  apply (rule fold_image_eq_general[OF fS, of T h g f e])
chaieb@30260
   914
  apply (rule ballI)
chaieb@30260
   915
  apply (frule kh)
chaieb@30260
   916
  apply (rule ex1I[])
chaieb@30260
   917
  apply blast
chaieb@30260
   918
  apply clarsimp
chaieb@30260
   919
  apply (drule hk) apply simp
chaieb@30260
   920
  apply (rule sym)
chaieb@30260
   921
  apply (erule conjunct1[OF conjunct2[OF hk]])
chaieb@30260
   922
  apply (rule ballI)
chaieb@30260
   923
  apply (drule  hk)
chaieb@30260
   924
  apply blast
chaieb@30260
   925
  done
chaieb@30260
   926
haftmann@26041
   927
end
haftmann@22917
   928
nipkow@15402
   929
subsection {* Generalized summation over a set *}
nipkow@15402
   930
wenzelm@30729
   931
interpretation comm_monoid_add: comm_monoid_mult "0::'a::comm_monoid_add" "op +"
haftmann@28823
   932
  proof qed (auto intro: add_assoc add_commute)
haftmann@26041
   933
nipkow@28853
   934
definition setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
nipkow@28853
   935
where "setsum f A == if finite A then fold_image (op +) f 0 A else 0"
nipkow@15402
   936
wenzelm@19535
   937
abbreviation
wenzelm@21404
   938
  Setsum  ("\<Sum>_" [1000] 999) where
wenzelm@19535
   939
  "\<Sum>A == setsum (%x. x) A"
wenzelm@19535
   940
nipkow@15402
   941
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   942
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   943
nipkow@15402
   944
syntax
paulson@17189
   945
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   946
syntax (xsymbols)
paulson@17189
   947
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   948
syntax (HTML output)
paulson@17189
   949
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   950
nipkow@15402
   951
translations -- {* Beware of argument permutation! *}
nipkow@28853
   952
  "SUM i:A. b" == "CONST setsum (%i. b) A"
nipkow@28853
   953
  "\<Sum>i\<in>A. b" == "CONST setsum (%i. b) A"
nipkow@15402
   954
nipkow@15402
   955
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   956
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   957
nipkow@15402
   958
syntax
paulson@17189
   959
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   960
syntax (xsymbols)
paulson@17189
   961
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   962
syntax (HTML output)
paulson@17189
   963
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   964
nipkow@15402
   965
translations
nipkow@28853
   966
  "SUM x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@28853
   967
  "\<Sum>x|P. t" => "CONST setsum (%x. t) {x. P}"
nipkow@15402
   968
nipkow@15402
   969
print_translation {*
nipkow@15402
   970
let
wenzelm@19535
   971
  fun setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
wenzelm@19535
   972
    if x<>y then raise Match
wenzelm@19535
   973
    else let val x' = Syntax.mark_bound x
wenzelm@19535
   974
             val t' = subst_bound(x',t)
wenzelm@19535
   975
             val P' = subst_bound(x',P)
wenzelm@19535
   976
         in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
wenzelm@19535
   977
in [("setsum", setsum_tr')] end
nipkow@15402
   978
*}
nipkow@15402
   979
wenzelm@19535
   980
nipkow@15402
   981
lemma setsum_empty [simp]: "setsum f {} = 0"
nipkow@28853
   982
by (simp add: setsum_def)
nipkow@15402
   983
nipkow@15402
   984
lemma setsum_insert [simp]:
nipkow@28853
   985
  "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
nipkow@28853
   986
by (simp add: setsum_def)
nipkow@15402
   987
paulson@15409
   988
lemma setsum_infinite [simp]: "~ finite A ==> setsum f A = 0"
nipkow@28853
   989
by (simp add: setsum_def)
paulson@15409
   990
nipkow@15402
   991
lemma setsum_reindex:
nipkow@15402
   992
     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
nipkow@28853
   993
by(auto simp add: setsum_def comm_monoid_add.fold_image_reindex dest!:finite_imageD)
nipkow@15402
   994
nipkow@15402
   995
lemma setsum_reindex_id:
nipkow@15402
   996
     "inj_on f B ==> setsum f B = setsum id (f ` B)"
nipkow@15402
   997
by (auto simp add: setsum_reindex)
nipkow@15402
   998
chaieb@29674
   999
lemma setsum_reindex_nonzero: 
chaieb@29674
  1000
  assumes fS: "finite S"
chaieb@29674
  1001
  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
  1002
  shows "setsum h (f ` S) = setsum (h o f) S"
chaieb@29674
  1003
using nz
chaieb@29674
  1004
proof(induct rule: finite_induct[OF fS])
chaieb@29674
  1005
  case 1 thus ?case by simp
chaieb@29674
  1006
next
chaieb@29674
  1007
  case (2 x F) 
chaieb@29674
  1008
  {assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
chaieb@29674
  1009
    then obtain y where y: "y \<in> F" "f x = f y" by auto 
chaieb@29674
  1010
    from "2.hyps" y have xy: "x \<noteq> y" by auto
chaieb@29674
  1011
    
chaieb@29674
  1012
    from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
chaieb@29674
  1013
    have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
chaieb@29674
  1014
    also have "\<dots> = setsum (h o f) (insert x F)" 
chaieb@29674
  1015
      unfolding setsum_insert[OF `finite F` `x\<notin>F`]
chaieb@29674
  1016
      using h0 
chaieb@29674
  1017
      apply simp
chaieb@29674
  1018
      apply (rule "2.hyps"(3))
chaieb@29674
  1019
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
  1020
      apply simp_all
chaieb@29674
  1021
      done
chaieb@29674
  1022
    finally have ?case .}
chaieb@29674
  1023
  moreover
chaieb@29674
  1024
  {assume fxF: "f x \<notin> f ` F"
chaieb@29674
  1025
    have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)" 
chaieb@29674
  1026
      using fxF "2.hyps" by simp 
chaieb@29674
  1027
    also have "\<dots> = setsum (h o f) (insert x F)"
chaieb@29674
  1028
      unfolding setsum_insert[OF `finite F` `x\<notin>F`]
chaieb@29674
  1029
      apply simp
chaieb@29674
  1030
      apply (rule cong[OF refl[of "op + (h (f x))"]])
chaieb@29674
  1031
      apply (rule "2.hyps"(3))
chaieb@29674
  1032
      apply (rule_tac y="y" in  "2.prems")
chaieb@29674
  1033
      apply simp_all
chaieb@29674
  1034
      done
chaieb@29674
  1035
    finally have ?case .}
chaieb@29674
  1036
  ultimately show ?case by blast
chaieb@29674
  1037
qed
chaieb@29674
  1038
nipkow@15402
  1039
lemma setsum_cong:
nipkow@15402
  1040
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
nipkow@28853
  1041
by(fastsimp simp: setsum_def intro: comm_monoid_add.fold_image_cong)
nipkow@15402
  1042
nipkow@16733
  1043
lemma strong_setsum_cong[cong]:
nipkow@16733
  1044
  "A = B ==> (!!x. x:B =simp=> f x = g x)
nipkow@16733
  1045
   ==> setsum (%x. f x) A = setsum (%x. g x) B"
nipkow@28853
  1046
by(fastsimp simp: simp_implies_def setsum_def intro: comm_monoid_add.fold_image_cong)
berghofe@16632
  1047
nipkow@15554
  1048
lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A";
nipkow@28853
  1049
by (rule setsum_cong[OF refl], auto);
nipkow@15554
  1050
nipkow@15402
  1051
lemma setsum_reindex_cong:
nipkow@28853
  1052
   "[|inj_on f A; B = f ` A; !!a. a:A \<Longrightarrow> g a = h (f a)|] 
nipkow@28853
  1053
    ==> setsum h B = setsum g A"
nipkow@28853
  1054
by (simp add: setsum_reindex cong: setsum_cong)
nipkow@15402
  1055
chaieb@29674
  1056
nipkow@15542
  1057
lemma setsum_0[simp]: "setsum (%i. 0) A = 0"
nipkow@15402
  1058
apply (clarsimp simp: setsum_def)
ballarin@15765
  1059
apply (erule finite_induct, auto)
nipkow@15402
  1060
done
nipkow@15402
  1061
nipkow@15543
  1062
lemma setsum_0': "ALL a:A. f a = 0 ==> setsum f A = 0"
nipkow@15543
  1063
by(simp add:setsum_cong)
nipkow@15402
  1064
nipkow@15402
  1065
lemma setsum_Un_Int: "finite A ==> finite B ==>
nipkow@15402
  1066
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
nipkow@15402
  1067
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
nipkow@28853
  1068
by(simp add: setsum_def comm_monoid_add.fold_image_Un_Int [symmetric])
nipkow@15402
  1069
nipkow@15402
  1070
lemma setsum_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1071
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
nipkow@15402
  1072
by (subst setsum_Un_Int [symmetric], auto)
nipkow@15402
  1073
chaieb@29674
  1074
lemma setsum_mono_zero_left: 
chaieb@29674
  1075
  assumes fT: "finite T" and ST: "S \<subseteq> T"
chaieb@29674
  1076
  and z: "\<forall>i \<in> T - S. f i = 0"
chaieb@29674
  1077
  shows "setsum f S = setsum f T"
chaieb@29674
  1078
proof-
chaieb@29674
  1079
  have eq: "T = S \<union> (T - S)" using ST by blast
chaieb@29674
  1080
  have d: "S \<inter> (T - S) = {}" using ST by blast
chaieb@29674
  1081
  from fT ST have f: "finite S" "finite (T - S)" by (auto intro: finite_subset)
chaieb@29674
  1082
  show ?thesis 
chaieb@29674
  1083
  by (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] setsum_0'[OF z])
chaieb@29674
  1084
qed
chaieb@29674
  1085
chaieb@29674
  1086
lemma setsum_mono_zero_right: 
chaieb@29674
  1087
  assumes fT: "finite T" and ST: "S \<subseteq> T"
chaieb@29674
  1088
  and z: "\<forall>i \<in> T - S. f i = 0"
chaieb@29674
  1089
  shows "setsum f T = setsum f S"
chaieb@29674
  1090
using setsum_mono_zero_left[OF fT ST z] by simp
chaieb@29674
  1091
chaieb@29674
  1092
lemma setsum_mono_zero_cong_left: 
chaieb@29674
  1093
  assumes fT: "finite T" and ST: "S \<subseteq> T"
chaieb@29674
  1094
  and z: "\<forall>i \<in> T - S. g i = 0"
chaieb@29674
  1095
  and fg: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
chaieb@29674
  1096
  shows "setsum f S = setsum g T"
chaieb@29674
  1097
proof-
chaieb@29674
  1098
  have eq: "T = S \<union> (T - S)" using ST by blast
chaieb@29674
  1099
  have d: "S \<inter> (T - S) = {}" using ST by blast
chaieb@29674
  1100
  from fT ST have f: "finite S" "finite (T - S)" by (auto intro: finite_subset)
chaieb@29674
  1101
  show ?thesis 
chaieb@29674
  1102
    using fg by (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] setsum_0'[OF z])
chaieb@29674
  1103
qed
chaieb@29674
  1104
chaieb@29674
  1105
lemma setsum_mono_zero_cong_right: 
chaieb@29674
  1106
  assumes fT: "finite T" and ST: "S \<subseteq> T"
chaieb@29674
  1107
  and z: "\<forall>i \<in> T - S. f i = 0"
chaieb@29674
  1108
  and fg: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
chaieb@29674
  1109
  shows "setsum f T = setsum g S"
chaieb@29674
  1110
using setsum_mono_zero_cong_left[OF fT ST z] fg[symmetric] by auto 
chaieb@29674
  1111
chaieb@29674
  1112
lemma setsum_delta: 
chaieb@29674
  1113
  assumes fS: "finite S"
chaieb@29674
  1114
  shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
chaieb@29674
  1115
proof-
chaieb@29674
  1116
  let ?f = "(\<lambda>k. if k=a then b k else 0)"
chaieb@29674
  1117
  {assume a: "a \<notin> S"
chaieb@29674
  1118
    hence "\<forall> k\<in> S. ?f k = 0" by simp
chaieb@29674
  1119
    hence ?thesis  using a by simp}
chaieb@29674
  1120
  moreover 
chaieb@29674
  1121
  {assume a: "a \<in> S"
chaieb@29674
  1122
    let ?A = "S - {a}"
chaieb@29674
  1123
    let ?B = "{a}"
chaieb@29674
  1124
    have eq: "S = ?A \<union> ?B" using a by blast 
chaieb@29674
  1125
    have dj: "?A \<inter> ?B = {}" by simp
chaieb@29674
  1126
    from fS have fAB: "finite ?A" "finite ?B" by auto  
chaieb@29674
  1127
    have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
chaieb@29674
  1128
      using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
chaieb@29674
  1129
      by simp
chaieb@29674
  1130
    then have ?thesis  using a by simp}
chaieb@29674
  1131
  ultimately show ?thesis by blast
chaieb@29674
  1132
qed
chaieb@29674
  1133
lemma setsum_delta': 
chaieb@29674
  1134
  assumes fS: "finite S" shows 
chaieb@29674
  1135
  "setsum (\<lambda>k. if a = k then b k else 0) S = 
chaieb@29674
  1136
     (if a\<in> S then b a else 0)"
chaieb@29674
  1137
  using setsum_delta[OF fS, of a b, symmetric] 
chaieb@29674
  1138
  by (auto intro: setsum_cong)
chaieb@29674
  1139
chaieb@30260
  1140
lemma setsum_restrict_set:
chaieb@30260
  1141
  assumes fA: "finite A"
chaieb@30260
  1142
  shows "setsum f (A \<inter> B) = setsum (\<lambda>x. if x \<in> B then f x else 0) A"
chaieb@30260
  1143
proof-
chaieb@30260
  1144
  from fA have fab: "finite (A \<inter> B)" by auto
chaieb@30260
  1145
  have aba: "A \<inter> B \<subseteq> A" by blast
chaieb@30260
  1146
  let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
chaieb@30260
  1147
  from setsum_mono_zero_left[OF fA aba, of ?g]
chaieb@30260
  1148
  show ?thesis by simp
chaieb@30260
  1149
qed
chaieb@30260
  1150
chaieb@30260
  1151
lemma setsum_cases:
chaieb@30260
  1152
  assumes fA: "finite A"
chaieb@30260
  1153
  shows "setsum (\<lambda>x. if x \<in> B then f x else g x) A =
chaieb@30260
  1154
         setsum f (A \<inter> B) + setsum g (A \<inter> - B)"
chaieb@30260
  1155
proof-
chaieb@30260
  1156
  have a: "A = A \<inter> B \<union> A \<inter> -B" "(A \<inter> B) \<inter> (A \<inter> -B) = {}" 
chaieb@30260
  1157
    by blast+
chaieb@30260
  1158
  from fA 
chaieb@30260
  1159
  have f: "finite (A \<inter> B)" "finite (A \<inter> -B)" by auto
chaieb@30260
  1160
  let ?g = "\<lambda>x. if x \<in> B then f x else g x"
chaieb@30260
  1161
  from setsum_Un_disjoint[OF f a(2), of ?g] a(1)
chaieb@30260
  1162
  show ?thesis by simp
chaieb@30260
  1163
qed
chaieb@30260
  1164
chaieb@29674
  1165
paulson@15409
  1166
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
paulson@15409
  1167
  the lhs need not be, since UNION I A could still be finite.*)
nipkow@15402
  1168
lemma setsum_UN_disjoint:
nipkow@15402
  1169
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1170
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1171
      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
nipkow@28853
  1172
by(simp add: setsum_def comm_monoid_add.fold_image_UN_disjoint cong: setsum_cong)
nipkow@15402
  1173
paulson@15409
  1174
text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
paulson@15409
  1175
directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
nipkow@15402
  1176
lemma setsum_Union_disjoint:
paulson@15409
  1177
  "[| (ALL A:C. finite A);
paulson@15409
  1178
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |]
paulson@15409
  1179
   ==> setsum f (Union C) = setsum (setsum f) C"
paulson@15409
  1180
apply (cases "finite C") 
paulson@15409
  1181
 prefer 2 apply (force dest: finite_UnionD simp add: setsum_def)
nipkow@15402
  1182
  apply (frule setsum_UN_disjoint [of C id f])
paulson@15409
  1183
 apply (unfold Union_def id_def, assumption+)
paulson@15409
  1184
done
nipkow@15402
  1185
paulson@15409
  1186
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
paulson@15409
  1187
  the rhs need not be, since SIGMA A B could still be finite.*)
nipkow@15402
  1188
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
paulson@17189
  1189
    (\<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
  1190
by(simp add:setsum_def comm_monoid_add.fold_image_Sigma split_def cong:setsum_cong)
nipkow@15402
  1191
paulson@15409
  1192
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1193
lemma setsum_cartesian_product: 
paulson@17189
  1194
   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
paulson@15409
  1195
apply (cases "finite A") 
paulson@15409
  1196
 apply (cases "finite B") 
paulson@15409
  1197
  apply (simp add: setsum_Sigma)
paulson@15409
  1198
 apply (cases "A={}", simp)
nipkow@15543
  1199
 apply (simp) 
paulson@15409
  1200
apply (auto simp add: setsum_def
paulson@15409
  1201
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1202
done
nipkow@15402
  1203
nipkow@15402
  1204
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
nipkow@28853
  1205
by(simp add:setsum_def comm_monoid_add.fold_image_distrib)
nipkow@15402
  1206
nipkow@15402
  1207
nipkow@15402
  1208
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1209
nipkow@15402
  1210
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
nipkow@28853
  1211
apply (case_tac "finite A")
nipkow@28853
  1212
 prefer 2 apply (simp add: setsum_def)
nipkow@28853
  1213
apply (erule rev_mp)
nipkow@28853
  1214
apply (erule finite_induct, auto)
nipkow@28853
  1215
done
nipkow@15402
  1216
nipkow@15402
  1217
lemma setsum_eq_0_iff [simp]:
nipkow@15402
  1218
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
nipkow@28853
  1219
by (induct set: finite) auto
nipkow@15402
  1220
nipkow@15402
  1221
lemma setsum_Un_nat: "finite A ==> finite B ==>
nipkow@28853
  1222
  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
  1223
  -- {* For the natural numbers, we have subtraction. *}
nipkow@29667
  1224
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
nipkow@15402
  1225
nipkow@15402
  1226
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@28853
  1227
  (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@28853
  1228
   setsum f A + setsum f B - setsum f (A Int B)"
nipkow@29667
  1229
by (subst setsum_Un_Int [symmetric], auto simp add: algebra_simps)
nipkow@15402
  1230
chaieb@30260
  1231
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
  1232
  apply (induct set: finite)
chaieb@30260
  1233
  apply simp by (auto simp add: fold_image_insert)
chaieb@30260
  1234
chaieb@30260
  1235
lemma (in comm_monoid_mult) fold_image_Un_one:
chaieb@30260
  1236
  assumes fS: "finite S" and fT: "finite T"
chaieb@30260
  1237
  and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
chaieb@30260
  1238
  shows "fold_image (op *) f 1 (S \<union> T) = fold_image (op *) f 1 S * fold_image (op *) f 1 T"
chaieb@30260
  1239
proof-
chaieb@30260
  1240
  have "fold_image op * f 1 (S \<inter> T) = 1" 
chaieb@30260
  1241
    apply (rule fold_image_1)
chaieb@30260
  1242
    using fS fT I0 by auto 
chaieb@30260
  1243
  with fold_image_Un_Int[OF fS fT] show ?thesis by simp
chaieb@30260
  1244
qed
chaieb@30260
  1245
chaieb@30260
  1246
lemma setsum_eq_general_reverses:
chaieb@30260
  1247
  assumes fS: "finite S" and fT: "finite T"
chaieb@30260
  1248
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
chaieb@30260
  1249
  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
  1250
  shows "setsum f S = setsum g T"
chaieb@30260
  1251
  apply (simp add: setsum_def fS fT)
chaieb@30260
  1252
  apply (rule comm_monoid_add.fold_image_eq_general_inverses[OF fS])
chaieb@30260
  1253
  apply (erule kh)
chaieb@30260
  1254
  apply (erule hk)
chaieb@30260
  1255
  done
chaieb@30260
  1256
chaieb@30260
  1257
chaieb@30260
  1258
chaieb@30260
  1259
lemma setsum_Un_zero:  
chaieb@30260
  1260
  assumes fS: "finite S" and fT: "finite T"
chaieb@30260
  1261
  and I0: "\<forall>x \<in> S\<inter>T. f x = 0"
chaieb@30260
  1262
  shows "setsum f (S \<union> T) = setsum f S  + setsum f T"
chaieb@30260
  1263
  using fS fT
chaieb@30260
  1264
  apply (simp add: setsum_def)
chaieb@30260
  1265
  apply (rule comm_monoid_add.fold_image_Un_one)
chaieb@30260
  1266
  using I0 by auto
chaieb@30260
  1267
chaieb@30260
  1268
chaieb@30260
  1269
lemma setsum_UNION_zero: 
chaieb@30260
  1270
  assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
chaieb@30260
  1271
  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
  1272
  shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
chaieb@30260
  1273
  using fSS f0
chaieb@30260
  1274
proof(induct rule: finite_induct[OF fS])
chaieb@30260
  1275
  case 1 thus ?case by simp
chaieb@30260
  1276
next
chaieb@30260
  1277
  case (2 T F)
chaieb@30260
  1278
  then have fTF: "finite T" "\<forall>T\<in>F. finite T" "finite F" and TF: "T \<notin> F" 
chaieb@30260
  1279
    and H: "setsum f (\<Union> F) = setsum (setsum f) F" by (auto simp add: finite_insert)
chaieb@30260
  1280
  from fTF have fUF: "finite (\<Union>F)" by (auto intro: finite_Union)
chaieb@30260
  1281
  from "2.prems" TF fTF
chaieb@30260
  1282
  show ?case 
chaieb@30260
  1283
    by (auto simp add: H[symmetric] intro: setsum_Un_zero[OF fTF(1) fUF, of f])
chaieb@30260
  1284
qed
chaieb@30260
  1285
chaieb@30260
  1286
nipkow@15402
  1287
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
nipkow@28853
  1288
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@28853
  1289
apply (case_tac "finite A")
nipkow@28853
  1290
 prefer 2 apply (simp add: setsum_def)
nipkow@28853
  1291
apply (erule finite_induct)
nipkow@28853
  1292
 apply (auto simp add: insert_Diff_if)
nipkow@28853
  1293
apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@28853
  1294
done
nipkow@15402
  1295
nipkow@15402
  1296
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
  1297
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
  1298
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@28853
  1299
by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@28853
  1300
nipkow@28853
  1301
lemma setsum_diff1'[rule_format]:
nipkow@28853
  1302
  "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
nipkow@28853
  1303
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
  1304
apply (auto simp add: insert_Diff_if add_ac)
nipkow@28853
  1305
done
obua@15552
  1306
nipkow@15402
  1307
(* By Jeremy Siek: *)
nipkow@15402
  1308
nipkow@15402
  1309
lemma setsum_diff_nat: 
nipkow@28853
  1310
assumes "finite B" and "B \<subseteq> A"
nipkow@28853
  1311
shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
nipkow@28853
  1312
using assms
wenzelm@19535
  1313
proof induct
nipkow@15402
  1314
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15402
  1315
next
nipkow@15402
  1316
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15402
  1317
    and xFinA: "insert x F \<subseteq> A"
nipkow@15402
  1318
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15402
  1319
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15402
  1320
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15402
  1321
    by (simp add: setsum_diff1_nat)
nipkow@15402
  1322
  from xFinA have "F \<subseteq> A" by simp
nipkow@15402
  1323
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15402
  1324
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15402
  1325
    by simp
nipkow@15402
  1326
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15402
  1327
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15402
  1328
    by simp
nipkow@15402
  1329
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15402
  1330
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15402
  1331
    by simp
nipkow@15402
  1332
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15402
  1333
qed
nipkow@15402
  1334
nipkow@15402
  1335
lemma setsum_diff:
nipkow@15402
  1336
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
  1337
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
  1338
proof -
nipkow@15402
  1339
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
  1340
  show ?thesis using finiteB le
wenzelm@21575
  1341
  proof induct
wenzelm@19535
  1342
    case empty
wenzelm@19535
  1343
    thus ?case by auto
wenzelm@19535
  1344
  next
wenzelm@19535
  1345
    case (insert x F)
wenzelm@19535
  1346
    thus ?case using le finiteB 
wenzelm@19535
  1347
      by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
  1348
  qed
wenzelm@19535
  1349
qed
nipkow@15402
  1350
nipkow@15402
  1351
lemma setsum_mono:
nipkow@15402
  1352
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
nipkow@15402
  1353
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
  1354
proof (cases "finite K")
nipkow@15402
  1355
  case True
nipkow@15402
  1356
  thus ?thesis using le
wenzelm@19535
  1357
  proof induct
nipkow@15402
  1358
    case empty
nipkow@15402
  1359
    thus ?case by simp
nipkow@15402
  1360
  next
nipkow@15402
  1361
    case insert
wenzelm@19535
  1362
    thus ?case using add_mono by fastsimp
nipkow@15402
  1363
  qed
nipkow@15402
  1364
next
nipkow@15402
  1365
  case False
nipkow@15402
  1366
  thus ?thesis
nipkow@15402
  1367
    by (simp add: setsum_def)
nipkow@15402
  1368
qed
nipkow@15402
  1369
nipkow@15554
  1370
lemma setsum_strict_mono:
wenzelm@19535
  1371
  fixes f :: "'a \<Rightarrow> 'b::{pordered_cancel_ab_semigroup_add,comm_monoid_add}"
wenzelm@19535
  1372
  assumes "finite A"  "A \<noteq> {}"
wenzelm@19535
  1373
    and "!!x. x:A \<Longrightarrow> f x < g x"
wenzelm@19535
  1374
  shows "setsum f A < setsum g A"
wenzelm@19535
  1375
  using prems
nipkow@15554
  1376
proof (induct rule: finite_ne_induct)
nipkow@15554
  1377
  case singleton thus ?case by simp
nipkow@15554
  1378
next
nipkow@15554
  1379
  case insert thus ?case by (auto simp: add_strict_mono)
nipkow@15554
  1380
qed
nipkow@15554
  1381
nipkow@15535
  1382
lemma setsum_negf:
wenzelm@19535
  1383
  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
nipkow@15535
  1384
proof (cases "finite A")
berghofe@22262
  1385
  case True thus ?thesis by (induct set: finite) auto
nipkow@15535
  1386
next
nipkow@15535
  1387
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1388
qed
nipkow@15402
  1389
nipkow@15535
  1390
lemma setsum_subtractf:
wenzelm@19535
  1391
  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
wenzelm@19535
  1392
    setsum f A - setsum g A"
nipkow@15535
  1393
proof (cases "finite A")
nipkow@15535
  1394
  case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15535
  1395
next
nipkow@15535
  1396
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1397
qed
nipkow@15402
  1398
nipkow@15535
  1399
lemma setsum_nonneg:
wenzelm@19535
  1400
  assumes nn: "\<forall>x\<in>A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
wenzelm@19535
  1401
  shows "0 \<le> setsum f A"
nipkow@15535
  1402
proof (cases "finite A")
nipkow@15535
  1403
  case True thus ?thesis using nn
wenzelm@21575
  1404
  proof induct
wenzelm@19535
  1405
    case empty then show ?case by simp
wenzelm@19535
  1406
  next
wenzelm@19535
  1407
    case (insert x F)
wenzelm@19535
  1408
    then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
wenzelm@19535
  1409
    with insert show ?case by simp
wenzelm@19535
  1410
  qed
nipkow@15535
  1411
next
nipkow@15535
  1412
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1413
qed
nipkow@15402
  1414
nipkow@15535
  1415
lemma setsum_nonpos:
wenzelm@19535
  1416
  assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})"
wenzelm@19535
  1417
  shows "setsum f A \<le> 0"
nipkow@15535
  1418
proof (cases "finite A")
nipkow@15535
  1419
  case True thus ?thesis using np
wenzelm@21575
  1420
  proof induct
wenzelm@19535
  1421
    case empty then show ?case by simp
wenzelm@19535
  1422
  next
wenzelm@19535
  1423
    case (insert x F)
wenzelm@19535
  1424
    then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
wenzelm@19535
  1425
    with insert show ?case by simp
wenzelm@19535
  1426
  qed
nipkow@15535
  1427
next
nipkow@15535
  1428
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1429
qed
nipkow@15402
  1430
nipkow@15539
  1431
lemma setsum_mono2:
nipkow@15539
  1432
fixes f :: "'a \<Rightarrow> 'b :: {pordered_ab_semigroup_add_imp_le,comm_monoid_add}"
nipkow@15539
  1433
assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
nipkow@15539
  1434
shows "setsum f A \<le> setsum f B"
nipkow@15539
  1435
proof -
nipkow@15539
  1436
  have "setsum f A \<le> setsum f A + setsum f (B-A)"
nipkow@15539
  1437
    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
nipkow@15539
  1438
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15539
  1439
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15539
  1440
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15539
  1441
  finally show ?thesis .
nipkow@15539
  1442
qed
nipkow@15542
  1443
avigad@16775
  1444
lemma setsum_mono3: "finite B ==> A <= B ==> 
avigad@16775
  1445
    ALL x: B - A. 
avigad@16775
  1446
      0 <= ((f x)::'a::{comm_monoid_add,pordered_ab_semigroup_add}) ==>
avigad@16775
  1447
        setsum f A <= setsum f B"
avigad@16775
  1448
  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")
avigad@16775
  1449
  apply (erule ssubst)
avigad@16775
  1450
  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")
avigad@16775
  1451
  apply simp
avigad@16775
  1452
  apply (rule add_left_mono)
avigad@16775
  1453
  apply (erule setsum_nonneg)
avigad@16775
  1454
  apply (subst setsum_Un_disjoint [THEN sym])
avigad@16775
  1455
  apply (erule finite_subset, assumption)
avigad@16775
  1456
  apply (rule finite_subset)
avigad@16775
  1457
  prefer 2
avigad@16775
  1458
  apply assumption
avigad@16775
  1459
  apply auto
avigad@16775
  1460
  apply (rule setsum_cong)
avigad@16775
  1461
  apply auto
avigad@16775
  1462
done
avigad@16775
  1463
ballarin@19279
  1464
lemma setsum_right_distrib: 
huffman@22934
  1465
  fixes f :: "'a => ('b::semiring_0)"
nipkow@15402
  1466
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
  1467
proof (cases "finite A")
nipkow@15402
  1468
  case True
nipkow@15402
  1469
  thus ?thesis
wenzelm@21575
  1470
  proof induct
nipkow@15402
  1471
    case empty thus ?case by simp
nipkow@15402
  1472
  next
nipkow@15402
  1473
    case (insert x A) thus ?case by (simp add: right_distrib)
nipkow@15402
  1474
  qed
nipkow@15402
  1475
next
nipkow@15402
  1476
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1477
qed
nipkow@15402
  1478
ballarin@17149
  1479
lemma setsum_left_distrib:
huffman@22934
  1480
  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
ballarin@17149
  1481
proof (cases "finite A")
ballarin@17149
  1482
  case True
ballarin@17149
  1483
  then show ?thesis
ballarin@17149
  1484
  proof induct
ballarin@17149
  1485
    case empty thus ?case by simp
ballarin@17149
  1486
  next
ballarin@17149
  1487
    case (insert x A) thus ?case by (simp add: left_distrib)
ballarin@17149
  1488
  qed
ballarin@17149
  1489
next
ballarin@17149
  1490
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
  1491
qed
ballarin@17149
  1492
ballarin@17149
  1493
lemma setsum_divide_distrib:
ballarin@17149
  1494
  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
ballarin@17149
  1495
proof (cases "finite A")
ballarin@17149
  1496
  case True
ballarin@17149
  1497
  then show ?thesis
ballarin@17149
  1498
  proof induct
ballarin@17149
  1499
    case empty thus ?case by simp
ballarin@17149
  1500
  next
ballarin@17149
  1501
    case (insert x A) thus ?case by (simp add: add_divide_distrib)
ballarin@17149
  1502
  qed
ballarin@17149
  1503
next
ballarin@17149
  1504
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
  1505
qed
ballarin@17149
  1506
nipkow@15535
  1507
lemma setsum_abs[iff]: 
haftmann@25303
  1508
  fixes f :: "'a => ('b::pordered_ab_group_add_abs)"
nipkow@15402
  1509
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15535
  1510
proof (cases "finite A")
nipkow@15535
  1511
  case True
nipkow@15535
  1512
  thus ?thesis
wenzelm@21575
  1513
  proof induct
nipkow@15535
  1514
    case empty thus ?case by simp
nipkow@15535
  1515
  next
nipkow@15535
  1516
    case (insert x A)
nipkow@15535
  1517
    thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15535
  1518
  qed
nipkow@15402
  1519
next
nipkow@15535
  1520
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1521
qed
nipkow@15402
  1522
nipkow@15535
  1523
lemma setsum_abs_ge_zero[iff]: 
haftmann@25303
  1524
  fixes f :: "'a => ('b::pordered_ab_group_add_abs)"
nipkow@15402
  1525
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15535
  1526
proof (cases "finite A")
nipkow@15535
  1527
  case True
nipkow@15535
  1528
  thus ?thesis
wenzelm@21575
  1529
  proof induct
nipkow@15535
  1530
    case empty thus ?case by simp
nipkow@15535
  1531
  next
nipkow@21733
  1532
    case (insert x A) thus ?case by (auto simp: add_nonneg_nonneg)
nipkow@15535
  1533
  qed
nipkow@15402
  1534
next
nipkow@15535
  1535
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1536
qed
nipkow@15402
  1537
nipkow@15539
  1538
lemma abs_setsum_abs[simp]: 
haftmann@25303
  1539
  fixes f :: "'a => ('b::pordered_ab_group_add_abs)"
nipkow@15539
  1540
  shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
nipkow@15539
  1541
proof (cases "finite A")
nipkow@15539
  1542
  case True
nipkow@15539
  1543
  thus ?thesis
wenzelm@21575
  1544
  proof induct
nipkow@15539
  1545
    case empty thus ?case by simp
nipkow@15539
  1546
  next
nipkow@15539
  1547
    case (insert a A)
nipkow@15539
  1548
    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
  1549
    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
  1550
    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
avigad@16775
  1551
      by (simp del: abs_of_nonneg)
nipkow@15539
  1552
    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
nipkow@15539
  1553
    finally show ?case .
nipkow@15539
  1554
  qed
nipkow@15539
  1555
next
nipkow@15539
  1556
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15539
  1557
qed
nipkow@15539
  1558
nipkow@15402
  1559
ballarin@17149
  1560
text {* Commuting outer and inner summation *}
ballarin@17149
  1561
ballarin@17149
  1562
lemma swap_inj_on:
ballarin@17149
  1563
  "inj_on (%(i, j). (j, i)) (A \<times> B)"
ballarin@17149
  1564
  by (unfold inj_on_def) fast
ballarin@17149
  1565
ballarin@17149
  1566
lemma swap_product:
ballarin@17149
  1567
  "(%(i, j). (j, i)) ` (A \<times> B) = B \<times> A"
ballarin@17149
  1568
  by (simp add: split_def image_def) blast
ballarin@17149
  1569
ballarin@17149
  1570
lemma setsum_commute:
ballarin@17149
  1571
  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
ballarin@17149
  1572
proof (simp add: setsum_cartesian_product)
paulson@17189
  1573
  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
paulson@17189
  1574
    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
ballarin@17149
  1575
    (is "?s = _")
ballarin@17149
  1576
    apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on)
ballarin@17149
  1577
    apply (simp add: split_def)
ballarin@17149
  1578
    done
paulson@17189
  1579
  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
ballarin@17149
  1580
    (is "_ = ?t")
ballarin@17149
  1581
    apply (simp add: swap_product)
ballarin@17149
  1582
    done
ballarin@17149
  1583
  finally show "?s = ?t" .
ballarin@17149
  1584
qed
ballarin@17149
  1585
ballarin@19279
  1586
lemma setsum_product:
huffman@22934
  1587
  fixes f :: "'a => ('b::semiring_0)"
ballarin@19279
  1588
  shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
ballarin@19279
  1589
  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute)
ballarin@19279
  1590
ballarin@17149
  1591
nipkow@15402
  1592
subsection {* Generalized product over a set *}
nipkow@15402
  1593
nipkow@28853
  1594
definition setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
nipkow@28853
  1595
where "setprod f A == if finite A then fold_image (op *) f 1 A else 1"
nipkow@15402
  1596
wenzelm@19535
  1597
abbreviation
wenzelm@21404
  1598
  Setprod  ("\<Prod>_" [1000] 999) where
wenzelm@19535
  1599
  "\<Prod>A == setprod (%x. x) A"
wenzelm@19535
  1600
nipkow@15402
  1601
syntax
paulson@17189
  1602
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
nipkow@15402
  1603
syntax (xsymbols)
paulson@17189
  1604
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1605
syntax (HTML output)
paulson@17189
  1606
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@16550
  1607
nipkow@16550
  1608
translations -- {* Beware of argument permutation! *}
nipkow@28853
  1609
  "PROD i:A. b" == "CONST setprod (%i. b) A" 
nipkow@28853
  1610
  "\<Prod>i\<in>A. b" == "CONST setprod (%i. b) A" 
nipkow@16550
  1611
nipkow@16550
  1612
text{* Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter
nipkow@16550
  1613
 @{text"\<Prod>x|P. e"}. *}
nipkow@16550
  1614
nipkow@16550
  1615
syntax
paulson@17189
  1616
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)
nipkow@16550
  1617
syntax (xsymbols)
paulson@17189
  1618
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1619
syntax (HTML output)
paulson@17189
  1620
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1621
nipkow@15402
  1622
translations
nipkow@28853
  1623
  "PROD x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@28853
  1624
  "\<Prod>x|P. t" => "CONST setprod (%x. t) {x. P}"
nipkow@16550
  1625
nipkow@15402
  1626
nipkow@15402
  1627
lemma setprod_empty [simp]: "setprod f {} = 1"
nipkow@28853
  1628
by (auto simp add: setprod_def)
nipkow@15402
  1629
nipkow@15402
  1630
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
nipkow@15402
  1631
    setprod f (insert a A) = f a * setprod f A"
nipkow@28853
  1632
by (simp add: setprod_def)
nipkow@15402
  1633
paulson@15409
  1634
lemma setprod_infinite [simp]: "~ finite A ==> setprod f A = 1"
nipkow@28853
  1635
by (simp add: setprod_def)
paulson@15409
  1636
nipkow@15402
  1637
lemma setprod_reindex:
nipkow@28853
  1638
   "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
nipkow@28853
  1639
by(auto simp: setprod_def fold_image_reindex dest!:finite_imageD)
nipkow@15402
  1640
nipkow@15402
  1641
lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
nipkow@15402
  1642
by (auto simp add: setprod_reindex)
nipkow@15402
  1643
nipkow@15402
  1644
lemma setprod_cong:
nipkow@15402
  1645
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
nipkow@28853
  1646
by(fastsimp simp: setprod_def intro: fold_image_cong)
nipkow@15402
  1647
berghofe@16632
  1648
lemma strong_setprod_cong:
berghofe@16632
  1649
  "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"
nipkow@28853
  1650
by(fastsimp simp: simp_implies_def setprod_def intro: fold_image_cong)
berghofe@16632
  1651
nipkow@15402
  1652
lemma setprod_reindex_cong: "inj_on f A ==>
nipkow@15402
  1653
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
nipkow@28853
  1654
by (frule setprod_reindex, simp)
nipkow@15402
  1655
chaieb@29674
  1656
lemma strong_setprod_reindex_cong: assumes i: "inj_on f A"
chaieb@29674
  1657
  and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x"
chaieb@29674
  1658
  shows "setprod h B = setprod g A"
chaieb@29674
  1659
proof-
chaieb@29674
  1660
    have "setprod h B = setprod (h o f) A"
chaieb@29674
  1661
      by (simp add: B setprod_reindex[OF i, of h])
chaieb@29674
  1662
    then show ?thesis apply simp
chaieb@29674
  1663
      apply (rule setprod_cong)
chaieb@29674
  1664
      apply simp
chaieb@29674
  1665
      by (erule eq[symmetric])
chaieb@29674
  1666
qed
chaieb@29674
  1667
chaieb@30260
  1668
lemma setprod_Un_one:  
chaieb@30260
  1669
  assumes fS: "finite S" and fT: "finite T"
chaieb@30260
  1670
  and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
chaieb@30260
  1671
  shows "setprod f (S \<union> T) = setprod f S  * setprod f T"
chaieb@30260
  1672
  using fS fT
chaieb@30260
  1673
  apply (simp add: setprod_def)
chaieb@30260
  1674
  apply (rule fold_image_Un_one)
chaieb@30260
  1675
  using I0 by auto
chaieb@30260
  1676
nipkow@15402
  1677
nipkow@15402
  1678
lemma setprod_1: "setprod (%i. 1) A = 1"
nipkow@28853
  1679
apply (case_tac "finite A")
nipkow@28853
  1680
apply (erule finite_induct, auto simp add: mult_ac)
nipkow@28853
  1681
done
nipkow@15402
  1682
nipkow@15402
  1683
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
nipkow@28853
  1684
apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
nipkow@28853
  1685
apply (erule ssubst, rule setprod_1)
nipkow@28853
  1686
apply (rule setprod_cong, auto)
nipkow@28853
  1687
done
nipkow@15402
  1688
nipkow@15402
  1689
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
  1690
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
nipkow@28853
  1691
by(simp add: setprod_def fold_image_Un_Int[symmetric])
nipkow@15402
  1692
nipkow@15402
  1693
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1694
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
nipkow@15402
  1695
by (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1696
chaieb@29674
  1697
lemma setprod_delta: 
chaieb@29674
  1698
  assumes fS: "finite S"
chaieb@29674
  1699
  shows "setprod (\<lambda>k. if k=a then b k else 1) S = (if a \<in> S then b a else 1)"
chaieb@29674
  1700
proof-
chaieb@29674
  1701
  let ?f = "(\<lambda>k. if k=a then b k else 1)"
chaieb@29674
  1702
  {assume a: "a \<notin> S"
chaieb@29674
  1703
    hence "\<forall> k\<in> S. ?f k = 1" by simp
chaieb@29674
  1704
    hence ?thesis  using a by (simp add: setprod_1 cong add: setprod_cong) }
chaieb@29674
  1705
  moreover 
chaieb@29674
  1706
  {assume a: "a \<in> S"
chaieb@29674
  1707
    let ?A = "S - {a}"
chaieb@29674
  1708
    let ?B = "{a}"
chaieb@29674
  1709
    have eq: "S = ?A \<union> ?B" using a by blast 
chaieb@29674
  1710
    have dj: "?A \<inter> ?B = {}" by simp
chaieb@29674
  1711
    from fS have fAB: "finite ?A" "finite ?B" by auto  
chaieb@29674
  1712
    have fA1: "setprod ?f ?A = 1" apply (rule setprod_1') by auto
chaieb@29674
  1713
    have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
chaieb@29674
  1714
      using setprod_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
chaieb@29674
  1715
      by simp
chaieb@29674
  1716
    then have ?thesis  using a by (simp add: fA1 cong add: setprod_cong cong del: if_weak_cong)}
chaieb@29674
  1717
  ultimately show ?thesis by blast
chaieb@29674
  1718
qed
chaieb@29674
  1719
chaieb@29674
  1720
lemma setprod_delta': 
chaieb@29674
  1721
  assumes fS: "finite S" shows 
chaieb@29674
  1722
  "setprod (\<lambda>k. if a = k then b k else 1) S = 
chaieb@29674
  1723
     (if a\<in> S then b a else 1)"
chaieb@29674
  1724
  using setprod_delta[OF fS, of a b, symmetric] 
chaieb@29674
  1725
  by (auto intro: setprod_cong)
chaieb@29674
  1726
chaieb@29674
  1727
nipkow@15402
  1728
lemma setprod_UN_disjoint:
nipkow@15402
  1729
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1730
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1731
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
nipkow@28853
  1732
by(simp add: setprod_def fold_image_UN_disjoint cong: setprod_cong)
nipkow@15402
  1733
nipkow@15402
  1734
lemma setprod_Union_disjoint:
paulson@15409
  1735
  "[| (ALL A:C. finite A);
paulson@15409
  1736
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |] 
paulson@15409
  1737
   ==> setprod f (Union C) = setprod (setprod f) C"
paulson@15409
  1738
apply (cases "finite C") 
paulson@15409
  1739
 prefer 2 apply (force dest: finite_UnionD simp add: setprod_def)
nipkow@15402
  1740
  apply (frule setprod_UN_disjoint [of C id f])
paulson@15409
  1741
 apply (unfold Union_def id_def, assumption+)
paulson@15409
  1742
done
nipkow@15402
  1743
nipkow@15402
  1744
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@16550
  1745
    (\<Prod>x\<in>A. (\<Prod>y\<in> B x. f x y)) =
paulson@17189
  1746
    (\<Prod>(x,y)\<in>(SIGMA x:A. B x). f x y)"
nipkow@28853
  1747
by(simp add:setprod_def fold_image_Sigma split_def cong:setprod_cong)
nipkow@15402
  1748
paulson@15409
  1749
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1750
lemma setprod_cartesian_product: 
paulson@17189
  1751
     "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
paulson@15409
  1752
apply (cases "finite A") 
paulson@15409
  1753
 apply (cases "finite B") 
paulson@15409
  1754
  apply (simp add: setprod_Sigma)
paulson@15409
  1755
 apply (cases "A={}", simp)
paulson@15409
  1756
 apply (simp add: setprod_1) 
paulson@15409
  1757
apply (auto simp add: setprod_def
paulson@15409
  1758
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1759
done
nipkow@15402
  1760
nipkow@15402
  1761
lemma setprod_timesf:
paulson@15409
  1762
     "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
nipkow@28853
  1763
by(simp add:setprod_def fold_image_distrib)
nipkow@15402
  1764
nipkow@15402
  1765
nipkow@15402
  1766
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1767
nipkow@15402
  1768
lemma setprod_eq_1_iff [simp]:
nipkow@28853
  1769
  "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
nipkow@28853
  1770
by (induct set: finite) auto
nipkow@15402
  1771
nipkow@15402
  1772
lemma setprod_zero:
huffman@23277
  1773
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
nipkow@28853
  1774
apply (induct set: finite, force, clarsimp)
nipkow@28853
  1775
apply (erule disjE, auto)
nipkow@28853
  1776
done
nipkow@15402
  1777
nipkow@15402
  1778
lemma setprod_nonneg [rule_format]:
huffman@30841
  1779
   "(ALL x: A. (0::'a::ordered_semidom) \<le> f x) --> 0 \<le> setprod f A"
huffman@30841
  1780
by (cases "finite A", induct set: finite, simp_all add: mult_nonneg_nonneg)
huffman@30841
  1781
huffman@30841
  1782
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_semidom) < f x)
nipkow@28853
  1783
  --> 0 < setprod f A"
huffman@30841
  1784
by (cases "finite A", induct set: finite, simp_all add: mult_pos_pos)
nipkow@15402
  1785
nipkow@15402
  1786
lemma setprod_nonzero [rule_format]:
nipkow@28853
  1787
  "(ALL x y. (x::'a::comm_semiring_1) * y = 0 --> x = 0 | y = 0) ==>
nipkow@28853
  1788
    finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
nipkow@28853
  1789
by (erule finite_induct, auto)
nipkow@15402
  1790
nipkow@15402
  1791
lemma setprod_zero_eq:
huffman@23277
  1792
    "(ALL x y. (x::'a::comm_semiring_1) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1793
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
nipkow@28853
  1794
by (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
nipkow@15402
  1795
nipkow@15402
  1796
lemma setprod_nonzero_field:
huffman@23277
  1797
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::idom)) ==> setprod f A \<noteq> 0"
nipkow@28853
  1798
by (rule setprod_nonzero, auto)
nipkow@15402
  1799
nipkow@15402
  1800
lemma setprod_zero_eq_field:
huffman@23277
  1801
    "finite A ==> (setprod f A = (0::'a::idom)) = (EX x: A. f x = 0)"
nipkow@28853
  1802
by (rule setprod_zero_eq, auto)
nipkow@15402
  1803
nipkow@15402
  1804
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
nipkow@28853
  1805
  (setprod f (A Un B) :: 'a ::{field})
nipkow@28853
  1806
   = setprod f A * setprod f B / setprod f (A Int B)"
nipkow@28853
  1807
apply (subst setprod_Un_Int [symmetric], auto)
nipkow@28853
  1808
apply (subgoal_tac "finite (A Int B)")
nipkow@28853
  1809
apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
nipkow@28853
  1810
apply (subst times_divide_eq_right [THEN sym], auto)
nipkow@28853
  1811
done
nipkow@15402
  1812
nipkow@15402
  1813
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@28853
  1814
  (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@28853
  1815
  (if a:A then setprod f A / f a else setprod f A)"
nipkow@23413
  1816
by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
  1817
nipkow@15402
  1818
lemma setprod_inversef: "finite A ==>
nipkow@28853
  1819
  ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
nipkow@28853
  1820
  setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@28853
  1821
by (erule finite_induct) auto
nipkow@15402
  1822
nipkow@15402
  1823
lemma setprod_dividef:
nipkow@28853
  1824
   "[|finite A;
nipkow@28853
  1825
      \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
nipkow@28853
  1826
    ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@28853
  1827
apply (subgoal_tac
nipkow@15402
  1828
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@28853
  1829
apply (erule ssubst)
nipkow@28853
  1830
apply (subst divide_inverse)
nipkow@28853
  1831
apply (subst setprod_timesf)
nipkow@28853
  1832
apply (subst setprod_inversef, assumption+, rule refl)
nipkow@28853
  1833
apply (rule setprod_cong, rule refl)
nipkow@28853
  1834
apply (subst divide_inverse, auto)
nipkow@28853
  1835
done
nipkow@28853
  1836
nipkow@29925
  1837
lemma setprod_dvd_setprod [rule_format]: 
nipkow@29925
  1838
    "(ALL x : A. f x dvd g x) \<longrightarrow> setprod f A dvd setprod g A"
nipkow@29925
  1839
  apply (cases "finite A")
nipkow@29925
  1840
  apply (induct set: finite)
nipkow@29925
  1841
  apply (auto simp add: dvd_def)
nipkow@29925
  1842
  apply (rule_tac x = "k * ka" in exI)
nipkow@29925
  1843
  apply (simp add: algebra_simps)
nipkow@29925
  1844
done
nipkow@29925
  1845
nipkow@29925
  1846
lemma setprod_dvd_setprod_subset:
nipkow@29925
  1847
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> setprod f A dvd setprod f B"
nipkow@29925
  1848
  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")
nipkow@29925
  1849
  apply (unfold dvd_def, blast)
nipkow@29925
  1850
  apply (subst setprod_Un_disjoint [symmetric])
nipkow@29925
  1851
  apply (auto elim: finite_subset intro: setprod_cong)
nipkow@29925
  1852
done
nipkow@29925
  1853
nipkow@29925
  1854
lemma setprod_dvd_setprod_subset2:
nipkow@29925
  1855
  "finite B \<Longrightarrow> A <= B \<Longrightarrow> ALL x : A. (f x::'a::comm_semiring_1) dvd g x \<Longrightarrow> 
nipkow@29925
  1856
      setprod f A dvd setprod g B"
nipkow@29925
  1857
  apply (rule dvd_trans)
nipkow@29925
  1858
  apply (rule setprod_dvd_setprod, erule (1) bspec)
nipkow@29925
  1859
  apply (erule (1) setprod_dvd_setprod_subset)
nipkow@29925
  1860
done
nipkow@29925
  1861
nipkow@29925
  1862
lemma dvd_setprod: "finite A \<Longrightarrow> i:A \<Longrightarrow> 
nipkow@29925
  1863
    (f i ::'a::comm_semiring_1) dvd setprod f A"
nipkow@29925
  1864
by (induct set: finite) (auto intro: dvd_mult)
nipkow@29925
  1865
nipkow@29925
  1866
lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) \<longrightarrow> 
nipkow@29925
  1867
    (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"
nipkow@29925
  1868
  apply (cases "finite A")
nipkow@29925
  1869
  apply (induct set: finite)
nipkow@29925
  1870
  apply auto
nipkow@29925
  1871
done
nipkow@29925
  1872
nipkow@15402
  1873
wenzelm@12396
  1874
subsection {* Finite cardinality *}
wenzelm@12396
  1875
nipkow@15402
  1876
text {* This definition, although traditional, is ugly to work with:
nipkow@15402
  1877
@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
nipkow@15402
  1878
But now that we have @{text setsum} things are easy:
wenzelm@12396
  1879
*}
wenzelm@12396
  1880
nipkow@28853
  1881
definition card :: "'a set \<Rightarrow> nat"
nipkow@28853
  1882
where "card A = setsum (\<lambda>x. 1) A"
wenzelm@12396
  1883
wenzelm@12396
  1884
lemma card_empty [simp]: "card {} = 0"
nipkow@24853
  1885
by (simp add: card_def)
nipkow@15402
  1886
paulson@24427
  1887
lemma card_infinite [simp]: "~ finite A ==> card A = 0"
nipkow@24853
  1888
by (simp add: card_def)
paulson@15409
  1889
nipkow@15402
  1890
lemma card_eq_setsum: "card A = setsum (%x. 1) A"
nipkow@15402
  1891
by (simp add: card_def)
wenzelm@12396
  1892
wenzelm@12396
  1893
lemma card_insert_disjoint [simp]:
wenzelm@12396
  1894
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
ballarin@15765
  1895
by(simp add: card_def)
nipkow@15402
  1896
nipkow@15402
  1897
lemma card_insert_if:
nipkow@28853
  1898
  "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
nipkow@28853
  1899
by (simp add: insert_absorb)
wenzelm@12396
  1900
paulson@24286
  1901
lemma card_0_eq [simp,noatp]: "finite A ==> (card A = 0) = (A = {})"
nipkow@28853
  1902
apply auto
nipkow@28853
  1903
apply (drule_tac a = x in mk_disjoint_insert, clarify, auto)
nipkow@28853
  1904
done
wenzelm@12396
  1905
paulson@15409
  1906
lemma card_eq_0_iff: "(card A = 0) = (A = {} | ~ finite A)"
paulson@15409
  1907
by auto
paulson@15409
  1908
nipkow@24853
  1909
wenzelm@12396
  1910
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
  1911
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
  1912
apply(simp del:insert_Diff_single)
nipkow@14302
  1913
done
wenzelm@12396
  1914
wenzelm@12396
  1915
lemma card_Diff_singleton:
nipkow@24853
  1916
  "finite A ==> x: A ==> card (A - {x}) = card A - 1"
nipkow@24853
  1917
by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
  1918
wenzelm@12396
  1919
lemma card_Diff_singleton_if:
nipkow@24853
  1920
  "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
nipkow@24853
  1921
by (simp add: card_Diff_singleton)
nipkow@24853
  1922
nipkow@24853
  1923
lemma card_Diff_insert[simp]:
nipkow@24853
  1924
assumes "finite A" and "a:A" and "a ~: B"
nipkow@24853
  1925
shows "card(A - insert a B) = card(A - B) - 1"
nipkow@24853
  1926
proof -
nipkow@24853
  1927
  have "A - insert a B = (A - B) - {a}" using assms by blast
nipkow@24853
  1928
  then show ?thesis using assms by(simp add:card_Diff_singleton)
nipkow@24853
  1929
qed
wenzelm@12396
  1930
wenzelm@12396
  1931
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
nipkow@24853
  1932
by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert)
wenzelm@12396
  1933
wenzelm@12396
  1934
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
nipkow@24853
  1935
by (simp add: card_insert_if)
wenzelm@12396
  1936
nipkow@15402
  1937
lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
nipkow@15539
  1938
by (simp add: card_def setsum_mono2)
nipkow@15402
  1939
wenzelm@12396
  1940
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
nipkow@28853
  1941
apply (induct set: finite, simp, clarify)
nipkow@28853
  1942
apply (subgoal_tac "finite A & A - {x} <= F")
nipkow@28853
  1943
 prefer 2 apply (blast intro: finite_subset, atomize)
nipkow@28853
  1944
apply (drule_tac x = "A - {x}" in spec)
nipkow@28853
  1945
apply (simp add: card_Diff_singleton_if split add: split_if_asm)
nipkow@28853
  1946
apply (case_tac "card A", auto)
nipkow@28853
  1947
done
wenzelm@12396
  1948
wenzelm@12396
  1949
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
berghofe@26792
  1950
apply (simp add: psubset_eq linorder_not_le [symmetric])
nipkow@24853
  1951
apply (blast dest: card_seteq)
nipkow@24853
  1952
done
wenzelm@12396
  1953
wenzelm@12396
  1954
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
  1955
    ==> card A + card B = card (A Un B) + card (A Int B)"
nipkow@15402
  1956
by(simp add:card_def setsum_Un_Int)
wenzelm@12396
  1957
wenzelm@12396
  1958
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
  1959
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
nipkow@24853
  1960
by (simp add: card_Un_Int)
wenzelm@12396
  1961
wenzelm@12396
  1962
lemma card_Diff_subset:
nipkow@15402
  1963
  "finite B ==> B <= A ==> card (A - B) = card A - card B"
nipkow@15402
  1964
by(simp add:card_def setsum_diff_nat)
wenzelm@12396
  1965
wenzelm@12396
  1966
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
nipkow@28853
  1967
apply (rule Suc_less_SucD)
nipkow@28853
  1968
apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
nipkow@28853
  1969
done
wenzelm@12396
  1970
wenzelm@12396
  1971
lemma card_Diff2_less:
nipkow@28853
  1972
  "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
nipkow@28853
  1973
apply (case_tac "x = y")
nipkow@28853
  1974
 apply (simp add: card_Diff1_less del:card_Diff_insert)
nipkow@28853
  1975
apply (rule less_trans)
nipkow@28853
  1976
 prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
nipkow@28853
  1977
done
wenzelm@12396
  1978
wenzelm@12396
  1979
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
nipkow@28853
  1980
apply (case_tac "x : A")
nipkow@28853
  1981
 apply (simp_all add: card_Diff1_less less_imp_le)
nipkow@28853
  1982
done
wenzelm@12396
  1983
wenzelm@12396
  1984
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
  1985
by (erule psubsetI, blast)
wenzelm@12396
  1986
paulson@14889
  1987
lemma insert_partition:
nipkow@15402
  1988
  "\<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
  1989
  \<Longrightarrow> x \<inter> \<Union> F = {}"
paulson@14889
  1990
by auto
paulson@14889
  1991
paulson@19793
  1992
text{* main cardinality theorem *}
paulson@14889
  1993
lemma card_partition [rule_format]:
nipkow@28853
  1994
  "finite C ==>
nipkow@28853
  1995
     finite (\<Union> C) -->
nipkow@28853
  1996
     (\<forall>c\<in>C. card c = k) -->
nipkow@28853
  1997
     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
nipkow@28853
  1998
     k * card(C) = card (\<Union> C)"
paulson@14889
  1999
apply (erule finite_induct, simp)
paulson@14889
  2000
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
  2001
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
  2002
done
paulson@14889
  2003
wenzelm@12396
  2004
paulson@19793
  2005
text{*The form of a finite set of given cardinality*}
paulson@19793
  2006
paulson@19793
  2007
lemma card_eq_SucD:
nipkow@24853
  2008
assumes "card A = Suc k"
nipkow@24853
  2009
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
paulson@19793
  2010
proof -
nipkow@24853
  2011
  have fin: "finite A" using assms by (auto intro: ccontr)
nipkow@24853
  2012
  moreover have "card A \<noteq> 0" using assms by auto
nipkow@24853
  2013
  ultimately obtain b where b: "b \<in> A" by auto
paulson@19793
  2014
  show ?thesis
paulson@19793
  2015
  proof (intro exI conjI)
paulson@19793
  2016
    show "A = insert b (A-{b})" using b by blast
paulson@19793
  2017
    show "b \<notin> A - {b}" by blast
nipkow@24853
  2018
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
nipkow@24853
  2019
      using assms b fin by(fastsimp dest:mk_disjoint_insert)+
paulson@19793
  2020
  qed
paulson@19793
  2021
qed
paulson@19793
  2022
paulson@19793
  2023
lemma card_Suc_eq:
nipkow@24853
  2024
  "(card A = Suc k) =
nipkow@24853
  2025
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
nipkow@24853
  2026
apply(rule iffI)
nipkow@24853
  2027
 apply(erule card_eq_SucD)
nipkow@24853
  2028
apply(auto)
nipkow@24853
  2029
apply(subst card_insert)
nipkow@24853
  2030
 apply(auto intro:ccontr)
nipkow@24853
  2031
done
paulson@19793
  2032
nipkow@15539
  2033
lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat(card A) * y"
nipkow@15539
  2034
apply (cases "finite A")
nipkow@15539
  2035
apply (erule finite_induct)
nipkow@29667
  2036
apply (auto simp add: algebra_simps)
paulson@15409
  2037
done
nipkow@15402
  2038
krauss@21199
  2039
lemma setprod_constant: "finite A ==> (\<Prod>x\<in> A. (y::'a::{recpower, comm_monoid_mult})) = y^(card A)"
nipkow@28853
  2040
apply (erule finite_induct)
nipkow@28853
  2041
apply (auto simp add: power_Suc)
nipkow@28853
  2042
done
nipkow@15402
  2043
chaieb@29674
  2044
lemma setprod_gen_delta:
chaieb@29674
  2045
  assumes fS: "finite S"
chaieb@29674
  2046
  shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::{comm_monoid_mult, recpower}) * c^ (card S - 1) else c^ card S)"
chaieb@29674
  2047
proof-
chaieb@29674
  2048
  let ?f = "(\<lambda>k. if k=a then b k else c)"
chaieb@29674
  2049
  {assume a: "a \<notin> S"
chaieb@29674
  2050
    hence "\<forall> k\<in> S. ?f k = c" by simp
chaieb@29674
  2051
    hence ?thesis  using a setprod_constant[OF fS, of c] by (simp add: setprod_1 cong add: setprod_cong) }
chaieb@29674
  2052
  moreover 
chaieb@29674
  2053
  {assume a: "a \<in> S"
chaieb@29674
  2054
    let ?A = "S - {a}"
chaieb@29674
  2055
    let ?B = "{a}"
chaieb@29674
  2056
    have eq: "S = ?A \<union> ?B" using a by blast 
chaieb@29674
  2057
    have dj: "?A \<inter> ?B = {}" by simp
chaieb@29674
  2058
    from fS have fAB: "finite ?A" "finite ?B" by auto  
chaieb@29674
  2059
    have fA0:"setprod ?f ?A = setprod (\<lambda>i. c) ?A"
chaieb@29674
  2060
      apply (rule setprod_cong) by auto
chaieb@29674
  2061
    have cA: "card ?A = card S - 1" using fS a by auto
chaieb@29674
  2062
    have fA1: "setprod ?f ?A = c ^ card ?A"  unfolding fA0 apply (rule setprod_constant) using fS by auto
chaieb@29674
  2063
    have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
chaieb@29674
  2064
      using setprod_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
chaieb@29674
  2065
      by simp
chaieb@29674
  2066
    then have ?thesis using a cA
chaieb@29674
  2067
      by (simp add: fA1 ring_simps cong add: setprod_cong cong del: if_weak_cong)}
chaieb@29674
  2068
  ultimately show ?thesis by blast
chaieb@29674
  2069
qed
chaieb@29674
  2070
chaieb@29674
  2071
nipkow@15542
  2072
lemma setsum_bounded:
huffman@23277
  2073
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, pordered_ab_semigroup_add})"
nipkow@15542
  2074
  shows "setsum f A \<le> of_nat(card A) * K"
nipkow@15542
  2075
proof (cases "finite A")
nipkow@15542
  2076
  case True
nipkow@15542
  2077
  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
nipkow@15542
  2078
next
nipkow@15542
  2079
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15542
  2080
qed
nipkow@15542
  2081
nipkow@15402
  2082
nipkow@15402
  2083
subsubsection {* Cardinality of unions *}
nipkow@15402
  2084
nipkow@15402
  2085
lemma card_UN_disjoint:
nipkow@28853
  2086
  "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@28853
  2087
   (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {})
nipkow@28853
  2088
   ==> card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
nipkow@28853
  2089
apply (simp add: card_def del: setsum_constant)
nipkow@28853
  2090
apply (subgoal_tac
nipkow@28853
  2091
         "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
nipkow@28853
  2092
apply (simp add: setsum_UN_disjoint del: setsum_constant)
nipkow@28853
  2093
apply (simp cong: setsum_cong)
nipkow@28853
  2094
done
nipkow@15402
  2095
nipkow@15402
  2096
lemma card_Union_disjoint:
nipkow@15402
  2097
  "finite C ==> (ALL A:C. finite A) ==>
nipkow@28853
  2098
   (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {})
nipkow@28853
  2099
   ==> card (Union C) = setsum card C"
nipkow@28853
  2100
apply (frule card_UN_disjoint [of C id])
nipkow@28853
  2101
apply (unfold Union_def id_def, assumption+)
nipkow@28853
  2102
done
nipkow@28853
  2103
nipkow@15402
  2104
wenzelm@12396
  2105
subsubsection {* Cardinality of image *}
wenzelm@12396
  2106
nipkow@28853
  2107
text{*The image of a finite set can be expressed using @{term fold_image}.*}
nipkow@28853
  2108
lemma image_eq_fold_image:
nipkow@28853
  2109
  "finite A ==> f ` A = fold_image (op Un) (%x. {f x}) {} A"
haftmann@26041
  2110
proof (induct rule: finite_induct)
haftmann@26041
  2111
  case empty then show ?case by simp
haftmann@26041
  2112
next
haftmann@29509
  2113
  interpret ab_semigroup_mult "op Un"
haftmann@28823
  2114
    proof qed auto
haftmann@26041
  2115
  case insert 
haftmann@26041
  2116
  then show ?case by simp
haftmann@26041
  2117
qed
paulson@15447
  2118
wenzelm@12396
  2119
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
nipkow@28853
  2120
apply (induct set: finite)
nipkow@28853
  2121
 apply simp
nipkow@28853
  2122
apply (simp add: le_SucI finite_imageI card_insert_if)
nipkow@28853
  2123
done
wenzelm@12396
  2124
nipkow@15402
  2125
lemma card_image: "inj_on f A ==> card (f ` A) = card A"
nipkow@15539
  2126
by(simp add:card_def setsum_reindex o_def del:setsum_constant)
wenzelm@12396
  2127
wenzelm@12396
  2128
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
nipkow@25162
  2129
by (simp add: card_seteq card_image)
wenzelm@12396
  2130
nipkow@15111
  2131
lemma eq_card_imp_inj_on:
nipkow@15111
  2132
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
wenzelm@21575
  2133
apply (induct rule:finite_induct)
wenzelm@21575
  2134
apply simp
nipkow@15111
  2135
apply(frule card_image_le[where f = f])
nipkow@15111
  2136
apply(simp add:card_insert_if split:if_splits)
nipkow@15111
  2137
done
nipkow@15111
  2138
nipkow@15111
  2139
lemma inj_on_iff_eq_card:
nipkow@15111
  2140
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
nipkow@15111
  2141
by(blast intro: card_image eq_card_imp_inj_on)
nipkow@15111
  2142
wenzelm@12396
  2143
nipkow@15402
  2144
lemma card_inj_on_le:
nipkow@28853
  2145
  "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
nipkow@15402
  2146
apply (subgoal_tac "finite A") 
nipkow@15402
  2147
 apply (force intro: card_mono simp add: card_image [symmetric])
nipkow@15402
  2148
apply (blast intro: finite_imageD dest: finite_subset) 
nipkow@15402
  2149
done
nipkow@15402
  2150
nipkow@15402
  2151
lemma card_bij_eq:
nipkow@28853
  2152
  "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
nipkow@28853
  2153
     finite A; finite B |] ==> card A = card B"
nipkow@28853
  2154
by (auto intro: le_anti_sym card_inj_on_le)
nipkow@15402
  2155
nipkow@15402
  2156
nipkow@15402
  2157
subsubsection {* Cardinality of products *}
nipkow@15402
  2158
nipkow@15402
  2159
(*
nipkow@15402
  2160
lemma SigmaI_insert: "y \<notin> A ==>
nipkow@15402
  2161
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
nipkow@15402
  2162
  by auto
nipkow@15402
  2163
*)
nipkow@15402
  2164
nipkow@15402
  2165
lemma card_SigmaI [simp]:
nipkow@15402
  2166
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
nipkow@15402
  2167
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
nipkow@15539
  2168
by(simp add:card_def setsum_Sigma del:setsum_constant)
nipkow@15402
  2169
paulson@15409
  2170
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
paulson@15409
  2171
apply (cases "finite A") 
paulson@15409
  2172
apply (cases "finite B") 
paulson@15409
  2173
apply (auto simp add: card_eq_0_iff
nipkow@15539
  2174
            dest: finite_cartesian_productD1 finite_cartesian_productD2)
paulson@15409
  2175
done
nipkow@15402
  2176
nipkow@15402
  2177
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
nipkow@15539
  2178
by (simp add: card_cartesian_product)
paulson@15409
  2179
nipkow@15402
  2180
huffman@29025
  2181
subsubsection {* Cardinality of sums *}
huffman@29025
  2182
huffman@29025
  2183
lemma card_Plus:
huffman@29025
  2184
  assumes "finite A" and "finite B"
huffman@29025
  2185
  shows "card (A <+> B) = card A + card B"
huffman@29025
  2186
proof -
huffman@29025
  2187
  have "Inl`A \<inter> Inr`B = {}" by fast
huffman@29025
  2188
  with assms show ?thesis
huffman@29025
  2189
    unfolding Plus_def
huffman@29025
  2190
    by (simp add: card_Un_disjoint card_image)
huffman@29025
  2191
qed
huffman@29025
  2192
nipkow@15402
  2193
wenzelm@12396
  2194
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
  2195
wenzelm@12396
  2196
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
nipkow@28853
  2197
apply (induct set: finite)
nipkow@28853
  2198
 apply (simp_all add: Pow_insert)
nipkow@28853
  2199
apply (subst card_Un_disjoint, blast)
nipkow@28853
  2200
  apply (blast intro: finite_imageI, blast)
nipkow@28853
  2201
apply (subgoal_tac "inj_on (insert x) (Pow F)")
nipkow@28853
  2202
 apply (simp add: card_image Pow_insert)
nipkow@28853
  2203
apply (unfold inj_on_def)
nipkow@28853
  2204
apply (blast elim!: equalityE)
nipkow@28853
  2205
done
wenzelm@12396
  2206
haftmann@24342
  2207
text {* Relates to equivalence classes.  Based on a theorem of F. Kammüller.  *}
wenzelm@12396
  2208
wenzelm@12396
  2209
lemma dvd_partition:
nipkow@15392
  2210
  "finite (Union C) ==>
wenzelm@12396
  2211
    ALL c : C. k dvd card c ==>
paulson@14430
  2212
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
  2213
  k dvd card (Union C)"
nipkow@15392
  2214
apply(frule finite_UnionD)
nipkow@15392
  2215
apply(rotate_tac -1)
nipkow@28853
  2216
apply (induct set: finite, simp_all, clarify)
nipkow@28853
  2217
apply (subst card_Un_disjoint)
nipkow@28853
  2218
   apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
nipkow@28853
  2219
done
wenzelm@12396
  2220
wenzelm@12396
  2221
nipkow@25162
  2222
subsubsection {* Relating injectivity and surjectivity *}
nipkow@25162
  2223
nipkow@25162
  2224
lemma finite_surj_inj: "finite(A) \<Longrightarrow> A <= f`A \<Longrightarrow> inj_on f A"
nipkow@25162
  2225
apply(rule eq_card_imp_inj_on, assumption)
nipkow@25162
  2226
apply(frule finite_imageI)
nipkow@25162
  2227
apply(drule (1) card_seteq)
nipkow@28853
  2228
 apply(erule card_image_le)
nipkow@25162
  2229
apply simp
nipkow@25162
  2230
done
nipkow@25162
  2231
nipkow@25162
  2232
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
nipkow@25162
  2233
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
nipkow@25162
  2234
by (blast intro: finite_surj_inj subset_UNIV dest:surj_range)
nipkow@25162
  2235
nipkow@25162
  2236
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
nipkow@25162
  2237
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
nipkow@25162
  2238
by(fastsimp simp:surj_def dest!: endo_inj_surj)
nipkow@25162
  2239
nipkow@25162
  2240
corollary infinite_UNIV_nat: "~finite(UNIV::nat set)"
nipkow@25162
  2241
proof
nipkow@25162
  2242
  assume "finite(UNIV::nat set)"
nipkow@25162
  2243
  with finite_UNIV_inj_surj[of Suc]
nipkow@25162
  2244
  show False by simp (blast dest: Suc_neq_Zero surjD)
nipkow@25162
  2245
qed
nipkow@25162
  2246
nipkow@29879
  2247
lemma infinite_UNIV_char_0:
nipkow@29879
  2248
  "\<not> finite (UNIV::'a::semiring_char_0 set)"
nipkow@29879
  2249
proof
nipkow@29879
  2250
  assume "finite (UNIV::'a set)"
nipkow@29879
  2251
  with subset_UNIV have "finite (range of_nat::'a set)"
nipkow@29879
  2252
    by (rule finite_subset)
nipkow@29879
  2253
  moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
nipkow@29879
  2254
    by (simp add: inj_on_def)
nipkow@29879
  2255
  ultimately have "finite (UNIV::nat set)"
nipkow@29879
  2256
    by (rule finite_imageD)
nipkow@29879
  2257
  then show "False"
nipkow@29879
  2258
    by (simp add: infinite_UNIV_nat)
nipkow@29879
  2259
qed
nipkow@25162
  2260
nipkow@15392
  2261
subsection{* A fold functional for non-empty sets *}
nipkow@15392
  2262
nipkow@15392
  2263
text{* Does not require start value. *}
wenzelm@12396
  2264
berghofe@23736
  2265
inductive
berghofe@22262
  2266
  fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
berghofe@22262
  2267
  for f :: "'a => 'a => 'a"
berghofe@22262
  2268
where
paulson@15506
  2269
  fold1Set_insertI [intro]:
nipkow@28853
  2270
   "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
wenzelm@12396
  2271
nipkow@15392
  2272
constdefs
nipkow@15392
  2273
  fold1 :: "('a => 'a => 'a) => 'a set => 'a"
berghofe@22262
  2274
  "fold1 f A == THE x. fold1Set f A x"
paulson@15506
  2275
paulson@15506
  2276
lemma fold1Set_nonempty:
haftmann@22917
  2277
  "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
nipkow@28853
  2278
by(erule fold1Set.cases, simp_all)
nipkow@15392
  2279
berghofe@23736
  2280
inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
berghofe@23736
  2281
berghofe@23736
  2282
inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
berghofe@22262
  2283
berghofe@22262
  2284
berghofe@22262
  2285
lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
nipkow@28853
  2286
by (blast intro: fold_graph.intros elim: fold_graph.cases)
nipkow@15392
  2287
haftmann@22917
  2288
lemma fold1_singleton [simp]: "fold1 f {a} = a"
nipkow@28853
  2289
by (unfold fold1_def) blast
wenzelm@12396
  2290
paulson@15508
  2291
lemma finite_nonempty_imp_fold1Set:
berghofe@22262
  2292
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
paulson@15508
  2293
apply (induct A rule: finite_induct)
nipkow@28853
  2294
apply (auto dest: finite_imp_fold_graph [of _ f])
paulson@15508
  2295
done
paulson@15506
  2296
nipkow@28853
  2297
text{*First, some lemmas about @{const fold_graph}.*}
nipkow@15392
  2298
haftmann@26041
  2299
context ab_semigroup_mult
haftmann@26041
  2300
begin
haftmann@26041
  2301
nipkow@28853
  2302
lemma fun_left_comm: "fun_left_comm(op *)"
nipkow@28853
  2303
by unfold_locales (simp add: mult_ac)
nipkow@28853
  2304
nipkow@28853
  2305
lemma fold_graph_insert_swap:
nipkow@28853
  2306
assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
nipkow@28853
  2307
shows "fold_graph times z (insert b A) (z * y)"
nipkow@28853
  2308
proof -
ballarin@29223
  2309
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  2310
from assms show ?thesis
nipkow@28853
  2311
proof (induct rule: fold_graph.induct)
haftmann@26041
  2312
  case emptyI thus ?case by (force simp add: fold_insert_aux mult_commute)
paulson@15508
  2313
next
berghofe@22262
  2314
  case (insertI x A y)
nipkow@28853
  2315
    have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
paulson@15521
  2316
      using insertI by force  --{*how does @{term id} get unfolded?*}
haftmann@26041
  2317
    thus ?case by (simp add: insert_commute mult_ac)
paulson@15508
  2318
qed
nipkow@28853
  2319
qed
nipkow@28853
  2320
nipkow@28853
  2321
lemma fold_graph_permute_diff:
nipkow@28853
  2322
assumes fold: "fold_graph times b A x"
nipkow@28853
  2323
shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
paulson@15508
  2324
using fold
nipkow@28853
  2325
proof (induct rule: fold_graph.induct)
paulson@15508
  2326
  case emptyI thus ?case by simp
paulson@15508
  2327
next
berghofe@22262
  2328
  case (insertI x A y)
paulson@15521
  2329
  have "a = x \<or> a \<in> A" using insertI by simp
paulson@15521
  2330
  thus ?case
paulson@15521
  2331
  proof
paulson@15521
  2332
    assume "a = x"
paulson@15521
  2333
    with insertI show ?thesis
nipkow@28853
  2334
      by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
paulson@15521
  2335
  next
paulson@15521
  2336
    assume ainA: "a \<in> A"
nipkow@28853
  2337
    hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
nipkow@28853
  2338
      using insertI by force
paulson@15521
  2339
    moreover
paulson@15521
  2340
    have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
paulson@15521
  2341
      using ainA insertI by blast
nipkow@28853
  2342
    ultimately show ?thesis by simp
paulson@15508
  2343
  qed
paulson@15508
  2344
qed
paulson@15508
  2345
haftmann@26041
  2346
lemma fold1_eq_fold:
nipkow@28853
  2347
assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
nipkow@28853
  2348
proof -
ballarin@29223
  2349
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  2350
  from assms show ?thesis
nipkow@28853
  2351
apply (simp add: fold1_def fold_def)
paulson@15508
  2352
apply (rule the_equality)
nipkow@28853
  2353
apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
paulson@15508
  2354
apply (rule sym, clarify)
paulson@15508
  2355
apply (case_tac "Aa=A")
nipkow@28853
  2356
 apply (best intro: the_equality fold_graph_determ)
nipkow@28853
  2357
apply (subgoal_tac "fold_graph times a A x")
nipkow@28853
  2358
 apply (best intro: the_equality fold_graph_determ)
nipkow@28853
  2359
apply (subgoal_tac "insert aa (Aa - {a}) = A")
nipkow@28853
  2360
 prefer 2 apply (blast elim: equalityE)
nipkow@28853
  2361
apply (auto dest: fold_graph_permute_diff [where a=a])
paulson@15508
  2362
done
nipkow@28853
  2363
qed
paulson@15508
  2364
paulson@15521
  2365
lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
paulson@15521
  2366
apply safe
nipkow@28853
  2367
 apply simp
nipkow@28853
  2368
 apply (drule_tac x=x in spec)
nipkow@28853
  2369
 apply (drule_tac x="A-{x}" in spec, auto)
paulson@15508
  2370
done
paulson@15508
  2371
haftmann@26041
  2372
lemma fold1_insert:
paulson@15521
  2373
  assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
haftmann@26041
  2374
  shows "fold1 times (insert x A) = x * fold1 times A"
paulson@15521
  2375
proof -
ballarin@29223
  2376
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  2377
  from nonempty obtain a A' where "A = insert a A' & a ~: A'"
paulson@15521
  2378
    by (auto simp add: nonempty_iff)
paulson@15521
  2379
  with A show ?thesis
nipkow@28853
  2380
    by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
paulson@15521
  2381
qed
paulson@15521
  2382
haftmann@26041
  2383
end
haftmann@26041
  2384
haftmann@26041
  2385
context ab_semigroup_idem_mult
haftmann@26041
  2386
begin
haftmann@26041
  2387
nipkow@28853
  2388
lemma fun_left_comm_idem: "fun_left_comm_idem(op *)"
nipkow@28853
  2389
apply unfold_locales
nipkow@28853
  2390
 apply (simp add: mult_ac)
nipkow@28853
  2391
apply (simp add: mult_idem mult_assoc[symmetric])
nipkow@28853
  2392
done
nipkow@28853
  2393
nipkow@28853
  2394
haftmann@26041
  2395
lemma fold1_insert_idem [simp]:
paulson@15521
  2396
  assumes nonempty: "A \<noteq> {}" and A: "finite A" 
haftmann@26041
  2397
  shows "fold1 times (insert x A) = x * fold1 times A"
paulson@15521
  2398
proof -
ballarin@29223
  2399
  interpret fun_left_comm_idem "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a"
nipkow@28853
  2400
    by (rule fun_left_comm_idem)
nipkow@28853
  2401
  from nonempty obtain a A' where A': "A = insert a A' & a ~: A'"
paulson@15521
  2402
    by (auto simp add: nonempty_iff)
paulson@15521
  2403
  show ?thesis
paulson@15521
  2404
  proof cases
paulson@15521
  2405
    assume "a = x"
nipkow@28853
  2406
    thus ?thesis
paulson@15521
  2407
    proof cases
paulson@15521
  2408
      assume "A' = {}"
nipkow@28853
  2409
      with prems show ?thesis by (simp add: mult_idem)
paulson@15521
  2410
    next
paulson@15521
  2411
      assume "A' \<noteq> {}"
paulson@15521
  2412
      with prems show ?thesis
nipkow@28853
  2413
	by (simp add: fold1_insert mult_assoc [symmetric] mult_idem)
paulson@15521
  2414
    qed
paulson@15521
  2415
  next
paulson@15521
  2416
    assume "a \<noteq> x"
paulson@15521
  2417
    with prems show ?thesis
paulson@15521
  2418
      by (simp add: insert_commute fold1_eq_fold fold_insert_idem)
paulson@15521
  2419
  qed
paulson@15521
  2420
qed
paulson@15506
  2421
haftmann@26041
  2422
lemma hom_fold1_commute:
haftmann@26041
  2423
assumes hom: "!!x y. h (x * y) = h x * h y"
haftmann@26041
  2424
and N: "finite N" "N \<noteq> {}" shows "h (fold1 times N) = fold1 times (h ` N)"
haftmann@22917
  2425
using N proof (induct rule: finite_ne_induct)
haftmann@22917
  2426
  case singleton thus ?case by simp
haftmann@22917
  2427
next
haftmann@22917
  2428
  case (insert n N)
haftmann@26041
  2429
  then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp
haftmann@26041
  2430
  also have "\<dots> = h n * h (fold1 times N)" by(rule hom)
haftmann@26041
  2431
  also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert)
haftmann@26041
  2432
  also have "times (h n) \<dots> = fold1 times (insert (h n) (h ` N))"
haftmann@22917
  2433
    using insert by(simp)
haftmann@22917
  2434
  also have "insert (h n) (h ` N) = h ` insert n N" by simp
haftmann@22917
  2435
  finally show ?case .
haftmann@22917
  2436
qed
haftmann@22917
  2437
haftmann@26041
  2438
end
haftmann@26041
  2439
paulson@15506
  2440
paulson@15508
  2441
text{* Now the recursion rules for definitions: *}
paulson@15508
  2442
haftmann@22917
  2443
lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
paulson@15508
  2444
by(simp add:fold1_singleton)
paulson@15508
  2445
haftmann@26041
  2446
lemma (in ab_semigroup_mult) fold1_insert_def:
haftmann@26041
  2447
  "\<lbrakk> g = fold1 times; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
haftmann@26041
  2448
by (simp add:fold1_insert)
haftmann@26041
  2449
haftmann@26041
  2450
lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def:
haftmann@26041
  2451
  "\<lbrakk> g = fold1 times; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
haftmann@26041
  2452
by simp
paulson@15508
  2453
paulson@15508
  2454
subsubsection{* Determinacy for @{term fold1Set} *}
paulson@15508
  2455
nipkow@28853
  2456
(*Not actually used!!*)
nipkow@28853
  2457
(*
haftmann@26041
  2458
context ab_semigroup_mult