src/HOL/Finite_Set.thy
author paulson
Tue Dec 14 10:45:16 2004 +0100 (2004-12-14)
changeset 15409 a063687d24eb
parent 15402 97204f3b4705
child 15447 177ffdbabf80
permissions -rw-r--r--
new and stronger lemmas and improved simplification for finite sets
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
paulson@14430
     4
                Additions by Jeremy Avigad in Feb 2004
nipkow@15376
     5
nipkow@15402
     6
Get rid of a couple of superfluous finiteness assumptions in lemmas
nipkow@15402
     7
about setsum and card - see FIXME.
nipkow@15402
     8
NB: the analogous lemmas for setprod should also be simplified!
wenzelm@12396
     9
*)
wenzelm@12396
    10
wenzelm@12396
    11
header {* Finite sets *}
wenzelm@12396
    12
nipkow@15131
    13
theory Finite_Set
nipkow@15140
    14
imports Divides Power Inductive
nipkow@15131
    15
begin
wenzelm@12396
    16
nipkow@15392
    17
subsection {* Definition and basic properties *}
wenzelm@12396
    18
wenzelm@12396
    19
consts Finites :: "'a set set"
nipkow@13737
    20
syntax
nipkow@13737
    21
  finite :: "'a set => bool"
nipkow@13737
    22
translations
nipkow@13737
    23
  "finite A" == "A : Finites"
wenzelm@12396
    24
wenzelm@12396
    25
inductive Finites
wenzelm@12396
    26
  intros
wenzelm@12396
    27
    emptyI [simp, intro!]: "{} : Finites"
wenzelm@12396
    28
    insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
wenzelm@12396
    29
wenzelm@12396
    30
axclass finite \<subseteq> type
wenzelm@12396
    31
  finite: "finite UNIV"
wenzelm@12396
    32
nipkow@13737
    33
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    34
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    35
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    36
proof -
wenzelm@14661
    37
  from prems have "A \<noteq> UNIV" by blast
wenzelm@14661
    38
  thus ?thesis by blast
wenzelm@14661
    39
qed
wenzelm@12396
    40
wenzelm@12396
    41
lemma finite_induct [case_names empty insert, induct set: Finites]:
wenzelm@12396
    42
  "finite F ==>
nipkow@15327
    43
    P {} ==> (!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    44
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    45
proof -
wenzelm@13421
    46
  assume "P {}" and
nipkow@15327
    47
    insert: "!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    48
  assume "finite F"
wenzelm@12396
    49
  thus "P F"
wenzelm@12396
    50
  proof induct
wenzelm@12396
    51
    show "P {}" .
nipkow@15327
    52
    fix x F assume F: "finite F" and P: "P F"
wenzelm@12396
    53
    show "P (insert x F)"
wenzelm@12396
    54
    proof cases
wenzelm@12396
    55
      assume "x \<in> F"
wenzelm@12396
    56
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    57
      with P show ?thesis by (simp only:)
wenzelm@12396
    58
    next
wenzelm@12396
    59
      assume "x \<notin> F"
wenzelm@12396
    60
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    61
    qed
wenzelm@12396
    62
  qed
wenzelm@12396
    63
qed
wenzelm@12396
    64
wenzelm@12396
    65
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@12396
    66
  "finite F ==> F \<subseteq> A ==>
nipkow@15327
    67
    P {} ==> (!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
wenzelm@12396
    68
    P F"
wenzelm@12396
    69
proof -
wenzelm@13421
    70
  assume "P {}" and insert:
nipkow@15327
    71
    "!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@12396
    72
  assume "finite F"
wenzelm@12396
    73
  thus "F \<subseteq> A ==> P F"
wenzelm@12396
    74
  proof induct
wenzelm@12396
    75
    show "P {}" .
nipkow@15327
    76
    fix x F assume "finite F" and "x \<notin> F"
wenzelm@12396
    77
      and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    78
    show "P (insert x F)"
wenzelm@12396
    79
    proof (rule insert)
wenzelm@12396
    80
      from i show "x \<in> A" by blast
wenzelm@12396
    81
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    82
      with P show "P F" .
wenzelm@12396
    83
    qed
wenzelm@12396
    84
  qed
wenzelm@12396
    85
qed
wenzelm@12396
    86
nipkow@15392
    87
text{* Finite sets are the images of initial segments of natural numbers: *}
nipkow@15392
    88
nipkow@15392
    89
lemma finite_imp_nat_seg_image:
nipkow@15392
    90
assumes fin: "finite A" shows "\<exists> (n::nat) f. A = f ` {i::nat. i<n}"
nipkow@15392
    91
using fin
nipkow@15392
    92
proof induct
nipkow@15392
    93
  case empty
nipkow@15392
    94
  show ?case
nipkow@15392
    95
  proof show "\<exists>f. {} = f ` {i::nat. i < 0}" by(simp add:image_def) qed
nipkow@15392
    96
next
nipkow@15392
    97
  case (insert a A)
nipkow@15392
    98
  from insert.hyps obtain n f where "A = f ` {i::nat. i < n}" by blast
nipkow@15392
    99
  hence "insert a A = (%i. if i<n then f i else a) ` {i. i < n+1}"
nipkow@15392
   100
    by (auto simp add:image_def Ball_def)
nipkow@15392
   101
  thus ?case by blast
nipkow@15392
   102
qed
nipkow@15392
   103
nipkow@15392
   104
lemma nat_seg_image_imp_finite:
nipkow@15392
   105
  "!!f A. A = f ` {i::nat. i<n} \<Longrightarrow> finite A"
nipkow@15392
   106
proof (induct n)
nipkow@15392
   107
  case 0 thus ?case by simp
nipkow@15392
   108
next
nipkow@15392
   109
  case (Suc n)
nipkow@15392
   110
  let ?B = "f ` {i. i < n}"
nipkow@15392
   111
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
nipkow@15392
   112
  show ?case
nipkow@15392
   113
  proof cases
nipkow@15392
   114
    assume "\<exists>k<n. f n = f k"
nipkow@15392
   115
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   116
    thus ?thesis using finB by simp
nipkow@15392
   117
  next
nipkow@15392
   118
    assume "\<not>(\<exists> k<n. f n = f k)"
nipkow@15392
   119
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   120
    thus ?thesis using finB by simp
nipkow@15392
   121
  qed
nipkow@15392
   122
qed
nipkow@15392
   123
nipkow@15392
   124
lemma finite_conv_nat_seg_image:
nipkow@15392
   125
  "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
nipkow@15392
   126
by(blast intro: finite_imp_nat_seg_image nat_seg_image_imp_finite)
nipkow@15392
   127
nipkow@15392
   128
subsubsection{* Finiteness and set theoretic constructions *}
nipkow@15392
   129
wenzelm@12396
   130
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
wenzelm@12396
   131
  -- {* The union of two finite sets is finite. *}
wenzelm@12396
   132
  by (induct set: Finites) simp_all
wenzelm@12396
   133
wenzelm@12396
   134
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
   135
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
   136
proof -
wenzelm@12396
   137
  assume "finite B"
wenzelm@12396
   138
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
   139
  proof induct
wenzelm@12396
   140
    case empty
wenzelm@12396
   141
    thus ?case by simp
wenzelm@12396
   142
  next
nipkow@15327
   143
    case (insert x F A)
wenzelm@12396
   144
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
wenzelm@12396
   145
    show "finite A"
wenzelm@12396
   146
    proof cases
wenzelm@12396
   147
      assume x: "x \<in> A"
wenzelm@12396
   148
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   149
      with r have "finite (A - {x})" .
wenzelm@12396
   150
      hence "finite (insert x (A - {x}))" ..
wenzelm@12396
   151
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
wenzelm@12396
   152
      finally show ?thesis .
wenzelm@12396
   153
    next
wenzelm@12396
   154
      show "A \<subseteq> F ==> ?thesis" .
wenzelm@12396
   155
      assume "x \<notin> A"
wenzelm@12396
   156
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   157
    qed
wenzelm@12396
   158
  qed
wenzelm@12396
   159
qed
wenzelm@12396
   160
wenzelm@12396
   161
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
wenzelm@12396
   162
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
wenzelm@12396
   163
wenzelm@12396
   164
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   165
  -- {* The converse obviously fails. *}
wenzelm@12396
   166
  by (blast intro: finite_subset)
wenzelm@12396
   167
wenzelm@12396
   168
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   169
  apply (subst insert_is_Un)
paulson@14208
   170
  apply (simp only: finite_Un, blast)
wenzelm@12396
   171
  done
wenzelm@12396
   172
nipkow@15281
   173
lemma finite_Union[simp, intro]:
nipkow@15281
   174
 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
nipkow@15281
   175
by (induct rule:finite_induct) simp_all
nipkow@15281
   176
wenzelm@12396
   177
lemma finite_empty_induct:
wenzelm@12396
   178
  "finite A ==>
wenzelm@12396
   179
  P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
wenzelm@12396
   180
proof -
wenzelm@12396
   181
  assume "finite A"
wenzelm@12396
   182
    and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@12396
   183
  have "P (A - A)"
wenzelm@12396
   184
  proof -
wenzelm@12396
   185
    fix c b :: "'a set"
wenzelm@12396
   186
    presume c: "finite c" and b: "finite b"
wenzelm@12396
   187
      and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@12396
   188
    from c show "c \<subseteq> b ==> P (b - c)"
wenzelm@12396
   189
    proof induct
wenzelm@12396
   190
      case empty
wenzelm@12396
   191
      from P1 show ?case by simp
wenzelm@12396
   192
    next
nipkow@15327
   193
      case (insert x F)
wenzelm@12396
   194
      have "P (b - F - {x})"
wenzelm@12396
   195
      proof (rule P2)
wenzelm@12396
   196
        from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@12396
   197
        from insert show "x \<in> b - F" by simp
wenzelm@12396
   198
        from insert show "P (b - F)" by simp
wenzelm@12396
   199
      qed
wenzelm@12396
   200
      also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@12396
   201
      finally show ?case .
wenzelm@12396
   202
    qed
wenzelm@12396
   203
  next
wenzelm@12396
   204
    show "A \<subseteq> A" ..
wenzelm@12396
   205
  qed
wenzelm@12396
   206
  thus "P {}" by simp
wenzelm@12396
   207
qed
wenzelm@12396
   208
wenzelm@12396
   209
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
wenzelm@12396
   210
  by (rule Diff_subset [THEN finite_subset])
wenzelm@12396
   211
wenzelm@12396
   212
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   213
  apply (subst Diff_insert)
wenzelm@12396
   214
  apply (case_tac "a : A - B")
wenzelm@12396
   215
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   216
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   217
  done
wenzelm@12396
   218
wenzelm@12396
   219
nipkow@15392
   220
text {* Image and Inverse Image over Finite Sets *}
paulson@13825
   221
paulson@13825
   222
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   223
  -- {* The image of a finite set is finite. *}
paulson@13825
   224
  by (induct set: Finites) simp_all
paulson@13825
   225
paulson@14430
   226
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   227
  apply (frule finite_imageI)
paulson@14430
   228
  apply (erule finite_subset, assumption)
paulson@14430
   229
  done
paulson@14430
   230
paulson@13825
   231
lemma finite_range_imageI:
paulson@13825
   232
    "finite (range g) ==> finite (range (%x. f (g x)))"
paulson@14208
   233
  apply (drule finite_imageI, simp)
paulson@13825
   234
  done
paulson@13825
   235
wenzelm@12396
   236
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   237
proof -
wenzelm@12396
   238
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   239
  fix B :: "'a set"
wenzelm@12396
   240
  assume "finite B"
wenzelm@12396
   241
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   242
    apply induct
wenzelm@12396
   243
     apply simp
wenzelm@12396
   244
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   245
     apply clarify
wenzelm@12396
   246
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   247
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   248
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   249
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   250
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   251
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   252
    done
wenzelm@12396
   253
qed (rule refl)
wenzelm@12396
   254
wenzelm@12396
   255
paulson@13825
   256
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   257
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   258
         is included in a singleton. *}
paulson@14430
   259
  apply (auto simp add: inj_on_def)
paulson@14430
   260
  apply (blast intro: the_equality [symmetric])
paulson@13825
   261
  done
paulson@13825
   262
paulson@13825
   263
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   264
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   265
         is finite. *}
paulson@14430
   266
  apply (induct set: Finites, simp_all)
paulson@14430
   267
  apply (subst vimage_insert)
paulson@14430
   268
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
paulson@13825
   269
  done
paulson@13825
   270
paulson@13825
   271
nipkow@15392
   272
text {* The finite UNION of finite sets *}
wenzelm@12396
   273
wenzelm@12396
   274
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
wenzelm@12396
   275
  by (induct set: Finites) simp_all
wenzelm@12396
   276
wenzelm@12396
   277
text {*
wenzelm@12396
   278
  Strengthen RHS to
paulson@14430
   279
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   280
wenzelm@12396
   281
  We'd need to prove
paulson@14430
   282
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   283
  by induction. *}
wenzelm@12396
   284
wenzelm@12396
   285
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
wenzelm@12396
   286
  by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   287
wenzelm@12396
   288
nipkow@15392
   289
text {* Sigma of finite sets *}
wenzelm@12396
   290
wenzelm@12396
   291
lemma finite_SigmaI [simp]:
wenzelm@12396
   292
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   293
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   294
nipkow@15402
   295
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
nipkow@15402
   296
    finite (A <*> B)"
nipkow@15402
   297
  by (rule finite_SigmaI)
nipkow@15402
   298
wenzelm@12396
   299
lemma finite_Prod_UNIV:
wenzelm@12396
   300
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   301
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   302
   apply (erule ssubst)
paulson@14208
   303
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   304
  done
wenzelm@12396
   305
paulson@15409
   306
lemma finite_cartesian_productD1:
paulson@15409
   307
     "[| finite (A <*> B); B \<noteq> {} |] ==> finite A"
paulson@15409
   308
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   309
apply (drule_tac x=n in spec) 
paulson@15409
   310
apply (drule_tac x="fst o f" in spec) 
paulson@15409
   311
apply (auto simp add: o_def) 
paulson@15409
   312
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   313
apply (drule equalityD1) 
paulson@15409
   314
apply (rename_tac y x)
paulson@15409
   315
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   316
 prefer 2 apply force
paulson@15409
   317
apply clarify
paulson@15409
   318
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   319
done
paulson@15409
   320
paulson@15409
   321
lemma finite_cartesian_productD2:
paulson@15409
   322
     "[| finite (A <*> B); A \<noteq> {} |] ==> finite B"
paulson@15409
   323
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   324
apply (drule_tac x=n in spec) 
paulson@15409
   325
apply (drule_tac x="snd o f" in spec) 
paulson@15409
   326
apply (auto simp add: o_def) 
paulson@15409
   327
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   328
apply (drule equalityD1)
paulson@15409
   329
apply (rename_tac x y)
paulson@15409
   330
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   331
 prefer 2 apply force
paulson@15409
   332
apply clarify
paulson@15409
   333
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   334
done
paulson@15409
   335
paulson@15409
   336
wenzelm@12396
   337
instance unit :: finite
wenzelm@12396
   338
proof
wenzelm@12396
   339
  have "finite {()}" by simp
wenzelm@12396
   340
  also have "{()} = UNIV" by auto
wenzelm@12396
   341
  finally show "finite (UNIV :: unit set)" .
wenzelm@12396
   342
qed
wenzelm@12396
   343
wenzelm@12396
   344
instance * :: (finite, finite) finite
wenzelm@12396
   345
proof
wenzelm@12396
   346
  show "finite (UNIV :: ('a \<times> 'b) set)"
wenzelm@12396
   347
  proof (rule finite_Prod_UNIV)
wenzelm@12396
   348
    show "finite (UNIV :: 'a set)" by (rule finite)
wenzelm@12396
   349
    show "finite (UNIV :: 'b set)" by (rule finite)
wenzelm@12396
   350
  qed
wenzelm@12396
   351
qed
wenzelm@12396
   352
wenzelm@12396
   353
nipkow@15392
   354
text {* The powerset of a finite set *}
wenzelm@12396
   355
wenzelm@12396
   356
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   357
proof
wenzelm@12396
   358
  assume "finite (Pow A)"
wenzelm@12396
   359
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   360
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   361
next
wenzelm@12396
   362
  assume "finite A"
wenzelm@12396
   363
  thus "finite (Pow A)"
wenzelm@12396
   364
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   365
qed
wenzelm@12396
   366
nipkow@15392
   367
nipkow@15392
   368
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
nipkow@15392
   369
by(blast intro: finite_subset[OF subset_Pow_Union])
nipkow@15392
   370
nipkow@15392
   371
wenzelm@12396
   372
lemma finite_converse [iff]: "finite (r^-1) = finite r"
wenzelm@12396
   373
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
wenzelm@12396
   374
   apply simp
wenzelm@12396
   375
   apply (rule iffI)
wenzelm@12396
   376
    apply (erule finite_imageD [unfolded inj_on_def])
wenzelm@12396
   377
    apply (simp split add: split_split)
wenzelm@12396
   378
   apply (erule finite_imageI)
paulson@14208
   379
  apply (simp add: converse_def image_def, auto)
wenzelm@12396
   380
  apply (rule bexI)
wenzelm@12396
   381
   prefer 2 apply assumption
wenzelm@12396
   382
  apply simp
wenzelm@12396
   383
  done
wenzelm@12396
   384
paulson@14430
   385
nipkow@15392
   386
text {* \paragraph{Finiteness of transitive closure} (Thanks to Sidi
nipkow@15392
   387
Ehmety) *}
wenzelm@12396
   388
wenzelm@12396
   389
lemma finite_Field: "finite r ==> finite (Field r)"
wenzelm@12396
   390
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
wenzelm@12396
   391
  apply (induct set: Finites)
wenzelm@12396
   392
   apply (auto simp add: Field_def Domain_insert Range_insert)
wenzelm@12396
   393
  done
wenzelm@12396
   394
wenzelm@12396
   395
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
wenzelm@12396
   396
  apply clarify
wenzelm@12396
   397
  apply (erule trancl_induct)
wenzelm@12396
   398
   apply (auto simp add: Field_def)
wenzelm@12396
   399
  done
wenzelm@12396
   400
wenzelm@12396
   401
lemma finite_trancl: "finite (r^+) = finite r"
wenzelm@12396
   402
  apply auto
wenzelm@12396
   403
   prefer 2
wenzelm@12396
   404
   apply (rule trancl_subset_Field2 [THEN finite_subset])
wenzelm@12396
   405
   apply (rule finite_SigmaI)
wenzelm@12396
   406
    prefer 3
berghofe@13704
   407
    apply (blast intro: r_into_trancl' finite_subset)
wenzelm@12396
   408
   apply (auto simp add: finite_Field)
wenzelm@12396
   409
  done
wenzelm@12396
   410
wenzelm@12396
   411
nipkow@15392
   412
subsection {* A fold functional for finite sets *}
nipkow@15392
   413
nipkow@15392
   414
text {* The intended behaviour is
nipkow@15392
   415
@{text "fold f g e {x\<^isub>1, ..., x\<^isub>n} = f (g x\<^isub>1) (\<dots> (f (g x\<^isub>n) e)\<dots>)"}
nipkow@15392
   416
if @{text f} is associative-commutative. For an application of @{text fold}
nipkow@15392
   417
se the definitions of sums and products over finite sets.
nipkow@15392
   418
*}
nipkow@15392
   419
nipkow@15392
   420
consts
nipkow@15392
   421
  foldSet :: "('a => 'a => 'a) => ('b => 'a) => 'a => ('b set \<times> 'a) set"
nipkow@15392
   422
nipkow@15392
   423
inductive "foldSet f g e"
nipkow@15392
   424
intros
nipkow@15392
   425
emptyI [intro]: "({}, e) : foldSet f g e"
nipkow@15392
   426
insertI [intro]: "\<lbrakk> x \<notin> A; (A, y) : foldSet f g e \<rbrakk>
nipkow@15392
   427
 \<Longrightarrow> (insert x A, f (g x) y) : foldSet f g e"
nipkow@15392
   428
nipkow@15392
   429
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f g e"
nipkow@15392
   430
nipkow@15392
   431
constdefs
nipkow@15392
   432
  fold :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a"
nipkow@15392
   433
  "fold f g e A == THE x. (A, x) : foldSet f g e"
nipkow@15392
   434
nipkow@15392
   435
lemma Diff1_foldSet:
nipkow@15392
   436
  "(A - {x}, y) : foldSet f g e ==> x: A ==> (A, f (g x) y) : foldSet f g e"
nipkow@15392
   437
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
nipkow@15392
   438
nipkow@15392
   439
lemma foldSet_imp_finite: "(A, x) : foldSet f g e ==> finite A"
nipkow@15392
   440
  by (induct set: foldSet) auto
nipkow@15392
   441
nipkow@15392
   442
lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f g e"
nipkow@15392
   443
  by (induct set: Finites) auto
nipkow@15392
   444
nipkow@15392
   445
nipkow@15392
   446
subsubsection {* Commutative monoids *}
nipkow@15392
   447
nipkow@15392
   448
locale ACf =
nipkow@15392
   449
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
nipkow@15392
   450
  assumes commute: "x \<cdot> y = y \<cdot> x"
nipkow@15392
   451
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
nipkow@15392
   452
nipkow@15392
   453
locale ACe = ACf +
nipkow@15392
   454
  fixes e :: 'a
nipkow@15392
   455
  assumes ident [simp]: "x \<cdot> e = x"
nipkow@15392
   456
nipkow@15392
   457
lemma (in ACf) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
nipkow@15392
   458
proof -
nipkow@15392
   459
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
nipkow@15392
   460
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
nipkow@15392
   461
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
nipkow@15392
   462
  finally show ?thesis .
nipkow@15392
   463
qed
nipkow@15392
   464
nipkow@15392
   465
lemmas (in ACf) AC = assoc commute left_commute
nipkow@15392
   466
nipkow@15392
   467
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
nipkow@15392
   468
proof -
nipkow@15392
   469
  have "x \<cdot> e = x" by (rule ident)
nipkow@15392
   470
  thus ?thesis by (subst commute)
nipkow@15392
   471
qed
nipkow@15392
   472
nipkow@15402
   473
text{* Instantiation of locales: *}
nipkow@15402
   474
nipkow@15402
   475
lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15402
   476
by(fastsimp intro: ACf.intro add_assoc add_commute)
nipkow@15402
   477
nipkow@15402
   478
lemma ACe_add: "ACe (op +) (0::'a::comm_monoid_add)"
nipkow@15402
   479
by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
nipkow@15402
   480
nipkow@15402
   481
nipkow@15402
   482
lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15402
   483
by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
nipkow@15402
   484
nipkow@15402
   485
lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
nipkow@15402
   486
by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
nipkow@15402
   487
nipkow@15402
   488
nipkow@15392
   489
subsubsection{*From @{term foldSet} to @{term fold}*}
nipkow@15392
   490
nipkow@15392
   491
lemma (in ACf) foldSet_determ_aux:
nipkow@15392
   492
  "!!A x x' h. \<lbrakk> A = h`{i::nat. i<n}; (A,x) : foldSet f g e; (A,x') : foldSet f g e \<rbrakk>
nipkow@15392
   493
   \<Longrightarrow> x' = x"
nipkow@15392
   494
proof (induct n)
nipkow@15392
   495
  case 0 thus ?case by auto
nipkow@15392
   496
next
nipkow@15392
   497
  case (Suc n)
nipkow@15392
   498
  have IH: "!!A x x' h. \<lbrakk>A = h`{i::nat. i<n}; (A,x) \<in> foldSet f g e; (A,x') \<in> foldSet f g e\<rbrakk>
nipkow@15392
   499
           \<Longrightarrow> x' = x" and card: "A = h`{i. i<Suc n}"
nipkow@15392
   500
  and Afoldx: "(A, x) \<in> foldSet f g e" and Afoldy: "(A,x') \<in> foldSet f g e" .
nipkow@15392
   501
  show ?case
nipkow@15392
   502
  proof cases
nipkow@15392
   503
    assume "EX k<n. h n = h k"
nipkow@15392
   504
    hence card': "A = h ` {i. i < n}"
nipkow@15392
   505
      using card by (auto simp:image_def less_Suc_eq)
nipkow@15392
   506
    show ?thesis by(rule IH[OF card' Afoldx Afoldy])
nipkow@15392
   507
  next
nipkow@15392
   508
    assume new: "\<not>(EX k<n. h n = h k)"
nipkow@15392
   509
    show ?thesis
nipkow@15392
   510
    proof (rule foldSet.cases[OF Afoldx])
nipkow@15392
   511
      assume "(A, x) = ({}, e)"
nipkow@15392
   512
      thus "x' = x" using Afoldy by (auto)
nipkow@15392
   513
    next
nipkow@15392
   514
      fix B b y
nipkow@15392
   515
      assume eq1: "(A, x) = (insert b B, g b \<cdot> y)"
nipkow@15392
   516
	and y: "(B,y) \<in> foldSet f g e" and notinB: "b \<notin> B"
nipkow@15392
   517
      hence A1: "A = insert b B" and x: "x = g b \<cdot> y" by auto
nipkow@15392
   518
      show ?thesis
nipkow@15392
   519
      proof (rule foldSet.cases[OF Afoldy])
nipkow@15392
   520
	assume "(A,x') = ({}, e)"
nipkow@15392
   521
	thus ?thesis using A1 by auto
nipkow@15392
   522
      next
nipkow@15392
   523
	fix C c z
nipkow@15392
   524
	assume eq2: "(A,x') = (insert c C, g c \<cdot> z)"
nipkow@15392
   525
	  and z: "(C,z) \<in> foldSet f g e" and notinC: "c \<notin> C"
nipkow@15392
   526
	hence A2: "A = insert c C" and x': "x' = g c \<cdot> z" by auto
nipkow@15392
   527
	let ?h = "%i. if h i = b then h n else h i"
nipkow@15392
   528
	have less: "B = ?h`{i. i<n}" (is "_ = ?r")
nipkow@15392
   529
	proof
nipkow@15392
   530
	  show "B \<subseteq> ?r"
nipkow@15392
   531
	  proof
nipkow@15392
   532
	    fix u assume "u \<in> B"
nipkow@15392
   533
	    hence uinA: "u \<in> A" and unotb: "u \<noteq> b" using A1 notinB by blast+
nipkow@15392
   534
	    then obtain i\<^isub>u where below: "i\<^isub>u < Suc n" and [simp]: "u = h i\<^isub>u"
nipkow@15392
   535
	      using card by(auto simp:image_def)
nipkow@15392
   536
	    show "u \<in> ?r"
nipkow@15392
   537
	    proof cases
nipkow@15392
   538
	      assume "i\<^isub>u < n"
nipkow@15392
   539
	      thus ?thesis using unotb by(fastsimp)
nipkow@15392
   540
	    next
nipkow@15392
   541
	      assume "\<not> i\<^isub>u < n"
nipkow@15392
   542
	      with below have [simp]: "i\<^isub>u = n" by arith
nipkow@15392
   543
	      obtain i\<^isub>k where i\<^isub>k: "i\<^isub>k < Suc n" and [simp]: "b = h i\<^isub>k"
nipkow@15392
   544
		using A1 card by blast
nipkow@15392
   545
	      have "i\<^isub>k < n"
nipkow@15392
   546
	      proof (rule ccontr)
nipkow@15392
   547
		assume "\<not> i\<^isub>k < n"
nipkow@15392
   548
		hence "i\<^isub>k = n" using i\<^isub>k by arith
nipkow@15392
   549
		thus False using unotb by simp
nipkow@15392
   550
	      qed
nipkow@15392
   551
	      thus ?thesis by(auto simp add:image_def)
nipkow@15392
   552
	    qed
nipkow@15392
   553
	  qed
nipkow@15392
   554
	next
nipkow@15392
   555
	  show "?r \<subseteq> B"
nipkow@15392
   556
	  proof
nipkow@15392
   557
	    fix u assume "u \<in> ?r"
nipkow@15392
   558
	    then obtain i\<^isub>u where below: "i\<^isub>u < n" and
nipkow@15392
   559
              or: "b = h i\<^isub>u \<and> u = h n \<or> h i\<^isub>u \<noteq> b \<and> h i\<^isub>u = u"
nipkow@15392
   560
	      by(auto simp:image_def)
nipkow@15392
   561
	    from or show "u \<in> B"
nipkow@15392
   562
	    proof
nipkow@15392
   563
	      assume [simp]: "b = h i\<^isub>u \<and> u = h n"
nipkow@15392
   564
	      have "u \<in> A" using card by auto
nipkow@15392
   565
              moreover have "u \<noteq> b" using new below by auto
nipkow@15392
   566
	      ultimately show "u \<in> B" using A1 by blast
nipkow@15392
   567
	    next
nipkow@15392
   568
	      assume "h i\<^isub>u \<noteq> b \<and> h i\<^isub>u = u"
nipkow@15392
   569
	      moreover hence "u \<in> A" using card below by auto
nipkow@15392
   570
	      ultimately show "u \<in> B" using A1 by blast
nipkow@15392
   571
	    qed
nipkow@15392
   572
	  qed
nipkow@15392
   573
	qed
nipkow@15392
   574
	show ?thesis
nipkow@15392
   575
	proof cases
nipkow@15392
   576
	  assume "b = c"
nipkow@15392
   577
	  then moreover have "B = C" using A1 A2 notinB notinC by auto
nipkow@15392
   578
	  ultimately show ?thesis using IH[OF less] y z x x' by auto
nipkow@15392
   579
	next
nipkow@15392
   580
	  assume diff: "b \<noteq> c"
nipkow@15392
   581
	  let ?D = "B - {c}"
nipkow@15392
   582
	  have B: "B = insert c ?D" and C: "C = insert b ?D"
nipkow@15392
   583
	    using A1 A2 notinB notinC diff by(blast elim!:equalityE)+
nipkow@15402
   584
	  have "finite A" by(rule foldSet_imp_finite[OF Afoldx])
nipkow@15402
   585
	  with A1 have "finite ?D" by simp
nipkow@15392
   586
	  then obtain d where Dfoldd: "(?D,d) \<in> foldSet f g e"
nipkow@15392
   587
	    using finite_imp_foldSet by rules
nipkow@15392
   588
	  moreover have cinB: "c \<in> B" using B by(auto)
nipkow@15392
   589
	  ultimately have "(B,g c \<cdot> d) \<in> foldSet f g e"
nipkow@15392
   590
	    by(rule Diff1_foldSet)
nipkow@15392
   591
	  hence "g c \<cdot> d = y" by(rule IH[OF less y])
nipkow@15392
   592
          moreover have "g b \<cdot> d = z"
nipkow@15392
   593
	  proof (rule IH[OF _ z])
nipkow@15392
   594
	    let ?h = "%i. if h i = c then h n else h i"
nipkow@15392
   595
	    show "C = ?h`{i. i<n}" (is "_ = ?r")
nipkow@15392
   596
	    proof
nipkow@15392
   597
	      show "C \<subseteq> ?r"
nipkow@15392
   598
	      proof
nipkow@15392
   599
		fix u assume "u \<in> C"
nipkow@15392
   600
		hence uinA: "u \<in> A" and unotc: "u \<noteq> c"
nipkow@15392
   601
		  using A2 notinC by blast+
nipkow@15392
   602
		then obtain i\<^isub>u where below: "i\<^isub>u < Suc n" and [simp]: "u = h i\<^isub>u"
nipkow@15392
   603
		  using card by(auto simp:image_def)
nipkow@15392
   604
		show "u \<in> ?r"
nipkow@15392
   605
		proof cases
nipkow@15392
   606
		  assume "i\<^isub>u < n"
nipkow@15392
   607
		  thus ?thesis using unotc by(fastsimp)
nipkow@15392
   608
		next
nipkow@15392
   609
		  assume "\<not> i\<^isub>u < n"
nipkow@15392
   610
		  with below have [simp]: "i\<^isub>u = n" by arith
nipkow@15392
   611
		  obtain i\<^isub>k where i\<^isub>k: "i\<^isub>k < Suc n" and [simp]: "c = h i\<^isub>k"
nipkow@15392
   612
		    using A2 card by blast
nipkow@15392
   613
		  have "i\<^isub>k < n"
nipkow@15392
   614
		  proof (rule ccontr)
nipkow@15392
   615
		    assume "\<not> i\<^isub>k < n"
nipkow@15392
   616
		    hence "i\<^isub>k = n" using i\<^isub>k by arith
nipkow@15392
   617
		    thus False using unotc by simp
nipkow@15392
   618
		  qed
nipkow@15392
   619
		  thus ?thesis by(auto simp add:image_def)
nipkow@15392
   620
		qed
nipkow@15392
   621
	      qed
nipkow@15392
   622
	    next
nipkow@15392
   623
	      show "?r \<subseteq> C"
nipkow@15392
   624
	      proof
nipkow@15392
   625
		fix u assume "u \<in> ?r"
nipkow@15392
   626
		then obtain i\<^isub>u where below: "i\<^isub>u < n" and
nipkow@15392
   627
		  or: "c = h i\<^isub>u \<and> u = h n \<or> h i\<^isub>u \<noteq> c \<and> h i\<^isub>u = u"
nipkow@15392
   628
		  by(auto simp:image_def)
nipkow@15392
   629
		from or show "u \<in> C"
nipkow@15392
   630
		proof
nipkow@15392
   631
		  assume [simp]: "c = h i\<^isub>u \<and> u = h n"
nipkow@15392
   632
		  have "u \<in> A" using card by auto
nipkow@15392
   633
		  moreover have "u \<noteq> c" using new below by auto
nipkow@15392
   634
		  ultimately show "u \<in> C" using A2 by blast
nipkow@15392
   635
		next
nipkow@15392
   636
		  assume "h i\<^isub>u \<noteq> c \<and> h i\<^isub>u = u"
nipkow@15392
   637
		  moreover hence "u \<in> A" using card below by auto
nipkow@15392
   638
		  ultimately show "u \<in> C" using A2 by blast
nipkow@15392
   639
		qed
nipkow@15392
   640
	      qed
nipkow@15392
   641
	    qed
nipkow@15392
   642
	  next
nipkow@15392
   643
	    show "(C,g b \<cdot> d) \<in> foldSet f g e" using C notinB Dfoldd
nipkow@15392
   644
	      by fastsimp
nipkow@15392
   645
	  qed
nipkow@15392
   646
	  ultimately show ?thesis using x x' by(auto simp:AC)
nipkow@15392
   647
	qed
nipkow@15392
   648
      qed
nipkow@15392
   649
    qed
nipkow@15392
   650
  qed
nipkow@15392
   651
qed
nipkow@15392
   652
nipkow@15392
   653
(* The same proof, but using card 
nipkow@15392
   654
lemma (in ACf) foldSet_determ_aux:
nipkow@15392
   655
  "!!A x x'. \<lbrakk> card A < n; (A,x) : foldSet f g e; (A,x') : foldSet f g e \<rbrakk>
nipkow@15392
   656
   \<Longrightarrow> x' = x"
nipkow@15392
   657
proof (induct n)
nipkow@15392
   658
  case 0 thus ?case by simp
nipkow@15392
   659
next
nipkow@15392
   660
  case (Suc n)
nipkow@15392
   661
  have IH: "!!A x x'. \<lbrakk>card A < n; (A,x) \<in> foldSet f g e; (A,x') \<in> foldSet f g e\<rbrakk>
nipkow@15392
   662
           \<Longrightarrow> x' = x" and card: "card A < Suc n"
nipkow@15392
   663
  and Afoldx: "(A, x) \<in> foldSet f g e" and Afoldy: "(A,x') \<in> foldSet f g e" .
nipkow@15392
   664
  from card have "card A < n \<or> card A = n" by arith
nipkow@15392
   665
  thus ?case
nipkow@15392
   666
  proof
nipkow@15392
   667
    assume less: "card A < n"
nipkow@15392
   668
    show ?thesis by(rule IH[OF less Afoldx Afoldy])
nipkow@15392
   669
  next
nipkow@15392
   670
    assume cardA: "card A = n"
nipkow@15392
   671
    show ?thesis
nipkow@15392
   672
    proof (rule foldSet.cases[OF Afoldx])
nipkow@15392
   673
      assume "(A, x) = ({}, e)"
nipkow@15392
   674
      thus "x' = x" using Afoldy by (auto)
nipkow@15392
   675
    next
nipkow@15392
   676
      fix B b y
nipkow@15392
   677
      assume eq1: "(A, x) = (insert b B, g b \<cdot> y)"
nipkow@15392
   678
	and y: "(B,y) \<in> foldSet f g e" and notinB: "b \<notin> B"
nipkow@15392
   679
      hence A1: "A = insert b B" and x: "x = g b \<cdot> y" by auto
nipkow@15392
   680
      show ?thesis
nipkow@15392
   681
      proof (rule foldSet.cases[OF Afoldy])
nipkow@15392
   682
	assume "(A,x') = ({}, e)"
nipkow@15392
   683
	thus ?thesis using A1 by auto
nipkow@15392
   684
      next
nipkow@15392
   685
	fix C c z
nipkow@15392
   686
	assume eq2: "(A,x') = (insert c C, g c \<cdot> z)"
nipkow@15392
   687
	  and z: "(C,z) \<in> foldSet f g e" and notinC: "c \<notin> C"
nipkow@15392
   688
	hence A2: "A = insert c C" and x': "x' = g c \<cdot> z" by auto
nipkow@15392
   689
	have finA: "finite A" by(rule foldSet_imp_finite[OF Afoldx])
nipkow@15392
   690
	with cardA A1 notinB have less: "card B < n" by simp
nipkow@15392
   691
	show ?thesis
nipkow@15392
   692
	proof cases
nipkow@15392
   693
	  assume "b = c"
nipkow@15392
   694
	  then moreover have "B = C" using A1 A2 notinB notinC by auto
nipkow@15392
   695
	  ultimately show ?thesis using IH[OF less] y z x x' by auto
nipkow@15392
   696
	next
nipkow@15392
   697
	  assume diff: "b \<noteq> c"
nipkow@15392
   698
	  let ?D = "B - {c}"
nipkow@15392
   699
	  have B: "B = insert c ?D" and C: "C = insert b ?D"
nipkow@15392
   700
	    using A1 A2 notinB notinC diff by(blast elim!:equalityE)+
nipkow@15392
   701
	  have "finite ?D" using finA A1 by simp
nipkow@15392
   702
	  then obtain d where Dfoldd: "(?D,d) \<in> foldSet f g e"
nipkow@15392
   703
	    using finite_imp_foldSet by rules
nipkow@15392
   704
	  moreover have cinB: "c \<in> B" using B by(auto)
nipkow@15392
   705
	  ultimately have "(B,g c \<cdot> d) \<in> foldSet f g e"
nipkow@15392
   706
	    by(rule Diff1_foldSet)
nipkow@15392
   707
	  hence "g c \<cdot> d = y" by(rule IH[OF less y])
nipkow@15392
   708
          moreover have "g b \<cdot> d = z"
nipkow@15392
   709
	  proof (rule IH[OF _ z])
nipkow@15392
   710
	    show "card C < n" using C cardA A1 notinB finA cinB
nipkow@15392
   711
	      by(auto simp:card_Diff1_less)
nipkow@15392
   712
	  next
nipkow@15392
   713
	    show "(C,g b \<cdot> d) \<in> foldSet f g e" using C notinB Dfoldd
nipkow@15392
   714
	      by fastsimp
nipkow@15392
   715
	  qed
nipkow@15392
   716
	  ultimately show ?thesis using x x' by(auto simp:AC)
nipkow@15392
   717
	qed
nipkow@15392
   718
      qed
nipkow@15392
   719
    qed
nipkow@15392
   720
  qed
nipkow@15392
   721
qed
nipkow@15392
   722
*)
nipkow@15392
   723
nipkow@15392
   724
lemma (in ACf) foldSet_determ:
nipkow@15392
   725
  "(A, x) : foldSet f g e ==> (A, y) : foldSet f g e ==> y = x"
nipkow@15392
   726
apply(frule foldSet_imp_finite)
nipkow@15392
   727
apply(simp add:finite_conv_nat_seg_image)
nipkow@15392
   728
apply(blast intro: foldSet_determ_aux [rule_format])
nipkow@15392
   729
done
nipkow@15392
   730
nipkow@15392
   731
lemma (in ACf) fold_equality: "(A, y) : foldSet f g e ==> fold f g e A = y"
nipkow@15392
   732
  by (unfold fold_def) (blast intro: foldSet_determ)
nipkow@15392
   733
nipkow@15392
   734
text{* The base case for @{text fold}: *}
nipkow@15392
   735
nipkow@15392
   736
lemma fold_empty [simp]: "fold f g e {} = e"
nipkow@15392
   737
  by (unfold fold_def) blast
nipkow@15392
   738
nipkow@15392
   739
lemma (in ACf) fold_insert_aux: "x \<notin> A ==>
nipkow@15392
   740
    ((insert x A, v) : foldSet f g e) =
nipkow@15392
   741
    (EX y. (A, y) : foldSet f g e & v = f (g x) y)"
nipkow@15392
   742
  apply auto
nipkow@15392
   743
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
nipkow@15392
   744
   apply (fastsimp dest: foldSet_imp_finite)
nipkow@15392
   745
  apply (blast intro: foldSet_determ)
nipkow@15392
   746
  done
nipkow@15392
   747
nipkow@15392
   748
text{* The recursion equation for @{text fold}: *}
nipkow@15392
   749
nipkow@15392
   750
lemma (in ACf) fold_insert[simp]:
nipkow@15392
   751
    "finite A ==> x \<notin> A ==> fold f g e (insert x A) = f (g x) (fold f g e A)"
nipkow@15392
   752
  apply (unfold fold_def)
nipkow@15392
   753
  apply (simp add: fold_insert_aux)
nipkow@15392
   754
  apply (rule the_equality)
nipkow@15392
   755
  apply (auto intro: finite_imp_foldSet
nipkow@15392
   756
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
nipkow@15392
   757
  done
nipkow@15392
   758
nipkow@15392
   759
declare
nipkow@15392
   760
  empty_foldSetE [rule del]  foldSet.intros [rule del]
nipkow@15392
   761
  -- {* Delete rules to do with @{text foldSet} relation. *}
nipkow@15392
   762
nipkow@15392
   763
subsubsection{*Lemmas about @{text fold}*}
nipkow@15392
   764
nipkow@15392
   765
lemma (in ACf) fold_commute:
nipkow@15392
   766
  "finite A ==> (!!e. f (g x) (fold f g e A) = fold f g (f (g x) e) A)"
nipkow@15392
   767
  apply (induct set: Finites, simp)
nipkow@15392
   768
  apply (simp add: left_commute)
nipkow@15392
   769
  done
nipkow@15392
   770
nipkow@15392
   771
lemma (in ACf) fold_nest_Un_Int:
nipkow@15392
   772
  "finite A ==> finite B
nipkow@15392
   773
    ==> fold f g (fold f g e B) A = fold f g (fold f g e (A Int B)) (A Un B)"
nipkow@15392
   774
  apply (induct set: Finites, simp)
nipkow@15392
   775
  apply (simp add: fold_commute Int_insert_left insert_absorb)
nipkow@15392
   776
  done
nipkow@15392
   777
nipkow@15392
   778
lemma (in ACf) fold_nest_Un_disjoint:
nipkow@15392
   779
  "finite A ==> finite B ==> A Int B = {}
nipkow@15392
   780
    ==> fold f g e (A Un B) = fold f g (fold f g e B) A"
nipkow@15392
   781
  by (simp add: fold_nest_Un_Int)
nipkow@15392
   782
nipkow@15392
   783
lemma (in ACf) fold_reindex:
nipkow@15392
   784
assumes fin: "finite B"
nipkow@15392
   785
shows "inj_on h B \<Longrightarrow> fold f g e (h ` B) = fold f (g \<circ> h) e B"
nipkow@15392
   786
using fin apply (induct)
nipkow@15392
   787
 apply simp
nipkow@15392
   788
apply simp
nipkow@15392
   789
done
nipkow@15392
   790
nipkow@15392
   791
lemma (in ACe) fold_Un_Int:
nipkow@15392
   792
  "finite A ==> finite B ==>
nipkow@15392
   793
    fold f g e A \<cdot> fold f g e B =
nipkow@15392
   794
    fold f g e (A Un B) \<cdot> fold f g e (A Int B)"
nipkow@15392
   795
  apply (induct set: Finites, simp)
nipkow@15392
   796
  apply (simp add: AC insert_absorb Int_insert_left)
nipkow@15392
   797
  done
nipkow@15392
   798
nipkow@15392
   799
corollary (in ACe) fold_Un_disjoint:
nipkow@15392
   800
  "finite A ==> finite B ==> A Int B = {} ==>
nipkow@15392
   801
    fold f g e (A Un B) = fold f g e A \<cdot> fold f g e B"
nipkow@15392
   802
  by (simp add: fold_Un_Int)
nipkow@15392
   803
nipkow@15392
   804
lemma (in ACe) fold_UN_disjoint:
nipkow@15392
   805
  "\<lbrakk> finite I; ALL i:I. finite (A i);
nipkow@15392
   806
     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
nipkow@15392
   807
   \<Longrightarrow> fold f g e (UNION I A) =
nipkow@15392
   808
       fold f (%i. fold f g e (A i)) e I"
nipkow@15392
   809
  apply (induct set: Finites, simp, atomize)
nipkow@15392
   810
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
nipkow@15392
   811
   prefer 2 apply blast
nipkow@15392
   812
  apply (subgoal_tac "A x Int UNION F A = {}")
nipkow@15392
   813
   prefer 2 apply blast
nipkow@15392
   814
  apply (simp add: fold_Un_disjoint)
nipkow@15392
   815
  done
nipkow@15392
   816
nipkow@15392
   817
lemma (in ACf) fold_cong:
nipkow@15392
   818
  "finite A \<Longrightarrow> (!!x. x:A ==> g x = h x) ==> fold f g a A = fold f h a A"
nipkow@15392
   819
  apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold f g a C = fold f h a C")
nipkow@15392
   820
   apply simp
nipkow@15392
   821
  apply (erule finite_induct, simp)
nipkow@15392
   822
  apply (simp add: subset_insert_iff, clarify)
nipkow@15392
   823
  apply (subgoal_tac "finite C")
nipkow@15392
   824
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
nipkow@15392
   825
  apply (subgoal_tac "C = insert x (C - {x})")
nipkow@15392
   826
   prefer 2 apply blast
nipkow@15392
   827
  apply (erule ssubst)
nipkow@15392
   828
  apply (drule spec)
nipkow@15392
   829
  apply (erule (1) notE impE)
nipkow@15392
   830
  apply (simp add: Ball_def del: insert_Diff_single)
nipkow@15392
   831
  done
nipkow@15392
   832
nipkow@15392
   833
lemma (in ACe) fold_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15392
   834
  fold f (%x. fold f (g x) e (B x)) e A =
nipkow@15392
   835
  fold f (split g) e (SIGMA x:A. B x)"
nipkow@15392
   836
apply (subst Sigma_def)
nipkow@15392
   837
apply (subst fold_UN_disjoint)
nipkow@15392
   838
   apply assumption
nipkow@15392
   839
  apply simp
nipkow@15392
   840
 apply blast
nipkow@15392
   841
apply (erule fold_cong)
nipkow@15392
   842
apply (subst fold_UN_disjoint)
nipkow@15392
   843
   apply simp
nipkow@15392
   844
  apply simp
nipkow@15392
   845
 apply blast
nipkow@15392
   846
apply (simp)
nipkow@15392
   847
done
nipkow@15392
   848
nipkow@15392
   849
lemma (in ACe) fold_distrib: "finite A \<Longrightarrow>
nipkow@15392
   850
   fold f (%x. f (g x) (h x)) e A = f (fold f g e A) (fold f h e A)"
nipkow@15392
   851
apply (erule finite_induct)
nipkow@15392
   852
 apply simp
nipkow@15392
   853
apply (simp add:AC)
nipkow@15392
   854
done
nipkow@15392
   855
nipkow@15392
   856
nipkow@15402
   857
subsection {* Generalized summation over a set *}
nipkow@15402
   858
nipkow@15402
   859
constdefs
nipkow@15402
   860
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
nipkow@15402
   861
  "setsum f A == if finite A then fold (op +) f 0 A else 0"
nipkow@15402
   862
nipkow@15402
   863
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   864
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   865
nipkow@15402
   866
syntax
nipkow@15402
   867
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   868
syntax (xsymbols)
nipkow@15402
   869
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   870
syntax (HTML output)
nipkow@15402
   871
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   872
nipkow@15402
   873
translations -- {* Beware of argument permutation! *}
nipkow@15402
   874
  "SUM i:A. b" == "setsum (%i. b) A"
nipkow@15402
   875
  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
nipkow@15402
   876
nipkow@15402
   877
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   878
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   879
nipkow@15402
   880
syntax
nipkow@15402
   881
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   882
syntax (xsymbols)
nipkow@15402
   883
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   884
syntax (HTML output)
nipkow@15402
   885
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   886
nipkow@15402
   887
translations
nipkow@15402
   888
  "SUM x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   889
  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   890
nipkow@15402
   891
text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
nipkow@15402
   892
nipkow@15402
   893
syntax
nipkow@15402
   894
  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
nipkow@15402
   895
nipkow@15402
   896
parse_translation {*
nipkow@15402
   897
  let
nipkow@15402
   898
    fun Setsum_tr [A] = Syntax.const "setsum" $ Abs ("", dummyT, Bound 0) $ A
nipkow@15402
   899
  in [("_Setsum", Setsum_tr)] end;
nipkow@15402
   900
*}
nipkow@15402
   901
nipkow@15402
   902
print_translation {*
nipkow@15402
   903
let
nipkow@15402
   904
  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" $ A
nipkow@15402
   905
    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
nipkow@15402
   906
       if x<>y then raise Match
nipkow@15402
   907
       else let val x' = Syntax.mark_bound x
nipkow@15402
   908
                val t' = subst_bound(x',t)
nipkow@15402
   909
                val P' = subst_bound(x',P)
nipkow@15402
   910
            in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
nipkow@15402
   911
in
nipkow@15402
   912
[("setsum", setsum_tr')]
nipkow@15402
   913
end
nipkow@15402
   914
*}
nipkow@15402
   915
nipkow@15402
   916
lemma setsum_empty [simp]: "setsum f {} = 0"
nipkow@15402
   917
  by (simp add: setsum_def)
nipkow@15402
   918
nipkow@15402
   919
lemma setsum_insert [simp]:
nipkow@15402
   920
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
nipkow@15402
   921
  by (simp add: setsum_def ACf.fold_insert [OF ACf_add])
nipkow@15402
   922
paulson@15409
   923
lemma setsum_infinite [simp]: "~ finite A ==> setsum f A = 0"
paulson@15409
   924
  by (simp add: setsum_def)
paulson@15409
   925
nipkow@15402
   926
lemma setsum_reindex:
nipkow@15402
   927
     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
nipkow@15402
   928
by(auto simp add: setsum_def ACf.fold_reindex[OF ACf_add] dest!:finite_imageD)
nipkow@15402
   929
nipkow@15402
   930
lemma setsum_reindex_id:
nipkow@15402
   931
     "inj_on f B ==> setsum f B = setsum id (f ` B)"
nipkow@15402
   932
by (auto simp add: setsum_reindex)
nipkow@15402
   933
nipkow@15402
   934
lemma setsum_cong:
nipkow@15402
   935
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
nipkow@15402
   936
by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
nipkow@15402
   937
nipkow@15402
   938
lemma setsum_reindex_cong:
nipkow@15402
   939
     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
nipkow@15402
   940
      ==> setsum h B = setsum g A"
nipkow@15402
   941
  by (simp add: setsum_reindex cong: setsum_cong)
nipkow@15402
   942
nipkow@15402
   943
lemma setsum_0: "setsum (%i. 0) A = 0"
nipkow@15402
   944
apply (clarsimp simp: setsum_def)
nipkow@15402
   945
apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
nipkow@15402
   946
done
nipkow@15402
   947
nipkow@15402
   948
lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
nipkow@15402
   949
  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
nipkow@15402
   950
  apply (erule ssubst, rule setsum_0)
nipkow@15402
   951
  apply (rule setsum_cong, auto)
nipkow@15402
   952
  done
nipkow@15402
   953
nipkow@15402
   954
lemma setsum_Un_Int: "finite A ==> finite B ==>
nipkow@15402
   955
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
nipkow@15402
   956
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
nipkow@15402
   957
by(simp add: setsum_def ACe.fold_Un_Int[OF ACe_add,symmetric])
nipkow@15402
   958
nipkow@15402
   959
lemma setsum_Un_disjoint: "finite A ==> finite B
nipkow@15402
   960
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
nipkow@15402
   961
by (subst setsum_Un_Int [symmetric], auto)
nipkow@15402
   962
paulson@15409
   963
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
paulson@15409
   964
  the lhs need not be, since UNION I A could still be finite.*)
nipkow@15402
   965
lemma setsum_UN_disjoint:
nipkow@15402
   966
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
   967
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
   968
      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
nipkow@15402
   969
by(simp add: setsum_def ACe.fold_UN_disjoint[OF ACe_add] cong: setsum_cong)
nipkow@15402
   970
paulson@15409
   971
text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
paulson@15409
   972
directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
nipkow@15402
   973
lemma setsum_Union_disjoint:
paulson@15409
   974
  "[| (ALL A:C. finite A);
paulson@15409
   975
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |]
paulson@15409
   976
   ==> setsum f (Union C) = setsum (setsum f) C"
paulson@15409
   977
apply (cases "finite C") 
paulson@15409
   978
 prefer 2 apply (force dest: finite_UnionD simp add: setsum_def)
nipkow@15402
   979
  apply (frule setsum_UN_disjoint [of C id f])
paulson@15409
   980
 apply (unfold Union_def id_def, assumption+)
paulson@15409
   981
done
nipkow@15402
   982
paulson@15409
   983
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
paulson@15409
   984
  the rhs need not be, since SIGMA A B could still be finite.*)
nipkow@15402
   985
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15402
   986
    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
nipkow@15402
   987
    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
nipkow@15402
   988
by(simp add:setsum_def ACe.fold_Sigma[OF ACe_add] split_def cong:setsum_cong)
nipkow@15402
   989
paulson@15409
   990
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
   991
lemma setsum_cartesian_product: 
paulson@15409
   992
   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
paulson@15409
   993
apply (cases "finite A") 
paulson@15409
   994
 apply (cases "finite B") 
paulson@15409
   995
  apply (simp add: setsum_Sigma)
paulson@15409
   996
 apply (cases "A={}", simp)
paulson@15409
   997
 apply (simp add: setsum_0) 
paulson@15409
   998
apply (auto simp add: setsum_def
paulson@15409
   999
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1000
done
nipkow@15402
  1001
nipkow@15402
  1002
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
nipkow@15402
  1003
by(simp add:setsum_def ACe.fold_distrib[OF ACe_add])
nipkow@15402
  1004
nipkow@15402
  1005
nipkow@15402
  1006
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1007
nipkow@15402
  1008
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
nipkow@15402
  1009
  apply (case_tac "finite A")
nipkow@15402
  1010
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
  1011
  apply (erule rev_mp)
nipkow@15402
  1012
  apply (erule finite_induct, auto)
nipkow@15402
  1013
  done
nipkow@15402
  1014
nipkow@15402
  1015
lemma setsum_eq_0_iff [simp]:
nipkow@15402
  1016
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
nipkow@15402
  1017
  by (induct set: Finites) auto
nipkow@15402
  1018
nipkow@15402
  1019
lemma setsum_Un_nat: "finite A ==> finite B ==>
nipkow@15402
  1020
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
  1021
  -- {* For the natural numbers, we have subtraction. *}
nipkow@15402
  1022
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
nipkow@15402
  1023
nipkow@15402
  1024
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@15402
  1025
    (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@15402
  1026
      setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
  1027
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
nipkow@15402
  1028
nipkow@15402
  1029
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
nipkow@15402
  1030
    (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
  1031
  apply (case_tac "finite A")
nipkow@15402
  1032
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
  1033
  apply (erule finite_induct)
nipkow@15402
  1034
   apply (auto simp add: insert_Diff_if)
nipkow@15402
  1035
  apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@15402
  1036
  done
nipkow@15402
  1037
nipkow@15402
  1038
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
  1039
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
  1040
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
  1041
  by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
  1042
nipkow@15402
  1043
(* By Jeremy Siek: *)
nipkow@15402
  1044
nipkow@15402
  1045
lemma setsum_diff_nat: 
nipkow@15402
  1046
  assumes finB: "finite B"
nipkow@15402
  1047
  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
nipkow@15402
  1048
using finB
nipkow@15402
  1049
proof (induct)
nipkow@15402
  1050
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15402
  1051
next
nipkow@15402
  1052
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15402
  1053
    and xFinA: "insert x F \<subseteq> A"
nipkow@15402
  1054
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15402
  1055
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15402
  1056
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15402
  1057
    by (simp add: setsum_diff1_nat)
nipkow@15402
  1058
  from xFinA have "F \<subseteq> A" by simp
nipkow@15402
  1059
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15402
  1060
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15402
  1061
    by simp
nipkow@15402
  1062
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15402
  1063
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15402
  1064
    by simp
nipkow@15402
  1065
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15402
  1066
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15402
  1067
    by simp
nipkow@15402
  1068
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15402
  1069
qed
nipkow@15402
  1070
nipkow@15402
  1071
lemma setsum_diff:
nipkow@15402
  1072
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
  1073
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
  1074
proof -
nipkow@15402
  1075
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
  1076
  show ?thesis using finiteB le
nipkow@15402
  1077
    proof (induct)
nipkow@15402
  1078
      case empty
nipkow@15402
  1079
      thus ?case by auto
nipkow@15402
  1080
    next
nipkow@15402
  1081
      case (insert x F)
nipkow@15402
  1082
      thus ?case using le finiteB 
nipkow@15402
  1083
	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
  1084
    qed
nipkow@15402
  1085
  qed
nipkow@15402
  1086
nipkow@15402
  1087
lemma setsum_mono:
nipkow@15402
  1088
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
nipkow@15402
  1089
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
  1090
proof (cases "finite K")
nipkow@15402
  1091
  case True
nipkow@15402
  1092
  thus ?thesis using le
nipkow@15402
  1093
  proof (induct)
nipkow@15402
  1094
    case empty
nipkow@15402
  1095
    thus ?case by simp
nipkow@15402
  1096
  next
nipkow@15402
  1097
    case insert
nipkow@15402
  1098
    thus ?case using add_mono 
nipkow@15402
  1099
      by force
nipkow@15402
  1100
  qed
nipkow@15402
  1101
next
nipkow@15402
  1102
  case False
nipkow@15402
  1103
  thus ?thesis
nipkow@15402
  1104
    by (simp add: setsum_def)
nipkow@15402
  1105
qed
nipkow@15402
  1106
nipkow@15402
  1107
lemma setsum_mono2_nat:
nipkow@15402
  1108
  assumes fin: "finite B" and sub: "A \<subseteq> B"
nipkow@15402
  1109
shows "setsum f A \<le> (setsum f B :: nat)"
nipkow@15402
  1110
proof -
nipkow@15402
  1111
  have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
nipkow@15402
  1112
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15402
  1113
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15402
  1114
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15402
  1115
  finally show ?thesis .
nipkow@15402
  1116
qed
nipkow@15402
  1117
nipkow@15402
  1118
lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
nipkow@15402
  1119
  - setsum f A"
nipkow@15402
  1120
  by (induct set: Finites, auto)
nipkow@15402
  1121
nipkow@15402
  1122
lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
nipkow@15402
  1123
  setsum f A - setsum g A"
nipkow@15402
  1124
  by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15402
  1125
nipkow@15402
  1126
lemma setsum_nonneg: "[| finite A;
nipkow@15402
  1127
    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
nipkow@15402
  1128
    0 \<le> setsum f A";
nipkow@15402
  1129
  apply (induct set: Finites, auto)
nipkow@15402
  1130
  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
nipkow@15402
  1131
  apply (blast intro: add_mono)
nipkow@15402
  1132
  done
nipkow@15402
  1133
nipkow@15402
  1134
lemma setsum_nonpos: "[| finite A;
nipkow@15402
  1135
    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
nipkow@15402
  1136
    setsum f A \<le> 0";
nipkow@15402
  1137
  apply (induct set: Finites, auto)
nipkow@15402
  1138
  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
nipkow@15402
  1139
  apply (blast intro: add_mono)
nipkow@15402
  1140
  done
nipkow@15402
  1141
nipkow@15402
  1142
lemma setsum_mult: 
nipkow@15402
  1143
  fixes f :: "'a => ('b::semiring_0_cancel)"
nipkow@15402
  1144
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
  1145
proof (cases "finite A")
nipkow@15402
  1146
  case True
nipkow@15402
  1147
  thus ?thesis
nipkow@15402
  1148
  proof (induct)
nipkow@15402
  1149
    case empty thus ?case by simp
nipkow@15402
  1150
  next
nipkow@15402
  1151
    case (insert x A) thus ?case by (simp add: right_distrib)
nipkow@15402
  1152
  qed
nipkow@15402
  1153
next
nipkow@15402
  1154
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1155
qed
nipkow@15402
  1156
nipkow@15402
  1157
lemma setsum_abs: 
nipkow@15402
  1158
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1159
  assumes fin: "finite A" 
nipkow@15402
  1160
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15402
  1161
using fin 
nipkow@15402
  1162
proof (induct) 
nipkow@15402
  1163
  case empty thus ?case by simp
nipkow@15402
  1164
next
nipkow@15402
  1165
  case (insert x A)
nipkow@15402
  1166
  thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15402
  1167
qed
nipkow@15402
  1168
nipkow@15402
  1169
lemma setsum_abs_ge_zero: 
nipkow@15402
  1170
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1171
  assumes fin: "finite A" 
nipkow@15402
  1172
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15402
  1173
using fin 
nipkow@15402
  1174
proof (induct) 
nipkow@15402
  1175
  case empty thus ?case by simp
nipkow@15402
  1176
next
nipkow@15402
  1177
  case (insert x A) thus ?case by (auto intro: order_trans)
nipkow@15402
  1178
qed
nipkow@15402
  1179
nipkow@15402
  1180
nipkow@15402
  1181
subsection {* Generalized product over a set *}
nipkow@15402
  1182
nipkow@15402
  1183
constdefs
nipkow@15402
  1184
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
nipkow@15402
  1185
  "setprod f A == if finite A then fold (op *) f 1 A else 1"
nipkow@15402
  1186
nipkow@15402
  1187
syntax
nipkow@15402
  1188
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
nipkow@15402
  1189
nipkow@15402
  1190
syntax (xsymbols)
nipkow@15402
  1191
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1192
syntax (HTML output)
nipkow@15402
  1193
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1194
translations
nipkow@15402
  1195
  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
nipkow@15402
  1196
nipkow@15402
  1197
syntax
nipkow@15402
  1198
  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
nipkow@15402
  1199
nipkow@15402
  1200
parse_translation {*
nipkow@15402
  1201
  let
nipkow@15402
  1202
    fun Setprod_tr [A] = Syntax.const "setprod" $ Abs ("", dummyT, Bound 0) $ A
nipkow@15402
  1203
  in [("_Setprod", Setprod_tr)] end;
nipkow@15402
  1204
*}
nipkow@15402
  1205
print_translation {*
nipkow@15402
  1206
let fun setprod_tr' [Abs(x,Tx,t), A] =
nipkow@15402
  1207
    if t = Bound 0 then Syntax.const "_Setprod" $ A else raise Match
nipkow@15402
  1208
in
nipkow@15402
  1209
[("setprod", setprod_tr')]
nipkow@15402
  1210
end
nipkow@15402
  1211
*}
nipkow@15402
  1212
nipkow@15402
  1213
nipkow@15402
  1214
lemma setprod_empty [simp]: "setprod f {} = 1"
nipkow@15402
  1215
  by (auto simp add: setprod_def)
nipkow@15402
  1216
nipkow@15402
  1217
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
nipkow@15402
  1218
    setprod f (insert a A) = f a * setprod f A"
nipkow@15402
  1219
by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
nipkow@15402
  1220
paulson@15409
  1221
lemma setprod_infinite [simp]: "~ finite A ==> setprod f A = 1"
paulson@15409
  1222
  by (simp add: setprod_def)
paulson@15409
  1223
nipkow@15402
  1224
lemma setprod_reindex:
nipkow@15402
  1225
     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
nipkow@15402
  1226
by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
nipkow@15402
  1227
nipkow@15402
  1228
lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
nipkow@15402
  1229
by (auto simp add: setprod_reindex)
nipkow@15402
  1230
nipkow@15402
  1231
lemma setprod_cong:
nipkow@15402
  1232
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
nipkow@15402
  1233
by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
nipkow@15402
  1234
nipkow@15402
  1235
lemma setprod_reindex_cong: "inj_on f A ==>
nipkow@15402
  1236
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
nipkow@15402
  1237
  by (frule setprod_reindex, simp)
nipkow@15402
  1238
nipkow@15402
  1239
nipkow@15402
  1240
lemma setprod_1: "setprod (%i. 1) A = 1"
nipkow@15402
  1241
  apply (case_tac "finite A")
nipkow@15402
  1242
  apply (erule finite_induct, auto simp add: mult_ac)
nipkow@15402
  1243
  done
nipkow@15402
  1244
nipkow@15402
  1245
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
nipkow@15402
  1246
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
nipkow@15402
  1247
  apply (erule ssubst, rule setprod_1)
nipkow@15402
  1248
  apply (rule setprod_cong, auto)
nipkow@15402
  1249
  done
nipkow@15402
  1250
nipkow@15402
  1251
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
  1252
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
nipkow@15402
  1253
by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
nipkow@15402
  1254
nipkow@15402
  1255
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1256
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
nipkow@15402
  1257
by (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1258
nipkow@15402
  1259
lemma setprod_UN_disjoint:
nipkow@15402
  1260
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1261
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1262
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
nipkow@15402
  1263
by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
nipkow@15402
  1264
nipkow@15402
  1265
lemma setprod_Union_disjoint:
paulson@15409
  1266
  "[| (ALL A:C. finite A);
paulson@15409
  1267
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |] 
paulson@15409
  1268
   ==> setprod f (Union C) = setprod (setprod f) C"
paulson@15409
  1269
apply (cases "finite C") 
paulson@15409
  1270
 prefer 2 apply (force dest: finite_UnionD simp add: setprod_def)
nipkow@15402
  1271
  apply (frule setprod_UN_disjoint [of C id f])
paulson@15409
  1272
 apply (unfold Union_def id_def, assumption+)
paulson@15409
  1273
done
nipkow@15402
  1274
nipkow@15402
  1275
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15402
  1276
    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
nipkow@15402
  1277
    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
nipkow@15402
  1278
by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
nipkow@15402
  1279
paulson@15409
  1280
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1281
lemma setprod_cartesian_product: 
paulson@15409
  1282
     "(\<Prod>x:A. (\<Prod>y: B. f x y)) = (\<Prod>z:(A <*> B). f (fst z) (snd z))"
paulson@15409
  1283
apply (cases "finite A") 
paulson@15409
  1284
 apply (cases "finite B") 
paulson@15409
  1285
  apply (simp add: setprod_Sigma)
paulson@15409
  1286
 apply (cases "A={}", simp)
paulson@15409
  1287
 apply (simp add: setprod_1) 
paulson@15409
  1288
apply (auto simp add: setprod_def
paulson@15409
  1289
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1290
done
nipkow@15402
  1291
nipkow@15402
  1292
lemma setprod_timesf:
paulson@15409
  1293
     "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
nipkow@15402
  1294
by(simp add:setprod_def ACe.fold_distrib[OF ACe_mult])
nipkow@15402
  1295
nipkow@15402
  1296
nipkow@15402
  1297
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1298
nipkow@15402
  1299
lemma setprod_eq_1_iff [simp]:
nipkow@15402
  1300
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
nipkow@15402
  1301
  by (induct set: Finites) auto
nipkow@15402
  1302
nipkow@15402
  1303
lemma setprod_zero:
nipkow@15402
  1304
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
nipkow@15402
  1305
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1306
  apply (erule disjE, auto)
nipkow@15402
  1307
  done
nipkow@15402
  1308
nipkow@15402
  1309
lemma setprod_nonneg [rule_format]:
nipkow@15402
  1310
     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
nipkow@15402
  1311
  apply (case_tac "finite A")
nipkow@15402
  1312
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1313
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
nipkow@15402
  1314
  apply (rule mult_mono, assumption+)
nipkow@15402
  1315
  apply (auto simp add: setprod_def)
nipkow@15402
  1316
  done
nipkow@15402
  1317
nipkow@15402
  1318
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
nipkow@15402
  1319
     --> 0 < setprod f A"
nipkow@15402
  1320
  apply (case_tac "finite A")
nipkow@15402
  1321
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1322
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
nipkow@15402
  1323
  apply (rule mult_strict_mono, assumption+)
nipkow@15402
  1324
  apply (auto simp add: setprod_def)
nipkow@15402
  1325
  done
nipkow@15402
  1326
nipkow@15402
  1327
lemma setprod_nonzero [rule_format]:
nipkow@15402
  1328
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1329
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
nipkow@15402
  1330
  apply (erule finite_induct, auto)
nipkow@15402
  1331
  done
nipkow@15402
  1332
nipkow@15402
  1333
lemma setprod_zero_eq:
nipkow@15402
  1334
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1335
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
nipkow@15402
  1336
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
nipkow@15402
  1337
  done
nipkow@15402
  1338
nipkow@15402
  1339
lemma setprod_nonzero_field:
nipkow@15402
  1340
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
nipkow@15402
  1341
  apply (rule setprod_nonzero, auto)
nipkow@15402
  1342
  done
nipkow@15402
  1343
nipkow@15402
  1344
lemma setprod_zero_eq_field:
nipkow@15402
  1345
    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
nipkow@15402
  1346
  apply (rule setprod_zero_eq, auto)
nipkow@15402
  1347
  done
nipkow@15402
  1348
nipkow@15402
  1349
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
nipkow@15402
  1350
    (setprod f (A Un B) :: 'a ::{field})
nipkow@15402
  1351
      = setprod f A * setprod f B / setprod f (A Int B)"
nipkow@15402
  1352
  apply (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1353
  apply (subgoal_tac "finite (A Int B)")
nipkow@15402
  1354
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
nipkow@15402
  1355
  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
nipkow@15402
  1356
  done
nipkow@15402
  1357
nipkow@15402
  1358
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@15402
  1359
    (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@15402
  1360
      (if a:A then setprod f A / f a else setprod f A)"
nipkow@15402
  1361
  apply (erule finite_induct)
nipkow@15402
  1362
   apply (auto simp add: insert_Diff_if)
nipkow@15402
  1363
  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
nipkow@15402
  1364
  apply (erule ssubst)
nipkow@15402
  1365
  apply (subst times_divide_eq_right [THEN sym])
nipkow@15402
  1366
  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
nipkow@15402
  1367
  done
nipkow@15402
  1368
nipkow@15402
  1369
lemma setprod_inversef: "finite A ==>
nipkow@15402
  1370
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
nipkow@15402
  1371
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@15402
  1372
  apply (erule finite_induct)
nipkow@15402
  1373
  apply (simp, simp)
nipkow@15402
  1374
  done
nipkow@15402
  1375
nipkow@15402
  1376
lemma setprod_dividef:
nipkow@15402
  1377
     "[|finite A;
nipkow@15402
  1378
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
nipkow@15402
  1379
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@15402
  1380
  apply (subgoal_tac
nipkow@15402
  1381
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@15402
  1382
  apply (erule ssubst)
nipkow@15402
  1383
  apply (subst divide_inverse)
nipkow@15402
  1384
  apply (subst setprod_timesf)
nipkow@15402
  1385
  apply (subst setprod_inversef, assumption+, rule refl)
nipkow@15402
  1386
  apply (rule setprod_cong, rule refl)
nipkow@15402
  1387
  apply (subst divide_inverse, auto)
nipkow@15402
  1388
  done
nipkow@15402
  1389
wenzelm@12396
  1390
subsection {* Finite cardinality *}
wenzelm@12396
  1391
nipkow@15402
  1392
text {* This definition, although traditional, is ugly to work with:
nipkow@15402
  1393
@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
nipkow@15402
  1394
But now that we have @{text setsum} things are easy:
wenzelm@12396
  1395
*}
wenzelm@12396
  1396
wenzelm@12396
  1397
constdefs
wenzelm@12396
  1398
  card :: "'a set => nat"
nipkow@15402
  1399
  "card A == setsum (%x. 1::nat) A"
wenzelm@12396
  1400
wenzelm@12396
  1401
lemma card_empty [simp]: "card {} = 0"
nipkow@15402
  1402
  by (simp add: card_def)
nipkow@15402
  1403
paulson@15409
  1404
lemma card_infinite [simp]: "~ finite A ==> card A = 0"
paulson@15409
  1405
  by (simp add: card_def)
paulson@15409
  1406
nipkow@15402
  1407
lemma card_eq_setsum: "card A = setsum (%x. 1) A"
nipkow@15402
  1408
by (simp add: card_def)
wenzelm@12396
  1409
wenzelm@12396
  1410
lemma card_insert_disjoint [simp]:
wenzelm@12396
  1411
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
nipkow@15402
  1412
by(simp add: card_def ACf.fold_insert[OF ACf_add])
nipkow@15402
  1413
nipkow@15402
  1414
lemma card_insert_if:
nipkow@15402
  1415
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
nipkow@15402
  1416
  by (simp add: insert_absorb)
wenzelm@12396
  1417
wenzelm@12396
  1418
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
  1419
  apply auto
paulson@14208
  1420
  apply (drule_tac a = x in mk_disjoint_insert, clarify)
nipkow@15402
  1421
  apply (auto)
wenzelm@12396
  1422
  done
wenzelm@12396
  1423
paulson@15409
  1424
lemma card_eq_0_iff: "(card A = 0) = (A = {} | ~ finite A)"
paulson@15409
  1425
by auto
paulson@15409
  1426
wenzelm@12396
  1427
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
  1428
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
  1429
apply(simp del:insert_Diff_single)
nipkow@14302
  1430
done
wenzelm@12396
  1431
wenzelm@12396
  1432
lemma card_Diff_singleton:
wenzelm@12396
  1433
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
wenzelm@12396
  1434
  by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
  1435
wenzelm@12396
  1436
lemma card_Diff_singleton_if:
wenzelm@12396
  1437
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
wenzelm@12396
  1438
  by (simp add: card_Diff_singleton)
wenzelm@12396
  1439
wenzelm@12396
  1440
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
wenzelm@12396
  1441
  by (simp add: card_insert_if card_Suc_Diff1)
wenzelm@12396
  1442
wenzelm@12396
  1443
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
wenzelm@12396
  1444
  by (simp add: card_insert_if)
wenzelm@12396
  1445
nipkow@15402
  1446
lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
nipkow@15402
  1447
by (simp add: card_def setsum_mono2_nat)
nipkow@15402
  1448
wenzelm@12396
  1449
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
paulson@14208
  1450
  apply (induct set: Finites, simp, clarify)
wenzelm@12396
  1451
  apply (subgoal_tac "finite A & A - {x} <= F")
paulson@14208
  1452
   prefer 2 apply (blast intro: finite_subset, atomize)
wenzelm@12396
  1453
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
  1454
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
paulson@14208
  1455
  apply (case_tac "card A", auto)
wenzelm@12396
  1456
  done
wenzelm@12396
  1457
wenzelm@12396
  1458
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
wenzelm@12396
  1459
  apply (simp add: psubset_def linorder_not_le [symmetric])
wenzelm@12396
  1460
  apply (blast dest: card_seteq)
wenzelm@12396
  1461
  done
wenzelm@12396
  1462
wenzelm@12396
  1463
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
  1464
    ==> card A + card B = card (A Un B) + card (A Int B)"
nipkow@15402
  1465
by(simp add:card_def setsum_Un_Int)
wenzelm@12396
  1466
wenzelm@12396
  1467
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
  1468
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
wenzelm@12396
  1469
  by (simp add: card_Un_Int)
wenzelm@12396
  1470
wenzelm@12396
  1471
lemma card_Diff_subset:
nipkow@15402
  1472
  "finite B ==> B <= A ==> card (A - B) = card A - card B"
nipkow@15402
  1473
by(simp add:card_def setsum_diff_nat)
wenzelm@12396
  1474
wenzelm@12396
  1475
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
  1476
  apply (rule Suc_less_SucD)
wenzelm@12396
  1477
  apply (simp add: card_Suc_Diff1)
wenzelm@12396
  1478
  done
wenzelm@12396
  1479
wenzelm@12396
  1480
lemma card_Diff2_less:
wenzelm@12396
  1481
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
  1482
  apply (case_tac "x = y")
wenzelm@12396
  1483
   apply (simp add: card_Diff1_less)
wenzelm@12396
  1484
  apply (rule less_trans)
wenzelm@12396
  1485
   prefer 2 apply (auto intro!: card_Diff1_less)
wenzelm@12396
  1486
  done
wenzelm@12396
  1487
wenzelm@12396
  1488
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
  1489
  apply (case_tac "x : A")
wenzelm@12396
  1490
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
  1491
  done
wenzelm@12396
  1492
wenzelm@12396
  1493
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
  1494
by (erule psubsetI, blast)
wenzelm@12396
  1495
paulson@14889
  1496
lemma insert_partition:
nipkow@15402
  1497
  "\<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
  1498
  \<Longrightarrow> x \<inter> \<Union> F = {}"
paulson@14889
  1499
by auto
paulson@14889
  1500
paulson@14889
  1501
(* main cardinality theorem *)
paulson@14889
  1502
lemma card_partition [rule_format]:
paulson@14889
  1503
     "finite C ==>  
paulson@14889
  1504
        finite (\<Union> C) -->  
paulson@14889
  1505
        (\<forall>c\<in>C. card c = k) -->   
paulson@14889
  1506
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
paulson@14889
  1507
        k * card(C) = card (\<Union> C)"
paulson@14889
  1508
apply (erule finite_induct, simp)
paulson@14889
  1509
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
  1510
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
  1511
done
paulson@14889
  1512
wenzelm@12396
  1513
paulson@15409
  1514
lemma setsum_constant_nat: "(\<Sum>x\<in>A. y) = (card A) * y"
nipkow@15402
  1515
  -- {* Generalized to any @{text comm_semiring_1_cancel} in
nipkow@15402
  1516
        @{text IntDef} as @{text setsum_constant}. *}
paulson@15409
  1517
apply (cases "finite A") 
paulson@15409
  1518
apply (erule finite_induct, auto)
paulson@15409
  1519
done
nipkow@15402
  1520
nipkow@15402
  1521
lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
nipkow@15402
  1522
  apply (erule finite_induct)
nipkow@15402
  1523
  apply (auto simp add: power_Suc)
nipkow@15402
  1524
  done
nipkow@15402
  1525
nipkow@15402
  1526
nipkow@15402
  1527
subsubsection {* Cardinality of unions *}
nipkow@15402
  1528
nipkow@15402
  1529
lemma card_UN_disjoint:
nipkow@15402
  1530
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1531
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1532
      card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
nipkow@15402
  1533
  apply (simp add: card_def)
nipkow@15402
  1534
  apply (subgoal_tac
nipkow@15402
  1535
           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
nipkow@15402
  1536
  apply (simp add: setsum_UN_disjoint)
nipkow@15402
  1537
  apply (simp add: setsum_constant_nat cong: setsum_cong)
nipkow@15402
  1538
  done
nipkow@15402
  1539
nipkow@15402
  1540
lemma card_Union_disjoint:
nipkow@15402
  1541
  "finite C ==> (ALL A:C. finite A) ==>
nipkow@15402
  1542
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
nipkow@15402
  1543
      card (Union C) = setsum card C"
nipkow@15402
  1544
  apply (frule card_UN_disjoint [of C id])
nipkow@15402
  1545
  apply (unfold Union_def id_def, assumption+)
nipkow@15402
  1546
  done
nipkow@15402
  1547
wenzelm@12396
  1548
subsubsection {* Cardinality of image *}
wenzelm@12396
  1549
wenzelm@12396
  1550
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
paulson@14208
  1551
  apply (induct set: Finites, simp)
wenzelm@12396
  1552
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
  1553
  done
wenzelm@12396
  1554
nipkow@15402
  1555
lemma card_image: "inj_on f A ==> card (f ` A) = card A"
nipkow@15402
  1556
by(simp add:card_def setsum_reindex o_def)
wenzelm@12396
  1557
wenzelm@12396
  1558
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
wenzelm@12396
  1559
  by (simp add: card_seteq card_image)
wenzelm@12396
  1560
nipkow@15111
  1561
lemma eq_card_imp_inj_on:
nipkow@15111
  1562
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
nipkow@15111
  1563
apply(induct rule:finite_induct)
nipkow@15111
  1564
 apply simp
nipkow@15111
  1565
apply(frule card_image_le[where f = f])
nipkow@15111
  1566
apply(simp add:card_insert_if split:if_splits)
nipkow@15111
  1567
done
nipkow@15111
  1568
nipkow@15111
  1569
lemma inj_on_iff_eq_card:
nipkow@15111
  1570
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
nipkow@15111
  1571
by(blast intro: card_image eq_card_imp_inj_on)
nipkow@15111
  1572
wenzelm@12396
  1573
nipkow@15402
  1574
lemma card_inj_on_le:
nipkow@15402
  1575
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
nipkow@15402
  1576
apply (subgoal_tac "finite A") 
nipkow@15402
  1577
 apply (force intro: card_mono simp add: card_image [symmetric])
nipkow@15402
  1578
apply (blast intro: finite_imageD dest: finite_subset) 
nipkow@15402
  1579
done
nipkow@15402
  1580
nipkow@15402
  1581
lemma card_bij_eq:
nipkow@15402
  1582
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
nipkow@15402
  1583
       finite A; finite B |] ==> card A = card B"
nipkow@15402
  1584
  by (auto intro: le_anti_sym card_inj_on_le)
nipkow@15402
  1585
nipkow@15402
  1586
nipkow@15402
  1587
subsubsection {* Cardinality of products *}
nipkow@15402
  1588
nipkow@15402
  1589
(*
nipkow@15402
  1590
lemma SigmaI_insert: "y \<notin> A ==>
nipkow@15402
  1591
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
nipkow@15402
  1592
  by auto
nipkow@15402
  1593
*)
nipkow@15402
  1594
nipkow@15402
  1595
lemma card_SigmaI [simp]:
nipkow@15402
  1596
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
nipkow@15402
  1597
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
nipkow@15402
  1598
by(simp add:card_def setsum_Sigma)
nipkow@15402
  1599
paulson@15409
  1600
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
paulson@15409
  1601
apply (cases "finite A") 
paulson@15409
  1602
apply (cases "finite B") 
paulson@15409
  1603
  apply (simp add: setsum_constant_nat) 
paulson@15409
  1604
apply (auto simp add: card_eq_0_iff
paulson@15409
  1605
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1606
done
nipkow@15402
  1607
nipkow@15402
  1608
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
paulson@15409
  1609
by (simp add: card_cartesian_product) 
paulson@15409
  1610
nipkow@15402
  1611
nipkow@15402
  1612
wenzelm@12396
  1613
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
  1614
wenzelm@12396
  1615
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
wenzelm@12396
  1616
  apply (induct set: Finites)
wenzelm@12396
  1617
   apply (simp_all add: Pow_insert)
paulson@14208
  1618
  apply (subst card_Un_disjoint, blast)
paulson@14208
  1619
    apply (blast intro: finite_imageI, blast)
wenzelm@12396
  1620
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
  1621
   apply (simp add: card_image Pow_insert)
wenzelm@12396
  1622
  apply (unfold inj_on_def)
wenzelm@12396
  1623
  apply (blast elim!: equalityE)
wenzelm@12396
  1624
  done
wenzelm@12396
  1625
nipkow@15392
  1626
text {* Relates to equivalence classes.  Based on a theorem of
nipkow@15392
  1627
F. Kammüller's.  *}
wenzelm@12396
  1628
wenzelm@12396
  1629
lemma dvd_partition:
nipkow@15392
  1630
  "finite (Union C) ==>
wenzelm@12396
  1631
    ALL c : C. k dvd card c ==>
paulson@14430
  1632
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
  1633
  k dvd card (Union C)"
nipkow@15392
  1634
apply(frule finite_UnionD)
nipkow@15392
  1635
apply(rotate_tac -1)
paulson@14208
  1636
  apply (induct set: Finites, simp_all, clarify)
wenzelm@12396
  1637
  apply (subst card_Un_disjoint)
wenzelm@12396
  1638
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
  1639
  done
wenzelm@12396
  1640
wenzelm@12396
  1641
nipkow@15392
  1642
subsubsection {* Theorems about @{text "choose"} *}
wenzelm@12396
  1643
wenzelm@12396
  1644
text {*
nipkow@15392
  1645
  \medskip Basic theorem about @{text "choose"}.  By Florian
nipkow@15392
  1646
  Kamm\"uller, tidied by LCP.
wenzelm@12396
  1647
*}
wenzelm@12396
  1648
nipkow@15392
  1649
lemma card_s_0_eq_empty:
nipkow@15392
  1650
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
nipkow@15392
  1651
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
nipkow@15392
  1652
  apply (simp cong add: rev_conj_cong)
nipkow@15392
  1653
  done
wenzelm@12396
  1654
nipkow@15392
  1655
lemma choose_deconstruct: "finite M ==> x \<notin> M
nipkow@15392
  1656
  ==> {s. s <= insert x M & card(s) = Suc k}
nipkow@15392
  1657
       = {s. s <= M & card(s) = Suc k} Un
nipkow@15392
  1658
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
nipkow@15392
  1659
  apply safe
nipkow@15392
  1660
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
nipkow@15392
  1661
  apply (drule_tac x = "xa - {x}" in spec)
nipkow@15392
  1662
  apply (subgoal_tac "x \<notin> xa", auto)
nipkow@15392
  1663
  apply (erule rev_mp, subst card_Diff_singleton)
nipkow@15392
  1664
  apply (auto intro: finite_subset)
wenzelm@12396
  1665
  done
wenzelm@12396
  1666
nipkow@15392
  1667
text{*There are as many subsets of @{term A} having cardinality @{term k}
nipkow@15392
  1668
 as there are sets obtained from the former by inserting a fixed element
nipkow@15392
  1669
 @{term x} into each.*}
nipkow@15392
  1670
lemma constr_bij:
nipkow@15392
  1671
   "[|finite A; x \<notin> A|] ==>
nipkow@15392
  1672
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
nipkow@15392
  1673
    card {B. B <= A & card(B) = k}"
nipkow@15392
  1674
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
nipkow@15392
  1675
       apply (auto elim!: equalityE simp add: inj_on_def)
nipkow@15392
  1676
    apply (subst Diff_insert0, auto)
nipkow@15392
  1677
   txt {* finiteness of the two sets *}
nipkow@15392
  1678
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
nipkow@15392
  1679
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
nipkow@15392
  1680
   apply fast+
wenzelm@12396
  1681
  done
wenzelm@12396
  1682
nipkow@15392
  1683
text {*
nipkow@15392
  1684
  Main theorem: combinatorial statement about number of subsets of a set.
nipkow@15392
  1685
*}
wenzelm@12396
  1686
nipkow@15392
  1687
lemma n_sub_lemma:
nipkow@15392
  1688
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
nipkow@15392
  1689
  apply (induct k)
nipkow@15392
  1690
   apply (simp add: card_s_0_eq_empty, atomize)
nipkow@15392
  1691
  apply (rotate_tac -1, erule finite_induct)
nipkow@15392
  1692
   apply (simp_all (no_asm_simp) cong add: conj_cong
nipkow@15392
  1693
     add: card_s_0_eq_empty choose_deconstruct)
nipkow@15392
  1694
  apply (subst card_Un_disjoint)
nipkow@15392
  1695
     prefer 4 apply (force simp add: constr_bij)
nipkow@15392
  1696
    prefer 3 apply force
nipkow@15392
  1697
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
nipkow@15392
  1698
     finite_subset [of _ "Pow (insert x F)", standard])
nipkow@15392
  1699
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@12396
  1700
  done
wenzelm@12396
  1701
nipkow@15392
  1702
theorem n_subsets:
nipkow@15392
  1703
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
nipkow@15392
  1704
  by (simp add: n_sub_lemma)
nipkow@15392
  1705
nipkow@15392
  1706
nipkow@15392
  1707
subsection{* A fold functional for non-empty sets *}
nipkow@15392
  1708
nipkow@15392
  1709
text{* Does not require start value. *}
wenzelm@12396
  1710
nipkow@15392
  1711
consts
nipkow@15392
  1712
  foldSet1 :: "('a => 'a => 'a) => ('a set \<times> 'a) set"
nipkow@15392
  1713
nipkow@15392
  1714
inductive "foldSet1 f"
nipkow@15392
  1715
intros
nipkow@15392
  1716
foldSet1_singletonI [intro]: "({a}, a) : foldSet1 f"
nipkow@15392
  1717
foldSet1_insertI [intro]:
nipkow@15392
  1718
 "\<lbrakk> (A, x) : foldSet1 f; a \<notin> A; A \<noteq> {} \<rbrakk>
nipkow@15392
  1719
  \<Longrightarrow> (insert a A, f a x) : foldSet1 f"
wenzelm@12396
  1720
nipkow@15392
  1721
constdefs
nipkow@15392
  1722
  fold1 :: "('a => 'a => 'a) => 'a set => 'a"
nipkow@15392
  1723
  "fold1 f A == THE x. (A, x) : foldSet1 f"
nipkow@15392
  1724
nipkow@15392
  1725
lemma foldSet1_nonempty:
nipkow@15392
  1726
 "(A, x) : foldSet1 f \<Longrightarrow> A \<noteq> {}"
nipkow@15392
  1727
by(erule foldSet1.cases, simp_all) 
nipkow@15392
  1728
wenzelm@12396
  1729
nipkow@15392
  1730
inductive_cases empty_foldSet1E [elim!]: "({}, x) : foldSet1 f"
nipkow@15392
  1731
nipkow@15392
  1732
lemma foldSet1_sing[iff]: "(({a},b) : foldSet1 f) = (a = b)"
nipkow@15392
  1733
apply(rule iffI)
nipkow@15392
  1734
 prefer 2 apply fast
nipkow@15392
  1735
apply (erule foldSet1.cases)
nipkow@15392
  1736
 apply blast
nipkow@15392
  1737
apply (erule foldSet1.cases)
nipkow@15392
  1738
 apply blast
nipkow@15392
  1739
apply blast
nipkow@15376
  1740
done
wenzelm@12396
  1741
nipkow@15392
  1742
lemma Diff1_foldSet1:
nipkow@15392
  1743
  "(A - {x}, y) : foldSet1 f ==> x: A ==> (A, f x y) : foldSet1 f"
nipkow@15392
  1744
by (erule insert_Diff [THEN subst], rule foldSet1.intros,
nipkow@15392
  1745
    auto dest!:foldSet1_nonempty)
wenzelm@12396
  1746
nipkow@15392
  1747
lemma foldSet1_imp_finite: "(A, x) : foldSet1 f ==> finite A"
nipkow@15392
  1748
  by (induct set: foldSet1) auto
wenzelm@12396
  1749
nipkow@15392
  1750
lemma finite_nonempty_imp_foldSet1:
nipkow@15392
  1751
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. (A, x) : foldSet1 f"
nipkow@15392
  1752
  by (induct set: Finites) auto
nipkow@15376
  1753
nipkow@15392
  1754
lemma (in ACf) foldSet1_determ_aux:
nipkow@15392
  1755
  "!!A x y. \<lbrakk> card A < n; (A, x) : foldSet1 f; (A, y) : foldSet1 f \<rbrakk> \<Longrightarrow> y = x"
nipkow@15392
  1756
proof (induct n)
nipkow@15392
  1757
  case 0 thus ?case by simp
nipkow@15392
  1758
next
nipkow@15392
  1759
  case (Suc n)
nipkow@15392
  1760
  have IH: "!!A x y. \<lbrakk>card A < n; (A, x) \<in> foldSet1 f; (A, y) \<in> foldSet1 f\<rbrakk>
nipkow@15392
  1761
           \<Longrightarrow> y = x" and card: "card A < Suc n"
nipkow@15392
  1762
  and Afoldx: "(A, x) \<in> foldSet1 f" and Afoldy: "(A, y) \<in> foldSet1 f" .
nipkow@15392
  1763
  from card have "card A < n \<or> card A = n" by arith
nipkow@15392
  1764
  thus ?case
nipkow@15392
  1765
  proof
nipkow@15392
  1766
    assume less: "card A < n"
nipkow@15392
  1767
    show ?thesis by(rule IH[OF less Afoldx Afoldy])
nipkow@15392
  1768
  next
nipkow@15392
  1769
    assume cardA: "card A = n"
nipkow@15392
  1770
    show ?thesis
nipkow@15392
  1771
    proof (rule foldSet1.cases[OF Afoldx])
nipkow@15392
  1772
      fix a assume "(A, x) = ({a}, a)"
nipkow@15392
  1773
      thus "y = x" using Afoldy by (simp add:foldSet1_sing)
nipkow@15392
  1774
    next
nipkow@15392
  1775
      fix Ax ax x'
nipkow@15392
  1776
      assume eq1: "(A, x) = (insert ax Ax, ax \<cdot> x')"
nipkow@15392
  1777
	and x': "(Ax, x') \<in> foldSet1 f" and notinx: "ax \<notin> Ax"
nipkow@15392
  1778
	and Axnon: "Ax \<noteq> {}"
nipkow@15392
  1779
      hence A1: "A = insert ax Ax" and x: "x = ax \<cdot> x'" by auto
nipkow@15392
  1780
      show ?thesis
nipkow@15392
  1781
      proof (rule foldSet1.cases[OF Afoldy])
nipkow@15392
  1782
	fix ay assume "(A, y) = ({ay}, ay)"
nipkow@15392
  1783
	thus ?thesis using eq1 x' Axnon notinx
nipkow@15392
  1784
	  by (fastsimp simp:foldSet1_sing)
nipkow@15392
  1785
      next
nipkow@15392
  1786
	fix Ay ay y'
nipkow@15392
  1787
	assume eq2: "(A, y) = (insert ay Ay, ay \<cdot> y')"
nipkow@15392
  1788
	  and y': "(Ay, y') \<in> foldSet1 f" and notiny: "ay \<notin> Ay"
nipkow@15392
  1789
	  and Aynon: "Ay \<noteq> {}"
nipkow@15392
  1790
	hence A2: "A = insert ay Ay" and y: "y = ay \<cdot> y'" by auto
nipkow@15392
  1791
	have finA: "finite A" by(rule foldSet1_imp_finite[OF Afoldx])
nipkow@15392
  1792
	with cardA A1 notinx have less: "card Ax < n" by simp
nipkow@15392
  1793
	show ?thesis
nipkow@15392
  1794
	proof cases
nipkow@15392
  1795
	  assume "ax = ay"
nipkow@15392
  1796
	  then moreover have "Ax = Ay" using A1 A2 notinx notiny by auto
nipkow@15392
  1797
	  ultimately show ?thesis using IH[OF less x'] y' eq1 eq2 by auto
nipkow@15392
  1798
	next
nipkow@15392
  1799
	  assume diff: "ax \<noteq> ay"
nipkow@15392
  1800
	  let ?B = "Ax - {ay}"
nipkow@15392
  1801
	  have Ax: "Ax = insert ay ?B" and Ay: "Ay = insert ax ?B"
nipkow@15392
  1802
	    using A1 A2 notinx notiny diff by(blast elim!:equalityE)+
nipkow@15392
  1803
	  show ?thesis
nipkow@15392
  1804
	  proof cases
nipkow@15392
  1805
	    assume "?B = {}"
nipkow@15392
  1806
	    with Ax Ay show ?thesis using x' y' x y by(simp add:commute)
nipkow@15392
  1807
	  next
nipkow@15392
  1808
	    assume Bnon: "?B \<noteq> {}"
nipkow@15392
  1809
	    moreover have "finite ?B" using finA A1 by simp
nipkow@15392
  1810
	    ultimately obtain b where Bfoldb: "(?B,b) \<in> foldSet1 f"
nipkow@15392
  1811
	      using finite_nonempty_imp_foldSet1 by(blast)
nipkow@15392
  1812
	    moreover have ayinAx: "ay \<in> Ax" using Ax by(auto)
nipkow@15392
  1813
	    ultimately have "(Ax,ay\<cdot>b) \<in> foldSet1 f" by(rule Diff1_foldSet1)
nipkow@15392
  1814
	    hence "ay\<cdot>b = x'" by(rule IH[OF less x'])
nipkow@15392
  1815
            moreover have "ax\<cdot>b = y'"
nipkow@15392
  1816
	    proof (rule IH[OF _ y'])
nipkow@15392
  1817
	      show "card Ay < n" using Ay cardA A1 notinx finA ayinAx
nipkow@15392
  1818
		by(auto simp:card_Diff1_less)
nipkow@15392
  1819
	    next
nipkow@15392
  1820
	      show "(Ay,ax\<cdot>b) \<in> foldSet1 f" using Ay notinx Bfoldb Bnon
nipkow@15392
  1821
		by fastsimp
nipkow@15392
  1822
	    qed
nipkow@15392
  1823
	    ultimately show ?thesis using x y by(auto simp:AC)
nipkow@15392
  1824
	  qed
nipkow@15392
  1825
	qed
nipkow@15392
  1826
      qed
nipkow@15392
  1827
    qed
nipkow@15392
  1828
  qed
wenzelm@12396
  1829
qed
wenzelm@12396
  1830
nipkow@15392
  1831
nipkow@15392
  1832
lemma (in ACf) foldSet1_determ:
nipkow@15392
  1833
  "(A, x) : foldSet1 f ==> (A, y) : foldSet1 f ==> y = x"
nipkow@15392
  1834
by (blast intro: foldSet1_determ_aux [rule_format])
nipkow@15392
  1835
nipkow@15392
  1836
lemma (in ACf) foldSet1_equality: "(A, y) : foldSet1 f ==> fold1 f A = y"
nipkow@15392
  1837
  by (unfold fold1_def) (blast intro: foldSet1_determ)
nipkow@15392
  1838
nipkow@15392
  1839
lemma fold1_singleton: "fold1 f {a} = a"
nipkow@15392
  1840
  by (unfold fold1_def) blast
wenzelm@12396
  1841
nipkow@15392
  1842
lemma (in ACf) foldSet1_insert_aux: "x \<notin> A ==> A \<noteq> {} \<Longrightarrow> 
nipkow@15392
  1843
    ((insert x A, v) : foldSet1 f) =
nipkow@15392
  1844
    (EX y. (A, y) : foldSet1 f & v = f x y)"
nipkow@15392
  1845
apply auto
nipkow@15392
  1846
apply (rule_tac A1 = A and f1 = f in finite_nonempty_imp_foldSet1 [THEN exE])
nipkow@15392
  1847
  apply (fastsimp dest: foldSet1_imp_finite)
nipkow@15392
  1848
 apply blast
nipkow@15392
  1849
apply (blast intro: foldSet1_determ)
nipkow@15392
  1850
done
nipkow@15376
  1851
nipkow@15392
  1852
lemma (in ACf) fold1_insert:
nipkow@15392
  1853
  "finite A ==> x \<notin> A ==> A \<noteq> {} \<Longrightarrow> fold1 f (insert x A) = f x (fold1 f A)"
nipkow@15392
  1854
apply (unfold fold1_def)
nipkow@15392
  1855
apply (simp add: foldSet1_insert_aux)
nipkow@15392
  1856
apply (rule the_equality)
nipkow@15392
  1857
apply (auto intro: finite_nonempty_imp_foldSet1
nipkow@15392
  1858
    cong add: conj_cong simp add: fold1_def [symmetric] foldSet1_equality)
nipkow@15392
  1859
done
nipkow@15376
  1860
nipkow@15392
  1861
locale ACIf = ACf +
nipkow@15392
  1862
  assumes idem: "x \<cdot> x = x"
wenzelm@12396
  1863
nipkow@15392
  1864
lemma (in ACIf) fold1_insert2:
nipkow@15392
  1865
assumes finA: "finite A" and nonA: "A \<noteq> {}"
nipkow@15392
  1866
shows "fold1 f (insert a A) = f a (fold1 f A)"
nipkow@15392
  1867
proof cases
nipkow@15392
  1868
  assume "a \<in> A"
nipkow@15392
  1869
  then obtain B where A: "A = insert a B" and disj: "a \<notin> B"
nipkow@15392
  1870
    by(blast dest: mk_disjoint_insert)
nipkow@15392
  1871
  show ?thesis
nipkow@15392
  1872
  proof cases
nipkow@15392
  1873
    assume "B = {}"
nipkow@15392
  1874
    thus ?thesis using A by(simp add:idem fold1_singleton)
nipkow@15392
  1875
  next
nipkow@15392
  1876
    assume nonB: "B \<noteq> {}"
nipkow@15392
  1877
    from finA A have finB: "finite B" by(blast intro: finite_subset)
nipkow@15392
  1878
    have "fold1 f (insert a A) = fold1 f (insert a B)" using A by simp
nipkow@15392
  1879
    also have "\<dots> = f a (fold1 f B)"
nipkow@15392
  1880
      using finB nonB disj by(simp add: fold1_insert)
nipkow@15392
  1881
    also have "\<dots> = f a (fold1 f A)"
nipkow@15392
  1882
      using A finB nonB disj by(simp add:idem fold1_insert assoc[symmetric])
nipkow@15392
  1883
    finally show ?thesis .
nipkow@15392
  1884
  qed
nipkow@15392
  1885
next
nipkow@15392
  1886
  assume "a \<notin> A"
nipkow@15392
  1887
  with finA nonA show ?thesis by(simp add:fold1_insert)
nipkow@15392
  1888
qed
nipkow@15392
  1889
nipkow@15376
  1890
nipkow@15392
  1891
text{* Now the recursion rules for definitions: *}
nipkow@15392
  1892
nipkow@15392
  1893
lemma fold1_singleton_def: "g \<equiv> fold1 f \<Longrightarrow> g {a} = a"
nipkow@15392
  1894
by(simp add:fold1_singleton)
nipkow@15392
  1895
nipkow@15392
  1896
lemma (in ACf) fold1_insert_def:
nipkow@15392
  1897
  "\<lbrakk> g \<equiv> fold1 f; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g(insert x A) = x \<cdot> (g A)"
nipkow@15392
  1898
by(simp add:fold1_insert)
nipkow@15392
  1899
nipkow@15392
  1900
lemma (in ACIf) fold1_insert2_def:
nipkow@15392
  1901
  "\<lbrakk> g \<equiv> fold1 f; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g(insert x A) = x \<cdot> (g A)"
nipkow@15392
  1902
by(simp add:fold1_insert2)
nipkow@15392
  1903
nipkow@15376
  1904
nipkow@15392
  1905
subsection{*Min and Max*}
nipkow@15392
  1906
nipkow@15392
  1907
text{* As an application of @{text fold1} we define the minimal and
nipkow@15392
  1908
maximal element of a (non-empty) set over a linear order. First we
nipkow@15392
  1909
show that @{text min} and @{text max} are ACI: *}
nipkow@15392
  1910
nipkow@15392
  1911
lemma ACf_min: "ACf(min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  1912
apply(rule ACf.intro)
nipkow@15392
  1913
apply(auto simp:min_def)
nipkow@15392
  1914
done
nipkow@15392
  1915
nipkow@15392
  1916
lemma ACIf_min: "ACIf(min:: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  1917
apply(rule ACIf.intro[OF ACf_min])
nipkow@15392
  1918
apply(rule ACIf_axioms.intro)
nipkow@15392
  1919
apply(auto simp:min_def)
nipkow@15376
  1920
done
nipkow@15376
  1921
nipkow@15392
  1922
lemma ACf_max: "ACf(max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  1923
apply(rule ACf.intro)
nipkow@15392
  1924
apply(auto simp:max_def)
nipkow@15392
  1925
done
nipkow@15392
  1926
nipkow@15392
  1927
lemma ACIf_max: "ACIf(max:: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  1928
apply(rule ACIf.intro[OF ACf_max])
nipkow@15392
  1929
apply(rule ACIf_axioms.intro)
nipkow@15392
  1930
apply(auto simp:max_def)
nipkow@15376
  1931
done
wenzelm@12396
  1932
nipkow@15392
  1933
text{* Now we make the definitions: *}
nipkow@15392
  1934
nipkow@15392
  1935
constdefs
nipkow@15392
  1936
  Min :: "('a::linorder)set => 'a"
nipkow@15392
  1937
  "Min  ==  fold1 min"
nipkow@15392
  1938
nipkow@15392
  1939
  Max :: "('a::linorder)set => 'a"
nipkow@15392
  1940
  "Max  ==  fold1 max"
nipkow@15392
  1941
nipkow@15402
  1942
text{* Now we instantiate the recursion equations and declare them
nipkow@15392
  1943
simplification rules: *}
nipkow@15392
  1944
nipkow@15392
  1945
declare
nipkow@15392
  1946
  fold1_singleton_def[OF Min_def, simp]
nipkow@15392
  1947
  ACIf.fold1_insert2_def[OF ACIf_min Min_def, simp]
nipkow@15392
  1948
  fold1_singleton_def[OF Max_def, simp]
nipkow@15392
  1949
  ACIf.fold1_insert2_def[OF ACIf_max Max_def, simp]
nipkow@15392
  1950
nipkow@15392
  1951
text{* Now we prove some properties by induction: *}
nipkow@15392
  1952
nipkow@15392
  1953
lemma Min_in [simp]:
nipkow@15392
  1954
  assumes a: "finite S"
nipkow@15392
  1955
  shows "S \<noteq> {} \<Longrightarrow> Min S \<in> S"
nipkow@15392
  1956
using a
nipkow@15392
  1957
proof induct
nipkow@15392
  1958
  case empty thus ?case by simp
nipkow@15392
  1959
next
nipkow@15392
  1960
  case (insert x S)
nipkow@15392
  1961
  show ?case
nipkow@15392
  1962
  proof cases
nipkow@15392
  1963
    assume "S = {}" thus ?thesis by simp
nipkow@15392
  1964
  next
nipkow@15392
  1965
    assume "S \<noteq> {}" thus ?thesis using insert by (simp add:min_def)
nipkow@15392
  1966
  qed
nipkow@15392
  1967
qed
nipkow@15392
  1968
nipkow@15392
  1969
lemma Min_le [simp]:
nipkow@15392
  1970
  assumes a: "finite S"
nipkow@15392
  1971
  shows "\<lbrakk> S \<noteq> {}; x \<in> S \<rbrakk> \<Longrightarrow> Min S \<le> x"
nipkow@15392
  1972
using a
nipkow@15392
  1973
proof induct
nipkow@15392
  1974
  case empty thus ?case by simp
nipkow@15392
  1975
next
nipkow@15392
  1976
  case (insert y S)
nipkow@15392
  1977
  show ?case
nipkow@15392
  1978
  proof cases
nipkow@15392
  1979
    assume "S = {}" thus ?thesis using insert by simp
nipkow@15392
  1980
  next
nipkow@15392
  1981
    assume "S \<noteq> {}" thus ?thesis using insert by (auto simp add:min_def)
nipkow@15392
  1982
  qed
nipkow@15392
  1983
qed
nipkow@15392
  1984
nipkow@15392
  1985
lemma Max_in [simp]:
nipkow@15392
  1986
  assumes a: "finite S"
nipkow@15392
  1987
  shows "S \<noteq> {} \<Longrightarrow> Max S \<in> S"
nipkow@15392
  1988
using a
nipkow@15392
  1989
proof induct
nipkow@15392
  1990
  case empty thus ?case by simp
nipkow@15392
  1991
next
nipkow@15392
  1992
  case (insert x S)
nipkow@15392
  1993
  show ?case
nipkow@15392
  1994
  proof cases
nipkow@15392
  1995
    assume "S = {}" thus ?thesis by simp
nipkow@15392
  1996
  next
nipkow@15392
  1997
    assume "S \<noteq> {}" thus ?thesis using insert by (simp add:max_def)
nipkow@15392
  1998
  qed
nipkow@15392
  1999
qed
nipkow@15392
  2000
nipkow@15392
  2001
lemma Max_le [simp]:
nipkow@15392
  2002
  assumes a: "finite S"
nipkow@15392
  2003
  shows "\<lbrakk> S \<noteq> {}; x \<in> S \<rbrakk> \<Longrightarrow> x \<le> Max S"
nipkow@15392
  2004
using a
nipkow@15392
  2005
proof induct
nipkow@15392
  2006
  case empty thus ?case by simp
nipkow@15392
  2007
next
nipkow@15392
  2008
  case (insert y S)
nipkow@15392
  2009
  show ?case
nipkow@15392
  2010
  proof cases
nipkow@15392
  2011
    assume "S = {}" thus ?thesis using insert by simp
nipkow@15392
  2012
  next
nipkow@15392
  2013
    assume "S \<noteq> {}" thus ?thesis using insert by (auto simp add:max_def)
nipkow@15392
  2014
  qed
nipkow@15392
  2015
qed
nipkow@15392
  2016
wenzelm@12396
  2017
nipkow@15042
  2018
end