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