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