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