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