src/HOL/Finite_Set.thy
author paulson
Wed Jun 09 11:18:51 2004 +0200 (2004-06-09)
changeset 14889 d7711d6b9014
parent 14748 001323d6d75b
child 14944 efbaecbdc05c
permissions -rw-r--r--
moved some cardinality results into main HOL
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
wenzelm@12396
     5
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@12396
     6
*)
wenzelm@12396
     7
wenzelm@12396
     8
header {* Finite sets *}
wenzelm@12396
     9
paulson@14485
    10
theory Finite_Set = Divides + Power + Inductive:
wenzelm@12396
    11
wenzelm@12396
    12
subsection {* Collection of finite sets *}
wenzelm@12396
    13
wenzelm@12396
    14
consts Finites :: "'a set set"
nipkow@13737
    15
syntax
nipkow@13737
    16
  finite :: "'a set => bool"
nipkow@13737
    17
translations
nipkow@13737
    18
  "finite A" == "A : Finites"
wenzelm@12396
    19
wenzelm@12396
    20
inductive Finites
wenzelm@12396
    21
  intros
wenzelm@12396
    22
    emptyI [simp, intro!]: "{} : Finites"
wenzelm@12396
    23
    insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
wenzelm@12396
    24
wenzelm@12396
    25
axclass finite \<subseteq> type
wenzelm@12396
    26
  finite: "finite UNIV"
wenzelm@12396
    27
nipkow@13737
    28
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    29
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    30
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    31
proof -
wenzelm@14661
    32
  from prems have "A \<noteq> UNIV" by blast
wenzelm@14661
    33
  thus ?thesis by blast
wenzelm@14661
    34
qed
wenzelm@12396
    35
wenzelm@12396
    36
lemma finite_induct [case_names empty insert, induct set: Finites]:
wenzelm@12396
    37
  "finite F ==>
wenzelm@12396
    38
    P {} ==> (!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    39
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    40
proof -
wenzelm@13421
    41
  assume "P {}" and
wenzelm@13421
    42
    insert: "!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    43
  assume "finite F"
wenzelm@12396
    44
  thus "P F"
wenzelm@12396
    45
  proof induct
wenzelm@12396
    46
    show "P {}" .
wenzelm@12396
    47
    fix F x assume F: "finite F" and P: "P F"
wenzelm@12396
    48
    show "P (insert x F)"
wenzelm@12396
    49
    proof cases
wenzelm@12396
    50
      assume "x \<in> F"
wenzelm@12396
    51
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    52
      with P show ?thesis by (simp only:)
wenzelm@12396
    53
    next
wenzelm@12396
    54
      assume "x \<notin> F"
wenzelm@12396
    55
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    56
    qed
wenzelm@12396
    57
  qed
wenzelm@12396
    58
qed
wenzelm@12396
    59
wenzelm@12396
    60
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@12396
    61
  "finite F ==> F \<subseteq> A ==>
wenzelm@12396
    62
    P {} ==> (!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
wenzelm@12396
    63
    P F"
wenzelm@12396
    64
proof -
wenzelm@13421
    65
  assume "P {}" and insert:
wenzelm@13421
    66
    "!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@12396
    67
  assume "finite F"
wenzelm@12396
    68
  thus "F \<subseteq> A ==> P F"
wenzelm@12396
    69
  proof induct
wenzelm@12396
    70
    show "P {}" .
wenzelm@12396
    71
    fix F x assume "finite F" and "x \<notin> F"
wenzelm@12396
    72
      and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    73
    show "P (insert x F)"
wenzelm@12396
    74
    proof (rule insert)
wenzelm@12396
    75
      from i show "x \<in> A" by blast
wenzelm@12396
    76
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    77
      with P show "P F" .
wenzelm@12396
    78
    qed
wenzelm@12396
    79
  qed
wenzelm@12396
    80
qed
wenzelm@12396
    81
wenzelm@12396
    82
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
wenzelm@12396
    83
  -- {* The union of two finite sets is finite. *}
wenzelm@12396
    84
  by (induct set: Finites) simp_all
wenzelm@12396
    85
wenzelm@12396
    86
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
    87
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
    88
proof -
wenzelm@12396
    89
  assume "finite B"
wenzelm@12396
    90
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
    91
  proof induct
wenzelm@12396
    92
    case empty
wenzelm@12396
    93
    thus ?case by simp
wenzelm@12396
    94
  next
wenzelm@12396
    95
    case (insert F x A)
wenzelm@12396
    96
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
wenzelm@12396
    97
    show "finite A"
wenzelm@12396
    98
    proof cases
wenzelm@12396
    99
      assume x: "x \<in> A"
wenzelm@12396
   100
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   101
      with r have "finite (A - {x})" .
wenzelm@12396
   102
      hence "finite (insert x (A - {x}))" ..
wenzelm@12396
   103
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
wenzelm@12396
   104
      finally show ?thesis .
wenzelm@12396
   105
    next
wenzelm@12396
   106
      show "A \<subseteq> F ==> ?thesis" .
wenzelm@12396
   107
      assume "x \<notin> A"
wenzelm@12396
   108
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   109
    qed
wenzelm@12396
   110
  qed
wenzelm@12396
   111
qed
wenzelm@12396
   112
wenzelm@12396
   113
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
wenzelm@12396
   114
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
wenzelm@12396
   115
wenzelm@12396
   116
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   117
  -- {* The converse obviously fails. *}
wenzelm@12396
   118
  by (blast intro: finite_subset)
wenzelm@12396
   119
wenzelm@12396
   120
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   121
  apply (subst insert_is_Un)
paulson@14208
   122
  apply (simp only: finite_Un, blast)
wenzelm@12396
   123
  done
wenzelm@12396
   124
wenzelm@12396
   125
lemma finite_empty_induct:
wenzelm@12396
   126
  "finite A ==>
wenzelm@12396
   127
  P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
wenzelm@12396
   128
proof -
wenzelm@12396
   129
  assume "finite A"
wenzelm@12396
   130
    and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@12396
   131
  have "P (A - A)"
wenzelm@12396
   132
  proof -
wenzelm@12396
   133
    fix c b :: "'a set"
wenzelm@12396
   134
    presume c: "finite c" and b: "finite b"
wenzelm@12396
   135
      and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@12396
   136
    from c show "c \<subseteq> b ==> P (b - c)"
wenzelm@12396
   137
    proof induct
wenzelm@12396
   138
      case empty
wenzelm@12396
   139
      from P1 show ?case by simp
wenzelm@12396
   140
    next
wenzelm@12396
   141
      case (insert F x)
wenzelm@12396
   142
      have "P (b - F - {x})"
wenzelm@12396
   143
      proof (rule P2)
wenzelm@12396
   144
        from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@12396
   145
        from insert show "x \<in> b - F" by simp
wenzelm@12396
   146
        from insert show "P (b - F)" by simp
wenzelm@12396
   147
      qed
wenzelm@12396
   148
      also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@12396
   149
      finally show ?case .
wenzelm@12396
   150
    qed
wenzelm@12396
   151
  next
wenzelm@12396
   152
    show "A \<subseteq> A" ..
wenzelm@12396
   153
  qed
wenzelm@12396
   154
  thus "P {}" by simp
wenzelm@12396
   155
qed
wenzelm@12396
   156
wenzelm@12396
   157
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
wenzelm@12396
   158
  by (rule Diff_subset [THEN finite_subset])
wenzelm@12396
   159
wenzelm@12396
   160
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   161
  apply (subst Diff_insert)
wenzelm@12396
   162
  apply (case_tac "a : A - B")
wenzelm@12396
   163
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   164
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   165
  done
wenzelm@12396
   166
wenzelm@12396
   167
paulson@13825
   168
subsubsection {* Image and Inverse Image over Finite Sets *}
paulson@13825
   169
paulson@13825
   170
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   171
  -- {* The image of a finite set is finite. *}
paulson@13825
   172
  by (induct set: Finites) simp_all
paulson@13825
   173
paulson@14430
   174
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   175
  apply (frule finite_imageI)
paulson@14430
   176
  apply (erule finite_subset, assumption)
paulson@14430
   177
  done
paulson@14430
   178
paulson@13825
   179
lemma finite_range_imageI:
paulson@13825
   180
    "finite (range g) ==> finite (range (%x. f (g x)))"
paulson@14208
   181
  apply (drule finite_imageI, simp)
paulson@13825
   182
  done
paulson@13825
   183
wenzelm@12396
   184
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   185
proof -
wenzelm@12396
   186
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   187
  fix B :: "'a set"
wenzelm@12396
   188
  assume "finite B"
wenzelm@12396
   189
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   190
    apply induct
wenzelm@12396
   191
     apply simp
wenzelm@12396
   192
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   193
     apply clarify
wenzelm@12396
   194
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   195
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   196
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   197
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   198
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   199
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   200
    done
wenzelm@12396
   201
qed (rule refl)
wenzelm@12396
   202
wenzelm@12396
   203
paulson@13825
   204
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   205
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   206
         is included in a singleton. *}
paulson@14430
   207
  apply (auto simp add: inj_on_def)
paulson@14430
   208
  apply (blast intro: the_equality [symmetric])
paulson@13825
   209
  done
paulson@13825
   210
paulson@13825
   211
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   212
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   213
         is finite. *}
paulson@14430
   214
  apply (induct set: Finites, simp_all)
paulson@14430
   215
  apply (subst vimage_insert)
paulson@14430
   216
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
paulson@13825
   217
  done
paulson@13825
   218
paulson@13825
   219
wenzelm@12396
   220
subsubsection {* The finite UNION of finite sets *}
wenzelm@12396
   221
wenzelm@12396
   222
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
wenzelm@12396
   223
  by (induct set: Finites) simp_all
wenzelm@12396
   224
wenzelm@12396
   225
text {*
wenzelm@12396
   226
  Strengthen RHS to
paulson@14430
   227
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   228
wenzelm@12396
   229
  We'd need to prove
paulson@14430
   230
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   231
  by induction. *}
wenzelm@12396
   232
wenzelm@12396
   233
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
wenzelm@12396
   234
  by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   235
wenzelm@12396
   236
wenzelm@12396
   237
subsubsection {* Sigma of finite sets *}
wenzelm@12396
   238
wenzelm@12396
   239
lemma finite_SigmaI [simp]:
wenzelm@12396
   240
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   241
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   242
wenzelm@12396
   243
lemma finite_Prod_UNIV:
wenzelm@12396
   244
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   245
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   246
   apply (erule ssubst)
paulson@14208
   247
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   248
  done
wenzelm@12396
   249
wenzelm@12396
   250
instance unit :: finite
wenzelm@12396
   251
proof
wenzelm@12396
   252
  have "finite {()}" by simp
wenzelm@12396
   253
  also have "{()} = UNIV" by auto
wenzelm@12396
   254
  finally show "finite (UNIV :: unit set)" .
wenzelm@12396
   255
qed
wenzelm@12396
   256
wenzelm@12396
   257
instance * :: (finite, finite) finite
wenzelm@12396
   258
proof
wenzelm@12396
   259
  show "finite (UNIV :: ('a \<times> 'b) set)"
wenzelm@12396
   260
  proof (rule finite_Prod_UNIV)
wenzelm@12396
   261
    show "finite (UNIV :: 'a set)" by (rule finite)
wenzelm@12396
   262
    show "finite (UNIV :: 'b set)" by (rule finite)
wenzelm@12396
   263
  qed
wenzelm@12396
   264
qed
wenzelm@12396
   265
wenzelm@12396
   266
wenzelm@12396
   267
subsubsection {* The powerset of a finite set *}
wenzelm@12396
   268
wenzelm@12396
   269
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   270
proof
wenzelm@12396
   271
  assume "finite (Pow A)"
wenzelm@12396
   272
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   273
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   274
next
wenzelm@12396
   275
  assume "finite A"
wenzelm@12396
   276
  thus "finite (Pow A)"
wenzelm@12396
   277
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   278
qed
wenzelm@12396
   279
wenzelm@12396
   280
lemma finite_converse [iff]: "finite (r^-1) = finite r"
wenzelm@12396
   281
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
wenzelm@12396
   282
   apply simp
wenzelm@12396
   283
   apply (rule iffI)
wenzelm@12396
   284
    apply (erule finite_imageD [unfolded inj_on_def])
wenzelm@12396
   285
    apply (simp split add: split_split)
wenzelm@12396
   286
   apply (erule finite_imageI)
paulson@14208
   287
  apply (simp add: converse_def image_def, auto)
wenzelm@12396
   288
  apply (rule bexI)
wenzelm@12396
   289
   prefer 2 apply assumption
wenzelm@12396
   290
  apply simp
wenzelm@12396
   291
  done
wenzelm@12396
   292
paulson@14430
   293
wenzelm@12396
   294
subsubsection {* Finiteness of transitive closure *}
wenzelm@12396
   295
wenzelm@12396
   296
text {* (Thanks to Sidi Ehmety) *}
wenzelm@12396
   297
wenzelm@12396
   298
lemma finite_Field: "finite r ==> finite (Field r)"
wenzelm@12396
   299
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
wenzelm@12396
   300
  apply (induct set: Finites)
wenzelm@12396
   301
   apply (auto simp add: Field_def Domain_insert Range_insert)
wenzelm@12396
   302
  done
wenzelm@12396
   303
wenzelm@12396
   304
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
wenzelm@12396
   305
  apply clarify
wenzelm@12396
   306
  apply (erule trancl_induct)
wenzelm@12396
   307
   apply (auto simp add: Field_def)
wenzelm@12396
   308
  done
wenzelm@12396
   309
wenzelm@12396
   310
lemma finite_trancl: "finite (r^+) = finite r"
wenzelm@12396
   311
  apply auto
wenzelm@12396
   312
   prefer 2
wenzelm@12396
   313
   apply (rule trancl_subset_Field2 [THEN finite_subset])
wenzelm@12396
   314
   apply (rule finite_SigmaI)
wenzelm@12396
   315
    prefer 3
berghofe@13704
   316
    apply (blast intro: r_into_trancl' finite_subset)
wenzelm@12396
   317
   apply (auto simp add: finite_Field)
wenzelm@12396
   318
  done
wenzelm@12396
   319
paulson@14430
   320
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
paulson@14430
   321
    finite (A <*> B)"
paulson@14430
   322
  by (rule finite_SigmaI)
paulson@14430
   323
wenzelm@12396
   324
wenzelm@12396
   325
subsection {* Finite cardinality *}
wenzelm@12396
   326
wenzelm@12396
   327
text {*
wenzelm@12396
   328
  This definition, although traditional, is ugly to work with: @{text
wenzelm@12396
   329
  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
wenzelm@12396
   330
  switched to an inductive one:
wenzelm@12396
   331
*}
wenzelm@12396
   332
wenzelm@12396
   333
consts cardR :: "('a set \<times> nat) set"
wenzelm@12396
   334
wenzelm@12396
   335
inductive cardR
wenzelm@12396
   336
  intros
wenzelm@12396
   337
    EmptyI: "({}, 0) : cardR"
wenzelm@12396
   338
    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
wenzelm@12396
   339
wenzelm@12396
   340
constdefs
wenzelm@12396
   341
  card :: "'a set => nat"
wenzelm@12396
   342
  "card A == THE n. (A, n) : cardR"
wenzelm@12396
   343
wenzelm@12396
   344
inductive_cases cardR_emptyE: "({}, n) : cardR"
wenzelm@12396
   345
inductive_cases cardR_insertE: "(insert a A,n) : cardR"
wenzelm@12396
   346
wenzelm@12396
   347
lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
wenzelm@12396
   348
  by (induct set: cardR) simp_all
wenzelm@12396
   349
wenzelm@12396
   350
lemma cardR_determ_aux1:
wenzelm@12396
   351
    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
paulson@14208
   352
  apply (induct set: cardR, auto)
paulson@14208
   353
  apply (simp add: insert_Diff_if, auto)
wenzelm@12396
   354
  apply (drule cardR_SucD)
wenzelm@12396
   355
  apply (blast intro!: cardR.intros)
wenzelm@12396
   356
  done
wenzelm@12396
   357
wenzelm@12396
   358
lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
wenzelm@12396
   359
  by (drule cardR_determ_aux1) auto
wenzelm@12396
   360
wenzelm@12396
   361
lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
wenzelm@12396
   362
  apply (induct set: cardR)
wenzelm@12396
   363
   apply (safe elim!: cardR_emptyE cardR_insertE)
wenzelm@12396
   364
  apply (rename_tac B b m)
wenzelm@12396
   365
  apply (case_tac "a = b")
wenzelm@12396
   366
   apply (subgoal_tac "A = B")
paulson@14208
   367
    prefer 2 apply (blast elim: equalityE, blast)
wenzelm@12396
   368
  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
wenzelm@12396
   369
   prefer 2
wenzelm@12396
   370
   apply (rule_tac x = "A Int B" in exI)
wenzelm@12396
   371
   apply (blast elim: equalityE)
wenzelm@12396
   372
  apply (frule_tac A = B in cardR_SucD)
wenzelm@12396
   373
  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
wenzelm@12396
   374
  done
wenzelm@12396
   375
wenzelm@12396
   376
lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
wenzelm@12396
   377
  by (induct set: cardR) simp_all
wenzelm@12396
   378
wenzelm@12396
   379
lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
wenzelm@12396
   380
  by (induct set: Finites) (auto intro!: cardR.intros)
wenzelm@12396
   381
wenzelm@12396
   382
lemma card_equality: "(A,n) : cardR ==> card A = n"
wenzelm@12396
   383
  by (unfold card_def) (blast intro: cardR_determ)
wenzelm@12396
   384
wenzelm@12396
   385
lemma card_empty [simp]: "card {} = 0"
wenzelm@12396
   386
  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
wenzelm@12396
   387
wenzelm@12396
   388
lemma card_insert_disjoint [simp]:
wenzelm@12396
   389
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
wenzelm@12396
   390
proof -
wenzelm@12396
   391
  assume x: "x \<notin> A"
wenzelm@12396
   392
  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
wenzelm@12396
   393
    apply (auto intro!: cardR.intros)
wenzelm@12396
   394
    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
wenzelm@12396
   395
     apply (force dest: cardR_imp_finite)
wenzelm@12396
   396
    apply (blast intro!: cardR.intros intro: cardR_determ)
wenzelm@12396
   397
    done
wenzelm@12396
   398
  assume "finite A"
wenzelm@12396
   399
  thus ?thesis
wenzelm@12396
   400
    apply (simp add: card_def aux)
wenzelm@12396
   401
    apply (rule the_equality)
wenzelm@12396
   402
     apply (auto intro: finite_imp_cardR
wenzelm@12396
   403
       cong: conj_cong simp: card_def [symmetric] card_equality)
wenzelm@12396
   404
    done
wenzelm@12396
   405
qed
wenzelm@12396
   406
wenzelm@12396
   407
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
   408
  apply auto
paulson@14208
   409
  apply (drule_tac a = x in mk_disjoint_insert, clarify)
paulson@14208
   410
  apply (rotate_tac -1, auto)
wenzelm@12396
   411
  done
wenzelm@12396
   412
wenzelm@12396
   413
lemma card_insert_if:
wenzelm@12396
   414
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
wenzelm@12396
   415
  by (simp add: insert_absorb)
wenzelm@12396
   416
wenzelm@12396
   417
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
   418
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
   419
apply(simp del:insert_Diff_single)
nipkow@14302
   420
done
wenzelm@12396
   421
wenzelm@12396
   422
lemma card_Diff_singleton:
wenzelm@12396
   423
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
wenzelm@12396
   424
  by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
   425
wenzelm@12396
   426
lemma card_Diff_singleton_if:
wenzelm@12396
   427
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
wenzelm@12396
   428
  by (simp add: card_Diff_singleton)
wenzelm@12396
   429
wenzelm@12396
   430
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
wenzelm@12396
   431
  by (simp add: card_insert_if card_Suc_Diff1)
wenzelm@12396
   432
wenzelm@12396
   433
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
wenzelm@12396
   434
  by (simp add: card_insert_if)
wenzelm@12396
   435
wenzelm@12396
   436
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
paulson@14208
   437
  apply (induct set: Finites, simp, clarify)
wenzelm@12396
   438
  apply (subgoal_tac "finite A & A - {x} <= F")
paulson@14208
   439
   prefer 2 apply (blast intro: finite_subset, atomize)
wenzelm@12396
   440
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
   441
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
paulson@14208
   442
  apply (case_tac "card A", auto)
wenzelm@12396
   443
  done
wenzelm@12396
   444
wenzelm@12396
   445
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
wenzelm@12396
   446
  apply (simp add: psubset_def linorder_not_le [symmetric])
wenzelm@12396
   447
  apply (blast dest: card_seteq)
wenzelm@12396
   448
  done
wenzelm@12396
   449
wenzelm@12396
   450
lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
paulson@14208
   451
  apply (case_tac "A = B", simp)
wenzelm@12396
   452
  apply (simp add: linorder_not_less [symmetric])
wenzelm@12396
   453
  apply (blast dest: card_seteq intro: order_less_imp_le)
wenzelm@12396
   454
  done
wenzelm@12396
   455
wenzelm@12396
   456
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
   457
    ==> card A + card B = card (A Un B) + card (A Int B)"
paulson@14208
   458
  apply (induct set: Finites, simp)
wenzelm@12396
   459
  apply (simp add: insert_absorb Int_insert_left)
wenzelm@12396
   460
  done
wenzelm@12396
   461
wenzelm@12396
   462
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
   463
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
wenzelm@12396
   464
  by (simp add: card_Un_Int)
wenzelm@12396
   465
wenzelm@12396
   466
lemma card_Diff_subset:
wenzelm@12396
   467
    "finite A ==> B <= A ==> card A - card B = card (A - B)"
wenzelm@12396
   468
  apply (subgoal_tac "(A - B) Un B = A")
wenzelm@12396
   469
   prefer 2 apply blast
paulson@14331
   470
  apply (rule nat_add_right_cancel [THEN iffD1])
wenzelm@12396
   471
  apply (rule card_Un_disjoint [THEN subst])
wenzelm@12396
   472
     apply (erule_tac [4] ssubst)
wenzelm@12396
   473
     prefer 3 apply blast
wenzelm@12396
   474
    apply (simp_all add: add_commute not_less_iff_le
wenzelm@12396
   475
      add_diff_inverse card_mono finite_subset)
wenzelm@12396
   476
  done
wenzelm@12396
   477
wenzelm@12396
   478
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
   479
  apply (rule Suc_less_SucD)
wenzelm@12396
   480
  apply (simp add: card_Suc_Diff1)
wenzelm@12396
   481
  done
wenzelm@12396
   482
wenzelm@12396
   483
lemma card_Diff2_less:
wenzelm@12396
   484
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
   485
  apply (case_tac "x = y")
wenzelm@12396
   486
   apply (simp add: card_Diff1_less)
wenzelm@12396
   487
  apply (rule less_trans)
wenzelm@12396
   488
   prefer 2 apply (auto intro!: card_Diff1_less)
wenzelm@12396
   489
  done
wenzelm@12396
   490
wenzelm@12396
   491
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
   492
  apply (case_tac "x : A")
wenzelm@12396
   493
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
   494
  done
wenzelm@12396
   495
wenzelm@12396
   496
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
   497
by (erule psubsetI, blast)
wenzelm@12396
   498
paulson@14889
   499
lemma insert_partition:
paulson@14889
   500
     "[| x \<notin> F; \<forall>c1\<in>insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 --> c1 \<inter> c2 = {}|] 
paulson@14889
   501
      ==> x \<inter> \<Union> F = {}"
paulson@14889
   502
by auto
paulson@14889
   503
paulson@14889
   504
(* main cardinality theorem *)
paulson@14889
   505
lemma card_partition [rule_format]:
paulson@14889
   506
     "finite C ==>  
paulson@14889
   507
        finite (\<Union> C) -->  
paulson@14889
   508
        (\<forall>c\<in>C. card c = k) -->   
paulson@14889
   509
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
paulson@14889
   510
        k * card(C) = card (\<Union> C)"
paulson@14889
   511
apply (erule finite_induct, simp)
paulson@14889
   512
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
   513
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
   514
done
paulson@14889
   515
wenzelm@12396
   516
wenzelm@12396
   517
subsubsection {* Cardinality of image *}
wenzelm@12396
   518
wenzelm@12396
   519
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
paulson@14208
   520
  apply (induct set: Finites, simp)
wenzelm@12396
   521
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
   522
  done
wenzelm@12396
   523
wenzelm@12396
   524
lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
paulson@14430
   525
  apply (induct set: Finites, simp_all, atomize, safe)
paulson@14208
   526
   apply (unfold inj_on_def, blast)
wenzelm@12396
   527
  apply (subst card_insert_disjoint)
paulson@14208
   528
    apply (erule finite_imageI, blast, blast)
wenzelm@12396
   529
  done
wenzelm@12396
   530
wenzelm@12396
   531
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
wenzelm@12396
   532
  by (simp add: card_seteq card_image)
wenzelm@12396
   533
wenzelm@12396
   534
wenzelm@12396
   535
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
   536
wenzelm@12396
   537
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
wenzelm@12396
   538
  apply (induct set: Finites)
wenzelm@12396
   539
   apply (simp_all add: Pow_insert)
paulson@14208
   540
  apply (subst card_Un_disjoint, blast)
paulson@14208
   541
    apply (blast intro: finite_imageI, blast)
wenzelm@12396
   542
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
   543
   apply (simp add: card_image Pow_insert)
wenzelm@12396
   544
  apply (unfold inj_on_def)
wenzelm@12396
   545
  apply (blast elim!: equalityE)
wenzelm@12396
   546
  done
wenzelm@12396
   547
wenzelm@12396
   548
text {*
wenzelm@12396
   549
  \medskip Relates to equivalence classes.  Based on a theorem of
wenzelm@12396
   550
  F. Kammüller's.  The @{prop "finite C"} premise is redundant.
wenzelm@12396
   551
*}
wenzelm@12396
   552
wenzelm@12396
   553
lemma dvd_partition:
wenzelm@12396
   554
  "finite C ==> finite (Union C) ==>
wenzelm@12396
   555
    ALL c : C. k dvd card c ==>
paulson@14430
   556
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
   557
  k dvd card (Union C)"
paulson@14208
   558
  apply (induct set: Finites, simp_all, clarify)
wenzelm@12396
   559
  apply (subst card_Un_disjoint)
wenzelm@12396
   560
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
   561
  done
wenzelm@12396
   562
wenzelm@12396
   563
wenzelm@12396
   564
subsection {* A fold functional for finite sets *}
wenzelm@12396
   565
wenzelm@12396
   566
text {*
wenzelm@12396
   567
  For @{text n} non-negative we have @{text "fold f e {x1, ..., xn} =
wenzelm@12396
   568
  f x1 (... (f xn e))"} where @{text f} is at least left-commutative.
wenzelm@12396
   569
*}
wenzelm@12396
   570
wenzelm@12396
   571
consts
wenzelm@12396
   572
  foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
wenzelm@12396
   573
wenzelm@12396
   574
inductive "foldSet f e"
wenzelm@12396
   575
  intros
wenzelm@12396
   576
    emptyI [intro]: "({}, e) : foldSet f e"
wenzelm@12396
   577
    insertI [intro]: "x \<notin> A ==> (A, y) : foldSet f e ==> (insert x A, f x y) : foldSet f e"
wenzelm@12396
   578
wenzelm@12396
   579
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
wenzelm@12396
   580
wenzelm@12396
   581
constdefs
wenzelm@12396
   582
  fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
wenzelm@12396
   583
  "fold f e A == THE x. (A, x) : foldSet f e"
wenzelm@12396
   584
wenzelm@12396
   585
lemma Diff1_foldSet: "(A - {x}, y) : foldSet f e ==> x: A ==> (A, f x y) : foldSet f e"
paulson@14208
   586
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
wenzelm@12396
   587
wenzelm@12396
   588
lemma foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
wenzelm@12396
   589
  by (induct set: foldSet) auto
wenzelm@12396
   590
wenzelm@12396
   591
lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f e"
wenzelm@12396
   592
  by (induct set: Finites) auto
wenzelm@12396
   593
wenzelm@12396
   594
wenzelm@12396
   595
subsubsection {* Left-commutative operations *}
wenzelm@12396
   596
wenzelm@12396
   597
locale LC =
wenzelm@12396
   598
  fixes f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   599
  assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   600
wenzelm@12396
   601
lemma (in LC) foldSet_determ_aux:
wenzelm@12396
   602
  "ALL A x. card A < n --> (A, x) : foldSet f e -->
wenzelm@12396
   603
    (ALL y. (A, y) : foldSet f e --> y = x)"
wenzelm@12396
   604
  apply (induct n)
wenzelm@12396
   605
   apply (auto simp add: less_Suc_eq)
paulson@14208
   606
  apply (erule foldSet.cases, blast)
paulson@14208
   607
  apply (erule foldSet.cases, blast, clarify)
wenzelm@12396
   608
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
wenzelm@12396
   609
  apply (erule rev_mp)
wenzelm@12396
   610
  apply (simp add: less_Suc_eq_le)
wenzelm@12396
   611
  apply (rule impI)
wenzelm@12396
   612
  apply (rename_tac Aa xa ya Ab xb yb, case_tac "xa = xb")
wenzelm@12396
   613
   apply (subgoal_tac "Aa = Ab")
paulson@14208
   614
    prefer 2 apply (blast elim!: equalityE, blast)
wenzelm@12396
   615
  txt {* case @{prop "xa \<notin> xb"}. *}
wenzelm@12396
   616
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
paulson@14208
   617
   prefer 2 apply (blast elim!: equalityE, clarify)
wenzelm@12396
   618
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
wenzelm@12396
   619
   prefer 2 apply blast
wenzelm@12396
   620
  apply (subgoal_tac "card Aa <= card Ab")
wenzelm@12396
   621
   prefer 2
wenzelm@12396
   622
   apply (rule Suc_le_mono [THEN subst])
wenzelm@12396
   623
   apply (simp add: card_Suc_Diff1)
wenzelm@12396
   624
  apply (rule_tac A1 = "Aa - {xb}" and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   625
  apply (blast intro: foldSet_imp_finite finite_Diff)
wenzelm@12396
   626
  apply (frule (1) Diff1_foldSet)
wenzelm@12396
   627
  apply (subgoal_tac "ya = f xb x")
wenzelm@12396
   628
   prefer 2 apply (blast del: equalityCE)
wenzelm@12396
   629
  apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
wenzelm@12396
   630
   prefer 2 apply simp
wenzelm@12396
   631
  apply (subgoal_tac "yb = f xa x")
wenzelm@12396
   632
   prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
wenzelm@12396
   633
  apply (simp (no_asm_simp) add: left_commute)
wenzelm@12396
   634
  done
wenzelm@12396
   635
wenzelm@12396
   636
lemma (in LC) foldSet_determ: "(A, x) : foldSet f e ==> (A, y) : foldSet f e ==> y = x"
wenzelm@12396
   637
  by (blast intro: foldSet_determ_aux [rule_format])
wenzelm@12396
   638
wenzelm@12396
   639
lemma (in LC) fold_equality: "(A, y) : foldSet f e ==> fold f e A = y"
wenzelm@12396
   640
  by (unfold fold_def) (blast intro: foldSet_determ)
wenzelm@12396
   641
wenzelm@12396
   642
lemma fold_empty [simp]: "fold f e {} = e"
wenzelm@12396
   643
  by (unfold fold_def) blast
wenzelm@12396
   644
wenzelm@12396
   645
lemma (in LC) fold_insert_aux: "x \<notin> A ==>
wenzelm@12396
   646
    ((insert x A, v) : foldSet f e) =
wenzelm@12396
   647
    (EX y. (A, y) : foldSet f e & v = f x y)"
wenzelm@12396
   648
  apply auto
wenzelm@12396
   649
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   650
   apply (fastsimp dest: foldSet_imp_finite)
wenzelm@12396
   651
  apply (blast intro: foldSet_determ)
wenzelm@12396
   652
  done
wenzelm@12396
   653
wenzelm@12396
   654
lemma (in LC) fold_insert:
wenzelm@12396
   655
    "finite A ==> x \<notin> A ==> fold f e (insert x A) = f x (fold f e A)"
wenzelm@12396
   656
  apply (unfold fold_def)
wenzelm@12396
   657
  apply (simp add: fold_insert_aux)
wenzelm@12396
   658
  apply (rule the_equality)
wenzelm@12396
   659
  apply (auto intro: finite_imp_foldSet
wenzelm@12396
   660
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
wenzelm@12396
   661
  done
wenzelm@12396
   662
wenzelm@12396
   663
lemma (in LC) fold_commute: "finite A ==> (!!e. f x (fold f e A) = fold f (f x e) A)"
paulson@14208
   664
  apply (induct set: Finites, simp)
wenzelm@12396
   665
  apply (simp add: left_commute fold_insert)
wenzelm@12396
   666
  done
wenzelm@12396
   667
wenzelm@12396
   668
lemma (in LC) fold_nest_Un_Int:
wenzelm@12396
   669
  "finite A ==> finite B
wenzelm@12396
   670
    ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)"
paulson@14208
   671
  apply (induct set: Finites, simp)
wenzelm@12396
   672
  apply (simp add: fold_insert fold_commute Int_insert_left insert_absorb)
wenzelm@12396
   673
  done
wenzelm@12396
   674
wenzelm@12396
   675
lemma (in LC) fold_nest_Un_disjoint:
wenzelm@12396
   676
  "finite A ==> finite B ==> A Int B = {}
wenzelm@12396
   677
    ==> fold f e (A Un B) = fold f (fold f e B) A"
wenzelm@12396
   678
  by (simp add: fold_nest_Un_Int)
wenzelm@12396
   679
wenzelm@12396
   680
declare foldSet_imp_finite [simp del]
wenzelm@12396
   681
    empty_foldSetE [rule del]  foldSet.intros [rule del]
wenzelm@12396
   682
  -- {* Delete rules to do with @{text foldSet} relation. *}
wenzelm@12396
   683
wenzelm@12396
   684
wenzelm@12396
   685
wenzelm@12396
   686
subsubsection {* Commutative monoids *}
wenzelm@12396
   687
wenzelm@12396
   688
text {*
wenzelm@12396
   689
  We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
wenzelm@12396
   690
  instead of @{text "'b => 'a => 'a"}.
wenzelm@12396
   691
*}
wenzelm@12396
   692
wenzelm@12396
   693
locale ACe =
wenzelm@12396
   694
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   695
    and e :: 'a
wenzelm@12396
   696
  assumes ident [simp]: "x \<cdot> e = x"
wenzelm@12396
   697
    and commute: "x \<cdot> y = y \<cdot> x"
wenzelm@12396
   698
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
wenzelm@12396
   699
wenzelm@12396
   700
lemma (in ACe) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   701
proof -
wenzelm@12396
   702
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
wenzelm@12396
   703
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
wenzelm@12396
   704
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
wenzelm@12396
   705
  finally show ?thesis .
wenzelm@12396
   706
qed
wenzelm@12396
   707
wenzelm@12718
   708
lemmas (in ACe) AC = assoc commute left_commute
wenzelm@12396
   709
wenzelm@12693
   710
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
wenzelm@12396
   711
proof -
wenzelm@12396
   712
  have "x \<cdot> e = x" by (rule ident)
wenzelm@12396
   713
  thus ?thesis by (subst commute)
wenzelm@12396
   714
qed
wenzelm@12396
   715
wenzelm@12396
   716
lemma (in ACe) fold_Un_Int:
wenzelm@12396
   717
  "finite A ==> finite B ==>
wenzelm@12396
   718
    fold f e A \<cdot> fold f e B = fold f e (A Un B) \<cdot> fold f e (A Int B)"
paulson@14208
   719
  apply (induct set: Finites, simp)
wenzelm@13400
   720
  apply (simp add: AC insert_absorb Int_insert_left
wenzelm@13421
   721
    LC.fold_insert [OF LC.intro])
wenzelm@12396
   722
  done
wenzelm@12396
   723
wenzelm@12396
   724
lemma (in ACe) fold_Un_disjoint:
wenzelm@12396
   725
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   726
    fold f e (A Un B) = fold f e A \<cdot> fold f e B"
wenzelm@12396
   727
  by (simp add: fold_Un_Int)
wenzelm@12396
   728
wenzelm@12396
   729
lemma (in ACe) fold_Un_disjoint2:
wenzelm@12396
   730
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   731
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   732
proof -
wenzelm@12396
   733
  assume b: "finite B"
wenzelm@12396
   734
  assume "finite A"
wenzelm@12396
   735
  thus "A Int B = {} ==>
wenzelm@12396
   736
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   737
  proof induct
wenzelm@12396
   738
    case empty
wenzelm@12396
   739
    thus ?case by simp
wenzelm@12396
   740
  next
wenzelm@12396
   741
    case (insert F x)
paulson@13571
   742
    have "fold (f o g) e (insert x F \<union> B) = fold (f o g) e (insert x (F \<union> B))"
wenzelm@12396
   743
      by simp
paulson@13571
   744
    also have "... = (f o g) x (fold (f o g) e (F \<union> B))"
wenzelm@13400
   745
      by (rule LC.fold_insert [OF LC.intro])
wenzelm@13421
   746
        (insert b insert, auto simp add: left_commute)
paulson@13571
   747
    also from insert have "fold (f o g) e (F \<union> B) =
paulson@13571
   748
      fold (f o g) e F \<cdot> fold (f o g) e B" by blast
paulson@13571
   749
    also have "(f o g) x ... = (f o g) x (fold (f o g) e F) \<cdot> fold (f o g) e B"
wenzelm@12396
   750
      by (simp add: AC)
paulson@13571
   751
    also have "(f o g) x (fold (f o g) e F) = fold (f o g) e (insert x F)"
wenzelm@13400
   752
      by (rule LC.fold_insert [OF LC.intro, symmetric]) (insert b insert,
wenzelm@14661
   753
        auto simp add: left_commute)
wenzelm@12396
   754
    finally show ?case .
wenzelm@12396
   755
  qed
wenzelm@12396
   756
qed
wenzelm@12396
   757
wenzelm@12396
   758
wenzelm@12396
   759
subsection {* Generalized summation over a set *}
wenzelm@12396
   760
wenzelm@12396
   761
constdefs
obua@14738
   762
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
wenzelm@12396
   763
  "setsum f A == if finite A then fold (op + o f) 0 A else 0"
wenzelm@12396
   764
wenzelm@12396
   765
syntax
obua@14738
   766
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_:_. _)" [0, 51, 10] 10)
wenzelm@12396
   767
syntax (xsymbols)
obua@14738
   768
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
kleing@14565
   769
syntax (HTML output)
obua@14738
   770
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
wenzelm@12396
   771
translations
wenzelm@12396
   772
  "\<Sum>i:A. b" == "setsum (%i. b) A"  -- {* Beware of argument permutation! *}
wenzelm@12396
   773
wenzelm@12396
   774
wenzelm@12396
   775
lemma setsum_empty [simp]: "setsum f {} = 0"
wenzelm@12396
   776
  by (simp add: setsum_def)
wenzelm@12396
   777
wenzelm@12396
   778
lemma setsum_insert [simp]:
wenzelm@12396
   779
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
wenzelm@13365
   780
  by (simp add: setsum_def
obua@14738
   781
    LC.fold_insert [OF LC.intro] add_left_commute)
wenzelm@12396
   782
paulson@14485
   783
lemma setsum_reindex [rule_format]: "finite B ==>
paulson@14485
   784
                  inj_on f B --> setsum h (f ` B) = setsum (h \<circ> f) B"
paulson@14485
   785
apply (rule finite_induct, assumption, force)
paulson@14485
   786
apply (rule impI, auto)
paulson@14485
   787
apply (simp add: inj_on_def)
paulson@14485
   788
apply (subgoal_tac "f x \<notin> f ` F")
paulson@14485
   789
apply (subgoal_tac "finite (f ` F)")
paulson@14485
   790
apply (auto simp add: setsum_insert)
paulson@14485
   791
apply (simp add: inj_on_def)
wenzelm@12396
   792
  done
wenzelm@12396
   793
paulson@14485
   794
lemma setsum_reindex_id: "finite B ==> inj_on f B ==>
paulson@14485
   795
    setsum f B = setsum id (f ` B)"
paulson@14485
   796
by (auto simp add: setsum_reindex id_o)
wenzelm@12396
   797
wenzelm@14661
   798
lemma setsum_reindex_cong: "finite A ==> inj_on f A ==>
paulson@14485
   799
    B = f ` A ==> g = h \<circ> f ==> setsum h B = setsum g A"
paulson@14485
   800
  by (frule setsum_reindex, assumption, simp)
wenzelm@12396
   801
wenzelm@12396
   802
lemma setsum_cong:
wenzelm@12396
   803
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
wenzelm@12396
   804
  apply (case_tac "finite B")
paulson@14208
   805
   prefer 2 apply (simp add: setsum_def, simp)
wenzelm@12396
   806
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C")
wenzelm@12396
   807
   apply simp
paulson@14208
   808
  apply (erule finite_induct, simp)
paulson@14208
   809
  apply (simp add: subset_insert_iff, clarify)
wenzelm@12396
   810
  apply (subgoal_tac "finite C")
wenzelm@12396
   811
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
wenzelm@12396
   812
  apply (subgoal_tac "C = insert x (C - {x})")
wenzelm@12396
   813
   prefer 2 apply blast
wenzelm@12396
   814
  apply (erule ssubst)
wenzelm@12396
   815
  apply (drule spec)
wenzelm@12396
   816
  apply (erule (1) notE impE)
nipkow@14302
   817
  apply (simp add: Ball_def del:insert_Diff_single)
wenzelm@12396
   818
  done
wenzelm@12396
   819
paulson@14485
   820
lemma setsum_0: "setsum (%i. 0) A = 0"
paulson@14485
   821
  apply (case_tac "finite A")
paulson@14485
   822
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   823
  apply (erule finite_induct, auto)
paulson@14430
   824
  done
paulson@14430
   825
paulson@14430
   826
lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
paulson@14430
   827
  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
paulson@14430
   828
  apply (erule ssubst, rule setsum_0)
paulson@14430
   829
  apply (rule setsum_cong, auto)
paulson@14430
   830
  done
paulson@14430
   831
paulson@14485
   832
lemma card_eq_setsum: "finite A ==> card A = setsum (%x. 1) A"
paulson@14485
   833
  -- {* Could allow many @{text "card"} proofs to be simplified. *}
paulson@14485
   834
  by (induct set: Finites) auto
paulson@14430
   835
paulson@14485
   836
lemma setsum_Un_Int: "finite A ==> finite B
paulson@14485
   837
    ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
paulson@14485
   838
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
paulson@14485
   839
  apply (induct set: Finites, simp)
obua@14738
   840
  apply (simp add: add_ac Int_insert_left insert_absorb)
paulson@14485
   841
  done
paulson@14485
   842
paulson@14485
   843
lemma setsum_Un_disjoint: "finite A ==> finite B
paulson@14485
   844
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
paulson@14485
   845
  apply (subst setsum_Un_Int [symmetric], auto)
paulson@14485
   846
  done
paulson@14430
   847
paulson@14485
   848
lemma setsum_UN_disjoint:
paulson@14485
   849
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
   850
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
   851
      setsum f (UNION I A) = setsum (%i. setsum f (A i)) I"
paulson@14485
   852
  apply (induct set: Finites, simp, atomize)
paulson@14485
   853
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
paulson@14485
   854
   prefer 2 apply blast
paulson@14485
   855
  apply (subgoal_tac "A x Int UNION F A = {}")
paulson@14485
   856
   prefer 2 apply blast
paulson@14485
   857
  apply (simp add: setsum_Un_disjoint)
paulson@14485
   858
  done
paulson@14485
   859
paulson@14485
   860
lemma setsum_Union_disjoint:
paulson@14485
   861
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
   862
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
   863
      setsum f (Union C) = setsum (setsum f) C"
paulson@14485
   864
  apply (frule setsum_UN_disjoint [of C id f])
paulson@14485
   865
  apply (unfold Union_def id_def, assumption+)
paulson@14430
   866
  done
paulson@14430
   867
wenzelm@14661
   868
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
wenzelm@14661
   869
    (\<Sum>x:A. (\<Sum>y:B x. f x y)) =
wenzelm@14661
   870
    (\<Sum>z:(SIGMA x:A. B x). f (fst z) (snd z))"
paulson@14485
   871
  apply (subst Sigma_def)
paulson@14485
   872
  apply (subst setsum_UN_disjoint)
paulson@14485
   873
  apply assumption
paulson@14485
   874
  apply (rule ballI)
paulson@14485
   875
  apply (drule_tac x = i in bspec, assumption)
wenzelm@14661
   876
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
paulson@14485
   877
  apply (rule finite_surj)
paulson@14485
   878
  apply auto
paulson@14485
   879
  apply (rule setsum_cong, rule refl)
paulson@14485
   880
  apply (subst setsum_UN_disjoint)
paulson@14485
   881
  apply (erule bspec, assumption)
paulson@14485
   882
  apply auto
paulson@14485
   883
  done
paulson@14430
   884
paulson@14485
   885
lemma setsum_cartesian_product: "finite A ==> finite B ==>
wenzelm@14661
   886
    (\<Sum>x:A. (\<Sum>y:B. f x y)) =
wenzelm@14661
   887
    (\<Sum>z:A <*> B. f (fst z) (snd z))"
paulson@14485
   888
  by (erule setsum_Sigma, auto);
paulson@14485
   889
paulson@14485
   890
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
paulson@14485
   891
  apply (case_tac "finite A")
paulson@14485
   892
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   893
  apply (erule finite_induct, auto)
obua@14738
   894
  apply (simp add: add_ac)
paulson@14485
   895
  done
paulson@14430
   896
paulson@14430
   897
subsubsection {* Properties in more restricted classes of structures *}
paulson@14430
   898
paulson@14485
   899
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
paulson@14485
   900
  apply (case_tac "finite A")
paulson@14485
   901
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   902
  apply (erule rev_mp)
paulson@14485
   903
  apply (erule finite_induct, auto)
paulson@14485
   904
  done
paulson@14485
   905
paulson@14430
   906
lemma setsum_eq_0_iff [simp]:
paulson@14430
   907
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
paulson@14430
   908
  by (induct set: Finites) auto
paulson@14430
   909
paulson@14485
   910
lemma setsum_constant_nat [simp]:
paulson@14430
   911
    "finite A ==> (\<Sum>x: A. y) = (card A) * y"
nipkow@14740
   912
  -- {* Later generalized to any @{text comm_semiring_1_cancel}. *}
paulson@14430
   913
  by (erule finite_induct, auto)
paulson@14430
   914
paulson@14430
   915
lemma setsum_Un: "finite A ==> finite B ==>
paulson@14430
   916
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
paulson@14430
   917
  -- {* For the natural numbers, we have subtraction. *}
obua@14738
   918
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
paulson@14430
   919
paulson@14430
   920
lemma setsum_Un_ring: "finite A ==> finite B ==>
obua@14738
   921
    (setsum f (A Un B) :: 'a :: comm_ring_1) =
paulson@14430
   922
      setsum f A + setsum f B - setsum f (A Int B)"
obua@14738
   923
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
paulson@14430
   924
paulson@14430
   925
lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
paulson@14430
   926
    (if a:A then setsum f A - f a else setsum f A)"
paulson@14430
   927
  apply (case_tac "finite A")
paulson@14430
   928
   prefer 2 apply (simp add: setsum_def)
paulson@14430
   929
  apply (erule finite_induct)
paulson@14430
   930
   apply (auto simp add: insert_Diff_if)
paulson@14430
   931
  apply (drule_tac a = a in mk_disjoint_insert, auto)
paulson@14430
   932
  done
paulson@14430
   933
obua@14738
   934
lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::comm_ring_1) A =
paulson@14430
   935
  - setsum f A"
paulson@14430
   936
  by (induct set: Finites, auto)
paulson@14430
   937
obua@14738
   938
lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::comm_ring_1) - g x) A =
paulson@14430
   939
  setsum f A - setsum g A"
paulson@14430
   940
  by (simp add: diff_minus setsum_addf setsum_negf)
paulson@14430
   941
paulson@14430
   942
lemma setsum_nonneg: "[| finite A;
obua@14738
   943
    \<forall>x \<in> A. (0::'a::ordered_semidom) \<le> f x |] ==>
paulson@14430
   944
    0 \<le>  setsum f A";
paulson@14430
   945
  apply (induct set: Finites, auto)
paulson@14430
   946
  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
paulson@14430
   947
  apply (blast intro: add_mono)
paulson@14430
   948
  done
paulson@14430
   949
paulson@14485
   950
subsubsection {* Cardinality of unions and Sigma sets *}
paulson@14485
   951
paulson@14485
   952
lemma card_UN_disjoint:
paulson@14485
   953
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
   954
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
   955
      card (UNION I A) = setsum (%i. card (A i)) I"
paulson@14485
   956
  apply (subst card_eq_setsum)
paulson@14485
   957
  apply (subst finite_UN, assumption+)
paulson@14485
   958
  apply (subgoal_tac "setsum (%i. card (A i)) I =
paulson@14485
   959
      setsum (%i. (setsum (%x. 1) (A i))) I")
paulson@14485
   960
  apply (erule ssubst)
paulson@14485
   961
  apply (erule setsum_UN_disjoint, assumption+)
paulson@14485
   962
  apply (rule setsum_cong)
paulson@14485
   963
  apply simp+
paulson@14485
   964
  done
paulson@14485
   965
paulson@14485
   966
lemma card_Union_disjoint:
paulson@14485
   967
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
   968
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
   969
      card (Union C) = setsum card C"
paulson@14485
   970
  apply (frule card_UN_disjoint [of C id])
paulson@14485
   971
  apply (unfold Union_def id_def, assumption+)
paulson@14485
   972
  done
paulson@14430
   973
paulson@14430
   974
lemma SigmaI_insert: "y \<notin> A ==>
paulson@14430
   975
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
paulson@14430
   976
  by auto
paulson@14430
   977
paulson@14485
   978
lemma card_cartesian_product_singleton: "finite A ==>
paulson@14430
   979
    card({x} <*> A) = card(A)"
paulson@14430
   980
  apply (subgoal_tac "inj_on (%y .(x,y)) A")
paulson@14430
   981
  apply (frule card_image, assumption)
paulson@14430
   982
  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
paulson@14430
   983
  apply (auto simp add: inj_on_def)
paulson@14430
   984
  done
paulson@14430
   985
paulson@14430
   986
lemma card_SigmaI [rule_format,simp]: "finite A ==>
paulson@14430
   987
  (ALL a:A. finite (B a)) -->
paulson@14430
   988
  card (SIGMA x: A. B x) = (\<Sum>a: A. card (B a))"
paulson@14430
   989
  apply (erule finite_induct, auto)
paulson@14430
   990
  apply (subst SigmaI_insert, assumption)
paulson@14430
   991
  apply (subst card_Un_disjoint)
paulson@14485
   992
  apply (auto intro: finite_SigmaI simp add: card_cartesian_product_singleton)
paulson@14430
   993
  done
paulson@14430
   994
paulson@14485
   995
lemma card_cartesian_product: "[| finite A; finite B |] ==>
paulson@14430
   996
  card (A <*> B) = card(A) * card(B)"
paulson@14485
   997
  by simp
paulson@14430
   998
paulson@14430
   999
paulson@14430
  1000
subsection {* Generalized product over a set *}
paulson@14430
  1001
paulson@14430
  1002
constdefs
obua@14738
  1003
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
paulson@14430
  1004
  "setprod f A == if finite A then fold (op * o f) 1 A else 1"
paulson@14430
  1005
paulson@14430
  1006
syntax
obua@14738
  1007
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
paulson@14430
  1008
paulson@14430
  1009
syntax (xsymbols)
obua@14738
  1010
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
kleing@14565
  1011
syntax (HTML output)
obua@14738
  1012
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
paulson@14430
  1013
translations
paulson@14430
  1014
  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
paulson@14430
  1015
paulson@14430
  1016
lemma setprod_empty [simp]: "setprod f {} = 1"
paulson@14430
  1017
  by (auto simp add: setprod_def)
paulson@14430
  1018
paulson@14430
  1019
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
paulson@14430
  1020
    setprod f (insert a A) = f a * setprod f A"
paulson@14430
  1021
  by (auto simp add: setprod_def LC_def LC.fold_insert
obua@14738
  1022
      mult_left_commute)
paulson@14430
  1023
paulson@14748
  1024
lemma setprod_reindex [rule_format]:
paulson@14748
  1025
     "finite B ==> inj_on f B --> setprod h (f ` B) = setprod (h \<circ> f) B"
paulson@14485
  1026
apply (rule finite_induct, assumption, force)
paulson@14485
  1027
apply (rule impI, auto)
paulson@14485
  1028
apply (simp add: inj_on_def)
paulson@14485
  1029
apply (subgoal_tac "f x \<notin> f ` F")
paulson@14485
  1030
apply (subgoal_tac "finite (f ` F)")
paulson@14485
  1031
apply (auto simp add: setprod_insert)
paulson@14485
  1032
apply (simp add: inj_on_def)
paulson@14748
  1033
done
paulson@14430
  1034
paulson@14485
  1035
lemma setprod_reindex_id: "finite B ==> inj_on f B ==>
paulson@14485
  1036
    setprod f B = setprod id (f ` B)"
paulson@14485
  1037
by (auto simp add: setprod_reindex id_o)
paulson@14430
  1038
wenzelm@14661
  1039
lemma setprod_reindex_cong: "finite A ==> inj_on f A ==>
paulson@14485
  1040
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
paulson@14485
  1041
  by (frule setprod_reindex, assumption, simp)
paulson@14430
  1042
paulson@14430
  1043
lemma setprod_cong:
paulson@14430
  1044
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
paulson@14430
  1045
  apply (case_tac "finite B")
paulson@14430
  1046
   prefer 2 apply (simp add: setprod_def, simp)
paulson@14430
  1047
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setprod f C = setprod g C")
paulson@14430
  1048
   apply simp
paulson@14430
  1049
  apply (erule finite_induct, simp)
paulson@14430
  1050
  apply (simp add: subset_insert_iff, clarify)
paulson@14430
  1051
  apply (subgoal_tac "finite C")
paulson@14430
  1052
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
paulson@14430
  1053
  apply (subgoal_tac "C = insert x (C - {x})")
paulson@14430
  1054
   prefer 2 apply blast
paulson@14430
  1055
  apply (erule ssubst)
paulson@14430
  1056
  apply (drule spec)
paulson@14430
  1057
  apply (erule (1) notE impE)
paulson@14430
  1058
  apply (simp add: Ball_def del:insert_Diff_single)
paulson@14430
  1059
  done
paulson@14430
  1060
paulson@14485
  1061
lemma setprod_1: "setprod (%i. 1) A = 1"
paulson@14485
  1062
  apply (case_tac "finite A")
obua@14738
  1063
  apply (erule finite_induct, auto simp add: mult_ac)
paulson@14485
  1064
  apply (simp add: setprod_def)
paulson@14485
  1065
  done
paulson@14485
  1066
paulson@14430
  1067
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
paulson@14430
  1068
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
paulson@14430
  1069
  apply (erule ssubst, rule setprod_1)
paulson@14430
  1070
  apply (rule setprod_cong, auto)
paulson@14430
  1071
  done
paulson@14430
  1072
paulson@14485
  1073
lemma setprod_Un_Int: "finite A ==> finite B
paulson@14485
  1074
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
paulson@14485
  1075
  apply (induct set: Finites, simp)
obua@14738
  1076
  apply (simp add: mult_ac insert_absorb)
obua@14738
  1077
  apply (simp add: mult_ac Int_insert_left insert_absorb)
paulson@14485
  1078
  done
paulson@14430
  1079
paulson@14485
  1080
lemma setprod_Un_disjoint: "finite A ==> finite B
paulson@14485
  1081
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
obua@14738
  1082
  apply (subst setprod_Un_Int [symmetric], auto simp add: mult_ac)
paulson@14485
  1083
  done
paulson@14485
  1084
paulson@14485
  1085
lemma setprod_UN_disjoint:
paulson@14485
  1086
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
  1087
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
  1088
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
paulson@14485
  1089
  apply (induct set: Finites, simp, atomize)
paulson@14485
  1090
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
paulson@14485
  1091
   prefer 2 apply blast
paulson@14485
  1092
  apply (subgoal_tac "A x Int UNION F A = {}")
paulson@14485
  1093
   prefer 2 apply blast
paulson@14485
  1094
  apply (simp add: setprod_Un_disjoint)
paulson@14430
  1095
  done
paulson@14430
  1096
paulson@14485
  1097
lemma setprod_Union_disjoint:
paulson@14485
  1098
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
  1099
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
  1100
      setprod f (Union C) = setprod (setprod f) C"
paulson@14485
  1101
  apply (frule setprod_UN_disjoint [of C id f])
paulson@14485
  1102
  apply (unfold Union_def id_def, assumption+)
paulson@14485
  1103
  done
paulson@14430
  1104
wenzelm@14661
  1105
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
wenzelm@14661
  1106
    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
wenzelm@14661
  1107
    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
paulson@14485
  1108
  apply (subst Sigma_def)
paulson@14485
  1109
  apply (subst setprod_UN_disjoint)
paulson@14485
  1110
  apply assumption
paulson@14485
  1111
  apply (rule ballI)
paulson@14485
  1112
  apply (drule_tac x = i in bspec, assumption)
wenzelm@14661
  1113
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
paulson@14485
  1114
  apply (rule finite_surj)
paulson@14485
  1115
  apply auto
paulson@14485
  1116
  apply (rule setprod_cong, rule refl)
paulson@14485
  1117
  apply (subst setprod_UN_disjoint)
paulson@14485
  1118
  apply (erule bspec, assumption)
paulson@14485
  1119
  apply auto
paulson@14485
  1120
  done
paulson@14485
  1121
wenzelm@14661
  1122
lemma setprod_cartesian_product: "finite A ==> finite B ==>
wenzelm@14661
  1123
    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
wenzelm@14661
  1124
    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
paulson@14485
  1125
  by (erule setprod_Sigma, auto)
paulson@14485
  1126
paulson@14485
  1127
lemma setprod_timesf: "setprod (%x. f x * g x) A =
paulson@14485
  1128
    (setprod f A * setprod g A)"
paulson@14485
  1129
  apply (case_tac "finite A")
obua@14738
  1130
   prefer 2 apply (simp add: setprod_def mult_ac)
paulson@14485
  1131
  apply (erule finite_induct, auto)
obua@14738
  1132
  apply (simp add: mult_ac)
paulson@14485
  1133
  done
paulson@14430
  1134
paulson@14430
  1135
subsubsection {* Properties in more restricted classes of structures *}
paulson@14430
  1136
paulson@14430
  1137
lemma setprod_eq_1_iff [simp]:
paulson@14430
  1138
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
paulson@14430
  1139
  by (induct set: Finites) auto
paulson@14430
  1140
paulson@14430
  1141
lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::ringpower)) =
paulson@14430
  1142
    y^(card A)"
paulson@14430
  1143
  apply (erule finite_induct)
paulson@14430
  1144
  apply (auto simp add: power_Suc)
paulson@14430
  1145
  done
paulson@14430
  1146
obua@14738
  1147
lemma setprod_zero: "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==>
paulson@14430
  1148
    setprod f A = 0"
paulson@14430
  1149
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1150
  apply (erule disjE, auto)
paulson@14430
  1151
  done
paulson@14430
  1152
obua@14738
  1153
lemma setprod_nonneg [rule_format]: "(ALL x: A. (0::'a::ordered_idom) \<le> f x)
paulson@14430
  1154
     --> 0 \<le> setprod f A"
paulson@14430
  1155
  apply (case_tac "finite A")
paulson@14430
  1156
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1157
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
paulson@14430
  1158
  apply (rule mult_mono, assumption+)
paulson@14430
  1159
  apply (auto simp add: setprod_def)
paulson@14430
  1160
  done
paulson@14430
  1161
obua@14738
  1162
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
paulson@14430
  1163
     --> 0 < setprod f A"
paulson@14430
  1164
  apply (case_tac "finite A")
paulson@14430
  1165
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1166
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
paulson@14430
  1167
  apply (rule mult_strict_mono, assumption+)
paulson@14430
  1168
  apply (auto simp add: setprod_def)
paulson@14430
  1169
  done
paulson@14430
  1170
paulson@14430
  1171
lemma setprod_nonzero [rule_format]:
obua@14738
  1172
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
paulson@14430
  1173
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
paulson@14430
  1174
  apply (erule finite_induct, auto)
paulson@14430
  1175
  done
paulson@14430
  1176
paulson@14430
  1177
lemma setprod_zero_eq:
obua@14738
  1178
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
paulson@14430
  1179
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
paulson@14430
  1180
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
paulson@14430
  1181
  done
paulson@14430
  1182
paulson@14430
  1183
lemma setprod_nonzero_field:
paulson@14430
  1184
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
paulson@14430
  1185
  apply (rule setprod_nonzero, auto)
paulson@14430
  1186
  done
paulson@14430
  1187
paulson@14430
  1188
lemma setprod_zero_eq_field:
paulson@14430
  1189
    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
paulson@14430
  1190
  apply (rule setprod_zero_eq, auto)
paulson@14430
  1191
  done
paulson@14430
  1192
paulson@14430
  1193
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
paulson@14430
  1194
    (setprod f (A Un B) :: 'a ::{field})
paulson@14430
  1195
      = setprod f A * setprod f B / setprod f (A Int B)"
paulson@14430
  1196
  apply (subst setprod_Un_Int [symmetric], auto)
paulson@14430
  1197
  apply (subgoal_tac "finite (A Int B)")
paulson@14430
  1198
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
paulson@14430
  1199
  apply (subst times_divide_eq_right [THEN sym], auto)
paulson@14430
  1200
  done
paulson@14430
  1201
paulson@14430
  1202
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
paulson@14430
  1203
    (setprod f (A - {a}) :: 'a :: {field}) =
paulson@14430
  1204
      (if a:A then setprod f A / f a else setprod f A)"
paulson@14430
  1205
  apply (erule finite_induct)
paulson@14430
  1206
   apply (auto simp add: insert_Diff_if)
paulson@14430
  1207
  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
paulson@14430
  1208
  apply (erule ssubst)
paulson@14430
  1209
  apply (subst times_divide_eq_right [THEN sym])
paulson@14430
  1210
  apply (auto simp add: mult_ac)
paulson@14430
  1211
  done
paulson@14430
  1212
paulson@14430
  1213
lemma setprod_inversef: "finite A ==>
paulson@14430
  1214
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
paulson@14430
  1215
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
paulson@14430
  1216
  apply (erule finite_induct)
paulson@14430
  1217
  apply (simp, simp)
paulson@14430
  1218
  done
paulson@14430
  1219
paulson@14430
  1220
lemma setprod_dividef:
paulson@14430
  1221
     "[|finite A;
paulson@14430
  1222
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
paulson@14430
  1223
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
paulson@14430
  1224
  apply (subgoal_tac
paulson@14430
  1225
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
paulson@14430
  1226
  apply (erule ssubst)
paulson@14430
  1227
  apply (subst divide_inverse)
paulson@14430
  1228
  apply (subst setprod_timesf)
paulson@14430
  1229
  apply (subst setprod_inversef, assumption+, rule refl)
paulson@14430
  1230
  apply (rule setprod_cong, rule refl)
paulson@14430
  1231
  apply (subst divide_inverse, auto)
paulson@14430
  1232
  done
paulson@14430
  1233
paulson@14430
  1234
paulson@14430
  1235
subsection{* Min and Max of finite linearly ordered sets *}
nipkow@13490
  1236
nipkow@13490
  1237
text{* Seemed easier to define directly than via fold. *}
nipkow@13490
  1238
nipkow@13490
  1239
lemma ex_Max: fixes S :: "('a::linorder)set"
paulson@14430
  1240
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. s \<le> m"
nipkow@13490
  1241
using fin
nipkow@13490
  1242
proof (induct)
nipkow@13490
  1243
  case empty thus ?case by simp
nipkow@13490
  1244
next
nipkow@13490
  1245
  case (insert S x)
nipkow@13490
  1246
  show ?case
nipkow@13490
  1247
  proof (cases)
nipkow@13490
  1248
    assume "S = {}" thus ?thesis by simp
nipkow@13490
  1249
  next
nipkow@13490
  1250
    assume nonempty: "S \<noteq> {}"
nipkow@13490
  1251
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. s \<le> m" using insert by blast
nipkow@13490
  1252
    show ?thesis
nipkow@13490
  1253
    proof (cases)
nipkow@13490
  1254
      assume "x \<le> m" thus ?thesis using m by blast
nipkow@13490
  1255
    next
paulson@14430
  1256
      assume "~ x \<le> m" thus ?thesis using m
wenzelm@14661
  1257
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
  1258
    qed
nipkow@13490
  1259
  qed
nipkow@13490
  1260
qed
nipkow@13490
  1261
nipkow@13490
  1262
lemma ex_Min: fixes S :: "('a::linorder)set"
paulson@14430
  1263
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. m \<le> s"
nipkow@13490
  1264
using fin
nipkow@13490
  1265
proof (induct)
nipkow@13490
  1266
  case empty thus ?case by simp
nipkow@13490
  1267
next
nipkow@13490
  1268
  case (insert S x)
nipkow@13490
  1269
  show ?case
nipkow@13490
  1270
  proof (cases)
nipkow@13490
  1271
    assume "S = {}" thus ?thesis by simp
nipkow@13490
  1272
  next
nipkow@13490
  1273
    assume nonempty: "S \<noteq> {}"
nipkow@13490
  1274
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. m \<le> s" using insert by blast
nipkow@13490
  1275
    show ?thesis
nipkow@13490
  1276
    proof (cases)
nipkow@13490
  1277
      assume "m \<le> x" thus ?thesis using m by blast
nipkow@13490
  1278
    next
paulson@14430
  1279
      assume "~ m \<le> x" thus ?thesis using m
wenzelm@14661
  1280
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
  1281
    qed
nipkow@13490
  1282
  qed
nipkow@13490
  1283
qed
nipkow@13490
  1284
nipkow@13490
  1285
constdefs
wenzelm@14661
  1286
  Min :: "('a::linorder)set => 'a"
wenzelm@14661
  1287
  "Min S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. m \<le> s)"
nipkow@13490
  1288
wenzelm@14661
  1289
  Max :: "('a::linorder)set => 'a"
wenzelm@14661
  1290
  "Max S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. s \<le> m)"
nipkow@13490
  1291
wenzelm@14661
  1292
lemma Min [simp]:
wenzelm@14661
  1293
  assumes a: "finite S"  "S \<noteq> {}"
wenzelm@14661
  1294
  shows "Min S \<in> S \<and> (\<forall>s \<in> S. Min S \<le> s)" (is "?P(Min S)")
nipkow@13490
  1295
proof (unfold Min_def, rule theI')
nipkow@13490
  1296
  show "\<exists>!m. ?P m"
nipkow@13490
  1297
  proof (rule ex_ex1I)
nipkow@13490
  1298
    show "\<exists>m. ?P m" using ex_Min[OF a] by blast
nipkow@13490
  1299
  next
wenzelm@14661
  1300
    fix m1 m2 assume "?P m1" and "?P m2"
wenzelm@14661
  1301
    thus "m1 = m2" by (blast dest: order_antisym)
nipkow@13490
  1302
  qed
nipkow@13490
  1303
qed
nipkow@13490
  1304
wenzelm@14661
  1305
lemma Max [simp]:
wenzelm@14661
  1306
  assumes a: "finite S"  "S \<noteq> {}"
wenzelm@14661
  1307
  shows "Max S \<in> S \<and> (\<forall>s \<in> S. s \<le> Max S)" (is "?P(Max S)")
nipkow@13490
  1308
proof (unfold Max_def, rule theI')
nipkow@13490
  1309
  show "\<exists>!m. ?P m"
nipkow@13490
  1310
  proof (rule ex_ex1I)
nipkow@13490
  1311
    show "\<exists>m. ?P m" using ex_Max[OF a] by blast
nipkow@13490
  1312
  next
nipkow@13490
  1313
    fix m1 m2 assume "?P m1" "?P m2"
wenzelm@14661
  1314
    thus "m1 = m2" by (blast dest: order_antisym)
nipkow@13490
  1315
  qed
nipkow@13490
  1316
qed
nipkow@13490
  1317
wenzelm@14661
  1318
paulson@14430
  1319
subsection {* Theorems about @{text "choose"} *}
wenzelm@12396
  1320
wenzelm@12396
  1321
text {*
wenzelm@12396
  1322
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@14661
  1323
  Kamm\"uller, tidied by LCP.
wenzelm@12396
  1324
*}
wenzelm@12396
  1325
wenzelm@12396
  1326
lemma card_s_0_eq_empty:
wenzelm@12396
  1327
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@12396
  1328
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@12396
  1329
  apply (simp cong add: rev_conj_cong)
wenzelm@12396
  1330
  done
wenzelm@12396
  1331
wenzelm@12396
  1332
lemma choose_deconstruct: "finite M ==> x \<notin> M
wenzelm@12396
  1333
  ==> {s. s <= insert x M & card(s) = Suc k}
wenzelm@12396
  1334
       = {s. s <= M & card(s) = Suc k} Un
wenzelm@12396
  1335
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
wenzelm@12396
  1336
  apply safe
wenzelm@12396
  1337
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@12396
  1338
  apply (drule_tac x = "xa - {x}" in spec)
paulson@14430
  1339
  apply (subgoal_tac "x \<notin> xa", auto)
wenzelm@12396
  1340
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@12396
  1341
  apply (auto intro: finite_subset)
wenzelm@12396
  1342
  done
wenzelm@12396
  1343
wenzelm@12396
  1344
lemma card_inj_on_le:
paulson@14748
  1345
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
paulson@14748
  1346
apply (subgoal_tac "finite A") 
paulson@14748
  1347
 apply (force intro: card_mono simp add: card_image [symmetric])
paulson@14748
  1348
apply (blast intro: Finite_Set.finite_imageD dest: finite_subset) 
paulson@14748
  1349
done
wenzelm@12396
  1350
paulson@14430
  1351
lemma card_bij_eq:
paulson@14430
  1352
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
paulson@13595
  1353
       finite A; finite B |] ==> card A = card B"
wenzelm@12396
  1354
  by (auto intro: le_anti_sym card_inj_on_le)
wenzelm@12396
  1355
paulson@13595
  1356
text{*There are as many subsets of @{term A} having cardinality @{term k}
paulson@13595
  1357
 as there are sets obtained from the former by inserting a fixed element
paulson@13595
  1358
 @{term x} into each.*}
paulson@13595
  1359
lemma constr_bij:
paulson@13595
  1360
   "[|finite A; x \<notin> A|] ==>
paulson@13595
  1361
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
wenzelm@12396
  1362
    card {B. B <= A & card(B) = k}"
wenzelm@12396
  1363
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
paulson@13595
  1364
       apply (auto elim!: equalityE simp add: inj_on_def)
paulson@13595
  1365
    apply (subst Diff_insert0, auto)
paulson@13595
  1366
   txt {* finiteness of the two sets *}
paulson@13595
  1367
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
paulson@13595
  1368
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
paulson@13595
  1369
   apply fast+
wenzelm@12396
  1370
  done
wenzelm@12396
  1371
wenzelm@12396
  1372
text {*
wenzelm@12396
  1373
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@12396
  1374
*}
wenzelm@12396
  1375
wenzelm@12396
  1376
lemma n_sub_lemma:
wenzelm@12396
  1377
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1378
  apply (induct k)
paulson@14208
  1379
   apply (simp add: card_s_0_eq_empty, atomize)
wenzelm@12396
  1380
  apply (rotate_tac -1, erule finite_induct)
wenzelm@13421
  1381
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@13421
  1382
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@12396
  1383
  apply (subst card_Un_disjoint)
wenzelm@12396
  1384
     prefer 4 apply (force simp add: constr_bij)
wenzelm@12396
  1385
    prefer 3 apply force
wenzelm@12396
  1386
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@12396
  1387
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@12396
  1388
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@12396
  1389
  done
wenzelm@12396
  1390
wenzelm@13421
  1391
theorem n_subsets:
wenzelm@13421
  1392
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1393
  by (simp add: n_sub_lemma)
wenzelm@12396
  1394
wenzelm@12396
  1395
end