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