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