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