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