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