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