src/HOL/Finite.ML
author nipkow
Tue Oct 28 14:03:25 1997 +0100 (1997-10-28)
changeset 4014 df6cd80b6387
parent 3919 c036caebfc75
child 4059 59c1422c9da5
permissions -rw-r--r--
Added finite_UNION/SigmaI.
clasohm@1465
     1
(*  Title:      HOL/Finite.thy
clasohm@923
     2
    ID:         $Id$
nipkow@1531
     3
    Author:     Lawrence C Paulson & Tobias Nipkow
nipkow@1531
     4
    Copyright   1995  University of Cambridge & TU Muenchen
clasohm@923
     5
nipkow@1531
     6
Finite sets and their cardinality
clasohm@923
     7
*)
clasohm@923
     8
clasohm@923
     9
open Finite;
clasohm@923
    10
nipkow@3413
    11
section "finite";
nipkow@1531
    12
nipkow@3413
    13
(*
clasohm@923
    14
goalw Finite.thy Fin.defs "!!A B. A<=B ==> Fin(A) <= Fin(B)";
clasohm@1465
    15
by (rtac lfp_mono 1);
clasohm@923
    16
by (REPEAT (ares_tac basic_monos 1));
clasohm@923
    17
qed "Fin_mono";
clasohm@923
    18
clasohm@923
    19
goalw Finite.thy Fin.defs "Fin(A) <= Pow(A)";
paulson@2922
    20
by (blast_tac (!claset addSIs [lfp_lowerbound]) 1);
clasohm@923
    21
qed "Fin_subset_Pow";
clasohm@923
    22
clasohm@923
    23
(* A : Fin(B) ==> A <= B *)
clasohm@923
    24
val FinD = Fin_subset_Pow RS subsetD RS PowD;
nipkow@3413
    25
*)
clasohm@923
    26
clasohm@923
    27
(*Discharging ~ x:y entails extra work*)
clasohm@923
    28
val major::prems = goal Finite.thy 
nipkow@3413
    29
    "[| finite F;  P({}); \
nipkow@3413
    30
\       !!F x. [| finite F;  x ~: F;  P(F) |] ==> P(insert x F) \
clasohm@923
    31
\    |] ==> P(F)";
nipkow@3413
    32
by (rtac (major RS Finites.induct) 1);
nipkow@3413
    33
by (excluded_middle_tac "a:A" 2);
clasohm@923
    34
by (etac (insert_absorb RS ssubst) 3 THEN assume_tac 3);   (*backtracking!*)
clasohm@923
    35
by (REPEAT (ares_tac prems 1));
nipkow@3413
    36
qed "finite_induct";
nipkow@3413
    37
nipkow@3413
    38
val major::prems = goal Finite.thy 
nipkow@3413
    39
    "[| finite F; \
nipkow@3413
    40
\       P({}); \
nipkow@3413
    41
\       !!F a. [| finite F; a:A; a ~: F;  P(F) |] ==> P(insert a F) \
nipkow@3413
    42
\    |] ==> F <= A --> P(F)";
nipkow@3413
    43
by (rtac (major RS finite_induct) 1);
paulson@3457
    44
by (ALLGOALS (blast_tac (!claset addIs prems)));
nipkow@3413
    45
val lemma = result();
clasohm@923
    46
nipkow@3413
    47
val prems = goal Finite.thy 
nipkow@3413
    48
    "[| finite F;  F <= A; \
nipkow@3413
    49
\       P({}); \
nipkow@3413
    50
\       !!F a. [| finite F; a:A; a ~: F;  P(F) |] ==> P(insert a F) \
nipkow@3413
    51
\    |] ==> P(F)";
paulson@3457
    52
by (blast_tac (HOL_cs addIs ((lemma RS mp)::prems)) 1);
nipkow@3413
    53
qed "finite_subset_induct";
nipkow@3413
    54
nipkow@3413
    55
Addsimps Finites.intrs;
nipkow@3413
    56
AddSIs Finites.intrs;
clasohm@923
    57
clasohm@923
    58
(*The union of two finite sets is finite*)
clasohm@923
    59
val major::prems = goal Finite.thy
nipkow@3413
    60
    "[| finite F;  finite G |] ==> finite(F Un G)";
nipkow@3413
    61
by (rtac (major RS finite_induct) 1);
paulson@3517
    62
by (ALLGOALS (asm_simp_tac (!simpset addsimps prems)));
nipkow@3413
    63
qed "finite_UnI";
clasohm@923
    64
clasohm@923
    65
(*Every subset of a finite set is finite*)
nipkow@3413
    66
val [subs,fin] = goal Finite.thy "[| A<=B;  finite B |] ==> finite A";
nipkow@3413
    67
by (EVERY1 [subgoal_tac "ALL C. C<=B --> finite C",
clasohm@1465
    68
            rtac mp, etac spec,
clasohm@1465
    69
            rtac subs]);
nipkow@3413
    70
by (rtac (fin RS finite_induct) 1);
clasohm@1264
    71
by (simp_tac (!simpset addsimps [subset_Un_eq]) 1);
berghofe@1786
    72
by (safe_tac (!claset addSDs [subset_insert_iff RS iffD1]));
clasohm@923
    73
by (eres_inst_tac [("t","C")] (insert_Diff RS subst) 2);
clasohm@1264
    74
by (ALLGOALS Asm_simp_tac);
nipkow@3413
    75
qed "finite_subset";
clasohm@923
    76
nipkow@3413
    77
goal Finite.thy "finite(F Un G) = (finite F & finite G)";
nipkow@3413
    78
by (blast_tac (!claset addIs [finite_UnI] addDs
nipkow@3413
    79
                [Un_upper1 RS finite_subset, Un_upper2 RS finite_subset]) 1);
nipkow@3413
    80
qed "finite_Un";
nipkow@3413
    81
AddIffs[finite_Un];
nipkow@1531
    82
nipkow@3413
    83
goal Finite.thy "finite(insert a A) = finite A";
paulson@1553
    84
by (stac insert_is_Un 1);
nipkow@3413
    85
by (simp_tac (HOL_ss addsimps [finite_Un]) 1);
paulson@3427
    86
by (Blast_tac 1);
nipkow@3413
    87
qed "finite_insert";
nipkow@3413
    88
Addsimps[finite_insert];
nipkow@1531
    89
nipkow@3413
    90
(*The image of a finite set is finite *)
nipkow@3413
    91
goal Finite.thy  "!!F. finite F ==> finite(h``F)";
nipkow@3413
    92
by (etac finite_induct 1);
clasohm@1264
    93
by (Simp_tac 1);
nipkow@3413
    94
by (Asm_simp_tac 1);
nipkow@3413
    95
qed "finite_imageI";
clasohm@923
    96
clasohm@923
    97
val major::prems = goal Finite.thy 
nipkow@3413
    98
    "[| finite c;  finite b;                                  \
clasohm@1465
    99
\       P(b);                                                   \
nipkow@3413
   100
\       !!x y. [| finite y;  x:y;  P(y) |] ==> P(y-{x}) \
clasohm@923
   101
\    |] ==> c<=b --> P(b-c)";
nipkow@3413
   102
by (rtac (major RS finite_induct) 1);
paulson@2031
   103
by (stac Diff_insert 2);
clasohm@923
   104
by (ALLGOALS (asm_simp_tac
nipkow@3413
   105
                (!simpset addsimps (prems@[Diff_subset RS finite_subset]))));
nipkow@1531
   106
val lemma = result();
clasohm@923
   107
clasohm@923
   108
val prems = goal Finite.thy 
nipkow@3413
   109
    "[| finite A;                                       \
nipkow@3413
   110
\       P(A);                                           \
nipkow@3413
   111
\       !!a A. [| finite A;  a:A;  P(A) |] ==> P(A-{a}) \
clasohm@923
   112
\    |] ==> P({})";
clasohm@923
   113
by (rtac (Diff_cancel RS subst) 1);
nipkow@1531
   114
by (rtac (lemma RS mp) 1);
clasohm@923
   115
by (REPEAT (ares_tac (subset_refl::prems) 1));
nipkow@3413
   116
qed "finite_empty_induct";
nipkow@1531
   117
nipkow@1531
   118
paulson@1618
   119
(* finite B ==> finite (B - Ba) *)
paulson@1618
   120
bind_thm ("finite_Diff", Diff_subset RS finite_subset);
nipkow@1531
   121
Addsimps [finite_Diff];
nipkow@1531
   122
paulson@3368
   123
goal Finite.thy "finite(A-{a}) = finite(A)";
paulson@3368
   124
by (case_tac "a:A" 1);
paulson@3457
   125
by (rtac (finite_insert RS sym RS trans) 1);
paulson@3368
   126
by (stac insert_Diff 1);
paulson@3368
   127
by (ALLGOALS Asm_simp_tac);
paulson@3368
   128
qed "finite_Diff_singleton";
paulson@3368
   129
AddIffs [finite_Diff_singleton];
paulson@3368
   130
nipkow@3413
   131
goal Finite.thy "!!A. finite B ==> !A. f``A = B --> inj_onto f A --> finite A";
paulson@1553
   132
by (etac finite_induct 1);
nipkow@3413
   133
 by (ALLGOALS Asm_simp_tac);
paulson@3708
   134
by (Clarify_tac 1);
nipkow@3413
   135
by (subgoal_tac "EX y:A. f y = x & F = f``(A-{y})" 1);
paulson@3708
   136
 by (Clarify_tac 1);
paulson@3457
   137
 by (rewtac inj_onto_def);
nipkow@3413
   138
 by (Blast_tac 1);
paulson@3368
   139
by (thin_tac "ALL A. ?PP(A)" 1);
nipkow@3413
   140
by (forward_tac [[equalityD2, insertI1] MRS subsetD] 1);
paulson@3708
   141
by (Clarify_tac 1);
paulson@3368
   142
by (res_inst_tac [("x","xa")] bexI 1);
paulson@3368
   143
by (ALLGOALS Asm_simp_tac);
paulson@3708
   144
by (blast_tac (!claset addEs [equalityE]) 1);
paulson@3368
   145
val lemma = result();
paulson@3368
   146
paulson@3368
   147
goal Finite.thy "!!A. [| finite(f``A);  inj_onto f A |] ==> finite A";
paulson@3457
   148
by (dtac lemma 1);
paulson@3368
   149
by (Blast_tac 1);
paulson@3368
   150
qed "finite_imageD";
paulson@3368
   151
nipkow@4014
   152
(** The finite UNION of finite sets **)
nipkow@4014
   153
nipkow@4014
   154
val [prem] = goal Finite.thy
nipkow@4014
   155
 "finite A ==> (!a:A. finite(B a)) --> finite(UN a:A. B a)";
nipkow@4014
   156
br (prem RS finite_induct) 1;
nipkow@4014
   157
by(ALLGOALS Asm_simp_tac);
nipkow@4014
   158
bind_thm("finite_UnionI", ballI RSN (2, result() RS mp));
nipkow@4014
   159
Addsimps [finite_UnionI];
nipkow@4014
   160
nipkow@4014
   161
(** Sigma of finite sets **)
nipkow@4014
   162
nipkow@4014
   163
goalw Finite.thy [Sigma_def]
nipkow@4014
   164
 "!!A. [| finite A; !a:A. finite(B a) |] ==> finite(SIGMA a:A. B a)";
nipkow@4014
   165
by(blast_tac (!claset addSIs [finite_UnionI]) 1);
nipkow@4014
   166
bind_thm("finite_SigmaI", ballI RSN (2,result()));
nipkow@4014
   167
Addsimps [finite_SigmaI];
paulson@3368
   168
paulson@3368
   169
(** The powerset of a finite set **)
paulson@3368
   170
paulson@3368
   171
goal Finite.thy "!!A. finite(Pow A) ==> finite A";
paulson@3368
   172
by (subgoal_tac "finite ((%x.{x})``A)" 1);
paulson@3457
   173
by (rtac finite_subset 2);
paulson@3457
   174
by (assume_tac 3);
paulson@3368
   175
by (ALLGOALS
paulson@3368
   176
    (fast_tac (!claset addSDs [rewrite_rule [inj_onto_def] finite_imageD])));
paulson@3368
   177
val lemma = result();
paulson@3368
   178
paulson@3368
   179
goal Finite.thy "finite(Pow A) = finite A";
paulson@3457
   180
by (rtac iffI 1);
paulson@3457
   181
by (etac lemma 1);
paulson@3368
   182
(*Opposite inclusion: finite A ==> finite (Pow A) *)
paulson@3340
   183
by (etac finite_induct 1);
paulson@3340
   184
by (ALLGOALS 
paulson@3340
   185
    (asm_simp_tac
paulson@3340
   186
     (!simpset addsimps [finite_UnI, finite_imageI, Pow_insert])));
paulson@3368
   187
qed "finite_Pow_iff";
paulson@3368
   188
AddIffs [finite_Pow_iff];
paulson@3340
   189
nipkow@3439
   190
goal Finite.thy "finite(r^-1) = finite r";
paulson@3457
   191
by (subgoal_tac "r^-1 = (%(x,y).(y,x))``r" 1);
paulson@3457
   192
 by (Asm_simp_tac 1);
paulson@3457
   193
 by (rtac iffI 1);
paulson@3457
   194
  by (etac (rewrite_rule [inj_onto_def] finite_imageD) 1);
nipkow@3919
   195
  by (simp_tac (!simpset addsplits [expand_split]) 1);
paulson@3457
   196
 by (etac finite_imageI 1);
paulson@3457
   197
by (simp_tac (!simpset addsimps [inverse_def,image_def]) 1);
paulson@3457
   198
by (Auto_tac());
paulson@3457
   199
 by (rtac bexI 1);
paulson@3457
   200
 by (assume_tac 2);
paulson@3457
   201
 by (Simp_tac 1);
paulson@3457
   202
by (split_all_tac 1);
paulson@3457
   203
by (Asm_full_simp_tac 1);
nipkow@3439
   204
qed "finite_inverse";
nipkow@3439
   205
AddIffs [finite_inverse];
nipkow@1531
   206
nipkow@1548
   207
section "Finite cardinality -- 'card'";
nipkow@1531
   208
nipkow@1531
   209
goal Set.thy "{f i |i. P i | i=n} = insert (f n) {f i|i. P i}";
paulson@2922
   210
by (Blast_tac 1);
nipkow@1531
   211
val Collect_conv_insert = result();
nipkow@1531
   212
nipkow@1531
   213
goalw Finite.thy [card_def] "card {} = 0";
paulson@1553
   214
by (rtac Least_equality 1);
paulson@1553
   215
by (ALLGOALS Asm_full_simp_tac);
nipkow@1531
   216
qed "card_empty";
nipkow@1531
   217
Addsimps [card_empty];
nipkow@1531
   218
nipkow@1531
   219
val [major] = goal Finite.thy
nipkow@1531
   220
  "finite A ==> ? (n::nat) f. A = {f i |i. i<n}";
paulson@1553
   221
by (rtac (major RS finite_induct) 1);
paulson@1553
   222
 by (res_inst_tac [("x","0")] exI 1);
paulson@1553
   223
 by (Simp_tac 1);
paulson@1553
   224
by (etac exE 1);
paulson@1553
   225
by (etac exE 1);
paulson@1553
   226
by (hyp_subst_tac 1);
paulson@1553
   227
by (res_inst_tac [("x","Suc n")] exI 1);
paulson@1553
   228
by (res_inst_tac [("x","%i. if i<n then f i else x")] exI 1);
oheimb@1660
   229
by (asm_simp_tac (!simpset addsimps [Collect_conv_insert, less_Suc_eq]
nipkow@1548
   230
                          addcongs [rev_conj_cong]) 1);
nipkow@1531
   231
qed "finite_has_card";
nipkow@1531
   232
nipkow@1531
   233
goal Finite.thy
wenzelm@3842
   234
  "!!A.[| x ~: A; insert x A = {f i|i. i<n} |] ==> \
wenzelm@3842
   235
\  ? m::nat. m<n & (? g. A = {g i|i. i<m})";
paulson@1553
   236
by (res_inst_tac [("n","n")] natE 1);
paulson@1553
   237
 by (hyp_subst_tac 1);
paulson@1553
   238
 by (Asm_full_simp_tac 1);
paulson@1553
   239
by (rename_tac "m" 1);
paulson@1553
   240
by (hyp_subst_tac 1);
paulson@1553
   241
by (case_tac "? a. a:A" 1);
paulson@1553
   242
 by (res_inst_tac [("x","0")] exI 2);
paulson@1553
   243
 by (Simp_tac 2);
paulson@2922
   244
 by (Blast_tac 2);
paulson@1553
   245
by (etac exE 1);
oheimb@1660
   246
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@1553
   247
by (rtac exI 1);
paulson@1782
   248
by (rtac (refl RS disjI2 RS conjI) 1);
paulson@1553
   249
by (etac equalityE 1);
paulson@1553
   250
by (asm_full_simp_tac
oheimb@1660
   251
     (!simpset addsimps [subset_insert,Collect_conv_insert, less_Suc_eq]) 1);
paulson@2922
   252
by (safe_tac (!claset));
paulson@1553
   253
  by (Asm_full_simp_tac 1);
paulson@1553
   254
  by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
berghofe@1786
   255
  by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   256
   by (subgoal_tac "x ~= f m" 1);
paulson@2922
   257
    by (Blast_tac 2);
paulson@1553
   258
   by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   259
    by (Blast_tac 2);
berghofe@1786
   260
   by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   261
   by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   262
   by (Asm_simp_tac 1);
nipkow@3919
   263
  by (simp_tac (!simpset addsplits [expand_if]) 1);
paulson@2922
   264
  by (Blast_tac 1);
paulson@3457
   265
 by (dtac sym 1);
paulson@1553
   266
 by (rotate_tac ~1 1);
paulson@1553
   267
 by (Asm_full_simp_tac 1);
paulson@1553
   268
 by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
berghofe@1786
   269
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   270
  by (subgoal_tac "x ~= f m" 1);
paulson@2922
   271
   by (Blast_tac 2);
paulson@1553
   272
  by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   273
   by (Blast_tac 2);
berghofe@1786
   274
  by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   275
  by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   276
  by (Asm_simp_tac 1);
nipkow@3919
   277
 by (simp_tac (!simpset addsplits [expand_if]) 1);
paulson@2922
   278
 by (Blast_tac 1);
paulson@1553
   279
by (res_inst_tac [("x","%j. if f j = f i then f m else f j")] exI 1);
berghofe@1786
   280
by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   281
 by (subgoal_tac "x ~= f i" 1);
paulson@2922
   282
  by (Blast_tac 2);
paulson@1553
   283
 by (case_tac "x = f m" 1);
paulson@1553
   284
  by (res_inst_tac [("x","i")] exI 1);
paulson@1553
   285
  by (Asm_simp_tac 1);
paulson@1553
   286
 by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   287
  by (Blast_tac 2);
berghofe@1786
   288
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   289
 by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   290
 by (Asm_simp_tac 1);
nipkow@3919
   291
by (simp_tac (!simpset addsplits [expand_if]) 1);
paulson@2922
   292
by (Blast_tac 1);
nipkow@1531
   293
val lemma = result();
nipkow@1531
   294
nipkow@1531
   295
goal Finite.thy "!!A. [| finite A; x ~: A |] ==> \
wenzelm@3842
   296
\ (LEAST n. ? f. insert x A = {f i|i. i<n}) = Suc(LEAST n. ? f. A={f i|i. i<n})";
paulson@1553
   297
by (rtac Least_equality 1);
paulson@3457
   298
 by (dtac finite_has_card 1);
paulson@3457
   299
 by (etac exE 1);
wenzelm@3842
   300
 by (dres_inst_tac [("P","%n.? f. A={f i|i. i<n}")] LeastI 1);
paulson@3457
   301
 by (etac exE 1);
paulson@1553
   302
 by (res_inst_tac
nipkow@1531
   303
   [("x","%i. if i<(LEAST n. ? f. A={f i |i. i < n}) then f i else x")] exI 1);
paulson@1553
   304
 by (simp_tac
oheimb@1660
   305
    (!simpset addsimps [Collect_conv_insert, less_Suc_eq] 
paulson@2031
   306
              addcongs [rev_conj_cong]) 1);
paulson@3457
   307
 by (etac subst 1);
paulson@3457
   308
 by (rtac refl 1);
paulson@1553
   309
by (rtac notI 1);
paulson@1553
   310
by (etac exE 1);
paulson@1553
   311
by (dtac lemma 1);
paulson@3457
   312
 by (assume_tac 1);
paulson@1553
   313
by (etac exE 1);
paulson@1553
   314
by (etac conjE 1);
paulson@1553
   315
by (dres_inst_tac [("P","%x. ? g. A = {g i |i. i < x}")] Least_le 1);
paulson@1553
   316
by (dtac le_less_trans 1 THEN atac 1);
oheimb@1660
   317
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@1553
   318
by (etac disjE 1);
paulson@1553
   319
by (etac less_asym 1 THEN atac 1);
paulson@1553
   320
by (hyp_subst_tac 1);
paulson@1553
   321
by (Asm_full_simp_tac 1);
nipkow@1531
   322
val lemma = result();
nipkow@1531
   323
nipkow@1531
   324
goalw Finite.thy [card_def]
nipkow@1531
   325
  "!!A. [| finite A; x ~: A |] ==> card(insert x A) = Suc(card A)";
paulson@1553
   326
by (etac lemma 1);
paulson@1553
   327
by (assume_tac 1);
nipkow@1531
   328
qed "card_insert_disjoint";
paulson@3352
   329
Addsimps [card_insert_disjoint];
paulson@3352
   330
paulson@3352
   331
goal Finite.thy  "!!A. finite A ==> !B. B <= A --> card(B) <= card(A)";
paulson@3352
   332
by (etac finite_induct 1);
paulson@3352
   333
by (Simp_tac 1);
paulson@3708
   334
by (Clarify_tac 1);
paulson@3352
   335
by (case_tac "x:B" 1);
nipkow@3413
   336
 by (dres_inst_tac [("A","B")] mk_disjoint_insert 1);
paulson@3352
   337
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@3352
   338
 by (rotate_tac ~1 1);
paulson@3352
   339
 by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
paulson@3352
   340
by (rotate_tac ~1 1);
paulson@3352
   341
by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
paulson@3352
   342
qed_spec_mp "card_mono";
paulson@3352
   343
paulson@3352
   344
goal Finite.thy "!!A B. [| finite A; finite B |]\
paulson@3352
   345
\                       ==> A Int B = {} --> card(A Un B) = card A + card B";
paulson@3352
   346
by (etac finite_induct 1);
paulson@3352
   347
by (ALLGOALS 
paulson@3517
   348
    (asm_simp_tac (!simpset addsimps [Int_insert_left]
nipkow@3919
   349
	                    addsplits [expand_if])));
paulson@3352
   350
qed_spec_mp "card_Un_disjoint";
paulson@3352
   351
paulson@3352
   352
goal Finite.thy "!!A. [| finite A; B<=A |] ==> card A - card B = card (A - B)";
paulson@3352
   353
by (subgoal_tac "(A-B) Un B = A" 1);
paulson@3352
   354
by (Blast_tac 2);
paulson@3457
   355
by (rtac (add_right_cancel RS iffD1) 1);
paulson@3457
   356
by (rtac (card_Un_disjoint RS subst) 1);
paulson@3457
   357
by (etac ssubst 4);
paulson@3352
   358
by (Blast_tac 3);
paulson@3352
   359
by (ALLGOALS 
paulson@3352
   360
    (asm_simp_tac
paulson@3352
   361
     (!simpset addsimps [add_commute, not_less_iff_le, 
paulson@3352
   362
			 add_diff_inverse, card_mono, finite_subset])));
paulson@3352
   363
qed "card_Diff_subset";
nipkow@1531
   364
paulson@1618
   365
goal Finite.thy "!!A. [| finite A; x: A |] ==> Suc(card(A-{x})) = card A";
paulson@1618
   366
by (res_inst_tac [("t", "A")] (insert_Diff RS subst) 1);
paulson@1618
   367
by (assume_tac 1);
paulson@3352
   368
by (Asm_simp_tac 1);
paulson@1618
   369
qed "card_Suc_Diff";
paulson@1618
   370
paulson@1618
   371
goal Finite.thy "!!A. [| finite A; x: A |] ==> card(A-{x}) < card A";
paulson@2031
   372
by (rtac Suc_less_SucD 1);
paulson@1618
   373
by (asm_simp_tac (!simpset addsimps [card_Suc_Diff]) 1);
paulson@1618
   374
qed "card_Diff";
paulson@1618
   375
paulson@3389
   376
paulson@3389
   377
(*** Cardinality of the Powerset ***)
paulson@3389
   378
nipkow@1531
   379
val [major] = goal Finite.thy
nipkow@1531
   380
  "finite A ==> card(insert x A) = Suc(card(A-{x}))";
paulson@1553
   381
by (case_tac "x:A" 1);
paulson@1553
   382
by (asm_simp_tac (!simpset addsimps [insert_absorb]) 1);
paulson@1553
   383
by (dtac mk_disjoint_insert 1);
paulson@1553
   384
by (etac exE 1);
paulson@1553
   385
by (Asm_simp_tac 1);
paulson@1553
   386
by (rtac card_insert_disjoint 1);
paulson@1553
   387
by (rtac (major RSN (2,finite_subset)) 1);
paulson@2922
   388
by (Blast_tac 1);
paulson@2922
   389
by (Blast_tac 1);
paulson@1553
   390
by (asm_simp_tac (!simpset addsimps [major RS card_insert_disjoint]) 1);
nipkow@1531
   391
qed "card_insert";
nipkow@1531
   392
Addsimps [card_insert];
nipkow@1531
   393
paulson@3340
   394
goal Finite.thy "!!A. finite(A) ==> inj_onto f A --> card (f `` A) = card A";
paulson@3340
   395
by (etac finite_induct 1);
paulson@3340
   396
by (ALLGOALS Asm_simp_tac);
paulson@3724
   397
by Safe_tac;
paulson@3457
   398
by (rewtac inj_onto_def);
paulson@3340
   399
by (Blast_tac 1);
paulson@3340
   400
by (stac card_insert_disjoint 1);
paulson@3340
   401
by (etac finite_imageI 1);
paulson@3340
   402
by (Blast_tac 1);
paulson@3340
   403
by (Blast_tac 1);
paulson@3340
   404
qed_spec_mp "card_image";
paulson@3340
   405
paulson@3389
   406
goal thy "!!A. finite A ==> card (Pow A) = 2 ^ card A";
paulson@3389
   407
by (etac finite_induct 1);
paulson@3389
   408
by (ALLGOALS (asm_simp_tac (!simpset addsimps [Pow_insert])));
paulson@3389
   409
by (stac card_Un_disjoint 1);
paulson@3389
   410
by (EVERY (map (blast_tac (!claset addIs [finite_imageI])) [3,2,1]));
paulson@3389
   411
by (subgoal_tac "inj_onto (insert x) (Pow F)" 1);
paulson@3389
   412
by (asm_simp_tac (!simpset addsimps [card_image, Pow_insert]) 1);
paulson@3457
   413
by (rewtac inj_onto_def);
paulson@3389
   414
by (blast_tac (!claset addSEs [equalityE]) 1);
paulson@3389
   415
qed "card_Pow";
paulson@3389
   416
Addsimps [card_Pow];
paulson@3340
   417
paulson@3389
   418
paulson@3389
   419
(*Proper subsets*)
nipkow@3222
   420
goalw Finite.thy [psubset_def]
nipkow@3222
   421
"!!B. finite B ==> !A. A < B --> card(A) < card(B)";
nipkow@3222
   422
by (etac finite_induct 1);
nipkow@3222
   423
by (Simp_tac 1);
paulson@3708
   424
by (Clarify_tac 1);
nipkow@3222
   425
by (case_tac "x:A" 1);
nipkow@3222
   426
(*1*)
nipkow@3413
   427
by (dres_inst_tac [("A","A")]mk_disjoint_insert 1);
nipkow@3222
   428
by (etac exE 1);
nipkow@3222
   429
by (etac conjE 1);
nipkow@3222
   430
by (hyp_subst_tac 1);
nipkow@3222
   431
by (rotate_tac ~1 1);
nipkow@3222
   432
by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
paulson@3708
   433
by (Blast_tac 1);
nipkow@3222
   434
(*2*)
nipkow@3222
   435
by (rotate_tac ~1 1);
paulson@3708
   436
by (eres_inst_tac [("P","?a<?b")] notE 1);
nipkow@3222
   437
by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
nipkow@3222
   438
by (case_tac "A=F" 1);
paulson@3708
   439
by (ALLGOALS Asm_simp_tac);
nipkow@3222
   440
qed_spec_mp "psubset_card" ;
paulson@3368
   441
paulson@3368
   442
wenzelm@3430
   443
(*Relates to equivalence classes.   Based on a theorem of F. Kammueller's.
paulson@3368
   444
  The "finite C" premise is redundant*)
paulson@3368
   445
goal thy "!!C. finite C ==> finite (Union C) --> \
paulson@3368
   446
\          (! c : C. k dvd card c) -->  \
paulson@3368
   447
\          (! c1: C. ! c2: C. c1 ~= c2 --> c1 Int c2 = {}) \
paulson@3368
   448
\          --> k dvd card(Union C)";
paulson@3368
   449
by (etac finite_induct 1);
paulson@3368
   450
by (ALLGOALS Asm_simp_tac);
paulson@3708
   451
by (Clarify_tac 1);
paulson@3368
   452
by (stac card_Un_disjoint 1);
paulson@3368
   453
by (ALLGOALS
paulson@3368
   454
    (asm_full_simp_tac (!simpset
paulson@3368
   455
			 addsimps [dvd_add, disjoint_eq_subset_Compl])));
paulson@3368
   456
by (thin_tac "!c:F. ?PP(c)" 1);
paulson@3368
   457
by (thin_tac "!c:F. ?PP(c) & ?QQ(c)" 1);
paulson@3708
   458
by (Clarify_tac 1);
paulson@3368
   459
by (ball_tac 1);
paulson@3368
   460
by (Blast_tac 1);
paulson@3368
   461
qed_spec_mp "dvd_partition";
paulson@3368
   462