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