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