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