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