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