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